File: TestAttributeSmoothingFilter.py

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: 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 (198 lines) | stat: -rwxr-xr-x 4,608 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
from vtkmodules.vtkCommonCore import (
    vtkFloatArray,
    vtkPoints,
    vtkUnsignedCharArray,
)
from vtkmodules.vtkCommonDataModel import (
    vtkCellArray,
    vtkPolyData,
)
from vtkmodules.vtkFiltersCore import vtkExecutionTimer
from vtkmodules.vtkFiltersGeometry import vtkAttributeSmoothingFilter
from vtkmodules.vtkFiltersSources import vtkPlaneSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer,
)
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Test the vtkAttributeSmoothingFilter

# Control test size
res = 25

# Generate synthetic attributes
#
ps = vtkPlaneSource()
ps.SetXResolution(res-1)
ps.SetYResolution(res-1)
ps.Update()

pd = vtkPolyData()
pts = vtkPoints()
polys = vtkCellArray()
pd.SetPoints(ps.GetOutput().GetPoints())
pd.SetPolys(ps.GetOutput().GetPolys())

# Create some synthetic attribute data
s = vtkFloatArray()
s.SetNumberOfTuples(res*res)
s.Fill(4) # Interior all the same value
pd.GetPointData().SetScalars(s)

def SetRow(row,value):
    for x in range(0,res):
        idx = x + row*res
        s.SetTuple1(idx,value)

def SetColumn(col,value):
    for y in range(0,res):
        idx = col + y*res
        s.SetTuple1(idx,value)

def SetPoint(col,row,value):
    idx = col + row*res
    s.SetTuple1(idx,value)

# Set values adjacent to boundary
SetRow(1,1)
SetRow(res-2,1)
SetColumn(1,1)
SetColumn(res-2,1)

# Set boundary values
SetRow(0,0)
SetRow(res-1,0)
SetColumn(0,0)
SetColumn(res-1,0)

# Set center point
SetPoint(int(res/2),int(res/2),0)

# Display what we've got
pdm0 = vtkPolyDataMapper()
pdm0.SetInputData(pd)
pdm0.SetScalarRange(0,4)

a0 = vtkActor()
a0.SetMapper(pdm0)

# Different ways smooth the attribute data
att1 = vtkAttributeSmoothingFilter()
att1.SetInputData(pd)
att1.SetSmoothingStrategyToAllPoints()
att1.SetNumberOfIterations(50)
att1.SetRelaxationFactor(0.1)

timer = vtkExecutionTimer()
timer.SetFilter(att1)
att1.Update()
t1 = timer.GetElapsedWallClockTime()
print ("Smooth Attributes: ", t1)

pdm1 = vtkPolyDataMapper()
pdm1.SetInputConnection(att1.GetOutputPort())
pdm1.SetScalarRange(0,4)

a1 = vtkActor()
a1.SetMapper(pdm1)

# Smooth everything but the boundary
att2 = vtkAttributeSmoothingFilter()
att2.SetInputData(pd)
att2.SetSmoothingStrategyToAllButBoundary()
att2.SetNumberOfIterations(2)
att2.SetRelaxationFactor(0.5)
att2.Update()

pdm2 = vtkPolyDataMapper()
pdm2.SetInputConnection(att2.GetOutputPort())
pdm2.SetScalarRange(0,4)

a2 = vtkActor()
a2.SetMapper(pdm2)

# Only smooth points near boundary
att3 = vtkAttributeSmoothingFilter()
att3.SetInputData(pd)
att3.SetSmoothingStrategyToAdjacentToBoundary()
att3.SetNumberOfIterations(3)
att3.SetRelaxationFactor(0.333)
att3.Update()

pdm3 = vtkPolyDataMapper()
pdm3.SetInputConnection(att3.GetOutputPort())
pdm3.SetScalarRange(0,4)

a3 = vtkActor()
a3.SetMapper(pdm3)

# Using smoothing mask, smooth all points except center point
smoothArray = vtkUnsignedCharArray()
smoothArray.SetNumberOfTuples(res*res)
smoothArray.Fill(1) # Smooth all points
idx = int(res/2) + int(res/2)*res
smoothArray.SetTuple1(idx,0) # Except the center point

att4 = vtkAttributeSmoothingFilter()
att4.SetInputData(pd)
att4.SetSmoothingStrategyToSmoothingMask()
att4.SetNumberOfIterations(50)
att4.SetRelaxationFactor(0.10)
att4.SetSmoothingMask(smoothArray)
att4.Update()

pdm4 = vtkPolyDataMapper()
pdm4.SetInputConnection(att4.GetOutputPort())
pdm4.SetScalarRange(0,4)

a4 = vtkActor()
a4.SetMapper(pdm4)

# Create the RenderWindow, Renderer and interactive renderer
#
renWin = vtkRenderWindow()
ren0 = vtkRenderer()
ren0.SetViewport(0,0,0.20,1)
ren1 = vtkRenderer()
ren1.SetViewport(0.20,0,0.40,1)
ren2 = vtkRenderer()
ren2.SetViewport(0.40,0,0.60,1)
ren3 = vtkRenderer()
ren3.SetViewport(0.60,0,0.80,1)
ren4 = vtkRenderer()
ren4.SetViewport(0.80,0,1,1)
renWin.AddRenderer(ren0)
renWin.AddRenderer(ren1)
renWin.AddRenderer(ren2)
renWin.AddRenderer(ren3)
renWin.AddRenderer(ren4)

# make sure to have the same regression image on all platforms.
renWin.SetMultiSamples(0)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

ren0.AddActor(a0)
ren0.SetBackground(0, 0, 0)
ren1.AddActor(a1)
ren1.SetBackground(0, 0, 0)
ren2.AddActor(a2)
ren2.SetBackground(0, 0, 0)
ren3.AddActor(a3)
ren3.SetBackground(0, 0, 0)
ren4.AddActor(a4)
ren4.SetBackground(0, 0, 0)

renWin.SetSize(1000, 200)

renWin.Render()
iren.Start()