wxnviz.py 62 KB

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