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

Contents of /trunk/pyvisi/test/python/vtk/arrowPlot3D.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: 4815 byte(s)
example cleanup
1 # $Id: arrowPlot3D.py,v 1.1 2005/11/08 08:21:25 paultcochrane Exp $
2
3 """
4 Example of plotting a 3D vector field
5 """
6
7 # set up some data to plot
8 from Numeric import *
9
10 dim = 10
11
12 # initialise the positions of the vectors
13 x = zeros((dim,dim), typecode=Float)
14 y = zeros((dim,dim), typecode=Float)
15 z = zeros((dim,dim), typecode=Float)
16
17 # initialise the vector displacements
18 # (I may need to rethink how this works in the interface)
19 dx = zeros((dim,dim), typecode=Float)
20 dy = zeros((dim,dim), typecode=Float)
21 dz = zeros((dim,dim), typecode=Float)
22
23 # set the positions randomly, and set the displacements to some smaller
24 # random number but of mean zero instead of distributed between 0 and 1
25 import random
26 random.seed()
27 for i in range(dim):
28 for j in range(dim):
29 x[i,j] = random.random()
30 y[i,j] = random.random()
31 z[i,j] = random.random()
32 dx[i,j] = (random.random()-0.5)/5.0
33 dy[i,j] = (random.random()-0.5)/5.0
34 dz[i,j] = (random.random()-0.5)/5.0
35
36 #### original vtk code
37 import vtk
38
39 # loading a vtk file as input
40 reader = vtk.vtkXMLUnstructuredGridReader()
41 reader.SetFileName("../../vel-0500.vtk")
42 reader.Update()
43
44 grid = reader.GetOutput()
45
46 # grab the model centre and bounds
47 centre = grid.GetCenter()
48 bounds = grid.GetBounds()
49
50 # grab the norm of the vectors
51 norm = vtk.vtkVectorNorm()
52 norm.SetInput(grid)
53
54 maxNorm = grid.GetPointData().GetVectors().GetMaxNorm()
55
56 # to make arrow glyphs need an arrow source
57 arrow = vtk.vtkArrowSource()
58
59 # the arrows are 3D glyphs so set that up now
60 glyph = vtk.vtkGlyph3D()
61 glyph.ScalingOn()
62 glyph.SetScaleModeToScaleByScalar()
63 glyph.SetColorModeToColorByScalar()
64 glyph.SetVectorModeToUseVector()
65 glyph.SetScaleFactor(0.1/maxNorm)
66 glyph.SetInput(norm.GetOutput())
67 glyph.SetSource(arrow.GetOutput())
68 glyph.ClampingOff()
69
70 # set up a stripper to speed up rendering
71 stripper = vtk.vtkStripper()
72 stripper.SetInput(glyph.GetOutput())
73
74 # make a lookup table for the colour map and invert it (colours look
75 # better when it's inverted)
76 lut = vtk.vtkLookupTable()
77 refLut = vtk.vtkLookupTable()
78 lut.Build()
79 refLut.Build()
80 for j in range(256):
81 lut.SetTableValue(j, refLut.GetTableValue(255-j))
82
83 # set up the mapper
84 mapper = vtk.vtkPolyDataMapper()
85 mapper.SetInput(stripper.GetOutput())
86 mapper.SetScalarRange(0,maxNorm)
87 mapper.SetLookupTable(lut)
88
89 # set up the actor
90 actor = vtk.vtkActor()
91 actor.SetMapper(mapper)
92
93 # set up the text properties for nice text
94 font_size = 20
95 textProp = vtk.vtkTextProperty()
96 textProp.SetFontSize(font_size)
97 textProp.SetFontFamilyToArial()
98 textProp.BoldOff()
99 textProp.ItalicOff()
100 textProp.ShadowOff()
101 textProp.SetColor(0.0, 0.0, 0.0)
102
103 # make a title
104 title = vtk.vtkTextMapper()
105 title.SetInput("Example 3D arrow/quiver/vector field plot")
106
107 # make the title text use the text properties
108 titleProp = title.GetTextProperty()
109 titleProp.ShallowCopy(textProp)
110 titleProp.SetJustificationToCentered()
111 titleProp.SetVerticalJustificationToTop()
112 titleProp.BoldOn()
113
114 # make the actor for the title
115 titleActor = vtk.vtkTextActor()
116 titleActor.SetMapper(title)
117 titleActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
118 titleActor.GetPositionCoordinate().SetValue(0.5, 0.95)
119
120 # put an outline around the data
121 outline = vtk.vtkOutlineSource()
122 outline.SetBounds(bounds)
123
124 # make its mapper
125 outlineMapper = vtk.vtkPolyDataMapper()
126 outlineMapper.SetInput(outline.GetOutput())
127
128 # make its actor
129 outlineActor = vtk.vtkActor()
130 outlineActor.SetMapper(outlineMapper)
131 outlineActor.GetProperty().SetColor(0,0,0)
132
133 # set up the renderer and render window
134 ren = vtk.vtkRenderer()
135 renWin = vtk.vtkRenderWindow()
136
137 renWin.SetSize(800,600)
138 renWin.AddRenderer(ren)
139 ren.SetBackground(1,1,1)
140
141 # add the relevant actors
142 ren.AddActor(actor)
143 ren.AddActor(titleActor)
144 ren.AddActor(outlineActor)
145
146 cam = ren.GetActiveCamera()
147 #cam.Azimuth(0)
148 #cam.Elevation(-90)
149 cam.Zoom(1.2)
150 ren.SetActiveCamera(cam)
151 ren.ResetCameraClippingRange()
152
153 # add some axes
154 axes = vtk.vtkCubeAxesActor2D()
155 axes.SetInput(grid)
156 axes.SetCamera(ren.GetActiveCamera())
157 axes.SetLabelFormat("%6.4g")
158 axes.SetFlyModeToOuterEdges()
159 axes.SetFontFactor(0.8)
160 axes.SetAxisTitleTextProperty(textProp)
161 axes.SetAxisLabelTextProperty(textProp)
162 axes.SetXLabel("x")
163 axes.SetYLabel("y")
164 axes.SetZLabel("z")
165 axes.SetNumberOfLabels(5)
166 axes.GetProperty().SetColor(0,0,0)
167 ren.AddProp(axes)
168
169 # set up stuff for interactive viewing
170 iren = vtk.vtkRenderWindowInteractor()
171 iren.SetRenderWindow(renWin)
172
173 iren.Initialize()
174 renWin.Render()
175 iren.Start()
176
177 # the WindowToImageFilter is what one uses to save the window to an
178 # image file
179 win2img = vtk.vtkWindowToImageFilter()
180 win2img.SetInput(renWin)
181
182 # set up the PNMWriter as we're saving to png
183 writer = vtk.vtkPNGWriter()
184 writer.SetFileName("arrowPlot3D.png")
185 writer.SetInput(win2img.GetOutput())
186 writer.Write()
187
188 # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26