forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkEuclideanClusterExtraction.cxx
461 lines (394 loc) · 13.9 KB
/
vtkEuclideanClusterExtraction.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*=========================================================================
Program: Visualization Toolkit
Module: vtkEuclideanClusterExtraction.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 "vtkEuclideanClusterExtraction.h"
#include "vtkPointSet.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkFloatArray.h"
#include "vtkAbstractPointLocator.h"
#include "vtkStaticPointLocator.h"
#include "vtkIdTypeArray.h"
vtkStandardNewMacro(vtkEuclideanClusterExtraction);
vtkCxxSetObjectMacro(vtkEuclideanClusterExtraction,Locator,vtkAbstractPointLocator);
//----------------------------------------------------------------------------
// Construct with default extraction mode to extract largest cluster.
vtkEuclideanClusterExtraction::vtkEuclideanClusterExtraction()
{
this->ClusterSizes = vtkIdTypeArray::New();
this->ExtractionMode = VTK_EXTRACT_LARGEST_CLUSTER;
this->ColorClusters = false;
this->ScalarConnectivity = false;
this->ScalarRange[0] = 0.0;
this->ScalarRange[1] = 1.0;
this->ClosestPoint[0] = this->ClosestPoint[1] = this->ClosestPoint[2] = 0.0;
this->Locator = vtkStaticPointLocator::New();
this->NeighborScalars = vtkFloatArray::New();
this->NeighborScalars->Allocate(64);
this->NeighborPointIds = vtkIdList::New();
this->NeighborPointIds->Allocate(64);
this->Seeds = vtkIdList::New();
this->SpecifiedClusterIds = vtkIdList::New();
this->NewScalars = nullptr;
}
//----------------------------------------------------------------------------
vtkEuclideanClusterExtraction::~vtkEuclideanClusterExtraction()
{
this->SetLocator(nullptr);
this->ClusterSizes->Delete();
this->NeighborScalars->Delete();
this->NeighborPointIds->Delete();
this->Seeds->Delete();
this->SpecifiedClusterIds->Delete();
}
//----------------------------------------------------------------------------
int vtkEuclideanClusterExtraction::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()));
vtkIdType numPts, i, ptId;
vtkPoints *newPts;
int maxPointsInCluster, clusterId, largestClusterId=0;
vtkPointData *pd=input->GetPointData(), *outputPD=output->GetPointData();
vtkDebugMacro(<<"Executing point clustering filter.");
// Check input/allocate storage
//
if ( (numPts=input->GetNumberOfPoints()) < 1 )
{
vtkDebugMacro(<<"No data to cluster!");
return 1;
}
vtkPoints *inPts = input->GetPoints();
// Need to build a locator
if ( !this->Locator )
{
vtkErrorMacro(<<"Point locator required\n");
return 0;
}
this->Locator->SetDataSet(input);
this->Locator->BuildLocator();
// See whether to consider scalar connectivity
//
this->InScalars = input->GetPointData()->GetScalars();
if ( !this->ScalarConnectivity )
{
this->InScalars = nullptr;
}
else
{
this->NeighborScalars->SetNumberOfComponents(this->InScalars->GetNumberOfComponents());
if ( this->ScalarRange[1] < this->ScalarRange[0] )
{
this->ScalarRange[1] = this->ScalarRange[0];
}
}
// Initialize. Keep track of the points visited.
//
this->Visited = new char [numPts];
std::fill_n(this->Visited, numPts, static_cast<char>(0));
this->ClusterSizes->Reset();
this->PointMap = new vtkIdType[numPts];
std::fill_n(this->PointMap, numPts, static_cast<vtkIdType>(-1));
this->NewScalars = vtkIdTypeArray::New();
this->NewScalars->SetName("ClusterId");
this->NewScalars->SetNumberOfTuples(numPts);
newPts = vtkPoints::New();
newPts->SetDataType(input->GetPoints()->GetDataType());
newPts->Allocate(numPts);
// Traverse all points marking those visited. Each new search
// starts a new connected cluster. Connected clusters grow
// using a connected wave propagation.
//
this->Wave = vtkIdList::New();
this->Wave->Allocate(numPts/4+1,numPts);
this->Wave2 = vtkIdList::New();
this->Wave2->Allocate(numPts/4+1,numPts);
this->PointNumber = 0;
this->ClusterNumber = 0;
maxPointsInCluster = 0;
this->PointIds = vtkIdList::New();
this->PointIds->Allocate(8, VTK_CELL_SIZE);
if ( this->ExtractionMode != VTK_EXTRACT_POINT_SEEDED_CLUSTERS &&
this->ExtractionMode != VTK_EXTRACT_CLOSEST_POINT_CLUSTER )
{ //visit all points assigning cluster number
for (ptId=0; ptId < numPts; ptId++)
{
if ( ptId && !(ptId % 10000) )
{
this->UpdateProgress (0.1 + 0.8*ptId/numPts);
}
if ( ! this->Visited[ptId] )
{
this->NumPointsInCluster = 0;
this->InsertIntoWave(this->Wave,ptId);
this->TraverseAndMark (inPts);
if ( this->NumPointsInCluster > maxPointsInCluster )
{
maxPointsInCluster = this->NumPointsInCluster;
largestClusterId = this->ClusterNumber;
}
if ( this->NumPointsInCluster > 0 )
{
this->ClusterSizes->InsertValue(this->ClusterNumber++,
this->NumPointsInCluster);
}
this->Wave->Reset();
this->Wave2->Reset();
}
}
}
else // clusters have been seeded, everything considered in same cluster
{
this->NumPointsInCluster = 0;
if ( this->ExtractionMode == VTK_EXTRACT_POINT_SEEDED_CLUSTERS )
{
this->NumPointsInCluster = 0;
for (i=0; i < this->Seeds->GetNumberOfIds(); i++)
{
ptId = this->Seeds->GetId(i);
if ( ptId >= 0 )
{
this->InsertIntoWave(this->Wave,ptId);
}
}
}
else if ( this->ExtractionMode == VTK_EXTRACT_CLOSEST_POINT_CLUSTER )
{//loop over points, find closest one
ptId = this->Locator->FindClosestPoint(this->ClosestPoint);
this->InsertIntoWave(this->Wave,ptId);
}
this->UpdateProgress (0.5);
//mark all seeded clusters
this->TraverseAndMark (inPts);
this->ClusterSizes->InsertValue(this->ClusterNumber,this->NumPointsInCluster);
this->UpdateProgress (0.9);
}
vtkDebugMacro (<<"Extracted " << this->ClusterNumber << " cluster(s)");
this->Wave->Delete();
this->Wave2->Delete();
delete [] this->Visited;
// Now that points have been marked, traverse the PointMap pulling
// everything that has been visited and is selected for output.
outputPD->CopyAllocate(pd);
if ( this->ExtractionMode == VTK_EXTRACT_POINT_SEEDED_CLUSTERS ||
this->ExtractionMode == VTK_EXTRACT_CLOSEST_POINT_CLUSTER ||
this->ExtractionMode == VTK_EXTRACT_ALL_CLUSTERS)
{ // extract any point that's been visited
for (ptId=0; ptId < numPts; ptId++)
{
if ( this->PointMap[ptId] >= 0 )
{
newPts->InsertPoint(this->PointMap[ptId],inPts->GetPoint(ptId));
outputPD->CopyData(pd,ptId,this->PointMap[ptId]);
}
}
}
else if ( this->ExtractionMode == VTK_EXTRACT_SPECIFIED_CLUSTERS )
{
bool inCluster;
for (ptId=0; ptId < numPts; ptId++)
{
if ( this->PointMap[ptId] >= 0 )
{
clusterId = this->NewScalars->GetValue(this->PointMap[ptId]);
for (inCluster=false,i=0; i<this->SpecifiedClusterIds->GetNumberOfIds(); i++)
{
if ( clusterId == this->SpecifiedClusterIds->GetId(i) )
{
inCluster = true;
break;
}
}
if ( inCluster )
{
newPts->InsertPoint(this->PointMap[ptId],inPts->GetPoint(ptId));
outputPD->CopyData(pd,ptId,this->PointMap[ptId]);
}
}
}
}
else //extract largest cluster
{
for (ptId=0; ptId < numPts; ptId++)
{
if ( this->PointMap[ptId] >= 0 )
{
clusterId = this->NewScalars->GetValue(this->PointMap[ptId]);
if ( clusterId == largestClusterId )
{
newPts->InsertPoint(this->PointMap[ptId],inPts->GetPoint(ptId));
outputPD->CopyData(pd,ptId,this->PointMap[ptId]);
}
}
}
}
// if coloring clusters; send down new scalar data
if ( this->ColorClusters )
{
int idx = outputPD->AddArray(this->NewScalars);
outputPD->SetActiveAttribute(idx, vtkDataSetAttributes::SCALARS);
}
this->NewScalars->Delete();
newPts->Squeeze();
output->SetPoints(newPts);
delete [] this->PointMap;
this->PointIds->Delete();
// print out some debugging information
int num = this->GetNumberOfExtractedClusters();
int count = 0;
for (int ii = 0; ii < num; ii++)
{
count += this->ClusterSizes->GetValue (ii);
}
vtkDebugMacro (<< "Total # of points accounted for: " << count);
vtkDebugMacro (<< "Extracted " << newPts->GetNumberOfPoints() << " points");
newPts->Delete();
return 1;
}
//----------------------------------------------------------------------------
// Insert point into connected wave. Check to make sure it satisfies connectivity
// criterion (if enabled).
void vtkEuclideanClusterExtraction::
InsertIntoWave(vtkIdList *wave, vtkIdType ptId)
{
this->Visited[ptId] = 1;
if ( this->InScalars ) //is scalar connectivity enabled?
{
double s = this->InScalars->GetTuple1(ptId);
if ( s >= this->ScalarRange[0] && s <= this->ScalarRange[1] )
{
wave->InsertNextId(ptId);
}
}
else
{
wave->InsertNextId(ptId);
}
}
//----------------------------------------------------------------------------
// Update current point information including updating cluster number. Note:
// traversal occurs across proximally located points, possibly limited by
// scalar connectivty.
//
void vtkEuclideanClusterExtraction::TraverseAndMark (vtkPoints *inPts)
{
vtkIdType i, j, numPts, numIds, ptId;
vtkIdList *tmpWave;
double x[3];
while ( (numIds=this->Wave->GetNumberOfIds()) > 0 )
{
for ( i=0; i < numIds; i++ ) //for all points in this wave
{
ptId = this->Wave->GetId(i);
this->PointMap[ptId] = this->PointNumber++;
this->NewScalars->SetValue(this->PointMap[ptId],this->ClusterNumber);
this->NumPointsInCluster++;
inPts->GetPoint(ptId,x);
this->Locator->FindPointsWithinRadius(this->Radius,x,this->NeighborPointIds);
numPts = this->NeighborPointIds->GetNumberOfIds();
for (j=0; j < numPts; ++j)
{
ptId = this->NeighborPointIds->GetId(j);
if ( ! this->Visited[ptId] )
{
this->InsertIntoWave(this->Wave2,ptId);
}//if point not yet visited
}//for all neighbors
}//for all cells in this connected wave
tmpWave = this->Wave;
this->Wave = this->Wave2;
this->Wave2 = tmpWave;
tmpWave->Reset();
} //while wave is not empty
}
//----------------------------------------------------------------------------
// Obtain the number of connected clusters.
int vtkEuclideanClusterExtraction::GetNumberOfExtractedClusters()
{
return this->ClusterSizes->GetMaxId() + 1;
}
//----------------------------------------------------------------------------
// Initialize list of point ids used to seed clusters.
void vtkEuclideanClusterExtraction::InitializeSeedList()
{
this->Modified();
this->Seeds->Reset();
}
//----------------------------------------------------------------------------
// Add a seed id. Note: ids are 0-offset.
void vtkEuclideanClusterExtraction::AddSeed(vtkIdType id)
{
this->Modified();
this->Seeds->InsertNextId(id);
}
//----------------------------------------------------------------------------
// Delete a seed id. Note: ids are 0-offset.
void vtkEuclideanClusterExtraction::DeleteSeed(vtkIdType id)
{
this->Modified();
this->Seeds->DeleteId(id);
}
//----------------------------------------------------------------------------
// Initialize list of cluster ids to extract.
void vtkEuclideanClusterExtraction::InitializeSpecifiedClusterList()
{
this->Modified();
this->SpecifiedClusterIds->Reset();
}
//----------------------------------------------------------------------------
// Add a cluster id to extract. Note: ids are 0-offset.
void vtkEuclideanClusterExtraction::AddSpecifiedCluster(int id)
{
this->Modified();
this->SpecifiedClusterIds->InsertNextId(id);
}
//----------------------------------------------------------------------------
// Delete a cluster id to extract. Note: ids are 0-offset.
void vtkEuclideanClusterExtraction::DeleteSpecifiedCluster(int id)
{
this->Modified();
this->SpecifiedClusterIds->DeleteId(id);
}
//----------------------------------------------------------------------------
int vtkEuclideanClusterExtraction::
FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
return 1;
}
//----------------------------------------------------------------------------
void vtkEuclideanClusterExtraction::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Extraction Mode: ";
os << this->GetExtractionModeAsString() << "\n";
os << indent << "Closest Point: (" << this->ClosestPoint[0] << ", "
<< this->ClosestPoint[1] << ", " << this->ClosestPoint[2] << ")\n";
os << indent << "Color Clusters: " << (this->ColorClusters ? "On\n" : "Off\n");
os << indent << "Scalar Connectivity: "
<< (this->ScalarConnectivity ? "On\n" : "Off\n");
double *range = this->GetScalarRange();
os << indent << "Scalar Range: (" << range[0] << ", " << range[1] << ")\n";
os << indent << "Locator: " << this->Locator << "\n";
}