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(
46  mrpt::system::ConsoleForegroundColor::RED);
47 }
48 
50 {
51  mrpt::system::consoleColorAndStyle(
52  mrpt::system::ConsoleForegroundColor::DEFAULT);
53 }
54 
55 int main(int argc, char** argv)
56 {
57  try
58  {
59  cli = std::make_unique<cli_flags>();
60 
61  if (!cli->cmd.parse(argc, argv))
62  {
64  return 1;
65  }
66 
67  if (cli->argVersion.isSet())
68  {
69  printVersion();
70  return 0;
71  }
72 
73  // Take first unlabeled argument:
75  if (const auto& lst = cli->argCmd.getValue(); !lst.empty())
76  command = lst.at(0);
77 
78  // Look up command in table:
79  auto itCmd = cliCommands.find(command);
80 
81  if (!cli->argCmd.isSet() || itCmd == cliCommands.end())
82  {
83  if (!cli->argHelp.isSet())
84  {
86  std::cerr << "Error: missing or unknown command.\n";
88  }
90  return 1;
91  }
92 
93  // Execute command:
94  return (itCmd->second)();
95  }
96  catch (const std::exception& e)
97  {
98  std::cerr << "ERROR: " << mrpt::exception_to_str(e);
99  return 1;
100  }
101  return 0;
102 }
103 
105 {
106  fprintf(
107  stderr,
108  R"XXX(sm-cli v%s: CLI tool to manipulate and inspect 'simplemap's.
109 
110 Available commands:
111  sm-cli cut Cut part of a .simplemap file into a new file.
112  sm-cli export-keyframes Export KF poses (opt: twist too) as TUM format.
113  sm-cli export-rawlog Export KFs as rawlog for inspection.
114  sm-cli info Analyze a .simplemap file.
115  sm-cli join Join two or more .simplemap files into one.
116  sm-cli level Makes a .simplemap file level (horizontal).
117  sm-cli tf Applies a SE(3) transform by the left to a map.
118  sm-cli trim Extracts part of a .simplemap inside a given box.
119  sm-cli --version Shows program version.
120  sm-cli --help Shows this information.
121 
122 Or use `sm-cli <COMMAND> --help` for further options
123 )XXX",
124  MP2P_ICP_VERSION);
125  return 0;
126 }
127 
128 void printVersion()
129 {
130  std::cout << "sm-cli v" << MP2P_ICP_VERSION << std::endl;
131 }
132 
133 // Common part of most commands:
134 mrpt::maps::CSimpleMap read_input_sm_from_cli(const std::string& inFile)
135 {
136  ASSERT_FILE_EXISTS_(inFile);
137 
138  const auto sizeBytes = mrpt::system::getFileSize(inFile);
139 
140  std::cout << "Loading: '" << inFile << "' of "
141  << mrpt::system::unitsFormat(sizeBytes) << "B..." << std::endl;
142 
143  // register mrpt-obs classes, since we are not using them explicitly and
144  // hence they are not auto-loading.
145  mrpt::maps::registerAllClasses_mrpt_maps();
146  mrpt::obs::registerAllClasses_mrpt_obs();
147 
148  mrpt::maps::CSimpleMap sm;
149 
150  bool loadOk = sm.loadFromFile(inFile);
151  ASSERT_(loadOk);
152 
153  return sm;
154 }
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:119
cmd_t
std::function< int(void)> cmd_t
Definition: sm-cli.h:77
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:55
commandExportKF
int commandExportKF()
Definition: sm-cli-export-kfs.cpp:20
setConsoleNormalColor
void setConsoleNormalColor()
Definition: sm-cli-main.cpp:49
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:104
printVersion
void printVersion()
Definition: sm-cli-main.cpp:113


mp2p_icp
Author(s):
autogenerated on Wed Oct 2 2024 02:45:25