wxnviz.py 43 KB

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