forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkMaskPointsFilter.cxx
296 lines (253 loc) · 9.36 KB
/
vtkMaskPointsFilter.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMaskPointsFilter.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See LICENSE file for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkMaskPointsFilter.h"
#include "vtkObjectFactory.h"
#include "vtkImageData.h"
#include "vtkPointSet.h"
#include "vtkPoints.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkSMPTools.h"
vtkStandardNewMacro(vtkMaskPointsFilter);
//----------------------------------------------------------------------------
// Helper classes to support efficient computing, and threaded execution.
namespace {
//----------------------------------------------------------------------------
// The threaded core of the algorithm
template <typename T>
struct ExtractPoints
{
unsigned char *Mask;
unsigned char EmptyValue;
const T *Points;
vtkIdType *PointMap;
double hX, hY, hZ; //internal data members for performance
double fX, fY, fZ, bX, bY, bZ;
vtkIdType xD, yD, zD, xyD;
ExtractPoints(unsigned char *mask, unsigned char ev, int dims[3],
double origin[3], double spacing[3], T *points, vtkIdType *map) :
Mask(mask), EmptyValue(ev), Points(points), PointMap(map)
{
this->hX = spacing[0];
this->hY = spacing[1];
this->hZ = spacing[2];
this->fX = 1.0 / spacing[0];
this->fY = 1.0 / spacing[1];
this->fZ = 1.0 / spacing[2];
this->bX = origin[0] - 0.5*this->hX;
this->bY = origin[1] - 0.5*this->hY;
this->bZ = origin[2] - 0.5*this->hZ;
this->xD = dims[0];
this->yD = dims[1];
this->zD = dims[2];
this->xyD = dims[0] * dims[1];
}
void operator() (vtkIdType ptId, vtkIdType endPtId)
{
const T *x = this->Points + 3*ptId;
vtkIdType *map = this->PointMap + ptId;
const unsigned char *mask = this->Mask;
const unsigned char emptyValue = this->EmptyValue;
int i, j, k;
for ( ; ptId < endPtId; ++ptId, x+=3, ++map)
{
i = static_cast<int>(((x[0] - this->bX) * this->fX));
j = static_cast<int>(((x[1] - this->bY) * this->fY));
k = static_cast<int>(((x[2] - this->bZ) * this->fZ));
// If not inside image then skip
if ( i < 0 || i >= this->xD ||
j < 0 || j >= this->yD ||
k < 0 || k >= this->zD )
{
*map = -1;
}
else if ( mask[i + j*this->xD + k*this->xyD] != emptyValue )
{
*map = 1;
}
else
{
*map = -1;
}
}
}
static void Execute(unsigned char *mask, unsigned char ev, int dims[3],
double origin[3], double spacing[3], vtkIdType numPts,
T *points, vtkIdType *map)
{
ExtractPoints extract(mask, ev, dims, origin, spacing, points, map);
vtkSMPTools::For(0, numPts, extract);
}
}; //ExtractPoints
} //anonymous namespace
//================= Begin class proper =======================================
//----------------------------------------------------------------------------
vtkMaskPointsFilter::vtkMaskPointsFilter()
{
this->SetNumberOfInputPorts(2);
this->EmptyValue = 0;
this->Mask = nullptr;
}
//----------------------------------------------------------------------------
vtkMaskPointsFilter::~vtkMaskPointsFilter() = default;
//----------------------------------------------------------------------------
int vtkMaskPointsFilter::
FillInputPortInformation(int port, vtkInformation *info)
{
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
return 1;
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
void vtkMaskPointsFilter::SetMaskConnection(vtkAlgorithmOutput* algOutput)
{
this->SetInputConnection(1, algOutput);
}
//----------------------------------------------------------------------------
void vtkMaskPointsFilter::SetMaskData(vtkDataObject *input)
{
this->SetInputData(1, input);
}
//----------------------------------------------------------------------------
vtkDataObject *vtkMaskPointsFilter::GetMask()
{
if (this->GetNumberOfInputConnections(1) < 1)
{
return nullptr;
}
return this->GetExecutive()->GetInputData(1, 0);
}
//----------------------------------------------------------------------------
// Traverse all the input points and extract points that are contained within
// the mask.
int vtkMaskPointsFilter::FilterPoints(vtkPointSet *input)
{
// Grab the image data and metadata. The type and existence of image data
// should have been checked in RequestData().
double origin[3], spacing[3];
int dims[3];
this->Mask->GetDimensions(dims);
this->Mask->GetOrigin(origin);
this->Mask->GetSpacing(spacing);
unsigned char ev = this->EmptyValue;
unsigned char *m =
static_cast<unsigned char*>(this->Mask->GetScalarPointer());
// Determine which points, if any, should be removed. We create a map
// to keep track. The bulk of the algorithmic work is done in this pass.
vtkIdType numPts = input->GetNumberOfPoints();
void *inPtr = input->GetPoints()->GetVoidPointer(0);
switch (input->GetPoints()->GetDataType())
{
vtkTemplateMacro(ExtractPoints<VTK_TT>::Execute(m, ev, dims, origin, spacing,
numPts, (VTK_TT *)inPtr, this->PointMap));
}
return 1;
}
//----------------------------------------------------------------------------
// Due to the second input, retrieve it and then invoke the superclass
// RequestData.
int vtkMaskPointsFilter::RequestData(
vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *maskInfo = inputVector[1]->GetInformationObject(0);
// get the mask
this->Mask = vtkImageData::SafeDownCast(
maskInfo->Get(vtkDataObject::DATA_OBJECT()));
if ( !this->Mask )
{
vtkWarningMacro(<<"No image mask available");
return 1;
}
if ( this->Mask->GetScalarType() != VTK_UNSIGNED_CHAR )
{
vtkWarningMacro(<<"Image mask must be unsigned char type");
return 1;
}
return this->Superclass::RequestData(request, inputVector, outputVector);
}
//----------------------------------------------------------------------------
int vtkMaskPointsFilter::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *maskInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
outInfo->CopyEntry(maskInfo,
vtkStreamingDemandDrivenPipeline::TIME_STEPS());
outInfo->CopyEntry(maskInfo,
vtkStreamingDemandDrivenPipeline::TIME_RANGE());
outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()),
6);
// Make sure that the scalar type and number of components
// are propagated from the mask not the input.
if (vtkImageData::HasScalarType(maskInfo))
{
vtkImageData::SetScalarType(vtkImageData::GetScalarType(maskInfo),
outInfo);
}
if (vtkImageData::HasNumberOfScalarComponents(maskInfo))
{
vtkImageData::SetNumberOfScalarComponents(
vtkImageData::GetNumberOfScalarComponents(maskInfo),
outInfo);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkMaskPointsFilter::RequestUpdateExtent(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *maskInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(), 0);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(), 1);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(), 0);
maskInfo->Set(
vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER(),
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()));
maskInfo->Set(
vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES(),
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()));
maskInfo->Set(
vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS()));
maskInfo->Set(
vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
maskInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()),
6);
return 1;
}
//----------------------------------------------------------------------------
void vtkMaskPointsFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Empty Value: " << this->EmptyValue << "\n";
}