wxnviz.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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 LookAtCenter(self):
  116. """!Center view at center of displayed surface"""
  117. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  118. Debug.msg(3, "Nviz::LookAtCenter()")
  119. def GetFocus(self):
  120. """!Get focus"""
  121. Debug.msg(3, "Nviz::GetFocus()")
  122. if Nviz_has_focus(self.data):
  123. x = c_float()
  124. y = c_float()
  125. z = c_float()
  126. Nviz_get_focus(self.data, byref(x), byref(y), byref(z))
  127. return x.value, y.value, z.value
  128. else:
  129. return -1, -1, -1
  130. def SetFocus(self, x, y, z):
  131. """!Set focus"""
  132. Debug.msg(3, "Nviz::SetFocus()")
  133. Nviz_set_focus(self.data, x, y, z)
  134. def SetZExag(self, z_exag):
  135. """!Set z-exag value
  136. @param z_exag value
  137. @return 1
  138. """
  139. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  140. return Nviz_change_exag(self.data, z_exag)
  141. def Draw(self, quick, quick_mode):
  142. """!Draw canvas
  143. Draw quick mode:
  144. - DRAW_QUICK_SURFACE
  145. - DRAW_QUICK_VLINES
  146. - DRAW_QUICK_VPOINTS
  147. - DRAW_QUICK_VOLUME
  148. @param quick if true draw in wiremode
  149. @param quick_mode quick mode
  150. """
  151. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  152. Nviz_draw_cplane(self.data, -1, -1) # ?
  153. if quick:
  154. Nviz_draw_quick(self.data, quick_mode)
  155. else:
  156. Nviz_draw_all(self.data)
  157. def EraseMap(self):
  158. """!Erase map display (with background color)
  159. """
  160. Debug.msg(1, "Nviz::EraseMap()")
  161. GS_clear(Nviz_get_bgcolor(self.data))
  162. def InitView(self):
  163. """!Initialize view"""
  164. # initialize nviz data
  165. Nviz_init_data(self.data)
  166. # define default attributes for map objects
  167. Nviz_set_surface_attr_default()
  168. # set background color
  169. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white"))
  170. GS_clear(Nviz_get_bgcolor(self.data))
  171. # initialize view, lights
  172. Nviz_init_view(self.data)
  173. Debug.msg(1, "Nviz::InitView()")
  174. def SetBgColor(self, color_str):
  175. """!Set background color
  176. @param color_str color string
  177. """
  178. Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
  179. def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
  180. """!Change lighting settings
  181. @param x,y,z position
  182. @param color light color (as string)
  183. @param bright light brightness
  184. @param ambient light ambient
  185. @param w local coordinate (default to 0)
  186. """
  187. Nviz_set_light_position(self.data, lid, x, y, z, w)
  188. Nviz_set_light_bright(self.data, lid, bright)
  189. Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
  190. Nviz_set_light_ambient(self.data, lid, ambient)
  191. def LoadSurface(self, name, color_name, color_value):
  192. """!Load raster map (surface)
  193. @param name raster map name
  194. @param color_name raster map for color (None for color_value)
  195. @param color_value color string (named color or RGB triptet)
  196. @return object id
  197. @return -1 on failure
  198. """
  199. mapset = G_find_raster2(name, "")
  200. if mapset is None:
  201. G_warning(_("Raster map <%s> not found"), name)
  202. return -1
  203. # topography
  204. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  205. G_fully_qualified_name(name, mapset), 0.0,
  206. self.data)
  207. if color_name: # check for color map
  208. mapset = G_find_raster2(color_name, "")
  209. if mapset is None:
  210. G_warning(_("Raster map <%s> not found"), color_name)
  211. GS_delete_surface(id)
  212. return -1
  213. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  214. G_fully_qualified_name(color_name, mapset), -1.0,
  215. self.data)
  216. elif color_value: # check for color value
  217. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  218. None, Nviz_color_from_str(color_value),
  219. self.data)
  220. else: # use by default elevation map for coloring
  221. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  222. G_fully_qualified_name(name, mapset), -1.0,
  223. self.data)
  224. # if (i > 1)
  225. # set_default_wirecolors(self.data, i)
  226. # focus on loaded self.data
  227. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  228. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  229. return id
  230. def AddConstant(self, value, color):
  231. """!Add new constant surface"""
  232. id = Nviz_new_map_obj(MAP_OBJ_SURF, None, value, self.data)
  233. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  234. None, Nviz_color_from_str(color),
  235. self.data)
  236. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  237. Debug.msg(1, "Nviz::AddConstant(): id=%d", id)
  238. return id
  239. def UnloadSurface(self, id):
  240. """!Unload surface
  241. @param id surface id
  242. @return 1 on success
  243. @return 0 on failure
  244. """
  245. if not GS_surf_exists(id):
  246. return 0
  247. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  248. if GS_delete_surface(id) < 0:
  249. return 0
  250. return 1
  251. def LoadVector(self, name, points):
  252. """!Load vector map overlay
  253. @param name vector map name
  254. @param points if true load 2d points rather then 2d lines
  255. @return object id
  256. @return -1 on failure
  257. """
  258. if GS_num_surfs() == 0: # load base surface if no loaded
  259. Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  260. nsurf = c_int()
  261. surf_list = GS_get_surf_list(byref(nsurf))
  262. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  263. mapset = G_find_vector2 (name, "")
  264. if mapset is None:
  265. G_warning(_("Vector map <%s> not found"),
  266. name)
  267. if points:
  268. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  269. G_fully_qualified_name(name, mapset), 0.0,
  270. self.data)
  271. else:
  272. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  273. G_fully_qualified_name(name, mapset), 0.0,
  274. self.data)
  275. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  276. return id
  277. def UnloadVector(self, id, points):
  278. """!Unload vector set
  279. @param id vector set id
  280. @param points vector points or lines set
  281. @return 1 on success
  282. @return 0 on failure
  283. """
  284. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  285. if points:
  286. if not GP_site_exists(id):
  287. return 0
  288. if GP_delete_site(id) < 0:
  289. return 0
  290. else:
  291. if not GV_vect_exists(id):
  292. return 0
  293. if GV_delete_vector(id) < 0:
  294. return 0
  295. return 1
  296. def VectorSurfaceSelected(self, vid, sid):
  297. """!Check if surface is selected (currently unused)
  298. @param vid vector id
  299. @param sid surface id
  300. @return True if selected
  301. @return False if not selected
  302. """
  303. selected = GV_surf_is_selected(vid, sid)
  304. Debug.msg(1, "Nviz::VectorSurfaceSelected(): vid=%s, sid=%d -> selected=%d", vid, sid, selected)
  305. return selected
  306. def LoadVolume(self, name, color_name, color_value):
  307. """!Load 3d raster map (volume)
  308. @param name 3d raster map name
  309. @param color_name 3d raster map for color (None for color_value)
  310. @param color_value color string (named color or RGB triptet)
  311. @return object id
  312. @return -1 on failure
  313. """
  314. mapset = G_find_grid3(name, "")
  315. if mapset is None:
  316. G_warning(_("3d raster map <%s> not found"),
  317. name)
  318. return -1
  319. # topography
  320. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  321. G_fully_qualified_name(name, mapset), 0.0,
  322. self.data)
  323. if color_name: # check for color map
  324. mapset = G_find_grid3(color_name, "")
  325. if mapset is None:
  326. G_warning(_("3d raster map <%s> not found"),
  327. color_name)
  328. GVL_delete_vol(id)
  329. return -1
  330. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  331. G_fully_qualified_name(color_name, mapset), -1.0,
  332. self.data)
  333. elif color_value: # check for color value
  334. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  335. None, Nviz_color_from_str(color_value),
  336. self.data)
  337. else: # use by default elevation map for coloring
  338. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  339. G_fully_qualified_name(name, mapset), -1.0,
  340. self.data)
  341. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  342. return id
  343. def UnloadVolume(self, id):
  344. """!Unload volume
  345. @param id volume id
  346. @return 1 on success
  347. @return 0 on failure
  348. """
  349. if not GVL_vol_exists(id):
  350. return 0
  351. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  352. if GVL_delete_vol(id) < 0:
  353. return 0
  354. return 1
  355. def SetSurfaceTopo(self, id, map, value):
  356. """!Set surface topography
  357. @param id surface id
  358. @param map if true use map otherwise constant
  359. @param value map name of value
  360. @return 1 on success
  361. @return -1 surface not found
  362. @return -2 setting attributes failed
  363. """
  364. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  365. def SetSurfaceColor(self, id, map, value):
  366. """!Set surface color
  367. @param id surface id
  368. @param map if true use map otherwise constant
  369. @param value map name or value
  370. @return 1 on success
  371. @return -1 surface not found
  372. @return -2 setting attributes failed
  373. """
  374. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  375. def SetSurfaceMask(self, id, invert, value):
  376. """!Set surface mask
  377. @todo invert
  378. @param id surface id
  379. @param invert if true invert mask
  380. @param value map name of value
  381. @return 1 on success
  382. @return -1 surface not found
  383. @return -2 setting attributes failed
  384. """
  385. return self.SetSurfaceAttr(id, ATT_MASK, true, value)
  386. def SetSurfaceTransp(self, id, map, value):
  387. """!Set surface mask
  388. @todo invert
  389. @param id surface id
  390. @param map if true use map otherwise constant
  391. @param value map name of value
  392. @return 1 on success
  393. @return -1 surface not found
  394. @return -2 setting attributes failed
  395. """
  396. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  397. def SetSurfaceShine(self, id, map, value):
  398. """!Set surface shininess
  399. @param id surface id
  400. @param map if true use map otherwise constant
  401. @param value map name of value
  402. @return 1 on success
  403. @return -1 surface not found
  404. @return -2 setting attributes failed
  405. """
  406. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  407. def SetSurfaceEmit(self, id, map, value):
  408. """!Set surface emission (currently unused)
  409. @param id surface id
  410. @param map if true use map otherwise constant
  411. @param value map name of value
  412. @return 1 on success
  413. @return -1 surface not found
  414. @return -2 setting attributes failed
  415. """
  416. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  417. def SetSurfaceAttr(self, id, attr, map, value):
  418. """!Set surface attribute
  419. @param id surface id
  420. @param attr attribute desc
  421. @param map if true use map otherwise constant
  422. @param value map name of value
  423. @return 1 on success
  424. @return -1 surface not found
  425. @return -2 setting attributes failed
  426. """
  427. if not GS_surf_exists(id):
  428. return -1
  429. if map:
  430. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  431. value, -1.0, self.data)
  432. else:
  433. if attr == ATT_COLOR:
  434. val = Nviz_color_from_str(value)
  435. else:
  436. val = float(value)
  437. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  438. None, val, self.data)
  439. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  440. id, attr, map, value)
  441. if ret < 0:
  442. return -2
  443. return 1
  444. def UnsetSurfaceMask(self, id):
  445. """!Unset surface mask
  446. @param id surface id
  447. @return 1 on success
  448. @return -1 surface not found
  449. @return -2 setting attributes failed
  450. @return -1 on failure
  451. """
  452. return self.UnsetSurfaceAttr(id, ATT_MASK)
  453. def UnsetSurfaceTransp(self, id):
  454. """!Unset surface transparency
  455. @param id surface id
  456. @return 1 on success
  457. @return -1 surface not found
  458. @return -2 setting attributes failed
  459. """
  460. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  461. def UnsetSurfaceEmit(self, id):
  462. """!Unset surface emission (currently unused)
  463. @param id surface id
  464. @return 1 on success
  465. @return -1 surface not found
  466. @return -2 setting attributes failed
  467. """
  468. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  469. def UnsetSurfaceAttr(self, id, attr):
  470. """!Unset surface attribute
  471. @param id surface id
  472. @param attr attribute descriptor
  473. @return 1 on success
  474. @return -1 surface not found
  475. @return -2 setting attributes failed
  476. """
  477. if not GS_surf_exists(id):
  478. return -1
  479. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  480. id, attr)
  481. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  482. if ret < 0:
  483. return -2
  484. return 1
  485. def SetSurfaceRes(self, id, fine, coarse):
  486. """!Set surface resolution
  487. @param id surface id
  488. @param fine x/y fine resolution
  489. @param coarse x/y coarse resolution
  490. @return 1 on success
  491. @return -1 surface not found
  492. @return -2 setting attributes failed
  493. """
  494. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  495. id, fine, coarse)
  496. if id > 0:
  497. if not GS_surf_exists(id):
  498. return -1
  499. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  500. return -2
  501. else:
  502. GS_setall_drawres(fine, fine, coarse, coarse)
  503. return 1
  504. def SetSurfaceStyle(self, id, style):
  505. """!Set draw style
  506. Draw styles:
  507. - DM_GOURAUD
  508. - DM_FLAT
  509. - DM_FRINGE
  510. - DM_WIRE
  511. - DM_COL_WIRE
  512. - DM_POLY
  513. - DM_WIRE_POLY
  514. - DM_GRID_WIRE
  515. - DM_GRID_SURF
  516. @param id surface id (<= 0 for all)
  517. @param style draw style
  518. @return 1 on success
  519. @return -1 surface not found
  520. @return -2 setting attributes failed
  521. """
  522. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  523. id, style)
  524. if id > 0:
  525. if not GS_surf_exists(id):
  526. return -1
  527. if GS_set_drawmode(id, style) < 0:
  528. return -2
  529. return 1
  530. if GS_setall_drawmode(style) < 0:
  531. return -2
  532. return 1
  533. def SetWireColor(self, id, color_str):
  534. """!Set color of wire
  535. @todo all
  536. @param surface id (< 0 for all)
  537. @param color color string (R:G:B)
  538. @return 1 on success
  539. @return -1 surface not found
  540. @return -2 setting attributes failed
  541. @return 1 on success
  542. @return 0 on failure
  543. """
  544. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  545. id, color_str)
  546. color = Nviz_color_from_str(color_str)
  547. if id > 0:
  548. if not GS_surf_exists(id):
  549. return -1
  550. GS_set_wire_color(id, color)
  551. else:
  552. nsurfs = c_int()
  553. surf_list = GS_get_surf_list(byref(nsurfs))
  554. for i in xrange(nsurfs.value):
  555. id = surf_list[i]
  556. GS_set_wire_color(id, color)
  557. G_free(surf_list)
  558. surf_list = None
  559. return 1
  560. def GetSurfacePosition(self, id):
  561. """!Get surface position
  562. @param id surface id
  563. @return x,y,z
  564. @return zero-length vector on error
  565. """
  566. if not GS_surf_exists(id):
  567. return []
  568. x, y, z = c_float(), c_float(), c_float()
  569. GS_get_trans(id, byref(x), byref(y), byref(z))
  570. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  571. id, x.value, y.value, z.value)
  572. return [x.value, y.value, z.value]
  573. def SetSurfacePosition(self, id, x, y, z):
  574. """!Set surface position
  575. @param id surface id
  576. @param x,y,z translation values
  577. @return 1 on success
  578. @return -1 surface not found
  579. @return -2 setting position failed
  580. """
  581. if not GS_surf_exists(id):
  582. return -1
  583. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  584. id, x, y, z)
  585. GS_set_trans(id, x, y, z)
  586. return 1
  587. def SetVectorLineMode(self, id, color_str, width, flat):
  588. """!Set mode of vector line overlay
  589. @param id vector id
  590. @param color_str color string
  591. @param width line width
  592. @param flat display flat or on surface
  593. @return -1 vector set not found
  594. @return -2 on failure
  595. @return 1 on success
  596. """
  597. if not GV_vect_exists(id):
  598. return -1
  599. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  600. id, color_str, width, flat)
  601. color = Nviz_color_from_str(color_str)
  602. # use memory by default
  603. if GV_set_vectmode(id, 1, color, width, flat) < 0:
  604. return -2
  605. return 1
  606. def SetVectorLineHeight(self, id, height):
  607. """!Set vector height above surface (lines)
  608. @param id vector set id
  609. @param height
  610. @return -1 vector set not found
  611. @return 1 on success
  612. """
  613. if not GV_vect_exists(id):
  614. return -1
  615. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  616. id, height)
  617. GV_set_trans(id, 0.0, 0.0, height)
  618. return 1
  619. def SetVectorLineSurface(self, id, surf_id):
  620. """!Set reference surface of vector set (lines)
  621. @param id vector set id
  622. @param surf_id surface id
  623. @return 1 on success
  624. @return -1 vector set not found
  625. @return -2 surface not found
  626. @return -3 on failure
  627. """
  628. if not GV_vect_exists(id):
  629. return -1
  630. if not GS_surf_exists(surf_id):
  631. return -2
  632. if GV_select_surf(id, surf_id) < 0:
  633. return -3
  634. return 1
  635. def UnsetVectorLineSurface(self, id, surf_id):
  636. """!Unset reference surface of vector set (lines)
  637. @param id vector set id
  638. @param surf_id surface id
  639. @return 1 on success
  640. @return -1 vector set not found
  641. @return -2 surface not found
  642. @return -3 on failure
  643. """
  644. if not GV_vect_exists(id):
  645. return -1
  646. if not GS_surf_exists(surf_id):
  647. return -2
  648. if GV_unselect_surf(id, surf_id) < 0:
  649. return -3
  650. return 1
  651. def SetVectorPointMode(self, id, color_str, width, size, marker):
  652. """!Set mode of vector point overlay
  653. @param id vector id
  654. @param color_str color string
  655. @param width line width
  656. @param flat
  657. @return -1 vector set not found
  658. """
  659. if not GP_site_exists(id):
  660. return -1
  661. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  662. "width=%d, size=%f, marker=%d",
  663. id, color_str, width, size, marker)
  664. color = Nviz_color_from_str(color_str)
  665. if GP_set_style(id, color, width, size, marker) < 0:
  666. return -2
  667. return 1
  668. def SetVectorPointHeight(self, id, height):
  669. """!Set vector height above surface (points)
  670. @param id vector set id
  671. @param height
  672. @return -1 vector set not found
  673. @return 1 on success
  674. """
  675. if not GP_site_exists(id):
  676. return -1
  677. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  678. id, height)
  679. GP_set_trans(id, 0.0, 0.0, height)
  680. return 1
  681. def SetVectorPointSurface(self, id, surf_id):
  682. """!Set reference surface of vector set (points)
  683. @param id vector set id
  684. @param surf_id surface id
  685. @return 1 on success
  686. @return -1 vector set not found
  687. @return -2 surface not found
  688. @return -3 on failure
  689. """
  690. if not GP_site_exists(id):
  691. return -1
  692. if not GS_surf_exists(surf_id):
  693. return -2
  694. if GP_select_surf(id, surf_id) < 0:
  695. return -3
  696. return 1
  697. def UnsetVectorPointSurface(self, id, surf_id):
  698. """!Unset reference surface of vector set (points)
  699. @param id vector set id
  700. @param surf_id surface id
  701. @return 1 on success
  702. @return -1 vector set not found
  703. @return -2 surface not found
  704. @return -3 on failure
  705. """
  706. if not GP_site_exists(id):
  707. return -1
  708. if not GS_surf_exists(surf_id):
  709. return -2
  710. if GP_unselect_surf(id, surf_id) < 0:
  711. return -3
  712. return 1
  713. def AddIsosurface(self, id, level):
  714. """!Add new isosurface
  715. @param id volume id
  716. @param level isosurface level (topography)
  717. @return -1 on failure
  718. @return 1 on success
  719. """
  720. if not GVL_vol_exists(id):
  721. return -1
  722. if GVL_isosurf_add(id) < 0:
  723. return -1
  724. # set topography level
  725. nisosurfs = GVL_isosurf_num_isosurfs(id)
  726. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  727. def DeleteIsosurface(self, id, isosurf_id):
  728. """!Delete isosurface
  729. @param id volume id
  730. @param isosurf_id isosurface id
  731. @return 1 on success
  732. @return -1 volume not found
  733. @return -2 isosurface not found
  734. @return -3 on failure
  735. """
  736. if not GVL_vol_exists(id):
  737. return -1
  738. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  739. return -2
  740. ret = GVL_isosurf_del(id, isosurf_id)
  741. if ret < 0:
  742. return -3
  743. return 1
  744. def MoveIsosurface(self, id, isosurf_id, up):
  745. """!Move isosurface up/down in the list
  746. @param id volume id
  747. @param isosurf_id isosurface id
  748. @param up if true move up otherwise down
  749. @return 1 on success
  750. @return -1 volume not found
  751. @return -2 isosurface not found
  752. @return -3 on failure
  753. """
  754. if not GVL_vol_exists(id):
  755. return -1
  756. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  757. return -2
  758. if up:
  759. ret = GVL_isosurf_move_up(id, isosurf_id)
  760. else:
  761. ret = GVL_isosurf_move_down(id, isosurf_id)
  762. if ret < 0:
  763. return -3
  764. return 1
  765. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  766. """!Set isosurface color
  767. @param id volume id
  768. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  769. @param map if true use map otherwise constant
  770. @param value map name of value
  771. @return 1 on success
  772. @return -1 volume not found
  773. @return -2 isosurface not found
  774. @return -3 on failure
  775. """
  776. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  777. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  778. """!Set isosurface mask
  779. @todo invert
  780. @param id volume id
  781. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  782. @param invert true for invert mask
  783. @param value map name to be used for mask
  784. @return 1 on success
  785. @return -1 volume not found
  786. @return -2 isosurface not found
  787. @return -3 on failure
  788. """
  789. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, true, value)
  790. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  791. """!Set isosurface transparency
  792. @param id volume id
  793. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  794. @param map if true use map otherwise constant
  795. @param value map name of value
  796. @return 1 on success
  797. @return -1 volume not found
  798. @return -2 isosurface not found
  799. @return -3 on failure
  800. """
  801. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  802. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  803. """!Set isosurface shininess
  804. @param id volume id
  805. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  806. @param map if true use map otherwise constant
  807. @param value map name of value
  808. @return 1 on success
  809. @return -1 volume not found
  810. @return -2 isosurface not found
  811. @return -3 on failure
  812. """
  813. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  814. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  815. """!Set isosurface emission
  816. @param id volume id
  817. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  818. @param map if true use map otherwise constant
  819. @param value map name of value
  820. @return 1 on success
  821. @return -1 volume not found
  822. @return -2 isosurface not found
  823. @return -3 on failure
  824. """
  825. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  826. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  827. """!Set isosurface attribute
  828. @param id volume id
  829. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  830. @param attr attribute desc
  831. @param map if true use map otherwise constant
  832. @param value map name of value
  833. @return 1 on success
  834. @return -1 volume not found
  835. @return -2 isosurface not found
  836. @return -3 setting attributes failed
  837. """
  838. if not GVL_vol_exists(id):
  839. return -1
  840. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  841. return -2
  842. if map:
  843. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  844. else:
  845. if attr == ATT_COLOR:
  846. val = Nviz_color_from_str(value)
  847. else:
  848. val = float(value)
  849. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  850. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  851. "attr=%d, map=%d, value=%s",
  852. id, isosurf_id, attr, map, value)
  853. if ret < 0:
  854. return -2
  855. return 1
  856. def UnsetIsosurfaceMask(self, id, isosurf_id):
  857. """!Unset isosurface mask
  858. @param id volume id
  859. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  860. @return 1 on success
  861. @return -1 volume not found
  862. @return -2 isosurface not found
  863. @return -3 setting attributes failed
  864. """
  865. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  866. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  867. """!Unset isosurface transparency
  868. @param id volume id
  869. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  870. @return 1 on success
  871. @return -1 volume not found
  872. @return -2 isosurface not found
  873. @return -3 setting attributes failed
  874. """
  875. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  876. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  877. """!Unset isosurface emission
  878. @param id volume id
  879. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  880. @return 1 on success
  881. @return -1 volume not found
  882. @return -2 isosurface not found
  883. @return -3 setting attributes failed
  884. """
  885. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  886. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  887. """!Unset surface attribute
  888. @param id surface id
  889. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  890. @param attr attribute descriptor
  891. @return 1 on success
  892. @return -1 volume not found
  893. @return -2 isosurface not found
  894. @return -2 on failure
  895. """
  896. if not GVL_vol_exists(id):
  897. return -1
  898. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  899. return -2
  900. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  901. id, isosurf_id, attr)
  902. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  903. if ret < 0:
  904. return -2
  905. return 1
  906. def SetIsosurfaceMode(self, id, mode):
  907. """!Set draw mode for isosurfaces
  908. @param mode
  909. @return 1 on success
  910. @return -1 volume set not found
  911. @return -2 on failure
  912. """
  913. if not GVL_vol_exists(id):
  914. return -1
  915. ret = GVL_isosurf_set_drawmode(id, mode)
  916. if ret < 0:
  917. return -2
  918. return 1
  919. def SetIsosurfaceRes(self, id, res):
  920. """!Set draw resolution for isosurfaces
  921. @param res resolution value
  922. @return 1 on success
  923. @return -1 volume set not found
  924. @return -2 on failure
  925. """
  926. if not GVL_vol_exists(id):
  927. return -1
  928. ret = GVL_isosurf_set_drawres(id, res, res, res)
  929. if ret < 0:
  930. return -2
  931. return 1
  932. def GetCPlanesCount(self):
  933. """!Returns number of cutting planes"""
  934. return Nviz_num_cplanes(self.data)
  935. def GetCPlaneRotation(self):
  936. """!Returns rotation parameters of current cutting plane"""
  937. x, y, z = c_float(), c_float(), c_float()
  938. current = Nviz_get_current_cplane(self.data)
  939. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  940. return x.value, y.value, z.value
  941. def GetCPlaneTranslation(self):
  942. """!Returns translation parameters of current cutting plane"""
  943. x, y, z = c_float(), c_float(), c_float()
  944. current = Nviz_get_current_cplane(self.data)
  945. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  946. return x.value, y.value, z.value
  947. def SetCPlaneRotation(self, x, y, z):
  948. """!Set current clip plane rotation
  949. @param x,y,z rotation parameters
  950. """
  951. current = Nviz_get_current_cplane(self.data)
  952. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  953. Nviz_draw_cplane(self.data, -1, -1)
  954. def SetCPlaneTranslation(self, x, y, z):
  955. """!Set current clip plane translation
  956. @param x,y,z translation parameters
  957. """
  958. current = Nviz_get_current_cplane(self.data)
  959. Nviz_set_cplane_translation(self.data, current, x, y, z)
  960. Nviz_draw_cplane(self.data, -1, -1)
  961. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  962. current, x, y, z)
  963. def SelectCPlane(self, index):
  964. """!Select cutting plane
  965. @param index index of cutting plane
  966. """
  967. Nviz_on_cplane(self.data, index)
  968. def UnselectCPlane(self, index):
  969. """!Unselect cutting plane
  970. @param index index of cutting plane
  971. """
  972. Nviz_off_cplane(self.data, index)
  973. def SetFenceColor(self, index):
  974. """!Select current cutting plane
  975. @param index type of fence - from 0 (off) to 4
  976. """
  977. Nviz_set_fence_color(self.data, index)
  978. def GetXYRange(self):
  979. """!Get xy range"""
  980. return Nviz_get_xyrange(self.data)
  981. def GetZRange(self):
  982. """!Get z range"""
  983. min, max = c_float(), c_float()
  984. Nviz_get_zrange(self.data, byref(min), byref(max))
  985. return min.value, max.value
  986. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  987. """!Save current GL screen to ppm/tif file
  988. @param filename file name
  989. @param width image width
  990. @param height image height
  991. @param itype image type ('ppm' or 'tif')
  992. """
  993. widthOrig = self.width
  994. heightOrig = self.height
  995. self.ResizeWindow(width, height)
  996. GS_clear(Nviz_get_bgcolor(self.data))
  997. self.Draw(False, -1)
  998. if itype == 'ppm':
  999. GS_write_ppm(filename)
  1000. else:
  1001. GS_write_tif(filename)
  1002. self.ResizeWindow(widthOrig, heightOrig)
  1003. def DrawLightingModel(self):
  1004. """!Draw lighting model"""
  1005. if self.showLight:
  1006. GS_draw_lighting_model()
  1007. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1008. """!Set fringe
  1009. @param sid surface id
  1010. @param color color
  1011. @param elev elevation (height)
  1012. @param nw,ne,sw,se fringe edges (turn on/off)
  1013. """
  1014. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1015. Nviz_set_fringe(self.data,
  1016. sid, Nviz_color_from_str(scolor),
  1017. elev, int(nw), int(ne), int(sw), int(se))
  1018. def GetPointOnSurface(self, sx, sy):
  1019. """!Get point on surface
  1020. @param sx,sy canvas coordinates (LL)
  1021. """
  1022. sid = c_int()
  1023. x = c_float()
  1024. y = c_float()
  1025. z = c_float()
  1026. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1027. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1028. if num == 0:
  1029. return (None, None, None, None)
  1030. return (sid.value, x.value, y.value, z.value)
  1031. def QueryMap(self, sx, sy):
  1032. """!Query surface map
  1033. @param sx,sy canvas coordinates (LL)
  1034. """
  1035. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1036. if not sid:
  1037. return None
  1038. catstr = create_string_buffer(256)
  1039. valstr = create_string_buffer(256)
  1040. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1041. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1042. return { 'id' : sid,
  1043. 'x' : x,
  1044. 'y' : y,
  1045. 'z' : z,
  1046. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1047. 'color' : valstr.value }
  1048. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1049. """!Get distance measured along surface"""
  1050. d = c_float()
  1051. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1052. byref(d), int(useExag))
  1053. return d.value