wxnviz.py 51 KB

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