Glyph.py :  » Game-2D-3D » MayaVi » MayaVi-1.5 » Modules » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » MayaVi 
MayaVi » MayaVi 1.5 » Modules » Glyph.py
"""

This module displays glyphs scaled and colored as per the input data.
This will work for any dataset and can be used for both scalar and
vector data.

This code is distributed under the conditions of the BSD license.  See
LICENSE.txt for details.

Copyright (c) 2003, Prabhu Ramachandran.
"""

__author__ = "Prabhu Ramachandran <prabhu_r@users.sf.net>"
__version__ = "$Revision: 1.5 $"
__date__ = "$Date: 2005/08/02 18:30:13 $"

import Base.Objects, Common
import Tkinter, tkColorChooser, math
import vtk
import vtkPipeline.vtkMethodParser
import vtkPipeline.ConfigVtkObj

debug = Common.debug


class Glyph (Base.Objects.Module):

    """ This module displays glyphs scaled and colored as per the
    input data.  This will work for any dataset and can be used for
    both scalar and vector data. """

    def __init__ (self, mod_m): 
        debug ("In Glyph::__init__ ()")
        Common.state.busy ()
        Base.Objects.Module.__init__ (self, mod_m)
        self.glyph_src = vtk.vtkGlyphSource2D ()
        self.glyph = vtk.vtkGlyph3D ()
        self.mapper = self.map = vtk.vtkPolyDataMapper ()
        self.actor = self.act = vtk.vtkActor ()
        self.data_out = self.mod_m.GetOutput ()
        
        self._initialize ()
        self._gui_init ()
        self.renwin.Render ()
        Common.state.idle ()

    def __del__ (self): 
        debug ("In Glyph::__del__ ()")
        if self.actor:
            self.renwin.remove_actors (self.act)
        self.renwin.Render ()

    def _initialize (self):
        debug ("In Glyph::_initialize ()")
        self.glyph_src.SetGlyphTypeToArrow ()
        self.glyph_src.SetFilled (1)

        self.glyph.SetInput (self.mod_m.GetOutput ())
        self.glyph.SetSource (self.glyph_src.GetOutput ())
        self.glyph.SetScaleModeToScaleByScalar ()
        self.glyph.SetColorModeToColorByScalar ()
        self.glyph.SetClamping (1)
        dr = self.mod_m.get_scalar_data_range ()
        self.glyph.SetRange (dr)

        self.map.SetInput (self.glyph.GetOutput ())
        self.act.SetMapper (self.map)
        self.map.SetScalarRange (dr)
        self.map.SetLookupTable (self.mod_m.get_scalar_lut ())
        self.act.GetProperty ().SetLineWidth (2)
        self.act.GetProperty ().BackfaceCullingOff ()
        self.act.GetProperty ().FrontfaceCullingOff ()
        self.act.GetProperty ().SetColor (*Common.config.fg_color)
        self.center = self.data_out.GetCenter ()
        self.renwin.add_actors (self.act)
        # used for the pipeline browser
        self.pipe_objs = self.actor
        
    def _gui_init (self): 
        debug ("In Glyph::_gui_init ()")
        self.root = None
        self.glyph_gui = None
        self.glyph_var = Tkinter.IntVar ()
        self.glyph_var.set (0)

    def SetInput (self, source): 
        debug ("In Glyph::SetInput ()")
        Common.state.busy ()
        self.data_out = source
        self.glyph.SetInput (self.data_out)
        self.update_color_mode()
        Common.state.idle ()

    def save_config (self, file): 
        debug ("In Glyph::save_config ()")
        file.write ("%d\n"%(self.glyph_var.get ()))
        p = vtkPipeline.vtkMethodParser.VtkPickler ()
        for obj in (self.glyph, self.glyph_src,
                    self.map, self.act, self.act.GetProperty ()):
            p.dump (obj, file)

    def load_config (self, file):
        debug ("In Glyph::load_config ()")
        val = file.readline ()
        g_t = eval (val)
        self.glyph_var.set (g_t)
        self.set_glyph_mode ()
        p = vtkPipeline.vtkMethodParser.VtkPickler ()
        for obj in (self.glyph, self.glyph_src,
                    self.map, self.act, self.act.GetProperty ()):
            p.load (obj, file)

        self.update_color_mode()

    def config_changed (self): 
        debug ("In Glyph::config_changed ()")
        self.act.GetProperty ().SetColor (*Common.config.fg_color)

    def make_main_gui (self): 
        debug ("In Glyph::make_main_gui ()")
        CVOF = vtkPipeline.ConfigVtkObj.ConfigVtkObjFrame
        self.glyph_gui = gg = CVOF(self.root, self.renwin)
        t_m = ["ClampingOn", "OrientOn", "ScalingOn"]
        gs_m = ["Range", "ScaleFactor"]
        st_m = [['SetColorModeToColorByScalar',
                 'SetColorModeToColorByVector'],
                ['SetScaleModeToDataScalingOff',
                 'SetScaleModeToScaleByScalar',
                 'SetScaleModeToScaleByVector',
                 'SetScaleModeToScaleByVectorComponents'],
                ['SetVectorModeToUseNormal',
                 'SetVectorModeToUseVector',
                 'SetVectorModeToVectorRotationOff']]
        gg.configure(self.glyph, get=[], toggle=t_m, get_set=gs_m,
                     state=st_m, auto_update=1, one_frame=1)
        gg.set_update_method(self.update_color_mode)
        gg.pack(side='top', fill='both', expand=1)
        self.make_choice_gui ()

    def make_choice_gui (self):
        debug ("In Glyph::make_choice_gui ()")
        frame = Tkinter.Frame (self.root)
        frame.pack (side='top', fill='both', expand=1)        
        self.make_glyph_gui (frame)

    def make_glyph_gui (self, master):
        debug ("In Glyph::make_glyph_gui ()")        
        frame = Tkinter.Frame (master, relief='ridge', bd=2)
        frame.pack (side='left', fill='both', expand=1)
        rw = 0
        rb = Tkinter.Radiobutton (frame, text="2D Glyphs",
                                  variable=self.glyph_var, value=0,
                                  command=self.set_glyph_mode)
        rb.grid (row=rw, column=0, sticky='w')
        rw = rw + 1
        rb = Tkinter.Radiobutton (frame, text="Cone",
                                  variable=self.glyph_var, value=1,
                                  command=self.set_glyph_mode)
        rb.grid (row=rw, column=0, sticky='w')
        rw = rw + 1
        rb = Tkinter.Radiobutton (frame, text="Sphere",
                                  variable=self.glyph_var, value=2,
                                  command=self.set_glyph_mode)
        rb.grid (row=rw, column=0, sticky='w')
        rw = rw + 1
        rb = Tkinter.Radiobutton (frame, text="Cube",
                                  variable=self.glyph_var, value=3,
                                  command=self.set_glyph_mode)
        rb.grid (row=rw, column=0, sticky='w')
        rw = rw + 1
        rb = Tkinter.Radiobutton (frame, text="Cylinder",
                                  variable=self.glyph_var, value=4,
                                  command=self.set_glyph_mode)
        rb.grid (row=rw, column=0, sticky='w')
        rw = rw + 1
        rb = Tkinter.Radiobutton (frame, text="3D Arrow",
                                  variable=self.glyph_var, value=5,
                                  command=self.set_glyph_mode)
        rb.grid (row=rw, column=0, sticky='w')
        rw = rw + 1
        
        but = Tkinter.Button(frame, text="Configure Glyph Source",
                             underline=1,
                             command=self.config_glyph_src_gui)
        but.grid (row=rw, column=0, sticky='wens')
        self.root.bind('<Alt-o>', self.config_glyph_src_gui)
        rw = rw + 1
        
    def set_glyph_mode (self, event=None):
        debug ("In Glyph::set_glyph_mode ()")
        Common.state.busy ()
        val = self.glyph_var.get ()
        if val == 0: # 2d glyph 
            self.glyph_src = vtk.vtkGlyphSource2D ()
            self.glyph_src.SetGlyphTypeToArrow ()
        elif val == 1: # Cone
            self.glyph_src = vtk.vtkConeSource()
        elif val == 2: # Sphere
            self.glyph_src = vtk.vtkSphereSource()
            self.glyph_src.SetPhiResolution(4)
            self.glyph_src.SetThetaResolution(4)
        elif val == 3: # Cube
            self.glyph_src = vtk.vtkCubeSource()
        elif val == 4: # Cylinder
            self.glyph_src = vtk.vtkCylinderSource()
        elif val == 5: # 3D arrow
            self.glyph_src = vtk.vtkArrowSource()
        self.glyph.SetSource (self.glyph_src.GetOutput ())

        self.renwin.Render ()
        Common.state.idle ()

    def update_color_mode(self, event=None):
        cm = self.glyph.GetColorModeAsString()
        if cm == 'ColorByScalar':
            self.map.ScalarVisibilityOn ()
            dr = self.mod_m.get_scalar_data_range ()
            self.map.SetScalarRange (dr)
            self.glyph.SetRange(dr)
            self.map.SetLookupTable (self.mod_m.get_scalar_lut ())
        elif cm == 'ColorByVector':
            dr = self.mod_m.get_vector_data_range ()
            self.map.ScalarVisibilityOn ()
            self.map.SetScalarRange (dr)
            self.glyph.SetRange(dr)
            self.map.SetLookupTable (self.mod_m.get_vector_lut ())
        if self.root and self.glyph_gui and self.root.winfo_exists():
            self.glyph_gui.update_gui()        

    def config_glyph_src_gui(self, event=None):
        debug("In Glyph::config_glyph_src_gui()")
        conf = vtkPipeline.ConfigVtkObj.ConfigVtkObj (self.renwin)
        conf.configure (None, self.glyph_src, run_command=0)

    def close_gui(self, event=None):        
        """Called when the 'close' button is clicked.  Overridden to
        handle cyclic references."""
        debug ("In Glyph::close_gui ()")
        del self.glyph_gui
        Base.Objects.Module.close_gui(self, event)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.