Repository navigation
Expand file tree
/
Copy pathregistration.cpp
More file actions
95 lines (78 loc) · 3.27 KB
/
Copy pathregistration.cpp
File metadata and controls
95 lines (78 loc) · 3.27 KB
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
#include "glasslio/registration.hpp"
#include <cmath>
#include <Eigen/Eigenvalues>
#include "glass_core/gauss_newton.hpp"
#include "sophus/se3.hpp"
namespace glasslio
{
using namespace glass_core; // the Gauss-Newton solver (NOLINT: build/namespaces)
RegistrationResult alignPointToPlane(
const CloudXYZI & source,
const LocalMap & map,
const Eigen::Isometry3d & guess,
const RegistrationParams & params)
{
RegistrationResult result;
result.pose = guess;
if (map.empty() || source.empty()) {
return result;
}
Sophus::SE3d T(guess.linear(), guess.translation());
GaussNewtonParams gn_params;
gn_params.max_iterations = params.max_iterations;
gn_params.eps_translation = params.eps_translation;
gn_params.eps_rotation = params.eps_rotation;
gn_params.huber_delta = params.huber_delta;
gn_params.min_residuals = params.min_correspondences;
// ICP alternates: you need the pose to find the correspondences, and the
// correspondences to find the pose. This lambda is the ASSOCIATE half, and the
// solver re-runs it every iteration -- that re-association IS the ICP loop.
// Everything else (H, b, Huber, LDLT, retraction) is generic least squares and
// lives in gauss_newton.hpp, which has never heard of a point cloud.
const auto associate = [&](const Sophus::SE3d & T_cur, NormalEquations & eq) {
for (const auto & pt : source.points) {
if (!std::isfinite(pt.x) || !std::isfinite(pt.y) || !std::isfinite(pt.z)) {
continue;
}
const Eigen::Vector3d p(pt.x, pt.y, pt.z);
const Eigen::Vector3d q = T_cur * p; // source point in the world frame
// Correspondence is a HASH LOOKUP, not a KD-tree query: the map caches one
// plane per voxel, so we hash q and scan the 27-cell neighbourhood.
Plane plane;
if (!map.closestPlane(q, params.max_correspondence_distance, plane)) {
continue;
}
eq.add(
pointToPlaneResidual(q, plane.centroid, plane.normal),
pointToPlaneJacobian(q, plane.normal),
params.huber_delta);
}
};
const GaussNewtonResult gn = optimizeSE3(T, gn_params, associate);
result.valid = gn.valid;
result.converged = gn.converged;
result.iterations = gn.iterations;
result.correspondences = gn.residuals;
result.rmse = gn.rmse;
// DEGENERACY GATE (see RegistrationParams::min_translation_eigenvalue_ratio).
// associate() only ever hands optimizeSE3 an eq built at each iteration's OWN pose
// -- none of those survive the loop -- so re-run it once more at the converged pose
// to get the H this solution actually rests on. One extra association pass, no
// extra Gauss-Newton iterations.
if (result.valid) {
NormalEquations final_eq;
associate(T, final_eq);
const Eigen::Matrix3d Ht = final_eq.H().topLeftCorner<3, 3>();
const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es(Ht);
const double trace = Ht.trace();
result.translation_eigenvalue_ratio =
trace > 0.0 ? es.eigenvalues().minCoeff() / trace : 0.0;
if (result.translation_eigenvalue_ratio < params.min_translation_eigenvalue_ratio) {
result.valid = false;
}
}
result.pose.linear() = T.rotationMatrix();
result.pose.translation() = T.translation();
return result;
}
} // namespace glasslio