wxnviz.py 35 KB

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