wxnviz.py 36 KB

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