wxnviz.py 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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 UnsetVectorPointSurface(self, id, surf_id):
  713. """!Unset reference surface of vector set (points)
  714. @param id vector set id
  715. @param surf_id surface id
  716. @return 1 on success
  717. @return -1 vector set not found
  718. @return -2 surface not found
  719. @return -3 on failure
  720. """
  721. if not GP_site_exists(id):
  722. return -1
  723. if not GS_surf_exists(surf_id):
  724. return -2
  725. if GP_unselect_surf(id, surf_id) < 0:
  726. return -3
  727. return 1
  728. def AddIsosurface(self, id, level, isosurf_id = None):
  729. """!Add new isosurface
  730. @param id volume id
  731. @param level isosurface level (topography)
  732. @return -1 on failure
  733. @return 1 on success
  734. """
  735. if not GVL_vol_exists(id):
  736. return -1
  737. if isosurf_id is not None:
  738. num = GVL_isosurf_num_isosurfs(id)
  739. if num < 0 or isosurf_id != num:
  740. return -1
  741. if GVL_isosurf_add(id) < 0:
  742. return -1
  743. # set topography level
  744. nisosurfs = GVL_isosurf_num_isosurfs(id)
  745. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  746. def AddSlice(self, id, slice_id = None):
  747. """!Add new slice
  748. @param id volume id
  749. @return -1 on failure
  750. @return number of slices
  751. """
  752. if not GVL_vol_exists(id):
  753. return -1
  754. if slice_id is not None:
  755. num = GVL_slice_num_slices(id)
  756. if num < 0 or slice_id != num:
  757. return -1
  758. if GVL_slice_add(id) < 0:
  759. return -1
  760. return GVL_slice_num_slices(id)
  761. def DeleteIsosurface(self, id, isosurf_id):
  762. """!Delete isosurface
  763. @param id volume id
  764. @param isosurf_id isosurface id
  765. @return 1 on success
  766. @return -1 volume not found
  767. @return -2 isosurface not found
  768. @return -3 on failure
  769. """
  770. if not GVL_vol_exists(id):
  771. return -1
  772. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  773. return -2
  774. ret = GVL_isosurf_del(id, isosurf_id)
  775. if ret < 0:
  776. return -3
  777. return 1
  778. def DeleteSlice(self, id, slice_id):
  779. """!Delete slice
  780. @param id volume id
  781. @param slice_id slice id
  782. @return 1 on success
  783. @return -1 volume not found
  784. @return -2 slice not found
  785. @return -3 on failure
  786. """
  787. if not GVL_vol_exists(id):
  788. return -1
  789. if slice_id > GVL_slice_num_slices(id):
  790. return -2
  791. ret = GVL_slice_del(id, slice_id)
  792. if ret < 0:
  793. return -3
  794. return 1
  795. def MoveIsosurface(self, id, isosurf_id, up):
  796. """!Move isosurface up/down in the list
  797. @param id volume id
  798. @param isosurf_id isosurface id
  799. @param up if true move up otherwise down
  800. @return 1 on success
  801. @return -1 volume not found
  802. @return -2 isosurface not found
  803. @return -3 on failure
  804. """
  805. if not GVL_vol_exists(id):
  806. return -1
  807. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  808. return -2
  809. if up:
  810. ret = GVL_isosurf_move_up(id, isosurf_id)
  811. else:
  812. ret = GVL_isosurf_move_down(id, isosurf_id)
  813. if ret < 0:
  814. return -3
  815. return 1
  816. def MoveSlice(self, id, slice_id, up):
  817. """!Move slice up/down in the list
  818. @param id volume id
  819. @param slice_id slice id
  820. @param up if true move up otherwise down
  821. @return 1 on success
  822. @return -1 volume not found
  823. @return -2 slice not found
  824. @return -3 on failure
  825. """
  826. if not GVL_vol_exists(id):
  827. return -1
  828. if slice_id > GVL_slice_num_slices(id):
  829. return -2
  830. if up:
  831. ret = GVL_slice_move_up(id, slice_id)
  832. else:
  833. ret = GVL_slice_move_down(id, slice_id)
  834. if ret < 0:
  835. return -3
  836. return 1
  837. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  838. """!Set isosurface level
  839. @param id volume id
  840. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  841. @param map if true use map otherwise constant
  842. @param value map name of value
  843. @return 1 on success
  844. @return -1 volume not found
  845. @return -2 isosurface not found
  846. @return -3 on failure
  847. """
  848. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  849. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  850. """!Set isosurface color
  851. @param id volume id
  852. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  853. @param map if true use map otherwise constant
  854. @param value map name of value
  855. @return 1 on success
  856. @return -1 volume not found
  857. @return -2 isosurface not found
  858. @return -3 on failure
  859. """
  860. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  861. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  862. """!Set isosurface mask
  863. @todo invert
  864. @param id volume id
  865. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  866. @param invert true for invert mask
  867. @param value map name to be used for mask
  868. @return 1 on success
  869. @return -1 volume not found
  870. @return -2 isosurface not found
  871. @return -3 on failure
  872. """
  873. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  874. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  875. """!Set isosurface transparency
  876. @param id volume id
  877. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  878. @param map if true use map otherwise constant
  879. @param value map name of value
  880. @return 1 on success
  881. @return -1 volume not found
  882. @return -2 isosurface not found
  883. @return -3 on failure
  884. """
  885. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  886. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  887. """!Set isosurface shininess
  888. @param id volume id
  889. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  890. @param map if true use map otherwise constant
  891. @param value map name of value
  892. @return 1 on success
  893. @return -1 volume not found
  894. @return -2 isosurface not found
  895. @return -3 on failure
  896. """
  897. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  898. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  899. """!Set isosurface emission (currently unused)
  900. @param id volume id
  901. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  902. @param map if true use map otherwise constant
  903. @param value map name of value
  904. @return 1 on success
  905. @return -1 volume not found
  906. @return -2 isosurface not found
  907. @return -3 on failure
  908. """
  909. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  910. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  911. """!Set isosurface attribute
  912. @param id volume id
  913. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  914. @param attr attribute desc
  915. @param map if true use map otherwise constant
  916. @param value map name of value
  917. @return 1 on success
  918. @return -1 volume not found
  919. @return -2 isosurface not found
  920. @return -3 setting attributes failed
  921. """
  922. if not GVL_vol_exists(id):
  923. return -1
  924. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  925. return -2
  926. if map:
  927. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  928. else:
  929. if attr == ATT_COLOR:
  930. val = Nviz_color_from_str(value)
  931. else:
  932. val = float(value)
  933. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  934. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  935. "attr=%d, map=%s, value=%s",
  936. id, isosurf_id, attr, map, value)
  937. if ret < 0:
  938. return -2
  939. return 1
  940. def UnsetIsosurfaceMask(self, id, isosurf_id):
  941. """!Unset isosurface mask
  942. @param id volume id
  943. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  944. @return 1 on success
  945. @return -1 volume not found
  946. @return -2 isosurface not found
  947. @return -3 setting attributes failed
  948. """
  949. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  950. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  951. """!Unset isosurface transparency
  952. @param id volume id
  953. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  954. @return 1 on success
  955. @return -1 volume not found
  956. @return -2 isosurface not found
  957. @return -3 setting attributes failed
  958. """
  959. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  960. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  961. """!Unset isosurface emission (currently unused)
  962. @param id volume id
  963. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  964. @return 1 on success
  965. @return -1 volume not found
  966. @return -2 isosurface not found
  967. @return -3 setting attributes failed
  968. """
  969. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  970. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  971. """!Unset surface attribute
  972. @param id surface id
  973. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  974. @param attr attribute descriptor
  975. @return 1 on success
  976. @return -1 volume not found
  977. @return -2 isosurface not found
  978. @return -2 on failure
  979. """
  980. if not GVL_vol_exists(id):
  981. return -1
  982. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  983. return -2
  984. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  985. id, isosurf_id, attr)
  986. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  987. if ret < 0:
  988. return -2
  989. return 1
  990. def SetIsosurfaceMode(self, id, mode):
  991. """!Set draw mode for isosurfaces
  992. @param mode
  993. @return 1 on success
  994. @return -1 volume set not found
  995. @return -2 on failure
  996. """
  997. if not GVL_vol_exists(id):
  998. return -1
  999. ret = GVL_isosurf_set_drawmode(id, mode)
  1000. if ret < 0:
  1001. return -2
  1002. return 1
  1003. def SetSliceMode(self, id, mode):
  1004. """!Set draw mode for slices
  1005. @param mode
  1006. @return 1 on success
  1007. @return -1 volume set not found
  1008. @return -2 on failure
  1009. """
  1010. if not GVL_vol_exists(id):
  1011. return -1
  1012. ret = GVL_slice_set_drawmode(id, mode)
  1013. if ret < 0:
  1014. return -2
  1015. return 1
  1016. def SetIsosurfaceRes(self, id, res):
  1017. """!Set draw resolution for isosurfaces
  1018. @param res resolution value
  1019. @return 1 on success
  1020. @return -1 volume set not found
  1021. @return -2 on failure
  1022. """
  1023. if not GVL_vol_exists(id):
  1024. return -1
  1025. ret = GVL_isosurf_set_drawres(id, res, res, res)
  1026. if ret < 0:
  1027. return -2
  1028. return 1
  1029. def SetSliceRes(self, id, res):
  1030. """!Set draw resolution for slices
  1031. @param res resolution value
  1032. @return 1 on success
  1033. @return -1 volume set not found
  1034. @return -2 on failure
  1035. """
  1036. if not GVL_vol_exists(id):
  1037. return -1
  1038. ret = GVL_slice_set_drawres(id, res, res, res)
  1039. if ret < 0:
  1040. return -2
  1041. return 1
  1042. def SetSlicePosition(self, id, slice_id, x1, x2, y1, y2, z1, z2, dir):
  1043. """!Set slice position
  1044. @param id volume id
  1045. @param slice_id slice id
  1046. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1047. @param dir axis
  1048. @return 1 on success
  1049. @return -1 volume not found
  1050. @return -2 slice not found
  1051. @return -3 on failure
  1052. """
  1053. if not GVL_vol_exists(id):
  1054. return -1
  1055. if slice_id > GVL_slice_num_slices(id):
  1056. return -2
  1057. ret = GVL_slice_set_pos(id, slice_id, x1, x2, y1, y2, z1, z2, dir)
  1058. if ret < 0:
  1059. return -2
  1060. return 1
  1061. def SetSliceTransp(self, id, slice_id, value):
  1062. """!Set slice transparency
  1063. @param id volume id
  1064. @param slice_id slice id
  1065. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1066. @param value transparency value (0 - 255)
  1067. @return 1 on success
  1068. @return -1 volume not found
  1069. @return -2 slice not found
  1070. @return -3 on failure
  1071. """
  1072. if not GVL_vol_exists(id):
  1073. return -1
  1074. if slice_id > GVL_slice_num_slices(id):
  1075. return -2
  1076. ret = GVL_slice_set_transp(id, slice_id, value)
  1077. if ret < 0:
  1078. return -2
  1079. return 1
  1080. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  1081. """!Set inout mode
  1082. @param inout mode true/false
  1083. @return 1 on success
  1084. @return -1 volume set not found
  1085. @return -2 isosurface not found
  1086. @return -3 on failure
  1087. """
  1088. if not GVL_vol_exists(id):
  1089. return -1
  1090. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1091. return -2
  1092. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  1093. if ret < 0:
  1094. return -3
  1095. return 1
  1096. def GetVolumePosition(self, id):
  1097. """!Get volume position
  1098. @param id volume id
  1099. @return x,y,z
  1100. @return zero-length vector on error
  1101. """
  1102. if not GVL_vol_exists(id):
  1103. return []
  1104. x, y, z = c_float(), c_float(), c_float()
  1105. GVL_get_trans(id, byref(x), byref(y), byref(z))
  1106. Debug.msg(3, "Nviz::GetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1107. id, x.value, y.value, z.value)
  1108. return [x.value, y.value, z.value]
  1109. def SetVolumePosition(self, id, x, y, z):
  1110. """!Set volume position
  1111. @param id volume id
  1112. @param x,y,z translation values
  1113. @return 1 on success
  1114. @return -1 volume not found
  1115. @return -2 setting position failed
  1116. """
  1117. if not GVL_vol_exists(id):
  1118. return -1
  1119. Debug.msg(3, "Nviz::SetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1120. id, x, y, z)
  1121. GVL_set_trans(id, x, y, z)
  1122. return 1
  1123. def GetCPlaneCurrent(self):
  1124. return Nviz_get_current_cplane(self.data)
  1125. def GetCPlanesCount(self):
  1126. """!Returns number of cutting planes"""
  1127. return Nviz_num_cplanes(self.data)
  1128. def GetCPlaneRotation(self):
  1129. """!Returns rotation parameters of current cutting plane"""
  1130. x, y, z = c_float(), c_float(), c_float()
  1131. current = Nviz_get_current_cplane(self.data)
  1132. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  1133. return x.value, y.value, z.value
  1134. def GetCPlaneTranslation(self):
  1135. """!Returns translation parameters of current cutting plane"""
  1136. x, y, z = c_float(), c_float(), c_float()
  1137. current = Nviz_get_current_cplane(self.data)
  1138. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  1139. return x.value, y.value, z.value
  1140. def SetCPlaneRotation(self, x, y, z):
  1141. """!Set current clip plane rotation
  1142. @param x,y,z rotation parameters
  1143. """
  1144. current = Nviz_get_current_cplane(self.data)
  1145. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  1146. Nviz_draw_cplane(self.data, -1, -1)
  1147. def SetCPlaneTranslation(self, x, y, z):
  1148. """!Set current clip plane translation
  1149. @param x,y,z translation parameters
  1150. """
  1151. current = Nviz_get_current_cplane(self.data)
  1152. Nviz_set_cplane_translation(self.data, current, x, y, z)
  1153. Nviz_draw_cplane(self.data, -1, -1)
  1154. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  1155. current, x, y, z)
  1156. def SetCPlaneInteractively(self, x, y):
  1157. current = Nviz_get_current_cplane(self.data)
  1158. ret = Nviz_set_cplane_here(self.data, current, x, y)
  1159. if ret:
  1160. Nviz_draw_cplane(self.data, -1, -1)
  1161. x, y, z = self.GetCPlaneTranslation()
  1162. return x, y, z
  1163. else:
  1164. return None, None, None
  1165. def SelectCPlane(self, index):
  1166. """!Select cutting plane
  1167. @param index index of cutting plane
  1168. """
  1169. Nviz_on_cplane(self.data, index)
  1170. def UnselectCPlane(self, index):
  1171. """!Unselect cutting plane
  1172. @param index index of cutting plane
  1173. """
  1174. Nviz_off_cplane(self.data, index)
  1175. def SetFenceColor(self, index):
  1176. """!Select current cutting plane
  1177. @param index type of fence - from 0 (off) to 4
  1178. """
  1179. Nviz_set_fence_color(self.data, index)
  1180. def GetXYRange(self):
  1181. """!Get xy range"""
  1182. return Nviz_get_xyrange(self.data)
  1183. def GetZRange(self):
  1184. """!Get z range"""
  1185. min, max = c_float(), c_float()
  1186. Nviz_get_zrange(self.data, byref(min), byref(max))
  1187. return min.value, max.value
  1188. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  1189. """!Save current GL screen to ppm/tif file
  1190. @param filename file name
  1191. @param width image width
  1192. @param height image height
  1193. @param itype image type ('ppm' or 'tif')
  1194. """
  1195. widthOrig = self.width
  1196. heightOrig = self.height
  1197. self.ResizeWindow(width, height)
  1198. GS_clear(Nviz_get_bgcolor(self.data))
  1199. self.Draw(False, -1)
  1200. if itype == 'ppm':
  1201. GS_write_ppm(filename)
  1202. else:
  1203. GS_write_tif(filename)
  1204. self.ResizeWindow(widthOrig, heightOrig)
  1205. def DrawLightingModel(self):
  1206. """!Draw lighting model"""
  1207. if self.showLight:
  1208. Nviz_draw_model(self.data)
  1209. def DrawFringe(self):
  1210. """!Draw fringe"""
  1211. Nviz_draw_fringe(self.data)
  1212. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1213. """!Set fringe
  1214. @param sid surface id
  1215. @param color color
  1216. @param elev elevation (height)
  1217. @param nw,ne,sw,se fringe edges (turn on/off)
  1218. """
  1219. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1220. Nviz_set_fringe(self.data,
  1221. sid, Nviz_color_from_str(scolor),
  1222. elev, int(nw), int(ne), int(sw), int(se))
  1223. def DrawArrow(self):
  1224. """!Draw north arrow
  1225. """
  1226. return Nviz_draw_arrow(self.data)
  1227. def SetArrow(self, sx, sy, size, color):
  1228. """!Set north arrow from canvas coordinates
  1229. @param sx,sy canvas coordinates
  1230. @param size arrow length
  1231. @param color arrow color
  1232. """
  1233. return Nviz_set_arrow(self.data, sx, sy, size, Nviz_color_from_str(color))
  1234. def DeleteArrow(self):
  1235. """!Delete north arrow
  1236. """
  1237. Nviz_delete_arrow(self.data)
  1238. def SetScalebar(self, id, sx, sy, size, color):
  1239. """!Set scale bar from canvas coordinates
  1240. @param sx,sy canvas coordinates
  1241. @param id scale bar id
  1242. @param size scale bar length
  1243. @param color scale bar color
  1244. """
  1245. return Nviz_set_scalebar(self.data, id, sx, sy, size, Nviz_color_from_str(color))
  1246. def DrawScalebar(self):
  1247. """!Draw scale bar
  1248. """
  1249. return Nviz_draw_scalebar(self.data)
  1250. def DeleteScalebar(self, id):
  1251. """!Delete scalebar
  1252. """
  1253. Nviz_delete_scalebar(self.data, id)
  1254. def GetPointOnSurface(self, sx, sy):
  1255. """!Get point on surface
  1256. @param sx,sy canvas coordinates (LL)
  1257. """
  1258. sid = c_int()
  1259. x = c_float()
  1260. y = c_float()
  1261. z = c_float()
  1262. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1263. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1264. if num == 0:
  1265. return (None, None, None, None)
  1266. return (sid.value, x.value, y.value, z.value)
  1267. def QueryMap(self, sx, sy):
  1268. """!Query surface map
  1269. @param sx,sy canvas coordinates (LL)
  1270. """
  1271. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1272. if not sid:
  1273. return None
  1274. catstr = create_string_buffer(256)
  1275. valstr = create_string_buffer(256)
  1276. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1277. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1278. return { 'id' : sid,
  1279. 'x' : x,
  1280. 'y' : y,
  1281. 'z' : z,
  1282. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1283. 'color' : valstr.value }
  1284. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1285. """!Get distance measured along surface"""
  1286. d = c_float()
  1287. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1288. byref(d), int(useExag))
  1289. return d.value
  1290. def GetRotationParameters(self, dx, dy):
  1291. """!Get rotation parameters (angle, x, y, z axes)
  1292. @param dx,dy difference from previous mouse drag event
  1293. """
  1294. modelview = (c_double * 16)()
  1295. Nviz_get_modelview(byref(modelview))
  1296. angle = sqrt(dx*dx+dy*dy)/float(self.width+1)*180.0
  1297. m = []
  1298. row = []
  1299. for i, item in enumerate(modelview):
  1300. row.append(item)
  1301. if (i+1) % 4 == 0:
  1302. m.append(row)
  1303. row = []
  1304. inv = matrix(m).I
  1305. ax, ay, az = dy, dx, 0.
  1306. x = inv[0,0]*ax + inv[1,0]*ay + inv[2,0]*az
  1307. y = inv[0,1]*ax + inv[1,1]*ay + inv[2,1]*az
  1308. z = inv[0,2]*ax + inv[1,2]*ay + inv[2,2]*az
  1309. return angle, x, y, z
  1310. def Rotate(self, angle, x, y, z):
  1311. """!Set rotation parameters
  1312. Rotate scene (difference from current state).
  1313. @param angle angle
  1314. @param x,y,z axis coordinate
  1315. """
  1316. Nviz_set_rotation(angle, x, y, z)
  1317. def UnsetRotation(self):
  1318. """!Stop rotating the scene"""
  1319. Nviz_unset_rotation()
  1320. def ResetRotation(self):
  1321. """!Reset scene rotation"""
  1322. Nviz_init_rotation()