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
162
163
164
165
166
167
168
169
170
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);
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);
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
239 {
242 }
243
246
247 splineFilter->Delete();
248 inputCellIds->Delete();
249 clusterCenters->Delete();
250 clusterCenterPoints->Delete();
251 clusterCenterLines->Delete();
252
253
254 labelArray->Delete();
255 centerLabelArray->Delete();
256
257 return 1;
258}
#define VTK_VMTK_LARGE_DOUBLE