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();
 
   30 static double normald(
const double sigma) { 
return rnd.drawGaussian1D_normalized() * sigma; }
 
   31 static float  normalf(
const float sigma) { 
return rnd.drawGaussian1D_normalized() * sigma; }
 
   39     const CPose3D p = CPose3D(
 
   43         rnd.drawUniform(-M_PI, M_PI), 
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
 
   44         rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
 
   46     mrpt::tfest::TMatchingPair pair;
 
   51     mrpt::math::CMatrixFixed<double, 3, 12> J1;
 
   56     const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
 
   58     const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
 
   61     CMatrixDouble numJacob;
 
   63         CVectorFixedDouble<6> x_mean;
 
   66         CVectorFixedDouble<6> x_incrs;
 
   68         mrpt::math::estimateJacobian(
 
   72                 const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)>(
 
   75                     const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)
 
   78                     const CPose3D incr         = Lie::SE<3>::exp(eps);
 
   79                     const CPose3D D_expEpsilon = D + incr;
 
   82             x_incrs, p, numJacob);
 
   85     if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
 
   87         std::cerr << 
"numJacob:\n" 
   88                   << numJacob.asEigen() << 
"\njacob:\n" 
   89                   << jacob.asEigen() << 
"\nDiff:\n" 
   90                   << (numJacob - jacob) << 
"\nJ1:\n" 
   91                   << J1.asEigen() << 
"\n";
 
   92         THROW_EXCEPTION(
"Jacobian mismatch, see above.");
 
  102     const CPose3D p = CPose3D(
 
  106         rnd.drawUniform(-M_PI, M_PI), 
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
 
  107         rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
 
  121     mrpt::math::CMatrixFixed<double, 3, 12> J1;
 
  126     const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
 
  128     const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
 
  131     CMatrixDouble numJacob;
 
  133         CVectorFixedDouble<6> x_mean;
 
  136         CVectorFixedDouble<6> x_incrs;
 
  138         mrpt::math::estimateJacobian(
 
  142                 const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)>(
 
  145                     const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)
 
  148                     const CPose3D incr         = Lie::SE<3>::exp(eps);
 
  149                     const CPose3D D_expEpsilon = D + incr;
 
  152             x_incrs, p, numJacob);
 
  155     if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
 
  157         std::cerr << 
"relativePose: " << p
 
  160                   << numJacob.asEigen()
 
  166                   << (numJacob - jacob)
 
  169                   << J1.asEigen() << 
"\n";
 
  170         THROW_EXCEPTION(
"Jacobian mismatch, see above.");
 
  180     const CPose3D p = CPose3D(
 
  184         rnd.drawUniform(-M_PI, M_PI), 
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
 
  185         rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
 
  201     mrpt::math::CMatrixFixed<double, 3, 12> J1;
 
  206     const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
 
  208     const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
 
  211     CMatrixDouble numJacob;
 
  213         CVectorFixedDouble<6> x_mean;
 
  216         CVectorFixedDouble<6> x_incrs;
 
  218         mrpt::math::estimateJacobian(
 
  222                 const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)>(
 
  225                     const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)
 
  228                     const CPose3D incr         = Lie::SE<3>::exp(eps);
 
  229                     const CPose3D D_expEpsilon = D + incr;
 
  232             x_incrs, p, numJacob);
 
  235     if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
 
  237         std::cerr << 
"numJacob:\n" 
  238                   << numJacob.asEigen() << 
"\njacob:\n" 
  239                   << jacob.asEigen() << 
"\nDiff:\n" 
  240                   << (numJacob - jacob) << 
"\nJ1:\n" 
  241                   << J1.asEigen() << 
"\n";
 
  242         THROW_EXCEPTION(
"Jacobian mismatch, see above.");
 
  250 static void test_Jacob_error_line2line()
 
  252     const CPose3D p = CPose3D(
 
  256         rnd.drawUniform(-M_PI, M_PI), 
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
 
  257         rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
 
  276     mrpt::math::CMatrixFixed<double, 4, 12> J1;
 
  281     const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
 
  283     const mrpt::math::CMatrixFixed<double, 4, 6> jacob(J1 * dDexpe_de);
 
  286     CMatrixDouble numJacob;
 
  288         CVectorFixedDouble<6> x_mean;
 
  291         CVectorFixedDouble<6> x_incrs;
 
  293         mrpt::math::estimateJacobian(
 
  297                 const CVectorFixedDouble<6>& eps, 
const CPose3D& D,
 
  298                 CVectorFixedDouble<4>& err)>(
 
  301                     const CVectorFixedDouble<6>& eps, 
const CPose3D& D,
 
  302                     CVectorFixedDouble<4>& err) {
 
  304                     const CPose3D incr         = Lie::SE<3>::exp(eps);
 
  305                     const CPose3D D_expEpsilon = D + incr;
 
  308             x_incrs, p, numJacob);
 
  311     if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
 
  313         std::cerr << 
"numJacob:\n" 
  314                   << numJacob.asEigen() << 
"\njacob:\n" 
  315                   << jacob.asEigen() << 
"\nDiff:\n" 
  316                   << (numJacob - jacob) << 
"\nJ1:\n" 
  317                   << J1.asEigen() << 
"\n";
 
  318         THROW_EXCEPTION(
"Jacobian mismatch, see above.");
 
  329     const CPose3D p = CPose3D(
 
  333         rnd.drawUniform(-M_PI, M_PI), 
rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5),
 
  334         rnd.drawUniform(-M_PI * 0.5, M_PI * 0.5));
 
  353     mrpt::math::CMatrixFixed<double, 3, 12> J1;
 
  358     const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
 
  360     const mrpt::math::CMatrixFixed<double, 3, 6> jacob(J1 * dDexpe_de);
 
  363     CMatrixDouble numJacob;
 
  365         CVectorFixedDouble<6> x_mean;
 
  368         CVectorFixedDouble<6> x_incrs;
 
  370         mrpt::math::estimateJacobian(
 
  374                 const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)>(
 
  377                     const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<3>& err)
 
  380                     const CPose3D incr         = Lie::SE<3>::exp(eps);
 
  381                     const CPose3D D_expEpsilon = D + incr;
 
  384             x_incrs, p, numJacob);
 
  387     if ((numJacob.asEigen() - jacob.asEigen()).array().abs().maxCoeff() > 1e-5)
 
  389         std::cerr << 
"numJacob:\n" 
  390                   << numJacob.asEigen() << 
"\njacob:\n" 
  391                   << jacob.asEigen() << 
"\nDiff:\n" 
  392                   << (numJacob - jacob) << 
"\nJ1:\n" 
  393                   << J1.asEigen() << 
"\n";
 
  394         THROW_EXCEPTION(
"Jacobian mismatch, see above.");
 
  404     const CPose3D p = CPose3D(
 
  427     mrpt::math::CMatrixFixed<double, 4, 12> J1;
 
  429     mrpt::math::CVectorFixedDouble<4> ref_error;
 
  430     ref_error[0] = 0.0517;
 
  431     ref_error[1] = 0.2007;
 
  432     ref_error[2] = 0.6372;
 
  433     ref_error[3] = -1.1610;
 
  436     mrpt::math::CVectorFixedDouble<4> error =
 
  439     std::cout << 
"\nResultado: \n" 
  440               << error << 
"\nRecta A:\n" 
  446     const auto dDexpe_de = mrpt::poses::Lie::SE<3>::jacob_dDexpe_de(p);
 
  448     const mrpt::math::CMatrixFixed<double, 4, 6> jacob(J1 * dDexpe_de);
 
  451     CMatrixDouble numJacob;
 
  453         CVectorFixedDouble<6> x_mean;
 
  456         CVectorFixedDouble<6> x_incrs;
 
  458         mrpt::math::estimateJacobian(
 
  462                 const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<4>& err)>(
 
  465                     const CVectorFixedDouble<6>& eps, 
const CPose3D& D, CVectorFixedDouble<4>& err)
 
  468                     const CPose3D incr         = Lie::SE<3>::exp(eps);
 
  469                     const CPose3D D_expEpsilon = D + incr;
 
  472             x_incrs, p, numJacob);
 
  475     std::cout << 
"numJacob:\n" 
  476               << numJacob.asEigen() << 
"\njacob:\n" 
  477               << jacob.asEigen() << 
"\nDiff:\n" 
  478               << (numJacob - jacob) << 
"\nJ1:\n" 
  479               << J1.asEigen() << 
"\ndDexp_de:\n" 
  480               << dDexpe_de.asEigen() << 
"\n";
 
  486     const CPose3D p = CPose3D::Identity();
 
  493     pair.
ln_global.director = mrpt::math::TPoint3D(0, 0, 1).unitarize();
 
  500     mrpt::math::CMatrixFixed<double, 3, 12> J1;
 
  503     ASSERT_NEAR_(err[0], 0.0, 1e-6);
 
  506 int main([[maybe_unused]] 
int argc, [[maybe_unused]] 
char** argv)
 
  515         for (
int i = 0; i < 1000; i++)
 
  531     catch (std::exception& e)
 
  533         std::cerr << mrpt::exception_to_str(e) << 
"\n";