wxnviz.py 33 KB

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