GRASS Programmer's Manual  6.4.2(2012)
wxnviz.py
Go to the documentation of this file.
00001 """!
00002 @package wxnviz.py
00003 
00004 @brief wxGUI 3D view mode
00005 
00006 This module implements 3D visualization mode for map display.
00007 
00008 List of classes:
00009  - Nviz
00010 
00011 (C) 2008-2010 by the GRASS Development Team
00012 
00013 This program is free software under the GNU General Public
00014 License (>=v2). Read the file COPYING that comes with GRASS
00015 for details.
00016 
00017 @author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
00018 @author Pythonized by Glynn Clements
00019 """
00020 
00021 import sys
00022 from threading import Thread
00023 
00024 from ctypes import *
00025 try:
00026     from grass.lib.gis   import *
00027     from grass.lib.g3d   import *
00028     from grass.lib.ogsf  import *
00029     from grass.lib.nviz  import *
00030 except ImportError, e:
00031     sys.stderr.write(_("3D view mode: %s\n") % e)
00032 
00033 from debug import Debug
00034 
00035 log      = None
00036 progress = None
00037 
00038 def print_error(msg, type):
00039     """!Redirect stderr"""
00040     global log
00041     if log:
00042         log.write(msg)
00043     else:
00044         print msg
00045     
00046     return 0
00047 
00048 def print_progress(value):
00049     """!Redirect progress info"""
00050     global progress
00051     if progress:
00052         progress.SetValue(value)
00053     else:
00054         print value
00055     
00056     return 0
00057 
00058 errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
00059 errfunc = errtype(print_error)
00060 pertype = CFUNCTYPE(UNCHECKED(c_int), c_int)
00061 perfunc = pertype(print_progress)
00062 
00063 class Nviz(object):
00064     def __init__(self, glog, gprogress):
00065         """!Initialize Nviz class instance
00066         
00067         @param log logging area
00068         """
00069         global errfunc, perfunc, log, progress
00070         log = glog
00071         progress = gprogress
00072         
00073         G_gisinit("")
00074         G_set_error_routine(errfunc)
00075         G_set_percent_routine(perfunc)
00076         
00077         GS_libinit()
00078         GVL_libinit()
00079         
00080         self.data_obj = nv_data()
00081         self.data = pointer(self.data_obj)
00082         self.width = self.height = -1
00083         self.showLight = False
00084         
00085         Debug.msg(1, "Nviz::Nviz()")
00086         
00087     def __del__(self):
00088         """!Destroy Nviz class instance"""
00089         G_unset_error_routine()
00090         G_unset_percent_routine()
00091         del self.data
00092         del self.data_obj
00093         self.log = None
00094         
00095     def ResizeWindow(self, width, height):
00096         """!GL canvas resized
00097         
00098         @param width window width
00099         @param height window height
00100         
00101         @return 1 on success
00102         @return 0 on failure (window resized by default to 20x20 px)
00103         """
00104         self.width  = width
00105         self.height = height
00106         Debug.msg(3, "Nviz::ResizeWindow(): width=%d height=%d",
00107                   width, height)
00108         return Nviz_resize_window(width, height)
00109     
00110     def SetViewDefault(self):
00111         """!Set default view (based on loaded data)
00112         
00113         @return z-exag value, default, min and max height
00114         """
00115         # determine z-exag
00116         z_exag = Nviz_get_exag()
00117         Nviz_change_exag(self.data, z_exag)
00118         
00119         # determine height
00120         hdef = c_double()
00121         hmin = c_double()
00122         hmax = c_double()
00123         Nviz_get_exag_height(byref(hdef), byref(hmin), byref(hmax))
00124         
00125         Debug.msg(1, "Nviz::SetViewDefault(): hdef=%f, hmin=%f, hmax=%f",
00126                   hdef.value, hmin.value, hmax.value)
00127         
00128         return (z_exag, hdef.value, hmin.value, hmax.value)
00129     
00130     def SetView(self, x, y, height, persp, twist):
00131         """!Change view settings
00132         @param x,y position
00133         @param height
00134         @param persp perpective
00135         @param twist
00136         """
00137         Nviz_set_viewpoint_height(height)
00138         Nviz_set_viewpoint_position(x, y)
00139         Nviz_set_viewpoint_twist(twist)
00140         Nviz_set_viewpoint_persp(persp)
00141         
00142         Debug.msg(3, "Nviz::SetView(): x=%f, y=%f, height=%f, persp=%f, twist=%f",
00143                   x, y, height, persp, twist)
00144         
00145     def SetZExag(self, z_exag):
00146         """!Set z-exag value
00147         
00148         @param z_exag value
00149         
00150         @return 1
00151         """
00152         Debug.msg(3, "Nviz::SetZExag(): z_exag=%f", z_exag)
00153         return Nviz_change_exag(self.data, z_exag)
00154     
00155     def Draw(self, quick, quick_mode):
00156         """!Draw canvas
00157         
00158         Draw quick mode:
00159          - DRAW_QUICK_SURFACE
00160          - DRAW_QUICK_VLINES
00161          - DRAW_QUICK_VPOINTS
00162          - DRAW_QUICK_VOLUME
00163         
00164         @param quick if true draw in wiremode
00165         @param quick_mode quick mode
00166         """
00167         Debug.msg(3, "Nviz::Draw(): quick=%d", quick)
00168         
00169         Nviz_draw_cplane(self.data, -1, -1) # ?
00170         
00171         if quick:
00172             Nviz_draw_quick(self.data, quick_mode)
00173         else:
00174             Nviz_draw_all(self.data)
00175         
00176     def EraseMap(self):
00177         """!Erase map display (with background color)
00178         """
00179         Debug.msg(1, "Nviz::EraseMap()")
00180         GS_clear(Nviz_get_bgcolor(self.data))
00181         
00182     def InitView(self):
00183         """!Initialize view"""
00184         # initialize nviz data
00185         Nviz_init_data(self.data)
00186         
00187         # define default attributes for map objects
00188         Nviz_set_surface_attr_default()
00189         # set background color
00190         Nviz_set_bgcolor(self.data, Nviz_color_from_str("white"))
00191         
00192         GS_clear(Nviz_get_bgcolor(self.data))
00193         # initialize view, lights
00194         Nviz_init_view(self.data)
00195         
00196         Debug.msg(1, "Nviz::InitView()")
00197         
00198     def SetBgColor(self, color_str):
00199         """!Set background color
00200         
00201         @param color_str color string
00202         """
00203         Nviz_set_bgcolor(self.data, Nviz_color_from_str(color_str))
00204         
00205     def SetLight(self, x, y, z, color, bright, ambient, w = 0, lid = 1):
00206         """!Change lighting settings
00207         @param x,y,z position
00208         @param color light color (as string)
00209         @param bright light brightness
00210         @param ambient light ambient
00211         @param w local coordinate (default to 0)
00212         """
00213         Nviz_set_light_position(self.data, lid, x, y, z, w)
00214         Nviz_set_light_bright(self.data, lid, bright)
00215         Nviz_set_light_color(self.data, lid, int(color[0]), int(color[1]), int(color[2]))
00216         Nviz_set_light_ambient(self.data, lid, ambient)
00217                              
00218     def LoadSurface(self, name, color_name, color_value):
00219         """!Load raster map (surface)
00220         
00221         @param name raster map name
00222         @param color_name raster map for color (None for color_value)
00223         @param color_value color string (named color or RGB triptet)
00224         
00225         @return object id
00226         @return -1 on failure
00227         """
00228         mapset = G_find_cell2(name, "")
00229         if mapset is None:
00230             G_warning(_("Raster map <%s> not found"), name)
00231             return -1
00232         
00233         # topography
00234         id = Nviz_new_map_obj(MAP_OBJ_SURF,
00235                               G_fully_qualified_name(name, mapset), 0.0,
00236                               self.data)
00237         
00238         if color_name:      # check for color map
00239             mapset = G_find_cell2(color_name, "")
00240             if mapset is None:
00241                 G_warning(_("Raster map <%s> not found"), color_name)
00242                 GS_delete_surface(id)
00243                 return -1
00244             
00245             Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
00246                           G_fully_qualified_name(color_name, mapset), -1.0,
00247                           self.data)
00248         
00249         elif color_value:   # check for color value
00250             Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
00251                           None, Nviz_color_from_str(color_value),
00252                           self.data)
00253         
00254         else:               # use by default elevation map for coloring
00255             Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
00256                           G_fully_qualified_name(name, mapset), -1.0,
00257                           self.data)
00258         
00259         # if (i > 1)
00260         #     set_default_wirecolors(self.data, i)
00261         
00262         # focus on loaded self.data
00263         Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1)
00264         
00265         Debug.msg(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id)
00266         
00267         return id
00268 
00269     def UnloadSurface(self, id):
00270         """!Unload surface
00271         
00272         @param id surface id
00273         
00274         @return 1 on success
00275         @return 0 on failure
00276         """
00277         if not GS_surf_exists(id):
00278             return 0
00279         
00280         Debug.msg(1, "Nviz::UnloadSurface(): id=%d", id)
00281         
00282         if GS_delete_surface(id) < 0:
00283             return 0
00284         
00285         return 1
00286     
00287     def LoadVector(self, name, points):
00288         """!Load vector map overlay
00289         
00290         @param name vector map name
00291         @param points if true load 2d points rather then 2d lines
00292         
00293         @return object id
00294         @return -1 on failure
00295         """
00296         if GS_num_surfs() == 0:     # load base surface if no loaded
00297             Nviz_new_map_obj(MAP_OBJ_SURF, None, 0.0, self.data)
00298             
00299             nsurf = c_int()
00300             surf_list = GS_get_surf_list(byref(nsurf))
00301             GS_set_att_const(surf_list[0], ATT_TRANSP, 255)
00302         
00303         mapset = G_find_vector2 (name, "")
00304         if mapset is None:
00305             G_warning(_("Vector map <%s> not found"),
00306                       name)
00307         
00308         if points:
00309             id = Nviz_new_map_obj(MAP_OBJ_SITE,
00310                                   G_fully_qualified_name(name, mapset), 0.0,
00311                                   self.data)
00312         else:
00313             id = Nviz_new_map_obj(MAP_OBJ_VECT,
00314                                   G_fully_qualified_name(name, mapset), 0.0,
00315                                   self.data)
00316         
00317         Debug.msg(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id)
00318         
00319         return id
00320     
00321     def UnloadVector(self, id, points):
00322         """!Unload vector set
00323         
00324         @param id vector set id
00325         @param points vector points or lines set
00326         
00327         @return 1 on success
00328         @return 0 on failure
00329         """
00330         Debug.msg(1, "Nviz::UnloadVector(): id=%d", id)
00331         
00332         if points:
00333             if not GP_site_exists(id):
00334                 return 0
00335             if GP_delete_site(id) < 0:
00336                 return 0
00337         else:
00338             if not GV_vect_exists(id):
00339                 return 0
00340             if GV_delete_vector(id) < 0:
00341                 return 0
00342         
00343         return 1
00344 
00345     def LoadVolume(self, name, color_name, color_value):
00346         """!Load 3d raster map (volume)
00347         
00348         @param name 3d raster map name
00349         @param color_name 3d raster map for color (None for color_value)
00350         @param color_value color string (named color or RGB triptet)
00351         
00352         @return object id
00353         @return -1 on failure
00354         """
00355         mapset = G_find_grid3(name, "")
00356         if mapset is None:
00357             G_warning(_("3d raster map <%s> not found"),
00358                       name)
00359             return -1
00360         
00361         # topography
00362         id = Nviz_new_map_obj(MAP_OBJ_VOL,
00363                               G_fully_qualified_name(name, mapset), 0.0,
00364                               self.data)
00365         
00366         if color_name:      # check for color map
00367             mapset = G_find_grid3(color_name, "")
00368             if mapset is None:
00369                 G_warning(_("3d raster map <%s> not found"),
00370                           color_name)
00371                 GVL_delete_vol(id)
00372                 return -1
00373             
00374             Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
00375                           G_fully_qualified_name(color_name, mapset), -1.0,
00376                           self.data)
00377         elif color_value:   # check for color value
00378             Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
00379                           None, Nviz_color_from_str(color_value),
00380                           self.data)
00381         else:               # use by default elevation map for coloring
00382             Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
00383                           G_fully_qualified_name(name, mapset), -1.0,
00384                           self.data)
00385         
00386         Debug.msg(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id)
00387         
00388         return id
00389 
00390     def UnloadVolume(self, id):
00391         """!Unload volume
00392         
00393         @param id volume id
00394         
00395         @return 1 on success
00396         @return 0 on failure
00397         """
00398         if not GVL_vol_exists(id):
00399             return 0
00400         
00401         Debug.msg(1, "Nviz::UnloadVolume(): id=%d", id)
00402         
00403         if GVL_delete_vol(id) < 0:
00404           return 0
00405         
00406         return 1
00407     
00408     def SetSurfaceTopo(self, id, map, value):
00409         """!Set surface topography
00410         
00411         @param id surface id
00412         @param map if true use map otherwise constant
00413         @param value map name of value
00414         
00415         @return 1 on success
00416         @return -1 surface not found
00417         @return -2 setting attributes failed
00418         """
00419         return self.SetSurfaceAttr(id, ATT_TOPO, map, value)
00420     
00421     def SetSurfaceColor(self, id, map, value):
00422         """!Set surface color
00423         
00424         @param id surface id
00425         @param map if true use map otherwise constant
00426         @param value map name of value
00427         
00428         @return 1 on success
00429         @return -1 surface not found
00430         @return -2 setting attributes failed
00431         """
00432         return self.SetSurfaceAttr(id, ATT_COLOR, map, value)
00433     
00434     def SetSurfaceMask(self, id, invert, value):
00435         """!Set surface mask
00436         
00437         @todo invert
00438         
00439         @param id surface id
00440         @param invert if true invert mask 
00441         @param value map name of value
00442         
00443         @return 1 on success
00444         @return -1 surface not found
00445         @return -2 setting attributes failed
00446         """
00447         return self.SetSurfaceAttr(id, ATT_MASK, true, value)
00448     
00449     def SetSurfaceTransp(self, id, map, value):
00450         """!Set surface mask
00451         
00452         @todo invert
00453         
00454         @param id surface id
00455         @param map if true use map otherwise constant
00456         @param value map name of value
00457         
00458         @return 1 on success
00459         @return -1 surface not found
00460         @return -2 setting attributes failed
00461         """
00462         return self.SetSurfaceAttr(id, ATT_TRANSP, map, value)
00463     
00464     def SetSurfaceShine(self, id, map, value):
00465         """!Set surface shininess
00466         
00467         @param id surface id
00468         @param map if true use map otherwise constant
00469         @param value map name of value
00470         
00471         @return 1 on success
00472         @return -1 surface not found
00473         @return -2 setting attributes failed
00474         """
00475         return self.SetSurfaceAttr(id, ATT_SHINE, map, value)
00476     
00477     def SetSurfaceEmit(self, id, map, value):
00478         """!Set surface emission
00479         
00480         @param id surface id
00481         @param map if true use map otherwise constant
00482         @param value map name of value
00483         
00484         @return 1 on success
00485         @return -1 surface not found
00486         @return -2 setting attributes failed
00487         """
00488         return self.SetSurfaceAttr(id, ATT_EMIT, map, value)
00489     
00490     def SetSurfaceAttr(self, id, attr, map, value):
00491         """!Set surface attribute
00492         
00493         @param id surface id
00494         @param attr attribute desc
00495         @param map if true use map otherwise constant
00496         @param value map name of value
00497         
00498         @return 1 on success
00499         @return -1 surface not found
00500         @return -2 setting attributes failed
00501         """
00502         if not GS_surf_exists(id):
00503             return -1
00504         
00505         if map:
00506             ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
00507                                 value, -1.0, self.data)
00508         else:
00509             if attr == ATT_COLOR:
00510                 val = Nviz_color_from_str(value)
00511             else:
00512                 val = float(value)
00513             
00514             ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
00515                                 None, val, self.data)
00516         
00517         Debug.msg(3, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
00518                   id, attr, map, value)
00519         
00520         if ret < 0:
00521             return -2
00522         
00523         return 1
00524     
00525     def UnsetSurfaceMask(self, id):
00526         """!Unset surface mask
00527         
00528         @param id surface id
00529         
00530         @return 1 on success
00531         @return -1 surface not found
00532         @return -2 setting attributes failed
00533         @return -1 on failure
00534         """
00535         return self.UnsetSurfaceAttr(id, ATT_MASK)
00536     
00537     def UnsetSurfaceTransp(self, id):
00538         """!Unset surface transparency
00539         
00540         @param id surface id
00541         
00542         @return 1 on success
00543         @return -1 surface not found
00544         @return -2 setting attributes failed
00545         """
00546         return self.UnsetSurfaceAttr(id, ATT_TRANSP)
00547     
00548     def UnsetSurfaceEmit(self, id):
00549         """!Unset surface emission
00550         
00551         @param id surface id
00552         
00553         @return 1 on success
00554         @return -1 surface not found
00555         @return -2 setting attributes failed
00556         """
00557         return self.UnsetSurfaceAttr(id, ATT_EMIT)
00558     
00559     def UnsetSurfaceAttr(self, id, attr):
00560         """!Unset surface attribute
00561         
00562         @param id surface id
00563         @param attr attribute descriptor
00564         
00565         @return 1 on success
00566         @return -1 surface not found
00567         @return -2 setting attributes failed
00568         """
00569         if not GS_surf_exists(id):
00570             return -1
00571         
00572         Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
00573                   id, attr)
00574         
00575         ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr)
00576         
00577         if ret < 0:
00578             return -2
00579         
00580         return 1
00581 
00582     def SetSurfaceRes(self, id, fine, coarse):
00583         """!Set surface resolution
00584         
00585         @param id surface id
00586         @param fine x/y fine resolution
00587         @param coarse x/y coarse resolution
00588         
00589         @return 1 on success
00590         @return -1 surface not found
00591         @return -2 setting attributes failed
00592         """
00593         Debug.msg(3, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
00594                   id, fine, coarse)
00595         
00596         if id > 0:
00597             if not GS_surf_exists(id):
00598                 return -1
00599             
00600             if GS_set_drawres(id, fine, fine, coarse, coarse) < 0:
00601                 return -2
00602         else:
00603             GS_setall_drawres(fine, fine, coarse, coarse)
00604         
00605         return 1
00606 
00607     def SetSurfaceStyle(self, id, style):
00608         """!Set draw style
00609         
00610         Draw styles:
00611          - DM_GOURAUD
00612          - DM_FLAT
00613          - DM_FRINGE
00614          - DM_WIRE
00615          - DM_COL_WIRE
00616          - DM_POLY
00617          - DM_WIRE_POLY
00618          - DM_GRID_WIRE
00619          - DM_GRID_SURF
00620          
00621         @param id surface id (<= 0 for all)
00622         @param style draw style
00623         
00624         @return 1 on success
00625         @return -1 surface not found
00626         @return -2 setting attributes failed
00627         """
00628         Debug.msg(3, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
00629                   id, style)
00630         
00631         if id > 0:
00632             if not GS_surf_exists(id):
00633                 return -1
00634             
00635             if GS_set_drawmode(id, style) < 0:
00636                 return -2
00637             
00638             return 1
00639         
00640         if GS_setall_drawmode(style) < 0:
00641             return -2
00642         
00643         return 1
00644     
00645     def SetWireColor(self, id, color_str):
00646         """!Set color of wire
00647         
00648         @todo all
00649          
00650         @param surface id (< 0 for all)
00651         @param color color string (R:G:B)
00652         
00653         @return 1 on success
00654         @return -1 surface not found
00655         @return -2 setting attributes failed
00656         @return 1 on success
00657         @return 0 on failure
00658         """
00659         Debug.msg(3, "Nviz::SetWireColor(): id=%d, color=%s",
00660                   id, color_str)
00661         
00662         color = Nviz_color_from_str(color_str)
00663         
00664         if id > 0:
00665             if not GS_surf_exists(id):
00666                 return -1
00667             
00668             GS_set_wire_color(id, color)
00669         else:
00670             nsurfs = c_int()
00671             surf_list = GS_get_surf_list(byref(nsurfs))
00672             for i in xrange(nsurfs.value):
00673                 id = surf_list[i]
00674                 GS_set_wire_color(id, color)
00675             
00676             G_free(surf_list)
00677             surf_list = None
00678         
00679         return 1
00680     
00681     def GetSurfacePosition(self, id):
00682         """!Get surface position
00683         
00684         @param id surface id
00685         
00686         @return x,y,z
00687         @return zero-length vector on error
00688         """
00689         if not GS_surf_exists(id):
00690             return []
00691         
00692         x, y, z = c_float(), c_float(), c_float()
00693         GS_get_trans(id, byref(x), byref(y), byref(z))
00694         
00695         Debug.msg(3, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
00696                   id, x.value, y.value, z.value)
00697         
00698         return [x.value, y.value, z.value]
00699 
00700     def SetSurfacePosition(self, id, x, y, z):
00701         """!Set surface position
00702         
00703         @param id surface id
00704         @param x,y,z translation values
00705         
00706         @return 1 on success
00707         @return -1 surface not found
00708         @return -2 setting position failed
00709         """
00710         if not GS_surf_exists(id):
00711             return -1
00712         
00713         Debug.msg(3, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
00714                   id, x, y, z)
00715         
00716         GS_set_trans(id, x, y, z)
00717         
00718         return 1
00719 
00720     def SetVectorLineMode(self, id, color_str, width, flat):
00721         """!Set mode of vector line overlay
00722         
00723         @param id vector id
00724         @param color_str color string
00725         @param width line width
00726         @param flat display flat or on surface
00727         
00728         @return -1 vector set not found
00729         @return -2 on failure
00730         @return 1 on success
00731         """
00732         if not GV_vect_exists(id):
00733             return -1
00734         
00735         Debug.msg(3, "Nviz::SetVectorMode(): id=%d, color=%s, width=%d, flat=%d",
00736                   id, color_str, width, flat)
00737         
00738         color = Nviz_color_from_str(color_str)
00739         
00740         # use memory by default
00741         if GV_set_vectmode(id, 1, color, width, flat) < 0:
00742             return -2
00743         
00744         return 1
00745 
00746     def SetVectorLineHeight(self, id, height):
00747         """!Set vector height above surface (lines)
00748         
00749         @param id vector set id
00750         @param height
00751         
00752         @return -1 vector set not found
00753         @return 1 on success
00754         """
00755         if not GV_vect_exists(id):
00756             return -1
00757         
00758         Debug.msg(3, "Nviz::SetVectorLineHeight(): id=%d, height=%f",
00759                   id, height)
00760         
00761         GV_set_trans(id, 0.0, 0.0, height)
00762         
00763         return 1
00764 
00765     def SetVectorLineSurface(self, id, surf_id):
00766         """!Set reference surface of vector set (lines)
00767         
00768         @param id vector set id
00769         @param surf_id surface id
00770         
00771         @return 1 on success
00772         @return -1 vector set not found
00773         @return -2 surface not found
00774         @return -3 on failure
00775         """
00776         if not GV_vect_exists(id):
00777             return -1
00778         
00779         if not GS_surf_exists(surf_id):
00780             return -2
00781         
00782         if GV_select_surf(id, surf_id) < 0:
00783             return -3
00784         
00785         return 1
00786 
00787     def SetVectorPointMode(self, id, color_str, width, size, marker):
00788         """!Set mode of vector point overlay
00789         
00790         @param id vector id
00791         @param color_str color string
00792         @param width line width
00793         @param flat
00794         
00795         @return -1 vector set not found
00796         """
00797         if not GP_site_exists(id):
00798             return -1
00799         
00800         Debug.msg(3, "Nviz::SetVectorPointMode(): id=%d, color=%s, "
00801                   "width=%d, size=%f, marker=%d",
00802                   id, color_str, width, size, marker)
00803         
00804         color = Nviz_color_from_str(color_str)
00805         
00806         ### TODO
00807         # if GP_set_style(id, color, width, size, marker) < 0:
00808         #    return -2
00809         
00810         return 1
00811 
00812     def SetVectorPointHeight(self, id, height):
00813         """!Set vector height above surface (points)
00814         
00815         @param id vector set id
00816         @param height
00817         
00818         @return -1 vector set not found
00819         @return 1 on success
00820         """
00821         if not GP_site_exists(id):
00822             return -1
00823         
00824         Debug.msg(3, "Nviz::SetVectorPointHeight(): id=%d, height=%f",
00825                   id, height)
00826         
00827         GP_set_trans(id, 0.0, 0.0, height)
00828         
00829         return 1
00830 
00831     def SetVectorPointSurface(self, id, surf_id):
00832         """!Set reference surface of vector set (points)
00833         
00834         @param id vector set id
00835         @param surf_id surface id
00836         
00837         @return 1 on success
00838         @return -1 vector set not found
00839         @return -2 surface not found
00840         @return -3 on failure
00841         """
00842         if not GP_site_exists(id):
00843             return -1
00844         
00845         if not GS_surf_exists(surf_id):
00846             return -2
00847         
00848         if GP_select_surf(id, surf_id) < 0:
00849             return -3
00850         
00851         return 1
00852 
00853     def AddIsosurface(self, id, level):
00854         """!Add new isosurface
00855         
00856         @param id volume id
00857         @param level isosurface level (topography)
00858         
00859         @return -1 on failure
00860         @return 1 on success
00861         """
00862         if not GVL_vol_exists(id):
00863             return -1
00864         
00865         if GVL_isosurf_add(id) < 0:
00866             return -1
00867         
00868         # set topography level
00869         nisosurfs = GVL_isosurf_num_isosurfs(id)
00870         
00871         return GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level)
00872     
00873     def DeleteIsosurface(self, id, isosurf_id):
00874         """!Delete isosurface
00875         
00876         @param id volume id
00877         @param isosurf_id isosurface id
00878         
00879         @return 1 on success
00880         @return -1 volume not found
00881         @return -2 isosurface not found
00882         @return -3 on failure
00883         """
00884         if not GVL_vol_exists(id):
00885             return -1
00886         
00887         if isosurf_id > GVL_isosurf_num_isosurfs(id):
00888             return -2
00889         
00890         ret = GVL_isosurf_del(id, isosurf_id)
00891         
00892         if ret < 0:
00893             return -3
00894 
00895         return 1
00896     
00897     def MoveIsosurface(self, id, isosurf_id, up):
00898         """!Move isosurface up/down in the list
00899         
00900         @param id volume id
00901         @param isosurf_id isosurface id
00902         @param up if true move up otherwise down
00903         
00904         @return 1 on success
00905         @return -1 volume not found
00906         @return -2 isosurface not found
00907         @return -3 on failure
00908         """
00909         if not GVL_vol_exists(id):
00910             return -1
00911         
00912         if isosurf_id > GVL_isosurf_num_isosurfs(id):
00913             return -2
00914         
00915         if up:
00916             ret = GVL_isosurf_move_up(id, isosurf_id)
00917         else:
00918             ret = GVL_isosurf_move_down(id, isosurf_id)
00919         
00920         if ret < 0:
00921             return -3
00922 
00923         return 1
00924 
00925     def SetIsosurfaceColor(self, id, isosurf_id, map, value):
00926         """!Set isosurface color
00927         
00928         @param id volume id
00929         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
00930         @param map if true use map otherwise constant
00931         @param value map name of value
00932         
00933         @return 1 on success
00934         @return -1 volume not found
00935         @return -2 isosurface not found
00936         @return -3 on failure
00937         """
00938         return self.SetIsosurfaceAttr(id, isosurf_id, ATT_COLOR, map, value)
00939     
00940     def SetIsosurfaceMask(self, id, isosurf_id, invert, value):
00941         """!Set isosurface mask
00942         
00943         @todo invert
00944         
00945         @param id volume id
00946         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
00947         @param invert true for invert mask
00948         @param value map name to be used for mask
00949         
00950         @return 1 on success
00951         @return -1 volume not found
00952         @return -2 isosurface not found
00953         @return -3 on failure
00954         """
00955         return self.SetIsosurfaceAttr(id, isosurf_id, ATT_MASK, true, value)
00956     
00957     def SetIsosurfaceTransp(self, id, isosurf_id, map, value):
00958         """!Set isosurface transparency
00959         
00960         @param id volume id
00961         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
00962         @param map if true use map otherwise constant
00963         @param value map name of value
00964         
00965         @return 1 on success
00966         @return -1 volume not found
00967         @return -2 isosurface not found
00968         @return -3 on failure
00969         """
00970         return self.SetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP, map, value)
00971     
00972     def SetIsosurfaceShine(self, id, isosurf_id, map, value):
00973         """!Set isosurface shininess
00974         
00975         @param id volume id
00976         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
00977         @param map if true use map otherwise constant
00978         @param value map name of value
00979         
00980         @return 1 on success
00981         @return -1 volume not found
00982         @return -2 isosurface not found
00983         @return -3 on failure
00984         """
00985         return self.SetIsosurfaceAttr(id, isosurf_id, ATT_SHINE, map, value)
00986     
00987     def SetIsosurfaceEmit(self, id, isosurf_id, map, value):
00988         """!Set isosurface emission
00989         
00990         @param id volume id
00991         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
00992         @param map if true use map otherwise constant
00993         @param value map name of value
00994         
00995         @return 1 on success
00996         @return -1 volume not found
00997         @return -2 isosurface not found
00998         @return -3 on failure
00999         """
01000         return self.SetIsosurfaceAttr(id, isosurf_id, ATT_EMIT, map, value)
01001     
01002     def SetIsosurfaceAttr(self, id, isosurf_id, attr, map, value):
01003         """!Set isosurface attribute
01004         
01005         @param id volume id
01006         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
01007         @param attr attribute desc
01008         @param map if true use map otherwise constant
01009         @param value map name of value
01010         
01011         @return 1 on success
01012         @return -1 volume not found
01013         @return -2 isosurface not found
01014         @return -3 setting attributes failed
01015         """
01016         if not GVL_vol_exists(id):
01017             return -1
01018         
01019         if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
01020             return -2
01021         
01022         if map:
01023             ret = GVL_isosurf_set_att_map(id, isosurf_id, attr, value)
01024         else:
01025             if attr == ATT_COLOR:
01026                 val = Nviz_color_from_str(value)
01027             else:
01028                 val = float(value)
01029             
01030             ret = GVL_isosurf_set_att_const(id, isosurf_id, attr, val)
01031         
01032         Debug.msg(3, "Nviz::SetIsosurfaceAttr(): id=%d, isosurf=%d, "
01033                   "attr=%d, map=%d, value=%s",
01034                   id, isosurf_id, attr, map, value)
01035         
01036         if ret < 0:
01037             return -2
01038         
01039         return 1
01040     
01041     def UnsetIsosurfaceMask(self, id, isosurf_id):
01042         """!Unset isosurface mask
01043         
01044         @param id volume id
01045         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
01046         
01047         @return 1 on success
01048         @return -1 volume not found
01049         @return -2 isosurface not found
01050         @return -3 setting attributes failed
01051         """
01052         return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_MASK)
01053     
01054     def UnsetIsosurfaceTransp(self, id, isosurf_id):
01055         """!Unset isosurface transparency
01056         
01057         @param id volume id
01058         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
01059         
01060         @return 1 on success
01061         @return -1 volume not found
01062         @return -2 isosurface not found
01063         @return -3 setting attributes failed
01064         """
01065         return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_TRANSP)
01066     
01067     def UnsetIsosurfaceEmit(self, id, isosurf_id):
01068         """!Unset isosurface emission
01069         
01070         @param id volume id
01071         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
01072         
01073         @return 1 on success
01074         @return -1 volume not found
01075         @return -2 isosurface not found
01076         @return -3 setting attributes failed
01077         """
01078         return self.UnsetIsosurfaceAttr(id, isosurf_id, ATT_EMIT)
01079     
01080     def UnsetIsosurfaceAttr(self, id, isosurf_id, attr):
01081         """!Unset surface attribute
01082         
01083         @param id surface id
01084         @param isosurf_id isosurface id (0 - MAX_ISOSURFS)
01085         @param attr attribute descriptor
01086         
01087         @return 1 on success
01088         @return -1 volume not found
01089         @return -2 isosurface not found
01090         @return -2 on failure
01091         """
01092         if not GVL_vol_exists(id):
01093             return -1
01094         
01095         if isosurf_id > GVL_isosurf_num_isosurfs(id) - 1:
01096             return -2
01097         
01098         Debug.msg(3, "Nviz::UnsetSurfaceAttr(): id=%d, isosurf_id=%d, attr=%d",
01099                   id, isosurf_id, attr)
01100         
01101         ret = GVL_isosurf_unset_att(id, isosurf_id, attr)
01102         
01103         if ret < 0:
01104             return -2
01105         
01106         return 1
01107 
01108     def SetIsosurfaceMode(self, id, mode):
01109         """!Set draw mode for isosurfaces
01110         
01111         @param mode
01112         
01113         @return 1 on success
01114         @return -1 volume set not found
01115         @return -2 on failure
01116         """
01117         if not GVL_vol_exists(id):
01118             return -1
01119         
01120         ret = GVL_isosurf_set_drawmode(id, mode)
01121         
01122         if ret < 0:
01123             return -2
01124         
01125         return 1
01126     
01127     def SetIsosurfaceRes(self, id, res):
01128         """!Set draw resolution for isosurfaces
01129         
01130         @param res resolution value
01131         
01132         @return 1 on success
01133         @return -1 volume set not found
01134         @return -2 on failure
01135         """
01136         if not GVL_vol_exists(id):
01137             return -1
01138         
01139         ret = GVL_isosurf_set_drawres(id, res, res, res)
01140         
01141         if ret < 0:
01142             return -2
01143         
01144         return 1
01145 
01146     def SaveToFile(self, filename, width = 20, height = 20, itype = 'ppm'):
01147         """!Save current GL screen to ppm/tif file
01148 
01149         @param filename file name
01150         @param width image width
01151         @param height image height
01152         @param itype image type ('ppm' or 'tif')
01153         """
01154         widthOrig  = self.width
01155         heightOrig = self.height
01156         
01157         self.ResizeWindow(width, height)
01158         GS_clear(Nviz_get_bgcolor(self.data))
01159         self.Draw(False, -1)
01160         if itype == 'ppm':
01161             GS_write_ppm(filename)
01162         else:
01163             GS_write_tif(filename)
01164         
01165         self.ResizeWindow(widthOrig, heightOrig)
01166 
01167     def DrawLightingModel(self):
01168         """!Draw lighting model"""
01169         if self.showLight:
01170             GS_draw_lighting_model()
01171 
01172     def SetFringe(self, sid, color, elev, nw = False, ne = False, sw = False, se = False):
01173         """!Set fringe
01174 
01175         @param sid surface id
01176         @param color color
01177         @param elev elevation (height)
01178         @param nw,ne,sw,se fringe edges (turn on/off)
01179         """
01180         scolor = str(color[0]) + ':' + str(color[1]) + ':' + str(color[2])
01181 
01182         Nviz_set_fringe(self.data,
01183                         sid, Nviz_color_from_str(scolor),
01184                         elev, int(nw), int(ne), int(sw), int(se))
01185         
01186     def GetPointOnSurface(self, sx, sy):
01187         """!Get point on surface
01188 
01189         @param sx,sy canvas coordinates (LL)
01190         """
01191         sid = c_int()
01192         x   = c_float()
01193         y   = c_float()
01194         z   = c_float()
01195         Debug.msg(5, "GLWindow.GetPointOnSurface(): sx=%d sy=%d" % (sx, sy))
01196         num = GS_get_selected_point_on_surface(sx, sy, byref(sid), byref(x), byref(y), byref(z))
01197         if num == 0:
01198             return (None, None, None, None)
01199         
01200         return (sid.value, x.value, y.value, z.value)
01201 
01202     def QueryMap(self, sx, sy):
01203         """!Query surface map
01204 
01205         @param sx,sy canvas coordinates (LL)
01206         """
01207         sid, x, y, z = self.GetPointOnSurface(sx, sy)
01208         if not sid:
01209             return None
01210         
01211         catstr = create_string_buffer(256)
01212         valstr = create_string_buffer(256)
01213         GS_get_cat_at_xy(sid, ATT_TOPO, catstr, x, y)
01214         GS_get_val_at_xy(sid, ATT_COLOR, valstr, x, y)
01215         
01216         return { 'id' : sid,
01217                  'x'  : x,
01218                  'y'  : y,
01219                  'z'  : z,
01220                  'elevation' : catstr.value.replace('(', '').replace(')', ''),
01221                  'color'     : valstr.value }
01222     
01223     def GetDistanceAlongSurface(self, sid, p1, p2, useExag = True):
01224         """!Get distance measured along surface"""
01225         d = c_float()
01226         
01227         GS_get_distance_alongsurf(sid, p1[0], p1[1], p2[0], p2[1],
01228                                   byref(d), int(useExag))
01229         
01230         return d.value
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines