wxnviz.py 64 KB

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