wxnviz.py 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. try:
  18. from grass.lib.grass import *
  19. from grass.lib.ogsf import *
  20. from grass.lib.nviz import *
  21. except ImportError, e:
  22. print >> sys.stderr, "\nWARNING: Nviz extension (3D view mode) disabled. Reason: %s" % e
  23. sys.stderr.flush()
  24. from debug import Debug
  25. class Nviz(object):
  26. def __init__(self, log):
  27. """!Initialize Nviz class instance
  28. @param log logging area
  29. """
  30. self.log = log
  31. G_gisinit("")
  32. # G_set_error_routine(&print_error)
  33. # G_set_percent_routine(poiter(print_percent))
  34. GS_libinit()
  35. GVL_libinit()
  36. self.data_obj = nv_data()
  37. self.data = pointer(self.data_obj)
  38. self.width = self.height = -1
  39. self.showLight = False
  40. Debug.msg(1, "Nviz::Nviz()")
  41. def __del__(self):
  42. """!Destroy Nviz class instance"""
  43. # G_unset_error_routine()
  44. # G_unset_percent_routine()
  45. del self.data
  46. del self.data_obj
  47. self.log = None
  48. def ResizeWindow(self, width, height):
  49. """!GL canvas resized
  50. @param width window width
  51. @param height window height
  52. @return 1 on success
  53. @return 0 on failure (window resized by default to 20x20 px)
  54. """
  55. self.width = width
  56. self.height = height
  57. Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
  58. width, height)
  59. return Nviz_resize_window(width, height)
  60. def SetViewDefault(self):
  61. """!Set default view (based on loaded data)
  62. @return z-exag value, default, min and max height
  63. """
  64. # determine z-exag
  65. z_exag = Nviz_get_exag()
  66. Nviz_change_exag(self.data, z_exag)
  67. # determine height
  68. hdef = c_float()
  69. hmin = c_float()
  70. hmax = c_float()
  71. Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
  72. Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
  73. hdef.value, hmin.value, hmax.value)
  74. hdef.value = 805
  75. hmin.value = -2944.302979
  76. hmax.value = 4555.696777
  77. return (z_exag, hdef.value, hmin.value, hmax.value)
  78. def SetView(self, x, y, height, persp, twist):
  79. """!Change view settings
  80. @param x,y position
  81. @param height
  82. @param persp perpective
  83. @param twist
  84. """
  85. Nviz_set_viewpoint_height(height)
  86. Nviz_set_viewpoint_position(x, y)
  87. Nviz_set_viewpoint_twist(twist)
  88. Nviz_set_viewpoint_persp(persp)
  89. Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
  90. x, y, height, persp, twist)
  91. def SetZExag(self, z_exag):
  92. """!Set z-exag value
  93. @param z_exag value
  94. @return 1
  95. """
  96. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  97. return Nviz_change_exag(self.data, z_exag)
  98. def Draw(self, quick, quick_mode):
  99. """!Draw canvas
  100. Draw quick mode:
  101. - DRAW_QUICK_SURFACE
  102. - DRAW_QUICK_VLINES
  103. - DRAW_QUICK_VPOINTS
  104. - DRAW_QUICK_VOLUME
  105. @param quick if true draw in wiremode
  106. @param quick_mode quick mode
  107. """
  108. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  109. Nviz_draw_cplane(self.data, -1, -1) # ?
  110. if quick:
  111. Nviz_draw_quick(self.data, quick_mode)
  112. else:
  113. Nviz_draw_all(self.data)
  114. def EraseMap(self):
  115. """!Erase map display (with background color)
  116. """
  117. GS_clear(self.data.bgcolor)
  118. Debug.msg(1, "Nviz::EraseMap()")
  119. def InitView(self):
  120. """!Initialize view"""
  121. # initialize nviz data
  122. Nviz_init_data(self.data)
  123. # define default attributes for map objects
  124. Nviz_set_surface_attr_default()
  125. # set background color
  126. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white")) # TODO
  127. self.SetBgColor("white")
  128. # initialize view, lights
  129. Nviz_init_view(self.data)
  130. # clear window
  131. GS_clear(self.data.bgcolor)
  132. Debug.msg(1, "Nviz::InitView()")
  133. def SetBgColor(self, color_str):
  134. """!Set background color
  135. @param color_str color string
  136. """
  137. self.data.bgcolor = Nviz_color_from_str(color_str)
  138. def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
  139. """!Change lighting settings
  140. @param x,y,z position
  141. @param color light color (as string)
  142. @param bright light brightness
  143. @param ambient light ambient
  144. @param w local coordinate (default to 0)
  145. """
  146. Nviz_set_light_position(self.data, lid, x, y, z, w)
  147. Nviz_set_light_bright(self.data, lid, bright)
  148. Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
  149. Nviz_set_light_ambient(self.data, lid, ambient)
  150. def LoadSurface(self, name, color_name, color_value):
  151. """!Load raster map (surface)
  152. @param name raster map name
  153. @param color_name raster map for color (None for color_value)
  154. @param color_value color string (named color or RGB triptet)
  155. @return object id
  156. @return -1 on failure
  157. """
  158. mapset = G_find_raster2(name, "")
  159. if mapset is None:
  160. G_warning(_("Raster map <%s> not found"), name)
  161. return -1
  162. # topography
  163. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  164. G_fully_qualified_name(name, mapset), 0.0,
  165. self.data)
  166. if color_name: # check for color map
  167. mapset = G_find_raster2(color_name, "")
  168. if mapset is None:
  169. G_warning(_("Raster map <%s> not found"), color_name)
  170. GS_delete_surface(id)
  171. return -1
  172. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  173. G_fully_qualified_name(color_name, mapset), -1.0,
  174. self.data)
  175. elif color_value: # check for color value
  176. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  177. None, Nviz_color_from_str(color_value),
  178. self.data)
  179. else: # use by default elevation map for coloring
  180. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  181. G_fully_qualified_name(name, mapset), -1.0,
  182. self.data)
  183. # if (i > 1)
  184. # set_default_wirecolors(self.data, i)
  185. # focus on loaded self.data
  186. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  187. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  188. return id
  189. def UnloadSurface(self, id):
  190. """!Unload surface
  191. @param id surface id
  192. @return 1 on success
  193. @return 0 on failure
  194. """
  195. if not GS_surf_exists(id):
  196. return 0
  197. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  198. if GS_delete_surface(id) < 0:
  199. return 0
  200. return 1
  201. def LoadVector(self, name, points):
  202. """!Load vector map overlay
  203. @param name vector map name
  204. @param points if true load 2d points rather then 2d lines
  205. @return object id
  206. @return -1 on failure
  207. """
  208. if GS_num_surfs() == 0: # load base surface if no loaded
  209. Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  210. nsurf = c_int()
  211. surf_list = GS_get_surf_list(byref(nsurf))
  212. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  213. mapset = G_find_vector2 (name, "")
  214. if mapset is None:
  215. G_warning(_("Vector map <%s> not found"),
  216. name)
  217. if points:
  218. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  219. G_fully_qualified_name(name, mapset), 0.0,
  220. self.data)
  221. else:
  222. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  223. G_fully_qualified_name(name, mapset), 0.0,
  224. self.data)
  225. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  226. return id
  227. def UnloadVector(self, id, points):
  228. """!Unload vector set
  229. @param id vector set id
  230. @param points vector points or lines set
  231. @return 1 on success
  232. @return 0 on failure
  233. """
  234. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  235. if points:
  236. if not GP_site_exists(id):
  237. return 0
  238. if GP_delete_site(id) < 0:
  239. return 0
  240. else:
  241. if not GV_vect_exists(id):
  242. return 0
  243. if GV_delete_vector(id) < 0:
  244. return 0
  245. return 1
  246. def LoadVolume(self, name, color_name, color_value):
  247. """!Load 3d raster map (volume)
  248. @param name 3d raster map name
  249. @param color_name 3d raster map for color (None for color_value)
  250. @param color_value color string (named color or RGB triptet)
  251. @return object id
  252. @return -1 on failure
  253. """
  254. mapset = G_find_grid3(name, "")
  255. if mapset is None:
  256. G_warning(_("3d raster map <%s> not found"),
  257. name)
  258. return -1
  259. # topography
  260. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  261. G_fully_qualified_name(name, mapset), 0.0,
  262. self.data)
  263. if color_name: # check for color map
  264. mapset = G_find_grid3(color_name, "")
  265. if mapset is None:
  266. G_warning(_("3d raster map <%s> not found"),
  267. color_name)
  268. GVL_delete_vol(id)
  269. return -1
  270. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  271. G_fully_qualified_name(color_name, mapset), -1.0,
  272. self.data)
  273. elif color_value: # check for color value
  274. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  275. None, Nviz_color_from_str(color_value),
  276. self.data)
  277. else: # use by default elevation map for coloring
  278. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  279. G_fully_qualified_name(name, mapset), -1.0,
  280. self.data)
  281. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  282. return id
  283. def UnloadVolume(self, id):
  284. """!Unload volume
  285. @param id volume id
  286. @return 1 on success
  287. @return 0 on failure
  288. """
  289. if not GVL_vol_exists(id):
  290. return 0
  291. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  292. if GVL_delete_vol(id) < 0:
  293. return 0
  294. return 1
  295. def SetSurfaceTopo(self, id, map, value):
  296. """!Set surface topography
  297. @param id surface id
  298. @param map if true use map otherwise constant
  299. @param value map name of value
  300. @return 1 on success
  301. @return -1 surface not found
  302. @return -2 setting attributes failed
  303. """
  304. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  305. def SetSurfaceColor(self, id, map, value):
  306. """!Set surface color
  307. @param id surface id
  308. @param map if true use map otherwise constant
  309. @param value map name of value
  310. @return 1 on success
  311. @return -1 surface not found
  312. @return -2 setting attributes failed
  313. """
  314. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  315. def SetSurfaceMask(self, id, invert, value):
  316. """!Set surface mask
  317. @todo invert
  318. @param id surface id
  319. @param invert if true invert mask
  320. @param value map name of value
  321. @return 1 on success
  322. @return -1 surface not found
  323. @return -2 setting attributes failed
  324. """
  325. return self.SetSurfaceAttr(id, ATT_MASK, true, value)
  326. def SetSurfaceTransp(self, id, map, value):
  327. """!Set surface mask
  328. @todo invert
  329. @param id surface id
  330. @param map if true use map otherwise constant
  331. @param value map name of value
  332. @return 1 on success
  333. @return -1 surface not found
  334. @return -2 setting attributes failed
  335. """
  336. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  337. def SetSurfaceShine(self, id, map, value):
  338. """!Set surface shininess
  339. @param id surface id
  340. @param map if true use map otherwise constant
  341. @param value map name of value
  342. @return 1 on success
  343. @return -1 surface not found
  344. @return -2 setting attributes failed
  345. """
  346. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  347. def SetSurfaceEmit(self, id, map, value):
  348. """!Set surface emission
  349. @param id surface id
  350. @param map if true use map otherwise constant
  351. @param value map name of value
  352. @return 1 on success
  353. @return -1 surface not found
  354. @return -2 setting attributes failed
  355. """
  356. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  357. def SetSurfaceAttr(self, id, attr, map, value):
  358. """!Set surface attribute
  359. @param id surface id
  360. @param attr attribute desc
  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. if not GS_surf_exists(id):
  368. return -1
  369. if map:
  370. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  371. value, -1.0, self.data)
  372. else:
  373. if attr == ATT_COLOR:
  374. val = Nviz_color_from_str(value)
  375. else:
  376. val = float(value)
  377. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  378. None, val, self.data)
  379. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  380. id, attr, map, value)
  381. return 1 if ret else -2
  382. def UnsetSurfaceMask(self, id):
  383. """!Unset surface mask
  384. @param id surface id
  385. @return 1 on success
  386. @return -1 surface not found
  387. @return -2 setting attributes failed
  388. @return -1 on failure
  389. """
  390. return self.UnsetSurfaceAttr(id, ATT_MASK)
  391. def UnsetSurfaceTransp(self, id):
  392. """!Unset surface transparency
  393. @param id surface id
  394. @return 1 on success
  395. @return -1 surface not found
  396. @return -2 setting attributes failed
  397. """
  398. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  399. def UnsetSurfaceEmit(self, id):
  400. """!Unset surface emission
  401. @param id surface id
  402. @return 1 on success
  403. @return -1 surface not found
  404. @return -2 setting attributes failed
  405. """
  406. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  407. def UnsetSurfaceAttr(self, id, attr):
  408. """!Unset surface attribute
  409. @param id surface id
  410. @param attr attribute descriptor
  411. @return 1 on success
  412. @return -1 surface not found
  413. @return -2 setting attributes failed
  414. """
  415. if not GS_surf_exists(id):
  416. return -1
  417. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  418. id, attr)
  419. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  420. return 1 if ret else -2
  421. def SetSurfaceRes(self, id, fine, coarse):
  422. """!Set surface resolution
  423. @param id surface id
  424. @param fine x/y fine resolution
  425. @param coarse x/y coarse resolution
  426. @return 1 on success
  427. @return -1 surface not found
  428. @return -2 setting attributes failed
  429. """
  430. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  431. id, fine, coarse)
  432. if id > 0:
  433. if not GS_surf_exists(id):
  434. return -1
  435. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  436. return -2
  437. else:
  438. GS_setall_drawres(fine, fine, coarse, coarse)
  439. return 1
  440. def SetSurfaceStyle(self, id, style):
  441. """!Set draw style
  442. Draw styles:
  443. - DM_GOURAUD
  444. - DM_FLAT
  445. - DM_FRINGE
  446. - DM_WIRE
  447. - DM_COL_WIRE
  448. - DM_POLY
  449. - DM_WIRE_POLY
  450. - DM_GRID_WIRE
  451. - DM_GRID_SURF
  452. @param id surface id (<= 0 for all)
  453. @param style draw style
  454. @return 1 on success
  455. @return -1 surface not found
  456. @return -2 setting attributes failed
  457. """
  458. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  459. id, style)
  460. if id > 0:
  461. if not GS_surf_exists(id):
  462. return -1
  463. if GS_set_drawmode(id, style) < 0:
  464. return -2
  465. return 1
  466. if GS_setall_drawmode(style) < 0:
  467. return -2
  468. return 1
  469. def SetWireColor(self, id, color_str):
  470. """!Set color of wire
  471. @todo all
  472. @param surface id (< 0 for all)
  473. @param color color string (R:G:B)
  474. @return 1 on success
  475. @return -1 surface not found
  476. @return -2 setting attributes failed
  477. @return 1 on success
  478. @return 0 on failure
  479. """
  480. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  481. id, color_str)
  482. color = Nviz_color_from_str(color_str)
  483. if id > 0:
  484. if not GS_surf_exists(id):
  485. return -1
  486. GS_set_wire_color(id, color)
  487. else:
  488. nsurfs = c_int()
  489. surf_list = GS_get_surf_list(byref(nsurfs))
  490. for i in xrange(nsurfs.value):
  491. id = surf_list[i]
  492. GS_set_wire_color(id, color)
  493. G_free(surf_list)
  494. surf_list = None
  495. return 1
  496. def GetSurfacePosition(self, id):
  497. """!Get surface position
  498. @param id surface id
  499. @return x,y,z
  500. @return zero-length vector on error
  501. """
  502. if not GS_surf_exists(id):
  503. return []
  504. x, y, z = c_float(), c_float(), c_float()
  505. GS_get_trans(id, byref(x), byref(y), byref(z))
  506. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  507. id, x.value, y.value, z.value)
  508. return [x.value, y.value, z.value]
  509. def SetSurfacePosition(self, id, x, y, z):
  510. """!Set surface position
  511. @param id surface id
  512. @param x,y,z translation values
  513. @return 1 on success
  514. @return -1 surface not found
  515. @return -2 setting position failed
  516. """
  517. if not GS_surf_exists(id):
  518. return -1
  519. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  520. id, x, y, z)
  521. GS_set_trans(id, x, y, z)
  522. return 1
  523. def SetVectorLineMode(self, id, color_str, width, flat):
  524. """!Set mode of vector line overlay
  525. @param id vector id
  526. @param color_str color string
  527. @param width line width
  528. @param flat display flat or on surface
  529. @return -1 vector set not found
  530. @return -2 on failure
  531. @return 1 on success
  532. """
  533. if not GV_vect_exists(id):
  534. return -1
  535. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  536. id, color_str, width, flat)
  537. color = Nviz_color_from_str(color_str)
  538. # use memory by default
  539. if GV_set_vectmode(id, 1, color, width, flat) < 0:
  540. return -2
  541. return 1
  542. def SetVectorLineHeight(self, id, height):
  543. """!Set vector height above surface (lines)
  544. @param id vector set id
  545. @param height
  546. @return -1 vector set not found
  547. @return 1 on success
  548. """
  549. if not GV_vect_exists(id):
  550. return -1
  551. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  552. id, height)
  553. GV_set_trans(id, 0.0, 0.0, height)
  554. return 1
  555. def SetVectorLineSurface(self, id, surf_id):
  556. """!Set reference surface of vector set (lines)
  557. @param id vector set id
  558. @param surf_id surface id
  559. @return 1 on success
  560. @return -1 vector set not found
  561. @return -2 surface not found
  562. @return -3 on failure
  563. """
  564. if not GV_vect_exists(id):
  565. return -1
  566. if not GS_surf_exists(surf_id):
  567. return -2
  568. if GV_select_surf(id, surf_id) < 0:
  569. return -3
  570. return 1
  571. def SetVectorPointMode(self, id, color_str, width, size, marker):
  572. """!Set mode of vector point overlay
  573. @param id vector id
  574. @param color_str color string
  575. @param width line width
  576. @param flat
  577. @return -1 vector set not found
  578. """
  579. if not GP_site_exists(id):
  580. return -1
  581. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  582. "width=%d, size=%f, marker=%d",
  583. id, color_str, width, size, marker)
  584. color = Nviz_color_from_str(color_str)
  585. if GP_set_style(id, color, width, size, marker) < 0:
  586. return -2
  587. return 1
  588. def SetVectorPointHeight(self, id, height):
  589. """!Set vector height above surface (points)
  590. @param id vector set id
  591. @param height
  592. @return -1 vector set not found
  593. @return 1 on success
  594. """
  595. if not GP_site_exists(id):
  596. return -1
  597. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  598. id, height)
  599. GP_set_trans(id, 0.0, 0.0, height)
  600. return 1
  601. def SetVectorPointSurface(self, id, surf_id):
  602. """!Set reference surface of vector set (points)
  603. @param id vector set id
  604. @param surf_id surface id
  605. @return 1 on success
  606. @return -1 vector set not found
  607. @return -2 surface not found
  608. @return -3 on failure
  609. """
  610. if not GP_site_exists(id):
  611. return -1
  612. if not GS_surf_exists(surf_id):
  613. return -2
  614. if GP_select_surf(id, surf_id) < 0:
  615. return -3
  616. return 1
  617. def AddIsosurface(self, id, level):
  618. """!Add new isosurface
  619. @param id volume id
  620. @param level isosurface level (topography)
  621. @return -1 on failure
  622. @return 1 on success
  623. """
  624. if not GVL_vol_exists(id):
  625. return -1
  626. if GVL_isosurf_add(id) < 0:
  627. return -1
  628. # set topography level
  629. nisosurfs = GVL_isosurf_num_isosurfs(id)
  630. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  631. def DeleteIsosurface(self, id, isosurf_id):
  632. """!Delete isosurface
  633. @param id volume id
  634. @param isosurf_id isosurface id
  635. @return 1 on success
  636. @return -1 volume not found
  637. @return -2 isosurface not found
  638. @return -3 on failure
  639. """
  640. if not GVL_vol_exists(id):
  641. return -1
  642. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  643. return -2
  644. ret = GVL_isosurf_del(id, isosurf_id)
  645. return -3 if ret < 0 else 1
  646. def MoveIsosurface(self, id, isosurf_id, up):
  647. """!Move isosurface up/down in the list
  648. @param id volume id
  649. @param isosurf_id isosurface id
  650. @param up if true move up otherwise down
  651. @return 1 on success
  652. @return -1 volume not found
  653. @return -2 isosurface not found
  654. @return -3 on failure
  655. """
  656. if not GVL_vol_exists(id):
  657. return -1
  658. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  659. return -2
  660. if up:
  661. ret = GVL_isosurf_move_up(id, isosurf_id)
  662. else:
  663. ret = GVL_isosurf_move_down(id, isosurf_id)
  664. return -3 if ret < 0 else 1
  665. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  666. """!Set isosurface color
  667. @param id volume id
  668. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  669. @param map if true use map otherwise constant
  670. @param value map name of value
  671. @return 1 on success
  672. @return -1 volume not found
  673. @return -2 isosurface not found
  674. @return -3 on failure
  675. """
  676. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  677. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  678. """!Set isosurface mask
  679. @todo invert
  680. @param id volume id
  681. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  682. @param invert true for invert mask
  683. @param value map name to be used for mask
  684. @return 1 on success
  685. @return -1 volume not found
  686. @return -2 isosurface not found
  687. @return -3 on failure
  688. """
  689. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, true, value)
  690. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  691. """!Set isosurface transparency
  692. @param id volume id
  693. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  694. @param map if true use map otherwise constant
  695. @param value map name of value
  696. @return 1 on success
  697. @return -1 volume not found
  698. @return -2 isosurface not found
  699. @return -3 on failure
  700. """
  701. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  702. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  703. """!Set isosurface shininess
  704. @param id volume id
  705. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  706. @param map if true use map otherwise constant
  707. @param value map name of value
  708. @return 1 on success
  709. @return -1 volume not found
  710. @return -2 isosurface not found
  711. @return -3 on failure
  712. """
  713. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  714. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  715. """!Set isosurface emission
  716. @param id volume id
  717. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  718. @param map if true use map otherwise constant
  719. @param value map name of value
  720. @return 1 on success
  721. @return -1 volume not found
  722. @return -2 isosurface not found
  723. @return -3 on failure
  724. """
  725. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  726. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  727. """!Set isosurface attribute
  728. @param id volume id
  729. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  730. @param attr attribute desc
  731. @param map if true use map otherwise constant
  732. @param value map name of value
  733. @return 1 on success
  734. @return -1 volume not found
  735. @return -2 isosurface not found
  736. @return -3 setting attributes failed
  737. """
  738. if not GVL_vol_exists(id):
  739. return -1
  740. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  741. return -2
  742. if map:
  743. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  744. else:
  745. if attr == ATT_COLOR:
  746. val = Nviz_color_from_str(value)
  747. else:
  748. val = float(value)
  749. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  750. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  751. "attr=%d, map=%d, value=%s",
  752. id, isosurf_id, attr, map, value)
  753. return 1 if ret > 0 else -2
  754. def UnsetIsosurfaceMask(self, id, isosurf_id):
  755. """!Unset isosurface mask
  756. @param id volume id
  757. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  758. @return 1 on success
  759. @return -1 volume not found
  760. @return -2 isosurface not found
  761. @return -3 setting attributes failed
  762. """
  763. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  764. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  765. """!Unset isosurface transparency
  766. @param id volume id
  767. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  768. @return 1 on success
  769. @return -1 volume not found
  770. @return -2 isosurface not found
  771. @return -3 setting attributes failed
  772. """
  773. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  774. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  775. """!Unset isosurface emission
  776. @param id volume id
  777. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  778. @return 1 on success
  779. @return -1 volume not found
  780. @return -2 isosurface not found
  781. @return -3 setting attributes failed
  782. """
  783. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  784. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  785. """!Unset surface attribute
  786. @param id surface id
  787. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  788. @param attr attribute descriptor
  789. @return 1 on success
  790. @return -1 volume not found
  791. @return -2 isosurface not found
  792. @return -2 on failure
  793. """
  794. if not GVL_vol_exists(id):
  795. return -1
  796. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  797. return -2
  798. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  799. id, isosurf_id, attr)
  800. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  801. return 1 if ret > 0 else -2
  802. def SetIsosurfaceMode(self, id, mode):
  803. """!Set draw mode for isosurfaces
  804. @param mode
  805. @return 1 on success
  806. @return -1 volume set not found
  807. @return -2 on failure
  808. """
  809. if not GVL_vol_exists(id):
  810. return -1
  811. ret = GVL_isosurf_set_drawmode(id, mode)
  812. return -2 if ret < 0 else 1
  813. def SetIsosurfaceRes(self, id, res):
  814. """!Set draw resolution for isosurfaces
  815. @param res resolution value
  816. @return 1 on success
  817. @return -1 volume set not found
  818. @return -2 on failure
  819. """
  820. if not GVL_vol_exists(id):
  821. return -1
  822. ret = GVL_isosurf_set_drawres(id, res, res, res)
  823. return -2 if ret < 0 else 1
  824. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  825. """!Save current GL screen to ppm/tif file
  826. @param filename file name
  827. @param width image width
  828. @param height image height
  829. @param itype image type ('ppm' or 'tif')
  830. """
  831. widthOrig = self.width
  832. heightOrig = self.height
  833. self.ResizeWindow(width, height)
  834. GS_clear(self.data.bgcolor)
  835. self.Draw(False, -1)
  836. if itype == 'ppm':
  837. GS_write_ppm(filename)
  838. else:
  839. GS_write_tif(filename)
  840. self.ResizeWindow(widthOrig, heightOrig)
  841. def DrawLightingModel(self):
  842. """!Draw lighting model"""
  843. if self.showLight:
  844. GS_draw_lighting_model()
  845. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  846. """!Set fringe
  847. @param sid surface id
  848. @param color color
  849. @param elev elevation (height)
  850. @param nw,ne,sw,se fringe edges (turn on/off)
  851. """
  852. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  853. Nviz_set_fringe(self.data,
  854. sid, Nviz_color_from_str(scolor),
  855. elev, int(nw), int(ne), int(sw), int(se))