-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.cpp
118 lines (98 loc) · 3.33 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -------------------------------------------------------------------------
* A repertory of multi primitive-to-primitive (MP2P) ICP algorithms in C++
* Copyright (C) 2018-2024 Jose Luis Blanco, University of Almeria
* See LICENSE for license information.
* ------------------------------------------------------------------------- */
/**
* @file mm2txt/main.cpp
* @brief A CLI tool to export the layers of a metric map (`*.mm`) as CSV/TXT
* @author Jose Luis Blanco Claraco
* @date Feb 15, 2024
*/
#include <mp2p_icp/metricmap.h>
#include <mrpt/3rdparty/tclap/CmdLine.h>
#include <mrpt/containers/yaml.h>
#include <mrpt/maps/CPointsMapXYZI.h>
#include <mrpt/maps/CPointsMapXYZIRT.h>
#include <mrpt/system/filesystem.h>
// CLI flags:
static TCLAP::CmdLine cmd("mm2txt");
static TCLAP::UnlabeledValueArg<std::string> argMapFile(
"input", "Load this metric map file (*.mm)", true, "myMap.mm", "myMap.mm",
cmd);
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);
void run_mm2txt()
{
using namespace std::string_literals;
const auto& filInput = argMapFile.getValue();
ASSERT_FILE_EXISTS_(argMapFile.getValue());
std::cout << "[mm-info] Reading input map from: '" << filInput << "'..."
<< std::endl;
mp2p_icp::metric_map_t mm;
mm.load_from_file(filInput);
std::cout << "[mm-info] Done read map. Contents:\n"
<< mm.contents_summary() << std::endl;
std::vector<std::string> layers;
if (argLayers.isSet())
{
// only selected:
for (const auto& s : argLayers.getValue()) layers.push_back(s);
}
else
{ // all:
for (const auto& [name, map] : mm.layers) layers.push_back(name);
}
const auto baseFilName = mrpt::system::extractFileName(filInput);
// Export them:
for (const auto& name : layers)
{
const std::string filName = baseFilName + "_"s + name + ".txt"s;
std::cout << "Exporting layer: '" << name << "' to file '" << filName
<< "'..." << std::endl;
ASSERTMSG_(
mm.layers.count(name) == 1, "Layer not found in metric map!");
auto* pts = mp2p_icp::MapToPointsMap(*mm.layers.at(name));
if (!pts)
{
THROW_EXCEPTION_FMT(
"Layer '%s' is of type '%s' which cannot be converted into a "
"point cloud for exporting in TXT format.",
name.c_str(), mm.layers.at(name)->GetRuntimeClass()->className);
}
if (auto* xyzirt =
dynamic_cast<const mrpt::maps::CPointsMapXYZIRT*>(pts);
xyzirt)
{
xyzirt->saveXYZIRT_to_text_file(filName);
}
else if (auto* xyzi =
dynamic_cast<const mrpt::maps::CPointsMapXYZI*>(pts);
xyzi)
{
xyzi->saveXYZI_to_text_file(filName);
}
else
{
pts->save3D_to_text_file(filName);
}
}
}
int main(int argc, char** argv)
{
try
{
// Parse arguments:
if (!cmd.parse(argc, argv)) return 1; // should exit.
run_mm2txt();
}
catch (const std::exception& e)
{
std::cerr << e.what();
return 1;
}
return 0;
}