wxnviz.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. """!
  2. @package wxnviz.py
  3. @brief wxGUI 3D view mode
  4. This module implements 3D visualization mode for map display.
  5. List of classes:
  6. - Nviz
  7. (C) 2008-2010 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  12. @author Pythonized by Glynn Clements
  13. """
  14. import sys
  15. from threading import Thread
  16. from ctypes import *
  17. from grass.lib.gis import *
  18. from grass.lib.g3d import *
  19. from grass.lib.ogsf import *
  20. from grass.lib.nviz import *
  21. from debug import Debug
  22. log = None
  23. progress = None
  24. def print_error(msg, type):
  25. """!Redirect stderr"""
  26. global log
  27. if log:
  28. log.write(msg)
  29. else:
  30. print msg
  31. return 0
  32. def print_progress(value):
  33. """!Redirect progress info"""
  34. global progress
  35. if progress:
  36. progress.SetValue(value)
  37. else:
  38. print value
  39. return 0
  40. errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
  41. errfunc = errtype(print_error)
  42. pertype = CFUNCTYPE(UNCHECKED(c_int), c_int)
  43. perfunc = pertype(print_progress)
  44. class Nviz(object):
  45. def __init__(self, glog, gprogress):
  46. """!Initialize Nviz class instance
  47. @param log logging area
  48. """
  49. global errfunc, perfunc, log, progress
  50. log = glog
  51. progress = gprogress
  52. G_gisinit("")
  53. G_set_error_routine(errfunc)
  54. G_set_percent_routine(perfunc)
  55. GS_libinit()
  56. GVL_libinit()
  57. self.data_obj = nv_data()
  58. self.data = pointer(self.data_obj)
  59. self.width = self.height = -1
  60. self.showLight = False
  61. Debug.msg(1, "Nviz::Nviz()")
  62. def __del__(self):
  63. """!Destroy Nviz class instance"""
  64. G_unset_error_routine()
  65. G_unset_percent_routine()
  66. del self.data
  67. del self.data_obj
  68. self.log = None
  69. def ResizeWindow(self, width, height):
  70. """!GL canvas resized
  71. @param width window width
  72. @param height window height
  73. @return 1 on success
  74. @return 0 on failure (window resized by default to 20x20 px)
  75. """
  76. self.width = width
  77. self.height = height
  78. Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
  79. width, height)
  80. return Nviz_resize_window(width, height)
  81. def GetLongDim(self):
  82. """!Get longest dimension, used for initial size of north arrow"""
  83. return Nviz_get_longdim(self.data)
  84. def SetViewDefault(self):
  85. """!Set default view (based on loaded data)
  86. @return z-exag value, default, min and max height
  87. """
  88. # determine z-exag
  89. z_exag = Nviz_get_exag()
  90. Nviz_change_exag(self.data, z_exag)
  91. # determine height
  92. hdef = c_double()
  93. hmin = c_double()
  94. hmax = c_double()
  95. Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
  96. Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
  97. hdef.value, hmin.value, hmax.value)
  98. return (z_exag, hdef.value, hmin.value, hmax.value)
  99. def SetView(self, x, y, height, persp, twist):
  100. """!Change view settings
  101. @param x,y position
  102. @param height
  103. @param persp perpective
  104. @param twist
  105. """
  106. Nviz_set_viewpoint_height(height)
  107. Nviz_set_viewpoint_position(x, y)
  108. Nviz_set_viewpoint_twist(twist)
  109. Nviz_set_viewpoint_persp(persp)
  110. Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
  111. x, y, height, persp, twist)
  112. def LookHere(self, x, y):
  113. """!Look here feature
  114. @param x,y screen coordinates
  115. """
  116. Nviz_look_here(x, y)
  117. Debug.msg(3, "Nviz::LookHere(): x=%f, y=%f", x, y)
  118. def LookAtCenter(self):
  119. """!Center view at center of displayed surface"""
  120. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  121. Debug.msg(3, "Nviz::LookAtCenter()")
  122. def GetFocus(self):
  123. """!Get focus"""
  124. Debug.msg(3, "Nviz::GetFocus()")
  125. if Nviz_has_focus(self.data):
  126. x = c_float()
  127. y = c_float()
  128. z = c_float()
  129. Nviz_get_focus(self.data, byref(x), byref(y), byref(z))
  130. return x.value, y.value, z.value
  131. else:
  132. return -1, -1, -1
  133. def SetFocus(self, x, y, z):
  134. """!Set focus"""
  135. Debug.msg(3, "Nviz::SetFocus()")
  136. Nviz_set_focus(self.data, x, y, z)
  137. def SetZExag(self, z_exag):
  138. """!Set z-exag value
  139. @param z_exag value
  140. @return 1
  141. """
  142. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  143. return Nviz_change_exag(self.data, z_exag)
  144. def Draw(self, quick, quick_mode):
  145. """!Draw canvas
  146. Draw quick mode:
  147. - DRAW_QUICK_SURFACE
  148. - DRAW_QUICK_VLINES
  149. - DRAW_QUICK_VPOINTS
  150. - DRAW_QUICK_VOLUME
  151. @param quick if true draw in wiremode
  152. @param quick_mode quick mode
  153. """
  154. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  155. Nviz_draw_cplane(self.data, -1, -1) # ?
  156. if quick:
  157. Nviz_draw_quick(self.data, quick_mode)
  158. else:
  159. Nviz_draw_all(self.data)
  160. def EraseMap(self):
  161. """!Erase map display (with background color)
  162. """
  163. Debug.msg(1, "Nviz::EraseMap()")
  164. GS_clear(Nviz_get_bgcolor(self.data))
  165. def InitView(self):
  166. """!Initialize view"""
  167. # initialize nviz data
  168. Nviz_init_data(self.data)
  169. # define default attributes for map objects
  170. Nviz_set_surface_attr_default()
  171. # set background color
  172. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white"))
  173. GS_clear(Nviz_get_bgcolor(self.data))
  174. # initialize view, lights
  175. Nviz_init_view(self.data)
  176. Debug.msg(1, "Nviz::InitView()")
  177. def SetBgColor(self, color_str):
  178. """!Set background color
  179. @param color_str color string
  180. """
  181. Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
  182. def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
  183. """!Change lighting settings
  184. @param x,y,z position
  185. @param color light color (as string)
  186. @param bright light brightness
  187. @param ambient light ambient
  188. @param w local coordinate (default to 0)
  189. """
  190. Nviz_set_light_position(self.data, lid, x, y, z, w)
  191. Nviz_set_light_bright(self.data, lid, bright)
  192. Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
  193. Nviz_set_light_ambient(self.data, lid, ambient)
  194. def LoadSurface(self, name, color_name, color_value):
  195. """!Load raster map (surface)
  196. @param name raster map name
  197. @param color_name raster map for color (None for color_value)
  198. @param color_value color string (named color or RGB triptet)
  199. @return object id
  200. @return -1 on failure
  201. """
  202. mapset = G_find_raster2(name, "")
  203. if mapset is None:
  204. G_warning(_("Raster map <%s> not found"), name)
  205. return -1
  206. # topography
  207. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  208. G_fully_qualified_name(name, mapset), 0.0,
  209. self.data)
  210. if color_name: # check for color map
  211. mapset = G_find_raster2(color_name, "")
  212. if mapset is None:
  213. G_warning(_("Raster map <%s> not found"), color_name)
  214. GS_delete_surface(id)
  215. return -1
  216. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  217. G_fully_qualified_name(color_name, mapset), -1.0,
  218. self.data)
  219. elif color_value: # check for color value
  220. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  221. None, Nviz_color_from_str(color_value),
  222. self.data)
  223. else: # use by default elevation map for coloring
  224. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  225. G_fully_qualified_name(name, mapset), -1.0,
  226. self.data)
  227. # if (i > 1)
  228. # set_default_wirecolors(self.data, i)
  229. # focus on loaded self.data
  230. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  231. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  232. return id
  233. def AddConstant(self, value, color):
  234. """!Add new constant surface"""
  235. id = Nviz_new_map_obj(MAP_OBJ_SURF, None, value, self.data)
  236. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  237. None, Nviz_color_from_str(color),
  238. self.data)
  239. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  240. Debug.msg(1, "Nviz::AddConstant(): id=%d", id)
  241. return id
  242. def UnloadSurface(self, id):
  243. """!Unload surface
  244. @param id surface id
  245. @return 1 on success
  246. @return 0 on failure
  247. """
  248. if not GS_surf_exists(id):
  249. return 0
  250. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  251. if GS_delete_surface(id) < 0:
  252. return 0
  253. return 1
  254. def LoadVector(self, name, points):
  255. """!Load vector map overlay
  256. @param name vector map name
  257. @param points if true load 2d points rather then 2d lines
  258. @return object id
  259. @return -1 on failure
  260. """
  261. if GS_num_surfs() == 0: # load base surface if no loaded
  262. Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  263. nsurf = c_int()
  264. surf_list = GS_get_surf_list(byref(nsurf))
  265. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  266. mapset = G_find_vector2 (name, "")
  267. if mapset is None:
  268. G_warning(_("Vector map <%s> not found"),
  269. name)
  270. if points:
  271. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  272. G_fully_qualified_name(name, mapset), 0.0,
  273. self.data)
  274. else:
  275. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  276. G_fully_qualified_name(name, mapset), 0.0,
  277. self.data)
  278. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  279. return id
  280. def UnloadVector(self, id, points):
  281. """!Unload vector set
  282. @param id vector set id
  283. @param points vector points or lines set
  284. @return 1 on success
  285. @return 0 on failure
  286. """
  287. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  288. if points:
  289. if not GP_site_exists(id):
  290. return 0
  291. if GP_delete_site(id) < 0:
  292. return 0
  293. else:
  294. if not GV_vect_exists(id):
  295. return 0
  296. if GV_delete_vector(id) < 0:
  297. return 0
  298. return 1
  299. def VectorSurfaceSelected(self, vid, sid):
  300. """!Check if surface is selected (currently unused)
  301. @param vid vector id
  302. @param sid surface id
  303. @return True if selected
  304. @return False if not selected
  305. """
  306. selected = GV_surf_is_selected(vid, sid)
  307. Debug.msg(1, "Nviz::VectorSurfaceSelected(): vid=%s, sid=%d -> selected=%d", vid, sid, selected)
  308. return selected
  309. def LoadVolume(self, name, color_name, color_value):
  310. """!Load 3d raster map (volume)
  311. @param name 3d raster map name
  312. @param color_name 3d raster map for color (None for color_value)
  313. @param color_value color string (named color or RGB triptet)
  314. @return object id
  315. @return -1 on failure
  316. """
  317. mapset = G_find_grid3(name, "")
  318. if mapset is None:
  319. G_warning(_("3d raster map <%s> not found"),
  320. name)
  321. return -1
  322. # topography
  323. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  324. G_fully_qualified_name(name, mapset), 0.0,
  325. self.data)
  326. if color_name: # check for color map
  327. mapset = G_find_grid3(color_name, "")
  328. if mapset is None:
  329. G_warning(_("3d raster map <%s> not found"),
  330. color_name)
  331. GVL_delete_vol(id)
  332. return -1
  333. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  334. G_fully_qualified_name(color_name, mapset), -1.0,
  335. self.data)
  336. elif color_value: # check for color value
  337. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  338. None, Nviz_color_from_str(color_value),
  339. self.data)
  340. else: # use by default elevation map for coloring
  341. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  342. G_fully_qualified_name(name, mapset), -1.0,
  343. self.data)
  344. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  345. return id
  346. def UnloadVolume(self, id):
  347. """!Unload volume
  348. @param id volume id
  349. @return 1 on success
  350. @return 0 on failure
  351. """
  352. if not GVL_vol_exists(id):
  353. return 0
  354. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  355. if GVL_delete_vol(id) < 0:
  356. return 0
  357. return 1
  358. def SetSurfaceTopo(self, id, map, value):
  359. """!Set surface topography
  360. @param id surface id
  361. @param map if true use map otherwise constant
  362. @param value map name of value
  363. @return 1 on success
  364. @return -1 surface not found
  365. @return -2 setting attributes failed
  366. """
  367. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  368. def SetSurfaceColor(self, id, map, value):
  369. """!Set surface color
  370. @param id surface id
  371. @param map if true use map otherwise constant
  372. @param value map name or value
  373. @return 1 on success
  374. @return -1 surface not found
  375. @return -2 setting attributes failed
  376. """
  377. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  378. def SetSurfaceMask(self, id, invert, value):
  379. """!Set surface mask
  380. @todo invert
  381. @param id surface id
  382. @param invert if true invert mask
  383. @param value map name of value
  384. @return 1 on success
  385. @return -1 surface not found
  386. @return -2 setting attributes failed
  387. """
  388. return self.SetSurfaceAttr(id, ATT_MASK, True, value)
  389. def SetSurfaceTransp(self, id, map, value):
  390. """!Set surface mask
  391. @todo invert
  392. @param id surface id
  393. @param map if true use map otherwise constant
  394. @param value map name of value
  395. @return 1 on success
  396. @return -1 surface not found
  397. @return -2 setting attributes failed
  398. """
  399. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  400. def SetSurfaceShine(self, id, map, value):
  401. """!Set surface shininess
  402. @param id surface id
  403. @param map if true use map otherwise constant
  404. @param value map name of value
  405. @return 1 on success
  406. @return -1 surface not found
  407. @return -2 setting attributes failed
  408. """
  409. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  410. def SetSurfaceEmit(self, id, map, value):
  411. """!Set surface emission (currently unused)
  412. @param id surface id
  413. @param map if true use map otherwise constant
  414. @param value map name of value
  415. @return 1 on success
  416. @return -1 surface not found
  417. @return -2 setting attributes failed
  418. """
  419. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  420. def SetSurfaceAttr(self, id, attr, map, value):
  421. """!Set surface attribute
  422. @param id surface id
  423. @param attr attribute desc
  424. @param map if true use map otherwise constant
  425. @param value map name of value
  426. @return 1 on success
  427. @return -1 surface not found
  428. @return -2 setting attributes failed
  429. """
  430. if not GS_surf_exists(id):
  431. return -1
  432. if map:
  433. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  434. value, -1.0, self.data)
  435. else:
  436. if attr == ATT_COLOR:
  437. val = Nviz_color_from_str(value)
  438. else:
  439. val = float(value)
  440. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  441. None, val, self.data)
  442. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  443. id, attr, map, value)
  444. if ret < 0:
  445. return -2
  446. return 1
  447. def UnsetSurfaceMask(self, id):
  448. """!Unset surface mask
  449. @param id surface id
  450. @return 1 on success
  451. @return -1 surface not found
  452. @return -2 setting attributes failed
  453. @return -1 on failure
  454. """
  455. return self.UnsetSurfaceAttr(id, ATT_MASK)
  456. def UnsetSurfaceTransp(self, id):
  457. """!Unset surface transparency
  458. @param id surface id
  459. @return 1 on success
  460. @return -1 surface not found
  461. @return -2 setting attributes failed
  462. """
  463. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  464. def UnsetSurfaceEmit(self, id):
  465. """!Unset surface emission (currently unused)
  466. @param id surface id
  467. @return 1 on success
  468. @return -1 surface not found
  469. @return -2 setting attributes failed
  470. """
  471. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  472. def UnsetSurfaceAttr(self, id, attr):
  473. """!Unset surface attribute
  474. @param id surface id
  475. @param attr attribute descriptor
  476. @return 1 on success
  477. @return -1 surface not found
  478. @return -2 setting attributes failed
  479. """
  480. if not GS_surf_exists(id):
  481. return -1
  482. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  483. id, attr)
  484. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  485. if ret < 0:
  486. return -2
  487. return 1
  488. def SetSurfaceRes(self, id, fine, coarse):
  489. """!Set surface resolution
  490. @param id surface id
  491. @param fine x/y fine resolution
  492. @param coarse x/y coarse resolution
  493. @return 1 on success
  494. @return -1 surface not found
  495. @return -2 setting attributes failed
  496. """
  497. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  498. id, fine, coarse)
  499. if id > 0:
  500. if not GS_surf_exists(id):
  501. return -1
  502. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  503. return -2
  504. else:
  505. GS_setall_drawres(fine, fine, coarse, coarse)
  506. return 1
  507. def SetSurfaceStyle(self, id, style):
  508. """!Set draw style
  509. Draw styles:
  510. - DM_GOURAUD
  511. - DM_FLAT
  512. - DM_FRINGE
  513. - DM_WIRE
  514. - DM_COL_WIRE
  515. - DM_POLY
  516. - DM_WIRE_POLY
  517. - DM_GRID_WIRE
  518. - DM_GRID_SURF
  519. @param id surface id (<= 0 for all)
  520. @param style draw style
  521. @return 1 on success
  522. @return -1 surface not found
  523. @return -2 setting attributes failed
  524. """
  525. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  526. id, style)
  527. if id > 0:
  528. if not GS_surf_exists(id):
  529. return -1
  530. if GS_set_drawmode(id, style) < 0:
  531. return -2
  532. return 1
  533. if GS_setall_drawmode(style) < 0:
  534. return -2
  535. return 1
  536. def SetWireColor(self, id, color_str):
  537. """!Set color of wire
  538. @todo all
  539. @param surface id (< 0 for all)
  540. @param color color string (R:G:B)
  541. @return 1 on success
  542. @return -1 surface not found
  543. @return -2 setting attributes failed
  544. @return 1 on success
  545. @return 0 on failure
  546. """
  547. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  548. id, color_str)
  549. color = Nviz_color_from_str(color_str)
  550. if id > 0:
  551. if not GS_surf_exists(id):
  552. return -1
  553. GS_set_wire_color(id, color)
  554. else:
  555. nsurfs = c_int()
  556. surf_list = GS_get_surf_list(byref(nsurfs))
  557. for i in xrange(nsurfs.value):
  558. id = surf_list[i]
  559. GS_set_wire_color(id, color)
  560. G_free(surf_list)
  561. surf_list = None
  562. return 1
  563. def GetSurfacePosition(self, id):
  564. """!Get surface position
  565. @param id surface id
  566. @return x,y,z
  567. @return zero-length vector on error
  568. """
  569. if not GS_surf_exists(id):
  570. return []
  571. x, y, z = c_float(), c_float(), c_float()
  572. GS_get_trans(id, byref(x), byref(y), byref(z))
  573. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  574. id, x.value, y.value, z.value)
  575. return [x.value, y.value, z.value]
  576. def SetSurfacePosition(self, id, x, y, z):
  577. """!Set surface position
  578. @param id surface id
  579. @param x,y,z translation values
  580. @return 1 on success
  581. @return -1 surface not found
  582. @return -2 setting position failed
  583. """
  584. if not GS_surf_exists(id):
  585. return -1
  586. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  587. id, x, y, z)
  588. GS_set_trans(id, x, y, z)
  589. return 1
  590. def SetVectorLineMode(self, id, color_str, width, flat):
  591. """!Set mode of vector line overlay
  592. @param id vector id
  593. @param color_str color string
  594. @param width line width
  595. @param flat display flat or on surface
  596. @return -1 vector set not found
  597. @return -2 on failure
  598. @return 1 on success
  599. """
  600. if not GV_vect_exists(id):
  601. return -1
  602. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  603. id, color_str, width, flat)
  604. color = Nviz_color_from_str(color_str)
  605. # use memory by default
  606. if GV_set_vectmode(id, 1, color, width, flat) < 0:
  607. return -2
  608. return 1
  609. def SetVectorLineHeight(self, id, height):
  610. """!Set vector height above surface (lines)
  611. @param id vector set id
  612. @param height
  613. @return -1 vector set not found
  614. @return 1 on success
  615. """
  616. if not GV_vect_exists(id):
  617. return -1
  618. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  619. id, height)
  620. GV_set_trans(id, 0.0, 0.0, height)
  621. return 1
  622. def SetVectorLineSurface(self, id, surf_id):
  623. """!Set reference surface of vector set (lines)
  624. @param id vector set id
  625. @param surf_id surface id
  626. @return 1 on success
  627. @return -1 vector set not found
  628. @return -2 surface not found
  629. @return -3 on failure
  630. """
  631. if not GV_vect_exists(id):
  632. return -1
  633. if not GS_surf_exists(surf_id):
  634. return -2
  635. if GV_select_surf(id, surf_id) < 0:
  636. return -3
  637. return 1
  638. def UnsetVectorLineSurface(self, id, surf_id):
  639. """!Unset reference surface of vector set (lines)
  640. @param id vector set id
  641. @param surf_id surface id
  642. @return 1 on success
  643. @return -1 vector set not found
  644. @return -2 surface not found
  645. @return -3 on failure
  646. """
  647. if not GV_vect_exists(id):
  648. return -1
  649. if not GS_surf_exists(surf_id):
  650. return -2
  651. if GV_unselect_surf(id, surf_id) < 0:
  652. return -3
  653. return 1
  654. def SetVectorPointMode(self, id, color_str, width, size, marker):
  655. """!Set mode of vector point overlay
  656. @param id vector id
  657. @param color_str color string
  658. @param width line width
  659. @param flat
  660. @return -1 vector set not found
  661. """
  662. if not GP_site_exists(id):
  663. return -1
  664. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  665. "width=%d, size=%f, marker=%d",
  666. id, color_str, width, size, marker)
  667. color = Nviz_color_from_str(color_str)
  668. if GP_set_style(id, color, width, size, marker) < 0:
  669. return -2
  670. return 1
  671. def SetVectorPointHeight(self, id, height):
  672. """!Set vector height above surface (points)
  673. @param id vector set id
  674. @param height
  675. @return -1 vector set not found
  676. @return 1 on success
  677. """
  678. if not GP_site_exists(id):
  679. return -1
  680. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  681. id, height)
  682. GP_set_trans(id, 0.0, 0.0, height)
  683. return 1
  684. def SetVectorPointSurface(self, id, surf_id):
  685. """!Set reference surface of vector set (points)
  686. @param id vector set id
  687. @param surf_id surface id
  688. @return 1 on success
  689. @return -1 vector set not found
  690. @return -2 surface not found
  691. @return -3 on failure
  692. """
  693. if not GP_site_exists(id):
  694. return -1
  695. if not GS_surf_exists(surf_id):
  696. return -2
  697. if GP_select_surf(id, surf_id) < 0:
  698. return -3
  699. return 1
  700. def UnsetVectorPointSurface(self, id, surf_id):
  701. """!Unset reference surface of vector set (points)
  702. @param id vector set id
  703. @param surf_id surface id
  704. @return 1 on success
  705. @return -1 vector set not found
  706. @return -2 surface not found
  707. @return -3 on failure
  708. """
  709. if not GP_site_exists(id):
  710. return -1
  711. if not GS_surf_exists(surf_id):
  712. return -2
  713. if GP_unselect_surf(id, surf_id) < 0:
  714. return -3
  715. return 1
  716. def AddIsosurface(self, id, level):
  717. """!Add new isosurface
  718. @param id volume id
  719. @param level isosurface level (topography)
  720. @return -1 on failure
  721. @return 1 on success
  722. """
  723. if not GVL_vol_exists(id):
  724. return -1
  725. if GVL_isosurf_add(id) < 0:
  726. return -1
  727. # set topography level
  728. nisosurfs = GVL_isosurf_num_isosurfs(id)
  729. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  730. def DeleteIsosurface(self, id, isosurf_id):
  731. """!Delete isosurface
  732. @param id volume id
  733. @param isosurf_id isosurface id
  734. @return 1 on success
  735. @return -1 volume not found
  736. @return -2 isosurface not found
  737. @return -3 on failure
  738. """
  739. if not GVL_vol_exists(id):
  740. return -1
  741. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  742. return -2
  743. ret = GVL_isosurf_del(id, isosurf_id)
  744. if ret < 0:
  745. return -3
  746. return 1
  747. def MoveIsosurface(self, id, isosurf_id, up):
  748. """!Move isosurface up/down in the list
  749. @param id volume id
  750. @param isosurf_id isosurface id
  751. @param up if true move up otherwise down
  752. @return 1 on success
  753. @return -1 volume not found
  754. @return -2 isosurface not found
  755. @return -3 on failure
  756. """
  757. if not GVL_vol_exists(id):
  758. return -1
  759. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  760. return -2
  761. if up:
  762. ret = GVL_isosurf_move_up(id, isosurf_id)
  763. else:
  764. ret = GVL_isosurf_move_down(id, isosurf_id)
  765. if ret < 0:
  766. return -3
  767. return 1
  768. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  769. """!Set isosurface level
  770. @param id volume id
  771. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  772. @param map if true use map otherwise constant
  773. @param value map name of value
  774. @return 1 on success
  775. @return -1 volume not found
  776. @return -2 isosurface not found
  777. @return -3 on failure
  778. """
  779. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  780. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  781. """!Set isosurface color
  782. @param id volume id
  783. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  784. @param map if true use map otherwise constant
  785. @param value map name of value
  786. @return 1 on success
  787. @return -1 volume not found
  788. @return -2 isosurface not found
  789. @return -3 on failure
  790. """
  791. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  792. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  793. """!Set isosurface mask
  794. @todo invert
  795. @param id volume id
  796. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  797. @param invert true for invert mask
  798. @param value map name to be used for mask
  799. @return 1 on success
  800. @return -1 volume not found
  801. @return -2 isosurface not found
  802. @return -3 on failure
  803. """
  804. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  805. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  806. """!Set isosurface transparency
  807. @param id volume id
  808. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  809. @param map if true use map otherwise constant
  810. @param value map name of value
  811. @return 1 on success
  812. @return -1 volume not found
  813. @return -2 isosurface not found
  814. @return -3 on failure
  815. """
  816. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  817. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  818. """!Set isosurface shininess
  819. @param id volume id
  820. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  821. @param map if true use map otherwise constant
  822. @param value map name of value
  823. @return 1 on success
  824. @return -1 volume not found
  825. @return -2 isosurface not found
  826. @return -3 on failure
  827. """
  828. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  829. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  830. """!Set isosurface emission (currently unused)
  831. @param id volume id
  832. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  833. @param map if true use map otherwise constant
  834. @param value map name of value
  835. @return 1 on success
  836. @return -1 volume not found
  837. @return -2 isosurface not found
  838. @return -3 on failure
  839. """
  840. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  841. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  842. """!Set isosurface attribute
  843. @param id volume id
  844. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  845. @param attr attribute desc
  846. @param map if true use map otherwise constant
  847. @param value map name of value
  848. @return 1 on success
  849. @return -1 volume not found
  850. @return -2 isosurface not found
  851. @return -3 setting attributes failed
  852. """
  853. if not GVL_vol_exists(id):
  854. return -1
  855. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  856. return -2
  857. if map:
  858. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  859. else:
  860. if attr == ATT_COLOR:
  861. val = Nviz_color_from_str(value)
  862. else:
  863. val = float(value)
  864. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  865. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  866. "attr=%d, map=%d, value=%s",
  867. id, isosurf_id, attr, map, value)
  868. if ret < 0:
  869. return -2
  870. return 1
  871. def UnsetIsosurfaceMask(self, id, isosurf_id):
  872. """!Unset isosurface mask
  873. @param id volume id
  874. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  875. @return 1 on success
  876. @return -1 volume not found
  877. @return -2 isosurface not found
  878. @return -3 setting attributes failed
  879. """
  880. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  881. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  882. """!Unset isosurface transparency
  883. @param id volume id
  884. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  885. @return 1 on success
  886. @return -1 volume not found
  887. @return -2 isosurface not found
  888. @return -3 setting attributes failed
  889. """
  890. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  891. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  892. """!Unset isosurface emission (currently unused)
  893. @param id volume id
  894. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  895. @return 1 on success
  896. @return -1 volume not found
  897. @return -2 isosurface not found
  898. @return -3 setting attributes failed
  899. """
  900. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  901. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  902. """!Unset surface attribute
  903. @param id surface id
  904. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  905. @param attr attribute descriptor
  906. @return 1 on success
  907. @return -1 volume not found
  908. @return -2 isosurface not found
  909. @return -2 on failure
  910. """
  911. if not GVL_vol_exists(id):
  912. return -1
  913. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  914. return -2
  915. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  916. id, isosurf_id, attr)
  917. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  918. if ret < 0:
  919. return -2
  920. return 1
  921. def SetIsosurfaceMode(self, id, mode):
  922. """!Set draw mode for isosurfaces
  923. @param mode
  924. @return 1 on success
  925. @return -1 volume set not found
  926. @return -2 on failure
  927. """
  928. if not GVL_vol_exists(id):
  929. return -1
  930. ret = GVL_isosurf_set_drawmode(id, mode)
  931. if ret < 0:
  932. return -2
  933. return 1
  934. def SetIsosurfaceRes(self, id, res):
  935. """!Set draw resolution for isosurfaces
  936. @param res resolution value
  937. @return 1 on success
  938. @return -1 volume set not found
  939. @return -2 on failure
  940. """
  941. if not GVL_vol_exists(id):
  942. return -1
  943. ret = GVL_isosurf_set_drawres(id, res, res, res)
  944. if ret < 0:
  945. return -2
  946. return 1
  947. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  948. """!Set inout mode
  949. @param inout mode true/false
  950. @return 1 on success
  951. @return -1 volume set not found
  952. @return -2 isosurface not found
  953. @return -3 on failure
  954. """
  955. if not GVL_vol_exists(id):
  956. return -1
  957. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  958. return -2
  959. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  960. if ret < 0:
  961. return -3
  962. return 1
  963. def GetCPlaneCurrent(self):
  964. return Nviz_get_current_cplane(self.data)
  965. def GetCPlanesCount(self):
  966. """!Returns number of cutting planes"""
  967. return Nviz_num_cplanes(self.data)
  968. def GetCPlaneRotation(self):
  969. """!Returns rotation parameters of current cutting plane"""
  970. x, y, z = c_float(), c_float(), c_float()
  971. current = Nviz_get_current_cplane(self.data)
  972. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  973. return x.value, y.value, z.value
  974. def GetCPlaneTranslation(self):
  975. """!Returns translation parameters of current cutting plane"""
  976. x, y, z = c_float(), c_float(), c_float()
  977. current = Nviz_get_current_cplane(self.data)
  978. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  979. return x.value, y.value, z.value
  980. def SetCPlaneRotation(self, x, y, z):
  981. """!Set current clip plane rotation
  982. @param x,y,z rotation parameters
  983. """
  984. current = Nviz_get_current_cplane(self.data)
  985. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  986. Nviz_draw_cplane(self.data, -1, -1)
  987. def SetCPlaneTranslation(self, x, y, z):
  988. """!Set current clip plane translation
  989. @param x,y,z translation parameters
  990. """
  991. current = Nviz_get_current_cplane(self.data)
  992. Nviz_set_cplane_translation(self.data, current, x, y, z)
  993. Nviz_draw_cplane(self.data, -1, -1)
  994. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  995. current, x, y, z)
  996. def SetCPlaneInteractively(self, x, y):
  997. current = Nviz_get_current_cplane(self.data)
  998. ret = Nviz_set_cplane_here(self.data, current, x, y)
  999. if ret:
  1000. Nviz_draw_cplane(self.data, -1, -1)
  1001. x, y, z = self.GetCPlaneTranslation()
  1002. return x, y, z
  1003. else:
  1004. return None, None, None
  1005. def SelectCPlane(self, index):
  1006. """!Select cutting plane
  1007. @param index index of cutting plane
  1008. """
  1009. Nviz_on_cplane(self.data, index)
  1010. def UnselectCPlane(self, index):
  1011. """!Unselect cutting plane
  1012. @param index index of cutting plane
  1013. """
  1014. Nviz_off_cplane(self.data, index)
  1015. def SetFenceColor(self, index):
  1016. """!Select current cutting plane
  1017. @param index type of fence - from 0 (off) to 4
  1018. """
  1019. Nviz_set_fence_color(self.data, index)
  1020. def GetXYRange(self):
  1021. """!Get xy range"""
  1022. return Nviz_get_xyrange(self.data)
  1023. def GetZRange(self):
  1024. """!Get z range"""
  1025. min, max = c_float(), c_float()
  1026. Nviz_get_zrange(self.data, byref(min), byref(max))
  1027. return min.value, max.value
  1028. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  1029. """!Save current GL screen to ppm/tif file
  1030. @param filename file name
  1031. @param width image width
  1032. @param height image height
  1033. @param itype image type ('ppm' or 'tif')
  1034. """
  1035. widthOrig = self.width
  1036. heightOrig = self.height
  1037. self.ResizeWindow(width, height)
  1038. GS_clear(Nviz_get_bgcolor(self.data))
  1039. self.Draw(False, -1)
  1040. if itype == 'ppm':
  1041. GS_write_ppm(filename)
  1042. else:
  1043. GS_write_tif(filename)
  1044. self.ResizeWindow(widthOrig, heightOrig)
  1045. def DrawLightingModel(self):
  1046. """!Draw lighting model"""
  1047. if self.showLight:
  1048. Nviz_draw_model(self.data)
  1049. def DrawFringe(self):
  1050. """!Draw fringe"""
  1051. Nviz_draw_fringe(self.data)
  1052. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1053. """!Set fringe
  1054. @param sid surface id
  1055. @param color color
  1056. @param elev elevation (height)
  1057. @param nw,ne,sw,se fringe edges (turn on/off)
  1058. """
  1059. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1060. Nviz_set_fringe(self.data,
  1061. sid, Nviz_color_from_str(scolor),
  1062. elev, int(nw), int(ne), int(sw), int(se))
  1063. def DrawArrow(self):
  1064. """!Draw north arrow
  1065. """
  1066. return Nviz_draw_arrow(self.data)
  1067. def SetArrow(self, sx, sy, size, color):
  1068. """!Set north arrow from canvas coordinates
  1069. @param sx,sy canvas coordinates
  1070. @param size arrow length
  1071. @param color arrow color
  1072. """
  1073. return Nviz_set_arrow(self.data, sx, sy, size, Nviz_color_from_str(color))
  1074. def DeleteArrow(self):
  1075. """!Delete draw north arrow
  1076. """
  1077. Nviz_delete_arrow(self.data)
  1078. def GetPointOnSurface(self, sx, sy):
  1079. """!Get point on surface
  1080. @param sx,sy canvas coordinates (LL)
  1081. """
  1082. sid = c_int()
  1083. x = c_float()
  1084. y = c_float()
  1085. z = c_float()
  1086. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1087. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1088. if num == 0:
  1089. return (None, None, None, None)
  1090. return (sid.value, x.value, y.value, z.value)
  1091. def QueryMap(self, sx, sy):
  1092. """!Query surface map
  1093. @param sx,sy canvas coordinates (LL)
  1094. """
  1095. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1096. if not sid:
  1097. return None
  1098. catstr = create_string_buffer(256)
  1099. valstr = create_string_buffer(256)
  1100. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1101. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1102. return { 'id' : sid,
  1103. 'x' : x,
  1104. 'y' : y,
  1105. 'z' : z,
  1106. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1107. 'color' : valstr.value }
  1108. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1109. """!Get distance measured along surface"""
  1110. d = c_float()
  1111. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1112. byref(d), int(useExag))
  1113. return d.value