wxnviz.py 33 KB

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