wxnviz.py 35 KB

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