Skip to content

Commit 6830911

Browse files
Reduce code duplication in EdgeGICP
1 parent cd9da0f commit 6830911

1 file changed

Lines changed: 41 additions & 51 deletions

File tree

g2o/types/icp/edge_gicp.cpp

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,43 @@
2828

2929
#include <Eigen/Geometry>
3030

31+
#include "g2o/core/eigen_types.h"
32+
3133
namespace g2o {
3234

33-
constexpr double ySquaredBnd{0.1};
35+
namespace {
36+
constexpr double kYSquaredBnd{0.1};
37+
38+
void makeRot(Matrix3& R, const Vector3& normal) {
39+
Vector3 y(0., 1., 0.);
40+
R.row(2) = normal;
41+
y = y - normal(1) * normal;
42+
const double ysquarednorm = y.squaredNorm();
43+
if (ysquarednorm >= kYSquaredBnd) {
44+
y /= std::sqrt(ysquarednorm);
45+
R.row(1) = y;
46+
R.row(0) = normal.cross(R.row(1));
47+
} else {
48+
Vector3 x(-1., 0., 0.);
49+
x = x + normal(0) * normal;
50+
x.normalize();
51+
R.row(0) = x;
52+
R.row(1) = -normal.cross(R.row(0));
53+
}
54+
}
55+
56+
Matrix3 prec(double e, const Matrix3& R) {
57+
Matrix3 prec_mat;
58+
prec_mat << e, 0, 0, 0, e, 0, 0, 0, 1;
59+
return R.transpose() * prec_mat * R;
60+
}
61+
62+
Matrix3 cov(double e, const Matrix3& R) {
63+
Matrix3 cov_mat;
64+
cov_mat << 1, 0, 0, 0, 1, 0, 0, 0, e;
65+
return R.transpose() * cov_mat * R;
66+
}
67+
} // namespace
3468

3569
EdgeGICP::EdgeGICP() {
3670
pos0.setZero();
@@ -42,72 +76,28 @@ EdgeGICP::EdgeGICP() {
4276
R1.setIdentity();
4377
}
4478

45-
void EdgeGICP::makeRot0() {
46-
Vector3 y;
47-
y << 0, 1, 0;
48-
R0.row(2) = normal0;
49-
y = y - normal0(1) * normal0;
50-
double ysquarednorm = y.squaredNorm();
51-
if (ysquarednorm >= ySquaredBnd) {
52-
y /= std::sqrt(ysquarednorm);
53-
R0.row(1) = y;
54-
R0.row(0) = normal0.cross(R0.row(1));
55-
} else {
56-
Vector3 x;
57-
x << -1, 0, 0;
58-
x = x + normal0(0) * normal0;
59-
x.normalize();
60-
R0.row(0) = x;
61-
R0.row(1) = -normal0.cross(R0.row(0));
62-
}
63-
}
79+
void EdgeGICP::makeRot0() { makeRot(R0, normal0); }
6480

65-
void EdgeGICP::makeRot1() {
66-
Vector3 y;
67-
y << 0, 1, 0;
68-
R1.row(2) = normal1;
69-
y = y - normal1(1) * normal1;
70-
double ysquarednorm = y.squaredNorm();
71-
if (y.squaredNorm() >= ySquaredBnd) {
72-
y /= std::sqrt(ysquarednorm);
73-
R1.row(1) = y;
74-
R1.row(0) = normal1.cross(R1.row(1));
75-
} else {
76-
Vector3 x;
77-
x << -1, 0, 0;
78-
x = x + normal1(0) * normal1;
79-
x.normalize();
80-
R1.row(0) = x;
81-
R1.row(1) = -normal1.cross(R1.row(0));
82-
}
83-
}
81+
void EdgeGICP::makeRot1() { makeRot(R1, normal1); }
8482

8583
Matrix3 EdgeGICP::prec0(double e) {
8684
makeRot0();
87-
Matrix3 prec;
88-
prec << e, 0, 0, 0, e, 0, 0, 0, 1;
89-
return R0.transpose() * prec * R0;
85+
return prec(e, R0);
9086
}
9187

9288
Matrix3 EdgeGICP::prec1(double e) {
9389
makeRot1();
94-
Matrix3 prec;
95-
prec << e, 0, 0, 0, e, 0, 0, 0, 1;
96-
return R1.transpose() * prec * R1;
90+
return prec(e, R1);
9791
}
9892

9993
Matrix3 EdgeGICP::cov0(double e) {
10094
makeRot0();
101-
Matrix3 cov;
102-
cov << 1, 0, 0, 0, 1, 0, 0, 0, e;
103-
return R0.transpose() * cov * R0;
95+
return cov(e, R0);
10496
}
10597

10698
Matrix3 EdgeGICP::cov1(double e) {
10799
makeRot1();
108-
Matrix3 cov;
109-
cov << 1, 0, 0, 0, 1, 0, 0, 0, e;
110-
return R1.transpose() * cov * R1;
100+
return cov(e, R1);
111101
}
112102

113103
} // namespace g2o

0 commit comments

Comments
 (0)