-
Notifications
You must be signed in to change notification settings - Fork 5
/
registration.cpp
executable file
·268 lines (221 loc) · 9.83 KB
/
registration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <string>
#include <ctime>
#include <pcl/point_types.h>
#include <pcl/registration/icp.h>
#include <pcl/registration/ndt.h>
#include <pcl/registration/correspondence_estimation.h>
#include <pcl/filters/approximate_voxel_grid.h>
#include <pcl/filters/uniform_sampling.h>
#include <pcl/visualization/pcl_visualizer.h>
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
void print4x4Matrix(const Eigen::Matrix4d &matrix) {
printf("[[%f %f %f %f]\n[%f %f %f %f]\n[%f %f %f %f]\n[%f %f %f %f]]\n",
matrix(0, 0), matrix(0, 1), matrix(0, 2), matrix(0, 3),
matrix(1, 0), matrix(1, 1), matrix(1, 2), matrix(1, 3),
matrix(2, 0), matrix(2, 1), matrix(2, 2), matrix(2, 3),
matrix(3, 0), matrix(3, 1), matrix(3, 2), matrix(3, 3));
}
void pointsToCloud(const double *points,
const size_t size,
PointCloudT &cloud) {
cloud.width = size;
cloud.height = 1;
cloud.resize(size);
size_t index = 0;
for (auto &it : cloud) {
it.x = (float) points[index];
it.y = (float) points[index + 1];
it.z = (float) points[index + 2];
index += 3;
}
}
bool next_iteration = false;
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent &event) {
if (event.getKeySym() == "space" && event.keyDown())
next_iteration = true;
}
template<class T>
Eigen::Matrix4d plot(T &algorithm,
PointCloudT::Ptr &source,
const PointCloudT::ConstPtr &target) {
int iteration = algorithm.getMaximumIterations();
algorithm.align(*source);
algorithm.setMaximumIterations(1);
Eigen::Matrix4d transformation = algorithm.getFinalTransformation().template cast<double>();
pcl::visualization::PCLVisualizer viewer("Registration");
int v1(0);
viewer.createViewPort(0.0, 0.0, 1.0, 1.0, v1);
pcl::visualization::PointCloudColorHandlerCustom<PointT> target_in_color_h(target, 255, 255, 255);
viewer.addPointCloud(target, target_in_color_h, "target", v1);
pcl::visualization::PointCloudColorHandlerCustom<PointT> source_in_color_h(source, 180, 20, 20);
viewer.addPointCloud(source, source_in_color_h, "source", v1);
viewer.addText("White: Target point cloud\nRed: Source point cloud", 10, 15, 16, 1.0, 1.0, 1.0, "info", v1);
std::stringstream ss;
ss << "Iteration: " << iteration << "\nScore: " << algorithm.getFitnessScore();
std::string text = ss.str();
viewer.addText(text, 10, 65, 16, 1.0, 1.0, 1.0, "text", v1);
viewer.setBackgroundColor(0.0, 0.0, 0.0, v1);
viewer.setCameraPosition(300, 300, 300, 0, 0, 0, 0, 0, 1);
viewer.setSize(600, 600);
viewer.addCoordinateSystem(1.0);
viewer.registerKeyboardCallback(&keyboardEventOccurred);
while (!viewer.wasStopped()) {
viewer.spinOnce();
if (next_iteration) {
algorithm.align(*source);
iteration++;
if (algorithm.hasConverged()) {
std::cout << "\nTransformation " << iteration << ": Source -> Target" << std::endl;
transformation *= algorithm.getFinalTransformation().template cast<double>(); // WARNING /!\ This is not accurate! For "educational" purpose only!
print4x4Matrix(transformation); // Print the transformation between original pose and current pose
ss.str("");
ss << "Iteration: " << iteration << "\nScore: " << algorithm.getFitnessScore();
text = ss.str();
viewer.updateText(text, 10, 65, 16, 1.0, 1.0, 1.0, "text");
viewer.updatePointCloud(source, source_in_color_h, "source");
} else {
PCL_ERROR ("\nICP has not converged.\n");
}
} next_iteration = false;
}
return transformation;
}
void uniformSubsampling(const PointCloudT::ConstPtr &input,
PointCloudT &output,
double radius = 0.1) {
pcl::UniformSampling<PointT> us;
us.setInputCloud(input);
us.setRadiusSearch(radius);
us.filter(output);
}
extern "C" double ndt(const double *sourcePoints,
const size_t sourceSize,
const double *targetPoints,
const size_t targetSize,
double *transformation,
int nrIterations = 25,
double distanceThreshold = 1.0,
double epsilon = 0.01,
double inlierThreshold = 0.05,
double downsample = 0,
bool visualize = false,
float resolution = 1.0,
double stepSize = 0.1,
float voxelize = 0) {
PointCloudT::Ptr sourceCloud(new PointCloudT);
PointCloudT::Ptr coarseSourceCloud(new PointCloudT);
PointCloudT::Ptr voxelizedSourceCloud(new PointCloudT);
PointCloudT::Ptr finalSourceCloud(new PointCloudT);
pointsToCloud(sourcePoints, sourceSize, *sourceCloud);
*finalSourceCloud = *sourceCloud;
PointCloudT::Ptr targetCloud(new PointCloudT);
PointCloudT::Ptr coarseTargetCloud(new PointCloudT);
PointCloudT::Ptr finalTargetCloud(new PointCloudT);
pointsToCloud(targetPoints, targetSize, *targetCloud);
*finalTargetCloud = *targetCloud;
pcl::NormalDistributionsTransform<PointT, PointT> ndt;
ndt.setMaximumIterations(nrIterations);
ndt.setMaxCorrespondenceDistance(distanceThreshold);
ndt.setTransformationEpsilon(epsilon);
ndt.setRANSACOutlierRejectionThreshold(inlierThreshold);
//ndt.setRANSACIterations();
ndt.setResolution(resolution);
ndt.setStepSize(stepSize);
if (downsample > 0) {
uniformSubsampling(sourceCloud, *coarseSourceCloud, downsample);
*finalSourceCloud = *coarseSourceCloud;
uniformSubsampling(targetCloud, *coarseTargetCloud, downsample);
*finalTargetCloud = *coarseTargetCloud;
}
if (voxelize > 0) {
pcl::ApproximateVoxelGrid<PointT> voxelFilter;
voxelFilter.setLeafSize(voxelize, voxelize, voxelize);
voxelFilter.setInputCloud(finalSourceCloud);
voxelFilter.filter(*voxelizedSourceCloud);
*finalSourceCloud = *voxelizedSourceCloud;
}
ndt.setInputSource(finalSourceCloud);
ndt.setInputTarget(finalTargetCloud);
Eigen::Matrix4d finalTransform = Eigen::Matrix4d::Identity();
if (visualize) {
finalTransform = plot<pcl::NormalDistributionsTransform<PointT, PointT>>(ndt, finalSourceCloud, finalTargetCloud);
} else {
ndt.align(*finalSourceCloud);
finalTransform = ndt.getFinalTransformation().cast<double>();
}
if (ndt.hasConverged()) {
transformation[0] = finalTransform(0, 0);
transformation[1] = finalTransform(0, 1);
transformation[2] = finalTransform(0, 2);
transformation[3] = finalTransform(0, 3);
transformation[4] = finalTransform(1, 0);
transformation[5] = finalTransform(1, 1);
transformation[6] = finalTransform(1, 2);
transformation[7] = finalTransform(1, 3);
transformation[8] = finalTransform(2, 0);
transformation[9] = finalTransform(2, 1);
transformation[10] = finalTransform(2, 2);
transformation[11] = finalTransform(2, 3);
return ndt.getFitnessScore();
}
return (-1);
}
extern "C" double icp(const double *sourcePoints,
const size_t sourceSize,
const double *targetPoints,
const size_t targetSize,
double *transformation,
int nrIterations = 25,
double distanceThreshold = 1.0,
double epsilon = 0.01,
double inlierThreshold = 0.05,
double downsample = 0,
bool visualize = false) {
PointCloudT::Ptr sourceCloud(new PointCloudT);
PointCloudT::Ptr coarseSourceCloud(new PointCloudT);
PointCloudT::Ptr finalSourceCloud(new PointCloudT);
pointsToCloud(sourcePoints, sourceSize, *sourceCloud);
*finalSourceCloud = *sourceCloud;
PointCloudT::Ptr targetCloud(new PointCloudT);
PointCloudT::Ptr coarseTargetCloud(new PointCloudT);
PointCloudT::Ptr finalTargetCloud(new PointCloudT);
pointsToCloud(targetPoints, targetSize, *targetCloud);
*finalTargetCloud = *targetCloud;
pcl::IterativeClosestPoint<PointT, PointT> icp;
icp.setMaximumIterations(nrIterations);
icp.setMaxCorrespondenceDistance(distanceThreshold);
icp.setTransformationEpsilon(epsilon);
icp.setRANSACOutlierRejectionThreshold(inlierThreshold);
if (downsample > 0) {
uniformSubsampling(sourceCloud, *coarseSourceCloud, downsample);
*finalSourceCloud = *coarseSourceCloud;
uniformSubsampling(targetCloud, *coarseTargetCloud, downsample);
*finalTargetCloud = *coarseTargetCloud;
}
icp.setInputSource(finalSourceCloud);
icp.setInputTarget(finalTargetCloud);
Eigen::Matrix4d finalTransform = Eigen::Matrix4d::Identity();
if (visualize) {
finalTransform = plot<pcl::IterativeClosestPoint<PointT, PointT>>(icp, finalSourceCloud, finalTargetCloud);
} else {
icp.align(*finalSourceCloud);
finalTransform = icp.getFinalTransformation().cast<double>();
}
if (icp.hasConverged()) {
transformation[0] = finalTransform(0, 0);
transformation[1] = finalTransform(0, 1);
transformation[2] = finalTransform(0, 2);
transformation[3] = finalTransform(0, 3);
transformation[4] = finalTransform(1, 0);
transformation[5] = finalTransform(1, 1);
transformation[6] = finalTransform(1, 2);
transformation[7] = finalTransform(1, 3);
transformation[8] = finalTransform(2, 0);
transformation[9] = finalTransform(2, 1);
transformation[10] = finalTransform(2, 2);
transformation[11] = finalTransform(2, 3);
return icp.getFitnessScore();
}
return (-1);
}