wxnviz.py 33 KB

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