wxnviz.py 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. """!
  2. @package nviz.wxnviz
  3. @brief wxGUI 3D view mode (ctypes-based classes)
  4. This module implements 3D visualization mode for map display (ctypes
  5. required).
  6. List of classes:
  7. - wxnviz::Nviz
  8. - wxnviz::Texture
  9. - wxnviz::ImageTexture
  10. - wxnviz::TextTexture
  11. (C) 2008-2011 by the GRASS Development Team
  12. This program is free software under the GNU General Public License
  13. (>=v2). Read the file COPYING that comes with GRASS for details.
  14. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  15. @author Pythonized by Glynn Clements
  16. @author Anna Kratochvilova <KratochAnna seznam.cz> (Google SoC 2011)
  17. """
  18. import sys
  19. import locale
  20. import struct
  21. from math import sqrt
  22. try:
  23. from numpy import matrix
  24. except ImportError:
  25. msg = _("This module requires the NumPy module, which could not be "
  26. "imported. It probably is not installed (it's not part of the "
  27. "standard Python distribution). See the Numeric Python site "
  28. "(http://numpy.scipy.org) for information on downloading source or "
  29. "binaries.")
  30. print >> sys.stderr, "wxnviz.py: " + msg
  31. import wx
  32. from ctypes import *
  33. from grass.lib.gis import *
  34. from grass.lib.raster3d import *
  35. from grass.lib.vector import *
  36. from grass.lib.ogsf import *
  37. from grass.lib.nviz import *
  38. from grass.lib.raster import *
  39. from core.debug import Debug
  40. import grass.script as grass
  41. log = None
  42. progress = None
  43. def print_error(msg, type):
  44. """!Redirect stderr"""
  45. global log
  46. if log:
  47. log.write(msg)
  48. else:
  49. print msg
  50. return 0
  51. def print_progress(value):
  52. """!Redirect progress info"""
  53. global progress
  54. if progress:
  55. if not progress.GetRange() == 100:
  56. progress.SetRange(100)
  57. progress.SafeSetValue(value)
  58. else:
  59. print value
  60. return 0
  61. try:
  62. errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
  63. errfunc = errtype(print_error)
  64. pertype = CFUNCTYPE(UNCHECKED(c_int), c_int)
  65. perfunc = pertype(print_progress)
  66. except NameError:
  67. pass
  68. class Nviz(object):
  69. def __init__(self, glog, gprogress):
  70. """!Initialize Nviz class instance
  71. @param glog logging area
  72. @param gprogress progressbar
  73. """
  74. global errfunc, perfunc, log, progress
  75. log = glog
  76. progress = gprogress
  77. G_gisinit("wxnviz")
  78. # gislib is already initialized (where?)
  79. G_set_error_routine(errfunc)
  80. G_set_percent_routine(perfunc)
  81. self.Init()
  82. self.data_obj = nv_data()
  83. self.data = pointer(self.data_obj)
  84. self.color_obj = Colors()
  85. self.color = pointer(self.color_obj)
  86. self.width = self.height = -1
  87. self.showLight = False
  88. Debug.msg(1, "Nviz::Nviz()")
  89. def __del__(self):
  90. """!Destroy Nviz class instance"""
  91. G_unset_error_routine()
  92. G_unset_percent_routine()
  93. del self.data
  94. del self.data_obj
  95. self.log = None
  96. def Init(self):
  97. """!Initialize window"""
  98. locale.setlocale(locale.LC_NUMERIC, 'C')
  99. G_unset_window()
  100. Rast_unset_window()
  101. Rast__init_window()
  102. GS_libinit()
  103. GVL_libinit()
  104. GVL_init_region()
  105. def ResizeWindow(self, width, height):
  106. """!GL canvas resized
  107. @param width window width
  108. @param height window height
  109. @return 1 on success
  110. @return 0 on failure (window resized by default to 20x20 px)
  111. """
  112. self.width = width
  113. self.height = height
  114. Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
  115. width, height)
  116. return Nviz_resize_window(width, height)
  117. def GetLongDim(self):
  118. """!Get longest dimension, used for initial size of north arrow"""
  119. return Nviz_get_longdim(self.data)
  120. def SetViewDefault(self):
  121. """!Set default view (based on loaded data)
  122. @return z-exag value, default, min and max height
  123. """
  124. # determine z-exag
  125. z_exag = Nviz_get_exag()
  126. Nviz_change_exag(self.data, z_exag)
  127. # determine height
  128. hdef = c_double()
  129. hmin = c_double()
  130. hmax = c_double()
  131. Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
  132. Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
  133. hdef.value, hmin.value, hmax.value)
  134. return (z_exag, hdef.value, hmin.value, hmax.value)
  135. def SetView(self, x, y, height, persp, twist):
  136. """!Change view settings
  137. @param x,y position
  138. @param height
  139. @param persp perpective
  140. @param twist
  141. """
  142. Nviz_set_viewpoint_height(height)
  143. Nviz_set_viewpoint_position(x, y)
  144. Nviz_set_viewpoint_twist(twist)
  145. Nviz_set_viewpoint_persp(persp)
  146. Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
  147. x, y, height, persp, twist)
  148. def GetViewpointPosition(self):
  149. x = c_double()
  150. y = c_double()
  151. h = c_double()
  152. Nviz_get_viewpoint_height(byref(h))
  153. Nviz_get_viewpoint_position(byref(x), byref(y))
  154. return (x.value, y.value, h.value)
  155. def LookHere(self, x, y):
  156. """!Look here feature
  157. @param x,y screen coordinates
  158. """
  159. Nviz_look_here(x, y)
  160. Debug.msg(3, "Nviz::LookHere(): x=%f, y=%f", x, y)
  161. def LookAtCenter(self):
  162. """!Center view at center of displayed surface"""
  163. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  164. Debug.msg(3, "Nviz::LookAtCenter()")
  165. def GetFocus(self):
  166. """!Get focus"""
  167. Debug.msg(3, "Nviz::GetFocus()")
  168. if Nviz_has_focus(self.data):
  169. x = c_float()
  170. y = c_float()
  171. z = c_float()
  172. Nviz_get_focus(self.data, byref(x), byref(y), byref(z))
  173. return x.value, y.value, z.value
  174. else:
  175. return -1, -1, -1
  176. def SetFocus(self, x, y, z):
  177. """!Set focus"""
  178. Debug.msg(3, "Nviz::SetFocus()")
  179. Nviz_set_focus(self.data, x, y, z)
  180. def GetViewdir(self):
  181. """!Get viewdir"""
  182. Debug.msg(3, "Nviz::GetViewdir()")
  183. dir = (c_float * 3)()
  184. GS_get_viewdir(byref(dir))
  185. return dir[0], dir[1], dir[2]
  186. def SetViewdir(self, x, y, z):
  187. """!Set viewdir"""
  188. Debug.msg(3, "Nviz::SetViewdir(): x=%f, y=%f, z=%f" % (x, y, z))
  189. dir = (c_float * 3)()
  190. for i, coord in enumerate((x, y, z)):
  191. dir[i] = coord
  192. GS_set_viewdir(byref(dir))
  193. def SetZExag(self, z_exag):
  194. """!Set z-exag value
  195. @param z_exag value
  196. @return 1
  197. """
  198. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  199. return Nviz_change_exag(self.data, z_exag)
  200. def Draw(self, quick, quick_mode):
  201. """!Draw canvas
  202. Draw quick mode:
  203. - DRAW_QUICK_SURFACE
  204. - DRAW_QUICK_VLINES
  205. - DRAW_QUICK_VPOINTS
  206. - DRAW_QUICK_VOLUME
  207. @param quick if true draw in wiremode
  208. @param quick_mode quick mode
  209. """
  210. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  211. Nviz_draw_cplane(self.data, -1, -1) # ?
  212. if quick:
  213. Nviz_draw_quick(self.data, quick_mode)
  214. else:
  215. Nviz_draw_all(self.data)
  216. def EraseMap(self):
  217. """!Erase map display (with background color)
  218. """
  219. Debug.msg(1, "Nviz::EraseMap()")
  220. GS_clear(Nviz_get_bgcolor(self.data))
  221. def InitView(self):
  222. """!Initialize view"""
  223. # initialize nviz data
  224. Nviz_init_data(self.data)
  225. # define default attributes for map objects
  226. Nviz_set_surface_attr_default()
  227. # set background color
  228. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white"))
  229. GS_clear(Nviz_get_bgcolor(self.data))
  230. # initialize view, lights
  231. Nviz_init_view(self.data)
  232. Debug.msg(1, "Nviz::InitView()")
  233. def SetBgColor(self, color_str):
  234. """!Set background color
  235. @param color_str color string
  236. """
  237. Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
  238. def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
  239. """!Change lighting settings
  240. @param x,y,z position
  241. @param color light color (as string)
  242. @param bright light brightness
  243. @param ambient light ambient
  244. @param w local coordinate (default to 0)
  245. @param lid light id
  246. """
  247. Nviz_set_light_position(self.data, lid, x, y, z, w)
  248. Nviz_set_light_bright(self.data, lid, bright)
  249. Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
  250. Nviz_set_light_ambient(self.data, lid, ambient)
  251. def LoadSurface(self, name, color_name, color_value):
  252. """!Load raster map (surface)
  253. @param name raster map name
  254. @param color_name raster map for color (None for color_value)
  255. @param color_value color string (named color or RGB triptet)
  256. @return object id
  257. @return -1 on failure
  258. """
  259. mapset = G_find_raster2(name, "")
  260. if mapset is None:
  261. G_warning(_("Raster map <%s> not found"), name)
  262. return -1
  263. # topography
  264. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  265. G_fully_qualified_name(name, mapset), 0.0,
  266. self.data)
  267. if color_name: # check for color map
  268. mapset = G_find_raster2(color_name, "")
  269. if mapset is None:
  270. G_warning(_("Raster map <%s> not found"), color_name)
  271. GS_delete_surface(id)
  272. return -1
  273. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  274. G_fully_qualified_name(color_name, mapset), -1.0,
  275. self.data)
  276. elif color_value: # check for color value
  277. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  278. None, Nviz_color_from_str(color_value),
  279. self.data)
  280. else: # use by default elevation map for coloring
  281. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  282. G_fully_qualified_name(name, mapset), -1.0,
  283. self.data)
  284. # if (i > 1)
  285. # set_default_wirecolors(self.data, i)
  286. # focus on loaded self.data
  287. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  288. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  289. return id
  290. def AddConstant(self, value, color):
  291. """!Add new constant surface"""
  292. id = Nviz_new_map_obj(MAP_OBJ_SURF, None, value, self.data)
  293. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  294. None, Nviz_color_from_str(color),
  295. self.data)
  296. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  297. Debug.msg(1, "Nviz::AddConstant(): id=%d", id)
  298. return id
  299. def UnloadSurface(self, id):
  300. """!Unload surface
  301. @param id surface id
  302. @return 1 on success
  303. @return 0 on failure
  304. """
  305. if not GS_surf_exists(id):
  306. return 0
  307. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  308. if GS_delete_surface(id) < 0:
  309. return 0
  310. return 1
  311. def LoadVector(self, name, points):
  312. """!Load vector map overlay
  313. @param name vector map name
  314. @param points if true load 2d points rather then 2d lines
  315. @return object id, id of base surface (or -1 if it is not loaded)
  316. @return -1 on failure
  317. """
  318. baseId = -1
  319. if GS_num_surfs() == 0: # load base surface if no loaded
  320. baseId = Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  321. nsurf = c_int()
  322. surf_list = GS_get_surf_list(byref(nsurf))
  323. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  324. mapset = G_find_vector2 (name, "")
  325. if mapset is None:
  326. G_warning(_("Vector map <%s> not found"),
  327. name)
  328. if points:
  329. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  330. G_fully_qualified_name(name, mapset), 0.0,
  331. self.data)
  332. else:
  333. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  334. G_fully_qualified_name(name, mapset), 0.0,
  335. self.data)
  336. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  337. return id, baseId
  338. def UnloadVector(self, id, points):
  339. """!Unload vector set
  340. @param id vector set id
  341. @param points vector points or lines set
  342. @return 1 on success
  343. @return 0 on failure
  344. """
  345. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  346. if points:
  347. if not GP_site_exists(id):
  348. return 0
  349. if GP_delete_site(id) < 0:
  350. return 0
  351. else:
  352. if not GV_vect_exists(id):
  353. return 0
  354. if GV_delete_vector(id) < 0:
  355. return 0
  356. return 1
  357. def VectorSurfaceSelected(self, vid, sid):
  358. """!Check if surface is selected (currently unused)
  359. @param vid vector id
  360. @param sid surface id
  361. @return True if selected
  362. @return False if not selected
  363. """
  364. selected = GV_surf_is_selected(vid, sid)
  365. Debug.msg(1, "Nviz::VectorSurfaceSelected(): vid=%s, sid=%d -> selected=%d", vid, sid, selected)
  366. return selected
  367. def LoadVolume(self, name, color_name, color_value):
  368. """!Load 3d raster map (volume)
  369. @param name 3d raster map name
  370. @param color_name 3d raster map for color (None for color_value)
  371. @param color_value color string (named color or RGB triptet)
  372. @return object id
  373. @return -1 on failure
  374. """
  375. mapset = G_find_raster3d(name, "")
  376. if mapset is None:
  377. G_warning(_("3d raster map <%s> not found"),
  378. name)
  379. return -1
  380. # topography
  381. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  382. G_fully_qualified_name(name, mapset), 0.0,
  383. self.data)
  384. if color_name: # check for color map
  385. mapset = G_find_raster3d(color_name, "")
  386. if mapset is None:
  387. G_warning(_("3d raster map <%s> not found"),
  388. color_name)
  389. GVL_delete_vol(id)
  390. return -1
  391. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  392. G_fully_qualified_name(color_name, mapset), -1.0,
  393. self.data)
  394. elif color_value: # check for color value
  395. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  396. None, Nviz_color_from_str(color_value),
  397. self.data)
  398. else: # use by default elevation map for coloring
  399. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  400. G_fully_qualified_name(name, mapset), -1.0,
  401. self.data)
  402. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  403. return id
  404. def UnloadVolume(self, id):
  405. """!Unload volume
  406. @param id volume id
  407. @return 1 on success
  408. @return 0 on failure
  409. """
  410. if not GVL_vol_exists(id):
  411. return 0
  412. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  413. if GVL_delete_vol(id) < 0:
  414. return 0
  415. return 1
  416. def SetSurfaceTopo(self, id, map, value):
  417. """!Set surface topography
  418. @param id surface id
  419. @param map if true use map otherwise constant
  420. @param value map name of value
  421. @return 1 on success
  422. @return -1 surface not found
  423. @return -2 setting attributes failed
  424. """
  425. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  426. def SetSurfaceColor(self, id, map, value):
  427. """!Set surface color
  428. @param id surface id
  429. @param map if true use map otherwise constant
  430. @param value map name or value
  431. @return 1 on success
  432. @return -1 surface not found
  433. @return -2 setting attributes failed
  434. """
  435. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  436. def SetSurfaceMask(self, id, invert, value):
  437. """!Set surface mask
  438. @todo invert
  439. @param id surface id
  440. @param invert if true invert mask
  441. @param value map name of value
  442. @return 1 on success
  443. @return -1 surface not found
  444. @return -2 setting attributes failed
  445. """
  446. return self.SetSurfaceAttr(id, ATT_MASK, True, value)
  447. def SetSurfaceTransp(self, id, map, value):
  448. """!Set surface mask
  449. @todo invert
  450. @param id surface id
  451. @param map if true use map otherwise constant
  452. @param value map name of value
  453. @return 1 on success
  454. @return -1 surface not found
  455. @return -2 setting attributes failed
  456. """
  457. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  458. def SetSurfaceShine(self, id, map, value):
  459. """!Set surface shininess
  460. @param id surface id
  461. @param map if true use map otherwise constant
  462. @param value map name of value
  463. @return 1 on success
  464. @return -1 surface not found
  465. @return -2 setting attributes failed
  466. """
  467. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  468. def SetSurfaceEmit(self, id, map, value):
  469. """!Set surface emission (currently unused)
  470. @param id surface id
  471. @param map if true use map otherwise constant
  472. @param value map name of value
  473. @return 1 on success
  474. @return -1 surface not found
  475. @return -2 setting attributes failed
  476. """
  477. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  478. def SetSurfaceAttr(self, id, attr, map, value):
  479. """!Set surface attribute
  480. @param id surface id
  481. @param attr attribute desc
  482. @param map if true use map otherwise constant
  483. @param value map name of value
  484. @return 1 on success
  485. @return -1 surface not found
  486. @return -2 setting attributes failed
  487. """
  488. if not GS_surf_exists(id):
  489. return -1
  490. if map:
  491. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  492. value, -1.0, self.data)
  493. else:
  494. if attr == ATT_COLOR:
  495. val = Nviz_color_from_str(value)
  496. else:
  497. val = float(value)
  498. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  499. None, val, self.data)
  500. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  501. id, attr, map, value)
  502. if ret < 0:
  503. return -2
  504. return 1
  505. def UnsetSurfaceMask(self, id):
  506. """!Unset surface mask
  507. @param id surface id
  508. @return 1 on success
  509. @return -1 surface not found
  510. @return -2 setting attributes failed
  511. @return -1 on failure
  512. """
  513. return self.UnsetSurfaceAttr(id, ATT_MASK)
  514. def UnsetSurfaceTransp(self, id):
  515. """!Unset surface transparency
  516. @param id surface id
  517. @return 1 on success
  518. @return -1 surface not found
  519. @return -2 setting attributes failed
  520. """
  521. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  522. def UnsetSurfaceEmit(self, id):
  523. """!Unset surface emission (currently unused)
  524. @param id surface id
  525. @return 1 on success
  526. @return -1 surface not found
  527. @return -2 setting attributes failed
  528. """
  529. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  530. def UnsetSurfaceAttr(self, id, attr):
  531. """!Unset surface attribute
  532. @param id surface id
  533. @param attr attribute descriptor
  534. @return 1 on success
  535. @return -1 surface not found
  536. @return -2 setting attributes failed
  537. """
  538. if not GS_surf_exists(id):
  539. return -1
  540. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  541. id, attr)
  542. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  543. if ret < 0:
  544. return -2
  545. return 1
  546. def SetSurfaceRes(self, id, fine, coarse):
  547. """!Set surface resolution
  548. @param id surface id
  549. @param fine x/y fine resolution
  550. @param coarse x/y coarse resolution
  551. @return 1 on success
  552. @return -1 surface not found
  553. @return -2 setting attributes failed
  554. """
  555. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  556. id, fine, coarse)
  557. if id > 0:
  558. if not GS_surf_exists(id):
  559. return -1
  560. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  561. return -2
  562. else:
  563. GS_setall_drawres(fine, fine, coarse, coarse)
  564. return 1
  565. def SetSurfaceStyle(self, id, style):
  566. """!Set draw style
  567. Draw styles:
  568. - DM_GOURAUD
  569. - DM_FLAT
  570. - DM_FRINGE
  571. - DM_WIRE
  572. - DM_COL_WIRE
  573. - DM_POLY
  574. - DM_WIRE_POLY
  575. - DM_GRID_WIRE
  576. - DM_GRID_SURF
  577. @param id surface id (<= 0 for all)
  578. @param style draw style
  579. @return 1 on success
  580. @return -1 surface not found
  581. @return -2 setting attributes failed
  582. """
  583. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  584. id, style)
  585. if id > 0:
  586. if not GS_surf_exists(id):
  587. return -1
  588. if GS_set_drawmode(id, style) < 0:
  589. return -2
  590. return 1
  591. if GS_setall_drawmode(style) < 0:
  592. return -2
  593. return 1
  594. def SetWireColor(self, id, color_str):
  595. """!Set color of wire
  596. @todo all
  597. @param id surface id (< 0 for all)
  598. @param color_str color string (R:G:B)
  599. @return 1 on success
  600. @return -1 surface not found
  601. @return -2 setting attributes failed
  602. @return 1 on success
  603. @return 0 on failure
  604. """
  605. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  606. id, color_str)
  607. color = Nviz_color_from_str(color_str)
  608. if id > 0:
  609. if not GS_surf_exists(id):
  610. return -1
  611. GS_set_wire_color(id, color)
  612. else:
  613. nsurfs = c_int()
  614. surf_list = GS_get_surf_list(byref(nsurfs))
  615. for i in xrange(nsurfs.value):
  616. id = surf_list[i]
  617. GS_set_wire_color(id, color)
  618. G_free(surf_list)
  619. surf_list = None
  620. return 1
  621. def GetSurfacePosition(self, id):
  622. """!Get surface position
  623. @param id surface id
  624. @return x,y,z
  625. @return zero-length vector on error
  626. """
  627. if not GS_surf_exists(id):
  628. return []
  629. x, y, z = c_float(), c_float(), c_float()
  630. GS_get_trans(id, byref(x), byref(y), byref(z))
  631. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  632. id, x.value, y.value, z.value)
  633. return [x.value, y.value, z.value]
  634. def SetSurfacePosition(self, id, x, y, z):
  635. """!Set surface position
  636. @param id surface id
  637. @param x,y,z translation values
  638. @return 1 on success
  639. @return -1 surface not found
  640. @return -2 setting position failed
  641. """
  642. if not GS_surf_exists(id):
  643. return -1
  644. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  645. id, x, y, z)
  646. GS_set_trans(id, x, y, z)
  647. return 1
  648. def SetVectorLineMode(self, id, color_str, width, flat):
  649. """!Set mode of vector line overlay
  650. @param id vector id
  651. @param color_str color string
  652. @param width line width
  653. @param flat display flat or on surface
  654. @return -1 vector set not found
  655. @return -2 on failure
  656. @return 1 on success
  657. """
  658. if not GV_vect_exists(id):
  659. return -1
  660. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  661. id, color_str, width, flat)
  662. color = Nviz_color_from_str(color_str)
  663. # use memory by default
  664. if GV_set_style(id, 1, color, width, flat) < 0:
  665. return -2
  666. return 1
  667. def SetVectorLineHeight(self, id, height):
  668. """!Set vector height above surface (lines)
  669. @param id vector set id
  670. @param height
  671. @return -1 vector set not found
  672. @return 1 on success
  673. """
  674. if not GV_vect_exists(id):
  675. return -1
  676. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  677. id, height)
  678. GV_set_trans(id, 0.0, 0.0, height)
  679. return 1
  680. def SetVectorLineSurface(self, id, surf_id):
  681. """!Set reference surface of vector set (lines)
  682. @param id vector set id
  683. @param surf_id surface id
  684. @return 1 on success
  685. @return -1 vector set not found
  686. @return -2 surface not found
  687. @return -3 on failure
  688. """
  689. if not GV_vect_exists(id):
  690. return -1
  691. if not GS_surf_exists(surf_id):
  692. return -2
  693. if GV_select_surf(id, surf_id) < 0:
  694. return -3
  695. return 1
  696. def UnsetVectorLineSurface(self, id, surf_id):
  697. """!Unset reference surface of vector set (lines)
  698. @param id vector set id
  699. @param surf_id surface id
  700. @return 1 on success
  701. @return -1 vector set not found
  702. @return -2 surface not found
  703. @return -3 on failure
  704. """
  705. if not GV_vect_exists(id):
  706. return -1
  707. if not GS_surf_exists(surf_id):
  708. return -2
  709. if GV_unselect_surf(id, surf_id) < 0:
  710. return -3
  711. return 1
  712. def SetVectorPointMode(self, id, color_str, width, size, marker):
  713. """!Set mode of vector point overlay
  714. @param id vector id
  715. @param color_str color string
  716. @param width line width
  717. @param size size of the symbol
  718. @param marker type of the symbol
  719. @return -1 vector set not found
  720. """
  721. if not GP_site_exists(id):
  722. return -1
  723. # dtree and ctree defined but not used
  724. if marker > 5:
  725. marker += 2
  726. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  727. "width=%d, size=%f, marker=%d",
  728. id, color_str, width, size, marker)
  729. color = Nviz_color_from_str(color_str)
  730. if GP_set_style(id, color, width, size, marker) < 0:
  731. return -2
  732. return 1
  733. def SetVectorPointHeight(self, id, height):
  734. """!Set vector height above surface (points)
  735. @param id vector set id
  736. @param height
  737. @return -1 vector set not found
  738. @return 1 on success
  739. """
  740. if not GP_site_exists(id):
  741. return -1
  742. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  743. id, height)
  744. GP_set_trans(id, 0.0, 0.0, height)
  745. return 1
  746. def SetVectorPointSurface(self, id, surf_id):
  747. """!Set reference surface of vector set (points)
  748. @param id vector set id
  749. @param surf_id surface id
  750. @return 1 on success
  751. @return -1 vector set not found
  752. @return -2 surface not found
  753. @return -3 on failure
  754. """
  755. if not GP_site_exists(id):
  756. return -1
  757. if not GS_surf_exists(surf_id):
  758. return -2
  759. if GP_select_surf(id, surf_id) < 0:
  760. return -3
  761. return 1
  762. def ReadVectorColors(self, name, mapset):
  763. """!Read vector colors
  764. @param name vector map name
  765. @param mapset mapset name (empty string (\c "") for search path)
  766. @return -1 on error
  767. @return 0 if color table missing
  768. @return 1 on success (color table found)
  769. """
  770. return Vect_read_colors(name, mapset, self.color)
  771. def CheckColorTable(self, id, type):
  772. """!Check if color table exists.
  773. @param id vector set id
  774. @param type vector set type (lines/points)
  775. @return 1 color table exists
  776. @return 0 no color table found
  777. @return -1 on error
  778. @return -2 vector set not found
  779. """
  780. file = c_char_p()
  781. if type == 'points':
  782. ret = GP_get_sitename(id, byref(file))
  783. elif type == 'lines':
  784. ret = GV_get_vectname(id, byref(file))
  785. if ret < 0:
  786. return -2
  787. return self.ReadVectorColors(file, "")
  788. def SetPointsStyleThematic(self, id, layer, color = None, colorTable = False,
  789. width = None, size = None, symbol = None):
  790. """!Set thematic style for vector points
  791. @param id vector set id
  792. @param layer layer number for thematic mapping
  793. @param colorTable use color table
  794. @param color color column name
  795. @param width width column name
  796. @param size size column name
  797. @param symbol symbol column name
  798. """
  799. file = c_char_p()
  800. ret = GP_get_sitename(id, byref(file))
  801. if ret < 0:
  802. return -1
  803. ret = self.ReadVectorColors(file, "")
  804. if ret < 0:
  805. return -1
  806. if colorTable:
  807. GP_set_style_thematic(id, layer, color, width, size, symbol, self.color)
  808. else:
  809. GP_set_style_thematic(id, layer, color, width, size, symbol, None)
  810. def SetLinesStyleThematic(self, id, layer, color = None, colorTable = False, width = None):
  811. """!Set thematic style for vector lines
  812. @param id vector set id
  813. @param layer layer number for thematic mapping
  814. @param color color column name
  815. @param colorTable use color table
  816. @param width width column name
  817. """
  818. file = c_char_p()
  819. ret = GV_get_vectname(id, byref(file))
  820. if ret < 0:
  821. return -1
  822. ret = self.ReadVectorColors(file, "")
  823. if ret < 0:
  824. return -1
  825. if colorTable:
  826. GV_set_style_thematic(id, layer, color, width, self.color)
  827. else:
  828. GV_set_style_thematic(id, layer, color, width, None)
  829. def UnsetLinesStyleThematic(self, id):
  830. """!Unset thematic style for vector points"""
  831. GV_unset_style_thematic(id)
  832. def UnsetPointsStyleThematic(self, id):
  833. """!Unset thematic style for vector lines"""
  834. GP_unset_style_thematic(id)
  835. def UnsetVectorPointSurface(self, id, surf_id):
  836. """!Unset reference surface of vector set (points)
  837. @param id vector set id
  838. @param surf_id surface id
  839. @return 1 on success
  840. @return -1 vector set not found
  841. @return -2 surface not found
  842. @return -3 on failure
  843. """
  844. if not GP_site_exists(id):
  845. return -1
  846. if not GS_surf_exists(surf_id):
  847. return -2
  848. if GP_unselect_surf(id, surf_id) < 0:
  849. return -3
  850. return 1
  851. def AddIsosurface(self, id, level, isosurf_id = None):
  852. """!Add new isosurface
  853. @param id volume id
  854. @param level isosurface level (topography)
  855. @param isosurf_id isosurface id
  856. @return -1 on failure
  857. @return 1 on success
  858. """
  859. if not GVL_vol_exists(id):
  860. return -1
  861. if isosurf_id is not None:
  862. num = GVL_isosurf_num_isosurfs(id)
  863. if num < 0 or isosurf_id != num:
  864. return -1
  865. if GVL_isosurf_add(id) < 0:
  866. return -1
  867. # set topography level
  868. nisosurfs = GVL_isosurf_num_isosurfs(id)
  869. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  870. def AddSlice(self, id, slice_id = None):
  871. """!Add new slice
  872. @param id volume id
  873. @param slice_id slice id
  874. @return -1 on failure
  875. @return number of slices
  876. """
  877. if not GVL_vol_exists(id):
  878. return -1
  879. if slice_id is not None:
  880. num = GVL_slice_num_slices(id)
  881. if num < 0 or slice_id != num:
  882. return -1
  883. if GVL_slice_add(id) < 0:
  884. return -1
  885. return GVL_slice_num_slices(id)
  886. def DeleteIsosurface(self, id, isosurf_id):
  887. """!Delete isosurface
  888. @param id volume id
  889. @param isosurf_id isosurface id
  890. @return 1 on success
  891. @return -1 volume not found
  892. @return -2 isosurface not found
  893. @return -3 on failure
  894. """
  895. if not GVL_vol_exists(id):
  896. return -1
  897. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  898. return -2
  899. ret = GVL_isosurf_del(id, isosurf_id)
  900. if ret < 0:
  901. return -3
  902. return 1
  903. def DeleteSlice(self, id, slice_id):
  904. """!Delete slice
  905. @param id volume id
  906. @param slice_id slice id
  907. @return 1 on success
  908. @return -1 volume not found
  909. @return -2 slice not found
  910. @return -3 on failure
  911. """
  912. if not GVL_vol_exists(id):
  913. return -1
  914. if slice_id > GVL_slice_num_slices(id):
  915. return -2
  916. ret = GVL_slice_del(id, slice_id)
  917. if ret < 0:
  918. return -3
  919. return 1
  920. def MoveIsosurface(self, id, isosurf_id, up):
  921. """!Move isosurface up/down in the list
  922. @param id volume id
  923. @param isosurf_id isosurface id
  924. @param up if true move up otherwise down
  925. @return 1 on success
  926. @return -1 volume not found
  927. @return -2 isosurface not found
  928. @return -3 on failure
  929. """
  930. if not GVL_vol_exists(id):
  931. return -1
  932. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  933. return -2
  934. if up:
  935. ret = GVL_isosurf_move_up(id, isosurf_id)
  936. else:
  937. ret = GVL_isosurf_move_down(id, isosurf_id)
  938. if ret < 0:
  939. return -3
  940. return 1
  941. def MoveSlice(self, id, slice_id, up):
  942. """!Move slice up/down in the list
  943. @param id volume id
  944. @param slice_id slice id
  945. @param up if true move up otherwise down
  946. @return 1 on success
  947. @return -1 volume not found
  948. @return -2 slice not found
  949. @return -3 on failure
  950. """
  951. if not GVL_vol_exists(id):
  952. return -1
  953. if slice_id > GVL_slice_num_slices(id):
  954. return -2
  955. if up:
  956. ret = GVL_slice_move_up(id, slice_id)
  957. else:
  958. ret = GVL_slice_move_down(id, slice_id)
  959. if ret < 0:
  960. return -3
  961. return 1
  962. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  963. """!Set isosurface level
  964. @param id volume id
  965. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  966. @param map if true use map otherwise constant
  967. @param value map name of value
  968. @return 1 on success
  969. @return -1 volume not found
  970. @return -2 isosurface not found
  971. @return -3 on failure
  972. """
  973. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  974. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  975. """!Set isosurface color
  976. @param id volume id
  977. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  978. @param map if true use map otherwise constant
  979. @param value map name of value
  980. @return 1 on success
  981. @return -1 volume not found
  982. @return -2 isosurface not found
  983. @return -3 on failure
  984. """
  985. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  986. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  987. """!Set isosurface mask
  988. @todo invert
  989. @param id volume id
  990. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  991. @param invert true for invert mask
  992. @param value map name to be used for mask
  993. @return 1 on success
  994. @return -1 volume not found
  995. @return -2 isosurface not found
  996. @return -3 on failure
  997. """
  998. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  999. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  1000. """!Set isosurface transparency
  1001. @param id volume id
  1002. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1003. @param map if true use map otherwise constant
  1004. @param value map name of value
  1005. @return 1 on success
  1006. @return -1 volume not found
  1007. @return -2 isosurface not found
  1008. @return -3 on failure
  1009. """
  1010. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  1011. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  1012. """!Set isosurface shininess
  1013. @param id volume id
  1014. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1015. @param map if true use map otherwise constant
  1016. @param value map name of value
  1017. @return 1 on success
  1018. @return -1 volume not found
  1019. @return -2 isosurface not found
  1020. @return -3 on failure
  1021. """
  1022. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  1023. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  1024. """!Set isosurface emission (currently unused)
  1025. @param id volume id
  1026. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1027. @param map if true use map otherwise constant
  1028. @param value map name of value
  1029. @return 1 on success
  1030. @return -1 volume not found
  1031. @return -2 isosurface not found
  1032. @return -3 on failure
  1033. """
  1034. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  1035. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  1036. """!Set isosurface attribute
  1037. @param id volume id
  1038. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1039. @param attr attribute desc
  1040. @param map if true use map otherwise constant
  1041. @param value map name of value
  1042. @return 1 on success
  1043. @return -1 volume not found
  1044. @return -2 isosurface not found
  1045. @return -3 setting attributes failed
  1046. """
  1047. if not GVL_vol_exists(id):
  1048. return -1
  1049. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1050. return -2
  1051. if map:
  1052. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  1053. else:
  1054. if attr == ATT_COLOR:
  1055. val = Nviz_color_from_str(value)
  1056. else:
  1057. val = float(value)
  1058. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  1059. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  1060. "attr=%d, map=%s, value=%s",
  1061. id, isosurf_id, attr, map, value)
  1062. if ret < 0:
  1063. return -2
  1064. return 1
  1065. def UnsetIsosurfaceMask(self, id, isosurf_id):
  1066. """!Unset isosurface mask
  1067. @param id volume id
  1068. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1069. @return 1 on success
  1070. @return -1 volume not found
  1071. @return -2 isosurface not found
  1072. @return -3 setting attributes failed
  1073. """
  1074. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  1075. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  1076. """!Unset isosurface transparency
  1077. @param id volume id
  1078. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1079. @return 1 on success
  1080. @return -1 volume not found
  1081. @return -2 isosurface not found
  1082. @return -3 setting attributes failed
  1083. """
  1084. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  1085. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  1086. """!Unset isosurface emission (currently unused)
  1087. @param id volume id
  1088. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1089. @return 1 on success
  1090. @return -1 volume not found
  1091. @return -2 isosurface not found
  1092. @return -3 setting attributes failed
  1093. """
  1094. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  1095. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  1096. """!Unset surface attribute
  1097. @param id surface id
  1098. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1099. @param attr attribute descriptor
  1100. @return 1 on success
  1101. @return -1 volume not found
  1102. @return -2 isosurface not found
  1103. @return -2 on failure
  1104. """
  1105. if not GVL_vol_exists(id):
  1106. return -1
  1107. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1108. return -2
  1109. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  1110. id, isosurf_id, attr)
  1111. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  1112. if ret < 0:
  1113. return -2
  1114. return 1
  1115. def SetIsosurfaceMode(self, id, mode):
  1116. """!Set draw mode for isosurfaces
  1117. @param id isosurface id
  1118. @param mode isosurface draw mode
  1119. @return 1 on success
  1120. @return -1 volume set not found
  1121. @return -2 on failure
  1122. """
  1123. if not GVL_vol_exists(id):
  1124. return -1
  1125. ret = GVL_isosurf_set_drawmode(id, mode)
  1126. if ret < 0:
  1127. return -2
  1128. return 1
  1129. def SetSliceMode(self, id, mode):
  1130. """!Set draw mode for slices
  1131. @param id slice id
  1132. @param mode slice draw mode
  1133. @return 1 on success
  1134. @return -1 volume set not found
  1135. @return -2 on failure
  1136. """
  1137. if not GVL_vol_exists(id):
  1138. return -1
  1139. ret = GVL_slice_set_drawmode(id, mode)
  1140. if ret < 0:
  1141. return -2
  1142. return 1
  1143. def SetIsosurfaceRes(self, id, res):
  1144. """!Set draw resolution for isosurfaces
  1145. @param id isosurface id
  1146. @param res resolution value
  1147. @return 1 on success
  1148. @return -1 volume set not found
  1149. @return -2 on failure
  1150. """
  1151. if not GVL_vol_exists(id):
  1152. return -1
  1153. ret = GVL_isosurf_set_drawres(id, res, res, res)
  1154. if ret < 0:
  1155. return -2
  1156. return 1
  1157. def SetSliceRes(self, id, res):
  1158. """!Set draw resolution for slices
  1159. @param id slice id
  1160. @param res resolution value
  1161. @return 1 on success
  1162. @return -1 volume set not found
  1163. @return -2 on failure
  1164. """
  1165. if not GVL_vol_exists(id):
  1166. return -1
  1167. ret = GVL_slice_set_drawres(id, res, res, res)
  1168. if ret < 0:
  1169. return -2
  1170. return 1
  1171. def SetSlicePosition(self, id, slice_id, x1, x2, y1, y2, z1, z2, dir):
  1172. """!Set slice position
  1173. @param id volume id
  1174. @param slice_id slice id
  1175. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1176. @param dir axis
  1177. @return 1 on success
  1178. @return -1 volume not found
  1179. @return -2 slice not found
  1180. @return -3 on failure
  1181. """
  1182. if not GVL_vol_exists(id):
  1183. return -1
  1184. if slice_id > GVL_slice_num_slices(id):
  1185. return -2
  1186. ret = GVL_slice_set_pos(id, slice_id, x1, x2, y1, y2, z1, z2, dir)
  1187. if ret < 0:
  1188. return -2
  1189. return 1
  1190. def SetSliceTransp(self, id, slice_id, value):
  1191. """!Set slice transparency
  1192. @param id volume id
  1193. @param slice_id slice id
  1194. @param value transparency value (0 - 255)
  1195. @return 1 on success
  1196. @return -1 volume not found
  1197. @return -2 slice not found
  1198. @return -3 on failure
  1199. """
  1200. if not GVL_vol_exists(id):
  1201. return -1
  1202. if slice_id > GVL_slice_num_slices(id):
  1203. return -2
  1204. ret = GVL_slice_set_transp(id, slice_id, value)
  1205. if ret < 0:
  1206. return -2
  1207. return 1
  1208. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  1209. """!Set inout mode
  1210. @param id volume id
  1211. @param isosurf_id isosurface id
  1212. @param inout mode true/false
  1213. @return 1 on success
  1214. @return -1 volume set not found
  1215. @return -2 isosurface not found
  1216. @return -3 on failure
  1217. """
  1218. if not GVL_vol_exists(id):
  1219. return -1
  1220. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1221. return -2
  1222. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  1223. if ret < 0:
  1224. return -3
  1225. return 1
  1226. def GetVolumePosition(self, id):
  1227. """!Get volume position
  1228. @param id volume id
  1229. @return x,y,z
  1230. @return zero-length vector on error
  1231. """
  1232. if not GVL_vol_exists(id):
  1233. return []
  1234. x, y, z = c_float(), c_float(), c_float()
  1235. GVL_get_trans(id, byref(x), byref(y), byref(z))
  1236. Debug.msg(3, "Nviz::GetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1237. id, x.value, y.value, z.value)
  1238. return [x.value, y.value, z.value]
  1239. def SetVolumePosition(self, id, x, y, z):
  1240. """!Set volume position
  1241. @param id volume id
  1242. @param x,y,z translation values
  1243. @return 1 on success
  1244. @return -1 volume not found
  1245. @return -2 setting position failed
  1246. """
  1247. if not GVL_vol_exists(id):
  1248. return -1
  1249. Debug.msg(3, "Nviz::SetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1250. id, x, y, z)
  1251. GVL_set_trans(id, x, y, z)
  1252. return 1
  1253. def SetVolumeDrawBox(self, id, ifBox):
  1254. """!Display volume wire box
  1255. @param id volume id
  1256. @param ifBox True to draw wire box, False otherwise
  1257. @return 1 on success
  1258. @return -1 volume not found
  1259. """
  1260. if not GVL_vol_exists(id):
  1261. return -1
  1262. Debug.msg(3, "Nviz::SetVolumeDrawBox(): id=%d, ifBox=%d", id, ifBox)
  1263. GVL_set_draw_wire(id, int(ifBox))
  1264. return 1
  1265. def GetCPlaneCurrent(self):
  1266. return Nviz_get_current_cplane(self.data)
  1267. def GetCPlanesCount(self):
  1268. """!Returns number of cutting planes"""
  1269. return Nviz_num_cplanes(self.data)
  1270. def GetCPlaneRotation(self):
  1271. """!Returns rotation parameters of current cutting plane"""
  1272. x, y, z = c_float(), c_float(), c_float()
  1273. current = Nviz_get_current_cplane(self.data)
  1274. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  1275. return x.value, y.value, z.value
  1276. def GetCPlaneTranslation(self):
  1277. """!Returns translation parameters of current cutting plane"""
  1278. x, y, z = c_float(), c_float(), c_float()
  1279. current = Nviz_get_current_cplane(self.data)
  1280. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  1281. return x.value, y.value, z.value
  1282. def SetCPlaneRotation(self, x, y, z):
  1283. """!Set current clip plane rotation
  1284. @param x,y,z rotation parameters
  1285. """
  1286. current = Nviz_get_current_cplane(self.data)
  1287. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  1288. Nviz_draw_cplane(self.data, -1, -1)
  1289. def SetCPlaneTranslation(self, x, y, z):
  1290. """!Set current clip plane translation
  1291. @param x,y,z translation parameters
  1292. """
  1293. current = Nviz_get_current_cplane(self.data)
  1294. Nviz_set_cplane_translation(self.data, current, x, y, z)
  1295. Nviz_draw_cplane(self.data, -1, -1)
  1296. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  1297. current, x, y, z)
  1298. def SetCPlaneInteractively(self, x, y):
  1299. current = Nviz_get_current_cplane(self.data)
  1300. ret = Nviz_set_cplane_here(self.data, current, x, y)
  1301. if ret:
  1302. Nviz_draw_cplane(self.data, -1, -1)
  1303. x, y, z = self.GetCPlaneTranslation()
  1304. return x, y, z
  1305. else:
  1306. return None, None, None
  1307. def SelectCPlane(self, index):
  1308. """!Select cutting plane
  1309. @param index index of cutting plane
  1310. """
  1311. Nviz_on_cplane(self.data, index)
  1312. def UnselectCPlane(self, index):
  1313. """!Unselect cutting plane
  1314. @param index index of cutting plane
  1315. """
  1316. Nviz_off_cplane(self.data, index)
  1317. def SetFenceColor(self, index):
  1318. """!Select current cutting plane
  1319. @param index type of fence - from 0 (off) to 4
  1320. """
  1321. Nviz_set_fence_color(self.data, index)
  1322. def GetXYRange(self):
  1323. """!Get xy range"""
  1324. return Nviz_get_xyrange(self.data)
  1325. def GetZRange(self):
  1326. """!Get z range"""
  1327. min, max = c_float(), c_float()
  1328. Nviz_get_zrange(self.data, byref(min), byref(max))
  1329. return min.value, max.value
  1330. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  1331. """!Save current GL screen to ppm/tif file
  1332. @param filename file name
  1333. @param width image width
  1334. @param height image height
  1335. @param itype image type ('ppm' or 'tif')
  1336. """
  1337. widthOrig = self.width
  1338. heightOrig = self.height
  1339. self.ResizeWindow(width, height)
  1340. GS_clear(Nviz_get_bgcolor(self.data))
  1341. self.Draw(False, -1)
  1342. if itype == 'ppm':
  1343. GS_write_ppm(filename)
  1344. else:
  1345. GS_write_tif(filename)
  1346. self.ResizeWindow(widthOrig, heightOrig)
  1347. def DrawLightingModel(self):
  1348. """!Draw lighting model"""
  1349. if self.showLight:
  1350. Nviz_draw_model(self.data)
  1351. def DrawFringe(self):
  1352. """!Draw fringe"""
  1353. Nviz_draw_fringe(self.data)
  1354. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1355. """!Set fringe
  1356. @param sid surface id
  1357. @param color color
  1358. @param elev elevation (height)
  1359. @param nw,ne,sw,se fringe edges (turn on/off)
  1360. """
  1361. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1362. Nviz_set_fringe(self.data,
  1363. sid, Nviz_color_from_str(scolor),
  1364. elev, int(nw), int(ne), int(sw), int(se))
  1365. def DrawArrow(self):
  1366. """!Draw north arrow
  1367. """
  1368. return Nviz_draw_arrow(self.data)
  1369. def SetArrow(self, sx, sy, size, color):
  1370. """!Set north arrow from canvas coordinates
  1371. @param sx,sy canvas coordinates
  1372. @param size arrow length
  1373. @param color arrow color
  1374. """
  1375. return Nviz_set_arrow(self.data, sx, sy, size, Nviz_color_from_str(color))
  1376. def DeleteArrow(self):
  1377. """!Delete north arrow
  1378. """
  1379. Nviz_delete_arrow(self.data)
  1380. def SetScalebar(self, id, sx, sy, size, color):
  1381. """!Set scale bar from canvas coordinates
  1382. @param sx,sy canvas coordinates
  1383. @param id scale bar id
  1384. @param size scale bar length
  1385. @param color scale bar color
  1386. """
  1387. return Nviz_set_scalebar(self.data, id, sx, sy, size, Nviz_color_from_str(color))
  1388. def DrawScalebar(self):
  1389. """!Draw scale bar
  1390. """
  1391. return Nviz_draw_scalebar(self.data)
  1392. def DeleteScalebar(self, id):
  1393. """!Delete scalebar
  1394. """
  1395. Nviz_delete_scalebar(self.data, id)
  1396. def GetPointOnSurface(self, sx, sy):
  1397. """!Get point on surface
  1398. @param sx,sy canvas coordinates (LL)
  1399. """
  1400. sid = c_int()
  1401. x = c_float()
  1402. y = c_float()
  1403. z = c_float()
  1404. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1405. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1406. if num == 0:
  1407. return (None, None, None, None)
  1408. return (sid.value, x.value, y.value, z.value)
  1409. def QueryMap(self, sx, sy):
  1410. """!Query surface map
  1411. @param sx,sy canvas coordinates (LL)
  1412. """
  1413. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1414. if not sid:
  1415. return None
  1416. catstr = create_string_buffer(256)
  1417. valstr = create_string_buffer(256)
  1418. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1419. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1420. return { 'id' : sid,
  1421. 'x' : x,
  1422. 'y' : y,
  1423. 'z' : z,
  1424. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1425. 'color' : valstr.value }
  1426. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1427. """!Get distance measured along surface"""
  1428. d = c_float()
  1429. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1430. byref(d), int(useExag))
  1431. return d.value
  1432. def GetRotationParameters(self, dx, dy):
  1433. """!Get rotation parameters (angle, x, y, z axes)
  1434. @param dx,dy difference from previous mouse drag event
  1435. """
  1436. modelview = (c_double * 16)()
  1437. Nviz_get_modelview(byref(modelview))
  1438. angle = sqrt(dx*dx+dy*dy)/float(self.width+1)*180.0
  1439. m = []
  1440. row = []
  1441. for i, item in enumerate(modelview):
  1442. row.append(item)
  1443. if (i+1) % 4 == 0:
  1444. m.append(row)
  1445. row = []
  1446. inv = matrix(m).I
  1447. ax, ay, az = dy, dx, 0.
  1448. x = inv[0,0]*ax + inv[1,0]*ay + inv[2,0]*az
  1449. y = inv[0,1]*ax + inv[1,1]*ay + inv[2,1]*az
  1450. z = inv[0,2]*ax + inv[1,2]*ay + inv[2,2]*az
  1451. return angle, x, y, z
  1452. def Rotate(self, angle, x, y, z):
  1453. """!Set rotation parameters
  1454. Rotate scene (difference from current state).
  1455. @param angle angle
  1456. @param x,y,z axis coordinate
  1457. """
  1458. Nviz_set_rotation(angle, x, y, z)
  1459. def UnsetRotation(self):
  1460. """!Stop rotating the scene"""
  1461. Nviz_unset_rotation()
  1462. def ResetRotation(self):
  1463. """!Reset scene rotation"""
  1464. Nviz_init_rotation()
  1465. def GetRotationMatrix(self):
  1466. """!Get rotation matrix"""
  1467. matrix = (c_double * 16)()
  1468. GS_get_rotation_matrix(byref(matrix))
  1469. returnMatrix = []
  1470. for item in matrix:
  1471. returnMatrix.append(item)
  1472. return returnMatrix
  1473. def SetRotationMatrix(self, matrix):
  1474. """!Set rotation matrix"""
  1475. mtrx = (c_double * 16)()
  1476. for i in range(len(matrix)):
  1477. mtrx[i] = matrix[i]
  1478. GS_set_rotation_matrix(byref(mtrx))
  1479. def Start2D(self):
  1480. Nviz_set_2D(self.width, self.height)
  1481. def FlyThrough(self, flyInfo, mode, exagInfo):
  1482. """!Fly through the scene
  1483. @param flyInfo fly parameters
  1484. @param mode 0 or 1 for different fly behaviour
  1485. @param exagInfo parameters changing fly speed
  1486. """
  1487. fly = (c_float * 3)()
  1488. for i, item in enumerate(flyInfo):
  1489. fly[i] = item
  1490. exag = (c_int * 2)()
  1491. exag[0] = int(exagInfo['move'])
  1492. exag[1] = int(exagInfo['turn'])
  1493. Nviz_flythrough(self.data, fly, exag, mode)
  1494. class Texture(object):
  1495. """!Class representing OpenGL texture"""
  1496. def __init__(self, filepath, overlayId, coords):
  1497. """!Load image to texture
  1498. @param filepath path to image file
  1499. @param overlayId id of overlay (1 for legend, 101 and more for text)
  1500. @param coords image coordinates
  1501. """
  1502. self.path = filepath
  1503. self.image = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
  1504. self.width = self.image.GetWidth()
  1505. self.height = self.image.GetHeight()
  1506. self.id = overlayId
  1507. self.coords = list(coords)
  1508. self.bounds = wx.Rect()
  1509. self.active = True
  1510. # alpha needs to be initialized
  1511. if not self.image.HasAlpha():
  1512. self.image.InitAlpha()
  1513. # resize image to match 2^n
  1514. self.Resize()
  1515. # check max texture size
  1516. maxSize = c_int()
  1517. Nviz_get_max_texture(byref(maxSize))
  1518. self.maxSize = maxSize.value
  1519. if self.maxSize < self.width or self.maxSize < self.height:
  1520. # TODO: split up image
  1521. self.textureId = None
  1522. else:
  1523. self.textureId = self.Load()
  1524. def __del__(self):
  1525. """!Delete texture"""
  1526. if self.textureId:
  1527. Nviz_del_texture(self.textureId)
  1528. grass.try_remove(self.path)
  1529. def Resize(self):
  1530. """!Resize image to match 2^n"""
  1531. n = m = 1
  1532. while self.width > pow(2,n):
  1533. n += 1
  1534. while self.height > pow(2,m):
  1535. m += 1
  1536. self.image.Resize(size = (pow(2,n), pow(2,m)), pos = (0, 0))
  1537. self.width = self.image.GetWidth()
  1538. self.height = self.image.GetHeight()
  1539. def Load(self):
  1540. """!Load image to texture"""
  1541. if self.image.HasAlpha():
  1542. bytesPerPixel = 4
  1543. else:
  1544. bytesPerPixel = 3
  1545. bytes = bytesPerPixel * self.width * self.height
  1546. rev_val = self.height - 1
  1547. im = (c_ubyte * bytes)()
  1548. bytes3 = 3 * self.width * self.height
  1549. bytes1 = self.width * self.height
  1550. imageData = struct.unpack(str(bytes3) + 'B', self.image.GetData())
  1551. if self.image.HasAlpha():
  1552. alphaData = struct.unpack(str(bytes1) + 'B', self.image.GetAlphaData())
  1553. # this takes too much time
  1554. wx.BeginBusyCursor()
  1555. for i in range(self.height):
  1556. for j in range(self.width):
  1557. im[(j + i * self.width) * bytesPerPixel + 0] = imageData[( j + (rev_val - i) * self.width) * 3 + 0]
  1558. im[(j + i * self.width) * bytesPerPixel + 1] = imageData[( j + (rev_val - i) * self.width) * 3 + 1]
  1559. im[(j + i * self.width) * bytesPerPixel + 2] = imageData[( j + (rev_val - i) * self.width) * 3 + 2]
  1560. if self.image.HasAlpha():
  1561. im[(j + i * self.width) * bytesPerPixel + 3] = alphaData[( j + (rev_val - i) * self.width)]
  1562. wx.EndBusyCursor()
  1563. id = Nviz_load_image(im, self.width, self.height, self.image.HasAlpha())
  1564. return id
  1565. def Draw(self):
  1566. """!Draw texture as an image"""
  1567. Nviz_draw_image(self.coords[0], self.coords[1], self.width, self.height, self.textureId)
  1568. def SetBounds(self, rect):
  1569. """!Set Bounding Rectangle"""
  1570. self.bounds = rect
  1571. def HitTest(self, x, y, radius):
  1572. copy = wx.Rect(*self.bounds)
  1573. copy.Inflate(radius, radius)
  1574. return copy.ContainsXY(x, y)
  1575. def MoveTexture(self, dx, dy):
  1576. """!Move texture on the screen"""
  1577. self.coords[0] += dx
  1578. self.coords[1] += dy
  1579. self.bounds.OffsetXY(dx, dy)
  1580. def SetCoords(self, coords):
  1581. """!Set coordinates"""
  1582. dx = coords[0] - self.coords[0]
  1583. dy = coords[1] - self.coords[1]
  1584. self.MoveTexture(dx, dy)
  1585. def GetId(self):
  1586. """!Returns image id."""
  1587. return self.id
  1588. def SetActive(self, active = True):
  1589. self.active = active
  1590. def IsActive(self):
  1591. return self.active
  1592. class ImageTexture(Texture):
  1593. """!Class representing OpenGL texture as an overlay image"""
  1594. def __init__(self, filepath, overlayId, coords, cmd):
  1595. """!Load image to texture
  1596. @param filepath path to image file
  1597. @param overlayId id of overlay (1 for legend)
  1598. @param coords image coordinates
  1599. @param cmd d.legend command
  1600. """
  1601. Texture.__init__(self, filepath = filepath, overlayId = overlayId, coords = coords)
  1602. self.cmd = cmd
  1603. def GetCmd(self):
  1604. """!Returns overlay command."""
  1605. return self.cmd
  1606. def Corresponds(self, item):
  1607. return sorted(self.GetCmd()) == sorted(item.GetCmd())
  1608. class TextTexture(Texture):
  1609. """!Class representing OpenGL texture as a text label"""
  1610. def __init__(self, filepath, overlayId, coords, textDict):
  1611. """!Load image to texture
  1612. @param filepath path to image file
  1613. @param overlayId id of overlay (101 and more for text)
  1614. @param coords text coordinates
  1615. @param textDict text properties
  1616. """
  1617. Texture.__init__(self, filepath = filepath, overlayId = overlayId, coords = coords)
  1618. self.textDict = textDict
  1619. def GetTextDict(self):
  1620. """!Returns text properties."""
  1621. return self.textDict
  1622. def Corresponds(self, item):
  1623. t = self.GetTextDict()
  1624. for prop in t.keys():
  1625. if prop in ('coords','bbox'): continue
  1626. if t[prop] != item[prop]:
  1627. return False
  1628. return True