wxnviz.py 33 KB

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