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


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