sm-cli-main.cpp
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  * A repertory of multi primitive-to-primitive (MP2P) ICP algorithms in C++
3  * Copyright (C) 2018-2024 Jose Luis Blanco, University of Almeria
4  * See LICENSE for license information.
5  * ------------------------------------------------------------------------- */
6 
14 #include <mrpt/3rdparty/tclap/CmdLine.h>
15 #include <mrpt/core/exceptions.h>
16 #include <mrpt/system/filesystem.h>
17 #include <mrpt/system/os.h> // consoleColorAndStyle()
18 
19 // register, for read_input_sm_from_cli()
20 #include <mrpt/maps/registerAllClasses.h>
21 #include <mrpt/obs/registerAllClasses.h>
22 
23 #include <iostream>
24 #include <map>
25 
26 #include "sm-cli.h"
27 
28 // ======= Command handlers =======
29 std::unique_ptr<cli_flags> cli;
30 
31 const std::map<std::string, cmd_t> cliCommands = {
32  {"help", cmd_t(&printListCommands)},
33  {"info", cmd_t(&commandInfo)},
34  {"export-keyframes", cmd_t(&commandExportKF)},
35  {"export-rawlog", cmd_t(&commandExportRawlog)},
36  {"cut", cmd_t(&commandCut)},
37  {"level", cmd_t(&commandLevel)},
38  {"trim", cmd_t(&commandTrim)},
39  {"join", cmd_t(&commandJoin)},
40  {"tf", cmd_t(&commandTf)},
41 };
42 
44 {
45  mrpt::system::consoleColorAndStyle(mrpt::system::ConsoleForegroundColor::RED);
46 }
47 
49 {
50  mrpt::system::consoleColorAndStyle(mrpt::system::ConsoleForegroundColor::DEFAULT);
51 }
52 
53 int main(int argc, char** argv)
54 {
55  try
56  {
57  cli = std::make_unique<cli_flags>();
58 
59  if (!cli->cmd.parse(argc, argv))
60  {
62  return 1;
63  }
64 
65  if (cli->argVersion.isSet())
66  {
67  printVersion();
68  return 0;
69  }
70 
71  // Take first unlabeled argument:
73  if (const auto& lst = cli->argCmd.getValue(); !lst.empty()) command = lst.at(0);
74 
75  // Look up command in table:
76  auto itCmd = cliCommands.find(command);
77 
78  if (!cli->argCmd.isSet() || itCmd == cliCommands.end())
79  {
80  if (!cli->argHelp.isSet())
81  {
83  std::cerr << "Error: missing or unknown command.\n";
85  }
87  return 1;
88  }
89 
90  // Execute command:
91  return (itCmd->second)();
92  }
93  catch (const std::exception& e)
94  {
95  std::cerr << "ERROR: " << mrpt::exception_to_str(e);
96  return 1;
97  }
98  return 0;
99 }
100 
102 {
103  fprintf(
104  stderr,
105  R"XXX(sm-cli v%s: CLI tool to manipulate and inspect 'simplemap's.
106 
107 Available commands:
108  sm-cli cut Cut part of a .simplemap file into a new file.
109  sm-cli export-keyframes Export KF poses (opt: twist too) as TUM format.
110  sm-cli export-rawlog Export KFs as rawlog for inspection.
111  sm-cli info Analyze a .simplemap file.
112  sm-cli join Join two or more .simplemap files into one.
113  sm-cli level Makes a .simplemap file level (horizontal).
114  sm-cli tf Applies a SE(3) transform by the left to a map.
115  sm-cli trim Extracts part of a .simplemap inside a given box.
116  sm-cli --version Shows program version.
117  sm-cli --help Shows this information.
118 
119 Or use `sm-cli <COMMAND> --help` for further options
120 )XXX",
121  MP2P_ICP_VERSION);
122  return 0;
123 }
124 
125 void printVersion() { std::cout << "sm-cli v" << MP2P_ICP_VERSION << std::endl; }
126 
127 // Common part of most commands:
128 mrpt::maps::CSimpleMap read_input_sm_from_cli(const std::string& inFile)
129 {
130  ASSERT_FILE_EXISTS_(inFile);
131 
132  const auto sizeBytes = mrpt::system::getFileSize(inFile);
133 
134  std::cout << "Loading: '" << inFile << "' of " << mrpt::system::unitsFormat(sizeBytes) << "B..."
135  << std::endl;
136 
137  // register mrpt-obs classes, since we are not using them explicitly and
138  // hence they are not auto-loading.
139  mrpt::maps::registerAllClasses_mrpt_maps();
140  mrpt::obs::registerAllClasses_mrpt_obs();
141 
142  mrpt::maps::CSimpleMap sm;
143 
144  bool loadOk = sm.loadFromFile(inFile);
145  ASSERT_(loadOk);
146 
147  return sm;
148 }
commandExportRawlog
int commandExportRawlog()
Definition: sm-cli-export-rawlog.cpp:23
cliCommands
const std::map< std::string, cmd_t > cliCommands
Definition: sm-cli-main.cpp:31
cli
std::unique_ptr< cli_flags > cli
Definition: sm-cli-main.cpp:29
commandCut
int commandCut()
Definition: sm-cli-cut.cpp:17
command
ROSLIB_DECL std::string command(const std::string &cmd)
commandLevel
int commandLevel()
Definition: sm-cli-level.cpp:20
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:113
cmd_t
std::function< int(void)> cmd_t
Definition: sm-cli.h:65
commandTf
int commandTf()
Definition: sm-cli-tf.cpp:20
commandTrim
int commandTrim()
Definition: sm-cli-trim.cpp:17
sm-cli.h
commandInfo
int commandInfo()
Definition: sm-cli-info.cpp:19
main
int main(int argc, char **argv)
Definition: sm-cli-main.cpp:53
commandExportKF
int commandExportKF()
Definition: sm-cli-export-kfs.cpp:20
setConsoleNormalColor
void setConsoleNormalColor()
Definition: sm-cli-main.cpp:48
setConsoleErrorColor
void setConsoleErrorColor()
Definition: sm-cli-main.cpp:43
commandJoin
int commandJoin()
Definition: sm-cli-join.cpp:17
printListCommands
int printListCommands()
Definition: sm-cli-main.cpp:101
printVersion
void printVersion()
Definition: sm-cli-main.cpp:110


mp2p_icp
Author(s):
autogenerated on Mon May 26 2025 02:45:50