wxnviz.py 51 KB

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