10 #include <mrpt/core/exceptions.h>
11 #include <mrpt/maps/CSimpleMap.h>
12 #include <mrpt/math/CLevenbergMarquardt.h>
22 const auto& lstCmds =
cli->argCmd.getValue();
34 std::vector<mrpt::poses::CPose3D> poses;
35 poses.reserve(sm.size());
37 for (
const auto& [pose, sf, twist] : sm)
39 const auto p = pose->getMeanVal();
46 auto myCostFunction = [&](
const mrpt::math::CVectorDouble&
x,
47 [[maybe_unused]]
const mrpt::math::CVectorDouble& params,
48 mrpt::math::CVectorDouble& err)
50 const auto delta = mrpt::poses::CPose3D::FromYawPitchRoll(
x[0],
x[1],
x[2]);
52 const double z0 = poses.at(0).translation().z;
57 poses.cbegin(), poses.cend(), std::back_inserter(err),
58 [&delta, z0](
const mrpt::poses::CPose3D& p)
60 auto newPose = delta + p;
61 return newPose.translation().z - z0;
65 mrpt::math::CVectorDouble optimal_x, initial_x, params;
67 mrpt::math::CLevenbergMarquardt::TResultInfo info;
70 initial_x.assign(3U, 0.0);
72 const auto increments_x = mrpt::math::CVectorDouble::Constant(3, 1, 1e-6);
74 mrpt::system::VerbosityLevel logLevel = mrpt::system::LVL_INFO;
75 if (
cli->arg_verbosity_level.isSet())
77 using vl = mrpt::typemeta::TEnumType<mrpt::system::VerbosityLevel>;
78 logLevel = vl::name2value(
cli->arg_verbosity_level.getValue());
81 mrpt::math::CLevenbergMarquardt lm;
82 lm.execute(optimal_x, initial_x, myCostFunction, increments_x, params, info, logLevel);
84 std::cout <<
"Iterations: " << info.iterations_executed << std::endl;
85 std::cout <<
"Squared error (initial->final): " << info.initial_sqr_err <<
" => "
86 << info.final_sqr_err << std::endl;
89 mrpt::poses::CPose3D::FromYawPitchRoll(optimal_x[0], optimal_x[1], optimal_x[2]);
90 std::cout <<
"Final optimized rotation: " << delta << std::endl;
93 for (
auto& [pose, sf, twist] : sm)
95 const auto p = pose->getMeanVal();
97 pose->changeCoordinatesReference(delta);
101 std::cout <<
"Saving result to: '" << outFile <<
"... " << std::endl;
102 sm.saveToFile(outFile);
112 std::cerr <<
"Error: missing or unknown subcommand.\n";
120 sm-cli level <input.simplemap> <output.simplemap>
124 return showErrorMsg ? 1 : 0;