wxnviz.py 61 KB

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