wxnviz.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. errorMsg = ''
  22. except ImportError, err:
  23. errorMsg = err
  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. if ret < 0:
  377. return -2
  378. return 1
  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. if ret < 0:
  418. return -2
  419. return 1
  420. def SetSurfaceRes(self, id, fine, coarse):
  421. """!Set surface resolution
  422. @param id surface id
  423. @param fine x/y fine resolution
  424. @param coarse x/y coarse resolution
  425. @return 1 on success
  426. @return -1 surface not found
  427. @return -2 setting attributes failed
  428. """
  429. Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  430. id, fine, coarse)
  431. if id > 0:
  432. if not GS_surf_exists(id):
  433. return -1
  434. if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
  435. return -2
  436. else:
  437. GS_setall_drawres(fine, fine, coarse, coarse)
  438. return 1
  439. def SetSurfaceStyle(self, id, style):
  440. """!Set draw style
  441. Draw styles:
  442. - DM_GOURAUD
  443. - DM_FLAT
  444. - DM_FRINGE
  445. - DM_WIRE
  446. - DM_COL_WIRE
  447. - DM_POLY
  448. - DM_WIRE_POLY
  449. - DM_GRID_WIRE
  450. - DM_GRID_SURF
  451. @param id surface id (<= 0 for all)
  452. @param style draw style
  453. @return 1 on success
  454. @return -1 surface not found
  455. @return -2 setting attributes failed
  456. """
  457. Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  458. id, style)
  459. if id > 0:
  460. if not GS_surf_exists(id):
  461. return -1
  462. if GS_set_drawmode(id, style) < 0:
  463. return -2
  464. return 1
  465. if GS_setall_drawmode(style) < 0:
  466. return -2
  467. return 1
  468. def SetWireColor(self, id, color_str):
  469. """!Set color of wire
  470. @todo all
  471. @param surface id (< 0 for all)
  472. @param color color string (R:G:B)
  473. @return 1 on success
  474. @return -1 surface not found
  475. @return -2 setting attributes failed
  476. @return 1 on success
  477. @return 0 on failure
  478. """
  479. Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
  480. id, color_str)
  481. color = Nviz_color_from_str(color_str)
  482. if id > 0:
  483. if not GS_surf_exists(id):
  484. return -1
  485. GS_set_wire_color(id, color)
  486. else:
  487. nsurfs = c_int()
  488. surf_list = GS_get_surf_list(byref(nsurfs))
  489. for i in xrange(nsurfs.value):
  490. id = surf_list[i]
  491. GS_set_wire_color(id, color)
  492. G_free(surf_list)
  493. surf_list = None
  494. return 1
  495. def GetSurfacePosition(self, id):
  496. """!Get surface position
  497. @param id surface id
  498. @return x,y,z
  499. @return zero-length vector on error
  500. """
  501. if not GS_surf_exists(id):
  502. return []
  503. x, y, z = c_float(), c_float(), c_float()
  504. GS_get_trans(id, byref(x), byref(y), byref(z))
  505. Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  506. id, x.value, y.value, z.value)
  507. return [x.value, y.value, z.value]
  508. def SetSurfacePosition(self, id, x, y, z):
  509. """!Set surface position
  510. @param id surface id
  511. @param x,y,z translation values
  512. @return 1 on success
  513. @return -1 surface not found
  514. @return -2 setting position failed
  515. """
  516. if not GS_surf_exists(id):
  517. return -1
  518. Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  519. id, x, y, z)
  520. GS_set_trans(id, x, y, z)
  521. return 1
  522. def SetVectorLineMode(self, id, color_str, width, flat):
  523. """!Set mode of vector line overlay
  524. @param id vector id
  525. @param color_str color string
  526. @param width line width
  527. @param flat display flat or on surface
  528. @return -1 vector set not found
  529. @return -2 on failure
  530. @return 1 on success
  531. """
  532. if not GV_vect_exists(id):
  533. return -1
  534. Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
  535. id, color_str, width, flat)
  536. color = Nviz_color_from_str(color_str)
  537. # use memory by default
  538. if GV_set_vectmode(id, 1, color, width, flat) < 0:
  539. return -2
  540. return 1
  541. def SetVectorLineHeight(self, id, height):
  542. """!Set vector height above surface (lines)
  543. @param id vector set id
  544. @param height
  545. @return -1 vector set not found
  546. @return 1 on success
  547. """
  548. if not GV_vect_exists(id):
  549. return -1
  550. Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
  551. id, height)
  552. GV_set_trans(id, 0.0, 0.0, height)
  553. return 1
  554. def SetVectorLineSurface(self, id, surf_id):
  555. """!Set reference surface of vector set (lines)
  556. @param id vector set id
  557. @param surf_id surface id
  558. @return 1 on success
  559. @return -1 vector set not found
  560. @return -2 surface not found
  561. @return -3 on failure
  562. """
  563. if not GV_vect_exists(id):
  564. return -1
  565. if not GS_surf_exists(surf_id):
  566. return -2
  567. if GV_select_surf(id, surf_id) < 0:
  568. return -3
  569. return 1
  570. def SetVectorPointMode(self, id, color_str, width, size, marker):
  571. """!Set mode of vector point overlay
  572. @param id vector id
  573. @param color_str color string
  574. @param width line width
  575. @param flat
  576. @return -1 vector set not found
  577. """
  578. if not GP_site_exists(id):
  579. return -1
  580. Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
  581. "width=%d, size=%f, marker=%d",
  582. id, color_str, width, size, marker)
  583. color = Nviz_color_from_str(color_str)
  584. if GP_set_style(id, color, width, size, marker) < 0:
  585. return -2
  586. return 1
  587. def SetVectorPointHeight(self, id, height):
  588. """!Set vector height above surface (points)
  589. @param id vector set id
  590. @param height
  591. @return -1 vector set not found
  592. @return 1 on success
  593. """
  594. if not GP_site_exists(id):
  595. return -1
  596. Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
  597. id, height)
  598. GP_set_trans(id, 0.0, 0.0, height)
  599. return 1
  600. def SetVectorPointSurface(self, id, surf_id):
  601. """!Set reference surface of vector set (points)
  602. @param id vector set id
  603. @param surf_id surface id
  604. @return 1 on success
  605. @return -1 vector set not found
  606. @return -2 surface not found
  607. @return -3 on failure
  608. """
  609. if not GP_site_exists(id):
  610. return -1
  611. if not GS_surf_exists(surf_id):
  612. return -2
  613. if GP_select_surf(id, surf_id) < 0:
  614. return -3
  615. return 1
  616. def AddIsosurface(self, id, level):
  617. """!Add new isosurface
  618. @param id volume id
  619. @param level isosurface level (topography)
  620. @return -1 on failure
  621. @return 1 on success
  622. """
  623. if not GVL_vol_exists(id):
  624. return -1
  625. if GVL_isosurf_add(id) < 0:
  626. return -1
  627. # set topography level
  628. nisosurfs = GVL_isosurf_num_isosurfs(id)
  629. return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
  630. def DeleteIsosurface(self, id, isosurf_id):
  631. """!Delete isosurface
  632. @param id volume id
  633. @param isosurf_id isosurface id
  634. @return 1 on success
  635. @return -1 volume not found
  636. @return -2 isosurface not found
  637. @return -3 on failure
  638. """
  639. if not GVL_vol_exists(id):
  640. return -1
  641. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  642. return -2
  643. ret = GVL_isosurf_del(id, isosurf_id)
  644. if ret < 0:
  645. return -3
  646. return 1
  647. def MoveIsosurface(self, id, isosurf_id, up):
  648. """!Move isosurface up/down in the list
  649. @param id volume id
  650. @param isosurf_id isosurface id
  651. @param up if true move up otherwise down
  652. @return 1 on success
  653. @return -1 volume not found
  654. @return -2 isosurface not found
  655. @return -3 on failure
  656. """
  657. if not GVL_vol_exists(id):
  658. return -1
  659. if isosurf_id > GVL_isosurf_num_isosurfs(id):
  660. return -2
  661. if up:
  662. ret = GVL_isosurf_move_up(id, isosurf_id)
  663. else:
  664. ret = GVL_isosurf_move_down(id, isosurf_id)
  665. if ret < 0:
  666. return -3
  667. return 1
  668. def SetIsosurfaceColor(self, id, isosurf_id, map, value):
  669. """!Set isosurface color
  670. @param id volume id
  671. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  672. @param map if true use map otherwise constant
  673. @param value map name of value
  674. @return 1 on success
  675. @return -1 volume not found
  676. @return -2 isosurface not found
  677. @return -3 on failure
  678. """
  679. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
  680. def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
  681. """!Set isosurface mask
  682. @todo invert
  683. @param id volume id
  684. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  685. @param invert true for invert mask
  686. @param value map name to be used for mask
  687. @return 1 on success
  688. @return -1 volume not found
  689. @return -2 isosurface not found
  690. @return -3 on failure
  691. """
  692. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, true, value)
  693. def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
  694. """!Set isosurface transparency
  695. @param id volume id
  696. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  697. @param map if true use map otherwise constant
  698. @param value map name of value
  699. @return 1 on success
  700. @return -1 volume not found
  701. @return -2 isosurface not found
  702. @return -3 on failure
  703. """
  704. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
  705. def SetIsosurfaceShine(self, id, isosurf_id, map, value):
  706. """!Set isosurface shininess
  707. @param id volume id
  708. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  709. @param map if true use map otherwise constant
  710. @param value map name of value
  711. @return 1 on success
  712. @return -1 volume not found
  713. @return -2 isosurface not found
  714. @return -3 on failure
  715. """
  716. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
  717. def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
  718. """!Set isosurface emission
  719. @param id volume id
  720. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  721. @param map if true use map otherwise constant
  722. @param value map name of value
  723. @return 1 on success
  724. @return -1 volume not found
  725. @return -2 isosurface not found
  726. @return -3 on failure
  727. """
  728. return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
  729. def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
  730. """!Set isosurface attribute
  731. @param id volume id
  732. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  733. @param attr attribute desc
  734. @param map if true use map otherwise constant
  735. @param value map name of value
  736. @return 1 on success
  737. @return -1 volume not found
  738. @return -2 isosurface not found
  739. @return -3 setting attributes failed
  740. """
  741. if not GVL_vol_exists(id):
  742. return -1
  743. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  744. return -2
  745. if map:
  746. ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
  747. else:
  748. if attr == ATT_COLOR:
  749. val = Nviz_color_from_str(value)
  750. else:
  751. val = float(value)
  752. ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
  753. Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
  754. "attr=%d, map=%d, value=%s",
  755. id, isosurf_id, attr, map, value)
  756. if ret < 0:
  757. return -2
  758. return 1
  759. def UnsetIsosurfaceMask(self, id, isosurf_id):
  760. """!Unset isosurface mask
  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_MASK)
  769. def UnsetIsosurfaceTransp(self, id, isosurf_id):
  770. """!Unset isosurface transparency
  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_TRANSP)
  779. def UnsetIsosurfaceEmit(self, id, isosurf_id):
  780. """!Unset isosurface emission
  781. @param id volume id
  782. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  783. @return 1 on success
  784. @return -1 volume not found
  785. @return -2 isosurface not found
  786. @return -3 setting attributes failed
  787. """
  788. return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
  789. def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
  790. """!Unset surface attribute
  791. @param id surface id
  792. @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  793. @param attr attribute descriptor
  794. @return 1 on success
  795. @return -1 volume not found
  796. @return -2 isosurface not found
  797. @return -2 on failure
  798. """
  799. if not GVL_vol_exists(id):
  800. return -1
  801. if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
  802. return -2
  803. Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
  804. id, isosurf_id, attr)
  805. ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
  806. if ret < 0:
  807. return -2
  808. return 1
  809. def SetIsosurfaceMode(self, id, mode):
  810. """!Set draw mode for isosurfaces
  811. @param mode
  812. @return 1 on success
  813. @return -1 volume set not found
  814. @return -2 on failure
  815. """
  816. if not GVL_vol_exists(id):
  817. return -1
  818. ret = GVL_isosurf_set_drawmode(id, mode)
  819. if ret < 0:
  820. return -2
  821. return 1
  822. def SetIsosurfaceRes(self, id, res):
  823. """!Set draw resolution for isosurfaces
  824. @param res resolution value
  825. @return 1 on success
  826. @return -1 volume set not found
  827. @return -2 on failure
  828. """
  829. if not GVL_vol_exists(id):
  830. return -1
  831. ret = GVL_isosurf_set_drawres(id, res, res, res)
  832. if ret < 0:
  833. return -2
  834. return 1
  835. def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
  836. """!Save current GL screen to ppm/tif file
  837. @param filename file name
  838. @param width image width
  839. @param height image height
  840. @param itype image type ('ppm' or 'tif')
  841. """
  842. widthOrig = self.width
  843. heightOrig = self.height
  844. self.ResizeWindow(width, height)
  845. GS_clear(Nviz_get_bgcolor(self.data))
  846. self.Draw(False, -1)
  847. if itype == 'ppm':
  848. GS_write_ppm(filename)
  849. else:
  850. GS_write_tif(filename)
  851. self.ResizeWindow(widthOrig, heightOrig)
  852. def DrawLightingModel(self):
  853. """!Draw lighting model"""
  854. if self.showLight:
  855. GS_draw_lighting_model()
  856. def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
  857. """!Set fringe
  858. @param sid surface id
  859. @param color color
  860. @param elev elevation (height)
  861. @param nw,ne,sw,se fringe edges (turn on/off)
  862. """
  863. scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
  864. Nviz_set_fringe(self.data,
  865. sid, Nviz_color_from_str(scolor),
  866. elev, int(nw), int(ne), int(sw), int(se))
  867. def GetPointOnSurface(self, sx, sy):
  868. """!Get point on surface
  869. @param sx,sy canvas coordinates (LL)
  870. """
  871. sid = c_int()
  872. x = c_float()
  873. y = c_float()
  874. z = c_float()
  875. Debug.msg(5, "GLWindow.GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
  876. num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
  877. if num == 0:
  878. return (None, None, None, None)
  879. return (sid.value, x.value, y.value, z.value)
  880. def QueryMap(self, sx, sy):
  881. """!Query surface map
  882. @param sx,sy canvas coordinates (LL)
  883. """
  884. sid, x, y, z = self.GetPointOnSurface(sx, sy)
  885. if not sid:
  886. return None
  887. catstr = create_string_buffer(256)
  888. valstr = create_string_buffer(256)
  889. GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
  890. GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
  891. return { 'id' : sid,
  892. 'x' : x,
  893. 'y' : y,
  894. 'z' : z,
  895. 'elevation' : catstr.value.replace('(', '').replace(')', ''),
  896. 'color' : valstr.value }
  897. def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
  898. """!Get distance measured along surface"""
  899. d = c_float()
  900. GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
  901. byref(d), int(useExag))
  902. return d.value