wxnviz.py 33 KB

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