wxnviz.py 61 KB

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