-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
336 lines (264 loc) · 31.4 KB
/
index.html
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Enable responsiveness on mobile devices-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>
Berk Geveci · All About VTK / ParaView
</title>
<!-- CSS -->
<link rel="stylesheet" href="/public/css/poole.css">
<link rel="stylesheet" href="/public/css/syntax.css">
<!-- Icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-precomposed.png">
<link rel="shortcut icon" href="/public/favicon.ico">
<!-- RSS -->
<link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
</head>
<body>
<div class="container content">
<div class="masthead">
<h3 class="masthead-title">
<a href="/" title="Home">Berk Geveci</a>
<small><a href="/about">About</a></small>
<small><a href="/archive">Archive</a></small>
<small><a href="/atom.xml">Feed</a></small>
</h3>
</div>
<div class="posts">
<div class="post">
<h1 class="post-title">
<a href="/2015/03/07/threshold/">
Threshold with Cell Set
</a>
</h1>
<span class="post-date">07 Mar 2015</span>
<p>In my <a href="/2015/02/20/cell-set/">last blog</a>, I described a new
type of unstructured grid which can be used to represent a subset of a
structured dataset without explicit connectivity data structures. I mentioned
that one very good application of such a dataset is a better implementation
of the threshold filter. In the blog, we'll go over such an implementation as
shown <a href="https://github.com/berkgeveci/berkgeveci.github.io/blob/jekyll/code/zero-copy/vtkThreshold3.h">here</a>
and <a href="https://github.com/berkgeveci/berkgeveci.github.io/blob/jekyll/code/zero-copy/vtkThreshold3.cxx">here</a>.</p>
<p>Let's first quickly go over the original VTK implementation as can be found
<a href="https://github.com/Kitware/VTK/blob/v6.2.0/Filters/Core/vtkThreshold.cxx">here</a>.
First notice that this filter accepts any <code>vtkDataSet</code> and produces an unstructured
grid.</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="c1">// Note that subclasses of vtkUnstructuredGridAlgorithm produce</span>
<span class="c1">// vtkUnstructuredGrid in the first output by default.</span>
<span class="k">class</span> <span class="nc">VTKFILTERSCORE_EXPORT</span> <span class="nl">vtkThreshold</span> <span class="p">:</span> <span class="k">public</span> <span class="n">vtkUnstructuredGridAlgorithm</span>
<span class="p">{</span></code></pre></div>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="c1">// FillInputPortInformation is overriden to change the required</span>
<span class="c1">// input from vtkUnstructuredGrid to vtkDataSet</span>
<span class="kt">int</span> <span class="n">vtkThreshold</span><span class="o">::</span><span class="n">FillInputPortInformation</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="n">vtkInformation</span> <span class="o">*</span><span class="n">info</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">info</span><span class="o">-></span><span class="n">Set</span><span class="p">(</span><span class="n">vtkAlgorithm</span><span class="o">::</span><span class="n">INPUT_REQUIRED_DATA_TYPE</span><span class="p">(),</span> <span class="s">"vtkDataSet"</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span></code></pre></div>
<p>Next, let's quickly take a look at <code>RequestData</code>. I added some comments
in the excerpt to make it easier to follow. I also took out pieces that
are not very relevant to our discussion. See the full source for details.</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="kt">int</span> <span class="n">vtkThreshold</span><span class="o">::</span><span class="n">RequestData</span><span class="p">(</span>
<span class="n">vtkInformation</span> <span class="o">*</span><span class="n">vtkNotUsed</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
<span class="n">vtkInformationVector</span> <span class="o">**</span><span class="n">inputVector</span><span class="p">,</span>
<span class="n">vtkInformationVector</span> <span class="o">*</span><span class="n">outputVector</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// get the info objects</span>
<span class="n">vtkInformation</span> <span class="o">*</span><span class="n">inInfo</span> <span class="o">=</span> <span class="n">inputVector</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-></span><span class="n">GetInformationObject</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="n">vtkInformation</span> <span class="o">*</span><span class="n">outInfo</span> <span class="o">=</span> <span class="n">outputVector</span><span class="o">-></span><span class="n">GetInformationObject</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="c1">// get the input and output</span>
<span class="n">vtkDataSet</span> <span class="o">*</span><span class="n">input</span> <span class="o">=</span> <span class="n">vtkDataSet</span><span class="o">::</span><span class="n">SafeDownCast</span><span class="p">(</span>
<span class="n">inInfo</span><span class="o">-></span><span class="n">Get</span><span class="p">(</span><span class="n">vtkDataObject</span><span class="o">::</span><span class="n">DATA_OBJECT</span><span class="p">()));</span>
<span class="n">vtkUnstructuredGrid</span> <span class="o">*</span><span class="n">output</span> <span class="o">=</span> <span class="n">vtkUnstructuredGrid</span><span class="o">::</span><span class="n">SafeDownCast</span><span class="p">(</span>
<span class="n">outInfo</span><span class="o">-></span><span class="n">Get</span><span class="p">(</span><span class="n">vtkDataObject</span><span class="o">::</span><span class="n">DATA_OBJECT</span><span class="p">()));</span>
<span class="c1">// ...</span>
<span class="c1">// We create an output unstructured grid. This grid will contain</span>
<span class="c1">// the same set of point and cell arrays. But these will be new</span>
<span class="c1">// arrays that contain only the output subset.</span>
<span class="n">outPD</span><span class="o">-></span><span class="n">CopyGlobalIdsOn</span><span class="p">();</span>
<span class="n">outPD</span><span class="o">-></span><span class="n">CopyAllocate</span><span class="p">(</span><span class="n">pd</span><span class="p">);</span>
<span class="n">outCD</span><span class="o">-></span><span class="n">CopyGlobalIdsOn</span><span class="p">();</span>
<span class="n">outCD</span><span class="o">-></span><span class="n">CopyAllocate</span><span class="p">(</span><span class="n">cd</span><span class="p">);</span>
<span class="c1">// Create new output points.</span>
<span class="n">numPts</span> <span class="o">=</span> <span class="n">input</span><span class="o">-></span><span class="n">GetNumberOfPoints</span><span class="p">();</span>
<span class="n">output</span><span class="o">-></span><span class="n">Allocate</span><span class="p">(</span><span class="n">input</span><span class="o">-></span><span class="n">GetNumberOfCells</span><span class="p">());</span>
<span class="n">newPoints</span> <span class="o">=</span> <span class="n">vtkPoints</span><span class="o">::</span><span class="n">New</span><span class="p">();</span>
<span class="c1">// ...</span>
<span class="n">newPoints</span><span class="o">-></span><span class="n">Allocate</span><span class="p">(</span><span class="n">numPts</span><span class="p">);</span>
<span class="c1">// We need this map to find the mapping between input</span>
<span class="c1">// and output point ids so that we can copy the point data.</span>
<span class="n">pointMap</span> <span class="o">=</span> <span class="n">vtkIdList</span><span class="o">::</span><span class="n">New</span><span class="p">();</span> <span class="c1">//maps old point ids into new</span>
<span class="n">pointMap</span><span class="o">-></span><span class="n">SetNumberOfIds</span><span class="p">(</span><span class="n">numPts</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">numPts</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">pointMap</span><span class="o">-></span><span class="n">SetId</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">// ...</span>
<span class="c1">// Check that the scalars of each cell satisfy the threshold criterion</span>
<span class="k">for</span> <span class="p">(</span><span class="n">cellId</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">cellId</span> <span class="o"><</span> <span class="n">input</span><span class="o">-></span><span class="n">GetNumberOfCells</span><span class="p">();</span> <span class="n">cellId</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">cell</span> <span class="o">=</span> <span class="n">input</span><span class="o">-></span><span class="n">GetCell</span><span class="p">(</span><span class="n">cellId</span><span class="p">);</span>
<span class="n">cellPts</span> <span class="o">=</span> <span class="n">cell</span><span class="o">-></span><span class="n">GetPointIds</span><span class="p">();</span>
<span class="n">numCellPts</span> <span class="o">=</span> <span class="n">cell</span><span class="o">-></span><span class="n">GetNumberOfPoints</span><span class="p">();</span>
<span class="c1">// Here whether to keep a cell is determined. The answer is stored in</span>
<span class="c1">// the keepCell variable.</span>
<span class="c1">// If we are keeping the cell, copy to the output.</span>
<span class="k">if</span> <span class="p">(</span> <span class="n">numCellPts</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">keepCell</span> <span class="p">)</span>
<span class="p">{</span>
<span class="c1">// satisfied thresholding (also non-empty cell, i.e. not VTK_EMPTY_CELL)</span>
<span class="c1">// Go over the points of the cell.</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">numCellPts</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ptId</span> <span class="o">=</span> <span class="n">cellPts</span><span class="o">-></span><span class="n">GetId</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="c1">// If we didn't insert the point already, insert in the output</span>
<span class="k">if</span> <span class="p">(</span> <span class="p">(</span><span class="n">newId</span> <span class="o">=</span> <span class="n">pointMap</span><span class="o">-></span><span class="n">GetId</span><span class="p">(</span><span class="n">ptId</span><span class="p">))</span> <span class="o"><</span> <span class="mi">0</span> <span class="p">)</span>
<span class="p">{</span>
<span class="n">input</span><span class="o">-></span><span class="n">GetPoint</span><span class="p">(</span><span class="n">ptId</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>
<span class="n">newId</span> <span class="o">=</span> <span class="n">newPoints</span><span class="o">-></span><span class="n">InsertNextPoint</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="c1">// Update the point map for future use.</span>
<span class="n">pointMap</span><span class="o">-></span><span class="n">SetId</span><span class="p">(</span><span class="n">ptId</span><span class="p">,</span><span class="n">newId</span><span class="p">);</span>
<span class="c1">// Also copy the data.</span>
<span class="n">outPD</span><span class="o">-></span><span class="n">CopyData</span><span class="p">(</span><span class="n">pd</span><span class="p">,</span><span class="n">ptId</span><span class="p">,</span><span class="n">newId</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">// Insert the point to the output cell.</span>
<span class="n">newCellPts</span><span class="o">-></span><span class="n">InsertId</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">newId</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">// ...</span>
<span class="c1">// Insert the new cell to the output</span>
<span class="n">newCellId</span> <span class="o">=</span> <span class="n">output</span><span class="o">-></span><span class="n">InsertNextCell</span><span class="p">(</span><span class="n">cell</span><span class="o">-></span><span class="n">GetCellType</span><span class="p">(),</span><span class="n">newCellPts</span><span class="p">);</span>
<span class="c1">// Copy the cell data</span>
<span class="n">outCD</span><span class="o">-></span><span class="n">CopyData</span><span class="p">(</span><span class="n">cd</span><span class="p">,</span><span class="n">cellId</span><span class="p">,</span><span class="n">newCellId</span><span class="p">);</span>
<span class="n">newCellPts</span><span class="o">-></span><span class="n">Reset</span><span class="p">();</span>
<span class="p">}</span> <span class="c1">// satisfied thresholding</span>
<span class="p">}</span> <span class="c1">// for all cells</span>
<span class="c1">// ..</span>
<span class="n">output</span><span class="o">-></span><span class="n">SetPoints</span><span class="p">(</span><span class="n">newPoints</span><span class="p">);</span>
<span class="n">newPoints</span><span class="o">-></span><span class="n">Delete</span><span class="p">();</span>
<span class="n">output</span><span class="o">-></span><span class="n">Squeeze</span><span class="p">();</span>
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span></code></pre></div>
<p>In our new filter, much of this will remain the same. In fact, to quickly
develop it, I copied <code>vtkThreshold</code> to <code>vtkThreshold3</code> and made a few changes.
First, I change the filter to produce a <code>vtkCellSet</code>:</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="k">class</span> <span class="nc">vtkThreshold3</span> <span class="o">:</span> <span class="k">public</span> <span class="n">vtkUnstructuredGridBaseAlgorithm</span>
<span class="p">{</span></code></pre></div>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="kt">int</span> <span class="n">vtkThreshold3</span><span class="o">::</span><span class="n">RequestDataObject</span><span class="p">(</span>
<span class="n">vtkInformation</span> <span class="o">*</span><span class="n">vtkNotUsed</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
<span class="n">vtkInformationVector</span> <span class="o">**</span><span class="n">vtkNotUsed</span><span class="p">(</span><span class="n">inputVector</span><span class="p">),</span>
<span class="n">vtkInformationVector</span> <span class="o">*</span><span class="n">outputVector</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">vtkInformation</span><span class="o">*</span> <span class="n">info</span> <span class="o">=</span> <span class="n">outputVector</span><span class="o">-></span><span class="n">GetInformationObject</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="n">vtkCellSet</span> <span class="o">*</span><span class="n">output</span> <span class="o">=</span> <span class="n">vtkCellSet</span><span class="o">::</span><span class="n">SafeDownCast</span><span class="p">(</span>
<span class="n">info</span><span class="o">-></span><span class="n">Get</span><span class="p">(</span><span class="n">vtkDataObject</span><span class="o">::</span><span class="n">DATA_OBJECT</span><span class="p">()));</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">output</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">vtkCellSet</span><span class="o">*</span> <span class="n">newOutput</span> <span class="o">=</span> <span class="n">vtkCellSet</span><span class="o">::</span><span class="n">New</span><span class="p">();</span>
<span class="n">info</span><span class="o">-></span><span class="n">Set</span><span class="p">(</span><span class="n">vtkDataObject</span><span class="o">::</span><span class="n">DATA_OBJECT</span><span class="p">(),</span> <span class="n">newOutput</span><span class="p">);</span>
<span class="n">newOutput</span><span class="o">-></span><span class="n">Delete</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span></code></pre></div>
<p>Next, I changed the part of the code that creates new points and cells to
instead keep track of the ids of the cells that we want to threshold. So
the following code</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="c1">// If we are keeping the cell, copy to the output.</span>
<span class="k">if</span> <span class="p">(</span> <span class="n">numCellPts</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">keepCell</span> <span class="p">)</span>
<span class="p">{</span>
<span class="c1">// satisfied thresholding (also non-empty cell, i.e. not VTK_EMPTY_CELL)</span>
<span class="c1">// Go over the points of the cell.</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">numCellPts</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ptId</span> <span class="o">=</span> <span class="n">cellPts</span><span class="o">-></span><span class="n">GetId</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="c1">// If we didn't insert the point already, insert in the output</span>
<span class="k">if</span> <span class="p">(</span> <span class="p">(</span><span class="n">newId</span> <span class="o">=</span> <span class="n">pointMap</span><span class="o">-></span><span class="n">GetId</span><span class="p">(</span><span class="n">ptId</span><span class="p">))</span> <span class="o"><</span> <span class="mi">0</span> <span class="p">)</span>
<span class="p">{</span>
<span class="n">input</span><span class="o">-></span><span class="n">GetPoint</span><span class="p">(</span><span class="n">ptId</span><span class="p">,</span> <span class="n">x</span><span class="p">);</span>
<span class="n">newId</span> <span class="o">=</span> <span class="n">newPoints</span><span class="o">-></span><span class="n">InsertNextPoint</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="c1">// Update the point map for future use.</span>
<span class="n">pointMap</span><span class="o">-></span><span class="n">SetId</span><span class="p">(</span><span class="n">ptId</span><span class="p">,</span><span class="n">newId</span><span class="p">);</span>
<span class="c1">// Also copy the data.</span>
<span class="n">outPD</span><span class="o">-></span><span class="n">CopyData</span><span class="p">(</span><span class="n">pd</span><span class="p">,</span><span class="n">ptId</span><span class="p">,</span><span class="n">newId</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">// Insert the point to the output cell.</span>
<span class="n">newCellPts</span><span class="o">-></span><span class="n">InsertId</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">newId</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">// ...</span>
<span class="c1">// Insert the new cell to the output</span>
<span class="n">newCellId</span> <span class="o">=</span> <span class="n">output</span><span class="o">-></span><span class="n">InsertNextCell</span><span class="p">(</span><span class="n">cell</span><span class="o">-></span><span class="n">GetCellType</span><span class="p">(),</span><span class="n">newCellPts</span><span class="p">);</span>
<span class="c1">// Copy the cell data</span>
<span class="n">outCD</span><span class="o">-></span><span class="n">CopyData</span><span class="p">(</span><span class="n">cd</span><span class="p">,</span><span class="n">cellId</span><span class="p">,</span><span class="n">newCellId</span><span class="p">);</span>
<span class="n">newCellPts</span><span class="o">-></span><span class="n">Reset</span><span class="p">();</span>
<span class="p">}</span> <span class="c1">// satisfied thresholding</span></code></pre></div>
<p>changes to</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="k">if</span> <span class="p">(</span> <span class="n">numCellPts</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">keepCell</span> <span class="p">)</span>
<span class="p">{</span>
<span class="n">ids</span><span class="o">-></span><span class="n">InsertNextValue</span><span class="p">(</span><span class="n">cellId</span><span class="p">);</span>
<span class="p">}</span> <span class="c1">// satisfied thresholding</span></code></pre></div>
<p>And the <code>ids</code> are used in creating the cell set as follows</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="n">vtkIdTypeArray</span><span class="o">*</span> <span class="n">ids</span> <span class="o">=</span> <span class="n">vtkIdTypeArray</span><span class="o">::</span><span class="n">New</span><span class="p">();</span>
<span class="n">ids</span><span class="o">-></span><span class="n">Allocate</span><span class="p">(</span><span class="n">input</span><span class="o">-></span><span class="n">GetNumberOfCells</span><span class="p">());</span>
<span class="n">output</span><span class="o">-></span><span class="n">GetImplementation</span><span class="p">()</span><span class="o">-></span><span class="n">SetCellIds</span><span class="p">(</span><span class="n">ids</span><span class="p">);</span>
<span class="n">ids</span><span class="o">-></span><span class="n">Delete</span><span class="p">();</span></code></pre></div>
<p>Finally, we have to create an instance of <code>vtkPoints</code> for the output
unstructured grid. This has to be handled specially in the cases where
the input does not have its own <code>vtkPoints</code> (for example <code>vtkImageData</code>).
Here is the code:</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="n">vtkImageData</span><span class="o">*</span> <span class="n">inputImage</span> <span class="o">=</span> <span class="n">vtkImageData</span><span class="o">::</span><span class="n">SafeDownCast</span><span class="p">(</span><span class="n">input</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">inputImage</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">vtkNew</span><span class="o"><</span><span class="n">vtkImageData</span><span class="o">></span> <span class="n">img</span><span class="p">;</span>
<span class="n">img</span><span class="o">-></span><span class="n">CopyStructure</span><span class="p">(</span><span class="n">inputImage</span><span class="p">);</span>
<span class="n">vtkNew</span><span class="o"><</span><span class="n">vtkImagePointsArray</span><span class="o"><</span><span class="kt">double</span><span class="o">></span> <span class="o">></span> <span class="n">ptsArray</span><span class="p">;</span>
<span class="n">ptsArray</span><span class="o">-></span><span class="n">InitializeArray</span><span class="p">(</span><span class="n">img</span><span class="p">.</span><span class="n">GetPointer</span><span class="p">());</span>
<span class="n">ptsArray</span><span class="o">-></span><span class="n">SetName</span><span class="p">(</span><span class="s">"pts"</span><span class="p">);</span>
<span class="n">vtkNew</span><span class="o"><</span><span class="n">vtkPoints</span><span class="o">></span> <span class="n">points</span><span class="p">;</span>
<span class="n">points</span><span class="o">-></span><span class="n">SetData</span><span class="p">(</span><span class="n">ptsArray</span><span class="p">.</span><span class="n">GetPointer</span><span class="p">());</span>
<span class="n">output</span><span class="o">-></span><span class="n">SetPoints</span><span class="p">(</span><span class="n">points</span><span class="p">.</span><span class="n">GetPointer</span><span class="p">());</span>
<span class="p">}</span></code></pre></div>
<p>This is it! The output of this filter acts as an unstructured grid but does
not store cells or points explicitly. As a bonus, the filter actually got simpler
and performs better. We can exercise this filter as follows.</p>
<div class="highlight"><pre><code class="language-cpp" data-lang="cpp"><span class="n">vtkNew</span><span class="o"><</span><span class="n">vtkRTAnalyticSource</span><span class="o">></span> <span class="n">source</span><span class="p">;</span>
<span class="n">source</span><span class="o">-></span><span class="n">SetWholeExtent</span><span class="p">(</span><span class="o">-</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="o">-</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="o">-</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">);</span>
<span class="n">vtkNew</span><span class="o"><</span><span class="n">vtkThreshold3</span><span class="o">></span> <span class="n">threshold</span><span class="p">;</span>
<span class="n">threshold</span><span class="o">-></span><span class="n">ThresholdByLower</span><span class="p">(</span><span class="mi">200</span><span class="p">);</span>
<span class="n">threshold</span><span class="o">-></span><span class="n">SetInputConnection</span><span class="p">(</span><span class="n">source</span><span class="o">-></span><span class="n">GetOutputPort</span><span class="p">());</span>
<span class="n">threshold</span><span class="o">-></span><span class="n">Update</span><span class="p">();</span>
<span class="n">cerr</span> <span class="o"><<</span> <span class="s">"Threshold "</span> <span class="o"><<</span> <span class="n">timer</span><span class="o">-></span><span class="n">GetElapsedTime</span><span class="p">()</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
<span class="n">cerr</span> <span class="o"><<</span> <span class="n">threshold</span><span class="o">-></span><span class="n">GetOutput</span><span class="p">()</span><span class="o">-></span><span class="n">GetNumberOfCells</span><span class="p">()</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
<span class="n">vtkNew</span><span class="o"><</span><span class="n">vtkContourFilter</span><span class="o">></span> <span class="n">contour</span><span class="p">;</span>
<span class="n">contour</span><span class="o">-></span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">150</span><span class="p">);</span>
<span class="n">contour</span><span class="o">-></span><span class="n">SetInputConnection</span><span class="p">(</span><span class="n">threshold</span><span class="o">-></span><span class="n">GetOutputPort</span><span class="p">());</span>
<span class="n">contour</span><span class="o">-></span><span class="n">Update</span><span class="p">();</span>
<span class="n">cerr</span> <span class="o"><<</span> <span class="s">"Contour "</span> <span class="o"><<</span> <span class="n">timer</span><span class="o">-></span><span class="n">GetElapsedTime</span><span class="p">()</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span>
<span class="n">cerr</span> <span class="o"><<</span> <span class="n">contour</span><span class="o">-></span><span class="n">GetOutput</span><span class="p">()</span><span class="o">-></span><span class="n">GetNumberOfPoints</span><span class="p">()</span> <span class="o"><<</span> <span class="n">endl</span><span class="p">;</span></code></pre></div>
<p>Careful readers probably noticed that there is a flaw in this approach: the
output of the threshold filter contains <em>all</em> of the points of the input.
Therefore, any point-based filters such as glyph and point statistics will
incorrectly produce results based on all input points. With the current VTK
data model, there is no easy way of fixing this issue. In the near future,
we will add support for masking (blanking) of points and cells to all datasets.
With this feature in place, one could easily keep track of all points touched
by the output grid during tresholding and mask all others. In fact, if masking
support was in the data model, we wouldn't have to create a new unstructured
grid but directly mask cells and points that are not needed. A blog for another
time.</p>
</div>
</div>
<div class="pagination">
<a class="pagination-item older" href="/page2">Older</a>
<span class="pagination-item newer">Newer</span>
</div>
<div class="footer">
<p>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png" /></a>This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.
</p>
</div>
</div>
</body>
</html>