VMTK
Loading...
Searching...
No Matches
vtkvmtkStreamlineClusteringFilter Class Reference

Cluster streamlines based on Mahalanobis distance metric and K-Means clustering. More...

#include <vtkvmtkStreamlineClusteringFilter.h>

Inheritance diagram for vtkvmtkStreamlineClusteringFilter:
[legend]
Collaboration diagram for vtkvmtkStreamlineClusteringFilter:
[legend]

Public Types

typedef vtkPolyDataAlgorithm Superclass

Public Member Functions

virtual int IsA (const char *type)
vtkvmtkStreamlineClusteringFilter * NewInstance () const
void PrintSelf (std::ostream &os, vtkIndent indent) override
virtual vtkPolyData * GetClusterCenters ()

Static Public Member Functions

static int IsTypeOf (const char *type)
static vtkvmtkStreamlineClusteringFilter * SafeDownCast (vtkObjectBase *o)
static vtkvmtkStreamlineClusteringFilter * New ()

Protected Member Functions

virtual vtkObjectBase * NewInstanceInternal () const
 vtkvmtkStreamlineClusteringFilter ()
 ~vtkvmtkStreamlineClusteringFilter ()
virtual int RequestData (vtkInformation *, vtkInformationVector **, vtkInformationVector *) override

Protected Attributes

vtkPolyData * ClusterCenters

Detailed Description

Cluster streamlines based on Mahalanobis distance metric and K-Means clustering.

Groups the input polyline cells (streamlines, e.g. as produced by particle tracing on a CFD velocity field) into clusters of similar trajectories using K-Means clustering with a Mahalanobis distance metric between resampled streamline point sequences. The output is a copy of the input with an added cell data array identifying each streamline's cluster; the representative center streamline of each cluster is available via GetClusterCenters().

Definition at line 36 of file vtkvmtkStreamlineClusteringFilter.h.

Member Typedef Documentation

◆ Superclass

typedef vtkPolyDataAlgorithm vtkvmtkStreamlineClusteringFilter::Superclass

Definition at line 39 of file vtkvmtkStreamlineClusteringFilter.h.

Constructor & Destructor Documentation

◆ vtkvmtkStreamlineClusteringFilter()

vtkvmtkStreamlineClusteringFilter::vtkvmtkStreamlineClusteringFilter ( )
protected

Definition at line 42 of file vtkvmtkStreamlineClusteringFilter.cxx.

◆ ~vtkvmtkStreamlineClusteringFilter()

vtkvmtkStreamlineClusteringFilter::~vtkvmtkStreamlineClusteringFilter ( )
protected

Definition at line 47 of file vtkvmtkStreamlineClusteringFilter.cxx.

48{
49 if (this->ClusterCenters)
50 {
51 this->ClusterCenters->Delete();
52 this->ClusterCenters = NULL;
53 }
54}

Member Function Documentation

◆ IsTypeOf()

int vtkvmtkStreamlineClusteringFilter::IsTypeOf ( const char * type)
static

◆ IsA()

virtual int vtkvmtkStreamlineClusteringFilter::IsA ( const char * type)
virtual

◆ SafeDownCast()

vtkvmtkStreamlineClusteringFilter * vtkvmtkStreamlineClusteringFilter::SafeDownCast ( vtkObjectBase * o)
static

◆ NewInstanceInternal()

virtual vtkObjectBase * vtkvmtkStreamlineClusteringFilter::NewInstanceInternal ( ) const
protectedvirtual

◆ NewInstance()

vtkvmtkStreamlineClusteringFilter * vtkvmtkStreamlineClusteringFilter::NewInstance ( ) const

◆ PrintSelf()

void vtkvmtkStreamlineClusteringFilter::PrintSelf ( std::ostream & os,
vtkIndent indent )
override

Definition at line 260 of file vtkvmtkStreamlineClusteringFilter.cxx.

261{
262 this->Superclass::PrintSelf(os,indent);
263}

◆ New()

vtkvmtkStreamlineClusteringFilter * vtkvmtkStreamlineClusteringFilter::New ( )
static

◆ GetClusterCenters()

virtual vtkPolyData * vtkvmtkStreamlineClusteringFilter::GetClusterCenters ( )
virtual

Get the representative center streamline of each cluster, one polyline cell per cluster, with the cluster id stored in a "Label" cell data array. Valid only after Update() has been called.

◆ RequestData()

int vtkvmtkStreamlineClusteringFilter::RequestData ( vtkInformation * ,
vtkInformationVector ** ,
vtkInformationVector *  )
overrideprotectedvirtual

Definition at line 56 of file vtkvmtkStreamlineClusteringFilter.cxx.

60{
61 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
62 vtkInformation *outInfo = outputVector->GetInformationObject(0);
63
64 vtkPolyData *input = vtkPolyData::SafeDownCast(
65 inInfo->Get(vtkDataObject::DATA_OBJECT()));
66 vtkPolyData *output = vtkPolyData::SafeDownCast(
67 outInfo->Get(vtkDataObject::DATA_OBJECT()));
68
69 if (input->GetNumberOfPoints() < 1)
70 {
71 return 1;
72 }
73
74 double resampleLength = 0.01;
75 int numberOfChunkPoints = 40;
76 int numberOfClusters = 4;
77
78 vtkSplineFilter* splineFilter = vtkSplineFilter::New();
79 splineFilter->SetInputData(input);
80 splineFilter->SetSubdivideToLength();
81 splineFilter->SetLength(resampleLength);
82 splineFilter->Update();
83
84 vtkPolyData* resampledInput = splineFilter->GetOutput();
85
86 vtkPolyData* streamlines = vtkPolyData::New();
87 vtkPoints* streamlinesPoints = vtkPoints::New();
88 vtkCellArray* streamlinesLines = vtkCellArray::New();
89 streamlines->SetPoints(streamlinesPoints);
90 streamlines->SetLines(streamlinesLines);
91
92 streamlinesPoints->DeepCopy(splineFilter->GetOutput()->GetPoints());
93
94 int numberOfCells = resampledInput->GetNumberOfCells();
95
96 vtkIdList* inputCellIds = vtkIdList::New();
97 vtkIdList* inputLabelIds = vtkIdList::New();
98 int currentLabelId = -1;
99 for (int i=0; i<numberOfCells; i++)
100 {
101 bool isCluster = false;
102 if (i%(numberOfCells/numberOfClusters) == 0)
103 {
104 isCluster = true;
105 currentLabelId++;
106 }
107
108 vtkCell* line = resampledInput->GetCell(i);
109 int numberOfLinePoints = line->GetNumberOfPoints();
110 for (int j=0; j<numberOfLinePoints; j++)
111 {
112 if (j%numberOfChunkPoints == 0)
113 {
114 int numberOfChunkLinePoints = numberOfChunkPoints;
115 if (numberOfLinePoints-j < numberOfChunkPoints)
116 {
117 numberOfChunkLinePoints = numberOfLinePoints-j;
118 }
119 int id = streamlinesLines->InsertNextCell(numberOfChunkLinePoints);
120 if (isCluster)
121 {
122 inputCellIds->InsertNextId(id);
123 inputLabelIds->InsertNextId(currentLabelId);
124 }
125 }
126 int pointId = line->GetPointId(j);
127 streamlinesLines->InsertCellPoint(pointId);
128 }
129 }
130
131 numberOfCells = streamlines->GetNumberOfCells();
132
133 numberOfClusters = inputCellIds->GetNumberOfIds();
134
135 vtkPolyData* clusterCenters = vtkPolyData::New();
136 vtkPoints* clusterCenterPoints = vtkPoints::New();
137 vtkCellArray* clusterCenterLines = vtkCellArray::New();
138 clusterCenters->SetPoints(clusterCenterPoints);
139 clusterCenters->SetLines(clusterCenterLines);
140
141 vtkIntArray* centerLabelArray = vtkIntArray::New();
142 centerLabelArray->SetName("Label");
143
144 for (int i=0; i<numberOfClusters; i++)
145 {
146 vtkCell* line = streamlines->GetCell(inputCellIds->GetId(i));
147 int numberOfLinePoints = line->GetNumberOfPoints();
148 clusterCenterLines->InsertNextCell(numberOfLinePoints);
149 centerLabelArray->InsertNextValue(inputLabelIds->GetId(i));
150 for (int j=0; j<numberOfLinePoints; j++)
151 {
152 double point[3];
153 streamlines->GetPoint(line->GetPointId(j),point);
154 int pointId = clusterCenterPoints->InsertNextPoint(point);
155 clusterCenterLines->InsertCellPoint(pointId);
156 }
157 }
158
159 clusterCenters->GetCellData()->AddArray(centerLabelArray);
160
161// vtkDoubleArray* covariance = vtkDoubleArray::New();
162// covariance->SetName("PointCovariance");
163// covariance->SetNumberOfComponents(3);
164// covariance->SetNumberOfTuples(clusterCenterPoints->GetNumberOfPoints());
165
166// clusterCenters->GetPointData()->AddArray(covariance);
167
168// vtkPointLocator* pointLocator = vtkPointLocator::New();
169// pointLocator->SetDataSet(clusterCenters);
170// pointLocator->BuildLocator();
171
172 vtkIntArray* labelArray = vtkIntArray::New();
173 labelArray->SetName("Label");
174 labelArray->SetNumberOfValues(numberOfCells);
175
176 vtkDoubleArray* distance = vtkDoubleArray::New();
177 distance->SetName("Distance");
178 distance->SetNumberOfTuples(numberOfCells);
179
180 for (int i=0; i<numberOfCells; i++)
181 {
182 double point[3], clusterCenterPoint[3];
183 vtkCell* streamline = streamlines->GetCell(i);
184 double minClusterCenterDistance = VTK_VMTK_LARGE_DOUBLE;
185 int minClusterCenterDistanceId = -1;
186 for (int j=0; j<numberOfClusters; j++)
187 {
188 vtkCell* clusterCenterLine = clusterCenters->GetCell(j);
189 int numberOfClusterCenterLinePoints = clusterCenterLine->GetNumberOfPoints();
190 double squaredDistanceSum = 0.0;
191 int numberOfRepeatedMatches = 0;
192 int numberOfStreamlinePoints = streamline->GetNumberOfPoints();
193 vtkIdList* correspondenceIds = vtkIdList::New();
194 for (int ip=0; ip<numberOfStreamlinePoints; ip++)
195 {
196 streamlines->GetPoint(streamline->GetPointId(ip),point);
197 double minPointDistanceSquared = VTK_VMTK_LARGE_DOUBLE;
198 int minPointDistanceId = -1;
199 for (int jp=0; jp<numberOfClusterCenterLinePoints; jp++)
200 {
201 clusterCenters->GetPoint(clusterCenterLine->GetPointId(jp),clusterCenterPoint);
202 double distanceSquared = vtkMath::Distance2BetweenPoints(point,clusterCenterPoint);
203 if (distanceSquared < minPointDistanceSquared)
204 {
205 minPointDistanceSquared = distanceSquared;
206 minPointDistanceId = jp;
207 }
208 }
209 squaredDistanceSum += minPointDistanceSquared;
210 if (correspondenceIds->IsId(minPointDistanceId) != -1)
211 {
212 numberOfRepeatedMatches++;
213 }
214 else
215 {
216 correspondenceIds->InsertNextId(minPointDistanceId);
217 }
218 }
219 correspondenceIds->Delete();
220 double clusterCenterDistance = sqrt(squaredDistanceSum);
221 double averageDistance = clusterCenterDistance / numberOfStreamlinePoints;
222 double distancePenalty = (numberOfClusterCenterLinePoints - numberOfStreamlinePoints + numberOfRepeatedMatches) * averageDistance;
223 double distance = (clusterCenterDistance + distancePenalty) / numberOfStreamlinePoints;
224 if (distance < minClusterCenterDistance)
225 {
226 minClusterCenterDistance = distance;
227 minClusterCenterDistanceId = inputLabelIds->GetId(j);
228 }
229 }
230 labelArray->InsertValue(i,minClusterCenterDistanceId);
231 distance->InsertValue(i,minClusterCenterDistance);
232 }
233
234 output->DeepCopy(streamlines);
235 output->GetCellData()->AddArray(labelArray);
236 output->GetCellData()->AddArray(distance);
237
238 if (this->ClusterCenters)
239 {
240 this->ClusterCenters->Delete();
241 this->ClusterCenters = NULL;
242 }
243
244 this->ClusterCenters = vtkPolyData::New();
245 this->ClusterCenters->DeepCopy(clusterCenters);
246
247 splineFilter->Delete();
248 inputCellIds->Delete();
249 clusterCenters->Delete();
250 clusterCenterPoints->Delete();
251 clusterCenterLines->Delete();
252// covariance->Delete();
253// pointLocator->Delete();
254 labelArray->Delete();
255 centerLabelArray->Delete();
256
257 return 1;
258}
#define VTK_VMTK_LARGE_DOUBLE

Member Data Documentation

◆ ClusterCenters

vtkPolyData* vtkvmtkStreamlineClusteringFilter::ClusterCenters
protected

Definition at line 56 of file vtkvmtkStreamlineClusteringFilter.h.


The documentation for this class was generated from the following files: