/[escript]/trunk/pyvisi/test/python/vtk/contourPlot.py
ViewVC logotype

Contents of /trunk/pyvisi/test/python/vtk/contourPlot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations)
Mon Mar 20 02:23:14 2006 UTC (13 years, 6 months ago) by gross
File MIME type: text/x-python
File size: 4257 byte(s)
example cleanup
1 # $Id: contourPlot.py,v 1.1 2005/09/21 01:31:22 paultcochrane Exp $
2
3 """
4 Example of contour plotting with pyvisi - vtk specific code
5 """
6
7 # set up some data to plot
8 from Numeric import *
9
10 # the x and y axes
11 x = arange(-2,2,0.2, typecode=Float)
12 y = arange(-2,3,0.2, typecode=Float)
13
14 # pick some interesting function to generate the data in the third dimension
15 # this is the one used in the matlab docs: z = x*exp(-x^2-y^2)
16 z = zeros((len(x),len(y)), typecode=Float)
17
18 # boy do *I* feel old fashioned writing it this way
19 # surely there's another way to do it: - something to do later
20 for i in range(len(x)):
21 for j in range(len(y)):
22 z[i,j] = x[i]*exp(-x[i]*x[i] - y[j]*y[j])
23
24
25 import vtk
26
27 interactive = True
28
29 if not interactive:
30 _factGraphics = vtk.vtkGraphicsFactory()
31 _factGraphics.SetUseMesaClasses(1)
32 _factImage = vtk.vtkImagingFactory()
33 _factImage.SetUseMesaClasses(1)
34
35 # set up the renderer and the render window
36 _ren = vtk.vtkRenderer()
37 _renWin = vtk.vtkRenderWindow()
38 _renWin.AddRenderer(_ren)
39 _renWin.SetSize(640,480)
40 if not interactive:
41 _renWin.OffScreenRenderingOn()
42
43 # set up the points
44 _points = vtk.vtkPoints()
45 _points.SetNumberOfPoints(len(x))
46 _index = 0
47 for i in range(len(x)):
48 for j in range(len(y)):
49 _points.InsertPoint(_index, x[i], y[j], 0)
50 _index += 1
51
52 # create the data
53 _data = vtk.vtkFloatArray()
54 _data.SetNumberOfComponents(1)
55 _data.SetNumberOfValues(len(x)*len(y))
56 _index = 0
57 for i in range(len(x)):
58 for j in range(len(y)):
59 _data.InsertValue(_index, z[i][j])
60 _index += 1
61
62 # set up the grid (it's polydata since we're doing a Delaunay2D)
63 _grid = vtk.vtkPolyData()
64 _grid.SetPoints(_points)
65 _grid.GetPointData().SetScalars(_data)
66
67 # calculate the min and max of the z data
68 _zMin = min(z.flat)
69 _zMax = max(z.flat)
70
71 # make a lookup table for the colour map and invert it (colours look
72 # better when it's inverted)
73 _lut = vtk.vtkLookupTable()
74 _refLut = vtk.vtkLookupTable()
75 _lut.Build()
76 _refLut.Build()
77 for i in range(256):
78 _lut.SetTableValue(i, _refLut.GetTableValue(255-i))
79
80 # triangulate them
81 _delaunay = vtk.vtkDelaunay2D()
82 _delaunay.SetInput(_grid)
83 _delaunay.SetTolerance(0.001)
84
85 # set up the mapper
86 _mapper = vtk.vtkPolyDataMapper()
87 _mapper.SetInput(_delaunay.GetOutput())
88 _mapper.SetLookupTable(_lut)
89 _mapper.SetScalarRange(_zMin, _zMax)
90
91 # set up the actor
92 _plotActor = vtk.vtkActor()
93 _plotActor.SetMapper(_mapper)
94
95 # set up the text properties for nice text
96 _font_size = 20
97 _textProp = vtk.vtkTextProperty()
98 _textProp.SetFontSize(_font_size)
99 _textProp.SetFontFamilyToArial()
100 _textProp.BoldOff()
101 _textProp.ItalicOff()
102 _textProp.ShadowOff()
103
104 # use a scalar bar
105 _scalarBar = vtk.vtkScalarBarActor()
106 _scalarBar.SetLookupTable(_lut)
107 _scalarBar.SetWidth(0.1)
108 _scalarBar.SetHeight(0.7)
109 _scalarBar.SetPosition(0.9, 0.2)
110 _scalarBar.SetTitle("z") # don't need this I don't think
111
112 # make a title
113 _title = vtk.vtkTextMapper()
114 _title.SetInput("Example shaded contour plot")
115
116 # make the title text use the text properties
117 _titleProp = _title.GetTextProperty()
118 _titleProp.ShallowCopy(_textProp)
119 _titleProp.SetJustificationToCentered()
120 _titleProp.SetVerticalJustificationToTop()
121
122 # make the actor for the title
123 _titleActor = vtk.vtkTextActor()
124 _titleActor.SetMapper(_title)
125 _titleActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
126 _titleActor.GetPositionCoordinate().SetValue(0.5, 0.95)
127
128 # add the actor to the renderer
129 _ren.AddActor(_plotActor)
130 _ren.AddActor(_scalarBar)
131 _ren.AddActor(_titleActor)
132
133 # set up the camera, and associate the text with the camera
134 _camera = _ren.GetActiveCamera()
135 _ren.ResetCameraClippingRange()
136
137 # add some axes
138 _axes = vtk.vtkCubeAxesActor2D()
139 _axes.SetInput(_grid)
140 _axes.SetCamera(_ren.GetActiveCamera())
141 _axes.SetLabelFormat("%6.4g")
142 _axes.SetFlyModeToOuterEdges()
143 _axes.SetFontFactor(0.8)
144 _axes.SetAxisTitleTextProperty(_textProp)
145 _axes.SetAxisLabelTextProperty(_textProp)
146 _axes.SetXLabel("x")
147 _axes.SetYLabel("y")
148 _axes.YAxisVisibilityOff()
149 _axes.SetNumberOfLabels(5)
150 _ren.AddProp(_axes)
151
152 # render the scene
153 if interactive:
154 _iren = vtk.vtkRenderWindowInteractor()
155 _iren.SetRenderWindow(_renWin)
156 _iren.Initialize()
157 _renWin.Render()
158 _iren.Start()
159 else:
160 _renWin.Render()
161
162 # vim: expandtab shiftwidth=4:

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26