15 #include <mrpt/core/exceptions.h>
16 #include <mrpt/math/num_jacobian.h>
17 #include <mrpt/poses/CPose3D.h>
18 #include <mrpt/poses/Lie/SE.h>
19 #include <mrpt/random.h>
21 #include <Eigen/Dense>
25 using namespace mrpt::math;
26 using namespace mrpt::poses;
28 auto&
rnd = mrpt::random::getRandomGenerator();
32 return rnd.drawGaussian1D_normalized() * sigma;
36 return rnd.drawGaussian1D_normalized() * sigma;
45 const CPose3D p = CPose3D(
49 rnd.drawUniform(-M_PI, M_PI),
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
50 rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
52 mrpt::tfest::TMatchingPair pair;
57 mrpt::math::CMatrixFixed<double, 3, 12> J1;
62 const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
64 const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
67 CMatrixDouble numJacob;
69 CVectorFixedDouble<6> x_mean;
72 CVectorFixedDouble<6> x_incrs;
74 mrpt::math::estimateJacobian(
78 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
79 CVectorFixedDouble<3>& err)>(
82 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
83 CVectorFixedDouble<3>& err) {
85 const CPose3D incr = Lie::SE<3>::exp(eps);
86 const CPose3D D_expEpsilon = D + incr;
89 x_incrs, p, numJacob);
92 if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
94 std::cerr <<
"numJacob:\n"
95 << numJacob.asEigen() <<
"\njacob:\n"
96 << jacob.asEigen() <<
"\nDiff:\n"
97 << (numJacob - jacob) <<
"\nJ1:\n"
98 << J1.asEigen() <<
"\n";
99 THROW_EXCEPTION(
"Jacobian mismatch, see above.");
109 const CPose3D p = CPose3D(
113 rnd.drawUniform(-M_PI, M_PI),
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
114 rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
129 mrpt::math::CMatrixFixed<double, 3, 12> J1;
134 const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
136 const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
139 CMatrixDouble numJacob;
141 CVectorFixedDouble<6> x_mean;
144 CVectorFixedDouble<6> x_incrs;
146 mrpt::math::estimateJacobian(
150 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
151 CVectorFixedDouble<3>& err)>(
154 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
155 CVectorFixedDouble<3>& err) {
157 const CPose3D incr = Lie::SE<3>::exp(eps);
158 const CPose3D D_expEpsilon = D + incr;
161 x_incrs, p, numJacob);
164 if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
166 std::cerr <<
"relativePose: " << p
169 << numJacob.asEigen()
175 << (numJacob - jacob)
178 << J1.asEigen() <<
"\n";
179 THROW_EXCEPTION(
"Jacobian mismatch, see above.");
189 const CPose3D p = CPose3D(
193 rnd.drawUniform(-M_PI, M_PI),
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
194 rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
210 mrpt::math::CMatrixFixed<double, 3, 12> J1;
215 const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
217 const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
220 CMatrixDouble numJacob;
222 CVectorFixedDouble<6> x_mean;
225 CVectorFixedDouble<6> x_incrs;
227 mrpt::math::estimateJacobian(
231 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
232 CVectorFixedDouble<3>& err)>(
235 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
236 CVectorFixedDouble<3>& err) {
238 const CPose3D incr = Lie::SE<3>::exp(eps);
239 const CPose3D D_expEpsilon = D + incr;
242 x_incrs, p, numJacob);
245 if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
247 std::cerr <<
"numJacob:\n"
248 << numJacob.asEigen() <<
"\njacob:\n"
249 << jacob.asEigen() <<
"\nDiff:\n"
250 << (numJacob - jacob) <<
"\nJ1:\n"
251 << J1.asEigen() <<
"\n";
252 THROW_EXCEPTION(
"Jacobian mismatch, see above.");
260 static void test_Jacob_error_line2line()
262 const CPose3D p = CPose3D(
266 rnd.drawUniform(-M_PI, M_PI),
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
267 rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
286 mrpt::math::CMatrixFixed<double, 4, 12> J1;
291 const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
293 const mrpt::math::CMatrixFixed<double, 4, 6> jacob(J1 * dDexpe_de);
296 CMatrixDouble numJacob;
298 CVectorFixedDouble<6> x_mean;
301 CVectorFixedDouble<6> x_incrs;
303 mrpt::math::estimateJacobian(
307 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
308 CVectorFixedDouble<4>& err)>(
311 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
312 CVectorFixedDouble<4>& err) {
314 const CPose3D incr = Lie::SE<3>::exp(eps);
315 const CPose3D D_expEpsilon = D + incr;
318 x_incrs, p, numJacob);
321 if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
323 std::cerr <<
"numJacob:\n"
324 << numJacob.asEigen() <<
"\njacob:\n"
325 << jacob.asEigen() <<
"\nDiff:\n"
326 << (numJacob - jacob) <<
"\nJ1:\n"
327 << J1.asEigen() <<
"\n";
328 THROW_EXCEPTION(
"Jacobian mismatch, see above.");
339 const CPose3D p = CPose3D(
343 rnd.drawUniform(-M_PI, M_PI),
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
344 rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
363 mrpt::math::CMatrixFixed<double, 3, 12> J1;
368 const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
370 const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
373 CMatrixDouble numJacob;
375 CVectorFixedDouble<6> x_mean;
378 CVectorFixedDouble<6> x_incrs;
380 mrpt::math::estimateJacobian(
384 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
385 CVectorFixedDouble<3>& err)>(
388 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
389 CVectorFixedDouble<3>& err) {
391 const CPose3D incr = Lie::SE<3>::exp(eps);
392 const CPose3D D_expEpsilon = D + incr;
395 x_incrs, p, numJacob);
398 if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
400 std::cerr <<
"numJacob:\n"
401 << numJacob.asEigen() <<
"\njacob:\n"
402 << jacob.asEigen() <<
"\nDiff:\n"
403 << (numJacob - jacob) <<
"\nJ1:\n"
404 << J1.asEigen() <<
"\n";
405 THROW_EXCEPTION(
"Jacobian mismatch, see above.");
415 const CPose3D p = CPose3D(
438 mrpt::math::CMatrixFixed<double, 4, 12> J1;
440 mrpt::math::CVectorFixedDouble<4> ref_error;
441 ref_error[0] = 0.0517;
442 ref_error[1] = 0.2007;
443 ref_error[2] = 0.6372;
444 ref_error[3] = -1.1610;
447 mrpt::math::CVectorFixedDouble<4> error =
450 std::cout <<
"\nResultado: \n"
451 << error <<
"\nRecta A:\n"
457 const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
459 const mrpt::math::CMatrixFixed<double, 4, 6> jacob(J1 * dDexpe_de);
462 CMatrixDouble numJacob;
464 CVectorFixedDouble<6> x_mean;
467 CVectorFixedDouble<6> x_incrs;
469 mrpt::math::estimateJacobian(
473 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
474 CVectorFixedDouble<4>& err)>(
477 const CVectorFixedDouble<6>& eps,
const CPose3D& D,
478 CVectorFixedDouble<4>& err) {
480 const CPose3D incr = Lie::SE<3>::exp(eps);
481 const CPose3D D_expEpsilon = D + incr;
484 x_incrs, p, numJacob);
487 std::cout <<
"numJacob:\n"
488 << numJacob.asEigen() <<
"\njacob:\n"
489 << jacob.asEigen() <<
"\nDiff:\n"
490 << (numJacob - jacob) <<
"\nJ1:\n"
491 << J1.asEigen() <<
"\ndDexp_de:\n"
492 << dDexpe_de.asEigen() <<
"\n";
498 const CPose3D p = CPose3D::Identity();
505 pair.
ln_global.director = mrpt::math::TPoint3D(0, 0, 1).unitarize();
512 mrpt::math::CMatrixFixed<double, 3, 12> J1;
515 ASSERT_NEAR_(err[0], 0.0, 1e-6);
518 int main([[maybe_unused]]
int argc, [[maybe_unused]]
char** argv)
527 for (
int i = 0; i < 1000; i++)
543 catch (std::exception& e)
545 std::cerr << mrpt::exception_to_str(e) <<
"\n";