VMTK
Loading...
Searching...
No Matches
vtkvmtkStreamlineClusteringFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkStreamlineClusteringFilter.cxx,v $
5Language: C++
6Date: $Date: 2006/07/17 09:53:14 $
7Version: $Revision: 1.6 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23#include "vtkInformation.h"
24#include "vtkInformationVector.h"
25#include "vtkObjectFactory.h"
26#include "vtkVersion.h"
27
28#include "vtkvmtkConstants.h"
29
30#include "vtkDoubleArray.h"
31#include "vtkPointData.h"
32#include "vtkCellData.h"
33#include "vtkMath.h"
34#include "vtkSplineFilter.h"
35//#include "vtkPointLocator.h"
36#include "vtkCellArray.h"
37#include "vtkCell.h"
38
39
41
46
55
57 vtkInformation *vtkNotUsed(request),
58 vtkInformationVector **inputVector,
59 vtkInformationVector *outputVector)
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}
259
260void vtkvmtkStreamlineClusteringFilter::PrintSelf(std::ostream& os, vtkIndent indent)
261{
262 this->Superclass::PrintSelf(os,indent);
263}
Cluster streamlines based on Mahalanobis distance metric and K-Means clustering.
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkStreamlineClusteringFilter)