wxnviz.py 34 KB

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