wxnviz.py 51 KB

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