wxnviz.py 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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. try:
  34. from grass.lib.gis import *
  35. from grass.lib.raster3d import *
  36. from grass.lib.vector import *
  37. from grass.lib.ogsf import *
  38. from grass.lib.nviz import *
  39. from grass.lib.raster import *
  40. except ImportError, e:
  41. sys.stderr.write(_("3D view mode: %s\n") % e)
  42. from core.debug import Debug
  43. import grass.script as grass
  44. log = None
  45. progress = None
  46. def print_error(msg, type):
  47. """!Redirect stderr"""
  48. global log
  49. if log:
  50. log.write(msg)
  51. else:
  52. print msg
  53. return 0
  54. def print_progress(value):
  55. """!Redirect progress info"""
  56. global progress
  57. if progress:
  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 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 invert
  440. @param id surface id
  441. @param invert if true invert mask
  442. @param value map name of value
  443. @return 1 on success
  444. @return -1 surface not found
  445. @return -2 setting attributes failed
  446. """
  447. return self.SetSurfaceAttr(id, ATT_MASK, True, value)
  448. def SetSurfaceTransp(self, id, map, value):
  449. """!Set surface mask
  450. @todo invert
  451. @param id surface id
  452. @param map if true use map otherwise constant
  453. @param value map name of value
  454. @return 1 on success
  455. @return -1 surface not found
  456. @return -2 setting attributes failed
  457. """
  458. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  459. def SetSurfaceShine(self, id, map, value):
  460. """!Set surface shininess
  461. @param id surface id
  462. @param map if true use map otherwise constant
  463. @param value map name of value
  464. @return 1 on success
  465. @return -1 surface not found
  466. @return -2 setting attributes failed
  467. """
  468. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  469. def SetSurfaceEmit(self, id, map, value):
  470. """!Set surface emission (currently unused)
  471. @param id surface id
  472. @param map if true use map otherwise constant
  473. @param value map name of value
  474. @return 1 on success
  475. @return -1 surface not found
  476. @return -2 setting attributes failed
  477. """
  478. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  479. def SetSurfaceAttr(self, id, attr, map, value):
  480. """!Set surface attribute
  481. @param id surface id
  482. @param attr attribute desc
  483. @param map if true use map otherwise constant
  484. @param value map name of value
  485. @return 1 on success
  486. @return -1 surface not found
  487. @return -2 setting attributes failed
  488. """
  489. if not GS_surf_exists(id):
  490. return -1
  491. if map:
  492. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  493. value, -1.0, self.data)
  494. else:
  495. if attr == ATT_COLOR:
  496. val = Nviz_color_from_str(value)
  497. else:
  498. val = float(value)
  499. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  500. None, val, self.data)
  501. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  502. id, attr, map, value)
  503. if ret < 0:
  504. return -2
  505. return 1
  506. def UnsetSurfaceMask(self, id):
  507. """!Unset surface mask
  508. @param id surface id
  509. @return 1 on success
  510. @return -1 surface not found
  511. @return -2 setting attributes failed
  512. @return -1 on failure
  513. """
  514. return self.UnsetSurfaceAttr(id, ATT_MASK)
  515. def UnsetSurfaceTransp(self, id):
  516. """!Unset surface transparency
  517. @param id surface id
  518. @return 1 on success
  519. @return -1 surface not found
  520. @return -2 setting attributes failed
  521. """
  522. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  523. def UnsetSurfaceEmit(self, id):
  524. """!Unset surface emission (currently unused)
  525. @param id surface id
  526. @return 1 on success
  527. @return -1 surface not found
  528. @return -2 setting attributes failed
  529. """
  530. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  531. def UnsetSurfaceAttr(self, id, attr):
  532. """!Unset surface attribute
  533. @param id surface id
  534. @param attr attribute descriptor
  535. @return 1 on success
  536. @return -1 surface not found
  537. @return -2 setting attributes failed
  538. """
  539. if not GS_surf_exists(id):
  540. return -1
  541. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  542. id, attr)
  543. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  544. if ret < 0:
  545. return -2
  546. return 1
  547. def SetSurfaceRes(self, id, fine, coarse):
  548. """!Set surface resolution
  549. @param id surface id
  550. @param fine x/y fine resolution
  551. @param coarse x/y coarse resolution
  552. @return 1 on success
  553. @return -1 surface not found
  554. @return -2 setting attributes failed
  555. """
  556. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  557. id, fine, coarse)
  558. if id > 0:
  559. if not GS_surf_exists(id):
  560. return -1
  561. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  562. return -2
  563. else:
  564. GS_setall_drawres(fine, fine, coarse, coarse)
  565. return 1
  566. def SetSurfaceStyle(self, id, style):
  567. """!Set draw style
  568. Draw styles:
  569. - DM_GOURAUD
  570. - DM_FLAT
  571. - DM_FRINGE
  572. - DM_WIRE
  573. - DM_COL_WIRE
  574. - DM_POLY
  575. - DM_WIRE_POLY
  576. - DM_GRID_WIRE
  577. - DM_GRID_SURF
  578. @param id surface id (<= 0 for all)
  579. @param style draw style
  580. @return 1 on success
  581. @return -1 surface not found
  582. @return -2 setting attributes failed
  583. """
  584. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  585. id, style)
  586. if id > 0:
  587. if not GS_surf_exists(id):
  588. return -1
  589. if GS_set_drawmode(id, style) < 0:
  590. return -2
  591. return 1
  592. if GS_setall_drawmode(style) < 0:
  593. return -2
  594. return 1
  595. def SetWireColor(self, id, color_str):
  596. """!Set color of wire
  597. @todo all
  598. @param id surface id (< 0 for all)
  599. @param color_str color string (R:G:B)
  600. @return 1 on success
  601. @return -1 surface not found
  602. @return -2 setting attributes failed
  603. @return 1 on success
  604. @return 0 on failure
  605. """
  606. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  607. id, color_str)
  608. color = Nviz_color_from_str(color_str)
  609. if id > 0:
  610. if not GS_surf_exists(id):
  611. return -1
  612. GS_set_wire_color(id, color)
  613. else:
  614. nsurfs = c_int()
  615. surf_list = GS_get_surf_list(byref(nsurfs))
  616. for i in xrange(nsurfs.value):
  617. id = surf_list[i]
  618. GS_set_wire_color(id, color)
  619. G_free(surf_list)
  620. surf_list = None
  621. return 1
  622. def GetSurfacePosition(self, id):
  623. """!Get surface position
  624. @param id surface id
  625. @return x,y,z
  626. @return zero-length vector on error
  627. """
  628. if not GS_surf_exists(id):
  629. return []
  630. x, y, z = c_float(), c_float(), c_float()
  631. GS_get_trans(id, byref(x), byref(y), byref(z))
  632. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  633. id, x.value, y.value, z.value)
  634. return [x.value, y.value, z.value]
  635. def SetSurfacePosition(self, id, x, y, z):
  636. """!Set surface position
  637. @param id surface id
  638. @param x,y,z translation values
  639. @return 1 on success
  640. @return -1 surface not found
  641. @return -2 setting position failed
  642. """
  643. if not GS_surf_exists(id):
  644. return -1
  645. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  646. id, x, y, z)
  647. GS_set_trans(id, x, y, z)
  648. return 1
  649. def SetVectorLineMode(self, id, color_str, width, flat):
  650. """!Set mode of vector line overlay
  651. @param id vector id
  652. @param color_str color string
  653. @param width line width
  654. @param flat display flat or on surface
  655. @return -1 vector set not found
  656. @return -2 on failure
  657. @return 1 on success
  658. """
  659. if not GV_vect_exists(id):
  660. return -1
  661. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  662. id, color_str, width, flat)
  663. color = Nviz_color_from_str(color_str)
  664. # use memory by default
  665. if GV_set_style(id, 1, color, width, flat) < 0:
  666. return -2
  667. return 1
  668. def SetVectorLineHeight(self, id, height):
  669. """!Set vector height above surface (lines)
  670. @param id vector set id
  671. @param height
  672. @return -1 vector set not found
  673. @return 1 on success
  674. """
  675. if not GV_vect_exists(id):
  676. return -1
  677. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  678. id, height)
  679. GV_set_trans(id, 0.0, 0.0, height)
  680. return 1
  681. def SetVectorLineSurface(self, id, surf_id):
  682. """!Set reference surface of vector set (lines)
  683. @param id vector set id
  684. @param surf_id surface id
  685. @return 1 on success
  686. @return -1 vector set not found
  687. @return -2 surface not found
  688. @return -3 on failure
  689. """
  690. if not GV_vect_exists(id):
  691. return -1
  692. if not GS_surf_exists(surf_id):
  693. return -2
  694. if GV_select_surf(id, surf_id) < 0:
  695. return -3
  696. return 1
  697. def UnsetVectorLineSurface(self, id, surf_id):
  698. """!Unset reference surface of vector set (lines)
  699. @param id vector set id
  700. @param surf_id surface id
  701. @return 1 on success
  702. @return -1 vector set not found
  703. @return -2 surface not found
  704. @return -3 on failure
  705. """
  706. if not GV_vect_exists(id):
  707. return -1
  708. if not GS_surf_exists(surf_id):
  709. return -2
  710. if GV_unselect_surf(id, surf_id) < 0:
  711. return -3
  712. return 1
  713. def SetVectorPointMode(self, id, color_str, width, size, marker):
  714. """!Set mode of vector point overlay
  715. @param id vector id
  716. @param color_str color string
  717. @param width line width
  718. @param size size of the symbol
  719. @param marker type of the symbol
  720. @return -1 vector set not found
  721. """
  722. if not GP_site_exists(id):
  723. return -1
  724. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  725. "width=%d, size=%f, marker=%d",
  726. id, color_str, width, size, marker)
  727. color = Nviz_color_from_str(color_str)
  728. if GP_set_style(id, color, width, size, marker) < 0:
  729. return -2
  730. return 1
  731. def SetVectorPointHeight(self, id, height):
  732. """!Set vector height above surface (points)
  733. @param id vector set id
  734. @param height
  735. @return -1 vector set not found
  736. @return 1 on success
  737. """
  738. if not GP_site_exists(id):
  739. return -1
  740. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  741. id, height)
  742. GP_set_trans(id, 0.0, 0.0, height)
  743. return 1
  744. def SetVectorPointSurface(self, id, surf_id):
  745. """!Set reference surface of vector set (points)
  746. @param id vector set id
  747. @param surf_id surface id
  748. @return 1 on success
  749. @return -1 vector set not found
  750. @return -2 surface not found
  751. @return -3 on failure
  752. """
  753. if not GP_site_exists(id):
  754. return -1
  755. if not GS_surf_exists(surf_id):
  756. return -2
  757. if GP_select_surf(id, surf_id) < 0:
  758. return -3
  759. return 1
  760. def ReadVectorColors(self, name, mapset):
  761. """!Read vector colors
  762. @param name vector map name
  763. @param mapset mapset name (empty string (\c "") for search path)
  764. @return -1 on error
  765. @return 0 if color table missing
  766. @return 1 on success (color table found)
  767. """
  768. return Vect_read_colors(name, mapset, self.color)
  769. def CheckColorTable(self, id, type):
  770. """!Check if color table exists.
  771. @param id vector set id
  772. @param type vector set type (lines/points)
  773. @return 1 color table exists
  774. @return 0 no color table found
  775. @return -1 on error
  776. @return -2 vector set not found
  777. """
  778. file = c_char_p()
  779. if type == 'points':
  780. ret = GP_get_sitename(id, byref(file))
  781. elif type == 'lines':
  782. ret = GV_get_vectname(id, byref(file))
  783. if ret < 0:
  784. return -2
  785. return self.ReadVectorColors(file, "")
  786. def SetPointsStyleThematic(self, id, layer, color = None, colorTable = False,
  787. width = None, size = None, symbol = None):
  788. """!Set thematic style for vector points
  789. @param id vector set id
  790. @param layer layer number for thematic mapping
  791. @param colorTable use color table
  792. @param color color column name
  793. @param width width column name
  794. @param size size column name
  795. @param symbol symbol column name
  796. """
  797. file = c_char_p()
  798. ret = GP_get_sitename(id, byref(file))
  799. if ret < 0:
  800. return -1
  801. ret = self.ReadVectorColors(file, "")
  802. if ret < 0:
  803. return -1
  804. if colorTable:
  805. GP_set_style_thematic(id, layer, color, width, size, symbol, self.color)
  806. else:
  807. GP_set_style_thematic(id, layer, color, width, size, symbol, None)
  808. def SetLinesStyleThematic(self, id, layer, color = None, colorTable = False, width = None):
  809. """!Set thematic style for vector lines
  810. @param id vector set id
  811. @param layer layer number for thematic mapping
  812. @param color color column name
  813. @param colorTable use color table
  814. @param width width column name
  815. """
  816. file = c_char_p()
  817. ret = GV_get_vectname(id, byref(file))
  818. if ret < 0:
  819. return -1
  820. ret = self.ReadVectorColors(file, "")
  821. if ret < 0:
  822. return -1
  823. if colorTable:
  824. GV_set_style_thematic(id, layer, color, width, self.color)
  825. else:
  826. GV_set_style_thematic(id, layer, color, width, None)
  827. def UnsetLinesStyleThematic(self, id):
  828. """!Unset thematic style for vector points"""
  829. GV_unset_style_thematic(id)
  830. def UnsetPointsStyleThematic(self, id):
  831. """!Unset thematic style for vector lines"""
  832. GP_unset_style_thematic(id)
  833. def UnsetVectorPointSurface(self, id, surf_id):
  834. """!Unset reference surface of vector set (points)
  835. @param id vector set id
  836. @param surf_id surface id
  837. @return 1 on success
  838. @return -1 vector set not found
  839. @return -2 surface not found
  840. @return -3 on failure
  841. """
  842. if not GP_site_exists(id):
  843. return -1
  844. if not GS_surf_exists(surf_id):
  845. return -2
  846. if GP_unselect_surf(id, surf_id) < 0:
  847. return -3
  848. return 1
  849. def AddIsosurface(self, id, level, isosurf_id = None):
  850. """!Add new isosurface
  851. @param id volume id
  852. @param level isosurface level (topography)
  853. @param isosurf_id isosurface id
  854. @return -1 on failure
  855. @return 1 on success
  856. """
  857. if not GVL_vol_exists(id):
  858. return -1
  859. if isosurf_id is not None:
  860. num = GVL_isosurf_num_isosurfs(id)
  861. if num < 0 or isosurf_id != num:
  862. return -1
  863. if GVL_isosurf_add(id) < 0:
  864. return -1
  865. # set topography level
  866. nisosurfs = GVL_isosurf_num_isosurfs(id)
  867. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  868. def AddSlice(self, id, slice_id = None):
  869. """!Add new slice
  870. @param id volume id
  871. @param slice_id slice id
  872. @return -1 on failure
  873. @return number of slices
  874. """
  875. if not GVL_vol_exists(id):
  876. return -1
  877. if slice_id is not None:
  878. num = GVL_slice_num_slices(id)
  879. if num < 0 or slice_id != num:
  880. return -1
  881. if GVL_slice_add(id) < 0:
  882. return -1
  883. return GVL_slice_num_slices(id)
  884. def DeleteIsosurface(self, id, isosurf_id):
  885. """!Delete isosurface
  886. @param id volume id
  887. @param isosurf_id isosurface id
  888. @return 1 on success
  889. @return -1 volume not found
  890. @return -2 isosurface not found
  891. @return -3 on failure
  892. """
  893. if not GVL_vol_exists(id):
  894. return -1
  895. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  896. return -2
  897. ret = GVL_isosurf_del(id, isosurf_id)
  898. if ret < 0:
  899. return -3
  900. return 1
  901. def DeleteSlice(self, id, slice_id):
  902. """!Delete slice
  903. @param id volume id
  904. @param slice_id slice id
  905. @return 1 on success
  906. @return -1 volume not found
  907. @return -2 slice not found
  908. @return -3 on failure
  909. """
  910. if not GVL_vol_exists(id):
  911. return -1
  912. if slice_id > GVL_slice_num_slices(id):
  913. return -2
  914. ret = GVL_slice_del(id, slice_id)
  915. if ret < 0:
  916. return -3
  917. return 1
  918. def MoveIsosurface(self, id, isosurf_id, up):
  919. """!Move isosurface up/down in the list
  920. @param id volume id
  921. @param isosurf_id isosurface id
  922. @param up if true move up otherwise down
  923. @return 1 on success
  924. @return -1 volume not found
  925. @return -2 isosurface not found
  926. @return -3 on failure
  927. """
  928. if not GVL_vol_exists(id):
  929. return -1
  930. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  931. return -2
  932. if up:
  933. ret = GVL_isosurf_move_up(id, isosurf_id)
  934. else:
  935. ret = GVL_isosurf_move_down(id, isosurf_id)
  936. if ret < 0:
  937. return -3
  938. return 1
  939. def MoveSlice(self, id, slice_id, up):
  940. """!Move slice up/down in the list
  941. @param id volume id
  942. @param slice_id slice id
  943. @param up if true move up otherwise down
  944. @return 1 on success
  945. @return -1 volume not found
  946. @return -2 slice not found
  947. @return -3 on failure
  948. """
  949. if not GVL_vol_exists(id):
  950. return -1
  951. if slice_id > GVL_slice_num_slices(id):
  952. return -2
  953. if up:
  954. ret = GVL_slice_move_up(id, slice_id)
  955. else:
  956. ret = GVL_slice_move_down(id, slice_id)
  957. if ret < 0:
  958. return -3
  959. return 1
  960. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  961. """!Set isosurface level
  962. @param id volume id
  963. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  964. @param map if true use map otherwise constant
  965. @param value map name of value
  966. @return 1 on success
  967. @return -1 volume not found
  968. @return -2 isosurface not found
  969. @return -3 on failure
  970. """
  971. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  972. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  973. """!Set isosurface color
  974. @param id volume id
  975. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  976. @param map if true use map otherwise constant
  977. @param value map name of value
  978. @return 1 on success
  979. @return -1 volume not found
  980. @return -2 isosurface not found
  981. @return -3 on failure
  982. """
  983. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  984. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  985. """!Set isosurface mask
  986. @todo invert
  987. @param id volume id
  988. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  989. @param invert true for invert mask
  990. @param value map name to be used for mask
  991. @return 1 on success
  992. @return -1 volume not found
  993. @return -2 isosurface not found
  994. @return -3 on failure
  995. """
  996. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  997. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  998. """!Set isosurface transparency
  999. @param id volume id
  1000. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1001. @param map if true use map otherwise constant
  1002. @param value map name of value
  1003. @return 1 on success
  1004. @return -1 volume not found
  1005. @return -2 isosurface not found
  1006. @return -3 on failure
  1007. """
  1008. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  1009. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  1010. """!Set isosurface shininess
  1011. @param id volume id
  1012. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1013. @param map if true use map otherwise constant
  1014. @param value map name of value
  1015. @return 1 on success
  1016. @return -1 volume not found
  1017. @return -2 isosurface not found
  1018. @return -3 on failure
  1019. """
  1020. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  1021. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  1022. """!Set isosurface emission (currently unused)
  1023. @param id volume id
  1024. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1025. @param map if true use map otherwise constant
  1026. @param value map name of value
  1027. @return 1 on success
  1028. @return -1 volume not found
  1029. @return -2 isosurface not found
  1030. @return -3 on failure
  1031. """
  1032. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  1033. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  1034. """!Set isosurface attribute
  1035. @param id volume id
  1036. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1037. @param attr attribute desc
  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 setting attributes failed
  1044. """
  1045. if not GVL_vol_exists(id):
  1046. return -1
  1047. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1048. return -2
  1049. if map:
  1050. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  1051. else:
  1052. if attr == ATT_COLOR:
  1053. val = Nviz_color_from_str(value)
  1054. else:
  1055. val = float(value)
  1056. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  1057. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  1058. "attr=%d, map=%s, value=%s",
  1059. id, isosurf_id, attr, map, value)
  1060. if ret < 0:
  1061. return -2
  1062. return 1
  1063. def UnsetIsosurfaceMask(self, id, isosurf_id):
  1064. """!Unset isosurface mask
  1065. @param id volume id
  1066. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1067. @return 1 on success
  1068. @return -1 volume not found
  1069. @return -2 isosurface not found
  1070. @return -3 setting attributes failed
  1071. """
  1072. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  1073. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  1074. """!Unset isosurface transparency
  1075. @param id volume id
  1076. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1077. @return 1 on success
  1078. @return -1 volume not found
  1079. @return -2 isosurface not found
  1080. @return -3 setting attributes failed
  1081. """
  1082. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  1083. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  1084. """!Unset isosurface emission (currently unused)
  1085. @param id volume id
  1086. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1087. @return 1 on success
  1088. @return -1 volume not found
  1089. @return -2 isosurface not found
  1090. @return -3 setting attributes failed
  1091. """
  1092. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  1093. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  1094. """!Unset surface attribute
  1095. @param id surface id
  1096. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1097. @param attr attribute descriptor
  1098. @return 1 on success
  1099. @return -1 volume not found
  1100. @return -2 isosurface not found
  1101. @return -2 on failure
  1102. """
  1103. if not GVL_vol_exists(id):
  1104. return -1
  1105. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1106. return -2
  1107. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  1108. id, isosurf_id, attr)
  1109. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  1110. if ret < 0:
  1111. return -2
  1112. return 1
  1113. def SetIsosurfaceMode(self, id, mode):
  1114. """!Set draw mode for isosurfaces
  1115. @param id isosurface id
  1116. @param mode isosurface draw mode
  1117. @return 1 on success
  1118. @return -1 volume set not found
  1119. @return -2 on failure
  1120. """
  1121. if not GVL_vol_exists(id):
  1122. return -1
  1123. ret = GVL_isosurf_set_drawmode(id, mode)
  1124. if ret < 0:
  1125. return -2
  1126. return 1
  1127. def SetSliceMode(self, id, mode):
  1128. """!Set draw mode for slices
  1129. @param id slice id
  1130. @param mode slice draw mode
  1131. @return 1 on success
  1132. @return -1 volume set not found
  1133. @return -2 on failure
  1134. """
  1135. if not GVL_vol_exists(id):
  1136. return -1
  1137. ret = GVL_slice_set_drawmode(id, mode)
  1138. if ret < 0:
  1139. return -2
  1140. return 1
  1141. def SetIsosurfaceRes(self, id, res):
  1142. """!Set draw resolution for isosurfaces
  1143. @param id isosurface id
  1144. @param res resolution value
  1145. @return 1 on success
  1146. @return -1 volume set not found
  1147. @return -2 on failure
  1148. """
  1149. if not GVL_vol_exists(id):
  1150. return -1
  1151. ret = GVL_isosurf_set_drawres(id, res, res, res)
  1152. if ret < 0:
  1153. return -2
  1154. return 1
  1155. def SetSliceRes(self, id, res):
  1156. """!Set draw resolution for slices
  1157. @param id slice id
  1158. @param res resolution value
  1159. @return 1 on success
  1160. @return -1 volume set not found
  1161. @return -2 on failure
  1162. """
  1163. if not GVL_vol_exists(id):
  1164. return -1
  1165. ret = GVL_slice_set_drawres(id, res, res, res)
  1166. if ret < 0:
  1167. return -2
  1168. return 1
  1169. def SetSlicePosition(self, id, slice_id, x1, x2, y1, y2, z1, z2, dir):
  1170. """!Set slice position
  1171. @param id volume id
  1172. @param slice_id slice id
  1173. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1174. @param dir axis
  1175. @return 1 on success
  1176. @return -1 volume not found
  1177. @return -2 slice not found
  1178. @return -3 on failure
  1179. """
  1180. if not GVL_vol_exists(id):
  1181. return -1
  1182. if slice_id > GVL_slice_num_slices(id):
  1183. return -2
  1184. ret = GVL_slice_set_pos(id, slice_id, x1, x2, y1, y2, z1, z2, dir)
  1185. if ret < 0:
  1186. return -2
  1187. return 1
  1188. def SetSliceTransp(self, id, slice_id, value):
  1189. """!Set slice transparency
  1190. @param id volume id
  1191. @param slice_id slice id
  1192. @param value transparency value (0 - 255)
  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_transp(id, slice_id, value)
  1203. if ret < 0:
  1204. return -2
  1205. return 1
  1206. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  1207. """!Set inout mode
  1208. @param id volume id
  1209. @param isosurf_id isosurface id
  1210. @param inout mode true/false
  1211. @return 1 on success
  1212. @return -1 volume set not found
  1213. @return -2 isosurface not found
  1214. @return -3 on failure
  1215. """
  1216. if not GVL_vol_exists(id):
  1217. return -1
  1218. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1219. return -2
  1220. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  1221. if ret < 0:
  1222. return -3
  1223. return 1
  1224. def GetVolumePosition(self, id):
  1225. """!Get volume position
  1226. @param id volume id
  1227. @return x,y,z
  1228. @return zero-length vector on error
  1229. """
  1230. if not GVL_vol_exists(id):
  1231. return []
  1232. x, y, z = c_float(), c_float(), c_float()
  1233. GVL_get_trans(id, byref(x), byref(y), byref(z))
  1234. Debug.msg(3, "Nviz::GetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1235. id, x.value, y.value, z.value)
  1236. return [x.value, y.value, z.value]
  1237. def SetVolumePosition(self, id, x, y, z):
  1238. """!Set volume position
  1239. @param id volume id
  1240. @param x,y,z translation values
  1241. @return 1 on success
  1242. @return -1 volume not found
  1243. @return -2 setting position failed
  1244. """
  1245. if not GVL_vol_exists(id):
  1246. return -1
  1247. Debug.msg(3, "Nviz::SetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1248. id, x, y, z)
  1249. GVL_set_trans(id, x, y, z)
  1250. return 1
  1251. def SetVolumeDrawBox(self, id, ifBox):
  1252. """!Display volume wire box
  1253. @param id volume id
  1254. @param ifBox True to draw wire box, False otherwise
  1255. @return 1 on success
  1256. @return -1 volume not found
  1257. """
  1258. if not GVL_vol_exists(id):
  1259. return -1
  1260. Debug.msg(3, "Nviz::SetVolumeDrawBox(): id=%d, ifBox=%d", id, ifBox)
  1261. GVL_set_draw_wire(id, int(ifBox))
  1262. return 1
  1263. def GetCPlaneCurrent(self):
  1264. return Nviz_get_current_cplane(self.data)
  1265. def GetCPlanesCount(self):
  1266. """!Returns number of cutting planes"""
  1267. return Nviz_num_cplanes(self.data)
  1268. def GetCPlaneRotation(self):
  1269. """!Returns rotation parameters of current cutting plane"""
  1270. x, y, z = c_float(), c_float(), c_float()
  1271. current = Nviz_get_current_cplane(self.data)
  1272. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  1273. return x.value, y.value, z.value
  1274. def GetCPlaneTranslation(self):
  1275. """!Returns translation parameters of current cutting plane"""
  1276. x, y, z = c_float(), c_float(), c_float()
  1277. current = Nviz_get_current_cplane(self.data)
  1278. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  1279. return x.value, y.value, z.value
  1280. def SetCPlaneRotation(self, x, y, z):
  1281. """!Set current clip plane rotation
  1282. @param x,y,z rotation parameters
  1283. """
  1284. current = Nviz_get_current_cplane(self.data)
  1285. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  1286. Nviz_draw_cplane(self.data, -1, -1)
  1287. def SetCPlaneTranslation(self, x, y, z):
  1288. """!Set current clip plane translation
  1289. @param x,y,z translation parameters
  1290. """
  1291. current = Nviz_get_current_cplane(self.data)
  1292. Nviz_set_cplane_translation(self.data, current, x, y, z)
  1293. Nviz_draw_cplane(self.data, -1, -1)
  1294. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  1295. current, x, y, z)
  1296. def SetCPlaneInteractively(self, x, y):
  1297. current = Nviz_get_current_cplane(self.data)
  1298. ret = Nviz_set_cplane_here(self.data, current, x, y)
  1299. if ret:
  1300. Nviz_draw_cplane(self.data, -1, -1)
  1301. x, y, z = self.GetCPlaneTranslation()
  1302. return x, y, z
  1303. else:
  1304. return None, None, None
  1305. def SelectCPlane(self, index):
  1306. """!Select cutting plane
  1307. @param index index of cutting plane
  1308. """
  1309. Nviz_on_cplane(self.data, index)
  1310. def UnselectCPlane(self, index):
  1311. """!Unselect cutting plane
  1312. @param index index of cutting plane
  1313. """
  1314. Nviz_off_cplane(self.data, index)
  1315. def SetFenceColor(self, index):
  1316. """!Select current cutting plane
  1317. @param index type of fence - from 0 (off) to 4
  1318. """
  1319. Nviz_set_fence_color(self.data, index)
  1320. def GetXYRange(self):
  1321. """!Get xy range"""
  1322. return Nviz_get_xyrange(self.data)
  1323. def GetZRange(self):
  1324. """!Get z range"""
  1325. min, max = c_float(), c_float()
  1326. Nviz_get_zrange(self.data, byref(min), byref(max))
  1327. return min.value, max.value
  1328. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  1329. """!Save current GL screen to ppm/tif file
  1330. @param filename file name
  1331. @param width image width
  1332. @param height image height
  1333. @param itype image type ('ppm' or 'tif')
  1334. """
  1335. widthOrig = self.width
  1336. heightOrig = self.height
  1337. self.ResizeWindow(width, height)
  1338. GS_clear(Nviz_get_bgcolor(self.data))
  1339. self.Draw(False, -1)
  1340. if itype == 'ppm':
  1341. GS_write_ppm(filename)
  1342. else:
  1343. GS_write_tif(filename)
  1344. self.ResizeWindow(widthOrig, heightOrig)
  1345. def DrawLightingModel(self):
  1346. """!Draw lighting model"""
  1347. if self.showLight:
  1348. Nviz_draw_model(self.data)
  1349. def DrawFringe(self):
  1350. """!Draw fringe"""
  1351. Nviz_draw_fringe(self.data)
  1352. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1353. """!Set fringe
  1354. @param sid surface id
  1355. @param color color
  1356. @param elev elevation (height)
  1357. @param nw,ne,sw,se fringe edges (turn on/off)
  1358. """
  1359. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1360. Nviz_set_fringe(self.data,
  1361. sid, Nviz_color_from_str(scolor),
  1362. elev, int(nw), int(ne), int(sw), int(se))
  1363. def DrawArrow(self):
  1364. """!Draw north arrow
  1365. """
  1366. return Nviz_draw_arrow(self.data)
  1367. def SetArrow(self, sx, sy, size, color):
  1368. """!Set north arrow from canvas coordinates
  1369. @param sx,sy canvas coordinates
  1370. @param size arrow length
  1371. @param color arrow color
  1372. """
  1373. return Nviz_set_arrow(self.data, sx, sy, size, Nviz_color_from_str(color))
  1374. def DeleteArrow(self):
  1375. """!Delete north arrow
  1376. """
  1377. Nviz_delete_arrow(self.data)
  1378. def SetScalebar(self, id, sx, sy, size, color):
  1379. """!Set scale bar from canvas coordinates
  1380. @param sx,sy canvas coordinates
  1381. @param id scale bar id
  1382. @param size scale bar length
  1383. @param color scale bar color
  1384. """
  1385. return Nviz_set_scalebar(self.data, id, sx, sy, size, Nviz_color_from_str(color))
  1386. def DrawScalebar(self):
  1387. """!Draw scale bar
  1388. """
  1389. return Nviz_draw_scalebar(self.data)
  1390. def DeleteScalebar(self, id):
  1391. """!Delete scalebar
  1392. """
  1393. Nviz_delete_scalebar(self.data, id)
  1394. def GetPointOnSurface(self, sx, sy):
  1395. """!Get point on surface
  1396. @param sx,sy canvas coordinates (LL)
  1397. """
  1398. sid = c_int()
  1399. x = c_float()
  1400. y = c_float()
  1401. z = c_float()
  1402. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1403. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1404. if num == 0:
  1405. return (None, None, None, None)
  1406. return (sid.value, x.value, y.value, z.value)
  1407. def QueryMap(self, sx, sy):
  1408. """!Query surface map
  1409. @param sx,sy canvas coordinates (LL)
  1410. """
  1411. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1412. if not sid:
  1413. return None
  1414. catstr = create_string_buffer(256)
  1415. valstr = create_string_buffer(256)
  1416. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1417. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1418. return { 'id' : sid,
  1419. 'x' : x,
  1420. 'y' : y,
  1421. 'z' : z,
  1422. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1423. 'color' : valstr.value }
  1424. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1425. """!Get distance measured along surface"""
  1426. d = c_float()
  1427. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1428. byref(d), int(useExag))
  1429. return d.value
  1430. def GetRotationParameters(self, dx, dy):
  1431. """!Get rotation parameters (angle, x, y, z axes)
  1432. @param dx,dy difference from previous mouse drag event
  1433. """
  1434. modelview = (c_double * 16)()
  1435. Nviz_get_modelview(byref(modelview))
  1436. angle = sqrt(dx*dx+dy*dy)/float(self.width+1)*180.0
  1437. m = []
  1438. row = []
  1439. for i, item in enumerate(modelview):
  1440. row.append(item)
  1441. if (i+1) % 4 == 0:
  1442. m.append(row)
  1443. row = []
  1444. inv = matrix(m).I
  1445. ax, ay, az = dy, dx, 0.
  1446. x = inv[0,0]*ax + inv[1,0]*ay + inv[2,0]*az
  1447. y = inv[0,1]*ax + inv[1,1]*ay + inv[2,1]*az
  1448. z = inv[0,2]*ax + inv[1,2]*ay + inv[2,2]*az
  1449. return angle, x, y, z
  1450. def Rotate(self, angle, x, y, z):
  1451. """!Set rotation parameters
  1452. Rotate scene (difference from current state).
  1453. @param angle angle
  1454. @param x,y,z axis coordinate
  1455. """
  1456. Nviz_set_rotation(angle, x, y, z)
  1457. def UnsetRotation(self):
  1458. """!Stop rotating the scene"""
  1459. Nviz_unset_rotation()
  1460. def ResetRotation(self):
  1461. """!Reset scene rotation"""
  1462. Nviz_init_rotation()
  1463. def GetRotationMatrix(self):
  1464. """!Get rotation matrix"""
  1465. matrix = (c_double * 16)()
  1466. GS_get_rotation_matrix(byref(matrix))
  1467. returnMatrix = []
  1468. for item in matrix:
  1469. returnMatrix.append(item)
  1470. return returnMatrix
  1471. def SetRotationMatrix(self, matrix):
  1472. """!Set rotation matrix"""
  1473. mtrx = (c_double * 16)()
  1474. for i in range(len(matrix)):
  1475. mtrx[i] = matrix[i]
  1476. GS_set_rotation_matrix(byref(mtrx))
  1477. def Start2D(self):
  1478. Nviz_set_2D(self.width, self.height)
  1479. def FlyThrough(self, flyInfo, mode, exagInfo):
  1480. """!Fly through the scene
  1481. @param flyInfo fly parameters
  1482. @param mode 0 or 1 for different fly behaviour
  1483. @param exagInfo parameters changing fly speed
  1484. """
  1485. fly = (c_float * 3)()
  1486. for i, item in enumerate(flyInfo):
  1487. fly[i] = item
  1488. exag = (c_int * 2)()
  1489. exag[0] = int(exagInfo['move'])
  1490. exag[1] = int(exagInfo['turn'])
  1491. Nviz_flythrough(self.data, fly, exag, mode)
  1492. class Texture(object):
  1493. """!Class representing OpenGL texture"""
  1494. def __init__(self, filepath, overlayId, coords):
  1495. """!Load image to texture
  1496. @param filepath path to image file
  1497. @param overlayId id of overlay (1 for legend, 101 and more for text)
  1498. @param coords image coordinates
  1499. """
  1500. self.path = filepath
  1501. self.image = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
  1502. self.width = self.image.GetWidth()
  1503. self.height = self.image.GetHeight()
  1504. self.id = overlayId
  1505. self.coords = list(coords)
  1506. self.bounds = wx.Rect()
  1507. self.active = True
  1508. # alpha needs to be initialized
  1509. if not self.image.HasAlpha():
  1510. self.image.InitAlpha()
  1511. # resize image to match 2^n
  1512. self.Resize()
  1513. # check max texture size
  1514. maxSize = c_int()
  1515. Nviz_get_max_texture(byref(maxSize))
  1516. self.maxSize = maxSize.value
  1517. if self.maxSize < self.width or self.maxSize < self.height:
  1518. # TODO: split up image
  1519. self.textureId = None
  1520. else:
  1521. self.textureId = self.Load()
  1522. def __del__(self):
  1523. """!Delete texture"""
  1524. if self.textureId:
  1525. Nviz_del_texture(self.textureId)
  1526. grass.try_remove(self.path)
  1527. def Resize(self):
  1528. """!Resize image to match 2^n"""
  1529. n = m = 1
  1530. while self.width > pow(2,n):
  1531. n += 1
  1532. while self.height > pow(2,m):
  1533. m += 1
  1534. self.image.Resize(size = (pow(2,n), pow(2,m)), pos = (0, 0))
  1535. self.width = self.image.GetWidth()
  1536. self.height = self.image.GetHeight()
  1537. def Load(self):
  1538. """!Load image to texture"""
  1539. if self.image.HasAlpha():
  1540. bytesPerPixel = 4
  1541. else:
  1542. bytesPerPixel = 3
  1543. bytes = bytesPerPixel * self.width * self.height
  1544. rev_val = self.height - 1
  1545. im = (c_ubyte * bytes)()
  1546. bytes3 = 3 * self.width * self.height
  1547. bytes1 = self.width * self.height
  1548. imageData = struct.unpack(str(bytes3) + 'B', self.image.GetData())
  1549. if self.image.HasAlpha():
  1550. alphaData = struct.unpack(str(bytes1) + 'B', self.image.GetAlphaData())
  1551. # this takes too much time
  1552. wx.BeginBusyCursor()
  1553. for i in range(self.height):
  1554. for j in range(self.width):
  1555. im[(j + i * self.width) * bytesPerPixel + 0] = imageData[( j + (rev_val - i) * self.width) * 3 + 0]
  1556. im[(j + i * self.width) * bytesPerPixel + 1] = imageData[( j + (rev_val - i) * self.width) * 3 + 1]
  1557. im[(j + i * self.width) * bytesPerPixel + 2] = imageData[( j + (rev_val - i) * self.width) * 3 + 2]
  1558. if self.image.HasAlpha():
  1559. im[(j + i * self.width) * bytesPerPixel + 3] = alphaData[( j + (rev_val - i) * self.width)]
  1560. wx.EndBusyCursor()
  1561. id = Nviz_load_image(im, self.width, self.height, self.image.HasAlpha())
  1562. return id
  1563. def Draw(self):
  1564. """!Draw texture as an image"""
  1565. Nviz_draw_image(self.coords[0], self.coords[1], self.width, self.height, self.textureId)
  1566. def SetBounds(self, rect):
  1567. """!Set Bounding Rectangle"""
  1568. self.bounds = rect
  1569. def HitTest(self, x, y, radius):
  1570. copy = wx.Rect(*self.bounds)
  1571. copy.Inflate(radius, radius)
  1572. return copy.ContainsXY(x, y)
  1573. def MoveTexture(self, dx, dy):
  1574. """!Move texture on the screen"""
  1575. self.coords[0] += dx
  1576. self.coords[1] += dy
  1577. self.bounds.OffsetXY(dx, dy)
  1578. def SetCoords(self, coords):
  1579. """!Set coordinates"""
  1580. dx = coords[0] - self.coords[0]
  1581. dy = coords[1] - self.coords[1]
  1582. self.MoveTexture(dx, dy)
  1583. def GetId(self):
  1584. """!Returns image id."""
  1585. return self.id
  1586. def SetActive(self, active = True):
  1587. self.active = active
  1588. def IsActive(self):
  1589. return self.active
  1590. class ImageTexture(Texture):
  1591. """!Class representing OpenGL texture as an overlay image"""
  1592. def __init__(self, filepath, overlayId, coords, cmd):
  1593. """!Load image to texture
  1594. @param filepath path to image file
  1595. @param overlayId id of overlay (1 for legend)
  1596. @param coords image coordinates
  1597. @param cmd d.legend command
  1598. """
  1599. Texture.__init__(self, filepath = filepath, overlayId = overlayId, coords = coords)
  1600. self.cmd = cmd
  1601. def GetCmd(self):
  1602. """!Returns overlay command."""
  1603. return self.cmd
  1604. def Corresponds(self, item):
  1605. return sorted(self.GetCmd()) == sorted(item.GetCmd())
  1606. class TextTexture(Texture):
  1607. """!Class representing OpenGL texture as a text label"""
  1608. def __init__(self, filepath, overlayId, coords, textDict):
  1609. """!Load image to texture
  1610. @param filepath path to image file
  1611. @param overlayId id of overlay (101 and more for text)
  1612. @param coords text coordinates
  1613. @param textDict text properties
  1614. """
  1615. Texture.__init__(self, filepath = filepath, overlayId = overlayId, coords = coords)
  1616. self.textDict = textDict
  1617. def GetTextDict(self):
  1618. """!Returns text properties."""
  1619. return self.textDict
  1620. def Corresponds(self, item):
  1621. t = self.GetTextDict()
  1622. for prop in t.keys():
  1623. if prop in ('coords','bbox'): continue
  1624. if t[prop] != item[prop]:
  1625. return False
  1626. return True