GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
wxnviz.py
Go to the documentation of this file.
1 """!
2 @package wxnviz.py
3 
4 @brief wxGUI 3D view mode
5 
6 This module implements 3D visualization mode for map display.
7 
8 List of classes:
9  - Nviz
10 
11 (C) 2008-2010 by the GRASS Development Team
12 
13 This program is free software under the GNU General Public
14 License (>=v2). Read the file COPYING that comes with GRASS
15 for details.
16 
17 @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
18 @author Pythonized by Glynn Clements
19 """
20 
21 import sys
22 from threading import Thread
23 
24 from ctypes import *
25 try:
26  from grass.lib.gis import *
27  from grass.lib.g3d import *
28  from grass.lib.ogsf import *
29  from grass.lib.nviz import *
30 except ImportError, e:
31  sys.stderr.write(_("3D view mode: %s\n") % e)
32 
33 from debug import Debug
34 
35 log = None
36 progress = None
37 
38 def print_error(msg, type):
39  """!Redirect stderr"""
40  global log
41  if log:
42  log.write(msg)
43  else:
44  print msg
45 
46  return 0
47 
48 def print_progress(value):
49  """!Redirect progress info"""
50  global progress
51  if progress:
52  progress.SetValue(value)
53  else:
54  print value
55 
56  return 0
57 
58 errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
59 errfunc = errtype(print_error)
60 pertype = CFUNCTYPE(UNCHECKED(c_int), c_int)
61 perfunc = pertype(print_progress)
62 
63 class Nviz(object):
64  def __init__(self, glog, gprogress):
65  """!Initialize Nviz class instance
66 
67  @param log logging area
68  """
69  global errfunc, perfunc, log, progress
70  log = glog
71  progress = gprogress
72 
73  G_gisinit("")
74  G_set_error_routine(errfunc)
75  G_set_percent_routine(perfunc)
76 
77  GS_libinit()
78  GVL_libinit()
79 
80  self.data_obj = nv_data()
81  self.data = pointer(self.data_obj)
82  self.width = self.height = -1
83  self.showLight = False
84 
85  Debug.msg(1, "Nviz::Nviz()")
86 
87  def __del__(self):
88  """!Destroy Nviz class instance"""
91  del self.data
92  del self.data_obj
93  self.log = None
94 
95  def ResizeWindow(self, width, height):
96  """!GL canvas resized
97 
98  @param width window width
99  @param height window height
100 
101  @return 1 on success
102  @return 0 on failure (window resized by default to 20x20 px)
103  """
104  self.width = width
105  self.height = height
106  Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
107  width, height)
108  return Nviz_resize_window(width, height)
109 
110  def SetViewDefault(self):
111  """!Set default view (based on loaded data)
112 
113  @return z-exag value, default, min and max height
114  """
115  # determine z-exag
116  z_exag = Nviz_get_exag()
117  Nviz_change_exag(self.data, z_exag)
118 
119  # determine height
120  hdef = c_double()
121  hmin = c_double()
122  hmax = c_double()
123  Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
124 
125  Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
126  hdef.value, hmin.value, hmax.value)
127 
128  return (z_exag, hdef.value, hmin.value, hmax.value)
129 
130  def SetView(self, x, y, height, persp, twist):
131  """!Change view settings
132  @param x,y position
133  @param height
134  @param persp perpective
135  @param twist
136  """
141 
142  Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
143  x, y, height, persp, twist)
144 
145  def SetZExag(self, z_exag):
146  """!Set z-exag value
147 
148  @param z_exag value
149 
150  @return 1
151  """
152  Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
153  return Nviz_change_exag(self.data, z_exag)
154 
155  def Draw(self, quick, quick_mode):
156  """!Draw canvas
157 
158  Draw quick mode:
159  - DRAW_QUICK_SURFACE
160  - DRAW_QUICK_VLINES
161  - DRAW_QUICK_VPOINTS
162  - DRAW_QUICK_VOLUME
163 
164  @param quick if true draw in wiremode
165  @param quick_mode quick mode
166  """
167  Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
168 
169  Nviz_draw_cplane(self.data, -1, -1) # ?
170 
171  if quick:
172  Nviz_draw_quick(self.data, quick_mode)
173  else:
174  Nviz_draw_all(self.data)
175 
176  def EraseMap(self):
177  """!Erase map display (with background color)
178  """
179  Debug.msg(1, "Nviz::EraseMap()")
181 
182  def InitView(self):
183  """!Initialize view"""
184  # initialize nviz data
185  Nviz_init_data(self.data)
186 
187  # define default attributes for map objects
189  # set background color
191 
193  # initialize view, lights
194  Nviz_init_view(self.data)
195 
196  Debug.msg(1, "Nviz::InitView()")
197 
198  def SetBgColor(self, color_str):
199  """!Set background color
200 
201  @param color_str color string
202  """
203  Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
204 
205  def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
206  """!Change lighting settings
207  @param x,y,z position
208  @param color light color (as string)
209  @param bright light brightness
210  @param ambient light ambient
211  @param w local coordinate (default to 0)
212  """
213  Nviz_set_light_position(self.data, lid, x, y, z, w)
214  Nviz_set_light_bright(self.data, lid, bright)
215  Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
216  Nviz_set_light_ambient(self.data, lid, ambient)
217 
218  def LoadSurface(self, name, color_name, color_value):
219  """!Load raster map (surface)
220 
221  @param name raster map name
222  @param color_name raster map for color (None for color_value)
223  @param color_value color string (named color or RGB triptet)
224 
225  @return object id
226  @return -1 on failure
227  """
228  mapset = G_find_cell2(name, "")
229  if mapset is None:
230  G_warning(_("Raster map <%s> not found"), name)
231  return -1
232 
233  # topography
234  id = Nviz_new_map_obj(MAP_OBJ_SURF,
235  G_fully_qualified_name(name, mapset), 0.0,
236  self.data)
237 
238  if color_name: # check for color map
239  mapset = G_find_cell2(color_name, "")
240  if mapset is None:
241  G_warning(_("Raster map <%s> not found"), color_name)
243  return -1
244 
245  Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
246  G_fully_qualified_name(color_name, mapset), -1.0,
247  self.data)
248 
249  elif color_value: # check for color value
250  Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
251  None, Nviz_color_from_str(color_value),
252  self.data)
253 
254  else: # use by default elevation map for coloring
255  Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
256  G_fully_qualified_name(name, mapset), -1.0,
257  self.data)
258 
259  # if (i > 1)
260  # set_default_wirecolors(self.data, i)
261 
262  # focus on loaded self.data
263  Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
264 
265  Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
266 
267  return id
268 
269  def UnloadSurface(self, id):
270  """!Unload surface
271 
272  @param id surface id
273 
274  @return 1 on success
275  @return 0 on failure
276  """
277  if not GS_surf_exists(id):
278  return 0
279 
280  Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
281 
282  if GS_delete_surface(id) < 0:
283  return 0
284 
285  return 1
286 
287  def LoadVector(self, name, points):
288  """!Load vector map overlay
289 
290  @param name vector map name
291  @param points if true load 2d points rather then 2d lines
292 
293  @return object id
294  @return -1 on failure
295  """
296  if GS_num_surfs() == 0: # load base surface if no loaded
297  Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
298 
299  nsurf = c_int()
300  surf_list = GS_get_surf_list(byref(nsurf))
301  GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
302 
303  mapset = G_find_vector2 (name, "")
304  if mapset is None:
305  G_warning(_("Vector map <%s> not found"),
306  name)
307 
308  if points:
309  id = Nviz_new_map_obj(MAP_OBJ_SITE,
310  G_fully_qualified_name(name, mapset), 0.0,
311  self.data)
312  else:
313  id = Nviz_new_map_obj(MAP_OBJ_VECT,
314  G_fully_qualified_name(name, mapset), 0.0,
315  self.data)
316 
317  Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
318 
319  return id
320 
321  def UnloadVector(self, id, points):
322  """!Unload vector set
323 
324  @param id vector set id
325  @param points vector points or lines set
326 
327  @return 1 on success
328  @return 0 on failure
329  """
330  Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
331 
332  if points:
333  if not GP_site_exists(id):
334  return 0
335  if GP_delete_site(id) < 0:
336  return 0
337  else:
338  if not GV_vect_exists(id):
339  return 0
340  if GV_delete_vector(id) < 0:
341  return 0
342 
343  return 1
344 
345  def LoadVolume(self, name, color_name, color_value):
346  """!Load 3d raster map (volume)
347 
348  @param name 3d raster map name
349  @param color_name 3d raster map for color (None for color_value)
350  @param color_value color string (named color or RGB triptet)
351 
352  @return object id
353  @return -1 on failure
354  """
355  mapset = G_find_grid3(name, "")
356  if mapset is None:
357  G_warning(_("3d raster map <%s> not found"),
358  name)
359  return -1
360 
361  # topography
362  id = Nviz_new_map_obj(MAP_OBJ_VOL,
363  G_fully_qualified_name(name, mapset), 0.0,
364  self.data)
365 
366  if color_name: # check for color map
367  mapset = G_find_grid3(color_name, "")
368  if mapset is None:
369  G_warning(_("3d raster map <%s> not found"),
370  color_name)
371  GVL_delete_vol(id)
372  return -1
373 
374  Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
375  G_fully_qualified_name(color_name, mapset), -1.0,
376  self.data)
377  elif color_value: # check for color value
378  Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
379  None, Nviz_color_from_str(color_value),
380  self.data)
381  else: # use by default elevation map for coloring
382  Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
383  G_fully_qualified_name(name, mapset), -1.0,
384  self.data)
385 
386  Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
387 
388  return id
389 
390  def UnloadVolume(self, id):
391  """!Unload volume
392 
393  @param id volume id
394 
395  @return 1 on success
396  @return 0 on failure
397  """
398  if not GVL_vol_exists(id):
399  return 0
400 
401  Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
402 
403  if GVL_delete_vol(id) < 0:
404  return 0
405 
406  return 1
407 
408  def SetSurfaceTopo(self, id, map, value):
409  """!Set surface topography
410 
411  @param id surface id
412  @param map if true use map otherwise constant
413  @param value map name of value
414 
415  @return 1 on success
416  @return -1 surface not found
417  @return -2 setting attributes failed
418  """
419  return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
420 
421  def SetSurfaceColor(self, id, map, value):
422  """!Set surface color
423 
424  @param id surface id
425  @param map if true use map otherwise constant
426  @param value map name of value
427 
428  @return 1 on success
429  @return -1 surface not found
430  @return -2 setting attributes failed
431  """
432  return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
433 
434  def SetSurfaceMask(self, id, invert, value):
435  """!Set surface mask
436 
437  @todo invert
438 
439  @param id surface id
440  @param invert if true invert mask
441  @param value map name of value
442 
443  @return 1 on success
444  @return -1 surface not found
445  @return -2 setting attributes failed
446  """
447  return self.SetSurfaceAttr(id, ATT_MASK, true, value)
448 
449  def SetSurfaceTransp(self, id, map, value):
450  """!Set surface mask
451 
452  @todo invert
453 
454  @param id surface id
455  @param map if true use map otherwise constant
456  @param value map name of value
457 
458  @return 1 on success
459  @return -1 surface not found
460  @return -2 setting attributes failed
461  """
462  return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
463 
464  def SetSurfaceShine(self, id, map, value):
465  """!Set surface shininess
466 
467  @param id surface id
468  @param map if true use map otherwise constant
469  @param value map name of value
470 
471  @return 1 on success
472  @return -1 surface not found
473  @return -2 setting attributes failed
474  """
475  return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
476 
477  def SetSurfaceEmit(self, id, map, value):
478  """!Set surface emission
479 
480  @param id surface id
481  @param map if true use map otherwise constant
482  @param value map name of value
483 
484  @return 1 on success
485  @return -1 surface not found
486  @return -2 setting attributes failed
487  """
488  return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
489 
490  def SetSurfaceAttr(self, id, attr, map, value):
491  """!Set surface attribute
492 
493  @param id surface id
494  @param attr attribute desc
495  @param map if true use map otherwise constant
496  @param value map name of value
497 
498  @return 1 on success
499  @return -1 surface not found
500  @return -2 setting attributes failed
501  """
502  if not GS_surf_exists(id):
503  return -1
504 
505  if map:
506  ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
507  value, -1.0, self.data)
508  else:
509  if attr == ATT_COLOR:
510  val = Nviz_color_from_str(value)
511  else:
512  val = float(value)
513 
514  ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
515  None, val, self.data)
516 
517  Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
518  id, attr, map, value)
519 
520  if ret < 0:
521  return -2
522 
523  return 1
524 
525  def UnsetSurfaceMask(self, id):
526  """!Unset surface mask
527 
528  @param id surface id
529 
530  @return 1 on success
531  @return -1 surface not found
532  @return -2 setting attributes failed
533  @return -1 on failure
534  """
535  return self.UnsetSurfaceAttr(id, ATT_MASK)
536 
537  def UnsetSurfaceTransp(self, id):
538  """!Unset surface transparency
539 
540  @param id surface id
541 
542  @return 1 on success
543  @return -1 surface not found
544  @return -2 setting attributes failed
545  """
546  return self.UnsetSurfaceAttr(id, ATT_TRANSP)
547 
548  def UnsetSurfaceEmit(self, id):
549  """!Unset surface emission
550 
551  @param id surface id
552 
553  @return 1 on success
554  @return -1 surface not found
555  @return -2 setting attributes failed
556  """
557  return self.UnsetSurfaceAttr(id, ATT_EMIT)
558 
559  def UnsetSurfaceAttr(self, id, attr):
560  """!Unset surface attribute
561 
562  @param id surface id
563  @param attr attribute descriptor
564 
565  @return 1 on success
566  @return -1 surface not found
567  @return -2 setting attributes failed
568  """
569  if not GS_surf_exists(id):
570  return -1
571 
572  Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
573  id, attr)
574 
575  ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
576 
577  if ret < 0:
578  return -2
579 
580  return 1
581 
582  def SetSurfaceRes(self, id, fine, coarse):
583  """!Set surface resolution
584 
585  @param id surface id
586  @param fine x/y fine resolution
587  @param coarse x/y coarse resolution
588 
589  @return 1 on success
590  @return -1 surface not found
591  @return -2 setting attributes failed
592  """
593  Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
594  id, fine, coarse)
595 
596  if id > 0:
597  if not GS_surf_exists(id):
598  return -1
599 
600  if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
601  return -2
602  else:
603  GS_setall_drawres(fine, fine, coarse, coarse)
604 
605  return 1
606 
607  def SetSurfaceStyle(self, id, style):
608  """!Set draw style
609 
610  Draw styles:
611  - DM_GOURAUD
612  - DM_FLAT
613  - DM_FRINGE
614  - DM_WIRE
615  - DM_COL_WIRE
616  - DM_POLY
617  - DM_WIRE_POLY
618  - DM_GRID_WIRE
619  - DM_GRID_SURF
620 
621  @param id surface id (<= 0 for all)
622  @param style draw style
623 
624  @return 1 on success
625  @return -1 surface not found
626  @return -2 setting attributes failed
627  """
628  Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
629  id, style)
630 
631  if id > 0:
632  if not GS_surf_exists(id):
633  return -1
634 
635  if GS_set_drawmode(id, style) < 0:
636  return -2
637 
638  return 1
639 
640  if GS_setall_drawmode(style) < 0:
641  return -2
642 
643  return 1
644 
645  def SetWireColor(self, id, color_str):
646  """!Set color of wire
647 
648  @todo all
649 
650  @param surface id (< 0 for all)
651  @param color color string (R:G:B)
652 
653  @return 1 on success
654  @return -1 surface not found
655  @return -2 setting attributes failed
656  @return 1 on success
657  @return 0 on failure
658  """
659  Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
660  id, color_str)
661 
662  color = Nviz_color_from_str(color_str)
663 
664  if id > 0:
665  if not GS_surf_exists(id):
666  return -1
667 
668  GS_set_wire_color(id, color)
669  else:
670  nsurfs = c_int()
671  surf_list = GS_get_surf_list(byref(nsurfs))
672  for i in xrange(nsurfs.value):
673  id = surf_list[i]
674  GS_set_wire_color(id, color)
675 
676  G_free(surf_list)
677  surf_list = None
678 
679  return 1
680 
681  def GetSurfacePosition(self, id):
682  """!Get surface position
683 
684  @param id surface id
685 
686  @return x,y,z
687  @return zero-length vector on error
688  """
689  if not GS_surf_exists(id):
690  return []
691 
692  x, y, z = c_float(), c_float(), c_float()
693  GS_get_trans(id, byref(x), byref(y), byref(z))
694 
695  Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
696  id, x.value, y.value, z.value)
697 
698  return [x.value, y.value, z.value]
699 
700  def SetSurfacePosition(self, id, x, y, z):
701  """!Set surface position
702 
703  @param id surface id
704  @param x,y,z translation values
705 
706  @return 1 on success
707  @return -1 surface not found
708  @return -2 setting position failed
709  """
710  if not GS_surf_exists(id):
711  return -1
712 
713  Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
714  id, x, y, z)
715 
716  GS_set_trans(id, x, y, z)
717 
718  return 1
719 
720  def SetVectorLineMode(self, id, color_str, width, flat):
721  """!Set mode of vector line overlay
722 
723  @param id vector id
724  @param color_str color string
725  @param width line width
726  @param flat display flat or on surface
727 
728  @return -1 vector set not found
729  @return -2 on failure
730  @return 1 on success
731  """
732  if not GV_vect_exists(id):
733  return -1
734 
735  Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
736  id, color_str, width, flat)
737 
738  color = Nviz_color_from_str(color_str)
739 
740  # use memory by default
741  if GV_set_vectmode(id, 1, color, width, flat) < 0:
742  return -2
743 
744  return 1
745 
746  def SetVectorLineHeight(self, id, height):
747  """!Set vector height above surface (lines)
748 
749  @param id vector set id
750  @param height
751 
752  @return -1 vector set not found
753  @return 1 on success
754  """
755  if not GV_vect_exists(id):
756  return -1
757 
758  Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
759  id, height)
760 
761  GV_set_trans(id, 0.0, 0.0, height)
762 
763  return 1
764 
765  def SetVectorLineSurface(self, id, surf_id):
766  """!Set reference surface of vector set (lines)
767 
768  @param id vector set id
769  @param surf_id surface id
770 
771  @return 1 on success
772  @return -1 vector set not found
773  @return -2 surface not found
774  @return -3 on failure
775  """
776  if not GV_vect_exists(id):
777  return -1
778 
779  if not GS_surf_exists(surf_id):
780  return -2
781 
782  if GV_select_surf(id, surf_id) < 0:
783  return -3
784 
785  return 1
786 
787  def SetVectorPointMode(self, id, color_str, width, size, marker):
788  """!Set mode of vector point overlay
789 
790  @param id vector id
791  @param color_str color string
792  @param width line width
793  @param flat
794 
795  @return -1 vector set not found
796  """
797  if not GP_site_exists(id):
798  return -1
799 
800  Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
801  "width=%d, size=%f, marker=%d",
802  id, color_str, width, size, marker)
803 
804  color = Nviz_color_from_str(color_str)
805 
806  ### TODO
807  # if GP_set_style(id, color, width, size, marker) < 0:
808  # return -2
809 
810  return 1
811 
812  def SetVectorPointHeight(self, id, height):
813  """!Set vector height above surface (points)
814 
815  @param id vector set id
816  @param height
817 
818  @return -1 vector set not found
819  @return 1 on success
820  """
821  if not GP_site_exists(id):
822  return -1
823 
824  Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
825  id, height)
826 
827  GP_set_trans(id, 0.0, 0.0, height)
828 
829  return 1
830 
831  def SetVectorPointSurface(self, id, surf_id):
832  """!Set reference surface of vector set (points)
833 
834  @param id vector set id
835  @param surf_id surface id
836 
837  @return 1 on success
838  @return -1 vector set not found
839  @return -2 surface not found
840  @return -3 on failure
841  """
842  if not GP_site_exists(id):
843  return -1
844 
845  if not GS_surf_exists(surf_id):
846  return -2
847 
848  if GP_select_surf(id, surf_id) < 0:
849  return -3
850 
851  return 1
852 
853  def AddIsosurface(self, id, level):
854  """!Add new isosurface
855 
856  @param id volume id
857  @param level isosurface level (topography)
858 
859  @return -1 on failure
860  @return 1 on success
861  """
862  if not GVL_vol_exists(id):
863  return -1
864 
865  if GVL_isosurf_add(id) < 0:
866  return -1
867 
868  # set topography level
869  nisosurfs = GVL_isosurf_num_isosurfs(id)
870 
871  return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
872 
873  def DeleteIsosurface(self, id, isosurf_id):
874  """!Delete isosurface
875 
876  @param id volume id
877  @param isosurf_id isosurface id
878 
879  @return 1 on success
880  @return -1 volume not found
881  @return -2 isosurface not found
882  @return -3 on failure
883  """
884  if not GVL_vol_exists(id):
885  return -1
886 
887  if isosurf_id > GVL_isosurf_num_isosurfs(id):
888  return -2
889 
890  ret = GVL_isosurf_del(id, isosurf_id)
891 
892  if ret < 0:
893  return -3
894 
895  return 1
896 
897  def MoveIsosurface(self, id, isosurf_id, up):
898  """!Move isosurface up/down in the list
899 
900  @param id volume id
901  @param isosurf_id isosurface id
902  @param up if true move up otherwise down
903 
904  @return 1 on success
905  @return -1 volume not found
906  @return -2 isosurface not found
907  @return -3 on failure
908  """
909  if not GVL_vol_exists(id):
910  return -1
911 
912  if isosurf_id > GVL_isosurf_num_isosurfs(id):
913  return -2
914 
915  if up:
916  ret = GVL_isosurf_move_up(id, isosurf_id)
917  else:
918  ret = GVL_isosurf_move_down(id, isosurf_id)
919 
920  if ret < 0:
921  return -3
922 
923  return 1
924 
925  def SetIsosurfaceColor(self, id, isosurf_id, map, value):
926  """!Set isosurface color
927 
928  @param id volume id
929  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
930  @param map if true use map otherwise constant
931  @param value map name of value
932 
933  @return 1 on success
934  @return -1 volume not found
935  @return -2 isosurface not found
936  @return -3 on failure
937  """
938  return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
939 
940  def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
941  """!Set isosurface mask
942 
943  @todo invert
944 
945  @param id volume id
946  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
947  @param invert true for invert mask
948  @param value map name to be used for mask
949 
950  @return 1 on success
951  @return -1 volume not found
952  @return -2 isosurface not found
953  @return -3 on failure
954  """
955  return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, true, value)
956 
957  def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
958  """!Set isosurface transparency
959 
960  @param id volume id
961  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
962  @param map if true use map otherwise constant
963  @param value map name of value
964 
965  @return 1 on success
966  @return -1 volume not found
967  @return -2 isosurface not found
968  @return -3 on failure
969  """
970  return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
971 
972  def SetIsosurfaceShine(self, id, isosurf_id, map, value):
973  """!Set isosurface shininess
974 
975  @param id volume id
976  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
977  @param map if true use map otherwise constant
978  @param value map name of value
979 
980  @return 1 on success
981  @return -1 volume not found
982  @return -2 isosurface not found
983  @return -3 on failure
984  """
985  return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
986 
987  def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
988  """!Set isosurface emission
989 
990  @param id volume id
991  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
992  @param map if true use map otherwise constant
993  @param value map name of value
994 
995  @return 1 on success
996  @return -1 volume not found
997  @return -2 isosurface not found
998  @return -3 on failure
999  """
1000  return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
1001 
1002  def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
1003  """!Set isosurface attribute
1004 
1005  @param id volume id
1006  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
1007  @param attr attribute desc
1008  @param map if true use map otherwise constant
1009  @param value map name of value
1010 
1011  @return 1 on success
1012  @return -1 volume not found
1013  @return -2 isosurface not found
1014  @return -3 setting attributes failed
1015  """
1016  if not GVL_vol_exists(id):
1017  return -1
1018 
1019  if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
1020  return -2
1021 
1022  if map:
1023  ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
1024  else:
1025  if attr == ATT_COLOR:
1026  val = Nviz_color_from_str(value)
1027  else:
1028  val = float(value)
1029 
1030  ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
1031 
1032  Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
1033  "attr=%d, map=%d, value=%s",
1034  id, isosurf_id, attr, map, value)
1035 
1036  if ret < 0:
1037  return -2
1038 
1039  return 1
1040 
1041  def UnsetIsosurfaceMask(self, id, isosurf_id):
1042  """!Unset isosurface mask
1043 
1044  @param id volume id
1045  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
1046 
1047  @return 1 on success
1048  @return -1 volume not found
1049  @return -2 isosurface not found
1050  @return -3 setting attributes failed
1051  """
1052  return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
1053 
1054  def UnsetIsosurfaceTransp(self, id, isosurf_id):
1055  """!Unset isosurface transparency
1056 
1057  @param id volume id
1058  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
1059 
1060  @return 1 on success
1061  @return -1 volume not found
1062  @return -2 isosurface not found
1063  @return -3 setting attributes failed
1064  """
1065  return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
1066 
1067  def UnsetIsosurfaceEmit(self, id, isosurf_id):
1068  """!Unset isosurface emission
1069 
1070  @param id volume id
1071  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
1072 
1073  @return 1 on success
1074  @return -1 volume not found
1075  @return -2 isosurface not found
1076  @return -3 setting attributes failed
1077  """
1078  return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
1079 
1080  def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
1081  """!Unset surface attribute
1082 
1083  @param id surface id
1084  @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
1085  @param attr attribute descriptor
1086 
1087  @return 1 on success
1088  @return -1 volume not found
1089  @return -2 isosurface not found
1090  @return -2 on failure
1091  """
1092  if not GVL_vol_exists(id):
1093  return -1
1094 
1095  if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
1096  return -2
1097 
1098  Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
1099  id, isosurf_id, attr)
1100 
1101  ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
1102 
1103  if ret < 0:
1104  return -2
1105 
1106  return 1
1107 
1108  def SetIsosurfaceMode(self, id, mode):
1109  """!Set draw mode for isosurfaces
1110 
1111  @param mode
1112 
1113  @return 1 on success
1114  @return -1 volume set not found
1115  @return -2 on failure
1116  """
1117  if not GVL_vol_exists(id):
1118  return -1
1119 
1120  ret = GVL_isosurf_set_drawmode(id, mode)
1121 
1122  if ret < 0:
1123  return -2
1124 
1125  return 1
1126 
1127  def SetIsosurfaceRes(self, id, res):
1128  """!Set draw resolution for isosurfaces
1129 
1130  @param res resolution value
1131 
1132  @return 1 on success
1133  @return -1 volume set not found
1134  @return -2 on failure
1135  """
1136  if not GVL_vol_exists(id):
1137  return -1
1138 
1139  ret = GVL_isosurf_set_drawres(id, res, res, res)
1140 
1141  if ret < 0:
1142  return -2
1143 
1144  return 1
1145 
1146  def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
1147  """!Save current GL screen to ppm/tif file
1148 
1149  @param filename file name
1150  @param width image width
1151  @param height image height
1152  @param itype image type ('ppm' or 'tif')
1153  """
1154  widthOrig = self.width
1155  heightOrig = self.height
1156 
1157  self.ResizeWindow(width, height)
1159  self.Draw(False, -1)
1160  if itype == 'ppm':
1161  GS_write_ppm(filename)
1162  else:
1163  GS_write_tif(filename)
1164 
1165  self.ResizeWindow(widthOrig, heightOrig)
1166 
1168  """!Draw lighting model"""
1169  if self.showLight:
1171 
1172  def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
1173  """!Set fringe
1174 
1175  @param sid surface id
1176  @param color color
1177  @param elev elevation (height)
1178  @param nw,ne,sw,se fringe edges (turn on/off)
1179  """
1180  scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
1181 
1182  Nviz_set_fringe(self.data,
1183  sid, Nviz_color_from_str(scolor),
1184  elev, int(nw), int(ne), int(sw), int(se))
1185 
1186  def GetPointOnSurface(self, sx, sy):
1187  """!Get point on surface
1188 
1189  @param sx,sy canvas coordinates (LL)
1190  """
1191  sid = c_int()
1192  x = c_float()
1193  y = c_float()
1194  z = c_float()
1195  Debug.msg(5, "GLWindow.GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
1196  num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
1197  if num == 0:
1198  return (None, None, None, None)
1199 
1200  return (sid.value, x.value, y.value, z.value)
1201 
1202  def QueryMap(self, sx, sy):
1203  """!Query surface map
1204 
1205  @param sx,sy canvas coordinates (LL)
1206  """
1207  sid, x, y, z = self.GetPointOnSurface(sx, sy)
1208  if not sid:
1209  return None
1210 
1211  catstr = create_string_buffer(256)
1212  valstr = create_string_buffer(256)
1213  GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
1214  GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
1215 
1216  return { 'id' : sid,
1217  'x' : x,
1218  'y' : y,
1219  'z' : z,
1220  'elevation' : catstr.value.replace('(', '').replace(')', ''),
1221  'color' : valstr.value }
1222 
1223  def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
1224  """!Get distance measured along surface"""
1225  d = c_float()
1226 
1227  GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
1228  byref(d), int(useExag))
1229 
1230  return d.value