VMTK
Loading...
Searching...
No Matches
vtkvmtkIterativeClosestPointTransform.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkIterativeClosestPointTransform.cxx,v $
5Language: C++
6Date: $Date: 2010/05/30 11:32:56 $
7Version: $Revision: 1.0 $
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 "vtkDataSet.h"
24#include "vtkCellLocator.h"
25#include "vtkPoints.h"
26#include "vtkTransform.h"
27#include "vtkLandmarkTransform.h"
28#include "vtkMath.h"
29#include "vtkInformation.h"
30#include "vtkInformationVector.h"
31#include "vtkObjectFactory.h"
32
33
35
41
45
47{
48 // Check source, target
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 // Create locator
63
64 this->CreateDefaultLocator();
65 this->Locator->SetDataSet(this->Target);
66 this->Locator->SetNumberOfCellsPerBucket(1);
67 this->Locator->BuildLocator();
68
69 // Create two sets of points to handle iteration
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 // Allocate some points.
81 // - closestp is used so that the internal state of LandmarkTransform remains
82 // correct whenever the iteration process is stopped (hence its source
83 // and landmark points might be used in a vtkThinPlateSplineTransform).
84 // - points2 could have been avoided, but do not ask me why
85 // InternalTransformPoint is not working correctly on my computer when
86 // in and out are the same pointer.
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 // Fill with initial positions (sample dataset using step)
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 // Go
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
164 double tooFarThreshold2 = this->FarThreshold * this->FarThreshold;
165 do
166 {
167 // Fill points with the closest points to each vertex in input
168 if (!this->UseFarThreshold)
169 {
170 closestp->SetNumberOfPoints(nb_points); // It was set above, but at this point it should be removed from there
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 //use a FarThreshold
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 // Build the landmark transform
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 // Concatenate (can't use this->Concatenate directly)
215 accumulate->Concatenate(this->LandmarkTransform->GetMatrix());
216
217 // Move mesh and compute mean distance if needed
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 // Now recover accumulated result
263
264 this->Matrix->DeepCopy(accumulate->GetMatrix());
265
266 accumulate->Delete();
267 points1->Delete();
268 closestp->Delete();
269 points2->Delete();
270}
271
272void vtkvmtkIterativeClosestPointTransform::PrintSelf(std::ostream& os, vtkIndent indent)
273{
274 this->Superclass::PrintSelf(os,indent);
275
276 os << indent << "FarThreshold: " << this->FarThreshold << "\n";
277 os << indent << "UseFarThreshold: " << this->UseFarThreshold << "\n";
278}
Implements the ICP algorithm, with a FarThreshold variant.
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkvmtkIterativeClosestPointTransform)