wxnviz.py 62 KB

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