wxnviz.py 53 KB

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