wxnviz.py 60 KB

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