wxnviz.py 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. """!
  2. @package wxnviz.py
  3. @brief wxGUI 3D view mode
  4. This module implements 3D visualization mode for map display (ctypes
  5. required).
  6. List of classes:
  7. - Nviz
  8. (C) 2008-2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  13. @author Pythonized by Glynn Clements
  14. @author Anna Kratochvilova <KratochAnna seznam.cz> (Google SoC 2011)
  15. """
  16. import sys
  17. import locale
  18. from threading import Thread
  19. from math import sqrt
  20. from numpy import matrix
  21. from ctypes import *
  22. try:
  23. from grass.lib.gis import *
  24. from grass.lib.raster3d import *
  25. from grass.lib.vector import *
  26. from grass.lib.ogsf import *
  27. from grass.lib.nviz import *
  28. from grass.lib.raster import *
  29. except ImportError, e:
  30. sys.stderr.write(_("3D view mode: %s\n") % e)
  31. from debug import Debug
  32. log = None
  33. progress = None
  34. def print_error(msg, type):
  35. """!Redirect stderr"""
  36. global log
  37. if log:
  38. log.write(msg)
  39. else:
  40. print msg
  41. return 0
  42. def print_progress(value):
  43. """!Redirect progress info"""
  44. global progress
  45. if progress:
  46. progress.SetValue(value)
  47. else:
  48. print value
  49. return 0
  50. errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
  51. errfunc = errtype(print_error)
  52. pertype = CFUNCTYPE(UNCHECKED(c_int), c_int)
  53. perfunc = pertype(print_progress)
  54. class Nviz(object):
  55. def __init__(self, glog, gprogress):
  56. """!Initialize Nviz class instance
  57. @param log logging area
  58. @param gprogress progressbar
  59. """
  60. global errfunc, perfunc, log, progress
  61. log = glog
  62. progress = gprogress
  63. G_gisinit("wxnviz")
  64. # gislib is already initialized (where?)
  65. G_set_error_routine(errfunc)
  66. G_set_percent_routine(perfunc)
  67. self.Init()
  68. self.data_obj = nv_data()
  69. self.data = pointer(self.data_obj)
  70. self.color_obj = Colors()
  71. self.color = pointer(self.color_obj)
  72. self.width = self.height = -1
  73. self.showLight = False
  74. Debug.msg(1, "Nviz::Nviz()")
  75. def __del__(self):
  76. """!Destroy Nviz class instance"""
  77. G_unset_error_routine()
  78. G_unset_percent_routine()
  79. del self.data
  80. del self.data_obj
  81. self.log = None
  82. def Init(self):
  83. """!Initialize window"""
  84. locale.setlocale(locale.LC_NUMERIC, 'C')
  85. G_unset_window()
  86. Rast_unset_window()
  87. Rast__init_window()
  88. GS_libinit()
  89. GVL_libinit()
  90. def ResizeWindow(self, width, height):
  91. """!GL canvas resized
  92. @param width window width
  93. @param height window height
  94. @return 1 on success
  95. @return 0 on failure (window resized by default to 20x20 px)
  96. """
  97. self.width = width
  98. self.height = height
  99. Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
  100. width, height)
  101. return Nviz_resize_window(width, height)
  102. def GetLongDim(self):
  103. """!Get longest dimension, used for initial size of north arrow"""
  104. return Nviz_get_longdim(self.data)
  105. def SetViewDefault(self):
  106. """!Set default view (based on loaded data)
  107. @return z-exag value, default, min and max height
  108. """
  109. # determine z-exag
  110. z_exag = Nviz_get_exag()
  111. Nviz_change_exag(self.data, z_exag)
  112. # determine height
  113. hdef = c_double()
  114. hmin = c_double()
  115. hmax = c_double()
  116. Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
  117. Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
  118. hdef.value, hmin.value, hmax.value)
  119. return (z_exag, hdef.value, hmin.value, hmax.value)
  120. def SetView(self, x, y, height, persp, twist):
  121. """!Change view settings
  122. @param x,y position
  123. @param height
  124. @param persp perpective
  125. @param twist
  126. """
  127. Nviz_set_viewpoint_height(height)
  128. Nviz_set_viewpoint_position(x, y)
  129. Nviz_set_viewpoint_twist(twist)
  130. Nviz_set_viewpoint_persp(persp)
  131. Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
  132. x, y, height, persp, twist)
  133. def LookHere(self, x, y):
  134. """!Look here feature
  135. @param x,y screen coordinates
  136. """
  137. Nviz_look_here(x, y)
  138. Debug.msg(3, "Nviz::LookHere(): x=%f, y=%f", x, y)
  139. def LookAtCenter(self):
  140. """!Center view at center of displayed surface"""
  141. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  142. Debug.msg(3, "Nviz::LookAtCenter()")
  143. def GetFocus(self):
  144. """!Get focus"""
  145. Debug.msg(3, "Nviz::GetFocus()")
  146. if Nviz_has_focus(self.data):
  147. x = c_float()
  148. y = c_float()
  149. z = c_float()
  150. Nviz_get_focus(self.data, byref(x), byref(y), byref(z))
  151. return x.value, y.value, z.value
  152. else:
  153. return -1, -1, -1
  154. def SetFocus(self, x, y, z):
  155. """!Set focus"""
  156. Debug.msg(3, "Nviz::SetFocus()")
  157. Nviz_set_focus(self.data, x, y, z)
  158. def SetZExag(self, z_exag):
  159. """!Set z-exag value
  160. @param z_exag value
  161. @return 1
  162. """
  163. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  164. return Nviz_change_exag(self.data, z_exag)
  165. def Draw(self, quick, quick_mode):
  166. """!Draw canvas
  167. Draw quick mode:
  168. - DRAW_QUICK_SURFACE
  169. - DRAW_QUICK_VLINES
  170. - DRAW_QUICK_VPOINTS
  171. - DRAW_QUICK_VOLUME
  172. @param quick if true draw in wiremode
  173. @param quick_mode quick mode
  174. """
  175. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  176. Nviz_draw_cplane(self.data, -1, -1) # ?
  177. if quick:
  178. Nviz_draw_quick(self.data, quick_mode)
  179. else:
  180. Nviz_draw_all(self.data)
  181. def EraseMap(self):
  182. """!Erase map display (with background color)
  183. """
  184. Debug.msg(1, "Nviz::EraseMap()")
  185. GS_clear(Nviz_get_bgcolor(self.data))
  186. def InitView(self):
  187. """!Initialize view"""
  188. # initialize nviz data
  189. Nviz_init_data(self.data)
  190. # define default attributes for map objects
  191. Nviz_set_surface_attr_default()
  192. # set background color
  193. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white"))
  194. GS_clear(Nviz_get_bgcolor(self.data))
  195. # initialize view, lights
  196. Nviz_init_view(self.data)
  197. Debug.msg(1, "Nviz::InitView()")
  198. def SetBgColor(self, color_str):
  199. """!Set background color
  200. @param color_str color string
  201. """
  202. Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
  203. def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
  204. """!Change lighting settings
  205. @param x,y,z position
  206. @param color light color (as string)
  207. @param bright light brightness
  208. @param ambient light ambient
  209. @param w local coordinate (default to 0)
  210. """
  211. Nviz_set_light_position(self.data, lid, x, y, z, w)
  212. Nviz_set_light_bright(self.data, lid, bright)
  213. Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
  214. Nviz_set_light_ambient(self.data, lid, ambient)
  215. def LoadSurface(self, name, color_name, color_value):
  216. """!Load raster map (surface)
  217. @param name raster map name
  218. @param color_name raster map for color (None for color_value)
  219. @param color_value color string (named color or RGB triptet)
  220. @return object id
  221. @return -1 on failure
  222. """
  223. mapset = G_find_raster2(name, "")
  224. if mapset is None:
  225. G_warning(_("Raster map <%s> not found"), name)
  226. return -1
  227. # topography
  228. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  229. G_fully_qualified_name(name, mapset), 0.0,
  230. self.data)
  231. if color_name: # check for color map
  232. mapset = G_find_raster2(color_name, "")
  233. if mapset is None:
  234. G_warning(_("Raster map <%s> not found"), color_name)
  235. GS_delete_surface(id)
  236. return -1
  237. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  238. G_fully_qualified_name(color_name, mapset), -1.0,
  239. self.data)
  240. elif color_value: # check for color value
  241. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  242. None, Nviz_color_from_str(color_value),
  243. self.data)
  244. else: # use by default elevation map for coloring
  245. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  246. G_fully_qualified_name(name, mapset), -1.0,
  247. self.data)
  248. # if (i > 1)
  249. # set_default_wirecolors(self.data, i)
  250. # focus on loaded self.data
  251. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  252. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  253. return id
  254. def AddConstant(self, value, color):
  255. """!Add new constant surface"""
  256. id = Nviz_new_map_obj(MAP_OBJ_SURF, None, value, self.data)
  257. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  258. None, Nviz_color_from_str(color),
  259. self.data)
  260. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  261. Debug.msg(1, "Nviz::AddConstant(): id=%d", id)
  262. return id
  263. def UnloadSurface(self, id):
  264. """!Unload surface
  265. @param id surface id
  266. @return 1 on success
  267. @return 0 on failure
  268. """
  269. if not GS_surf_exists(id):
  270. return 0
  271. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  272. if GS_delete_surface(id) < 0:
  273. return 0
  274. return 1
  275. def LoadVector(self, name, points):
  276. """!Load vector map overlay
  277. @param name vector map name
  278. @param points if true load 2d points rather then 2d lines
  279. @return object id, id of base surface (or -1 if it is not loaded)
  280. @return -1 on failure
  281. """
  282. baseId = -1
  283. if GS_num_surfs() == 0: # load base surface if no loaded
  284. baseId = Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  285. nsurf = c_int()
  286. surf_list = GS_get_surf_list(byref(nsurf))
  287. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  288. mapset = G_find_vector2 (name, "")
  289. if mapset is None:
  290. G_warning(_("Vector map <%s> not found"),
  291. name)
  292. if points:
  293. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  294. G_fully_qualified_name(name, mapset), 0.0,
  295. self.data)
  296. else:
  297. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  298. G_fully_qualified_name(name, mapset), 0.0,
  299. self.data)
  300. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  301. return id, baseId
  302. def UnloadVector(self, id, points):
  303. """!Unload vector set
  304. @param id vector set id
  305. @param points vector points or lines set
  306. @return 1 on success
  307. @return 0 on failure
  308. """
  309. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  310. if points:
  311. if not GP_site_exists(id):
  312. return 0
  313. if GP_delete_site(id) < 0:
  314. return 0
  315. else:
  316. if not GV_vect_exists(id):
  317. return 0
  318. if GV_delete_vector(id) < 0:
  319. return 0
  320. return 1
  321. def VectorSurfaceSelected(self, vid, sid):
  322. """!Check if surface is selected (currently unused)
  323. @param vid vector id
  324. @param sid surface id
  325. @return True if selected
  326. @return False if not selected
  327. """
  328. selected = GV_surf_is_selected(vid, sid)
  329. Debug.msg(1, "Nviz::VectorSurfaceSelected(): vid=%s, sid=%d -> selected=%d", vid, sid, selected)
  330. return selected
  331. def LoadVolume(self, name, color_name, color_value):
  332. """!Load 3d raster map (volume)
  333. @param name 3d raster map name
  334. @param color_name 3d raster map for color (None for color_value)
  335. @param color_value color string (named color or RGB triptet)
  336. @return object id
  337. @return -1 on failure
  338. """
  339. mapset = G_find_raster3d(name, "")
  340. if mapset is None:
  341. G_warning(_("3d raster map <%s> not found"),
  342. name)
  343. return -1
  344. # topography
  345. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  346. G_fully_qualified_name(name, mapset), 0.0,
  347. self.data)
  348. if color_name: # check for color map
  349. mapset = G_find_raster3d(color_name, "")
  350. if mapset is None:
  351. G_warning(_("3d raster map <%s> not found"),
  352. color_name)
  353. GVL_delete_vol(id)
  354. return -1
  355. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  356. G_fully_qualified_name(color_name, mapset), -1.0,
  357. self.data)
  358. elif color_value: # check for color value
  359. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  360. None, Nviz_color_from_str(color_value),
  361. self.data)
  362. else: # use by default elevation map for coloring
  363. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  364. G_fully_qualified_name(name, mapset), -1.0,
  365. self.data)
  366. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  367. return id
  368. def UnloadVolume(self, id):
  369. """!Unload volume
  370. @param id volume id
  371. @return 1 on success
  372. @return 0 on failure
  373. """
  374. if not GVL_vol_exists(id):
  375. return 0
  376. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  377. if GVL_delete_vol(id) < 0:
  378. return 0
  379. return 1
  380. def SetSurfaceTopo(self, id, map, value):
  381. """!Set surface topography
  382. @param id surface id
  383. @param map if true use map otherwise constant
  384. @param value map name of value
  385. @return 1 on success
  386. @return -1 surface not found
  387. @return -2 setting attributes failed
  388. """
  389. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  390. def SetSurfaceColor(self, id, map, value):
  391. """!Set surface color
  392. @param id surface id
  393. @param map if true use map otherwise constant
  394. @param value map name or value
  395. @return 1 on success
  396. @return -1 surface not found
  397. @return -2 setting attributes failed
  398. """
  399. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  400. def SetSurfaceMask(self, id, invert, value):
  401. """!Set surface mask
  402. @todo invert
  403. @param id surface id
  404. @param invert if true invert mask
  405. @param value map name of value
  406. @return 1 on success
  407. @return -1 surface not found
  408. @return -2 setting attributes failed
  409. """
  410. return self.SetSurfaceAttr(id, ATT_MASK, True, value)
  411. def SetSurfaceTransp(self, id, map, value):
  412. """!Set surface mask
  413. @todo invert
  414. @param id surface id
  415. @param map if true use map otherwise constant
  416. @param value map name of value
  417. @return 1 on success
  418. @return -1 surface not found
  419. @return -2 setting attributes failed
  420. """
  421. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  422. def SetSurfaceShine(self, id, map, value):
  423. """!Set surface shininess
  424. @param id surface id
  425. @param map if true use map otherwise constant
  426. @param value map name of value
  427. @return 1 on success
  428. @return -1 surface not found
  429. @return -2 setting attributes failed
  430. """
  431. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  432. def SetSurfaceEmit(self, id, map, value):
  433. """!Set surface emission (currently unused)
  434. @param id surface id
  435. @param map if true use map otherwise constant
  436. @param value map name of value
  437. @return 1 on success
  438. @return -1 surface not found
  439. @return -2 setting attributes failed
  440. """
  441. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  442. def SetSurfaceAttr(self, id, attr, map, value):
  443. """!Set surface attribute
  444. @param id surface id
  445. @param attr attribute desc
  446. @param map if true use map otherwise constant
  447. @param value map name of value
  448. @return 1 on success
  449. @return -1 surface not found
  450. @return -2 setting attributes failed
  451. """
  452. if not GS_surf_exists(id):
  453. return -1
  454. if map:
  455. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  456. value, -1.0, self.data)
  457. else:
  458. if attr == ATT_COLOR:
  459. val = Nviz_color_from_str(value)
  460. else:
  461. val = float(value)
  462. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  463. None, val, self.data)
  464. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  465. id, attr, map, value)
  466. if ret < 0:
  467. return -2
  468. return 1
  469. def UnsetSurfaceMask(self, id):
  470. """!Unset surface mask
  471. @param id surface id
  472. @return 1 on success
  473. @return -1 surface not found
  474. @return -2 setting attributes failed
  475. @return -1 on failure
  476. """
  477. return self.UnsetSurfaceAttr(id, ATT_MASK)
  478. def UnsetSurfaceTransp(self, id):
  479. """!Unset surface transparency
  480. @param id surface id
  481. @return 1 on success
  482. @return -1 surface not found
  483. @return -2 setting attributes failed
  484. """
  485. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  486. def UnsetSurfaceEmit(self, id):
  487. """!Unset surface emission (currently unused)
  488. @param id surface id
  489. @return 1 on success
  490. @return -1 surface not found
  491. @return -2 setting attributes failed
  492. """
  493. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  494. def UnsetSurfaceAttr(self, id, attr):
  495. """!Unset surface attribute
  496. @param id surface id
  497. @param attr attribute descriptor
  498. @return 1 on success
  499. @return -1 surface not found
  500. @return -2 setting attributes failed
  501. """
  502. if not GS_surf_exists(id):
  503. return -1
  504. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  505. id, attr)
  506. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  507. if ret < 0:
  508. return -2
  509. return 1
  510. def SetSurfaceRes(self, id, fine, coarse):
  511. """!Set surface resolution
  512. @param id surface id
  513. @param fine x/y fine resolution
  514. @param coarse x/y coarse resolution
  515. @return 1 on success
  516. @return -1 surface not found
  517. @return -2 setting attributes failed
  518. """
  519. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  520. id, fine, coarse)
  521. if id > 0:
  522. if not GS_surf_exists(id):
  523. return -1
  524. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  525. return -2
  526. else:
  527. GS_setall_drawres(fine, fine, coarse, coarse)
  528. return 1
  529. def SetSurfaceStyle(self, id, style):
  530. """!Set draw style
  531. Draw styles:
  532. - DM_GOURAUD
  533. - DM_FLAT
  534. - DM_FRINGE
  535. - DM_WIRE
  536. - DM_COL_WIRE
  537. - DM_POLY
  538. - DM_WIRE_POLY
  539. - DM_GRID_WIRE
  540. - DM_GRID_SURF
  541. @param id surface id (<= 0 for all)
  542. @param style draw style
  543. @return 1 on success
  544. @return -1 surface not found
  545. @return -2 setting attributes failed
  546. """
  547. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  548. id, style)
  549. if id > 0:
  550. if not GS_surf_exists(id):
  551. return -1
  552. if GS_set_drawmode(id, style) < 0:
  553. return -2
  554. return 1
  555. if GS_setall_drawmode(style) < 0:
  556. return -2
  557. return 1
  558. def SetWireColor(self, id, color_str):
  559. """!Set color of wire
  560. @todo all
  561. @param surface id (< 0 for all)
  562. @param color color string (R:G:B)
  563. @return 1 on success
  564. @return -1 surface not found
  565. @return -2 setting attributes failed
  566. @return 1 on success
  567. @return 0 on failure
  568. """
  569. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  570. id, color_str)
  571. color = Nviz_color_from_str(color_str)
  572. if id > 0:
  573. if not GS_surf_exists(id):
  574. return -1
  575. GS_set_wire_color(id, color)
  576. else:
  577. nsurfs = c_int()
  578. surf_list = GS_get_surf_list(byref(nsurfs))
  579. for i in xrange(nsurfs.value):
  580. id = surf_list[i]
  581. GS_set_wire_color(id, color)
  582. G_free(surf_list)
  583. surf_list = None
  584. return 1
  585. def GetSurfacePosition(self, id):
  586. """!Get surface position
  587. @param id surface id
  588. @return x,y,z
  589. @return zero-length vector on error
  590. """
  591. if not GS_surf_exists(id):
  592. return []
  593. x, y, z = c_float(), c_float(), c_float()
  594. GS_get_trans(id, byref(x), byref(y), byref(z))
  595. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  596. id, x.value, y.value, z.value)
  597. return [x.value, y.value, z.value]
  598. def SetSurfacePosition(self, id, x, y, z):
  599. """!Set surface position
  600. @param id surface id
  601. @param x,y,z translation values
  602. @return 1 on success
  603. @return -1 surface not found
  604. @return -2 setting position failed
  605. """
  606. if not GS_surf_exists(id):
  607. return -1
  608. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  609. id, x, y, z)
  610. GS_set_trans(id, x, y, z)
  611. return 1
  612. def SetVectorLineMode(self, id, color_str, width, flat):
  613. """!Set mode of vector line overlay
  614. @param id vector id
  615. @param color_str color string
  616. @param width line width
  617. @param flat display flat or on surface
  618. @return -1 vector set not found
  619. @return -2 on failure
  620. @return 1 on success
  621. """
  622. if not GV_vect_exists(id):
  623. return -1
  624. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  625. id, color_str, width, flat)
  626. color = Nviz_color_from_str(color_str)
  627. # use memory by default
  628. if GV_set_style(id, 1, color, width, flat) < 0:
  629. return -2
  630. return 1
  631. def SetVectorLineHeight(self, id, height):
  632. """!Set vector height above surface (lines)
  633. @param id vector set id
  634. @param height
  635. @return -1 vector set not found
  636. @return 1 on success
  637. """
  638. if not GV_vect_exists(id):
  639. return -1
  640. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  641. id, height)
  642. GV_set_trans(id, 0.0, 0.0, height)
  643. return 1
  644. def SetVectorLineSurface(self, id, surf_id):
  645. """!Set reference surface of vector set (lines)
  646. @param id vector set id
  647. @param surf_id surface id
  648. @return 1 on success
  649. @return -1 vector set not found
  650. @return -2 surface not found
  651. @return -3 on failure
  652. """
  653. if not GV_vect_exists(id):
  654. return -1
  655. if not GS_surf_exists(surf_id):
  656. return -2
  657. if GV_select_surf(id, surf_id) < 0:
  658. return -3
  659. return 1
  660. def UnsetVectorLineSurface(self, id, surf_id):
  661. """!Unset reference surface of vector set (lines)
  662. @param id vector set id
  663. @param surf_id surface id
  664. @return 1 on success
  665. @return -1 vector set not found
  666. @return -2 surface not found
  667. @return -3 on failure
  668. """
  669. if not GV_vect_exists(id):
  670. return -1
  671. if not GS_surf_exists(surf_id):
  672. return -2
  673. if GV_unselect_surf(id, surf_id) < 0:
  674. return -3
  675. return 1
  676. def SetVectorPointMode(self, id, color_str, width, size, marker):
  677. """!Set mode of vector point overlay
  678. @param id vector id
  679. @param color_str color string
  680. @param width line width
  681. @param flat
  682. @return -1 vector set not found
  683. """
  684. if not GP_site_exists(id):
  685. return -1
  686. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  687. "width=%d, size=%f, marker=%d",
  688. id, color_str, width, size, marker)
  689. color = Nviz_color_from_str(color_str)
  690. if GP_set_style(id, color, width, size, marker) < 0:
  691. return -2
  692. return 1
  693. def SetVectorPointHeight(self, id, height):
  694. """!Set vector height above surface (points)
  695. @param id vector set id
  696. @param height
  697. @return -1 vector set not found
  698. @return 1 on success
  699. """
  700. if not GP_site_exists(id):
  701. return -1
  702. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  703. id, height)
  704. GP_set_trans(id, 0.0, 0.0, height)
  705. return 1
  706. def SetVectorPointSurface(self, id, surf_id):
  707. """!Set reference surface of vector set (points)
  708. @param id vector set id
  709. @param surf_id surface id
  710. @return 1 on success
  711. @return -1 vector set not found
  712. @return -2 surface not found
  713. @return -3 on failure
  714. """
  715. if not GP_site_exists(id):
  716. return -1
  717. if not GS_surf_exists(surf_id):
  718. return -2
  719. if GP_select_surf(id, surf_id) < 0:
  720. return -3
  721. return 1
  722. def ReadVectorColors(self, name, mapset):
  723. """!Read vector colors
  724. @param name vector map name
  725. @mapset mapset name ("" for search path)
  726. @return -1 on error
  727. @return 0 if color table missing
  728. @return 1 on success (color table found)
  729. """
  730. return Vect_read_colors(name, mapset, self.color)
  731. def SetPointsStyleThematic(self, id, layer, color = None, colorTable = False,
  732. width = None, size = None, symbol = None):
  733. """!Set thematic style for vector points
  734. @param id vector set id
  735. @param layer layer number for thematic mapping
  736. @param colorTable use color table
  737. @param color color column name
  738. @param width width column name
  739. @param size size column name
  740. @param symbol symbol column name
  741. """
  742. file = c_char_p()
  743. ret = GP_get_sitename(id, byref(file))
  744. if ret < 0:
  745. return -1
  746. ret = self.ReadVectorColors(file, "")
  747. if ret < 0:
  748. return -1
  749. if colorTable:
  750. GP_set_style_thematic(id, layer, color, width, size, symbol, self.color)
  751. else:
  752. GP_set_style_thematic(id, layer, color, width, size, symbol, None)
  753. def SetLinesStyleThematic(self, id, layer, color = None, colorTable = False, width = None):
  754. """!Set thematic style for vector lines
  755. @param id vector set id
  756. @param layer layer number for thematic mapping
  757. @param color color column name
  758. @param colorTable use color table
  759. @param width width column name
  760. """
  761. file = c_char_p()
  762. ret = GV_get_vectname(id, byref(file))
  763. if ret < 0:
  764. return -1
  765. ret = self.ReadVectorColors(file, "")
  766. if ret < 0:
  767. return -1
  768. if colorTable:
  769. GV_set_style_thematic(id, layer, color, width, self.color)
  770. else:
  771. GV_set_style_thematic(id, layer, color, width, None)
  772. def UnsetLinesStyleThematic(self, id):
  773. """!Unset thematic style for vector points"""
  774. GV_unset_style_thematic(id)
  775. def UnsetPointsStyleThematic(self, id):
  776. """!Unset thematic style for vector lines"""
  777. GP_unset_style_thematic(id)
  778. def UnsetVectorPointSurface(self, id, surf_id):
  779. """!Unset reference surface of vector set (points)
  780. @param id vector set id
  781. @param surf_id surface id
  782. @return 1 on success
  783. @return -1 vector set not found
  784. @return -2 surface not found
  785. @return -3 on failure
  786. """
  787. if not GP_site_exists(id):
  788. return -1
  789. if not GS_surf_exists(surf_id):
  790. return -2
  791. if GP_unselect_surf(id, surf_id) < 0:
  792. return -3
  793. return 1
  794. def AddIsosurface(self, id, level, isosurf_id = None):
  795. """!Add new isosurface
  796. @param id volume id
  797. @param level isosurface level (topography)
  798. @return -1 on failure
  799. @return 1 on success
  800. """
  801. if not GVL_vol_exists(id):
  802. return -1
  803. if isosurf_id is not None:
  804. num = GVL_isosurf_num_isosurfs(id)
  805. if num < 0 or isosurf_id != num:
  806. return -1
  807. if GVL_isosurf_add(id) < 0:
  808. return -1
  809. # set topography level
  810. nisosurfs = GVL_isosurf_num_isosurfs(id)
  811. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  812. def AddSlice(self, id, slice_id = None):
  813. """!Add new slice
  814. @param id volume id
  815. @return -1 on failure
  816. @return number of slices
  817. """
  818. if not GVL_vol_exists(id):
  819. return -1
  820. if slice_id is not None:
  821. num = GVL_slice_num_slices(id)
  822. if num < 0 or slice_id != num:
  823. return -1
  824. if GVL_slice_add(id) < 0:
  825. return -1
  826. return GVL_slice_num_slices(id)
  827. def DeleteIsosurface(self, id, isosurf_id):
  828. """!Delete isosurface
  829. @param id volume id
  830. @param isosurf_id isosurface id
  831. @return 1 on success
  832. @return -1 volume not found
  833. @return -2 isosurface not found
  834. @return -3 on failure
  835. """
  836. if not GVL_vol_exists(id):
  837. return -1
  838. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  839. return -2
  840. ret = GVL_isosurf_del(id, isosurf_id)
  841. if ret < 0:
  842. return -3
  843. return 1
  844. def DeleteSlice(self, id, slice_id):
  845. """!Delete slice
  846. @param id volume id
  847. @param slice_id slice id
  848. @return 1 on success
  849. @return -1 volume not found
  850. @return -2 slice not found
  851. @return -3 on failure
  852. """
  853. if not GVL_vol_exists(id):
  854. return -1
  855. if slice_id > GVL_slice_num_slices(id):
  856. return -2
  857. ret = GVL_slice_del(id, slice_id)
  858. if ret < 0:
  859. return -3
  860. return 1
  861. def MoveIsosurface(self, id, isosurf_id, up):
  862. """!Move isosurface up/down in the list
  863. @param id volume id
  864. @param isosurf_id isosurface id
  865. @param up if true move up otherwise down
  866. @return 1 on success
  867. @return -1 volume not found
  868. @return -2 isosurface not found
  869. @return -3 on failure
  870. """
  871. if not GVL_vol_exists(id):
  872. return -1
  873. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  874. return -2
  875. if up:
  876. ret = GVL_isosurf_move_up(id, isosurf_id)
  877. else:
  878. ret = GVL_isosurf_move_down(id, isosurf_id)
  879. if ret < 0:
  880. return -3
  881. return 1
  882. def MoveSlice(self, id, slice_id, up):
  883. """!Move slice up/down in the list
  884. @param id volume id
  885. @param slice_id slice id
  886. @param up if true move up otherwise down
  887. @return 1 on success
  888. @return -1 volume not found
  889. @return -2 slice not found
  890. @return -3 on failure
  891. """
  892. if not GVL_vol_exists(id):
  893. return -1
  894. if slice_id > GVL_slice_num_slices(id):
  895. return -2
  896. if up:
  897. ret = GVL_slice_move_up(id, slice_id)
  898. else:
  899. ret = GVL_slice_move_down(id, slice_id)
  900. if ret < 0:
  901. return -3
  902. return 1
  903. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  904. """!Set isosurface level
  905. @param id volume id
  906. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  907. @param map if true use map otherwise constant
  908. @param value map name of value
  909. @return 1 on success
  910. @return -1 volume not found
  911. @return -2 isosurface not found
  912. @return -3 on failure
  913. """
  914. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  915. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  916. """!Set isosurface color
  917. @param id volume id
  918. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  919. @param map if true use map otherwise constant
  920. @param value map name of value
  921. @return 1 on success
  922. @return -1 volume not found
  923. @return -2 isosurface not found
  924. @return -3 on failure
  925. """
  926. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  927. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  928. """!Set isosurface mask
  929. @todo invert
  930. @param id volume id
  931. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  932. @param invert true for invert mask
  933. @param value map name to be used for mask
  934. @return 1 on success
  935. @return -1 volume not found
  936. @return -2 isosurface not found
  937. @return -3 on failure
  938. """
  939. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  940. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  941. """!Set isosurface transparency
  942. @param id volume id
  943. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  944. @param map if true use map otherwise constant
  945. @param value map name of value
  946. @return 1 on success
  947. @return -1 volume not found
  948. @return -2 isosurface not found
  949. @return -3 on failure
  950. """
  951. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  952. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  953. """!Set isosurface shininess
  954. @param id volume id
  955. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  956. @param map if true use map otherwise constant
  957. @param value map name of value
  958. @return 1 on success
  959. @return -1 volume not found
  960. @return -2 isosurface not found
  961. @return -3 on failure
  962. """
  963. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  964. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  965. """!Set isosurface emission (currently unused)
  966. @param id volume id
  967. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  968. @param map if true use map otherwise constant
  969. @param value map name of value
  970. @return 1 on success
  971. @return -1 volume not found
  972. @return -2 isosurface not found
  973. @return -3 on failure
  974. """
  975. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  976. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  977. """!Set isosurface attribute
  978. @param id volume id
  979. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  980. @param attr attribute desc
  981. @param map if true use map otherwise constant
  982. @param value map name of value
  983. @return 1 on success
  984. @return -1 volume not found
  985. @return -2 isosurface not found
  986. @return -3 setting attributes failed
  987. """
  988. if not GVL_vol_exists(id):
  989. return -1
  990. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  991. return -2
  992. if map:
  993. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  994. else:
  995. if attr == ATT_COLOR:
  996. val = Nviz_color_from_str(value)
  997. else:
  998. val = float(value)
  999. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  1000. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  1001. "attr=%d, map=%s, value=%s",
  1002. id, isosurf_id, attr, map, value)
  1003. if ret < 0:
  1004. return -2
  1005. return 1
  1006. def UnsetIsosurfaceMask(self, id, isosurf_id):
  1007. """!Unset isosurface mask
  1008. @param id volume id
  1009. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1010. @return 1 on success
  1011. @return -1 volume not found
  1012. @return -2 isosurface not found
  1013. @return -3 setting attributes failed
  1014. """
  1015. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  1016. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  1017. """!Unset isosurface transparency
  1018. @param id volume id
  1019. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1020. @return 1 on success
  1021. @return -1 volume not found
  1022. @return -2 isosurface not found
  1023. @return -3 setting attributes failed
  1024. """
  1025. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  1026. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  1027. """!Unset isosurface emission (currently unused)
  1028. @param id volume id
  1029. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1030. @return 1 on success
  1031. @return -1 volume not found
  1032. @return -2 isosurface not found
  1033. @return -3 setting attributes failed
  1034. """
  1035. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  1036. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  1037. """!Unset surface attribute
  1038. @param id surface id
  1039. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1040. @param attr attribute descriptor
  1041. @return 1 on success
  1042. @return -1 volume not found
  1043. @return -2 isosurface not found
  1044. @return -2 on failure
  1045. """
  1046. if not GVL_vol_exists(id):
  1047. return -1
  1048. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1049. return -2
  1050. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  1051. id, isosurf_id, attr)
  1052. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  1053. if ret < 0:
  1054. return -2
  1055. return 1
  1056. def SetIsosurfaceMode(self, id, mode):
  1057. """!Set draw mode for isosurfaces
  1058. @param mode
  1059. @return 1 on success
  1060. @return -1 volume set not found
  1061. @return -2 on failure
  1062. """
  1063. if not GVL_vol_exists(id):
  1064. return -1
  1065. ret = GVL_isosurf_set_drawmode(id, mode)
  1066. if ret < 0:
  1067. return -2
  1068. return 1
  1069. def SetSliceMode(self, id, mode):
  1070. """!Set draw mode for slices
  1071. @param mode
  1072. @return 1 on success
  1073. @return -1 volume set not found
  1074. @return -2 on failure
  1075. """
  1076. if not GVL_vol_exists(id):
  1077. return -1
  1078. ret = GVL_slice_set_drawmode(id, mode)
  1079. if ret < 0:
  1080. return -2
  1081. return 1
  1082. def SetIsosurfaceRes(self, id, res):
  1083. """!Set draw resolution for isosurfaces
  1084. @param res resolution value
  1085. @return 1 on success
  1086. @return -1 volume set not found
  1087. @return -2 on failure
  1088. """
  1089. if not GVL_vol_exists(id):
  1090. return -1
  1091. ret = GVL_isosurf_set_drawres(id, res, res, res)
  1092. if ret < 0:
  1093. return -2
  1094. return 1
  1095. def SetSliceRes(self, id, res):
  1096. """!Set draw resolution for slices
  1097. @param res resolution value
  1098. @return 1 on success
  1099. @return -1 volume set not found
  1100. @return -2 on failure
  1101. """
  1102. if not GVL_vol_exists(id):
  1103. return -1
  1104. ret = GVL_slice_set_drawres(id, res, res, res)
  1105. if ret < 0:
  1106. return -2
  1107. return 1
  1108. def SetSlicePosition(self, id, slice_id, x1, x2, y1, y2, z1, z2, dir):
  1109. """!Set slice position
  1110. @param id volume id
  1111. @param slice_id slice id
  1112. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1113. @param dir axis
  1114. @return 1 on success
  1115. @return -1 volume not found
  1116. @return -2 slice not found
  1117. @return -3 on failure
  1118. """
  1119. if not GVL_vol_exists(id):
  1120. return -1
  1121. if slice_id > GVL_slice_num_slices(id):
  1122. return -2
  1123. ret = GVL_slice_set_pos(id, slice_id, x1, x2, y1, y2, z1, z2, dir)
  1124. if ret < 0:
  1125. return -2
  1126. return 1
  1127. def SetSliceTransp(self, id, slice_id, value):
  1128. """!Set slice transparency
  1129. @param id volume id
  1130. @param slice_id slice id
  1131. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1132. @param value transparency value (0 - 255)
  1133. @return 1 on success
  1134. @return -1 volume not found
  1135. @return -2 slice not found
  1136. @return -3 on failure
  1137. """
  1138. if not GVL_vol_exists(id):
  1139. return -1
  1140. if slice_id > GVL_slice_num_slices(id):
  1141. return -2
  1142. ret = GVL_slice_set_transp(id, slice_id, value)
  1143. if ret < 0:
  1144. return -2
  1145. return 1
  1146. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  1147. """!Set inout mode
  1148. @param inout mode true/false
  1149. @return 1 on success
  1150. @return -1 volume set not found
  1151. @return -2 isosurface not found
  1152. @return -3 on failure
  1153. """
  1154. if not GVL_vol_exists(id):
  1155. return -1
  1156. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1157. return -2
  1158. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  1159. if ret < 0:
  1160. return -3
  1161. return 1
  1162. def GetVolumePosition(self, id):
  1163. """!Get volume position
  1164. @param id volume id
  1165. @return x,y,z
  1166. @return zero-length vector on error
  1167. """
  1168. if not GVL_vol_exists(id):
  1169. return []
  1170. x, y, z = c_float(), c_float(), c_float()
  1171. GVL_get_trans(id, byref(x), byref(y), byref(z))
  1172. Debug.msg(3, "Nviz::GetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1173. id, x.value, y.value, z.value)
  1174. return [x.value, y.value, z.value]
  1175. def SetVolumePosition(self, id, x, y, z):
  1176. """!Set volume position
  1177. @param id volume id
  1178. @param x,y,z translation values
  1179. @return 1 on success
  1180. @return -1 volume not found
  1181. @return -2 setting position failed
  1182. """
  1183. if not GVL_vol_exists(id):
  1184. return -1
  1185. Debug.msg(3, "Nviz::SetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1186. id, x, y, z)
  1187. GVL_set_trans(id, x, y, z)
  1188. return 1
  1189. def GetCPlaneCurrent(self):
  1190. return Nviz_get_current_cplane(self.data)
  1191. def GetCPlanesCount(self):
  1192. """!Returns number of cutting planes"""
  1193. return Nviz_num_cplanes(self.data)
  1194. def GetCPlaneRotation(self):
  1195. """!Returns rotation parameters of current cutting plane"""
  1196. x, y, z = c_float(), c_float(), c_float()
  1197. current = Nviz_get_current_cplane(self.data)
  1198. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  1199. return x.value, y.value, z.value
  1200. def GetCPlaneTranslation(self):
  1201. """!Returns translation parameters of current cutting plane"""
  1202. x, y, z = c_float(), c_float(), c_float()
  1203. current = Nviz_get_current_cplane(self.data)
  1204. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  1205. return x.value, y.value, z.value
  1206. def SetCPlaneRotation(self, x, y, z):
  1207. """!Set current clip plane rotation
  1208. @param x,y,z rotation parameters
  1209. """
  1210. current = Nviz_get_current_cplane(self.data)
  1211. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  1212. Nviz_draw_cplane(self.data, -1, -1)
  1213. def SetCPlaneTranslation(self, x, y, z):
  1214. """!Set current clip plane translation
  1215. @param x,y,z translation parameters
  1216. """
  1217. current = Nviz_get_current_cplane(self.data)
  1218. Nviz_set_cplane_translation(self.data, current, x, y, z)
  1219. Nviz_draw_cplane(self.data, -1, -1)
  1220. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  1221. current, x, y, z)
  1222. def SetCPlaneInteractively(self, x, y):
  1223. current = Nviz_get_current_cplane(self.data)
  1224. ret = Nviz_set_cplane_here(self.data, current, x, y)
  1225. if ret:
  1226. Nviz_draw_cplane(self.data, -1, -1)
  1227. x, y, z = self.GetCPlaneTranslation()
  1228. return x, y, z
  1229. else:
  1230. return None, None, None
  1231. def SelectCPlane(self, index):
  1232. """!Select cutting plane
  1233. @param index index of cutting plane
  1234. """
  1235. Nviz_on_cplane(self.data, index)
  1236. def UnselectCPlane(self, index):
  1237. """!Unselect cutting plane
  1238. @param index index of cutting plane
  1239. """
  1240. Nviz_off_cplane(self.data, index)
  1241. def SetFenceColor(self, index):
  1242. """!Select current cutting plane
  1243. @param index type of fence - from 0 (off) to 4
  1244. """
  1245. Nviz_set_fence_color(self.data, index)
  1246. def GetXYRange(self):
  1247. """!Get xy range"""
  1248. return Nviz_get_xyrange(self.data)
  1249. def GetZRange(self):
  1250. """!Get z range"""
  1251. min, max = c_float(), c_float()
  1252. Nviz_get_zrange(self.data, byref(min), byref(max))
  1253. return min.value, max.value
  1254. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  1255. """!Save current GL screen to ppm/tif file
  1256. @param filename file name
  1257. @param width image width
  1258. @param height image height
  1259. @param itype image type ('ppm' or 'tif')
  1260. """
  1261. widthOrig = self.width
  1262. heightOrig = self.height
  1263. self.ResizeWindow(width, height)
  1264. GS_clear(Nviz_get_bgcolor(self.data))
  1265. self.Draw(False, -1)
  1266. if itype == 'ppm':
  1267. GS_write_ppm(filename)
  1268. else:
  1269. GS_write_tif(filename)
  1270. self.ResizeWindow(widthOrig, heightOrig)
  1271. def DrawLightingModel(self):
  1272. """!Draw lighting model"""
  1273. if self.showLight:
  1274. Nviz_draw_model(self.data)
  1275. def DrawFringe(self):
  1276. """!Draw fringe"""
  1277. Nviz_draw_fringe(self.data)
  1278. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1279. """!Set fringe
  1280. @param sid surface id
  1281. @param color color
  1282. @param elev elevation (height)
  1283. @param nw,ne,sw,se fringe edges (turn on/off)
  1284. """
  1285. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1286. Nviz_set_fringe(self.data,
  1287. sid, Nviz_color_from_str(scolor),
  1288. elev, int(nw), int(ne), int(sw), int(se))
  1289. def DrawArrow(self):
  1290. """!Draw north arrow
  1291. """
  1292. return Nviz_draw_arrow(self.data)
  1293. def SetArrow(self, sx, sy, size, color):
  1294. """!Set north arrow from canvas coordinates
  1295. @param sx,sy canvas coordinates
  1296. @param size arrow length
  1297. @param color arrow color
  1298. """
  1299. return Nviz_set_arrow(self.data, sx, sy, size, Nviz_color_from_str(color))
  1300. def DeleteArrow(self):
  1301. """!Delete north arrow
  1302. """
  1303. Nviz_delete_arrow(self.data)
  1304. def SetScalebar(self, id, sx, sy, size, color):
  1305. """!Set scale bar from canvas coordinates
  1306. @param sx,sy canvas coordinates
  1307. @param id scale bar id
  1308. @param size scale bar length
  1309. @param color scale bar color
  1310. """
  1311. return Nviz_set_scalebar(self.data, id, sx, sy, size, Nviz_color_from_str(color))
  1312. def DrawScalebar(self):
  1313. """!Draw scale bar
  1314. """
  1315. return Nviz_draw_scalebar(self.data)
  1316. def DeleteScalebar(self, id):
  1317. """!Delete scalebar
  1318. """
  1319. Nviz_delete_scalebar(self.data, id)
  1320. def GetPointOnSurface(self, sx, sy):
  1321. """!Get point on surface
  1322. @param sx,sy canvas coordinates (LL)
  1323. """
  1324. sid = c_int()
  1325. x = c_float()
  1326. y = c_float()
  1327. z = c_float()
  1328. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1329. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1330. if num == 0:
  1331. return (None, None, None, None)
  1332. return (sid.value, x.value, y.value, z.value)
  1333. def QueryMap(self, sx, sy):
  1334. """!Query surface map
  1335. @param sx,sy canvas coordinates (LL)
  1336. """
  1337. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1338. if not sid:
  1339. return None
  1340. catstr = create_string_buffer(256)
  1341. valstr = create_string_buffer(256)
  1342. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1343. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1344. return { 'id' : sid,
  1345. 'x' : x,
  1346. 'y' : y,
  1347. 'z' : z,
  1348. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1349. 'color' : valstr.value }
  1350. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1351. """!Get distance measured along surface"""
  1352. d = c_float()
  1353. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1354. byref(d), int(useExag))
  1355. return d.value
  1356. def GetRotationParameters(self, dx, dy):
  1357. """!Get rotation parameters (angle, x, y, z axes)
  1358. @param dx,dy difference from previous mouse drag event
  1359. """
  1360. modelview = (c_double * 16)()
  1361. Nviz_get_modelview(byref(modelview))
  1362. angle = sqrt(dx*dx+dy*dy)/float(self.width+1)*180.0
  1363. m = []
  1364. row = []
  1365. for i, item in enumerate(modelview):
  1366. row.append(item)
  1367. if (i+1) % 4 == 0:
  1368. m.append(row)
  1369. row = []
  1370. inv = matrix(m).I
  1371. ax, ay, az = dy, dx, 0.
  1372. x = inv[0,0]*ax + inv[1,0]*ay + inv[2,0]*az
  1373. y = inv[0,1]*ax + inv[1,1]*ay + inv[2,1]*az
  1374. z = inv[0,2]*ax + inv[1,2]*ay + inv[2,2]*az
  1375. return angle, x, y, z
  1376. def Rotate(self, angle, x, y, z):
  1377. """!Set rotation parameters
  1378. Rotate scene (difference from current state).
  1379. @param angle angle
  1380. @param x,y,z axis coordinate
  1381. """
  1382. Nviz_set_rotation(angle, x, y, z)
  1383. def UnsetRotation(self):
  1384. """!Stop rotating the scene"""
  1385. Nviz_unset_rotation()
  1386. def ResetRotation(self):
  1387. """!Reset scene rotation"""
  1388. Nviz_init_rotation()
  1389. def GetRotationMatrix(self):
  1390. """!Get rotation matrix"""
  1391. matrix = (c_double * 16)()
  1392. GS_get_rotation_matrix(byref(matrix))
  1393. returnMatrix = []
  1394. for item in matrix:
  1395. returnMatrix.append(item)
  1396. return returnMatrix
  1397. def SetRotationMatrix(self, matrix):
  1398. """!Set rotation matrix"""
  1399. mtrx = (c_double * 16)()
  1400. for i in range(len(matrix)):
  1401. mtrx[i] = matrix[i]
  1402. GS_set_rotation_matrix(byref(mtrx))