This repository was archived by the owner on May 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSection4_vtk.py
More file actions
174 lines (152 loc) · 4.16 KB
/
Section4_vtk.py
File metadata and controls
174 lines (152 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#make vtk available to python
import vtk
# Make a window on our display
renwin = vtk.vtkRenderWindow()
# Define the size of the initial window
renwin.SetSize(500,500)
# Create a renderer
renderer = vtk.vtkRenderer()
renderer.SetBackground2(1,1,1)
renderer.SetGradientBackground(1)
# Create an interactor
iren = vtk.vtkRenderWindowInteractor()
# Add the renderer to the render window
renwin.AddRenderer(renderer)
# Set the interactor to the render window
renwin.SetInteractor(iren)
# Show the (empty) window
renwin.Render()
# Initialize the interactor
iren.Initialize()
# Update the rendering pipeline
renwin.Render()
# Start the UI event loop
iren.Start()
# Reading DEM data
# Creating a reader
reader = vtk.vtkDataSetReader()
filename = "SaintHelens.vtk"
reader.SetFileName(filename)
# Updating the pipeline
reader.Update()
# Printing information about the output of the reader
id = reader.GetOutput()
print(id.GetClassName())
print(id.GetBounds())
print id.GetPointData().GetArray(0).GetRange()
# Create the mapper
mapper = vtk.vtkDataSetMapper()
# Set the visisbility on the scalar
mapper.ScalarVisibilityOn()
# Set the scalar range to have a nice blue-red range
mapper.SetScalarRange(682.0, 2543.0)
# Connect the mapper to the reader
mapper.SetInputConnection(reader.GetOutputPort())
# Create the actor
actor = vtk.vtkActor()
# Sets the mapper to the actor
actor.SetMapper(mapper)
# Add the actor to the renderer
renderer.AddViewProp(actor)
# Perform rendering
renwin.Render()
# Center the camera to see the full scene
renderer.ResetCamera()
# Final rendering
renwin.Render()
# Initialize the interactor
iren.Initialize()
# Update the rendering pipeline
renwin.Render()
# Start the UI event loop
iren.Start()
# We elevate the surface with the vtkWarpScalarFilter
warp = vtk.vtkWarpScalar()
# Connect the filter to the reader
warp.SetInputConnection(reader.GetOutputPort())
# Update the pipeline
warp.Update()
print(warp.GetOutput().GetBounds())
# Show the elevated surface
mapper.SetInputConnection(warp.GetOutputPort())
renwin.Render()
renderer.ResetCamera()
renwin.Render()
# Initialize the interactor
iren.Initialize()
# Update the rendering pipeline
renwin.Render()
# Start the UI event loop
iren.Start()
# Draw the iso-surface
isosurface = vtk.vtkContourFilter()
# Generate 10 iso-surfaces along the elevation
isosurface.GenerateValues(10,682.0,2543.0);
# Connect the filter to the reader
isosurface.SetInputConnection(warp.GetOutputPort())
# Update the pipeline
isosurface.Update()
mapper.SetInputConnection(isosurface.GetOutputPort())
renwin.Render()
renderer.ResetCamera()
renwin.Render()
# Start the UI Loop
iren.Initialize()
renwin.Render()
iren.Start()
# Render everything into one
mapper.SetInputConnection(warp.GetOutputPort())
# Create a new actor
mapperiso = vtk.vtkDataSetMapper()
mapperiso.SetInputConnection(isosurface.GetOutputPort())
# Create the actor
actoriso = vtk.vtkActor()
actoriso.SetMapper(mapperiso)
# Disable the scalar coloring
mapperiso.ScalarVisibilityOff()
# Set the color to black
actoriso.GetProperty().SetColor(0,0,0)
# Set the line width larger
actoriso.GetProperty().SetLineWidth(2)
# add the actor to the rendering
renderer.AddViewProp(actoriso)
# Start the UI Loop
iren.Initialize()
renwin.Render()
iren.Start()
# Removes the iso actor
renderer.RemoveViewProp(actoriso)
# Clip the data
cf = vtk.vtkClipDataSet()
# Set the clipping plane
plane = vtk.vtkPlane()
cf.SetClipFunction(plane)
print plane
# Set the plane origin
plane.SetOrigin(560000,5120000,2000)
# Connect the pipeline
cf.SetInputConnection(warp.GetOutputPort())
mapper.SetInputConnection(cf.GetOutputPort())
# Start the UI Loop
iren.Initialize()
renwin.Render()
iren.Start()
# Creates an implicit plane widget
widget = vtk.vtkImplicitPlaneWidget()
widget.PlaceWidget(warp.GetOutput().GetBounds())
widget.SetOrigin([plane.GetOrigin()[x] for x in 0,1,2])
widget.SetNormal([plane.GetNormal()[x] for x in 0,1,2])
widget.SetInteractor(iren)
# Connects the interaction event to the plane
def cb(obj,event):
global plane
obj.GetPlane(plane)
widget.AddObserver("InteractionEvent", cb)
widget.SetEnabled(1)
widget.DrawPlaneOn()
widget.TubingOn()
renwin.Render()
# Start the UI Loop
iren.Initialize()
renwin.Render()
iren.Start()