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