apps/txt2mm/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/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>
23 
24 const char* VALID_FORMATS = "(xyz|xyzi|xyzirt|xyzrgb)";
25 
26 using namespace std::string_literals;
27 
28 // CLI flags:
29 struct Cli
30 {
31  TCLAP::CmdLine cmd{"txt2mm"};
32 
33  TCLAP::ValueArg<std::string> argInput{
34  "i",
35  "input",
36  "Path to input TXT or CSV file. One point per row. Columns separated "
37  "by "
38  "spaces or commas. See docs for supported formats.",
39  true,
40  "input.txt",
41  "input.txt",
42  cmd};
43 
44  TCLAP::ValueArg<std::string> argOutput{
45  "o", "output", "Output file to write to.", true, "out.mm",
46  "out.mm", cmd};
47 
48  TCLAP::ValueArg<std::string> argFormat{
49  "f",
50  "format",
51  "Point cloud format. Mandatory flag.\n"s
52  "Options: "s +
54  true,
55  "xyz",
57  cmd};
58 
59  TCLAP::ValueArg<std::string> argLayer{
60  "l", "layer", "Target layer name (Default: \"raw\").", false, "raw",
61  "raw", cmd};
62 
63  TCLAP::ValueArg<int> argIndexXYZ{
64  "",
65  "column-x",
66  "Column index for the X coordinate in the input data (Default: 0).",
67  false,
68  0,
69  "column index",
70  cmd};
71 
72  TCLAP::ValueArg<int> argIndexI{
73  "",
74  "column-i",
75  "Column index for the Intensity channel in the input data (Default: "
76  "3).",
77  false,
78  3,
79  "column index",
80  cmd};
81 
82  TCLAP::ValueArg<int> argIndexR{
83  "",
84  "column-r",
85  "Column index for the Ring channel in the input data (Default: 4).",
86  false,
87  4,
88  "column index",
89  cmd};
90 
91  TCLAP::ValueArg<int> argIndexT{
92  "",
93  "column-t",
94  "Column index for the Timestamp channel in the input data (Default: "
95  "5).",
96  false,
97  5,
98  "column index",
99  cmd};
100 
101  TCLAP::ValueArg<uint64_t> argID{
102  "", "id", "Metric map numeric ID (Default: none).", false, 0,
103  "[ID]", cmd};
104 
105  TCLAP::ValueArg<std::string> argLabel{
106  "", "label", "Metric map label string (Default: none).",
107  false, "label", "[label]",
108  cmd};
109 };
110 
111 int main(int argc, char** argv)
112 {
113  try
114  {
115  Cli cli;
116 
117  // Parse arguments:
118  if (!cli.cmd.parse(argc, argv)) return 1; // should exit.
119 
120  const auto& f = cli.argInput.getValue();
121  ASSERT_FILE_EXISTS_(f);
122 
123  std::cout << "Reading data from '" << f << "'..." << std::endl;
124 
125  mrpt::math::CMatrixFloat data;
126  data.loadFromTextFile(f);
127 
128  const size_t nRows = data.size().at(0), nCols = data.size().at(1);
129 
130  std::cout << "Done: " << nRows << " rows, " << nCols << " columns."
131  << std::endl;
132 
133  mrpt::maps::CPointsMap::Ptr pc;
134  const auto format = cli.argFormat.getValue();
135 
136  const auto idxX = cli.argIndexXYZ.getValue();
137  const auto idxI = cli.argIndexI.getValue();
138  const auto idxR = cli.argIndexR.getValue();
139  const auto idxT = cli.argIndexT.getValue();
140 
141  if (format == "xyz")
142  {
143  ASSERT_GE_(nCols, 3U);
144  pc = mrpt::maps::CSimplePointsMap::Create();
145  pc->reserve(nRows);
146  if (nCols > 3)
147  std::cout << "Warning: Only the first 3 columns from the file "
148  "will be used for the output format 'xyz'"
149  << std::endl;
150 
151  for (size_t i = 0; i < nRows; i++)
152  pc->insertPointFast(
153  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
154  }
155  else if (format == "xyzi")
156  {
157  ASSERT_GE_(nCols, 4U);
158  auto pts = mrpt::maps::CPointsMapXYZI::Create();
159  pts->reserve(nRows);
160  if (nCols > 4)
161  std::cout << "Warning: Only the first 4 columns from the file "
162  "will be used for the output format 'xyzi'"
163  << std::endl;
164 
165  for (size_t i = 0; i < nRows; i++)
166  {
167  pts->insertPointFast(
168  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
169  pts->insertPointField_Intensity(data(i, idxI));
170  }
171 
172  pc = pts;
173  }
174  else if (format == "xyzirt")
175  {
176  ASSERT_GE_(nCols, 6U);
177  auto pts = mrpt::maps::CPointsMapXYZI::Create();
178  pts->reserve(nRows);
179  if (nCols > 6)
180  std::cout << "Warning: Only the first 6 columns from the file "
181  "will be used for the output format 'xyzirt'"
182  << std::endl;
183 
184  for (size_t i = 0; i < nRows; i++)
185  {
186  pts->insertPointFast(
187  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
188  pts->insertPointField_Intensity(data(i, idxI));
189  pts->insertPointField_Ring(data(i, idxR));
190  pts->insertPointField_Timestamp(data(i, idxT));
191  }
192 
193  pc = pts;
194  }
195  else if (format == "xyzrgb")
196  {
197  ASSERT_GE_(nCols, 6U);
198  auto pts = mrpt::maps::CColouredPointsMap::Create();
199  pts->reserve(nRows);
200  if (nCols > 6)
201  std::cout << "Warning: Only the first 6 columns from the file "
202  "will be used for the output format 'xyzrgb'"
203  << std::endl;
204 
205  const size_t idxRed = 3, idxGreen = 4, idxBlue = 5;
206 
207  for (size_t i = 0; i < nRows; i++)
208  {
209  pts->insertPointFast(
210  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
211  pts->insertPointField_color_R(mrpt::u8tof(data(i, idxRed)));
212  pts->insertPointField_color_G(mrpt::u8tof(data(i, idxGreen)));
213  pts->insertPointField_color_B(mrpt::u8tof(data(i, idxBlue)));
214  }
215 
216  pc = pts;
217  }
218  else
219  {
220  THROW_EXCEPTION_FMT(
221  "Invalid --format set to '%s'. Valid values: %s",
222  format.c_str(), VALID_FORMATS);
223  }
224 
225  // Save as mm file:
227  mm.layers["raw"] = std::move(pc);
228 
229  if (cli.argID.isSet()) mm.id = cli.argID.getValue();
230  if (cli.argLabel.isSet()) mm.label = cli.argLabel.getValue();
231 
232  std::cout << "Map contents: " << mm.contents_summary() << std::endl;
233  std::cout << "Saving map to: " << cli.argOutput.getValue() << std::endl;
234 
235  if (!mm.save_to_file(cli.argOutput.getValue()))
236  THROW_EXCEPTION_FMT(
237  "Error writing to target file '%s'",
238  cli.argOutput.getValue().c_str());
239  }
240  catch (const std::exception& e)
241  {
242  std::cerr << mrpt::exception_to_str(e);
243  return 1;
244  }
245  return 0;
246 }
cli
std::unique_ptr< cli_flags > cli
Definition: sm-cli-main.cpp:29
argLayer
static TCLAP::ValueArg< std::string > argLayer("l", "layer", "Target layer name (Default: \"raw\").", false, "raw", "raw", cmd)
s
XmlRpcServer s
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:567
kitti-run-seq.cmd
string cmd
Definition: kitti-run-seq.py:14
argInput
static TCLAP::ValueArg< std::string > argInput("i", "input", "KITTI .bin pointcloud file.", true, "kitti-00.bin", "kitti-00.bin", cmd)
mp2p_icp::metric_map_t::label
std::optional< std::string > label
Definition: metricmap.h:97
Cli
Definition: apps/mm-filter/main.cpp:20
main
int main(int argc, char **argv)
Definition: apps/txt2mm/main.cpp:111
mp2p_icp::metric_map_t::contents_summary
virtual std::string contents_summary() const
Definition: metricmap.cpp:500
argOutput
static TCLAP::ValueArg< std::string > argOutput("o", "output", "Output file to write to.", true, "out.mm", "out.mm", cmd)
argID
static TCLAP::ValueArg< uint64_t > argID("", "id", "Metric map numeric ID (Default: none).", false, 0, "[ID]", cmd)
VALID_FORMATS
const char * VALID_FORMATS
Definition: apps/txt2mm/main.cpp:24
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::id
std::optional< uint64_t > id
Definition: metricmap.h:89
argLabel
static TCLAP::ValueArg< std::string > argLabel("", "label", "Metric map label string (Default: none).", false, "label", "[label]", cmd)
mp2p_icp::metric_map_t::layers
std::map< layer_name_t, mrpt::maps::CMetricMap::Ptr > layers
Definition: metricmap.h:76


mp2p_icp
Author(s):
autogenerated on Thu Dec 26 2024 03:48:12