forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPointCloudFilter.cxx
355 lines (301 loc) · 10.6 KB
/
vtkPointCloudFilter.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPointCloudFilter.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 "vtkPointCloudFilter.h"
#include "vtkObjectFactory.h"
#include "vtkAbstractPointLocator.h"
#include "vtkStaticPointLocator.h"
#include "vtkPointSet.h"
#include "vtkDataArray.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkMath.h"
#include "vtkSMPTools.h"
#include "vtkSMPThreadLocalObject.h"
#include "vtkArrayListTemplate.h" // For processing attribute data
//----------------------------------------------------------------------------
// Helper classes to support efficient computing, and threaded execution.
namespace {
//----------------------------------------------------------------------------
// Map input points to output. Basically the third pass of the algorithm.
template <typename T>
struct MapPoints
{
T *InPoints;
T *OutPoints;
const vtkIdType *PointMap;
ArrayList Arrays;
MapPoints(vtkIdType, T *inPts, vtkIdType numOutPts, T *outPts,
vtkIdType *map, vtkPointData *inPD, vtkPointData *outPD) :
InPoints(inPts), OutPoints(outPts), PointMap(map)
{
this->Arrays.AddArrays(numOutPts, inPD, outPD, 0.0, false);
}
void operator() (vtkIdType ptId, vtkIdType endPtId)
{
T *inP, *outP;
const vtkIdType *map=this->PointMap;
vtkIdType outPtId;
for ( ; ptId < endPtId; ++ptId)
{
outPtId = map[ptId];
if ( outPtId != -1 )
{
inP = this->InPoints + 3*ptId;
outP = this->OutPoints + 3*outPtId;
*outP++ = *inP++;
*outP++ = *inP++;
*outP = *inP;
this->Arrays.Copy(ptId,outPtId);
}
}
}
static void Execute(vtkIdType numInPts, T *inPts,
vtkIdType numOutPts, T *outPts, vtkIdType *map,
vtkPointData *inPD, vtkPointData *outPD)
{
MapPoints copy(numInPts, inPts, numOutPts, outPts, map, inPD, outPD);
vtkSMPTools::For(0, numInPts, copy);
}
}; //MapPoints
//----------------------------------------------------------------------------
// Map outlier points to second output. This is an optional pass of the
// algorithm.
template <typename T>
struct MapOutliers
{
T *InPoints;
T *OutPoints;
const vtkIdType *PointMap;
ArrayList Arrays;
MapOutliers(vtkIdType, T *inPts, vtkIdType numOutPts, T *outPts,
vtkIdType *map, vtkPointData *inPD, vtkPointData *outPD2) :
InPoints(inPts), OutPoints(outPts), PointMap(map)
{
this->Arrays.AddArrays(numOutPts, inPD, outPD2, 0.0, false);
}
void operator() (vtkIdType ptId, vtkIdType endPtId)
{
T *inP, *outP;
const vtkIdType *map=this->PointMap;
vtkIdType outPtId;
for ( ; ptId < endPtId; ++ptId)
{
outPtId = map[ptId];
if ( outPtId < 0 )
{
outPtId = (-outPtId) - 1;
inP = this->InPoints + 3*ptId;
outP = this->OutPoints + 3*outPtId;
*outP++ = *inP++;
*outP++ = *inP++;
*outP = *inP;
this->Arrays.Copy(ptId,outPtId);
}
}
}
static void Execute(vtkIdType numInPts, T *inPts,
vtkIdType numOutPts, T *outPts, vtkIdType *map,
vtkPointData *inPD, vtkPointData *outPD2)
{
MapOutliers copy(numInPts, inPts, numOutPts, outPts, map, inPD, outPD2);
vtkSMPTools::For(0, numInPts, copy);
}
}; //MapOutliers
} //anonymous namespace
//================= Begin class proper =======================================
//----------------------------------------------------------------------------
vtkPointCloudFilter::vtkPointCloudFilter()
{
this->PointMap = nullptr;
this->NumberOfPointsRemoved = 0;
this->GenerateOutliers = false;
this->GenerateVertices = false;
// Optional second output of outliers
this->SetNumberOfOutputPorts(2);
}
//----------------------------------------------------------------------------
vtkPointCloudFilter::~vtkPointCloudFilter()
{
delete [] this->PointMap;
}
//----------------------------------------------------------------------------
const vtkIdType* vtkPointCloudFilter::GetPointMap()
{
return this->PointMap;
}
//----------------------------------------------------------------------------
vtkIdType vtkPointCloudFilter::GetNumberOfPointsRemoved()
{
return this->NumberOfPointsRemoved;
}
//----------------------------------------------------------------------------
// There are three high level passes. First we traverse all the input points
// to see how many neighbors each point has within a specified radius, and a
// map is created indicating whether an input point is to be copied to the
// output. Next a prefix sum is used to count the output points, and to
// update the mapping between the input and the output. Finally, non-removed
// input points (and associated attributes) are copied to the output.
int vtkPointCloudFilter::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkPointSet *input = vtkPointSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Reset the filter
this->NumberOfPointsRemoved = 0;
delete [] this->PointMap; //might have executed previously
// Check input
if ( !input || !output )
{
return 1;
}
vtkIdType numPts = input->GetNumberOfPoints();
if ( numPts < 1 )
{
return 1;
}
// Okay invoke filtering operation. This is always the initial pass.
this->PointMap = new vtkIdType[numPts];
if ( ! this->FilterPoints(input) )
{
return 1;
}
// Count the resulting points (prefix sum). The second pass of the algorithm; it
// could be threaded but prefix sum does not benefit very much from threading.
vtkIdType ptId;
vtkIdType count=0;
vtkIdType *map = this->PointMap;
for (ptId=0; ptId < numPts; ++ptId)
{
if ( map[ptId] != -1 )
{
map[ptId] = count;
count++;
}
}
this->NumberOfPointsRemoved = numPts - count;
// If the number of input and output points is the same we short circuit
// the process. Otherwise, copy the masked input points to the output.
vtkPointData *inPD = input->GetPointData();
vtkPointData *outPD = output->GetPointData();
if ( this->NumberOfPointsRemoved == 0 )
{
output->SetPoints(input->GetPoints());
outPD->PassData(inPD);
this->GenerateVerticesIfRequested(output);
return 1;
}
// Okay copy the points from the input to the output. We use a threaded
// operation that provides a minor benefit (since it's mostly data
// movement with almost no computation).
outPD->CopyAllocate(inPD,count);
vtkPoints *points = input->GetPoints()->NewInstance();
points->SetDataType(input->GetPoints()->GetDataType());
points->SetNumberOfPoints(count);
output->SetPoints(points);
void *inPtr = input->GetPoints()->GetVoidPointer(0);
void *outPtr = output->GetPoints()->GetVoidPointer(0);
switch (output->GetPoints()->GetDataType())
{
vtkTemplateMacro(MapPoints<VTK_TT>::Execute(numPts, (VTK_TT *)inPtr, count,
(VTK_TT *)outPtr, this->PointMap, inPD, outPD));
}
// Generate poly vertex cell if requested
this->GenerateVerticesIfRequested(output);
// Clean up. We leave the map in case the user wants to use it.
points->Delete();
// Create the second output if requested. Note that we are using a negative
// count in the map (offset by -1) which indicates the final position of
// the output point in the second output.
if ( this->GenerateOutliers && this->NumberOfPointsRemoved > 0 )
{
vtkInformation *outInfo2 = outputVector->GetInformationObject(1);
// get the second output
vtkPolyData *output2 = vtkPolyData::SafeDownCast(
outInfo2->Get(vtkDataObject::DATA_OBJECT()));
vtkPointData *outPD2 = output2->GetPointData();
outPD2->CopyAllocate(inPD,(count-1));
// Update map
count = 1; //offset by one
map = this->PointMap;
for (ptId=0; ptId < numPts; ++ptId)
{
if ( map[ptId] == -1 )
{
map[ptId] = (-count);
count++;
}
}
// Copy to second output
vtkPoints *points2 = input->GetPoints()->NewInstance();
points2->SetDataType(input->GetPoints()->GetDataType());
points2->SetNumberOfPoints(count-1);
output2->SetPoints(points2);
void *outPtr2 = output2->GetPoints()->GetVoidPointer(0);
switch (output->GetPoints()->GetDataType())
{
vtkTemplateMacro(MapOutliers<VTK_TT>::Execute(numPts, (VTK_TT *)inPtr, (count-1),
(VTK_TT *)outPtr2, this->PointMap, inPD, outPD2));
}
points2->Delete();
// Produce poly vertex cell if requested
this->GenerateVerticesIfRequested(output2);
}
return 1;
}
//----------------------------------------------------------------------------
void vtkPointCloudFilter::GenerateVerticesIfRequested(vtkPolyData *output)
{
vtkIdType numPts;
if ( ! this->GenerateVertices || output->GetPoints() == nullptr ||
(numPts=output->GetNumberOfPoints()) < 1)
{
return;
}
// Okay create a cell array and assign it to the output
vtkCellArray *verts = vtkCellArray::New();
verts->EstimateSize(1,numPts);
verts->InsertNextCell(numPts);
for (vtkIdType ptId=0; ptId < numPts; ++ptId)
{
verts->InsertCellPoint(ptId);
}
output->SetVerts(verts);
verts->Delete();
}
//----------------------------------------------------------------------------
int vtkPointCloudFilter::
FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
return 1;
}
//----------------------------------------------------------------------------
void vtkPointCloudFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Number of Points Removed: "
<< this->NumberOfPointsRemoved << "\n";
os << indent << "Generate Outliers: "
<< (this->GenerateOutliers ? "On\n" : "Off\n");
os << indent << "Generate Vertices: "
<< (this->GenerateVertices ? "On\n" : "Off\n");
}