47{
48
49
50 if (this->Source == NULL || !this->Source->GetNumberOfPoints())
51 {
52 vtkErrorMacro(<<"Can't execute with NULL or empty input");
53 return;
54 }
55
56 if (this->Target == NULL || !this->Target->GetNumberOfPoints())
57 {
58 vtkErrorMacro(<<"Can't execute with NULL or empty target");
59 return;
60 }
61
62
63
64 this->CreateDefaultLocator();
65 this->Locator->SetDataSet(this->Target);
66 this->Locator->SetNumberOfCellsPerBucket(1);
67 this->Locator->BuildLocator();
68
69
70
71 int step = 1;
72 if (this->Source->GetNumberOfPoints() > this->MaximumNumberOfLandmarks)
73 {
74 step = this->Source->GetNumberOfPoints() / this->MaximumNumberOfLandmarks;
75 vtkDebugMacro(<< "Landmarks step is now : " << step);
76 }
77
78 vtkIdType nb_points = this->Source->GetNumberOfPoints() / step;
79
80
81
82
83
84
85
86
87
88 vtkPoints *points1 = vtkPoints::New();
89 points1->SetNumberOfPoints(nb_points);
90
91 vtkPoints *closestp = vtkPoints::New();
92 closestp->SetNumberOfPoints(nb_points);
93
94 vtkPoints *points2 = vtkPoints::New();
95 points2->SetNumberOfPoints(nb_points);
96
97
98
99 vtkTransform *accumulate = vtkTransform::New();
100 accumulate->PostMultiply();
101
102 vtkIdType i;
103 int j;
104 double p1[3], p2[3];
105
106 if (StartByMatchingCentroids)
107 {
108 double source_centroid[3] = {0,0,0};
109 for (i = 0; i < this->Source->GetNumberOfPoints(); i++)
110 {
111 this->Source->GetPoint(i, p1);
112 source_centroid[0] += p1[0];
113 source_centroid[1] += p1[1];
114 source_centroid[2] += p1[2];
115 }
116 source_centroid[0] /= this->Source->GetNumberOfPoints();
117 source_centroid[1] /= this->Source->GetNumberOfPoints();
118 source_centroid[2] /= this->Source->GetNumberOfPoints();
119
120 double target_centroid[3] = {0,0,0};
121 for (i = 0; i < this->Target->GetNumberOfPoints(); i++)
122 {
123 this->Target->GetPoint(i, p2);
124 target_centroid[0] += p2[0];
125 target_centroid[1] += p2[1];
126 target_centroid[2] += p2[2];
127 }
128 target_centroid[0] /= this->Target->GetNumberOfPoints();
129 target_centroid[1] /= this->Target->GetNumberOfPoints();
130 target_centroid[2] /= this->Target->GetNumberOfPoints();
131
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();
136
137 for (i = 0, j = 0; i < nb_points; i++, j += step)
138 {
139 double outPoint[3];
140 accumulate->InternalTransformPoint(this->Source->GetPoint(j),
141 outPoint);
142 points1->SetPoint(i, outPoint);
143 }
144 }
145 else
146 {
147 for (i = 0, j = 0; i < nb_points; i++, j += step)
148 {
149 points1->SetPoint(i, this->Source->GetPoint(j));
150 }
151 }
152
153
154
155 vtkIdType cell_id;
156 int sub_id;
157 double dist2, totaldist = 0;
158 double outPoint[3];
159
160 vtkPoints *temp, *a = points1, *b = points2;
161
162 this->NumberOfIterations = 0;
163
165 do
166 {
167
169 {
170 closestp->SetNumberOfPoints(nb_points);
171 for(i = 0; i < nb_points; i++)
172 {
173 this->Locator->FindClosestPoint(a->GetPoint(i),
174 outPoint,
175 cell_id,
176 sub_id,
177 dist2);
178 closestp->SetPoint(i, outPoint);
179 }
180 this->LandmarkTransform->SetSourceLandmarks(a);
181 }
182 else
183 {
184 vtkPoints* sourceLandmarks = vtkPoints::New();
185 closestp->Initialize();
186 for(i = 0; i < nb_points; i++)
187 {
188 this->Locator->FindClosestPoint(a->GetPoint(i),
189 outPoint,
190 cell_id,
191 sub_id,
192 dist2);
193 if(dist2 < tooFarThreshold2)
194 {
195 closestp->InsertNextPoint(outPoint);
196 sourceLandmarks->InsertNextPoint(a->GetPoint(i));
197 }
198 }
199 this->LandmarkTransform->SetSourceLandmarks(sourceLandmarks);
200 sourceLandmarks->Delete();
201 }
202
203
204 this->LandmarkTransform->SetTargetLandmarks(closestp);
205 this->LandmarkTransform->Update();
206
207 this->NumberOfIterations++;
208 vtkDebugMacro(<< "Iteration: " << this->NumberOfIterations);
209 if (this->NumberOfIterations > this->MaximumNumberOfIterations)
210 {
211 break;
212 }
213
214
215 accumulate->Concatenate(this->LandmarkTransform->GetMatrix());
216
217
218
219 if (this->CheckMeanDistance)
220 {
221 totaldist = 0.0;
222 }
223
224 for(i = 0; i < nb_points; i++)
225 {
226 a->GetPoint(i, p1);
227 this->LandmarkTransform->InternalTransformPoint(p1, p2);
228 b->SetPoint(i, p2);
229 if (this->CheckMeanDistance)
230 {
231 if (this->MeanDistanceMode == VTK_ICP_MODE_RMS)
232 {
233 totaldist += vtkMath::Distance2BetweenPoints(p1, p2);
234 } else {
235 totaldist += sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
236 }
237 }
238 }
239
240 if (this->CheckMeanDistance)
241 {
242 if (this->MeanDistanceMode == VTK_ICP_MODE_RMS)
243 {
244 this->MeanDistance = sqrt(totaldist / (double)nb_points);
245 } else {
246 this->MeanDistance = totaldist / (double)nb_points;
247 }
248 vtkDebugMacro("Mean distance: " << this->MeanDistance);
249 if (this->MeanDistance <= this->MaximumMeanDistance)
250 {
251 break;
252 }
253 }
254
255 temp = a;
256 a = b;
257 b = temp;
258
259 }
260 while (1);
261
262
263
264 this->Matrix->DeepCopy(accumulate->GetMatrix());
265
266 accumulate->Delete();
267 points1->Delete();
268 closestp->Delete();
269 points2->Delete();
270}