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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations)
Mon Mar 20 02:23:14 2006 UTC (13 years, 7 months ago) by gross
File MIME type: text/x-python
File size: 5020 byte(s)
example cleanup
1 # $Id: surfacePlot.py,v 1.1 2005/11/08 07:26:31 paultcochrane Exp $
2
3 """
4 Example of plotting surfaces
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 #### original vtk code
25
26 import vtk
27
28 # set up the points
29 _points = vtk.vtkPoints()
30 _points.SetNumberOfPoints(len(x))
31 _index = 0
32 for i in range(len(x)):
33 for j in range(len(y)):
34 _points.InsertPoint(_index, x[i], y[j], 0)
35 _index += 1
36
37 # create the data
38 _data = vtk.vtkFloatArray()
39 _data.SetNumberOfComponents(1)
40 _data.SetNumberOfValues(len(x)*len(y))
41 _index = 0
42 for i in range(len(x)):
43 for j in range(len(y)):
44 _data.InsertValue(_index, z[i][j])
45 _index += 1
46
47 # set up the grid (it's polydata since we're doing a Delaunay2D)
48 _grid = vtk.vtkPolyData()
49 _grid.SetPoints(_points)
50 _grid.GetPointData().SetScalars(_data)
51
52 # calculate the min and max of the z data
53 _zMin = min(z.flat)
54 _zMax = max(z.flat)
55
56 # make a lookup table for the colour map and invert it (colours look
57 # better when it's inverted)
58 _lut = vtk.vtkLookupTable()
59 _refLut = vtk.vtkLookupTable()
60 _lut.Build()
61 _refLut.Build()
62 for i in range(256):
63 _lut.SetTableValue(i, _refLut.GetTableValue(255-i))
64
65 # triangulate them
66 _delaunay = vtk.vtkDelaunay2D()
67 _delaunay.SetInput(_grid)
68 _delaunay.SetTolerance(0.001)
69
70 # warp the surface to generate the surface or "carpet"
71 _warp = vtk.vtkWarpScalar()
72 _warp.SetInput(_delaunay.GetOutput())
73 _warp.SetScaleFactor(1.0)
74
75 # set up the mapper
76 _mapper = vtk.vtkPolyDataMapper()
77 _mapper.SetInput(_warp.GetOutput())
78 _mapper.SetLookupTable(_lut)
79 _mapper.SetScalarRange(_zMin, _zMax)
80
81 # set up the actor
82 _plotActor = vtk.vtkActor()
83 _plotActor.SetMapper(_mapper)
84
85 # set up the text properties for nice text
86 _font_size = 20
87 _textProp = vtk.vtkTextProperty()
88 _textProp.SetFontSize(_font_size)
89 _textProp.SetFontFamilyToArial()
90 _textProp.BoldOff()
91 _textProp.ItalicOff()
92 _textProp.ShadowOff()
93
94 # use a scalar bar
95 _scalarBar = vtk.vtkScalarBarActor()
96 _scalarBar.SetLookupTable(_lut)
97 _scalarBar.SetWidth(0.1)
98 _scalarBar.SetHeight(0.7)
99 _scalarBar.SetPosition(0.9, 0.2)
100 _scalarBar.SetTitle("z") # don't need this I don't think
101
102 # make a title
103 _title = vtk.vtkTextMapper()
104 _title.SetInput("Example surface plot")
105
106 # make the title text use the text properties
107 _titleProp = _title.GetTextProperty()
108 _titleProp.ShallowCopy(_textProp)
109 _titleProp.SetJustificationToCentered()
110 _titleProp.SetVerticalJustificationToTop()
111
112 # make the actor for the title
113 _titleActor = vtk.vtkTextActor()
114 _titleActor.SetMapper(_title)
115 _titleActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
116 _titleActor.GetPositionCoordinate().SetValue(0.5, 0.95)
117
118 # set up the renderer and the render window
119 _ren = vtk.vtkRenderer()
120 _renWin = vtk.vtkRenderWindow()
121 _renWin.AddRenderer(_ren)
122 _renWin.SetSize(640,480)
123
124 # add the actor to the renderer
125 _ren.AddActor(_plotActor)
126 _ren.AddActor(_scalarBar)
127 _ren.AddActor(_titleActor)
128
129 # set up the camera, and associate the text with the camera
130 _camera = _ren.GetActiveCamera()
131 _gridCentre = _grid.GetCenter()
132 _camera.SetFocalPoint(_gridCentre)
133 _camPos = _camera.GetPosition()
134 _pos = (_camPos[0] - _gridCentre[0],
135 _camPos[1] - _gridCentre[1],
136 _camPos[2] - _gridCentre[2])
137 _radius = sqrt(_pos[0]*_pos[0] + _pos[1]*_pos[1] + _pos[2]*_pos[2])
138
139 # these are the matlab defaults... they look ok, so use them
140 # note that azimuth and elevation mean slightly different things in vtk
141 # to what they mean in matlab: they aren't absolute, they are relative
142 # to the current position
143 _azimuth = -37.5
144 _elevation = 30
145
146 # take the position where I am as being 0 azimuth, and 90 elevation
147
148 # position the camera appropriately
149 _xPos = _gridCentre[0] + _radius*sin(math.pi*_azimuth/180.0)
150 _yPos = _gridCentre[1] - _radius*cos(math.pi*_elevation/180.0) \
151 + _radius*cos(math.pi*_azimuth/180.0)
152 _zPos = _gridCentre[2] + _radius - _radius*sin(math.pi*_elevation/180.0)
153
154 _camera.SetPosition(_xPos, _yPos, _zPos)
155
156 _camera.SetViewUp(0,0,1)
157
158 _ren.SetActiveCamera(_camera)
159
160 _ren.ResetCameraClippingRange()
161
162 # add some axes
163 _axes = vtk.vtkCubeAxesActor2D()
164 _axes.SetInput(_warp.GetOutput())
165 _axes.SetCamera(_ren.GetActiveCamera())
166 _axes.SetLabelFormat("%6.2g")
167 _axes.SetFlyModeToOuterEdges()
168 _axes.SetFontFactor(0.8)
169 _axes.SetAxisTitleTextProperty(_textProp)
170 _axes.SetAxisLabelTextProperty(_textProp)
171 _axes.SetXLabel("x")
172 _axes.SetYLabel("y")
173 _axes.SetZLabel("z")
174 _axes.SetNumberOfLabels(5)
175 _ren.AddProp(_axes)
176
177 # render the scene
178 _iren = vtk.vtkRenderWindowInteractor()
179 _iren.SetRenderWindow(_renWin)
180 _iren.Initialize()
181 _renWin.Render()
182 _iren.Start()
183
184 # vim: expandtab shiftwidth=4:
185

  ViewVC Help
Powered by ViewVC 1.1.26