wxnviz.py 36 KB

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