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

Contents of /trunk/pyvisi/test/python/vtk/ellipsoidPlot.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: 3696 byte(s)
example cleanup
1 # $Id: ellipsoidPlot.py,v 1.1 2005/09/21 00:37:33 paultcochrane Exp $
2
3 """
4 Example of plotting ellipsoids (useful for visualising tensors) with pyvisi
5 """
6
7 import vtk
8
9 # reverse the order of the colourmap (looks better)
10 lut = vtk.vtkLookupTable()
11 refLut = vtk.vtkLookupTable()
12 lut.Build()
13 refLut.Build()
14 for i in range(256):
15 lut.SetTableValue(i, refLut.GetTableValue(255-i))
16
17 # set up the renderer
18 ren = vtk.vtkRenderer()
19 renWin = vtk.vtkRenderWindow()
20 renWin.AddRenderer(ren)
21 renWin.SetSize(640,480)
22 ren.SetBackground(1,1,1) # white
23
24 # load a vtk file as input
25 reader = vtk.vtkXMLUnstructuredGridReader()
26 reader.SetFileName("stress22.vtk")
27 reader.Update()
28
29 # grab the grid of the data
30 grid = reader.GetOutput()
31
32 # convert the cell data to point data
33 c2p = vtk.vtkCellDataToPointData()
34 c2p.SetInput(grid)
35
36 # now extract the tensor components
37 extract = vtk.vtkExtractTensorComponents()
38 extract.SetInput(c2p.GetOutput())
39 extract.SetScalarModeToEffectiveStress()
40 extract.ExtractScalarsOn()
41 extract.PassTensorsToOutputOn()
42 extract.ScalarIsEffectiveStress()
43
44 extractGrid = extract.GetOutput()
45 extractGrid.Update()
46 extractScalarRange = extractGrid.GetPointData().GetScalars().GetRange()
47
48 # make a sphere source for the glyphs
49 sphere = vtk.vtkSphereSource()
50 sphere.SetThetaResolution(6)
51 sphere.SetPhiResolution(6)
52 sphere.SetRadius(0.5)
53
54 # make tensor glyphs
55 glyph = vtk.vtkTensorGlyph()
56 glyph.SetSource(sphere.GetOutput())
57 glyph.SetInput(extractGrid)
58 glyph.SetColorModeToScalars()
59 glyph.ScalingOn()
60 glyph.SetMaxScaleFactor(5.0)
61 glyph.SetScaleFactor(1.0)
62 glyph.ClampScalingOn()
63
64 # make a stripper for faster rendering
65 stripper = vtk.vtkStripper()
66 stripper.SetInput(glyph.GetOutput())
67
68 # make the normals of the data
69 normals = vtk.vtkPolyDataNormals()
70 normals.SetInput(stripper.GetOutput())
71
72 # make the mapper for the data
73 mapper = vtk.vtkPolyDataMapper()
74 mapper.SetInput(normals.GetOutput())
75 mapper.SetLookupTable(lut)
76 mapper.SetScalarRange(extractScalarRange)
77
78 # make the actor
79 actor = vtk.vtkActor()
80 actor.SetMapper(mapper)
81
82 # add the actor to be rendered
83 ren.AddActor(actor)
84
85 # set up text properties
86 textProp = vtk.vtkTextProperty()
87 textProp.SetFontFamilyToArial()
88 textProp.BoldOff()
89 textProp.ItalicOff()
90 textProp.ShadowOff()
91 textProp.SetColor(0.0, 0.0, 0.0)
92
93 # make a title
94 title = vtk.vtkTextMapper()
95 title.SetInput("Example ellipsoid plot")
96
97 # make the title text use the text properties
98 titleProp = title.GetTextProperty()
99 titleProp.ShallowCopy(textProp)
100 titleProp.SetJustificationToCentered()
101 titleProp.SetVerticalJustificationToTop()
102 titleProp.SetFontSize(20)
103 titleProp.BoldOn()
104
105 # make the actor for the title
106 titleActor = vtk.vtkTextActor()
107 titleActor.SetMapper(title)
108 titleActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
109 titleActor.GetPositionCoordinate().SetValue(0.5, 0.95)
110
111 ren.AddActor(titleActor)
112
113 # add a scalar bar
114 scalarBar = vtk.vtkScalarBarActor()
115 scalarBar.SetLookupTable(lut)
116 scalarBar.SetWidth(0.1)
117 scalarBar.SetHeight(0.8)
118 scalarBar.SetPosition(0.9, 0.15)
119
120 # set up the label text properties
121 scalarBarTextProp = scalarBar.GetLabelTextProperty()
122 scalarBarTextProp.ShallowCopy(textProp)
123 scalarBarTextProp.SetFontSize(10)
124
125 ren.AddActor(scalarBar)
126
127 # set up stuff for interactive viewing
128 iren = vtk.vtkRenderWindowInteractor()
129 iren.SetRenderWindow(renWin)
130
131 iren.Initialize()
132 renWin.Render()
133 iren.Start()
134
135 # the WindowToImageFilter is what one uses to save the window to an
136 # image file
137 win2img = vtk.vtkWindowToImageFilter()
138 win2img.SetInput(renWin)
139
140 # set up the PNGWriter as we're saving to png
141 writer = vtk.vtkPNGWriter()
142 writer.SetFileName("ellipsoidPlot.png")
143 writer.SetInput(win2img.GetOutput())
144 writer.Write()
145
146 # vim: expandtab shiftwidth=4:
147

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26