15 #include <mrpt/3rdparty/tclap/CmdLine.h>
16 #include <mrpt/maps/CColouredPointsMap.h>
17 #include <mrpt/maps/CPointsMapXYZI.h>
18 #include <mrpt/maps/CPointsMapXYZIRT.h>
19 #include <mrpt/maps/CSimplePointsMap.h>
20 #include <mrpt/math/CMatrixDynamic.h>
21 #include <mrpt/obs/CObservationPointCloud.h>
22 #include <mrpt/system/filesystem.h>
26 using namespace std::string_literals;
31 TCLAP::CmdLine
cmd{
"txt2mm"};
33 TCLAP::ValueArg<std::string>
argInput{
36 "Path to input TXT or CSV file. One point per row. Columns separated "
38 "spaces or commas. See docs for supported formats.",
45 "o",
"output",
"Output file to write to.",
true,
"out.mm",
"out.mm",
cmd};
47 TCLAP::ValueArg<std::string> argFormat{
50 "Point cloud format. Mandatory flag.\n"s
59 "l",
"layer",
"Target layer name (Default: \"raw\").",
false,
"raw",
"raw",
cmd};
61 TCLAP::ValueArg<int> argIndexXYZ{
62 "",
"column-x",
"Column index for the X coordinate in the input data (Default: 0).",
63 false, 0,
"column index",
66 TCLAP::ValueArg<int> argIndexI{
69 "Column index for the Intensity channel in the input data (Default: "
76 TCLAP::ValueArg<int> argIndexR{
77 "",
"column-r",
"Column index for the Ring channel in the input data (Default: 4).",
78 false, 4,
"column index",
81 TCLAP::ValueArg<int> argIndexT{
84 "Column index for the Timestamp channel in the input data (Default: "
91 TCLAP::ValueArg<uint64_t>
argID{
92 "",
"id",
"Metric map numeric ID (Default: none).",
false, 0,
"[ID]",
cmd};
95 "",
"label",
"Metric map label string (Default: none).",
false,
"label",
"[label]",
cmd};
98 int main(
int argc,
char** argv)
105 if (!
cli.cmd.parse(argc, argv))
return 1;
107 const auto&
f =
cli.argInput.getValue();
108 ASSERT_FILE_EXISTS_(
f);
110 std::cout <<
"Reading data from '" <<
f <<
"'..." << std::endl;
112 mrpt::math::CMatrixFloat data;
113 data.loadFromTextFile(
f);
115 const size_t nRows = data.size().at(0), nCols = data.size().at(1);
117 std::cout <<
"Done: " << nRows <<
" rows, " << nCols <<
" columns." << std::endl;
119 mrpt::maps::CPointsMap::Ptr pc;
120 const auto format =
cli.argFormat.getValue();
122 const auto idxX =
cli.argIndexXYZ.getValue();
123 const auto idxI =
cli.argIndexI.getValue();
124 const auto idxR =
cli.argIndexR.getValue();
125 const auto idxT =
cli.argIndexT.getValue();
129 ASSERT_GE_(nCols, 3U);
130 pc = mrpt::maps::CSimplePointsMap::Create();
133 std::cout <<
"Warning: Only the first 3 columns from the file "
134 "will be used for the output format 'xyz'"
137 for (
size_t i = 0; i < nRows; i++)
138 pc->insertPointFast(data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
140 else if (format ==
"xyzi")
142 ASSERT_GE_(nCols, 4U);
143 auto pts = mrpt::maps::CPointsMapXYZI::Create();
146 std::cout <<
"Warning: Only the first 4 columns from the file "
147 "will be used for the output format 'xyzi'"
150 for (
size_t i = 0; i < nRows; i++)
152 pts->insertPointFast(data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
153 pts->insertPointField_Intensity(data(i, idxI));
158 else if (format ==
"xyzirt")
160 ASSERT_GE_(nCols, 6U);
161 auto pts = mrpt::maps::CPointsMapXYZI::Create();
164 std::cout <<
"Warning: Only the first 6 columns from the file "
165 "will be used for the output format 'xyzirt'"
168 for (
size_t i = 0; i < nRows; i++)
170 pts->insertPointFast(data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
171 pts->insertPointField_Intensity(data(i, idxI));
172 pts->insertPointField_Ring(data(i, idxR));
173 pts->insertPointField_Timestamp(data(i, idxT));
178 else if (format ==
"xyzrgb")
180 ASSERT_GE_(nCols, 6U);
181 auto pts = mrpt::maps::CColouredPointsMap::Create();
184 std::cout <<
"Warning: Only the first 6 columns from the file "
185 "will be used for the output format 'xyzrgb'"
188 const size_t idxRed = 3, idxGreen = 4, idxBlue = 5;
190 for (
size_t i = 0; i < nRows; i++)
192 pts->insertPointFast(data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
193 pts->insertPointField_color_R(mrpt::u8tof(data(i, idxRed)));
194 pts->insertPointField_color_G(mrpt::u8tof(data(i, idxGreen)));
195 pts->insertPointField_color_B(mrpt::u8tof(data(i, idxBlue)));
203 "Invalid --format set to '%s'. Valid values: %s", format.c_str(),
VALID_FORMATS);
208 mm.
layers[
"raw"] = std::move(pc);
210 if (
cli.argID.isSet()) mm.
id =
cli.argID.getValue();
211 if (
cli.argLabel.isSet()) mm.
label =
cli.argLabel.getValue();
214 std::cout <<
"Saving map to: " <<
cli.argOutput.getValue() << std::endl;
218 "Error writing to target file '%s'",
cli.argOutput.getValue().c_str());
220 catch (
const std::exception& e)
222 std::cerr << mrpt::exception_to_str(e);