wxnviz.py 37 KB

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