sm-cli-level.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/core/exceptions.h>
11 #include <mrpt/maps/CSimpleMap.h>
12 #include <mrpt/math/CLevenbergMarquardt.h>
13 
14 #include <iostream>
15 
16 #include "sm-cli.h"
17 
18 static int printCommandsLevel(bool showErrorMsg);
19 
21 {
22  const auto& lstCmds = cli->argCmd.getValue();
23  if (cli->argHelp.isSet()) return printCommandsLevel(false);
24  if (lstCmds.size() != 3) return printCommandsLevel(true);
25 
26  // Take second unlabeled argument:
27  const std::string inFile = lstCmds.at(1);
28  const std::string outFile = lstCmds.at(2);
29 
30  mrpt::maps::CSimpleMap sm = read_input_sm_from_cli(inFile);
31 
32  ASSERT_(!sm.empty());
33 
34  std::vector<mrpt::poses::CPose3D> poses; // all SF KeyFrame poses
35  poses.reserve(sm.size());
36 
37  for (const auto& [pose, sf, twist] : sm)
38  {
39  const auto p = pose->getMeanVal();
40  poses.push_back(p);
41  }
42 
43  // Optimize them such as the vertical variation is minimized:
44 
45  // The error function F(x):
46  auto myCostFunction =
47  [&](const mrpt::math::CVectorDouble& x,
48  [[maybe_unused]] const mrpt::math::CVectorDouble& params,
49  mrpt::math::CVectorDouble& err) {
50  const auto delta =
51  mrpt::poses::CPose3D::FromYawPitchRoll(x[0], x[1], x[2]);
52 
53  const double z0 = poses.at(0).translation().z;
54 
55  // cost=non-horizontality after transformation:
56  err.resize(0);
57  std::transform(
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;
62  });
63  };
64 
65  mrpt::math::CVectorDouble optimal_x, initial_x, params;
66 
67  mrpt::math::CLevenbergMarquardt::TResultInfo info;
68 
69  // x: [yaw pitch roll]
70  initial_x.assign(3U, 0.0);
71 
72  const auto increments_x = mrpt::math::CVectorDouble::Constant(3, 1, 1e-6);
73 
74  mrpt::system::VerbosityLevel logLevel = mrpt::system::LVL_INFO;
75  if (cli->arg_verbosity_level.isSet())
76  {
77  using vl = mrpt::typemeta::TEnumType<mrpt::system::VerbosityLevel>;
78  logLevel = vl::name2value(cli->arg_verbosity_level.getValue());
79  }
80 
81  mrpt::math::CLevenbergMarquardt lm;
82  lm.execute(
83  optimal_x, initial_x, myCostFunction, increments_x, params, info,
84  logLevel);
85 
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;
89 
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;
93 
94  // Modify KFs:
95  for (auto& [pose, sf, twist] : sm)
96  {
97  const auto p = pose->getMeanVal();
98  // This changes both, the mean and the covariance:
99  pose->changeCoordinatesReference(delta);
100  }
101 
102  // save:
103  std::cout << "Saving result to: '" << outFile << "... " << std::endl;
104  sm.saveToFile(outFile);
105 
106  return 0;
107 }
108 
109 int printCommandsLevel(bool showErrorMsg)
110 {
111  if (showErrorMsg)
112  {
114  std::cerr << "Error: missing or unknown subcommand.\n";
116  }
117 
118  fprintf(
119  stderr,
120  R"XXX(Usage:
121 
122  sm-cli level <input.simplemap> <output.simplemap>
123 
124 )XXX");
125 
126  return showErrorMsg ? 1 : 0;
127 }
cli
std::unique_ptr< cli_flags > cli
Definition: sm-cli-main.cpp:29
commandLevel
int commandLevel()
Definition: sm-cli-level.cpp:20
test.x
x
Definition: test.py:4
testing::internal::string
::std::string string
Definition: gtest.h:1979
read_input_sm_from_cli
mrpt::maps::CSimpleMap read_input_sm_from_cli(const std::string &inFile)
Definition: sm-cli-main.cpp:119
sm-cli.h
setConsoleNormalColor
void setConsoleNormalColor()
Definition: sm-cli-main.cpp:49
setConsoleErrorColor
void setConsoleErrorColor()
Definition: sm-cli-main.cpp:43
printCommandsLevel
static int printCommandsLevel(bool showErrorMsg)
Definition: sm-cli-level.cpp:109


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:41