wxnviz.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. """!
  2. @package wxnviz.py
  3. @brief wxGUI 3D view mode
  4. This module implements 3D visualization mode for map display.
  5. List of classes:
  6. - Nviz
  7. (C) 2008-2010 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  12. @author Pythonized by Glynn Clements
  13. """
  14. import sys
  15. from threading import Thread
  16. from ctypes import *
  17. try:
  18. from grass.lib.grass import *
  19. from grass.lib.ogsf import *
  20. from grass.lib.nviz import *
  21. except ImportError, e:
  22. print >> sys.stderr, "\nWARNING: Nviz extension (3D view mode) disabled. Reason: %s" % e
  23. sys.stderr.flush()
  24. from debug import Debug
  25. class Nviz(object):
  26. def __init__(self, log):
  27. """!Initialize Nviz class instance
  28. @param log logging area
  29. """
  30. self.log = log
  31. G_gisinit("")
  32. # G_set_error_routine(&print_error)
  33. # G_set_percent_routine(poiter(print_percent))
  34. GS_libinit()
  35. GVL_libinit()
  36. self.data_obj = nv_data()
  37. self.data = pointer(self.data_obj)
  38. self.width = self.height = -1
  39. self.showLight = False
  40. Debug.msg(1, "Nviz::Nviz()")
  41. def __del__(self):
  42. """!Destroy Nviz class instance"""
  43. # G_unset_error_routine()
  44. # G_unset_percent_routine()
  45. del self.data
  46. del self.data_obj
  47. self.log = None
  48. def ResizeWindow(self, width, height):
  49. """!GL canvas resized
  50. @param width window width
  51. @param height window height
  52. @return 1 on success
  53. @return 0 on failure (window resized by default to 20x20 px)
  54. """
  55. self.width = width
  56. self.height = height
  57. Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
  58. width, height)
  59. return Nviz_resize_window(width, height)
  60. def SetViewDefault(self):
  61. """!Set default view (based on loaded data)
  62. @return z-exag value, default, min and max height
  63. """
  64. # determine z-exag
  65. z_exag = Nviz_get_exag()
  66. Nviz_change_exag(self.data, z_exag)
  67. # determine height
  68. hdef = c_float()
  69. hmin = c_float()
  70. hmax = c_float()
  71. Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
  72. Debug.msg(3, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
  73. hdef.value, hmin.value, hmax.value)
  74. return (z_exag, hdef.value, hmin.value, hmax.value)
  75. def SetView(self, x, y, height, persp, twist):
  76. """!Change view settings
  77. @param x,y position
  78. @param height
  79. @param persp perpective
  80. @param twist
  81. """
  82. Nviz_set_viewpoint_height(self.data, height)
  83. Nviz_set_viewpoint_position(self.data, x, y)
  84. Nviz_set_viewpoint_twist(self.data, twist)
  85. Nviz_set_viewpoint_persp(self.data, persp)
  86. Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
  87. x, y, height, persp, twist)
  88. def SetZExag(self, z_exag):
  89. """!Set z-exag value
  90. @param z_exag value
  91. @return 1
  92. """
  93. Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
  94. return Nviz_change_exag(self.data, z_exag)
  95. def Draw(self, quick, quick_mode):
  96. """!Draw canvas
  97. Draw quick mode:
  98. - DRAW_QUICK_SURFACE
  99. - DRAW_QUICK_VLINES
  100. - DRAW_QUICK_VPOINTS
  101. - DRAW_QUICK_VOLUME
  102. @param quick if true draw in wiremode
  103. @param quick_mode quick mode
  104. """
  105. Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
  106. Nviz_draw_cplane(self.data, -1, -1) # ?
  107. if quick:
  108. Nviz_draw_quick(self.data, quick_mode)
  109. else:
  110. Nviz_draw_all(self.data)
  111. def EraseMap(self):
  112. """!Erase map display (with background color)
  113. """
  114. GS_clear(self.data.bgcolor)
  115. Debug.msg(1, "Nviz::EraseMap()")
  116. def InitView(self):
  117. """!Initialize view"""
  118. # initialize nviz data
  119. Nviz_init_data(self.data)
  120. # define default attributes for map objects
  121. Nviz_set_surface_attr_default()
  122. # set background color
  123. Nviz_set_bgcolor(self.data, Nviz_color_from_str("white")) # TODO
  124. self.SetBgColor("white")
  125. # initialize view, lights
  126. Nviz_init_view(self.data)
  127. # clear window
  128. GS_clear(self.data.bgcolor)
  129. Debug.msg(1, "Nviz::InitView()")
  130. def SetBgColor(self, color_str):
  131. """!Set background color
  132. @param color_str color string
  133. """
  134. self.data.bgcolor = Nviz_color_from_str(color_str)
  135. def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
  136. """!Change lighting settings
  137. @param x,y,z position
  138. @param color light color (as string)
  139. @param bright light brightness
  140. @param ambient light ambient
  141. @param w local coordinate (default to 0)
  142. """
  143. Nviz_set_light_position(self.data, lid, x, y, z, w)
  144. Nviz_set_light_color(self.data, lid, color[0] / 255., color[1] / 255., color[2] / 255.)
  145. Nviz_set_light_bright(self.data, lid, bright)
  146. Nviz_set_light_ambient(self.data, lid, ambient, ambient, ambient)
  147. def LoadSurface(self, name, color_name, color_value):
  148. """!Load raster map (surface)
  149. @param name raster map name
  150. @param color_name raster map for color (None for color_value)
  151. @param color_value color string (named color or RGB triptet)
  152. @return object id
  153. @return -1 on failure
  154. """
  155. mapset = G_find_raster2(name, "")
  156. if mapset is None:
  157. G_warning(_("Raster map <%s> not found"), name)
  158. return -1
  159. # topography
  160. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  161. G_fully_qualified_name(name, mapset), 0.0,
  162. self.data)
  163. if color_name: # check for color map
  164. mapset = G_find_raster2(color_name, "")
  165. if mapset is None:
  166. G_warning(_("Raster map <%s> not found"), color_name)
  167. GS_delete_surface(id)
  168. return -1
  169. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  170. G_fully_qualified_name(color_name, mapset), -1.0,
  171. self.data)
  172. elif color_value: # check for color value
  173. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  174. None, Nviz_color_from_str(color_value),
  175. self.data)
  176. else: # use by default elevation map for coloring
  177. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  178. G_fully_qualified_name(name, mapset), -1.0,
  179. self.data)
  180. # if (i > 1)
  181. # set_default_wirecolors(self.data, i)
  182. # focus on loaded self.data
  183. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
  184. Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
  185. return id
  186. def UnloadSurface(self, id):
  187. """!Unload surface
  188. @param id surface id
  189. @return 1 on success
  190. @return 0 on failure
  191. """
  192. if not GS_surf_exists(id):
  193. return 0
  194. Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
  195. if GS_delete_surface(id) < 0:
  196. return 0
  197. return 1
  198. def LoadVector(self, name, points):
  199. """!Load vector map overlay
  200. @param name vector map name
  201. @param points if true load 2d points rather then 2d lines
  202. @return object id
  203. @return -1 on failure
  204. """
  205. if GS_num_surfs() == 0: # load base surface if no loaded
  206. Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
  207. nsurf = c_int()
  208. surf_list = GS_get_surf_list(byref(nsurf))
  209. GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
  210. mapset = G_find_vector2 (name, "")
  211. if mapset is None:
  212. G_warning(_("Vector map <%s> not found"),
  213. name)
  214. if points:
  215. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  216. G_fully_qualified_name(name, mapset), 0.0,
  217. self.data)
  218. else:
  219. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  220. G_fully_qualified_name(name, mapset), 0.0,
  221. self.data)
  222. Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
  223. return id
  224. def UnloadVector(self, id, points):
  225. """!Unload vector set
  226. @param id vector set id
  227. @param points vector points or lines set
  228. @return 1 on success
  229. @return 0 on failure
  230. """
  231. Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
  232. if points:
  233. if not GP_site_exists(id):
  234. return 0
  235. if GP_delete_site(id) < 0:
  236. return 0
  237. else:
  238. if not GV_vect_exists(id):
  239. return 0
  240. if GV_delete_vector(id) < 0:
  241. return 0
  242. return 1
  243. def LoadVolume(self, name, color_name, color_value):
  244. """!Load 3d raster map (volume)
  245. @param name 3d raster map name
  246. @param color_name 3d raster map for color (None for color_value)
  247. @param color_value color string (named color or RGB triptet)
  248. @return object id
  249. @return -1 on failure
  250. """
  251. mapset = G_find_grid3(name, "")
  252. if mapset is None:
  253. G_warning(_("3d raster map <%s> not found"),
  254. name)
  255. return -1
  256. # topography
  257. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  258. G_fully_qualified_name(name, mapset), 0.0,
  259. self.data)
  260. if color_name: # check for color map
  261. mapset = G_find_grid3(color_name, "")
  262. if mapset is None:
  263. G_warning(_("3d raster map <%s> not found"),
  264. color_name)
  265. GVL_delete_vol(id)
  266. return -1
  267. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  268. G_fully_qualified_name(color_name, mapset), -1.0,
  269. self.data)
  270. elif color_value: # check for color value
  271. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  272. None, Nviz_color_from_str(color_value),
  273. self.data)
  274. else: # use by default elevation map for coloring
  275. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  276. G_fully_qualified_name(name, mapset), -1.0,
  277. self.data)
  278. Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
  279. return id
  280. def UnloadVolume(self, id):
  281. """!Unload volume
  282. @param id volume id
  283. @return 1 on success
  284. @return 0 on failure
  285. """
  286. if not GVL_vol_exists(id):
  287. return 0
  288. Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
  289. if GVL_delete_vol(id) < 0:
  290. return 0
  291. return 1
  292. def SetSurfaceTopo(self, id, map, value):
  293. """!Set surface topography
  294. @param id surface id
  295. @param map if true use map otherwise constant
  296. @param value map name of value
  297. @return 1 on success
  298. @return -1 surface not found
  299. @return -2 setting attributes failed
  300. """
  301. return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
  302. def SetSurfaceColor(self, id, map, value):
  303. """!Set surface color
  304. @param id surface id
  305. @param map if true use map otherwise constant
  306. @param value map name of value
  307. @return 1 on success
  308. @return -1 surface not found
  309. @return -2 setting attributes failed
  310. """
  311. return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
  312. def SetSurfaceMask(self, id, invert, value):
  313. """!Set surface mask
  314. @todo invert
  315. @param id surface id
  316. @param invert if true invert mask
  317. @param value map name of value
  318. @return 1 on success
  319. @return -1 surface not found
  320. @return -2 setting attributes failed
  321. """
  322. return self.SetSurfaceAttr(id, ATT_MASK, true, value)
  323. def SetSurfaceTransp(self, id, map, value):
  324. """!Set surface mask
  325. @todo invert
  326. @param id surface id
  327. @param map if true use map otherwise constant
  328. @param value map name of value
  329. @return 1 on success
  330. @return -1 surface not found
  331. @return -2 setting attributes failed
  332. """
  333. return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
  334. def SetSurfaceShine(self, id, map, value):
  335. """!Set surface shininess
  336. @param id surface id
  337. @param map if true use map otherwise constant
  338. @param value map name of value
  339. @return 1 on success
  340. @return -1 surface not found
  341. @return -2 setting attributes failed
  342. """
  343. return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
  344. def SetSurfaceEmit(self, id, map, value):
  345. """!Set surface emission
  346. @param id surface id
  347. @param map if true use map otherwise constant
  348. @param value map name of value
  349. @return 1 on success
  350. @return -1 surface not found
  351. @return -2 setting attributes failed
  352. """
  353. return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
  354. def SetSurfaceAttr(self, id, attr, map, value):
  355. """!Set surface attribute
  356. @param id surface id
  357. @param attr attribute desc
  358. @param map if true use map otherwise constant
  359. @param value map name of value
  360. @return 1 on success
  361. @return -1 surface not found
  362. @return -2 setting attributes failed
  363. """
  364. if not GS_surf_exists(id):
  365. return -1
  366. if map:
  367. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  368. value, -1.0, self.data)
  369. else:
  370. if attr == ATT_COLOR:
  371. val = Nviz_color_from_str(value)
  372. else:
  373. val = float(value)
  374. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  375. None, val, self.data)
  376. Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  377. id, attr, map, value)
  378. return 1 if ret else -2
  379. def UnsetSurfaceMask(self, id):
  380. """!Unset surface mask
  381. @param id surface id
  382. @return 1 on success
  383. @return -1 surface not found
  384. @return -2 setting attributes failed
  385. @return -1 on failure
  386. """
  387. return self.UnsetSurfaceAttr(id, ATT_MASK)
  388. def UnsetSurfaceTransp(self, id):
  389. """!Unset surface transparency
  390. @param id surface id
  391. @return 1 on success
  392. @return -1 surface not found
  393. @return -2 setting attributes failed
  394. """
  395. return self.UnsetSurfaceAttr(id, ATT_TRANSP)
  396. def UnsetSurfaceEmit(self, id):
  397. """!Unset surface emission
  398. @param id surface id
  399. @return 1 on success
  400. @return -1 surface not found
  401. @return -2 setting attributes failed
  402. """
  403. return self.UnsetSurfaceAttr(id, ATT_EMIT)
  404. def UnsetSurfaceAttr(self, id, attr):
  405. """!Unset surface attribute
  406. @param id surface id
  407. @param attr attribute descriptor
  408. @return 1 on success
  409. @return -1 surface not found
  410. @return -2 setting attributes failed
  411. """
  412. if not GS_surf_exists(id):
  413. return -1
  414. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  415. id, attr)
  416. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
  417. return 1 if ret else -2
  418. def SetSurfaceRes(self, id, fine, coarse):
  419. """!Set surface resolution
  420. @param id surface id
  421. @param fine x/y fine resolution
  422. @param coarse x/y coarse resolution
  423. @return 1 on success
  424. @return -1 surface not found
  425. @return -2 setting attributes failed
  426. """
  427. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  428. id, fine, coarse)
  429. if id > 0:
  430. if not GS_surf_exists(id):
  431. return -1
  432. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  433. return -2
  434. else:
  435. GS_setall_drawres(fine, fine, coarse, coarse)
  436. return 1
  437. def SetSurfaceStyle(self, id, style):
  438. """!Set draw style
  439. Draw styles:
  440. - DM_GOURAUD
  441. - DM_FLAT
  442. - DM_FRINGE
  443. - DM_WIRE
  444. - DM_COL_WIRE
  445. - DM_POLY
  446. - DM_WIRE_POLY
  447. - DM_GRID_WIRE
  448. - DM_GRID_SURF
  449. @param id surface id (<= 0 for all)
  450. @param style draw style
  451. @return 1 on success
  452. @return -1 surface not found
  453. @return -2 setting attributes failed
  454. """
  455. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  456. id, style)
  457. if id > 0:
  458. if not GS_surf_exists(id):
  459. return -1
  460. if GS_set_drawmode(id, style) < 0:
  461. return -2
  462. return 1
  463. if GS_setall_drawmode(style) < 0:
  464. return -2
  465. return 1
  466. def SetWireColor(self, id, color_str):
  467. """!Set color of wire
  468. @todo all
  469. @param surface id (< 0 for all)
  470. @param color color string (R:G:B)
  471. @return 1 on success
  472. @return -1 surface not found
  473. @return -2 setting attributes failed
  474. @return 1 on success
  475. @return 0 on failure
  476. """
  477. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  478. id, color_str)
  479. color = Nviz_color_from_str(color_str)
  480. if id > 0:
  481. if not GS_surf_exists(id):
  482. return -1
  483. GS_set_wire_color(id, color)
  484. else:
  485. nsurfs = c_int()
  486. surf_list = GS_get_surf_list(byref(nsurfs))
  487. for i in xrange(nsurfs.value):
  488. id = surf_list[i]
  489. GS_set_wire_color(id, color)
  490. G_free(surf_list)
  491. surf_list = None
  492. return 1
  493. def GetSurfacePosition(self, id):
  494. """!Get surface position
  495. @param id surface id
  496. @return x,y,z
  497. @return zero-length vector on error
  498. """
  499. if not GS_surf_exists(id):
  500. return []
  501. x, y, z = c_float(), c_float(), c_float()
  502. GS_get_trans(id, byref(x), byref(y), byref(z))
  503. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  504. id, x, y, z)
  505. return [x.value, y.value, z.value]
  506. def SetSurfacePosition(self, id, x, y, z):
  507. """!Set surface position
  508. @param id surface id
  509. @param x,y,z translation values
  510. @return 1 on success
  511. @return -1 surface not found
  512. @return -2 setting position failed
  513. """
  514. if not GS_surf_exists(id):
  515. return -1
  516. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  517. id, x, y, z)
  518. GS_set_trans(id, x, y, z)
  519. return 1
  520. def SetVectorLineMode(self, id, color_str, width, flat):
  521. """!Set mode of vector line overlay
  522. @param id vector id
  523. @param color_str color string
  524. @param width line width
  525. @param flat display flat or on surface
  526. @return -1 vector set not found
  527. @return -2 on failure
  528. @return 1 on success
  529. """
  530. if not GV_vect_exists(id):
  531. return -1
  532. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  533. id, color_str, width, flat)
  534. color = Nviz_color_from_str(color_str)
  535. # use memory by default
  536. if GV_set_vectmode(id, 1, color, width, flat) < 0:
  537. return -2
  538. return 1
  539. def SetVectorLineHeight(self, id, height):
  540. """!Set vector height above surface (lines)
  541. @param id vector set id
  542. @param height
  543. @return -1 vector set not found
  544. @return 1 on success
  545. """
  546. if not GV_vect_exists(id):
  547. return -1
  548. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  549. id, height)
  550. GV_set_trans(id, 0.0, 0.0, height)
  551. return 1
  552. def SetVectorLineSurface(self, id, surf_id):
  553. """!Set reference surface of vector set (lines)
  554. @param id vector set id
  555. @param surf_id surface id
  556. @return 1 on success
  557. @return -1 vector set not found
  558. @return -2 surface not found
  559. @return -3 on failure
  560. """
  561. if not GV_vect_exists(id):
  562. return -1
  563. if not GS_surf_exists(surf_id):
  564. return -2
  565. if GV_select_surf(id, surf_id) < 0:
  566. return -3
  567. return 1
  568. def SetVectorPointMode(self, id, color_str, width, size, marker):
  569. """!Set mode of vector point overlay
  570. @param id vector id
  571. @param color_str color string
  572. @param width line width
  573. @param flat
  574. @return -1 vector set not found
  575. """
  576. if not GP_site_exists(id):
  577. return -1
  578. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  579. "width=%d, size=%f, marker=%d",
  580. id, color_str, width, size, marker)
  581. color = Nviz_color_from_str(color_str)
  582. if GP_set_style(id, color, width, size, marker) < 0:
  583. return -2
  584. return 1
  585. def SetVectorPointHeight(self, id, height):
  586. """!Set vector height above surface (points)
  587. @param id vector set id
  588. @param height
  589. @return -1 vector set not found
  590. @return 1 on success
  591. """
  592. if not GP_site_exists(id):
  593. return -1
  594. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  595. id, height)
  596. GP_set_trans(id, 0.0, 0.0, height)
  597. return 1
  598. def SetVectorPointSurface(self, id, surf_id):
  599. """!Set reference surface of vector set (points)
  600. @param id vector set id
  601. @param surf_id surface id
  602. @return 1 on success
  603. @return -1 vector set not found
  604. @return -2 surface not found
  605. @return -3 on failure
  606. """
  607. if not GP_site_exists(id):
  608. return -1
  609. if not GS_surf_exists(surf_id):
  610. return -2
  611. if GP_select_surf(id, surf_id) < 0:
  612. return -3
  613. return 1
  614. def AddIsosurface(self, id, level):
  615. """!Add new isosurface
  616. @param id volume id
  617. @param level isosurface level (topography)
  618. @return -1 on failure
  619. @return 1 on success
  620. """
  621. if not GVL_vol_exists(id):
  622. return -1
  623. if GVL_isosurf_add(id) < 0:
  624. return -1
  625. # set topography level
  626. nisosurfs = GVL_isosurf_num_isosurfs(id)
  627. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  628. def DeleteIsosurface(self, id, isosurf_id):
  629. """!Delete isosurface
  630. @param id volume id
  631. @param isosurf_id isosurface id
  632. @return 1 on success
  633. @return -1 volume not found
  634. @return -2 isosurface not found
  635. @return -3 on failure
  636. """
  637. if not GVL_vol_exists(id):
  638. return -1
  639. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  640. return -2
  641. ret = GVL_isosurf_del(id, isosurf_id)
  642. return -3 if ret < 0 else 1
  643. def MoveIsosurface(self, id, isosurf_id, up):
  644. """!Move isosurface up/down in the list
  645. @param id volume id
  646. @param isosurf_id isosurface id
  647. @param up if true move up otherwise down
  648. @return 1 on success
  649. @return -1 volume not found
  650. @return -2 isosurface not found
  651. @return -3 on failure
  652. """
  653. if not GVL_vol_exists(id):
  654. return -1
  655. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  656. return -2
  657. if up:
  658. ret = GVL_isosurf_move_up(id, isosurf_id)
  659. else:
  660. ret = GVL_isosurf_move_down(id, isosurf_id)
  661. return -3 if ret < 0 else 1
  662. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  663. """!Set isosurface color
  664. @param id volume id
  665. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  666. @param map if true use map otherwise constant
  667. @param value map name of value
  668. @return 1 on success
  669. @return -1 volume not found
  670. @return -2 isosurface not found
  671. @return -3 on failure
  672. """
  673. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  674. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  675. """!Set isosurface mask
  676. @todo invert
  677. @param id volume id
  678. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  679. @param invert true for invert mask
  680. @param value map name to be used for mask
  681. @return 1 on success
  682. @return -1 volume not found
  683. @return -2 isosurface not found
  684. @return -3 on failure
  685. """
  686. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, true, value)
  687. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  688. """!Set isosurface transparency
  689. @param id volume id
  690. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  691. @param map if true use map otherwise constant
  692. @param value map name of value
  693. @return 1 on success
  694. @return -1 volume not found
  695. @return -2 isosurface not found
  696. @return -3 on failure
  697. """
  698. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  699. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  700. """!Set isosurface shininess
  701. @param id volume id
  702. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  703. @param map if true use map otherwise constant
  704. @param value map name of value
  705. @return 1 on success
  706. @return -1 volume not found
  707. @return -2 isosurface not found
  708. @return -3 on failure
  709. """
  710. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  711. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  712. """!Set isosurface emission
  713. @param id volume id
  714. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  715. @param map if true use map otherwise constant
  716. @param value map name of value
  717. @return 1 on success
  718. @return -1 volume not found
  719. @return -2 isosurface not found
  720. @return -3 on failure
  721. """
  722. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  723. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  724. """!Set isosurface attribute
  725. @param id volume id
  726. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  727. @param attr attribute desc
  728. @param map if true use map otherwise constant
  729. @param value map name of value
  730. @return 1 on success
  731. @return -1 volume not found
  732. @return -2 isosurface not found
  733. @return -3 setting attributes failed
  734. """
  735. if not GVL_vol_exists(id):
  736. return -1
  737. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  738. return -2
  739. if map:
  740. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  741. else:
  742. if attr == ATT_COLOR:
  743. val = Nviz_color_from_str(value)
  744. else:
  745. val = float(value)
  746. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  747. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  748. "attr=%d, map=%d, value=%s",
  749. id, isosurf_id, attr, map, value)
  750. return 1 if ret > 0 else -2
  751. def UnsetIsosurfaceMask(self, id, isosurf_id):
  752. """!Unset isosurface mask
  753. @param id volume id
  754. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  755. @return 1 on success
  756. @return -1 volume not found
  757. @return -2 isosurface not found
  758. @return -3 setting attributes failed
  759. """
  760. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
  761. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  762. """!Unset isosurface transparency
  763. @param id volume id
  764. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  765. @return 1 on success
  766. @return -1 volume not found
  767. @return -2 isosurface not found
  768. @return -3 setting attributes failed
  769. """
  770. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
  771. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  772. """!Unset isosurface emission
  773. @param id volume id
  774. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  775. @return 1 on success
  776. @return -1 volume not found
  777. @return -2 isosurface not found
  778. @return -3 setting attributes failed
  779. """
  780. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  781. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  782. """!Unset surface attribute
  783. @param id surface id
  784. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  785. @param attr attribute descriptor
  786. @return 1 on success
  787. @return -1 volume not found
  788. @return -2 isosurface not found
  789. @return -2 on failure
  790. """
  791. if not GVL_vol_exists(id):
  792. return -1
  793. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  794. return -2
  795. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  796. id, isosurf_id, attr)
  797. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  798. return 1 if ret > 0 else -2
  799. def SetIsosurfaceMode(self, id, mode):
  800. """!Set draw mode for isosurfaces
  801. @param mode
  802. @return 1 on success
  803. @return -1 volume set not found
  804. @return -2 on failure
  805. """
  806. if not GVL_vol_exists(id):
  807. return -1
  808. ret = GVL_isosurf_set_drawmode(id, mode)
  809. return -2 if ret < 0 else 1
  810. def SetIsosurfaceRes(self, id, res):
  811. """!Set draw resolution for isosurfaces
  812. @param res resolution value
  813. @return 1 on success
  814. @return -1 volume set not found
  815. @return -2 on failure
  816. """
  817. if not GVL_vol_exists(id):
  818. return -1
  819. ret = GVL_isosurf_set_drawres(id, res, res, res)
  820. return -2 if ret < 0 else 1
  821. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  822. """!Save current GL screen to ppm/tif file
  823. @param filename file name
  824. @param width image width
  825. @param height image height
  826. @param itype image type ('ppm' or 'tif')
  827. """
  828. widthOrig = self.width
  829. heightOrig = self.height
  830. self.ResizeWindow(width, height)
  831. GS_clear(self.data.bgcolor)
  832. self.Draw(False, -1)
  833. if itype == 'ppm':
  834. GS_write_ppm(filename)
  835. else:
  836. GS_write_tif(filename)
  837. self.ResizeWindow(widthOrig, heightOrig)
  838. def DrawLightingModel(self):
  839. """!Draw lighting model"""
  840. if self.showLight:
  841. GS_draw_lighting_model()