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();
47 [&](
const mrpt::math::CVectorDouble&
x,
48 [[maybe_unused]]
const mrpt::math::CVectorDouble& params,
49 mrpt::math::CVectorDouble& err) {
51 mrpt::poses::CPose3D::FromYawPitchRoll(
x[0],
x[1],
x[2]);
53 const double z0 = poses.at(0).translation().z;
58 poses.cbegin(), poses.cend(), std::back_inserter(err),
59 [&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;
83 optimal_x, initial_x, myCostFunction, increments_x, params, info,
86 std::cout <<
"Iterations: " << info.iterations_executed << std::endl;
87 std::cout <<
"Squared error (initial->final): " << info.initial_sqr_err
88 <<
" => " << info.final_sqr_err << std::endl;
90 const auto delta = mrpt::poses::CPose3D::FromYawPitchRoll(
91 optimal_x[0], optimal_x[1], optimal_x[2]);
92 std::cout <<
"Final optimized rotation: " << delta << std::endl;
95 for (
auto& [pose, sf, twist] : sm)
97 const auto p = pose->getMeanVal();
99 pose->changeCoordinatesReference(delta);
103 std::cout <<
"Saving result to: '" << outFile <<
"... " << std::endl;
104 sm.saveToFile(outFile);
114 std::cerr <<
"Error: missing or unknown subcommand.\n";
122 sm-cli level <input.simplemap> <output.simplemap>
126 return showErrorMsg ? 1 : 0;