wxnviz.py 40 KB

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