apps/mm2txt/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/maps/CPointsMapXYZI.h>
18 #include <mrpt/maps/CPointsMapXYZIRT.h>
19 #include <mrpt/system/filesystem.h>
20 
21 // CLI flags:
22 static TCLAP::CmdLine cmd("mm2txt");
23 
24 static TCLAP::UnlabeledValueArg<std::string> argMapFile(
25  "input", "Load this metric map file (*.mm)", true, "myMap.mm", "myMap.mm", cmd);
26 
27 static TCLAP::MultiArg<std::string> argLayers(
28  "l", "layer",
29  "Layer to export. If not provided, all will be exported. This argument can "
30  "appear several times.",
31  false, "layerName", cmd);
32 
33 void run_mm2txt()
34 {
35  using namespace std::string_literals;
36 
37  const auto& filInput = argMapFile.getValue();
38 
39  ASSERT_FILE_EXISTS_(argMapFile.getValue());
40 
41  std::cout << "[mm-info] Reading input map from: '" << filInput << "'..." << std::endl;
42 
44  mm.load_from_file(filInput);
45 
46  std::cout << "[mm-info] Done read map. Contents:\n" << mm.contents_summary() << std::endl;
47 
48  std::vector<std::string> layers;
49  if (argLayers.isSet())
50  {
51  // only selected:
52  for (const auto& s : argLayers.getValue()) layers.push_back(s);
53  }
54  else
55  { // all:
56  for (const auto& [name, map] : mm.layers) layers.push_back(name);
57  }
58 
59  const auto baseFilName = mrpt::system::extractFileName(filInput);
60 
61  // Export them:
62  for (const auto& name : layers)
63  {
64  const std::string filName = baseFilName + "_"s + name + ".txt"s;
65 
66  std::cout << "Exporting layer: '" << name << "' to file '" << filName << "'..."
67  << std::endl;
68 
69  ASSERTMSG_(mm.layers.count(name) == 1, "Layer not found in metric map!");
70 
71  auto* pts = mp2p_icp::MapToPointsMap(*mm.layers.at(name));
72  if (!pts)
73  {
74  THROW_EXCEPTION_FMT(
75  "Layer '%s' is of type '%s' which cannot be converted into a "
76  "point cloud for exporting in TXT format.",
77  name.c_str(), mm.layers.at(name)->GetRuntimeClass()->className);
78  }
79 
80  if (auto* xyzirt = dynamic_cast<const mrpt::maps::CPointsMapXYZIRT*>(pts); xyzirt)
81  {
82  xyzirt->saveXYZIRT_to_text_file(filName);
83  }
84  else if (auto* xyzi = dynamic_cast<const mrpt::maps::CPointsMapXYZI*>(pts); xyzi)
85  {
86  xyzi->saveXYZI_to_text_file(filName);
87  }
88  else
89  {
90  pts->save3D_to_text_file(filName);
91  }
92  }
93 }
94 
95 int main(int argc, char** argv)
96 {
97  try
98  {
99  // Parse arguments:
100  if (!cmd.parse(argc, argv)) return 1; // should exit.
101 
102  run_mm2txt();
103  }
104  catch (const std::exception& e)
105  {
106  std::cerr << e.what();
107  return 1;
108  }
109  return 0;
110 }
s
XmlRpcServer s
argMapFile
static TCLAP::UnlabeledValueArg< std::string > argMapFile("input", "Load this metric map file (*.mm)", true, "myMap.mm", "myMap.mm", cmd)
testing::internal::string
::std::string string
Definition: gtest.h:1979
argLayers
static TCLAP::MultiArg< std::string > argLayers("l", "layer", "Layer to export. If not provided, all will be exported. This argument can " "appear several times.", false, "layerName", cmd)
mp2p_icp::MapToPointsMap
const mrpt::maps::CPointsMap * MapToPointsMap(const mrpt::maps::CMetricMap &map)
Definition: metricmap.cpp:624
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
cmd
static TCLAP::CmdLine cmd("mm2txt")
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::layers
std::map< layer_name_t, mrpt::maps::CMetricMap::Ptr > layers
Definition: metricmap.h:82
run_mm2txt
void run_mm2txt()
Definition: apps/mm2txt/main.cpp:33
main
int main(int argc, char **argv)
Definition: apps/mm2txt/main.cpp:95


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