wxnviz.py 63 KB

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