File: TestImprintFilter7.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (277 lines) | stat: -rwxr-xr-x 7,768 bytes parent folder | download | duplicates (3)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
from vtkmodules.vtkCommonDataModel import (
    vtkCylinder,
    vtkPlane,
)
from vtkmodules.vtkCommonSystem import vtkTimerLog
from vtkmodules.vtkFiltersCore import (
    vtkFlyingEdgesPlaneCutter,
    vtkPointDataToCellData,
    vtkPolyDataPlaneClipper,
)
from vtkmodules.vtkFiltersGeneral import vtkSampleImplicitFunctionFilter
from vtkmodules.vtkFiltersModeling import (
    vtkContourLoopExtraction,
    vtkImprintFilter,
    vtkOutlineFilter,
)
from vtkmodules.vtkFiltersSources import vtkCylinderSource
from vtkmodules.vtkImagingHybrid import vtkSampleFunction
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2

# Test the processing of point and cell data with vtkImprintFilter
#

# Control the size of the test
res = 101
cylRes = 20

# Control the coloring of the data
coloring = 1

# The orientation of the plane
normal = [0,1.01,1]

# Create a pipeline that cuts a volume to create a plane. This plane will be
# cookie cut with a cylinder.

# The cylinder is cut to produce a trim loop.  This trim loop will cookie cut
# the plane mentioned previously.

# Along the way, various combinations of the cell data
# and point data will be created which are processed by
# the cookie cutter.

# Create a synthetic source: sample a sphere across a volume
cyl = vtkCylinder()
cyl.SetCenter( 0.0,0.0,0.0)
cyl.SetRadius(0.25)
cyl.SetAxis(0,1,0)

sample = vtkSampleFunction()
sample.SetImplicitFunction(cyl)
sample.SetModelBounds(-0.75,0.75, -1,1, -0.5,0.5)
sample.SetSampleDimensions(res,res,res)
sample.ComputeNormalsOff()
sample.SetOutputScalarTypeToFloat()
sample.Update()

# The cut plane
plane = vtkPlane()
plane.SetOrigin(0,0,0)
plane.SetNormal(normal)

# Cut the volume quickly
cut = vtkFlyingEdgesPlaneCutter()
cut.SetInputConnection(sample.GetOutputPort())
cut.SetPlane(plane)
cut.ComputeNormalsOff()
cut.Update()

# Create cell data
pd2cd = vtkPointDataToCellData()
pd2cd.SetInputConnection(cut.GetOutputPort())
pd2cd.PassPointDataOn()
pd2cd.Update()

cutMapper = vtkPolyDataMapper()
cutMapper.SetInputConnection(pd2cd.GetOutputPort())

cutActor = vtkActor()
cutActor.SetMapper(cutMapper);
cutActor.GetProperty().SetColor(0.1,0.1,0.1)
cutActor.GetProperty().SetRepresentationToWireframe()

# Clip a cylinder shell to produce a a trim loop.
shell = vtkCylinderSource()
shell.SetCenter(0,0,0)
shell.SetResolution(cylRes)
shell.SetHeight(5)
shell.CappingOff()
shell.Update()

clippedShell = vtkPolyDataPlaneClipper()
clippedShell.SetInputConnection(shell.GetOutputPort())
clippedShell.SetPlane(plane)
clippedShell.ClippingLoopsOn()
clippedShell.CappingOff()
clippedShell.Update()

sampleImp = vtkSampleImplicitFunctionFilter()
sampleImp.SetInputConnection(clippedShell.GetOutputPort(1))
sampleImp.SetImplicitFunction(plane)
sampleImp.ComputeGradientsOff()
sampleImp.SetScalarArrayName("scalars")
sampleImp.Update()

clippedShellMapper = vtkPolyDataMapper()
clippedShellMapper.SetInputConnection(sampleImp.GetOutputPort())
clippedShellMapper.ScalarVisibilityOff()

clippedShellActor = vtkActor()
clippedShellActor.SetMapper(clippedShellMapper);

trimMapper = vtkPolyDataMapper()
trimMapper.SetInputConnection(sampleImp.GetOutputPort(0))
trimMapper.ScalarVisibilityOff()

trimActor = vtkActor()
trimActor.SetMapper(trimMapper);
trimActor.GetProperty().SetColor(0,0,1)

# Build a loop from the clipper
buildLoops = vtkContourLoopExtraction()
buildLoops.SetInputConnection(sampleImp.GetOutputPort(0))
buildLoops.CleanPointsOn()
buildLoops.Update()

# Test passing cell data
imprint0 = vtkImprintFilter()
imprint0.SetTargetConnection(pd2cd.GetOutputPort())
imprint0.SetImprintConnection(buildLoops.GetOutputPort())
imprint0.SetOutputTypeToImprintedRegion()
imprint0.BoundaryEdgeInsertionOn()
imprint0.SetTolerance(0.0001)
imprint0.SetMergeTolerance(0.0005)
if coloring == 0:
    imprint0.PassCellDataOff()
    imprint0.PassPointDataOff()
else:
    imprint0.PassCellDataOn()
    imprint0.PassPointDataOff()

timer = vtkTimerLog()
timer.StartTimer()
imprint0.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Imprinting with cell data: {0}".format(time))

imprint0Mapper = vtkPolyDataMapper()
imprint0Mapper.SetInputConnection(imprint0.GetOutputPort())
imprint0Mapper.SetScalarRange(0,0.1)

imprint0Actor = vtkActor()
imprint0Actor.SetMapper(imprint0Mapper);

# Test point data - mesh interpolation
imprint1 = vtkImprintFilter()
imprint1.SetTargetConnection(pd2cd.GetOutputPort())
imprint1.SetImprintConnection(buildLoops.GetOutputPort())
imprint1.SetOutputTypeToImprintedRegion()
imprint1.BoundaryEdgeInsertionOn()
imprint1.SetTolerance(0.0001)
imprint1.SetMergeTolerance(0.0005)
if coloring == 0:
    imprint1.PassCellDataOff()
    imprint1.PassPointDataOff()
else:
    imprint1.PassCellDataOff()
    imprint1.PassPointDataOn()
    imprint1.SetPointInterpolationToTargetEdges()

timer = vtkTimerLog()
timer.StartTimer()
imprint1.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Imprinting with target edge interpolation: {0}".format(time))

imprint1Mapper = vtkPolyDataMapper()
imprint1Mapper.SetInputConnection(imprint1.GetOutputPort())
imprint1Mapper.SetScalarRange(0,0.1)
imprint1Mapper.SetScalarModeToUsePointFieldData()
imprint1Mapper.SelectColorArray("scalars")

imprint1Actor = vtkActor()
imprint1Actor.SetMapper(imprint1Mapper);

# Test point data - loop edge interpolation
imprint2 = vtkImprintFilter()
imprint2.SetTargetConnection(pd2cd.GetOutputPort())
imprint2.SetImprintConnection(buildLoops.GetOutputPort())
imprint2.SetOutputTypeToImprintedRegion()
imprint2.BoundaryEdgeInsertionOn()
imprint2.SetTolerance(0.0001)
imprint2.SetMergeTolerance(0.0005)
if coloring == 0:
    imprint2.PassCellDataOff()
    imprint2.PassPointDataOff()
else:
    imprint2.PassCellDataOff()
    imprint2.PassPointDataOn()
    imprint2.SetPointInterpolationToImprintEdges()

timer = vtkTimerLog()
timer.StartTimer()
imprint2.Update()
timer.StopTimer()
time = timer.GetElapsedTime()
print("Imprinting with imprint edge interpolation: {0}".format(time))

imprint2Mapper = vtkPolyDataMapper()
imprint2Mapper.SetInputConnection(imprint2.GetOutputPort())
imprint2Mapper.SetScalarRange(0,0.1)
imprint2Mapper.SetScalarModeToUsePointFieldData()
imprint2Mapper.SelectColorArray("scalars")

imprint2Actor = vtkActor()
imprint2Actor.SetMapper(imprint2Mapper);

# Bounding boxes are always nice
outline = vtkOutlineFilter()
outline.SetInputConnection(sample.GetOutputPort())
outlineMapper = vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtkActor()
outlineActor.SetMapper(outlineMapper)

# Create three renderers, one to display cell data,
# one to display mesh-interpolated point data, one
# to display trim loop interpolated point data.
ren0 = vtkRenderer()
ren0.GetActiveCamera().SetPosition(normal)
ren0.SetViewport(0,0,0.333,1.0)
ren1 = vtkRenderer()
ren1.SetActiveCamera(ren0.GetActiveCamera())
ren1.SetViewport(0.333,0,0.667,1.0)
ren2 = vtkRenderer()
ren2.SetActiveCamera(ren0.GetActiveCamera())
ren2.SetViewport(0.667,0,1.0,1.0)

renWin = vtkRenderWindow()
renWin.AddRenderer( ren0 )
renWin.AddRenderer( ren1 )
renWin.AddRenderer( ren2 )
renWin.SetSize(600,200)

#ren0.AddActor(cutActor)
#ren0.AddActor(trimActor)
#ren0.AddActor(clippedShellActor)
#ren0.AddActor(imprint0Actor)

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

ren0.AddActor(outlineActor)
ren0.AddActor(imprint0Actor)

ren1.AddActor(outlineActor)
ren1.AddActor(imprint1Actor)

ren2.AddActor(outlineActor)
ren2.AddActor(imprint2Actor)

ren0.ResetCamera()

renWin.Render()
iren.Start()