apps/mm-georef/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 <mp2p_icp/metricmap.h>
15 #include <mrpt/3rdparty/tclap/CmdLine.h>
16 #include <mrpt/containers/yaml.h>
17 #include <mrpt/io/CFileGZInputStream.h>
18 #include <mrpt/io/CFileGZOutputStream.h>
19 #include <mrpt/serialization/CArchive.h>
20 #include <mrpt/system/filesystem.h>
21 #include <mrpt/system/os.h>
22 
23 #include <fstream>
24 
25 namespace
26 {
27 
28 bool is_binary_file(const std::string& fil)
29 {
30  return mrpt::system::extractFileExtension(fil) == "georef";
31 }
32 
33 // CLI flags:
34 struct Cli
35 {
36  TCLAP::CmdLine cmd{"mm-georef"};
37 
38  TCLAP::ValueArg<std::string> argMap{
39  "m", "mao", "Input/Output .mm file to operate on", true, "theMap.mm", "theMap.mm", cmd};
40 
41  TCLAP::ValueArg<std::string> argGeoRef{
42  "g",
43  "georef",
44  "Input/Output file with geo-referencing metadata, in binary format (`*.georef`) or yaml "
45  "(*.yaml,*.yml) ",
46  true,
47  "(myMap.georef|myMap.yaml)",
48  "(myMap.georef|myMap.yaml)",
49  cmd};
50 
51  TCLAP::SwitchArg argExtract{
52  "", "extract-from-map",
53  "Reads the geo-referencing data from the map and saves it to a .georef "
54  "file",
55  cmd};
56 
57  TCLAP::SwitchArg argInject{
58  "", "inject-to-map",
59  "Reads the geo-referencing data from an input file and stores it into "
60  "the existing map file."
61  "file",
62  cmd};
63 
64  TCLAP::ValueArg<std::string> arg_plugins{
65  "l", "load-plugins", "One or more (comma separated) *.so files to load as plugins",
66  false, "foobar.so", "foobar.so",
67  cmd};
68 };
69 
70 void run_mm_extract(Cli& cli)
71 {
72  const auto& filInput = cli.argMap.getValue();
73 
74  std::cout << "[mm-georef] Reading input map from: '" << filInput << "'..." << std::endl;
75 
77  mm.load_from_file(filInput);
78 
79  std::cout << "[mm-georef] Done read map:" << mm.contents_summary() << std::endl;
80  ASSERT_(!mm.empty());
81 
82  std::cout << "[mm-georef] Done. Output map: " << mm.contents_summary() << std::endl;
83 
84  // Save as .georef file:
85  const auto filOut = cli.argGeoRef.getValue();
86  std::cout << "[mm-georef] Writing geo-referencing metamap to: '" << filOut << "'..."
87  << std::endl;
88 
89  if (is_binary_file(filOut))
90  {
91  mrpt::io::CFileGZOutputStream f(filOut);
92  auto arch = mrpt::serialization::archiveFrom(f);
93  arch << mm.georeferencing;
94  }
95  else
96  {
97  const auto yamlData = mp2p_icp::ToYAML(mm.georeferencing);
98  std::ofstream of(filOut);
99  ASSERT_(of.is_open());
100  of << yamlData;
101  }
102 }
103 
104 void run_mm_inject(Cli& cli)
105 {
106  // Load .georef file:
107  const auto filIn = cli.argGeoRef.getValue();
108  std::cout << "[mm-georef] Reading geo-referencing metamap from: '" << filIn << "'..."
109  << std::endl;
110 
111  std::optional<mp2p_icp::metric_map_t::Georeferencing> g;
112 
113  if (is_binary_file(filIn))
114  {
115  mrpt::io::CFileGZInputStream f(filIn);
116  auto arch = mrpt::serialization::archiveFrom(f);
117  arch >> g;
118  }
119  else
120  {
121  const auto yamlData = mrpt::containers::yaml::FromFile(filIn);
122 
123  g = mp2p_icp::FromYAML(yamlData);
124  }
125 
126  const auto& filMap = cli.argMap.getValue();
127 
128  std::cout << "[mm-georef] Reading input map from: '" << filMap << "'..." << std::endl;
129 
131  mm.load_from_file(filMap);
132 
133  std::cout << "[mm-georef] Done read map: " << mm.contents_summary() << std::endl;
134 
135  // Set geo-ref data:
136  mm.georeferencing = g;
137 
138  std::cout << "[mm-georef] Set georeferencing data. Updated map data: " << mm.contents_summary()
139  << std::endl;
140 
141  std::cout << "[mm-georef] Saving updated map to: '" << filMap << "'..." << std::endl;
142 
143  mm.save_to_file(filMap);
144 }
145 
146 void run_mm_georef(Cli& cli)
147 {
148  // Load plugins:
149  if (cli.arg_plugins.isSet())
150  {
151  std::string errMsg;
152  const auto plugins = cli.arg_plugins.getValue();
153  std::cout << "Loading plugin(s): " << plugins << std::endl;
154  if (!mrpt::system::loadPluginModules(plugins, errMsg)) throw std::runtime_error(errMsg);
155  }
156 
157  if (cli.argExtract.isSet())
158  {
159  return run_mm_extract(cli);
160  }
161  else if (cli.argInject.isSet())
162  {
163  return run_mm_inject(cli);
164  }
165 
166  THROW_EXCEPTION(
167  "One of either '--extract-from-map' or '--inject-to-map' flags must be "
168  "provided.");
169 }
170 } // namespace
171 
172 int main(int argc, char** argv)
173 {
174  try
175  {
176  Cli cli;
177 
178  // Parse arguments:
179  if (!cli.cmd.parse(argc, argv)) return 1; // should exit.
180 
181  run_mm_georef(cli);
182  }
183  catch (const std::exception& e)
184  {
185  std::cerr << e.what();
186  return 1;
187  }
188  return 0;
189 }
cli
std::unique_ptr< cli_flags > cli
Definition: sm-cli-main.cpp:29
mp2p_icp::metric_map_t::empty
virtual bool empty() const
Definition: metricmap.cpp:364
arg_plugins
static TCLAP::ValueArg< std::string > arg_plugins("l", "load-plugins", "One or more (comma separated) *.so files to load as plugins", false, "foobar.so", "foobar.so", cmd)
kitti-run-seq.f
string f
Definition: kitti-run-seq.py:12
mp2p_icp::metric_map_t::save_to_file
bool save_to_file(const std::string &fileName) const
Definition: metricmap.cpp:545
kitti-run-seq.cmd
string cmd
Definition: kitti-run-seq.py:14
testing::internal::string
::std::string string
Definition: gtest.h:1979
main
int main(int argc, char **argv)
Definition: apps/mm-georef/main.cpp:172
mp2p_icp::ToYAML
mrpt::containers::yaml ToYAML(const std::optional< metric_map_t::Georeferencing > &gref)
Serialization of geo-reference information as YAML.
Definition: metricmap.cpp:762
mp2p_icp::FromYAML
std::optional< metric_map_t::Georeferencing > FromYAML(const mrpt::containers::yaml &yaml_data)
Serialization of geo-reference information as YAML.
Definition: metricmap.cpp:731
mp2p_icp::metric_map_t::contents_summary
virtual std::string contents_summary() const
Definition: metricmap.cpp:481
mp2p_icp::metric_map_t::load_from_file
bool load_from_file(const std::string &fileName)
Definition: metricmap.cpp:556
metricmap.h
Generic representation of pointcloud(s) and/or extracted features.
mp2p_icp::metric_map_t
Generic container of pointcloud(s), extracted features and other maps.
Definition: metricmap.h:55
mp2p_icp::metric_map_t::georeferencing
std::optional< Georeferencing > georeferencing
Definition: metricmap.h:119


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