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

Contents of /trunk/pyvisi/test/python/vtk/isosurfacePlot.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: 4068 byte(s)
example cleanup
1 # $Id: isosurfacePlot.py,v 1.1 2005/11/08 06:08:17 paultcochrane Exp $
2
3 """
4 Example of plotting a set of isosurfaces
5 """
6
7 # set up some data to plot
8 from Numeric import *
9
10 import vtk
11
12 # load a vtk file as input
13 reader = vtk.vtkXMLUnstructuredGridReader()
14 reader.SetFileName("../../temp-0500.vtk")
15 reader.Update()
16
17 # need to do a delaunay 3D here to get decent looking isosurfaces
18 del3D = vtk.vtkDelaunay3D()
19 del3D.SetInput(reader.GetOutput())
20 del3D.SetOffset(2.5)
21 del3D.SetTolerance(0.001)
22 del3D.SetAlpha(0.0)
23
24 # set up a contour filter
25 cont = vtk.vtkContourGrid()
26 cont.SetInput(del3D.GetOutput())
27 cont.GenerateValues(5, 0.25, 0.75) # need to automate this!!
28 cont.ComputeScalarsOn()
29
30 # get the model centre and bounds
31 centre = reader.GetOutput().GetCenter()
32 bounds = reader.GetOutput().GetBounds()
33
34 # set up the mapper
35 mapper = vtk.vtkDataSetMapper()
36 mapper.SetInput(cont.GetOutput())
37 mapper.ScalarVisibilityOn()
38
39 # set up the actor
40 actor = vtk.vtkActor()
41 actor.SetMapper(mapper)
42
43 # set up the text properties for nice text
44 font_size = 20
45 textProp = vtk.vtkTextProperty()
46 textProp.SetFontSize(font_size)
47 textProp.SetFontFamilyToArial()
48 textProp.BoldOff()
49 textProp.ItalicOff()
50 textProp.ShadowOff()
51 textProp.SetColor(0.0, 0.0, 0.0)
52
53 # make a title
54 title = vtk.vtkTextMapper()
55 title.SetInput("Example isosurface plot")
56
57 # make the title text use the text properties
58 titleProp = title.GetTextProperty()
59 titleProp.ShallowCopy(textProp)
60 titleProp.SetJustificationToCentered()
61 titleProp.SetVerticalJustificationToTop()
62
63 # make the actor for the title
64 titleActor = vtk.vtkTextActor()
65 titleActor.SetMapper(title)
66 titleActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
67 titleActor.GetPositionCoordinate().SetValue(0.5, 0.95)
68
69 # put an outline around the data
70 outline = vtk.vtkOutlineSource()
71 outline.SetBounds(bounds)
72
73 # make its mapper
74 outlineMapper = vtk.vtkPolyDataMapper()
75 outlineMapper.SetInput(outline.GetOutput())
76
77 # make its actor
78 outlineActor = vtk.vtkActor()
79 outlineActor.SetMapper(outlineMapper)
80 outlineActor.GetProperty().SetColor(0,0,0)
81
82 # make a lookup table for the colour map and invert it (colours look
83 # better when it's inverted)
84 lut = vtk.vtkLookupTable()
85 refLut = vtk.vtkLookupTable()
86 lut.Build()
87 refLut.Build()
88 for j in range(256):
89 lut.SetTableValue(j, refLut.GetTableValue(255-j))
90
91 # set up the renderer and render window
92 ren = vtk.vtkRenderer()
93 renWin = vtk.vtkRenderWindow()
94
95 renWin.SetSize(800,600)
96 renWin.AddRenderer(ren)
97 ren.SetBackground(1,1,1)
98
99 # add some axes
100 axes = vtk.vtkCubeAxesActor2D()
101 axes.SetInput(reader.GetOutput())
102 axes.SetCamera(ren.GetActiveCamera())
103 axes.SetLabelFormat("%6.4g")
104 axes.SetFlyModeToOuterEdges()
105 axes.SetFontFactor(0.8)
106 axes.SetAxisTitleTextProperty(textProp)
107 axes.SetAxisLabelTextProperty(textProp)
108 axes.SetXLabel("x")
109 axes.SetYLabel("y")
110 axes.SetZLabel("z")
111 axes.SetNumberOfLabels(5)
112 axes.GetProperty().SetColor(0,0,0)
113
114 # add the relevant actors
115 ren.AddActor(actor)
116 ren.AddActor(titleActor)
117 ren.AddActor(outlineActor)
118 ren.AddActor(axes)
119
120 ### need to make a camera object in pyvisi!!!
121 ren.ResetCamera()
122 cam = ren.GetActiveCamera()
123 cam.Azimuth(0)
124 cam.Elevation(-90)
125 cam.SetFocalPoint(centre)
126 ren.SetActiveCamera(cam)
127 ren.ResetCameraClippingRange()
128
129 # play around with lighting
130 light = vtk.vtkLight()
131 light.SetFocalPoint(centre)
132 light.SetPosition(centre[0]-bounds[1],
133 centre[1]-bounds[3],
134 centre[2]+bounds[5])
135 ren.AddLight(light)
136
137 light2 = vtk.vtkLight()
138 light2.SetFocalPoint(centre)
139 light2.SetPosition(centre[0]+bounds[1],
140 centre[1]+bounds[3],
141 centre[2]-bounds[5])
142 ren.AddLight(light2)
143
144 # set up stuff for interactive viewing
145 iren = vtk.vtkRenderWindowInteractor()
146 iren.SetRenderWindow(renWin)
147
148 iren.Initialize()
149 renWin.Render()
150 iren.Start()
151
152 # the WindowToImageFilter is what one uses to save the window to an
153 # image file
154 win2img = vtk.vtkWindowToImageFilter()
155 win2img.SetInput(renWin)
156
157 # set up the PNGWriter as we're saving to png
158 writer = vtk.vtkPNGWriter()
159 writer.SetFileName("isosurfacePlot.png")
160 writer.SetInput(win2img.GetOutput())
161 writer.Write()
162
163 # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26