wxnviz.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. """!
  2. @package nviz.wxnviz
  3. @brief wxGUI 3D view mode (ctypes-based classes)
  4. This module implements 3D visualization mode for map display (ctypes
  5. required).
  6. List of classes:
  7. - wxnviz::Nviz
  8. - wxnviz::Texture
  9. - wxnviz::ImageTexture
  10. - wxnviz::TextTexture
  11. (C) 2008-2011 by the GRASS Development Team
  12. This program is free software under the GNU General Public License
  13. (>=v2). Read the file COPYING that comes with GRASS for details.
  14. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  15. @author Pythonized by Glynn Clements
  16. @author Anna Kratochvilova <KratochAnna seznam.cz> (Google SoC 2011)
  17. """
  18. import sys
  19. import locale
  20. import struct
  21. from math import sqrt
  22. try:
  23. from numpy import matrix
  24. except ImportError:
  25. msg = _("This module requires the NumPy module, which could not be "
  26. "imported. It probably is not installed (it's not part of the "
  27. "standard Python distribution). See the Numeric Python site "
  28. "(http://numpy.scipy.org) for information on downloading source or "
  29. "binaries.")
  30. print >> sys.stderr, "wxnviz.py: " + msg
  31. import wx
  32. from ctypes import *
  33. from grass.lib.gis import *
  34. from grass.lib.raster3d import *
  35. from grass.lib.vector import *
  36. from grass.lib.ogsf import *
  37. from grass.lib.nviz import *
  38. from grass.lib.raster import *
  39. from core.debug import Debug
  40. from core.utils import _
  41. import grass.script as grass
  42. log = None
  43. progress = None
  44. def print_error(msg, type):
  45. """!Redirect stderr"""
  46. global log
  47. if log:
  48. log.write(msg)
  49. else:
  50. print msg
  51. return 0
  52. def print_progress(value):
  53. """!Redirect progress info"""
  54. global progress
  55. if progress:
  56. if not progress.GetRange() == 100:
  57. progress.SetRange(100)
  58. progress.SafeSetValue(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. # dtree and ctree defined but not used
  725. if marker > 5:
  726. marker += 2
  727. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  728. "width=%d, size=%f, marker=%d",
  729. id, color_str, width, size, marker)
  730. color = Nviz_color_from_str(color_str)
  731. if GP_set_style(id, color, width, size, marker) < 0:
  732. return -2
  733. return 1
  734. def SetVectorPointHeight(self, id, height):
  735. """!Set vector height above surface (points)
  736. @param id vector set id
  737. @param height
  738. @return -1 vector set not found
  739. @return 1 on success
  740. """
  741. if not GP_site_exists(id):
  742. return -1
  743. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  744. id, height)
  745. GP_set_trans(id, 0.0, 0.0, height)
  746. return 1
  747. def SetVectorPointSurface(self, id, surf_id):
  748. """!Set reference surface of vector set (points)
  749. @param id vector set id
  750. @param surf_id surface id
  751. @return 1 on success
  752. @return -1 vector set not found
  753. @return -2 surface not found
  754. @return -3 on failure
  755. """
  756. if not GP_site_exists(id):
  757. return -1
  758. if not GS_surf_exists(surf_id):
  759. return -2
  760. if GP_select_surf(id, surf_id) < 0:
  761. return -3
  762. return 1
  763. def ReadVectorColors(self, name, mapset):
  764. """!Read vector colors
  765. @param name vector map name
  766. @param mapset mapset name (empty string (\c "") for search path)
  767. @return -1 on error
  768. @return 0 if color table missing
  769. @return 1 on success (color table found)
  770. """
  771. return Vect_read_colors(name, mapset, self.color)
  772. def CheckColorTable(self, id, type):
  773. """!Check if color table exists.
  774. @param id vector set id
  775. @param type vector set type (lines/points)
  776. @return 1 color table exists
  777. @return 0 no color table found
  778. @return -1 on error
  779. @return -2 vector set not found
  780. """
  781. file = c_char_p()
  782. if type == 'points':
  783. ret = GP_get_sitename(id, byref(file))
  784. elif type == 'lines':
  785. ret = GV_get_vectname(id, byref(file))
  786. if ret < 0:
  787. return -2
  788. return self.ReadVectorColors(file, "")
  789. def SetPointsStyleThematic(self, id, layer, color = None, colorTable = False,
  790. width = None, size = None, symbol = None):
  791. """!Set thematic style for vector points
  792. @param id vector set id
  793. @param layer layer number for thematic mapping
  794. @param colorTable use color table
  795. @param color color column name
  796. @param width width column name
  797. @param size size column name
  798. @param symbol symbol column name
  799. """
  800. file = c_char_p()
  801. ret = GP_get_sitename(id, byref(file))
  802. if ret < 0:
  803. return -1
  804. ret = self.ReadVectorColors(file, "")
  805. if ret < 0:
  806. return -1
  807. if colorTable:
  808. GP_set_style_thematic(id, layer, color, width, size, symbol, self.color)
  809. else:
  810. GP_set_style_thematic(id, layer, color, width, size, symbol, None)
  811. def SetLinesStyleThematic(self, id, layer, color = None, colorTable = False, width = None):
  812. """!Set thematic style for vector lines
  813. @param id vector set id
  814. @param layer layer number for thematic mapping
  815. @param color color column name
  816. @param colorTable use color table
  817. @param width width column name
  818. """
  819. file = c_char_p()
  820. ret = GV_get_vectname(id, byref(file))
  821. if ret < 0:
  822. return -1
  823. ret = self.ReadVectorColors(file, "")
  824. if ret < 0:
  825. return -1
  826. if colorTable:
  827. GV_set_style_thematic(id, layer, color, width, self.color)
  828. else:
  829. GV_set_style_thematic(id, layer, color, width, None)
  830. def UnsetLinesStyleThematic(self, id):
  831. """!Unset thematic style for vector points"""
  832. GV_unset_style_thematic(id)
  833. def UnsetPointsStyleThematic(self, id):
  834. """!Unset thematic style for vector lines"""
  835. GP_unset_style_thematic(id)
  836. def UnsetVectorPointSurface(self, id, surf_id):
  837. """!Unset reference surface of vector set (points)
  838. @param id vector set id
  839. @param surf_id surface id
  840. @return 1 on success
  841. @return -1 vector set not found
  842. @return -2 surface not found
  843. @return -3 on failure
  844. """
  845. if not GP_site_exists(id):
  846. return -1
  847. if not GS_surf_exists(surf_id):
  848. return -2
  849. if GP_unselect_surf(id, surf_id) < 0:
  850. return -3
  851. return 1
  852. def AddIsosurface(self, id, level, isosurf_id = None):
  853. """!Add new isosurface
  854. @param id volume id
  855. @param level isosurface level (topography)
  856. @param isosurf_id isosurface id
  857. @return -1 on failure
  858. @return 1 on success
  859. """
  860. if not GVL_vol_exists(id):
  861. return -1
  862. if isosurf_id is not None:
  863. num = GVL_isosurf_num_isosurfs(id)
  864. if num < 0 or isosurf_id != num:
  865. return -1
  866. if GVL_isosurf_add(id) < 0:
  867. return -1
  868. # set topography level
  869. nisosurfs = GVL_isosurf_num_isosurfs(id)
  870. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  871. def AddSlice(self, id, slice_id = None):
  872. """!Add new slice
  873. @param id volume id
  874. @param slice_id slice id
  875. @return -1 on failure
  876. @return number of slices
  877. """
  878. if not GVL_vol_exists(id):
  879. return -1
  880. if slice_id is not None:
  881. num = GVL_slice_num_slices(id)
  882. if num < 0 or slice_id != num:
  883. return -1
  884. if GVL_slice_add(id) < 0:
  885. return -1
  886. return GVL_slice_num_slices(id)
  887. def DeleteIsosurface(self, id, isosurf_id):
  888. """!Delete isosurface
  889. @param id volume id
  890. @param isosurf_id isosurface id
  891. @return 1 on success
  892. @return -1 volume not found
  893. @return -2 isosurface not found
  894. @return -3 on failure
  895. """
  896. if not GVL_vol_exists(id):
  897. return -1
  898. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  899. return -2
  900. ret = GVL_isosurf_del(id, isosurf_id)
  901. if ret < 0:
  902. return -3
  903. return 1
  904. def DeleteSlice(self, id, slice_id):
  905. """!Delete slice
  906. @param id volume id
  907. @param slice_id slice id
  908. @return 1 on success
  909. @return -1 volume not found
  910. @return -2 slice not found
  911. @return -3 on failure
  912. """
  913. if not GVL_vol_exists(id):
  914. return -1
  915. if slice_id > GVL_slice_num_slices(id):
  916. return -2
  917. ret = GVL_slice_del(id, slice_id)
  918. if ret < 0:
  919. return -3
  920. return 1
  921. def MoveIsosurface(self, id, isosurf_id, up):
  922. """!Move isosurface up/down in the list
  923. @param id volume id
  924. @param isosurf_id isosurface id
  925. @param up if true move up otherwise down
  926. @return 1 on success
  927. @return -1 volume not found
  928. @return -2 isosurface not found
  929. @return -3 on failure
  930. """
  931. if not GVL_vol_exists(id):
  932. return -1
  933. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  934. return -2
  935. if up:
  936. ret = GVL_isosurf_move_up(id, isosurf_id)
  937. else:
  938. ret = GVL_isosurf_move_down(id, isosurf_id)
  939. if ret < 0:
  940. return -3
  941. return 1
  942. def MoveSlice(self, id, slice_id, up):
  943. """!Move slice up/down in the list
  944. @param id volume id
  945. @param slice_id slice id
  946. @param up if true move up otherwise down
  947. @return 1 on success
  948. @return -1 volume not found
  949. @return -2 slice not found
  950. @return -3 on failure
  951. """
  952. if not GVL_vol_exists(id):
  953. return -1
  954. if slice_id > GVL_slice_num_slices(id):
  955. return -2
  956. if up:
  957. ret = GVL_slice_move_up(id, slice_id)
  958. else:
  959. ret = GVL_slice_move_down(id, slice_id)
  960. if ret < 0:
  961. return -3
  962. return 1
  963. def SetIsosurfaceTopo(self, id, isosurf_id, map, value):
  964. """!Set isosurface level
  965. @param id volume id
  966. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  967. @param map if true use map otherwise constant
  968. @param value map name of value
  969. @return 1 on success
  970. @return -1 volume not found
  971. @return -2 isosurface not found
  972. @return -3 on failure
  973. """
  974. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TOPO, map, value)
  975. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  976. """!Set isosurface color
  977. @param id volume id
  978. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  979. @param map if true use map otherwise constant
  980. @param value map name of value
  981. @return 1 on success
  982. @return -1 volume not found
  983. @return -2 isosurface not found
  984. @return -3 on failure
  985. """
  986. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  987. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  988. """!Set isosurface mask
  989. @todo invert
  990. @param id volume id
  991. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  992. @param invert true for invert mask
  993. @param value map name to be used for mask
  994. @return 1 on success
  995. @return -1 volume not found
  996. @return -2 isosurface not found
  997. @return -3 on failure
  998. """
  999. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, True, value)
  1000. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  1001. """!Set isosurface transparency
  1002. @param id volume id
  1003. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1004. @param map if true use map otherwise constant
  1005. @param value map name of value
  1006. @return 1 on success
  1007. @return -1 volume not found
  1008. @return -2 isosurface not found
  1009. @return -3 on failure
  1010. """
  1011. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  1012. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  1013. """!Set isosurface shininess
  1014. @param id volume id
  1015. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1016. @param map if true use map otherwise constant
  1017. @param value map name of value
  1018. @return 1 on success
  1019. @return -1 volume not found
  1020. @return -2 isosurface not found
  1021. @return -3 on failure
  1022. """
  1023. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  1024. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  1025. """!Set isosurface emission (currently unused)
  1026. @param id volume id
  1027. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1028. @param map if true use map otherwise constant
  1029. @param value map name of value
  1030. @return 1 on success
  1031. @return -1 volume not found
  1032. @return -2 isosurface not found
  1033. @return -3 on failure
  1034. """
  1035. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  1036. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  1037. """!Set isosurface attribute
  1038. @param id volume id
  1039. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1040. @param attr attribute desc
  1041. @param map if true use map otherwise constant
  1042. @param value map name of value
  1043. @return 1 on success
  1044. @return -1 volume not found
  1045. @return -2 isosurface not found
  1046. @return -3 setting attributes failed
  1047. """
  1048. if not GVL_vol_exists(id):
  1049. return -1
  1050. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1051. return -2
  1052. if map:
  1053. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  1054. else:
  1055. if attr == ATT_COLOR:
  1056. val = Nviz_color_from_str(value)
  1057. else:
  1058. val = float(value)
  1059. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  1060. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  1061. "attr=%d, map=%s, value=%s",
  1062. id, isosurf_id, attr, map, value)
  1063. if ret < 0:
  1064. return -2
  1065. return 1
  1066. def UnsetIsosurfaceMask(self, id, isosurf_id):
  1067. """!Unset isosurface mask
  1068. @param id volume id
  1069. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1070. @return 1 on success
  1071. @return -1 volume not found
  1072. @return -2 isosurface not found
  1073. @return -3 setting attributes failed
  1074. """
  1075. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  1076. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  1077. """!Unset isosurface transparency
  1078. @param id volume id
  1079. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1080. @return 1 on success
  1081. @return -1 volume not found
  1082. @return -2 isosurface not found
  1083. @return -3 setting attributes failed
  1084. """
  1085. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  1086. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  1087. """!Unset isosurface emission (currently unused)
  1088. @param id volume id
  1089. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1090. @return 1 on success
  1091. @return -1 volume not found
  1092. @return -2 isosurface not found
  1093. @return -3 setting attributes failed
  1094. """
  1095. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  1096. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  1097. """!Unset surface attribute
  1098. @param id surface id
  1099. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  1100. @param attr attribute descriptor
  1101. @return 1 on success
  1102. @return -1 volume not found
  1103. @return -2 isosurface not found
  1104. @return -2 on failure
  1105. """
  1106. if not GVL_vol_exists(id):
  1107. return -1
  1108. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1109. return -2
  1110. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  1111. id, isosurf_id, attr)
  1112. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  1113. if ret < 0:
  1114. return -2
  1115. return 1
  1116. def SetIsosurfaceMode(self, id, mode):
  1117. """!Set draw mode for isosurfaces
  1118. @param id isosurface id
  1119. @param mode isosurface draw mode
  1120. @return 1 on success
  1121. @return -1 volume set not found
  1122. @return -2 on failure
  1123. """
  1124. if not GVL_vol_exists(id):
  1125. return -1
  1126. ret = GVL_isosurf_set_drawmode(id, mode)
  1127. if ret < 0:
  1128. return -2
  1129. return 1
  1130. def SetSliceMode(self, id, mode):
  1131. """!Set draw mode for slices
  1132. @param id slice id
  1133. @param mode slice draw mode
  1134. @return 1 on success
  1135. @return -1 volume set not found
  1136. @return -2 on failure
  1137. """
  1138. if not GVL_vol_exists(id):
  1139. return -1
  1140. ret = GVL_slice_set_drawmode(id, mode)
  1141. if ret < 0:
  1142. return -2
  1143. return 1
  1144. def SetIsosurfaceRes(self, id, res):
  1145. """!Set draw resolution for isosurfaces
  1146. @param id isosurface id
  1147. @param res resolution value
  1148. @return 1 on success
  1149. @return -1 volume set not found
  1150. @return -2 on failure
  1151. """
  1152. if not GVL_vol_exists(id):
  1153. return -1
  1154. ret = GVL_isosurf_set_drawres(id, res, res, res)
  1155. if ret < 0:
  1156. return -2
  1157. return 1
  1158. def SetSliceRes(self, id, res):
  1159. """!Set draw resolution for slices
  1160. @param id slice id
  1161. @param res resolution value
  1162. @return 1 on success
  1163. @return -1 volume set not found
  1164. @return -2 on failure
  1165. """
  1166. if not GVL_vol_exists(id):
  1167. return -1
  1168. ret = GVL_slice_set_drawres(id, res, res, res)
  1169. if ret < 0:
  1170. return -2
  1171. return 1
  1172. def SetSlicePosition(self, id, slice_id, x1, x2, y1, y2, z1, z2, dir):
  1173. """!Set slice position
  1174. @param id volume id
  1175. @param slice_id slice id
  1176. @param x1,x2,y1,y2,z1,z2 slice coordinates
  1177. @param dir axis
  1178. @return 1 on success
  1179. @return -1 volume not found
  1180. @return -2 slice not found
  1181. @return -3 on failure
  1182. """
  1183. if not GVL_vol_exists(id):
  1184. return -1
  1185. if slice_id > GVL_slice_num_slices(id):
  1186. return -2
  1187. ret = GVL_slice_set_pos(id, slice_id, x1, x2, y1, y2, z1, z2, dir)
  1188. if ret < 0:
  1189. return -2
  1190. return 1
  1191. def SetSliceTransp(self, id, slice_id, value):
  1192. """!Set slice transparency
  1193. @param id volume id
  1194. @param slice_id slice id
  1195. @param value transparency value (0 - 255)
  1196. @return 1 on success
  1197. @return -1 volume not found
  1198. @return -2 slice not found
  1199. @return -3 on failure
  1200. """
  1201. if not GVL_vol_exists(id):
  1202. return -1
  1203. if slice_id > GVL_slice_num_slices(id):
  1204. return -2
  1205. ret = GVL_slice_set_transp(id, slice_id, value)
  1206. if ret < 0:
  1207. return -2
  1208. return 1
  1209. def SetIsosurfaceInOut(self, id, isosurf_id, inout):
  1210. """!Set inout mode
  1211. @param id volume id
  1212. @param isosurf_id isosurface id
  1213. @param inout mode true/false
  1214. @return 1 on success
  1215. @return -1 volume set not found
  1216. @return -2 isosurface not found
  1217. @return -3 on failure
  1218. """
  1219. if not GVL_vol_exists(id):
  1220. return -1
  1221. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  1222. return -2
  1223. ret = GVL_isosurf_set_flags(id, isosurf_id, inout)
  1224. if ret < 0:
  1225. return -3
  1226. return 1
  1227. def GetVolumePosition(self, id):
  1228. """!Get volume position
  1229. @param id volume id
  1230. @return x,y,z
  1231. @return zero-length vector on error
  1232. """
  1233. if not GVL_vol_exists(id):
  1234. return []
  1235. x, y, z = c_float(), c_float(), c_float()
  1236. GVL_get_trans(id, byref(x), byref(y), byref(z))
  1237. Debug.msg(3, "Nviz::GetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1238. id, x.value, y.value, z.value)
  1239. return [x.value, y.value, z.value]
  1240. def SetVolumePosition(self, id, x, y, z):
  1241. """!Set volume position
  1242. @param id volume id
  1243. @param x,y,z translation values
  1244. @return 1 on success
  1245. @return -1 volume not found
  1246. @return -2 setting position failed
  1247. """
  1248. if not GVL_vol_exists(id):
  1249. return -1
  1250. Debug.msg(3, "Nviz::SetVolumePosition(): id=%d, x=%f, y=%f, z=%f",
  1251. id, x, y, z)
  1252. GVL_set_trans(id, x, y, z)
  1253. return 1
  1254. def SetVolumeDrawBox(self, id, ifBox):
  1255. """!Display volume wire box
  1256. @param id volume id
  1257. @param ifBox True to draw wire box, False otherwise
  1258. @return 1 on success
  1259. @return -1 volume not found
  1260. """
  1261. if not GVL_vol_exists(id):
  1262. return -1
  1263. Debug.msg(3, "Nviz::SetVolumeDrawBox(): id=%d, ifBox=%d", id, ifBox)
  1264. GVL_set_draw_wire(id, int(ifBox))
  1265. return 1
  1266. def GetCPlaneCurrent(self):
  1267. return Nviz_get_current_cplane(self.data)
  1268. def GetCPlanesCount(self):
  1269. """!Returns number of cutting planes"""
  1270. return Nviz_num_cplanes(self.data)
  1271. def GetCPlaneRotation(self):
  1272. """!Returns rotation parameters of current cutting plane"""
  1273. x, y, z = c_float(), c_float(), c_float()
  1274. current = Nviz_get_current_cplane(self.data)
  1275. Nviz_get_cplane_rotation(self.data, current, byref(x), byref(y), byref(z))
  1276. return x.value, y.value, z.value
  1277. def GetCPlaneTranslation(self):
  1278. """!Returns translation parameters of current cutting plane"""
  1279. x, y, z = c_float(), c_float(), c_float()
  1280. current = Nviz_get_current_cplane(self.data)
  1281. Nviz_get_cplane_translation(self.data, current, byref(x), byref(y), byref(z))
  1282. return x.value, y.value, z.value
  1283. def SetCPlaneRotation(self, x, y, z):
  1284. """!Set current clip plane rotation
  1285. @param x,y,z rotation parameters
  1286. """
  1287. current = Nviz_get_current_cplane(self.data)
  1288. Nviz_set_cplane_rotation(self.data, current, x, y, z)
  1289. Nviz_draw_cplane(self.data, -1, -1)
  1290. def SetCPlaneTranslation(self, x, y, z):
  1291. """!Set current clip plane translation
  1292. @param x,y,z translation parameters
  1293. """
  1294. current = Nviz_get_current_cplane(self.data)
  1295. Nviz_set_cplane_translation(self.data, current, x, y, z)
  1296. Nviz_draw_cplane(self.data, -1, -1)
  1297. Debug.msg(3, "Nviz::SetCPlaneTranslation(): id=%d, x=%f, y=%f, z=%f",
  1298. current, x, y, z)
  1299. def SetCPlaneInteractively(self, x, y):
  1300. current = Nviz_get_current_cplane(self.data)
  1301. ret = Nviz_set_cplane_here(self.data, current, x, y)
  1302. if ret:
  1303. Nviz_draw_cplane(self.data, -1, -1)
  1304. x, y, z = self.GetCPlaneTranslation()
  1305. return x, y, z
  1306. else:
  1307. return None, None, None
  1308. def SelectCPlane(self, index):
  1309. """!Select cutting plane
  1310. @param index index of cutting plane
  1311. """
  1312. Nviz_on_cplane(self.data, index)
  1313. def UnselectCPlane(self, index):
  1314. """!Unselect cutting plane
  1315. @param index index of cutting plane
  1316. """
  1317. Nviz_off_cplane(self.data, index)
  1318. def SetFenceColor(self, index):
  1319. """!Select current cutting plane
  1320. @param index type of fence - from 0 (off) to 4
  1321. """
  1322. Nviz_set_fence_color(self.data, index)
  1323. def GetXYRange(self):
  1324. """!Get xy range"""
  1325. return Nviz_get_xyrange(self.data)
  1326. def GetZRange(self):
  1327. """!Get z range"""
  1328. min, max = c_float(), c_float()
  1329. Nviz_get_zrange(self.data, byref(min), byref(max))
  1330. return min.value, max.value
  1331. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  1332. """!Save current GL screen to ppm/tif file
  1333. @param filename file name
  1334. @param width image width
  1335. @param height image height
  1336. @param itype image type ('ppm' or 'tif')
  1337. """
  1338. widthOrig = self.width
  1339. heightOrig = self.height
  1340. self.ResizeWindow(width, height)
  1341. GS_clear(Nviz_get_bgcolor(self.data))
  1342. self.Draw(False, -1)
  1343. if itype == 'ppm':
  1344. GS_write_ppm(filename)
  1345. else:
  1346. GS_write_tif(filename)
  1347. self.ResizeWindow(widthOrig, heightOrig)
  1348. def DrawLightingModel(self):
  1349. """!Draw lighting model"""
  1350. if self.showLight:
  1351. Nviz_draw_model(self.data)
  1352. def DrawFringe(self):
  1353. """!Draw fringe"""
  1354. Nviz_draw_fringe(self.data)
  1355. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  1356. """!Set fringe
  1357. @param sid surface id
  1358. @param color color
  1359. @param elev elevation (height)
  1360. @param nw,ne,sw,se fringe edges (turn on/off)
  1361. """
  1362. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  1363. Nviz_set_fringe(self.data,
  1364. sid, Nviz_color_from_str(scolor),
  1365. elev, int(nw), int(ne), int(sw), int(se))
  1366. def DrawArrow(self):
  1367. """!Draw north arrow
  1368. """
  1369. return Nviz_draw_arrow(self.data)
  1370. def SetArrow(self, sx, sy, size, color):
  1371. """!Set north arrow from canvas coordinates
  1372. @param sx,sy canvas coordinates
  1373. @param size arrow length
  1374. @param color arrow color
  1375. """
  1376. return Nviz_set_arrow(self.data, sx, sy, size, Nviz_color_from_str(color))
  1377. def DeleteArrow(self):
  1378. """!Delete north arrow
  1379. """
  1380. Nviz_delete_arrow(self.data)
  1381. def SetScalebar(self, id, sx, sy, size, color):
  1382. """!Set scale bar from canvas coordinates
  1383. @param sx,sy canvas coordinates
  1384. @param id scale bar id
  1385. @param size scale bar length
  1386. @param color scale bar color
  1387. """
  1388. return Nviz_set_scalebar(self.data, id, sx, sy, size, Nviz_color_from_str(color))
  1389. def DrawScalebar(self):
  1390. """!Draw scale bar
  1391. """
  1392. return Nviz_draw_scalebar(self.data)
  1393. def DeleteScalebar(self, id):
  1394. """!Delete scalebar
  1395. """
  1396. Nviz_delete_scalebar(self.data, id)
  1397. def GetPointOnSurface(self, sx, sy):
  1398. """!Get point on surface
  1399. @param sx,sy canvas coordinates (LL)
  1400. """
  1401. sid = c_int()
  1402. x = c_float()
  1403. y = c_float()
  1404. z = c_float()
  1405. Debug.msg(5, "Nviz::GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  1406. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  1407. if num == 0:
  1408. return (None, None, None, None)
  1409. return (sid.value, x.value, y.value, z.value)
  1410. def QueryMap(self, sx, sy):
  1411. """!Query surface map
  1412. @param sx,sy canvas coordinates (LL)
  1413. """
  1414. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  1415. if not sid:
  1416. return None
  1417. catstr = create_string_buffer(256)
  1418. valstr = create_string_buffer(256)
  1419. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  1420. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  1421. return { 'id' : sid,
  1422. 'x' : x,
  1423. 'y' : y,
  1424. 'z' : z,
  1425. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  1426. 'color' : valstr.value }
  1427. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  1428. """!Get distance measured along surface"""
  1429. d = c_float()
  1430. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  1431. byref(d), int(useExag))
  1432. return d.value
  1433. def GetRotationParameters(self, dx, dy):
  1434. """!Get rotation parameters (angle, x, y, z axes)
  1435. @param dx,dy difference from previous mouse drag event
  1436. """
  1437. modelview = (c_double * 16)()
  1438. Nviz_get_modelview(byref(modelview))
  1439. angle = sqrt(dx*dx+dy*dy)/float(self.width+1)*180.0
  1440. m = []
  1441. row = []
  1442. for i, item in enumerate(modelview):
  1443. row.append(item)
  1444. if (i+1) % 4 == 0:
  1445. m.append(row)
  1446. row = []
  1447. inv = matrix(m).I
  1448. ax, ay, az = dy, dx, 0.
  1449. x = inv[0,0]*ax + inv[1,0]*ay + inv[2,0]*az
  1450. y = inv[0,1]*ax + inv[1,1]*ay + inv[2,1]*az
  1451. z = inv[0,2]*ax + inv[1,2]*ay + inv[2,2]*az
  1452. return angle, x, y, z
  1453. def Rotate(self, angle, x, y, z):
  1454. """!Set rotation parameters
  1455. Rotate scene (difference from current state).
  1456. @param angle angle
  1457. @param x,y,z axis coordinate
  1458. """
  1459. Nviz_set_rotation(angle, x, y, z)
  1460. def UnsetRotation(self):
  1461. """!Stop rotating the scene"""
  1462. Nviz_unset_rotation()
  1463. def ResetRotation(self):
  1464. """!Reset scene rotation"""
  1465. Nviz_init_rotation()
  1466. def GetRotationMatrix(self):
  1467. """!Get rotation matrix"""
  1468. matrix = (c_double * 16)()
  1469. GS_get_rotation_matrix(byref(matrix))
  1470. returnMatrix = []
  1471. for item in matrix:
  1472. returnMatrix.append(item)
  1473. return returnMatrix
  1474. def SetRotationMatrix(self, matrix):
  1475. """!Set rotation matrix"""
  1476. mtrx = (c_double * 16)()
  1477. for i in range(len(matrix)):
  1478. mtrx[i] = matrix[i]
  1479. GS_set_rotation_matrix(byref(mtrx))
  1480. def Start2D(self):
  1481. Nviz_set_2D(self.width, self.height)
  1482. def FlyThrough(self, flyInfo, mode, exagInfo):
  1483. """!Fly through the scene
  1484. @param flyInfo fly parameters
  1485. @param mode 0 or 1 for different fly behaviour
  1486. @param exagInfo parameters changing fly speed
  1487. """
  1488. fly = (c_float * 3)()
  1489. for i, item in enumerate(flyInfo):
  1490. fly[i] = item
  1491. exag = (c_int * 2)()
  1492. exag[0] = int(exagInfo['move'])
  1493. exag[1] = int(exagInfo['turn'])
  1494. Nviz_flythrough(self.data, fly, exag, mode)
  1495. class Texture(object):
  1496. """!Class representing OpenGL texture"""
  1497. def __init__(self, filepath, overlayId, coords):
  1498. """!Load image to texture
  1499. @param filepath path to image file
  1500. @param overlayId id of overlay (1 for legend, 101 and more for text)
  1501. @param coords image coordinates
  1502. """
  1503. self.path = filepath
  1504. self.image = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
  1505. self.width = self.image.GetWidth()
  1506. self.height = self.image.GetHeight()
  1507. self.id = overlayId
  1508. self.coords = list(coords)
  1509. self.bounds = wx.Rect()
  1510. self.active = True
  1511. # alpha needs to be initialized
  1512. if not self.image.HasAlpha():
  1513. self.image.InitAlpha()
  1514. # resize image to match 2^n
  1515. self.Resize()
  1516. # check max texture size
  1517. maxSize = c_int()
  1518. Nviz_get_max_texture(byref(maxSize))
  1519. self.maxSize = maxSize.value
  1520. if self.maxSize < self.width or self.maxSize < self.height:
  1521. # TODO: split up image
  1522. self.textureId = None
  1523. else:
  1524. self.textureId = self.Load()
  1525. def __del__(self):
  1526. """!Delete texture"""
  1527. if self.textureId:
  1528. Nviz_del_texture(self.textureId)
  1529. grass.try_remove(self.path)
  1530. def Resize(self):
  1531. """!Resize image to match 2^n"""
  1532. n = m = 1
  1533. while self.width > pow(2,n):
  1534. n += 1
  1535. while self.height > pow(2,m):
  1536. m += 1
  1537. self.image.Resize(size = (pow(2,n), pow(2,m)), pos = (0, 0))
  1538. self.width = self.image.GetWidth()
  1539. self.height = self.image.GetHeight()
  1540. def Load(self):
  1541. """!Load image to texture"""
  1542. if self.image.HasAlpha():
  1543. bytesPerPixel = 4
  1544. else:
  1545. bytesPerPixel = 3
  1546. bytes = bytesPerPixel * self.width * self.height
  1547. rev_val = self.height - 1
  1548. im = (c_ubyte * bytes)()
  1549. bytes3 = 3 * self.width * self.height
  1550. bytes1 = self.width * self.height
  1551. imageData = struct.unpack(str(bytes3) + 'B', self.image.GetData())
  1552. if self.image.HasAlpha():
  1553. alphaData = struct.unpack(str(bytes1) + 'B', self.image.GetAlphaData())
  1554. # this takes too much time
  1555. wx.BeginBusyCursor()
  1556. for i in range(self.height):
  1557. for j in range(self.width):
  1558. im[(j + i * self.width) * bytesPerPixel + 0] = imageData[( j + (rev_val - i) * self.width) * 3 + 0]
  1559. im[(j + i * self.width) * bytesPerPixel + 1] = imageData[( j + (rev_val - i) * self.width) * 3 + 1]
  1560. im[(j + i * self.width) * bytesPerPixel + 2] = imageData[( j + (rev_val - i) * self.width) * 3 + 2]
  1561. if self.image.HasAlpha():
  1562. im[(j + i * self.width) * bytesPerPixel + 3] = alphaData[( j + (rev_val - i) * self.width)]
  1563. wx.EndBusyCursor()
  1564. id = Nviz_load_image(im, self.width, self.height, self.image.HasAlpha())
  1565. return id
  1566. def Draw(self):
  1567. """!Draw texture as an image"""
  1568. Nviz_draw_image(self.coords[0], self.coords[1], self.width, self.height, self.textureId)
  1569. def SetBounds(self, rect):
  1570. """!Set Bounding Rectangle"""
  1571. self.bounds = rect
  1572. def HitTest(self, x, y, radius):
  1573. copy = wx.Rect(*self.bounds)
  1574. copy.Inflate(radius, radius)
  1575. return copy.ContainsXY(x, y)
  1576. def MoveTexture(self, dx, dy):
  1577. """!Move texture on the screen"""
  1578. self.coords[0] += dx
  1579. self.coords[1] += dy
  1580. self.bounds.OffsetXY(dx, dy)
  1581. def SetCoords(self, coords):
  1582. """!Set coordinates"""
  1583. dx = coords[0] - self.coords[0]
  1584. dy = coords[1] - self.coords[1]
  1585. self.MoveTexture(dx, dy)
  1586. def GetId(self):
  1587. """!Returns image id."""
  1588. return self.id
  1589. def SetActive(self, active = True):
  1590. self.active = active
  1591. def IsActive(self):
  1592. return self.active
  1593. class ImageTexture(Texture):
  1594. """!Class representing OpenGL texture as an overlay image"""
  1595. def __init__(self, filepath, overlayId, coords, cmd):
  1596. """!Load image to texture
  1597. @param filepath path to image file
  1598. @param overlayId id of overlay (1 for legend)
  1599. @param coords image coordinates
  1600. @param cmd d.legend command
  1601. """
  1602. Texture.__init__(self, filepath = filepath, overlayId = overlayId, coords = coords)
  1603. self.cmd = cmd
  1604. def GetCmd(self):
  1605. """!Returns overlay command."""
  1606. return self.cmd
  1607. def Corresponds(self, item):
  1608. return sorted(self.GetCmd()) == sorted(item.GetCmd())
  1609. class TextTexture(Texture):
  1610. """!Class representing OpenGL texture as a text label"""
  1611. def __init__(self, filepath, overlayId, coords, textDict):
  1612. """!Load image to texture
  1613. @param filepath path to image file
  1614. @param overlayId id of overlay (101 and more for text)
  1615. @param coords text coordinates
  1616. @param textDict text properties
  1617. """
  1618. Texture.__init__(self, filepath = filepath, overlayId = overlayId, coords = coords)
  1619. self.textDict = textDict
  1620. def GetTextDict(self):
  1621. """!Returns text properties."""
  1622. return self.textDict
  1623. def Corresponds(self, item):
  1624. t = self.GetTextDict()
  1625. for prop in t.keys():
  1626. if prop in ('coords','bbox'): continue
  1627. if t[prop] != item[prop]:
  1628. return False
  1629. return True