wxnviz.py 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. """
  2. @package nviz.wxnviz
  3. @brief wxGUI 3D view mode (ctypes-based classes)
  4. This module implements 3D visualization mode for map display (ctypes
  5. required).
  6. List of classes:
  7. - wxnviz::Nviz
  8. - wxnviz::Texture
  9. - wxnviz::ImageTexture
  10. - wxnviz::TextTexture
  11. (C) 2008-2011 by the GRASS Development Team
  12. This program is free software under the GNU General Public License
  13. (>=v2). Read the file COPYING that comes with GRASS for details.
  14. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  15. @author Pythonized by Glynn Clements
  16. @author Anna Kratochvilova <KratochAnna seznam.cz> (Google SoC 2011)
  17. """
  18. from __future__ import print_function
  19. import sys
  20. import locale
  21. import struct
  22. from math import sqrt
  23. try:
  24. from numpy import matrix
  25. except ImportError:
  26. msg = _(
  27. "This module requires the NumPy module, which could not be "
  28. "imported. It probably is not installed (it's not part of the "
  29. "standard Python distribution). See the Numeric Python site "
  30. "(http://numpy.scipy.org) for information on downloading source or "
  31. "binaries.")
  32. print("wxnviz.py: " + msg, file=sys.stderr)
  33. import wx
  34. from ctypes import *
  35. try:
  36. from grass.lib.gis import *
  37. from grass.lib.raster3d import *
  38. from grass.lib.vector import *
  39. from grass.lib.ogsf import *
  40. from grass.lib.nviz import *
  41. from grass.lib.raster import *
  42. except (ImportError, WindowsError) as e:
  43. print("wxnviz.py: {}".format(e), file=sys.stderr)
  44. from core.debug import Debug
  45. from core.utils import _, autoCropImageFromFile
  46. from core.gcmd import EncodeString
  47. from core.globalvar import wxPythonPhoenix
  48. from gui_core.wrap import Rect
  49. import grass.script as grass
  50. log = None
  51. progress = None
  52. def print_error(msg, type):
  53. """Redirect stderr"""
  54. global log
  55. if log:
  56. log.write(msg)
  57. else:
  58. print(msg)
  59. return 0
  60. def print_progress(value):
  61. """Redirect progress info"""
  62. global progress
  63. if progress:
  64. if not progress.GetRange() == 100:
  65. progress.SetRange(100)
  66. progress.SetValue(value)
  67. else:
  68. print(value)
  69. return 0
  70. try:
  71. errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
  72. errfunc = errtype(print_error)
  73. pertype = CFUNCTYPE(UNCHECKED(c_int), c_int)
  74. perfunc = pertype(print_progress)
  75. except NameError:
  76. pass
  77. class Nviz(object):
  78. def __init__(self, glog, gprogress):
  79. """Initialize Nviz class instance
  80. :param glog: logging area
  81. :param gprogress: progressbar
  82. """
  83. global errfunc, perfunc, log, progress
  84. log = glog
  85. progress = gprogress
  86. G_gisinit("wxnviz")
  87. # gislib is already initialized (where?)
  88. G_set_error_routine(errfunc)
  89. G_set_percent_routine(perfunc)
  90. self.Init()
  91. self.data_obj = nv_data()
  92. self.data = pointer(self.data_obj)
  93. self.color_obj = Colors()
  94. self.color = pointer(self.color_obj)
  95. self.width = self.height = -1
  96. self.showLight = False
  97. Debug.msg(1, "Nviz::Nviz()")
  98. def __del__(self):
  99. """Destroy Nviz class instance"""
  100. G_unset_error_routine()
  101. G_unset_percent_routine()
  102. del self.data
  103. del self.data_obj
  104. self.log = None
  105. def Init(self):
  106. """Initialize window"""
  107. locale.setlocale(locale.LC_NUMERIC, 'C')
  108. G_unset_window()
  109. Rast_unset_window()
  110. Rast__init_window()
  111. GS_libinit()
  112. GVL_libinit()
  113. GVL_init_region()
  114. def ResizeWindow(self, width, height):
  115. """GL canvas resized
  116. :param width: window width
  117. :param height: window height
  118. :return: 1 on success
  119. :return: 0 on failure (window resized by default to 20x20 px)
  120. """
  121. self.width = width
  122. self.height = height
  123. Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
  124. width, height)
  125. return Nviz_resize_window(width, height)
  126. def GetLongDim(self):
  127. """Get longest dimension, used for initial size of north arrow"""
  128. return Nviz_get_longdim(self.data)
  129. def SetViewDefault(self):
  130. """Set default view (based on loaded data)
  131. :return: z-exag value, default, min and max height
  132. """
  133. # determine z-exag
  134. z_exag = Nviz_get_exag()
  135. Nviz_change_exag(self.data, z_exag)
  136. # determine height
  137. hdef = c_double()
  138. hmin = c_double()
  139. hmax = c_double()
  140. Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
  141. Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
  142. hdef.value, hmin.value, hmax.value)
  143. return (z_exag, hdef.value, hmin.value, hmax.value)
  144. def SetView(self, x, y, height, persp, twist):
  145. """Change view settings
  146. :param x,y: position
  147. :param height:
  148. :param persp: perpective
  149. :param twist:
  150. """
  151. Nviz_set_viewpoint_height(height)
  152. Nviz_set_viewpoint_position(x, y)
  153. Nviz_set_viewpoint_twist(twist)
  154. Nviz_set_viewpoint_persp(persp)
  155. Debug.msg(
  156. 3,
  157. "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
  158. x,
  159. y,
  160. height,
  161. persp,
  162. twist)
  163. def GetViewpointPosition(self):
  164. x = c_double()
  165. y = c_double()
  166. h = c_double()
  167. Nviz_get_viewpoint_height(byref(h))
  168. Nviz_get_viewpoint_position(byref(x), byref(y))
  169. return (x.value, y.value, h.value)
  170. def LookHere(self, x, y):
  171. """Look here feature
  172. :param x,y: screen coordinates
  173. """
  174. Nviz_look_here(x, y)
  175. Debug.msg(3, "Nviz::LookHere(): x=%f, y=%f", x, y)
  176. def LookAtCenter(self):
  177. """Center view at center of displayed surface"""
  178. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  179. Debug.msg(3, "Nviz::LookAtCenter()")
  180. def GetFocus(self):
  181. """Get focus"""
  182. Debug.msg(3, "Nviz::GetFocus()")
  183. if Nviz_has_focus(self.data):
  184. x = c_float()
  185. y = c_float()
  186. z = c_float()
  187. Nviz_get_focus(self.data, byref(x), byref(y), byref(z))
  188. return x.value, y.value, z.value
  189. else:
  190. return -1, -1, -1
  191. def SetFocus(self, x, y, z):
  192. """Set focus"""
  193. Debug.msg(3, "Nviz::SetFocus()")
  194. Nviz_set_focus(self.data, x, y, z)
  195. def GetViewdir(self):
  196. """Get viewdir"""
  197. Debug.msg(3, "Nviz::GetViewdir()")
  198. dir = (c_float * 3)()
  199. GS_get_viewdir(byref(dir))
  200. return dir[0], dir[1], dir[2]
  201. def SetViewdir(self, x, y, z):
  202. """Set viewdir"""
  203. Debug.msg(3, "Nviz::SetViewdir(): x=%f, y=%f, z=%f" % (x, y, z))
  204. dir = (c_float * 3)()
  205. for i, coord in enumerate((x, y, z)):
  206. dir[i] = coord
  207. GS_set_viewdir(byref(dir))
  208. def SetZExag(self, z_exag):
  209. """Set z-exag value
  210. :param z_exag: value
  211. :return: 1
  212. """
  213. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  214. return Nviz_change_exag(self.data, z_exag)
  215. def Draw(self, quick, quick_mode):
  216. """Draw canvas
  217. Draw quick mode:
  218. - DRAW_QUICK_SURFACE
  219. - DRAW_QUICK_VLINES
  220. - DRAW_QUICK_VPOINTS
  221. - DRAW_QUICK_VOLUME
  222. :param quick: if true draw in wiremode
  223. :param quick_mode: quick mode
  224. """
  225. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  226. Nviz_draw_cplane(self.data, -1, -1) # ?
  227. if quick:
  228. Nviz_draw_quick(self.data, quick_mode)
  229. else:
  230. Nviz_draw_all(self.data)
  231. def EraseMap(self):
  232. """Erase map display (with background color)
  233. """
  234. Debug.msg(1, "Nviz::EraseMap()")
  235. GS_clear(Nviz_get_bgcolor(self.data))
  236. def InitView(self):
  237. """Initialize view"""
  238. # initialize nviz data
  239. Nviz_init_data(self.data)
  240. # define default attributes for map objects
  241. Nviz_set_surface_attr_default()
  242. # set background color
  243. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white"))
  244. GS_clear(Nviz_get_bgcolor(self.data))
  245. # initialize view, lights
  246. Nviz_init_view(self.data)
  247. Debug.msg(1, "Nviz::InitView()")
  248. def SetBgColor(self, color_str):
  249. """Set background color
  250. :param str color_str: color string
  251. """
  252. Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
  253. def SetLight(self, x, y, z, color, bright, ambient, w=0, lid=1):
  254. """Change lighting settings
  255. :param x,y,z: position
  256. :param color: light color (as string)
  257. :param bright: light brightness
  258. :param ambient: light ambient
  259. :param w: local coordinate (default to 0)
  260. :param lid: light id
  261. """
  262. Nviz_set_light_position(self.data, lid, x, y, z, w)
  263. Nviz_set_light_bright(self.data, lid, bright)
  264. Nviz_set_light_color(
  265. self.data, lid, int(
  266. color[0]), int(
  267. color[1]), int(
  268. color[2]))
  269. Nviz_set_light_ambient(self.data, lid, ambient)
  270. def LoadSurface(self, name, color_name, color_value):
  271. """Load raster map (surface)
  272. :param name: raster map name
  273. :param color_name: raster map for color (None for color_value)
  274. :param color_value: color string (named color or RGB triptet)
  275. :return: object id
  276. :return: -1 on failure
  277. """
  278. mapset = G_find_raster2(name, "")
  279. if mapset is None:
  280. G_warning(_("Raster map <%s> not found"), name)
  281. return -1
  282. # topography
  283. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  284. G_fully_qualified_name(name, mapset), 0.0,
  285. self.data)
  286. if color_name: # check for color map
  287. mapset = G_find_raster2(color_name, "")
  288. if mapset is None:
  289. G_warning(_("Raster map <%s> not found"), color_name)
  290. GS_delete_surface(id)
  291. return -1
  292. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  293. G_fully_qualified_name(color_name, mapset), -1.0,
  294. self.data)
  295. elif color_value: # check for color value
  296. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  297. None, Nviz_color_from_str(color_value),
  298. self.data)
  299. else: # use by default elevation map for coloring
  300. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  301. G_fully_qualified_name(name, mapset), -1.0,
  302. self.data)
  303. # if (i > 1)
  304. # set_default_wirecolors(self.data, i)
  305. # focus on loaded self.data
  306. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  307. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  308. return id
  309. def AddConstant(self, value, color):
  310. """Add new constant surface"""
  311. id = Nviz_new_map_obj(MAP_OBJ_SURF, None, value, self.data)
  312. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  313. None, Nviz_color_from_str(color),
  314. self.data)
  315. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  316. Debug.msg(1, "Nviz::AddConstant(): id=%d", id)
  317. return id
  318. def UnloadSurface(self, id):
  319. """Unload surface
  320. :param id: surface id
  321. :return: 1 on success
  322. :return: 0 on failure
  323. """
  324. if not GS_surf_exists(id):
  325. return 0
  326. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  327. if GS_delete_surface(id) < 0:
  328. return 0
  329. return 1
  330. def LoadVector(self, name, points):
  331. """Load vector map overlay
  332. :param name: vector map name
  333. :param points: if true load 2d points rather then 2d lines
  334. :return: object id, id of base surface (or -1 if it is not loaded)
  335. :return: -1 on failure
  336. """
  337. baseId = -1
  338. if GS_num_surfs() == 0: # load base surface if no loaded
  339. baseId = Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  340. nsurf = c_int()
  341. surf_list = GS_get_surf_list(byref(nsurf))
  342. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  343. mapset = G_find_vector2(name, "")
  344. if mapset is None:
  345. G_warning(_("Vector map <%s> not found"),
  346. name)
  347. if points:
  348. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  349. G_fully_qualified_name(name, mapset), 0.0,
  350. self.data)
  351. else:
  352. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  353. G_fully_qualified_name(name, mapset), 0.0,
  354. self.data)
  355. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  356. return id, baseId
  357. def UnloadVector(self, id, points):
  358. """Unload vector set
  359. :param id: vector set id
  360. :param points: vector points or lines set
  361. :return: 1 on success
  362. :return: 0 on failure
  363. """
  364. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  365. if points:
  366. if not GP_site_exists(id):
  367. return 0
  368. if GP_delete_site(id) < 0:
  369. return 0
  370. else:
  371. if not GV_vect_exists(id):
  372. return 0
  373. if GV_delete_vector(id) < 0:
  374. return 0
  375. return 1
  376. def VectorSurfaceSelected(self, vid, sid):
  377. """Check if surface is selected (currently unused)
  378. :param vid: vector id
  379. :param sid: surface id
  380. :return: True if selected
  381. :return: False if not selected
  382. """
  383. selected = GV_surf_is_selected(vid, sid)
  384. Debug.msg(
  385. 1,
  386. "Nviz::VectorSurfaceSelected(): vid=%s, sid=%d -> selected=%d",
  387. vid,
  388. sid,
  389. selected)
  390. return selected
  391. def LoadVolume(self, name, color_name, color_value):
  392. """Load 3d raster map (volume)
  393. :param name: 3d raster map name
  394. :param color_name: 3d raster map for color (None for color_value)
  395. :param color_value: color string (named color or RGB triptet)
  396. :return: object id
  397. :return: -1 on failure
  398. """
  399. mapset = G_find_raster3d(name, "")
  400. if mapset is None:
  401. G_warning(_("3d raster map <%s> not found"),
  402. name)
  403. return -1
  404. # topography
  405. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  406. G_fully_qualified_name(name, mapset), 0.0,
  407. self.data)
  408. if color_name: # check for color map
  409. mapset = G_find_raster3d(color_name, "")
  410. if mapset is None:
  411. G_warning(_("3d raster map <%s> not found"),
  412. color_name)
  413. GVL_delete_vol(id)
  414. return -1
  415. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  416. G_fully_qualified_name(color_name, mapset), -1.0,
  417. self.data)
  418. elif color_value: # check for color value
  419. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  420. None, Nviz_color_from_str(color_value),
  421. self.data)
  422. else: # use by default elevation map for coloring
  423. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  424. G_fully_qualified_name(name, mapset), -1.0,
  425. self.data)
  426. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  427. return id
  428. def UnloadVolume(self, id):
  429. """Unload volume
  430. :param id: volume id
  431. :return: 1 on success
  432. :return: 0 on failure
  433. """
  434. if not GVL_vol_exists(id):
  435. return 0
  436. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  437. if GVL_delete_vol(id) < 0:
  438. return 0
  439. return 1
  440. def SetSurfaceTopo(self, id, map, value):
  441. """Set surface topography
  442. :param id: surface id
  443. :param map: if true use map otherwise constant
  444. :param value: map name of value
  445. :return: 1 on success
  446. :return: -1 surface not found
  447. :return: -2 setting attributes failed
  448. """
  449. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  450. def SetSurfaceColor(self, id, map, value):
  451. """Set surface color
  452. :param id: surface id
  453. :param map: if true use map otherwise constant
  454. :param value: map name or value
  455. :return: 1 on success
  456. :return: -1 surface not found
  457. :return: -2 setting attributes failed
  458. """
  459. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  460. def SetSurfaceMask(self, id, invert, value):
  461. """Set surface mask
  462. .. todo::
  463. invert
  464. :param id: surface id
  465. :param invert: if true invert mask
  466. :param value: map name of value
  467. :return: 1 on success
  468. :return: -1 surface not found
  469. :return: -2 setting attributes failed
  470. """
  471. return self.SetSurfaceAttr(id, ATT_MASK, True, value)
  472. def SetSurfaceTransp(self, id, map, value):
  473. """Set surface mask
  474. ..todo::
  475. invert
  476. :param id: surface id
  477. :param map: if true use map otherwise constant
  478. :param value: map name of value
  479. :return: 1 on success
  480. :return: -1 surface not found
  481. :return: -2 setting attributes failed
  482. """
  483. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  484. def SetSurfaceShine(self, id, map, value):
  485. """Set surface shininess
  486. :param id: surface id
  487. :param map: if true use map otherwise constant
  488. :param value: map name of value
  489. :return: 1 on success
  490. :return: -1 surface not found
  491. :return: -2 setting attributes failed
  492. """
  493. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  494. def SetSurfaceEmit(self, id, map, value):
  495. """Set surface emission (currently unused)
  496. :param id: surface id
  497. :param map: if true use map otherwise constant
  498. :param value: map name of value
  499. :return: 1 on success
  500. :return: -1 surface not found
  501. :return: -2 setting attributes failed
  502. """
  503. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  504. def SetSurfaceAttr(self, id, attr, map, value):
  505. """Set surface attribute
  506. :param id: surface id
  507. :param attr: attribute desc
  508. :param map: if true use map otherwise constant
  509. :param value: map name of value
  510. :return: 1 on success
  511. :return: -1 surface not found
  512. :return: -2 setting attributes failed
  513. """
  514. if not GS_surf_exists(id):
  515. return -1
  516. if map:
  517. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  518. value, -1.0, self.data)
  519. else:
  520. if attr == ATT_COLOR:
  521. val = Nviz_color_from_str(value)
  522. else:
  523. val = float(value)
  524. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  525. None, val, self.data)
  526. Debug.msg(
  527. 3,
  528. "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  529. id,
  530. attr,
  531. map,
  532. value)
  533. if ret < 0:
  534. return -2
  535. return 1
  536. def UnsetSurfaceMask(self, id):
  537. """Unset surface mask
  538. :param id: surface id
  539. :return: 1 on success
  540. :return: -1 surface not found
  541. :return: -2 setting attributes failed
  542. :return: -1 on failure
  543. """
  544. return self.UnsetSurfaceAttr(id, ATT_MASK)
  545. def UnsetSurfaceTransp(self, id):
  546. """Unset surface transparency
  547. :param id: surface id
  548. :return: 1 on success
  549. :return: -1 surface not found
  550. :return: -2 setting attributes failed
  551. """
  552. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  553. def UnsetSurfaceEmit(self, id):
  554. """Unset surface emission (currently unused)
  555. :param id: surface id
  556. :return: 1 on success
  557. :return: -1 surface not found
  558. :return: -2 setting attributes failed
  559. """
  560. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  561. def UnsetSurfaceAttr(self, id, attr):
  562. """Unset surface attribute
  563. :param id: surface id
  564. :param attr: attribute descriptor
  565. :return: 1 on success
  566. :return: -1 surface not found
  567. :return: -2 setting attributes failed
  568. """
  569. if not GS_surf_exists(id):
  570. return -1
  571. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  572. id, attr)
  573. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  574. if ret < 0:
  575. return -2
  576. return 1
  577. def SetSurfaceRes(self, id, fine, coarse):
  578. """Set surface resolution
  579. :param id: surface id
  580. :param fine: x/y fine resolution
  581. :param coarse: x/y coarse resolution
  582. :return: 1 on success
  583. :return: -1 surface not found
  584. :return: -2 setting attributes failed
  585. """
  586. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  587. id, fine, coarse)
  588. if id > 0:
  589. if not GS_surf_exists(id):
  590. return -1
  591. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  592. return -2
  593. else:
  594. GS_setall_drawres(fine, fine, coarse, coarse)
  595. return 1
  596. def SetSurfaceStyle(self, id, style):
  597. """Set draw style
  598. Draw styles:
  599. - DM_GOURAUD
  600. - DM_FLAT
  601. - DM_FRINGE
  602. - DM_WIRE
  603. - DM_COL_WIRE
  604. - DM_POLY
  605. - DM_WIRE_POLY
  606. - DM_GRID_WIRE
  607. - DM_GRID_SURF
  608. :param id: surface id (<= 0 for all)
  609. :param style: draw style
  610. :return: 1 on success
  611. :return: -1 surface not found
  612. :return: -2 setting attributes failed
  613. """
  614. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  615. id, style)
  616. if id > 0:
  617. if not GS_surf_exists(id):
  618. return -1
  619. if GS_set_drawmode(id, style) < 0:
  620. return -2
  621. return 1
  622. if GS_setall_drawmode(style) < 0:
  623. return -2
  624. return 1
  625. def SetWireColor(self, id, color_str):
  626. """Set color of wire
  627. .. todo::
  628. all
  629. :param id: surface id (< 0 for all)
  630. :param color_str: color string (R:G:B)
  631. :return: 1 on success
  632. :return: -1 surface not found
  633. :return: -2 setting attributes failed
  634. :return: 1 on success
  635. :return: 0 on failure
  636. """
  637. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  638. id, color_str)
  639. color = Nviz_color_from_str(color_str)
  640. if id > 0:
  641. if not GS_surf_exists(id):
  642. return -1
  643. GS_set_wire_color(id, color)
  644. else:
  645. nsurfs = c_int()
  646. surf_list = GS_get_surf_list(byref(nsurfs))
  647. for i in xrange(nsurfs.value):
  648. id = surf_list[i]
  649. GS_set_wire_color(id, color)
  650. G_free(surf_list)
  651. surf_list = None
  652. return 1
  653. def GetSurfacePosition(self, id):
  654. """Get surface position
  655. :param id: surface id
  656. :return: x,y,z
  657. :return: zero-length vector on error
  658. """
  659. if not GS_surf_exists(id):
  660. return []
  661. x, y, z = c_float(), c_float(), c_float()
  662. GS_get_trans(id, byref(x), byref(y), byref(z))
  663. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  664. id, x.value, y.value, z.value)
  665. return [x.value, y.value, z.value]
  666. def SetSurfacePosition(self, id, x, y, z):
  667. """Set surface position
  668. :param id: surface id
  669. :param x,y,z: translation values
  670. :return: 1 on success
  671. :return: -1 surface not found
  672. :return: -2 setting position failed
  673. """
  674. if not GS_surf_exists(id):
  675. return -1
  676. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  677. id, x, y, z)
  678. GS_set_trans(id, x, y, z)
  679. return 1
  680. def SetVectorLineMode(self, id, color_str, width, use_z):
  681. """Set mode of vector line overlay
  682. :param id: vector id
  683. :param color_str: color string
  684. :param width: line width
  685. :param use_z: display 3d or on surface
  686. :return: -1 vector set not found
  687. :return: -2 on failure
  688. :return: 1 on success
  689. """
  690. if not GV_vect_exists(id):
  691. return -1
  692. Debug.msg(
  693. 3,
  694. "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, use_z=%d",
  695. id,
  696. color_str,
  697. width,
  698. use_z)
  699. color = Nviz_color_from_str(color_str)
  700. # use memory by default
  701. if GV_set_style(id, 1, color, width, use_z) < 0:
  702. return -2
  703. return 1
  704. def SetVectorLineHeight(self, id, height):
  705. """Set vector height above surface (lines)
  706. :param id: vector set id
  707. :param height:
  708. :return: -1 vector set not found
  709. :return: 1 on success
  710. """
  711. if not GV_vect_exists(id):
  712. return -1
  713. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  714. id, height)
  715. GV_set_trans(id, 0.0, 0.0, height)
  716. return 1
  717. def SetVectorLineSurface(self, id, surf_id):
  718. """Set reference surface of vector set (lines)
  719. :param id: vector set id
  720. :param surf_id: surface id
  721. :return: 1 on success
  722. :return: -1 vector set not found
  723. :return: -2 surface not found
  724. :return: -3 on failure
  725. """
  726. if not GV_vect_exists(id):
  727. return -1
  728. if not GS_surf_exists(surf_id):
  729. return -2
  730. if GV_select_surf(id, surf_id) < 0:
  731. return -3
  732. return 1
  733. def UnsetVectorLineSurface(self, id, surf_id):
  734. """Unset reference surface of vector set (lines)
  735. :param id: vector set id
  736. :param surf_id: surface id
  737. :return: 1 on success
  738. :return: -1 vector set not found
  739. :return: -2 surface not found
  740. :return: -3 on failure
  741. """
  742. if not GV_vect_exists(id):
  743. return -1
  744. if not GS_surf_exists(surf_id):
  745. return -2
  746. if GV_unselect_surf(id, surf_id) < 0:
  747. return -3
  748. return 1
  749. def SetVectorPointMode(self, id, color_str, width, size, marker):
  750. """Set mode of vector point overlay
  751. :param id: vector id
  752. :param color_str: color string
  753. :param width: line width
  754. :param size: size of the symbol
  755. :param marker: type of the symbol
  756. :return: -1 vector set not found
  757. """
  758. if not GP_site_exists(id):
  759. return -1
  760. # dtree and ctree defined but not used
  761. if marker > 5:
  762. marker += 2
  763. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  764. "width=%d, size=%f, marker=%d",
  765. id, color_str, width, size, marker)
  766. color = Nviz_color_from_str(color_str)
  767. if GP_set_style(id, color, width, size, marker) < 0:
  768. return -2
  769. return 1
  770. def SetVectorPointHeight(self, id, height):
  771. """Set vector height above surface (points)
  772. :param id: vector set id
  773. :param height:
  774. :return: -1 vector set not found
  775. :return: 1 on success
  776. """
  777. if not GP_site_exists(id):
  778. return -1
  779. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  780. id, height)
  781. GP_set_trans(id, 0.0, 0.0, height)
  782. return 1
  783. def SetVectorPointSurface(self, id, surf_id):
  784. """Set reference surface of vector set (points)
  785. :param id: vector set id
  786. :param surf_id: surface id
  787. :return: 1 on success
  788. :return: -1 vector set not found
  789. :return: -2 surface not found
  790. :return: -3 on failure
  791. """
  792. if not GP_site_exists(id):
  793. return -1
  794. if not GS_surf_exists(surf_id):
  795. return -2
  796. if GP_select_surf(id, surf_id) < 0:
  797. return -3
  798. return 1
  799. def ReadVectorColors(self, name, mapset):
  800. """Read vector colors
  801. :param name: vector map name
  802. :param mapset: mapset name (empty string (\c "") for search path)
  803. :return: -1 on error
  804. :return: 0 if color table missing
  805. :return: 1 on success (color table found)
  806. """
  807. return Vect_read_colors(name, mapset, self.color)
  808. def CheckColorTable(self, id, type):
  809. """Check if color table exists.
  810. :param id: vector set id
  811. :param type: vector set type (lines/points)
  812. :return: 1 color table exists
  813. :return: 0 no color table found
  814. :return: -1 on error
  815. :return: -2 vector set not found
  816. """
  817. file = c_char_p()
  818. if type == 'points':
  819. ret = GP_get_sitename(id, byref(file))
  820. elif type == 'lines':
  821. ret = GV_get_vectname(id, byref(file))
  822. if ret < 0:
  823. return -2
  824. return self.ReadVectorColors(file, "")
  825. def SetPointsStyleThematic(self, id, layer, color=None, colorTable=False,
  826. width=None, size=None, symbol=None):
  827. """Set thematic style for vector points
  828. :param id: vector set id
  829. :param layer: layer number for thematic mapping
  830. :param colorTable: use color table
  831. :param color: color column name
  832. :param width: width column name
  833. :param size: size column name
  834. :param symbol: symbol column name
  835. """
  836. file = c_char_p()
  837. ret = GP_get_sitename(id, byref(file))
  838. if ret < 0:
  839. return -1
  840. ret = self.ReadVectorColors(file, "")
  841. if ret < 0:
  842. return -1
  843. if colorTable:
  844. GP_set_style_thematic(
  845. id, layer, color, width, size, symbol, self.color)
  846. else:
  847. GP_set_style_thematic(id, layer, color, width, size, symbol, None)
  848. def SetLinesStyleThematic(
  849. self, id, layer, color=None, colorTable=False, width=None):
  850. """Set thematic style for vector lines
  851. :param id: vector set id
  852. :param layer: layer number for thematic mapping
  853. :param color: color column name
  854. :param colorTable: use color table
  855. :param width: width column name
  856. """
  857. file = c_char_p()
  858. ret = GV_get_vectname(id, byref(file))
  859. if ret < 0:
  860. return -1
  861. ret = self.ReadVectorColors(file, "")
  862. if ret < 0:
  863. return -1
  864. if colorTable:
  865. GV_set_style_thematic(id, layer, color, width, self.color)
  866. else:
  867. GV_set_style_thematic(id, layer, color, width, None)
  868. def UnsetLinesStyleThematic(self, id):
  869. """Unset thematic style for vector points"""
  870. GV_unset_style_thematic(id)
  871. def UnsetPointsStyleThematic(self, id):
  872. """Unset thematic style for vector lines"""
  873. GP_unset_style_thematic(id)
  874. def UnsetVectorPointSurface(self, id, surf_id):
  875. """Unset reference surface of vector set (points)
  876. :param id: vector set id
  877. :param surf_id: surface id
  878. :return: 1 on success
  879. :return: -1 vector set not found
  880. :return: -2 surface not found
  881. :return: -3 on failure
  882. """
  883. if not GP_site_exists(id):
  884. return -1
  885. if not GS_surf_exists(surf_id):
  886. return -2
  887. if GP_unselect_surf(id, surf_id) < 0:
  888. return -3
  889. return 1
  890. def SetVectorPointZMode(self, id, zMode):
  891. """Set z mode (use z coordinate or not)
  892. :param id: volume id
  893. :param zMode: bool
  894. :return: -1 on failure
  895. :return: 0 when no 3d
  896. :return: 1 on success
  897. """
  898. if not GP_site_exists(id):
  899. return -1
  900. return GP_set_zmode(id, int(zMode))
  901. def AddIsosurface(self, id, level, isosurf_id=None):
  902. """Add new isosurface
  903. :param id: volume id
  904. :param level: isosurface level (topography)
  905. :param isosurf_id: isosurface id
  906. :return: -1 on failure
  907. :return: 1 on success
  908. """
  909. if not GVL_vol_exists(id):
  910. return -1
  911. if isosurf_id is not None:
  912. num = GVL_isosurf_num_isosurfs(id)
  913. if num < 0 or isosurf_id != num:
  914. return -1
  915. if GVL_isosurf_add(id) < 0:
  916. return -1
  917. # set topography level
  918. nisosurfs = GVL_isosurf_num_isosurfs(id)
  919. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  920. def AddSlice(self, id, slice_id=None):
  921. """Add new slice
  922. :param id: volume id
  923. :param slice_id: slice id
  924. :return: -1 on failure
  925. :return: number of slices
  926. """
  927. if not GVL_vol_exists(id):
  928. return -1
  929. if slice_id is not None:
  930. num = GVL_slice_num_slices(id)
  931. if num < 0 or slice_id != num:
  932. return -1
  933. if GVL_slice_add(id) < 0:
  934. return -1
  935. return GVL_slice_num_slices(id)
  936. def DeleteIsosurface(self, id, isosurf_id):
  937. """Delete isosurface
  938. :param id: volume id
  939. :param isosurf_id: isosurface id
  940. :return: 1 on success
  941. :return: -1 volume not found
  942. :return: -2 isosurface not found
  943. :return: -3 on failure
  944. """
  945. if not GVL_vol_exists(id):
  946. return -1
  947. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  948. return -2
  949. ret = GVL_isosurf_del(id, isosurf_id)
  950. if ret < 0:
  951. return -3
  952. return 1
  953. def DeleteSlice(self, id, slice_id):
  954. """Delete slice
  955. :param id: volume id
  956. :param slice_id: slice id
  957. :return: 1 on success
  958. :return: -1 volume not found
  959. :return: -2 slice not found
  960. :return: -3 on failure
  961. """
  962. if not GVL_vol_exists(id):
  963. return -1
  964. if slice_id > GVL_slice_num_slices(id):
  965. return -2
  966. ret = GVL_slice_del(id, slice_id)
  967. if ret < 0:
  968. return -3
  969. return 1
  970. def MoveIsosurface(self, id, isosurf_id, up):
  971. """Move isosurface up/down in the list
  972. :param id: volume id
  973. :param isosurf_id: isosurface id
  974. :param up: if true move up otherwise down
  975. :return: 1 on success
  976. :return: -1 volume not found
  977. :return: -2 isosurface not found
  978. :return: -3 on failure
  979. """
  980. if not GVL_vol_exists(id):
  981. return -1
  982. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  983. return -2
  984. if up:
  985. ret = GVL_isosurf_move_up(id, isosurf_id)
  986. else:
  987. ret = GVL_isosurf_move_down(id, isosurf_id)
  988. if ret < 0:
  989. return -3
  990. return 1
  991. def MoveSlice(self, id, slice_id, up):
  992. """Move slice up/down in the list
  993. :param id: volume id
  994. :param slice_id: slice id
  995. :param up: if true move up otherwise down
  996. :return: 1 on success
  997. :return: -1 volume not found
  998. :return: -2 slice not found
  999. :return: -3 on failure
  1000. """
  1001. if not GVL_vol_exists(id):
  1002. return -1
  1003. if slice_id > GVL_slice_num_slices(id):
  1004. return -2
  1005. if up:
  1006. ret = GVL_slice_move_up(id, slice_id)
  1007. else:
  1008. ret = GVL_slice_move_down(id, slice_id)
  1009. if ret < 0:
  1010. return -3
  1011. return 1
  1012. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  1013. """Set isosurface level
  1014. :param id: volume id
  1015. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1016. :param map: if true use map otherwise constant
  1017. :param value: map name of value
  1018. :return: 1 on success
  1019. :return: -1 volume not found
  1020. :return: -2 isosurface not found
  1021. :return: -3 on failure
  1022. """
  1023. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  1024. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  1025. """Set isosurface color
  1026. :param id: volume id
  1027. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1028. :param map: if true use map otherwise constant
  1029. :param value: map name of value
  1030. :return: 1 on success
  1031. :return: -1 volume not found
  1032. :return: -2 isosurface not found
  1033. :return: -3 on failure
  1034. """
  1035. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  1036. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  1037. """Set isosurface mask
  1038. .. todo::
  1039. invert
  1040. :param id: volume id
  1041. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1042. :param invert: true for invert mask
  1043. :param value: map name to be used for mask
  1044. :return: 1 on success
  1045. :return: -1 volume not found
  1046. :return: -2 isosurface not found
  1047. :return: -3 on failure
  1048. """
  1049. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  1050. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  1051. """Set isosurface transparency
  1052. :param id: volume id
  1053. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1054. :param map: if true use map otherwise constant
  1055. :param value: map name of value
  1056. :return: 1 on success
  1057. :return: -1 volume not found
  1058. :return: -2 isosurface not found
  1059. :return: -3 on failure
  1060. """
  1061. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  1062. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  1063. """Set isosurface shininess
  1064. :param id: volume id
  1065. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1066. :param map: if true use map otherwise constant
  1067. :param value: map name of value
  1068. :return: 1 on success
  1069. :return: -1 volume not found
  1070. :return: -2 isosurface not found
  1071. :return: -3 on failure
  1072. """
  1073. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  1074. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  1075. """Set isosurface emission (currently unused)
  1076. :param id: volume id
  1077. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1078. :param map: if true use map otherwise constant
  1079. :param value: map name of value
  1080. :return: 1 on success
  1081. :return: -1 volume not found
  1082. :return: -2 isosurface not found
  1083. :return: -3 on failure
  1084. """
  1085. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  1086. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  1087. """Set isosurface attribute
  1088. :param id: volume id
  1089. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1090. :param attr: attribute desc
  1091. :param map: if true use map otherwise constant
  1092. :param value: map name of value
  1093. :return: 1 on success
  1094. :return: -1 volume not found
  1095. :return: -2 isosurface not found
  1096. :return: -3 setting attributes failed
  1097. """
  1098. if not GVL_vol_exists(id):
  1099. return -1
  1100. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1101. return -2
  1102. if map:
  1103. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  1104. else:
  1105. if attr == ATT_COLOR:
  1106. val = Nviz_color_from_str(value)
  1107. else:
  1108. val = float(value)
  1109. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  1110. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  1111. "attr=%d, map=%s, value=%s",
  1112. id, isosurf_id, attr, map, value)
  1113. if ret < 0:
  1114. return -2
  1115. return 1
  1116. def UnsetIsosurfaceMask(self, id, isosurf_id):
  1117. """Unset isosurface mask
  1118. :param id: volume id
  1119. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1120. :return: 1 on success
  1121. :return: -1 volume not found
  1122. :return: -2 isosurface not found
  1123. :return: -3 setting attributes failed
  1124. """
  1125. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  1126. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  1127. """Unset isosurface transparency
  1128. :param id: volume id
  1129. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1130. :return: 1 on success
  1131. :return: -1 volume not found
  1132. :return: -2 isosurface not found
  1133. :return: -3 setting attributes failed
  1134. """
  1135. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  1136. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  1137. """Unset isosurface emission (currently unused)
  1138. :param id: volume id
  1139. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1140. :return: 1 on success
  1141. :return: -1 volume not found
  1142. :return: -2 isosurface not found
  1143. :return: -3 setting attributes failed
  1144. """
  1145. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  1146. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  1147. """Unset surface attribute
  1148. :param id: surface id
  1149. :param isosurf_id: isosurface id (0 - MAX_ISOSURFS)
  1150. :param attr: attribute descriptor
  1151. :return: 1 on success
  1152. :return: -1 volume not found
  1153. :return: -2 isosurface not found
  1154. :return: -2 on failure
  1155. """
  1156. if not GVL_vol_exists(id):
  1157. return -1
  1158. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1159. return -2
  1160. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  1161. id, isosurf_id, attr)
  1162. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  1163. if ret < 0:
  1164. return -2
  1165. return 1
  1166. def SetIsosurfaceMode(self, id, mode):
  1167. """Set draw mode for isosurfaces
  1168. :param id: isosurface id
  1169. :param mode: isosurface draw mode
  1170. :return: 1 on success
  1171. :return: -1 volume set not found
  1172. :return: -2 on failure
  1173. """
  1174. if not GVL_vol_exists(id):
  1175. return -1
  1176. ret = GVL_isosurf_set_drawmode(id, mode)
  1177. if ret < 0:
  1178. return -2
  1179. return 1
  1180. def SetSliceMode(self, id, mode):
  1181. """Set draw mode for slices
  1182. :param id: slice id
  1183. :param mode: slice draw mode
  1184. :return: 1 on success
  1185. :return: -1 volume set not found
  1186. :return: -2 on failure
  1187. """
  1188. if not GVL_vol_exists(id):
  1189. return -1
  1190. ret = GVL_slice_set_drawmode(id, mode)
  1191. if ret < 0:
  1192. return -2
  1193. return 1
  1194. def SetIsosurfaceRes(self, id, res):
  1195. """Set draw resolution for isosurfaces
  1196. :param id: isosurface id
  1197. :param res: resolution value
  1198. :return: 1 on success
  1199. :return: -1 volume set not found
  1200. :return: -2 on failure
  1201. """
  1202. if not GVL_vol_exists(id):
  1203. return -1
  1204. ret = GVL_isosurf_set_drawres(id, res, res, res)
  1205. if ret < 0:
  1206. return -2
  1207. return 1
  1208. def SetSliceRes(self, id, res):
  1209. """Set draw resolution for slices
  1210. :param id: slice id
  1211. :param res: resolution value
  1212. :return: 1 on success
  1213. :return: -1 volume set not found
  1214. :return: -2 on failure
  1215. """
  1216. if not GVL_vol_exists(id):
  1217. return -1
  1218. ret = GVL_slice_set_drawres(id, res, res, res)
  1219. if ret < 0:
  1220. return -2
  1221. return 1
  1222. def SetSlicePosition(self, id, slice_id, x1, x2, y1, y2, z1, z2, dir):
  1223. """Set slice position
  1224. :param id: volume id
  1225. :param slice_id: slice id
  1226. :param x1,x2,y1,y2,z1,z2: slice coordinates
  1227. :param dir: axis
  1228. :return: 1 on success
  1229. :return: -1 volume not found
  1230. :return: -2 slice not found
  1231. :return: -3 on failure
  1232. """
  1233. if not GVL_vol_exists(id):
  1234. return -1
  1235. if slice_id > GVL_slice_num_slices(id):
  1236. return -2
  1237. ret = GVL_slice_set_pos(id, slice_id, x1, x2, y1, y2, z1, z2, dir)
  1238. if ret < 0:
  1239. return -2
  1240. return 1
  1241. def SetSliceTransp(self, id, slice_id, value):
  1242. """Set slice transparency
  1243. :param id: volume id
  1244. :param slice_id: slice id
  1245. :param value: transparency value (0 - 255)
  1246. :return: 1 on success
  1247. :return: -1 volume not found
  1248. :return: -2 slice not found
  1249. :return: -3 on failure
  1250. """
  1251. if not GVL_vol_exists(id):
  1252. return -1
  1253. if slice_id > GVL_slice_num_slices(id):
  1254. return -2
  1255. ret = GVL_slice_set_transp(id, slice_id, value)
  1256. if ret < 0:
  1257. return -2
  1258. return 1
  1259. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  1260. """Set inout mode
  1261. :param id: volume id
  1262. :param isosurf_id: isosurface id
  1263. :param inout: mode true/false
  1264. :return: 1 on success
  1265. :return: -1 volume set not found
  1266. :return: -2 isosurface not found
  1267. :return: -3 on failure
  1268. """
  1269. if not GVL_vol_exists(id):
  1270. return -1
  1271. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1272. return -2
  1273. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  1274. if ret < 0:
  1275. return -3
  1276. return 1
  1277. def GetVolumePosition(self, id):
  1278. """Get volume position
  1279. :param id: volume id
  1280. :return: x,y,z
  1281. :return: zero-length vector on error
  1282. """
  1283. if not GVL_vol_exists(id):
  1284. return []
  1285. x, y, z = c_float(), c_float(), c_float()
  1286. GVL_get_trans(id, byref(x), byref(y), byref(z))
  1287. Debug.msg(3, "Nviz::GetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1288. id, x.value, y.value, z.value)
  1289. return [x.value, y.value, z.value]
  1290. def SetVolumePosition(self, id, x, y, z):
  1291. """Set volume position
  1292. :param id: volume id
  1293. :param x,y,z: translation values
  1294. :return: 1 on success
  1295. :return: -1 volume not found
  1296. :return: -2 setting position failed
  1297. """
  1298. if not GVL_vol_exists(id):
  1299. return -1
  1300. Debug.msg(3, "Nviz::SetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1301. id, x, y, z)
  1302. GVL_set_trans(id, x, y, z)
  1303. return 1
  1304. def SetVolumeDrawBox(self, id, ifBox):
  1305. """Display volume wire box
  1306. :param id: volume id
  1307. :param ifBox: True to draw wire box, False otherwise
  1308. :type ifBox: bool
  1309. :return: 1 on success
  1310. :return: -1 volume not found
  1311. """
  1312. if not GVL_vol_exists(id):
  1313. return -1
  1314. Debug.msg(3, "Nviz::SetVolumeDrawBox(): id=%d, ifBox=%d", id, ifBox)
  1315. GVL_set_draw_wire(id, int(ifBox))
  1316. return 1
  1317. def GetCPlaneCurrent(self):
  1318. return Nviz_get_current_cplane(self.data)
  1319. def GetCPlanesCount(self):
  1320. """Returns number of cutting planes"""
  1321. return Nviz_num_cplanes(self.data)
  1322. def GetCPlaneRotation(self):
  1323. """Returns rotation parameters of current cutting plane"""
  1324. x, y, z = c_float(), c_float(), c_float()
  1325. current = Nviz_get_current_cplane(self.data)
  1326. Nviz_get_cplane_rotation(
  1327. self.data,
  1328. current,
  1329. byref(x),
  1330. byref(y),
  1331. byref(z))
  1332. return x.value, y.value, z.value
  1333. def GetCPlaneTranslation(self):
  1334. """Returns translation parameters of current cutting plane"""
  1335. x, y, z = c_float(), c_float(), c_float()
  1336. current = Nviz_get_current_cplane(self.data)
  1337. Nviz_get_cplane_translation(
  1338. self.data, current, byref(x), byref(y), byref(z))
  1339. return x.value, y.value, z.value
  1340. def SetCPlaneRotation(self, x, y, z):
  1341. """Set current clip plane rotation
  1342. :param x,y,z: rotation parameters
  1343. """
  1344. current = Nviz_get_current_cplane(self.data)
  1345. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  1346. Nviz_draw_cplane(self.data, -1, -1)
  1347. def SetCPlaneTranslation(self, x, y, z):
  1348. """Set current clip plane translation
  1349. :param x,y,z: translation parameters
  1350. """
  1351. current = Nviz_get_current_cplane(self.data)
  1352. Nviz_set_cplane_translation(self.data, current, x, y, z)
  1353. Nviz_draw_cplane(self.data, -1, -1)
  1354. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  1355. current, x, y, z)
  1356. def SetCPlaneInteractively(self, x, y):
  1357. current = Nviz_get_current_cplane(self.data)
  1358. ret = Nviz_set_cplane_here(self.data, current, x, y)
  1359. if ret:
  1360. Nviz_draw_cplane(self.data, -1, -1)
  1361. x, y, z = self.GetCPlaneTranslation()
  1362. return x, y, z
  1363. else:
  1364. return None, None, None
  1365. def SelectCPlane(self, index):
  1366. """Select cutting plane
  1367. :param index: index of cutting plane
  1368. """
  1369. Nviz_on_cplane(self.data, index)
  1370. def UnselectCPlane(self, index):
  1371. """Unselect cutting plane
  1372. :param index: index of cutting plane
  1373. """
  1374. Nviz_off_cplane(self.data, index)
  1375. def SetFenceColor(self, index):
  1376. """Select current cutting plane
  1377. :param index: type of fence - from 0 (off) to 4
  1378. """
  1379. Nviz_set_fence_color(self.data, index)
  1380. def GetXYRange(self):
  1381. """Get xy range"""
  1382. return Nviz_get_xyrange(self.data)
  1383. def GetZRange(self):
  1384. """Get z range"""
  1385. min, max = c_float(), c_float()
  1386. Nviz_get_zrange(self.data, byref(min), byref(max))
  1387. return min.value, max.value
  1388. def SaveToFile(self, filename, width=20, height=20, itype='ppm'):
  1389. """Save current GL screen to ppm/tif file
  1390. :param filename: file name
  1391. :param width: image width
  1392. :param height: image height
  1393. :param itype: image type ('ppm' or 'tif')
  1394. """
  1395. widthOrig = self.width
  1396. heightOrig = self.height
  1397. filename = EncodeString(filename)
  1398. self.ResizeWindow(width, height)
  1399. GS_clear(Nviz_get_bgcolor(self.data))
  1400. self.Draw(False, -1)
  1401. if itype == 'ppm':
  1402. GS_write_ppm(filename)
  1403. else:
  1404. GS_write_tif(filename)
  1405. self.ResizeWindow(widthOrig, heightOrig)
  1406. def DrawLightingModel(self):
  1407. """Draw lighting model"""
  1408. if self.showLight:
  1409. Nviz_draw_model(self.data)
  1410. def DrawFringe(self):
  1411. """Draw fringe"""
  1412. Nviz_draw_fringe(self.data)
  1413. def SetFringe(self, sid, color, elev, nw=False,
  1414. ne=False, sw=False, se=False):
  1415. """Set fringe
  1416. :param sid: surface id
  1417. :param color: color
  1418. :param elev: elevation (height)
  1419. :param nw,ne,sw,se: fringe edges (turn on/off)
  1420. """
  1421. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1422. Nviz_set_fringe(self.data,
  1423. sid, Nviz_color_from_str(scolor),
  1424. elev, int(nw), int(ne), int(sw), int(se))
  1425. def DrawArrow(self):
  1426. """Draw north arrow
  1427. """
  1428. return Nviz_draw_arrow(self.data)
  1429. def SetArrow(self, sx, sy, size, color):
  1430. """Set north arrow from canvas coordinates
  1431. :param sx,sy: canvas coordinates
  1432. :param size: arrow length
  1433. :param color: arrow color
  1434. """
  1435. return Nviz_set_arrow(self.data, sx, sy, size,
  1436. Nviz_color_from_str(color))
  1437. def DeleteArrow(self):
  1438. """Delete north arrow
  1439. """
  1440. Nviz_delete_arrow(self.data)
  1441. def SetScalebar(self, id, sx, sy, size, color):
  1442. """Set scale bar from canvas coordinates
  1443. :param sx,sy: canvas coordinates
  1444. :param id: scale bar id
  1445. :param size: scale bar length
  1446. :param color: scale bar color
  1447. """
  1448. return Nviz_set_scalebar(
  1449. self.data, id, sx, sy, size, Nviz_color_from_str(color))
  1450. def DrawScalebar(self):
  1451. """Draw scale bar
  1452. """
  1453. return Nviz_draw_scalebar(self.data)
  1454. def DeleteScalebar(self, id):
  1455. """Delete scalebar
  1456. """
  1457. Nviz_delete_scalebar(self.data, id)
  1458. def GetPointOnSurface(self, sx, sy):
  1459. """Get point on surface
  1460. :param sx,sy: canvas coordinates (LL)
  1461. """
  1462. sid = c_int()
  1463. x = c_float()
  1464. y = c_float()
  1465. z = c_float()
  1466. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1467. num = GS_get_selected_point_on_surface(
  1468. sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1469. if num == 0:
  1470. return (None, None, None, None)
  1471. return (sid.value, x.value, y.value, z.value)
  1472. def QueryMap(self, sx, sy):
  1473. """Query surface map
  1474. :param sx,sy: canvas coordinates (LL)
  1475. """
  1476. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1477. if not sid:
  1478. return None
  1479. catstr = create_string_buffer(256)
  1480. valstr = create_string_buffer(256)
  1481. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1482. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1483. return {'id': sid,
  1484. 'x': x,
  1485. 'y': y,
  1486. 'z': z,
  1487. 'elevation': catstr.value.replace('(', '').replace(')', ''),
  1488. 'color': valstr.value}
  1489. def GetDistanceAlongSurface(self, sid, p1, p2, useExag=True):
  1490. """Get distance measured along surface"""
  1491. d = c_float()
  1492. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1493. byref(d), int(useExag))
  1494. return d.value
  1495. def GetRotationParameters(self, dx, dy):
  1496. """Get rotation parameters (angle, x, y, z axes)
  1497. :param dx,dy: difference from previous mouse drag event
  1498. """
  1499. modelview = (c_double * 16)()
  1500. Nviz_get_modelview(byref(modelview))
  1501. angle = sqrt(dx * dx + dy * dy) / float(self.width + 1) * 180.0
  1502. m = []
  1503. row = []
  1504. for i, item in enumerate(modelview):
  1505. row.append(item)
  1506. if (i + 1) % 4 == 0:
  1507. m.append(row)
  1508. row = []
  1509. inv = matrix(m).I
  1510. ax, ay, az = dy, dx, 0.
  1511. x = inv[0, 0] * ax + inv[1, 0] * ay + inv[2, 0] * az
  1512. y = inv[0, 1] * ax + inv[1, 1] * ay + inv[2, 1] * az
  1513. z = inv[0, 2] * ax + inv[1, 2] * ay + inv[2, 2] * az
  1514. return angle, x, y, z
  1515. def Rotate(self, angle, x, y, z):
  1516. """Set rotation parameters
  1517. Rotate scene (difference from current state).
  1518. :param angle: angle
  1519. :param x,y,z: axis coordinate
  1520. """
  1521. Nviz_set_rotation(angle, x, y, z)
  1522. def UnsetRotation(self):
  1523. """Stop rotating the scene"""
  1524. Nviz_unset_rotation()
  1525. def ResetRotation(self):
  1526. """Reset scene rotation"""
  1527. Nviz_init_rotation()
  1528. def GetRotationMatrix(self):
  1529. """Get rotation matrix"""
  1530. matrix = (c_double * 16)()
  1531. GS_get_rotation_matrix(byref(matrix))
  1532. returnMatrix = []
  1533. for item in matrix:
  1534. returnMatrix.append(item)
  1535. return returnMatrix
  1536. def SetRotationMatrix(self, matrix):
  1537. """Set rotation matrix"""
  1538. mtrx = (c_double * 16)()
  1539. for i in range(len(matrix)):
  1540. mtrx[i] = matrix[i]
  1541. GS_set_rotation_matrix(byref(mtrx))
  1542. def Start2D(self):
  1543. Nviz_set_2D(self.width, self.height)
  1544. def FlyThrough(self, flyInfo, mode, exagInfo):
  1545. """Fly through the scene
  1546. :param flyInfo: fly parameters
  1547. :param mode: 0 or 1 for different fly behaviour
  1548. :param exagInfo: parameters changing fly speed
  1549. """
  1550. fly = (c_float * 3)()
  1551. for i, item in enumerate(flyInfo):
  1552. fly[i] = item
  1553. exag = (c_int * 2)()
  1554. exag[0] = int(exagInfo['move'])
  1555. exag[1] = int(exagInfo['turn'])
  1556. Nviz_flythrough(self.data, fly, exag, mode)
  1557. class Texture(object):
  1558. """Class representing OpenGL texture"""
  1559. def __init__(self, filepath, overlayId, coords):
  1560. """Load image to texture
  1561. :param filepath: path to image file
  1562. :param overlayId: id of overlay (1 for legend, 101 and more for text)
  1563. :param coords: image coordinates
  1564. """
  1565. self.path = filepath
  1566. self.image = autoCropImageFromFile(filepath)
  1567. self.width = self.orig_width = self.image.GetWidth()
  1568. self.height = self.orig_height = self.image.GetHeight()
  1569. self.id = overlayId
  1570. self.coords = coords
  1571. self.active = True
  1572. # alpha needs to be initialized
  1573. if not self.image.HasAlpha():
  1574. self.image.InitAlpha()
  1575. # resize image to match 2^n
  1576. self.Resize()
  1577. # check max texture size
  1578. maxSize = c_int()
  1579. Nviz_get_max_texture(byref(maxSize))
  1580. self.maxSize = maxSize.value
  1581. if self.maxSize < self.width or self.maxSize < self.height:
  1582. # TODO: split up image
  1583. self.textureId = None
  1584. else:
  1585. self.textureId = self.Load()
  1586. def __del__(self):
  1587. """Delete texture"""
  1588. if self.textureId:
  1589. Nviz_del_texture(self.textureId)
  1590. grass.try_remove(self.path)
  1591. def Resize(self):
  1592. """Resize image to match 2^n"""
  1593. n = m = 1
  1594. while self.width > pow(2, n):
  1595. n += 1
  1596. while self.height > pow(2, m):
  1597. m += 1
  1598. self.image.Resize(size=(pow(2, n), pow(2, m)), pos=(0, 0))
  1599. self.width = self.image.GetWidth()
  1600. self.height = self.image.GetHeight()
  1601. def Load(self):
  1602. """Load image to texture"""
  1603. if self.image.HasAlpha():
  1604. bytesPerPixel = 4
  1605. else:
  1606. bytesPerPixel = 3
  1607. bytes = bytesPerPixel * self.width * self.height
  1608. rev_val = self.height - 1
  1609. im = (c_ubyte * bytes)()
  1610. bytes3 = 3 * self.width * self.height
  1611. bytes1 = self.width * self.height
  1612. imageData = struct.unpack(str(bytes3) + 'B', self.image.GetData())
  1613. if self.image.HasAlpha():
  1614. if wxPythonPhoenix:
  1615. alphaData = struct.unpack(
  1616. str(bytes1) + 'B', self.image.GetAlpha())
  1617. else:
  1618. alphaData = struct.unpack(
  1619. str(bytes1) + 'B', self.image.GetAlphaData())
  1620. # this takes too much time
  1621. wx.BeginBusyCursor()
  1622. for i in range(self.height):
  1623. for j in range(self.width):
  1624. im[(j + i * self.width) * bytesPerPixel + 0] = imageData[(j +
  1625. (rev_val - i) * self.width) * 3 + 0]
  1626. im[(j + i * self.width) * bytesPerPixel + 1] = imageData[(j +
  1627. (rev_val - i) * self.width) * 3 + 1]
  1628. im[(j + i * self.width) * bytesPerPixel + 2] = imageData[(j +
  1629. (rev_val - i) * self.width) * 3 + 2]
  1630. if self.image.HasAlpha():
  1631. im[(j + i * self.width) * bytesPerPixel + 3] = alphaData[(j +
  1632. (rev_val - i) * self.width)]
  1633. wx.EndBusyCursor()
  1634. id = Nviz_load_image(
  1635. im,
  1636. self.width,
  1637. self.height,
  1638. self.image.HasAlpha())
  1639. return id
  1640. def Draw(self):
  1641. """Draw texture as an image"""
  1642. Nviz_draw_image(
  1643. self.coords[0],
  1644. self.coords[1],
  1645. self.width,
  1646. self.height,
  1647. self.textureId)
  1648. def HitTest(self, x, y, radius):
  1649. copy = Rect(self.coords[0], self.coords[1], self.orig_width, self.orig_height)
  1650. copy.Inflate(radius, radius)
  1651. return copy.ContainsXY(x, y)
  1652. def MoveTexture(self, dx, dy):
  1653. """Move texture on the screen"""
  1654. self.coords[0] += dx
  1655. self.coords[1] += dy
  1656. def SetCoords(self, coords):
  1657. """Set coordinates"""
  1658. dx = coords[0] - self.coords[0]
  1659. dy = coords[1] - self.coords[1]
  1660. self.MoveTexture(dx, dy)
  1661. def GetId(self):
  1662. """Returns image id."""
  1663. return self.id
  1664. def SetActive(self, active=True):
  1665. self.active = active
  1666. def IsActive(self):
  1667. return self.active
  1668. class ImageTexture(Texture):
  1669. """Class representing OpenGL texture as an overlay image"""
  1670. def __init__(self, filepath, overlayId, coords, cmd):
  1671. """Load image to texture
  1672. :param filepath: path to image file
  1673. :param overlayId: id of overlay (1 for legend)
  1674. :param coords: image coordinates
  1675. :param cmd: d.legend command
  1676. """
  1677. Texture.__init__(
  1678. self,
  1679. filepath=filepath,
  1680. overlayId=overlayId,
  1681. coords=coords)
  1682. self.cmd = cmd
  1683. def GetCmd(self):
  1684. """Returns overlay command."""
  1685. return self.cmd
  1686. def Corresponds(self, item):
  1687. return sorted(self.GetCmd()) == sorted(item.GetCmd())