wxnviz.py 50 KB

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