57 vtkInformation *vtkNotUsed(request),
58 vtkInformationVector **inputVector,
59 vtkInformationVector *outputVector)
61 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
62 vtkInformation *outInfo = outputVector->GetInformationObject(0);
64 vtkPolyData *input = vtkPolyData::SafeDownCast(
65 inInfo->Get(vtkDataObject::DATA_OBJECT()));
66 vtkPolyData *output = vtkPolyData::SafeDownCast(
67 outInfo->Get(vtkDataObject::DATA_OBJECT()));
69 if (input->GetNumberOfPoints() < 1)
74 double resampleLength = 0.01;
75 int numberOfChunkPoints = 40;
76 int numberOfClusters = 4;
78 vtkSplineFilter* splineFilter = vtkSplineFilter::New();
79 splineFilter->SetInputData(input);
80 splineFilter->SetSubdivideToLength();
81 splineFilter->SetLength(resampleLength);
82 splineFilter->Update();
84 vtkPolyData* resampledInput = splineFilter->GetOutput();
86 vtkPolyData* streamlines = vtkPolyData::New();
87 vtkPoints* streamlinesPoints = vtkPoints::New();
88 vtkCellArray* streamlinesLines = vtkCellArray::New();
89 streamlines->SetPoints(streamlinesPoints);
90 streamlines->SetLines(streamlinesLines);
92 streamlinesPoints->DeepCopy(splineFilter->GetOutput()->GetPoints());
94 int numberOfCells = resampledInput->GetNumberOfCells();
96 vtkIdList* inputCellIds = vtkIdList::New();
97 vtkIdList* inputLabelIds = vtkIdList::New();
98 int currentLabelId = -1;
99 for (
int i=0; i<numberOfCells; i++)
101 bool isCluster =
false;
102 if (i%(numberOfCells/numberOfClusters) == 0)
108 vtkCell* line = resampledInput->GetCell(i);
109 int numberOfLinePoints = line->GetNumberOfPoints();
110 for (
int j=0; j<numberOfLinePoints; j++)
112 if (j%numberOfChunkPoints == 0)
114 int numberOfChunkLinePoints = numberOfChunkPoints;
115 if (numberOfLinePoints-j < numberOfChunkPoints)
117 numberOfChunkLinePoints = numberOfLinePoints-j;
119 int id = streamlinesLines->InsertNextCell(numberOfChunkLinePoints);
122 inputCellIds->InsertNextId(
id);
123 inputLabelIds->InsertNextId(currentLabelId);
126 int pointId = line->GetPointId(j);
127 streamlinesLines->InsertCellPoint(pointId);
131 numberOfCells = streamlines->GetNumberOfCells();
133 numberOfClusters = inputCellIds->GetNumberOfIds();
135 vtkPolyData* clusterCenters = vtkPolyData::New();
136 vtkPoints* clusterCenterPoints = vtkPoints::New();
137 vtkCellArray* clusterCenterLines = vtkCellArray::New();
138 clusterCenters->SetPoints(clusterCenterPoints);
139 clusterCenters->SetLines(clusterCenterLines);
141 vtkIntArray* centerLabelArray = vtkIntArray::New();
142 centerLabelArray->SetName(
"Label");
144 for (
int i=0; i<numberOfClusters; i++)
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++)
153 streamlines->GetPoint(line->GetPointId(j),point);
154 int pointId = clusterCenterPoints->InsertNextPoint(point);
155 clusterCenterLines->InsertCellPoint(pointId);
159 clusterCenters->GetCellData()->AddArray(centerLabelArray);
172 vtkIntArray* labelArray = vtkIntArray::New();
173 labelArray->SetName(
"Label");
174 labelArray->SetNumberOfValues(numberOfCells);
176 vtkDoubleArray* distance = vtkDoubleArray::New();
177 distance->SetName(
"Distance");
178 distance->SetNumberOfTuples(numberOfCells);
180 for (
int i=0; i<numberOfCells; i++)
182 double point[3], clusterCenterPoint[3];
183 vtkCell* streamline = streamlines->GetCell(i);
185 int minClusterCenterDistanceId = -1;
186 for (
int j=0; j<numberOfClusters; j++)
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++)
196 streamlines->GetPoint(streamline->GetPointId(ip),point);
198 int minPointDistanceId = -1;
199 for (
int jp=0; jp<numberOfClusterCenterLinePoints; jp++)
201 clusterCenters->GetPoint(clusterCenterLine->GetPointId(jp),clusterCenterPoint);
202 double distanceSquared = vtkMath::Distance2BetweenPoints(point,clusterCenterPoint);
203 if (distanceSquared < minPointDistanceSquared)
205 minPointDistanceSquared = distanceSquared;
206 minPointDistanceId = jp;
209 squaredDistanceSum += minPointDistanceSquared;
210 if (correspondenceIds->IsId(minPointDistanceId) != -1)
212 numberOfRepeatedMatches++;
216 correspondenceIds->InsertNextId(minPointDistanceId);
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)
226 minClusterCenterDistance = distance;
227 minClusterCenterDistanceId = inputLabelIds->GetId(j);
230 labelArray->InsertValue(i,minClusterCenterDistanceId);
231 distance->InsertValue(i,minClusterCenterDistance);
234 output->DeepCopy(streamlines);
235 output->GetCellData()->AddArray(labelArray);
236 output->GetCellData()->AddArray(distance);
247 splineFilter->Delete();
248 inputCellIds->Delete();
249 clusterCenters->Delete();
250 clusterCenterPoints->Delete();
251 clusterCenterLines->Delete();
254 labelArray->Delete();
255 centerLabelArray->Delete();