wxnviz.py 53 KB

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