-
Notifications
You must be signed in to change notification settings - Fork 73
/
HandEyeCalibration_test.cpp
300 lines (235 loc) · 10.5 KB
/
HandEyeCalibration_test.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE HandEyeCalibration_test
#include <boost/math/constants/constants.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <random>
#include "camodocal/calib/HandEyeCalibration.h"
#include "camodocal/EigenUtils.h"
BOOST_AUTO_TEST_SUITE(HandEyeCalibration_test)
BOOST_AUTO_TEST_CASE(FullMotion)
{
camodocal::HandEyeCalibration::setVerbose(false);
Eigen::Matrix4d H_12_expected = Eigen::Matrix4d::Identity();
H_12_expected.block<3,3>(0,0) = Eigen::AngleAxisd(0.4, Eigen::Vector3d(0.1, 0.2, 0.3).normalized()).toRotationMatrix();
H_12_expected.block<3,1>(0,3) << 0.5, 0.6, 0.7;
std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > rvecs1, tvecs1, rvecs2, tvecs2;
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(-10.0, 10.0);
std::uniform_int_distribution<> dis1(-1.0, 1.0);
int motionCount = 2;
for (int i = 0; i < motionCount; ++i)
{
double droll = boost::math::constants::radian<double>()*dis(gen);
double dpitch = boost::math::constants::radian<double>()*dis(gen);
double dyaw = boost::math::constants::radian<double>()*dis(gen);
double dx = dis1(gen);
double dy = dis1(gen);
double dz = dis1(gen);
Eigen::Matrix3d R;
R = Eigen::AngleAxisd(dyaw, Eigen::Vector3d::UnitZ()) *
Eigen::AngleAxisd(dpitch, Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(droll, Eigen::Vector3d::UnitX());
Eigen::Matrix4d H = Eigen::Matrix4d::Identity();
H.block<3,3>(0,0) = R;
H.block<3,1>(0,3) << dx, dy, dz;
Eigen::Matrix4d H_ = H.inverse();
H = H_;
Eigen::Vector3d rvec1, tvec1, rvec2, tvec2;
Eigen::AngleAxisd angleAxis1((H_12_expected * H * H_12_expected.inverse()).block<3,3>(0,0));
rvec1 = angleAxis1.angle() * angleAxis1.axis();
tvec1 = (H_12_expected * H * H_12_expected.inverse()).block<3,1>(0,3);
Eigen::AngleAxisd angleAxis2(H.block<3,3>(0,0));
rvec2 = angleAxis2.angle() * angleAxis2.axis();
tvec2 = H.block<3,1>(0,3);
rvecs1.push_back(rvec1);
tvecs1.push_back(tvec1);
rvecs2.push_back(rvec2);
tvecs2.push_back(tvec2);
}
Eigen::Matrix4d H_12;
camodocal::HandEyeCalibration::estimateHandEyeScrew(rvecs1, tvecs1, rvecs2, tvecs2, H_12);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
BOOST_REQUIRE_CLOSE(H_12_expected(i,j),H_12(i,j),0.0000000001); }
}
}
BOOST_AUTO_TEST_CASE(PlanarMotion)
{
camodocal::HandEyeCalibration::setVerbose(false);
Eigen::Matrix4d H_12_expected = Eigen::Matrix4d::Identity();
H_12_expected.block<3,3>(0,0) = Eigen::AngleAxisd(0.4, Eigen::Vector3d(0.1, 0.2, 0.3).normalized()).toRotationMatrix();
H_12_expected.block<3,1>(0,3) << 0.5, 0.6, 0.7;
std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > rvecs1, tvecs1, rvecs2, tvecs2;
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis10(-10.0, 10.0);
std::uniform_int_distribution<> dis1(-1.0, 1.0);
int motionCount = 2;
for (int i = 0; i < motionCount; ++i)
{
double droll = boost::math::constants::radian<double>()*dis10(gen);
droll = 0;
double dpitch = boost::math::constants::radian<double>()*dis10(gen);
dpitch = 0;
double dyaw = boost::math::constants::radian<double>()*dis10(gen);
double dx = dis1(gen);
double dy = dis1(gen);
double dz = dis1(gen);
dz = 0;
Eigen::Matrix3d R;
R = Eigen::AngleAxisd(dyaw, Eigen::Vector3d::UnitZ()) *
Eigen::AngleAxisd(dpitch, Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(droll, Eigen::Vector3d::UnitX());
Eigen::Matrix4d H = Eigen::Matrix4d::Identity();
H.block<3,3>(0,0) = R;
H.block<3,1>(0,3) << dx, dy, dz;
Eigen::Matrix4d H_ = H.inverse();
H = H_;
Eigen::Vector3d rvec1, tvec1, rvec2, tvec2;
Eigen::AngleAxisd angleAxis1(H.block<3,3>(0,0));
rvec1 = angleAxis1.angle() * angleAxis1.axis();
tvec1 = H.block<3,1>(0,3);
Eigen::AngleAxisd angleAxis2((H_12_expected.inverse() * H * H_12_expected).block<3,3>(0,0));
rvec2 = angleAxis2.angle() * angleAxis2.axis();
tvec2 = (H_12_expected.inverse() * H * H_12_expected).block<3,1>(0,3);
rvecs1.push_back(rvec1);
tvecs1.push_back(tvec1);
rvecs2.push_back(rvec2);
tvecs2.push_back(tvec2);
}
Eigen::Matrix4d H_12;
camodocal::HandEyeCalibration::estimateHandEyeScrew(rvecs1, tvecs1, rvecs2, tvecs2, H_12, true);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (i == 2 && j == 3) continue;
BOOST_REQUIRE_CLOSE(H_12_expected(i,j),H_12(i,j),0.0000000001);
}
}
}
BOOST_AUTO_TEST_CASE(PlanarMotionWithNoise)
{
camodocal::HandEyeCalibration::setVerbose(true);
Eigen::Matrix4d H_12_expected = Eigen::Matrix4d::Identity();
H_12_expected.block<3,3>(0,0) = Eigen::AngleAxisd(0.4, Eigen::Vector3d(0.1, 0.2, 0.3).normalized()).toRotationMatrix();
H_12_expected.block<3,1>(0,3) << 0.5, 0.6, 0.7;
std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > rvecs1, tvecs1, rvecs2, tvecs2;
double scale = 1.5;
int motionCount = 200;
double sigma = 0.0005;
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis10(-10.0, 10.0);
std::uniform_int_distribution<> dis100(-100.0, 100.0);
std::normal_distribution<> gaussian(0,sigma);
for (int i = 0; i < motionCount; ++i)
{
double droll = boost::math::constants::radian<double>()*dis10(gen);
droll = 0;
double dpitch = boost::math::constants::radian<double>()*dis10(gen);
dpitch = 0;
double dyaw = boost::math::constants::radian<double>()*dis100(gen);
double dx = dis10(gen);
double dy = dis10(gen);
double dz = dis10(gen);
dz = 0;
Eigen::Matrix3d R;
R = Eigen::AngleAxisd(dyaw, Eigen::Vector3d::UnitZ()) *
Eigen::AngleAxisd(dpitch, Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(droll, Eigen::Vector3d::UnitX());
Eigen::Matrix4d H = Eigen::Matrix4d::Identity();
H.block<3,3>(0,0) = R;
H.block<3,1>(0,3) << dx, dy, dz;
Eigen::Matrix4d H_ = H.inverse();
H = H_;
Eigen::Vector3d rvec1, tvec1, rvec2, tvec2;
Eigen::AngleAxisd angleAxis1(H.block<3,3>(0,0));
rvec1 = angleAxis1.angle() * angleAxis1.axis();
tvec1 = H.block<3,1>(0,3);
Eigen::AngleAxisd angleAxis2((H_12_expected.inverse() * H * H_12_expected).block<3,3>(0,0));
rvec2 = angleAxis2.angle() * angleAxis2.axis();
double roll, pitch, yaw;
camodocal::mat2RPY(angleAxis2.toRotationMatrix(), roll, pitch, yaw);
roll += gaussian(gen);
pitch += gaussian(gen);
yaw += gaussian(gen);
angleAxis2.fromRotationMatrix(camodocal::RPY2mat(roll, pitch, yaw));
rvec2 = angleAxis2.angle() * angleAxis2.axis();
tvec2 = (H_12_expected.inverse() * H * H_12_expected).block<3,1>(0,3);
rvecs1.push_back(rvec1);
tvecs1.push_back(tvec1);
rvecs2.push_back(rvec2);
tvecs2.push_back(tvec2);
}
Eigen::Matrix4d H_12;
camodocal::HandEyeCalibration::estimateHandEyeScrew(rvecs1, tvecs1, rvecs2, tvecs2, H_12, true);
Eigen::Matrix4d H1 = H_12_expected;
Eigen::Matrix4d H2 = H_12;
std::cout << "# INFO: H_12_expected = " << std::endl;
std::cout << H_12_expected << std::endl; ;
// std::cout << H1 << std::endl << H2 << std::endl;
// for (int i = 0; i < 4; ++i)
// {
// for (int j = 0; j < 4; ++j)
// {
// if (i == 2 && j == 3) continue;
// EXPECT_NEAR(H1(i,j),H2(i,j),0.0000000001) << "Elements differ at (" << i << "," << j << ")";
// }
// }
}
/*
TEST(HandEyeCalibration, EstimateWithUnitTranslation)
{
Eigen::Matrix4d H_12_expected = Eigen::Matrix4d::Identity();
// H_12_expected.block<3,3>(0,0) = Eigen::AngleAxisd(0.4, Eigen::Vector3d(0.0, 0.0, 0.3).normalized()).toRotationMatrix();
// H_12_expected.block<3,3>(0,0) = Eigen::AngleAxisd(0.4, Eigen::Vector3d(1, 1, 0.3).normalized()).toRotationMatrix();
H_12_expected.block<3,1>(0,3) << 0.5, 0.6, 0.7;
std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > rvecs1, tvecs1, rvecs2, tvecs2;
int motionCount = 2;
for (int i = 0; i < motionCount; ++i)
{
double droll = boost::math::constants::radian<double>()*dis10(gen));
double dpitch = boost::math::constants::radian<double>()*dis10(gen));
double dyaw = boost::math::constants::radian<double>()*dis10(gen));
double dx = dis1(gen);
double dy = dis1(gen);
double dz = dis1(gen);
Eigen::Matrix3d R;
R = Eigen::AngleAxisd(dyaw, Eigen::Vector3d::UnitZ()) *
Eigen::AngleAxisd(dpitch, Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(droll, Eigen::Vector3d::UnitX());
Eigen::Matrix4d H = Eigen::Matrix4d::Identity();
H.block<3,3>(0,0) = R;
H.block<3,1>(0,3) << dx, dy, dz;
Eigen::Matrix4d H_ = H.inverse();
H = H_;
Eigen::Vector3d rvec1, tvec1, rvec2, tvec2;
Eigen::AngleAxisd angleAxis1((H_12_expected * H * H_12_expected.inverse()).block<3,3>(0,0));
rvec1 = angleAxis1.angle() * angleAxis1.axis();
tvec1 = (H_12_expected * H * H_12_expected.inverse()).block<3,1>(0,3);
tvec1.normalize();
Eigen::AngleAxisd angleAxis2(H.block<3,3>(0,0));
rvec2 = angleAxis2.angle() * angleAxis2.axis();
tvec2 = H.block<3,1>(0,3);
rvecs1.push_back(rvec1);
tvecs1.push_back(tvec1);
rvecs2.push_back(rvec2);
tvecs2.push_back(tvec2);
}
Eigen::Matrix4d H_12;
camodocal::HandEyeCalibration::estimateHandEyeScrew(rvecs1, tvecs1, rvecs2, tvecs2, H_12, true);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
EXPECT_NEAR(H_12_expected(i,j),H_12(i,j),0.0000000001) << "Elements differ at (" << i << "," << j << ")";
}
}
}
*/
}