50 if (this->Source == NULL || !this->Source->GetNumberOfPoints())
52 vtkErrorMacro(<<
"Can't execute with NULL or empty input");
56 if (this->Target == NULL || !this->Target->GetNumberOfPoints())
58 vtkErrorMacro(<<
"Can't execute with NULL or empty target");
64 this->CreateDefaultLocator();
65 this->Locator->SetDataSet(this->Target);
66 this->Locator->SetNumberOfCellsPerBucket(1);
67 this->Locator->BuildLocator();
72 if (this->Source->GetNumberOfPoints() > this->MaximumNumberOfLandmarks)
74 step = this->Source->GetNumberOfPoints() / this->MaximumNumberOfLandmarks;
75 vtkDebugMacro(<<
"Landmarks step is now : " << step);
78 vtkIdType nb_points = this->Source->GetNumberOfPoints() / step;
88 vtkPoints *points1 = vtkPoints::New();
89 points1->SetNumberOfPoints(nb_points);
91 vtkPoints *closestp = vtkPoints::New();
92 closestp->SetNumberOfPoints(nb_points);
94 vtkPoints *points2 = vtkPoints::New();
95 points2->SetNumberOfPoints(nb_points);
99 vtkTransform *accumulate = vtkTransform::New();
100 accumulate->PostMultiply();
106 if (StartByMatchingCentroids)
108 double source_centroid[3] = {0,0,0};
109 for (i = 0; i < this->Source->GetNumberOfPoints(); i++)
111 this->Source->GetPoint(i, p1);
112 source_centroid[0] += p1[0];
113 source_centroid[1] += p1[1];
114 source_centroid[2] += p1[2];
116 source_centroid[0] /= this->Source->GetNumberOfPoints();
117 source_centroid[1] /= this->Source->GetNumberOfPoints();
118 source_centroid[2] /= this->Source->GetNumberOfPoints();
120 double target_centroid[3] = {0,0,0};
121 for (i = 0; i < this->Target->GetNumberOfPoints(); i++)
123 this->Target->GetPoint(i, p2);
124 target_centroid[0] += p2[0];
125 target_centroid[1] += p2[1];
126 target_centroid[2] += p2[2];
128 target_centroid[0] /= this->Target->GetNumberOfPoints();
129 target_centroid[1] /= this->Target->GetNumberOfPoints();
130 target_centroid[2] /= this->Target->GetNumberOfPoints();
132 accumulate->Translate(target_centroid[0] - source_centroid[0],
133 target_centroid[1] - source_centroid[1],
134 target_centroid[2] - source_centroid[2]);
135 accumulate->Update();
137 for (i = 0, j = 0; i < nb_points; i++, j += step)
140 accumulate->InternalTransformPoint(this->Source->GetPoint(j),
142 points1->SetPoint(i, outPoint);
147 for (i = 0, j = 0; i < nb_points; i++, j += step)
149 points1->SetPoint(i, this->Source->GetPoint(j));
157 double dist2, totaldist = 0;
160 vtkPoints *temp, *a = points1, *b = points2;
162 this->NumberOfIterations = 0;
170 closestp->SetNumberOfPoints(nb_points);
171 for(i = 0; i < nb_points; i++)
173 this->Locator->FindClosestPoint(a->GetPoint(i),
178 closestp->SetPoint(i, outPoint);
180 this->LandmarkTransform->SetSourceLandmarks(a);
184 vtkPoints* sourceLandmarks = vtkPoints::New();
185 closestp->Initialize();
186 for(i = 0; i < nb_points; i++)
188 this->Locator->FindClosestPoint(a->GetPoint(i),
193 if(dist2 < tooFarThreshold2)
195 closestp->InsertNextPoint(outPoint);
196 sourceLandmarks->InsertNextPoint(a->GetPoint(i));
199 this->LandmarkTransform->SetSourceLandmarks(sourceLandmarks);
200 sourceLandmarks->Delete();
204 this->LandmarkTransform->SetTargetLandmarks(closestp);
205 this->LandmarkTransform->Update();
207 this->NumberOfIterations++;
208 vtkDebugMacro(<<
"Iteration: " << this->NumberOfIterations);
209 if (this->NumberOfIterations > this->MaximumNumberOfIterations)
215 accumulate->Concatenate(this->LandmarkTransform->GetMatrix());
219 if (this->CheckMeanDistance)
224 for(i = 0; i < nb_points; i++)
227 this->LandmarkTransform->InternalTransformPoint(p1, p2);
229 if (this->CheckMeanDistance)
231 if (this->MeanDistanceMode == VTK_ICP_MODE_RMS)
233 totaldist += vtkMath::Distance2BetweenPoints(p1, p2);
235 totaldist += sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
240 if (this->CheckMeanDistance)
242 if (this->MeanDistanceMode == VTK_ICP_MODE_RMS)
244 this->MeanDistance = sqrt(totaldist / (
double)nb_points);
246 this->MeanDistance = totaldist / (double)nb_points;
248 vtkDebugMacro(
"Mean distance: " << this->MeanDistance);
249 if (this->MeanDistance <= this->MaximumMeanDistance)
264 this->Matrix->DeepCopy(accumulate->GetMatrix());
266 accumulate->Delete();