wxnviz.py 51 KB

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