wxnviz.py 36 KB

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