wxnviz.py 50 KB

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