forked from strasdat/Sophus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.hpp
264 lines (229 loc) · 6.92 KB
/
tests.hpp
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
#ifndef SOPUHS_TESTS_HPP
#define SOPUHS_TESTS_HPP
#include <vector>
#include <unsupported/Eigen/MatrixFunctions>
#include "sophus.hpp"
namespace Sophus {
using namespace std;
using namespace Eigen;
template <class LieGroup>
class Tests {
public:
typedef typename LieGroup::Scalar Scalar;
typedef typename LieGroup::Transformation Transformation;
typedef typename LieGroup::Tangent Tangent;
typedef typename LieGroup::Point Point;
typedef typename LieGroup::Adjoint Adjoint;
static const int N = LieGroup::N;
static const int DoF = LieGroup::DoF;
const Scalar SMALL_EPS;
Tests() : SMALL_EPS(SophusConstants<Scalar>::epsilon()) {
}
void setGroupElements(const vector<LieGroup> & group_vec) {
group_vec_ = group_vec;
}
void setTangentVectors(const vector<Tangent> & tangent_vec) {
tangent_vec_ = tangent_vec;
}
void setPoints(const vector<Point> & point_vec) {
point_vec_ = point_vec;
}
bool adjointTest() {
bool passed = true;
for (size_t i=0; i<group_vec_.size(); ++i) {
Transformation T = group_vec_[i].matrix();
Adjoint Ad = group_vec_[i].Adj();
for (size_t j=0; j<tangent_vec_.size(); ++j) {
Tangent x = tangent_vec_[j];
Transformation I;
I.setIdentity();
Tangent ad1 = Ad*x;
Tangent ad2 = LieGroup::vee(T*LieGroup::hat(x)
*group_vec_[i].inverse().matrix());
Scalar nrm = norm(ad1-ad2);
if (isnan(nrm) || nrm>20.*SMALL_EPS) {
cerr << "Adjoint" << endl;
cerr << "Test case: " << i << "," << j <<endl;
cerr << (ad1-ad2) <<endl;
cerr << endl;
passed = false;
}
}
}
return passed;
}
bool expLogTest() {
bool passed = true;
for (size_t i=0; i<group_vec_.size(); ++i) {
Transformation T1 = group_vec_[i].matrix();
Transformation T2 = LieGroup::exp(group_vec_[i].log()).matrix();
Transformation DiffT = T1-T2;
Scalar nrm = DiffT.norm();
if (isnan(nrm) || nrm>SMALL_EPS) {
cerr << "G - exp(log(G))" << endl;
cerr << "Test case: " << i << endl;
cerr << DiffT <<endl;
cerr << endl;
passed = false;
}
}
return passed;
}
bool expMapTest() {
bool passed = true;
for (size_t i=0; i<tangent_vec_.size(); ++i) {
Tangent omega = tangent_vec_[i];
Transformation exp_x = LieGroup::exp(omega).matrix();
Transformation expmap_hat_x = (LieGroup::hat(omega)).exp();
Transformation DiffR = exp_x-expmap_hat_x;
Scalar nrm = DiffR.norm();
if (isnan(nrm) || nrm>10.*SMALL_EPS) {
cerr << "expmap(hat(x)) - exp(x)" << endl;
cerr << "Test case: " << i << endl;
cerr << exp_x <<endl;
cerr << expmap_hat_x <<endl;
cerr << DiffR <<endl;
cerr << endl;
passed = false;
}
}
return passed;
}
bool groupActionTest() {
bool passed = true;
for (size_t i=0; i<group_vec_.size(); ++i) {
for (size_t j=0; j<point_vec_.size(); ++j) {
const Point & p = point_vec_[j];
Transformation T = group_vec_[i].matrix();
Point res1 = group_vec_[i]*p;
Point res2 = map(T, p);
Scalar nrm = (res1-res2).norm();
if (isnan(nrm) || nrm>SMALL_EPS) {
cerr << "Transform vector" << endl;
cerr << "Test case: " << i << endl;
cerr << (res1-res2) <<endl;
cerr << endl;
passed = false;
}
}
}
return passed;
}
bool lieBracketTest() {
bool passed = true;
for (size_t i=0; i<tangent_vec_.size(); ++i) {
for (size_t j=0; j<tangent_vec_.size(); ++j) {
Tangent res1 = LieGroup::lieBracket(tangent_vec_[i],tangent_vec_[j]);
Transformation hati = LieGroup::hat(tangent_vec_[i]);
Transformation hatj = LieGroup::hat(tangent_vec_[j]);
Tangent res2 = LieGroup::vee(hati*hatj-hatj*hati);
Tangent resDiff = res1-res2;
if (norm(resDiff)>SMALL_EPS) {
cerr << "Lie Bracket Test" << endl;
cerr << "Test case: " << i << ", " <<j<< endl;
cerr << resDiff << endl;
cerr << endl;
passed = false;
}
}
}
return passed;
}
bool mapAndMultTest() {
bool passed = true;
for (size_t i=0; i<group_vec_.size(); ++i) {
for (size_t j=0; j<group_vec_.size(); ++j) {
Transformation mul_resmat = (group_vec_[i]*group_vec_[j]).matrix();
Scalar fastmul_res_raw[LieGroup::num_parameters];
Eigen::Map<LieGroup> fastmul_res(fastmul_res_raw);
Eigen::Map<const LieGroup> group_j_constmap(group_vec_[j].data());
fastmul_res = group_vec_[i];
fastmul_res.fastMultiply(group_j_constmap);
Transformation diff = mul_resmat-fastmul_res.matrix();
Scalar nrm = diff.norm();
if (isnan(nrm) || nrm>SMALL_EPS) {
cerr << "Map & Multiply" << endl;
cerr << "Test case: " << i << "," << j << endl;
cerr << diff <<endl;
cerr << endl;
passed = false;
}
}
}
return passed;
}
bool veeHatTest() {
bool passed = true;
for (size_t i=0; i<tangent_vec_.size(); ++i) {
Tangent resDiff
= tangent_vec_[i] - LieGroup::vee(LieGroup::hat(tangent_vec_[i]));
if (norm(resDiff)>SMALL_EPS) {
cerr << "Hat-vee Test" << endl;
cerr << "Test case: " << i << endl;
cerr << resDiff << endl;
cerr << endl;
passed = false;
}
}
return passed;
}
void runAllTests() {
bool passed = adjointTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
passed = expLogTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
passed = expMapTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
passed = groupActionTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
passed = lieBracketTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
passed = mapAndMultTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
passed = veeHatTest();
if (!passed) {
cerr << "failed!" << endl << endl;
exit(-1);
}
cerr << "passed." << endl << endl;
}
private:
Matrix<Scalar,N-1,1> map(const Matrix<Scalar,N,N> & T,
const Matrix<Scalar,N-1,1> & p) {
return T.template topLeftCorner<N-1,N-1>()*p
+ T.template topRightCorner<N-1,1>();
}
Matrix<Scalar,N,1> map(const Matrix<Scalar,N,N> & T,
const Matrix<Scalar,N,1> & p) {
return T*p;
}
Scalar norm(const Scalar & v) {
return std::abs(v);
}
Scalar norm(const Matrix<Scalar,DoF,1> & T) {
return T.norm();
}
std::vector<LieGroup> group_vec_;
std::vector<Tangent> tangent_vec_;
std::vector<Point> point_vec_;
};
}
#endif // TESTS_HPP