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/CPointsMapXYZI.h>
17 #include <mrpt/maps/CPointsMapXYZIRT.h>
18 #include <mrpt/maps/CSimplePointsMap.h>
19 #include <mrpt/math/CMatrixDynamic.h>
20 #include <mrpt/obs/CObservationPointCloud.h>
21 #include <mrpt/system/filesystem.h>
22 
23 // CLI flags:
24 static TCLAP::CmdLine cmd("txt2mm");
25 
26 static TCLAP::ValueArg<std::string> argInput(
27  "i", "input",
28  "Path to input TXT or CSV file. One point per row. Columns separated by "
29  "spaces or commas. See docs for supported formats.",
30  true, "input.txt", "input.txt", cmd);
31 
32 static TCLAP::ValueArg<std::string> argOutput(
33  "o", "output", "Output file to write to.", true, "out.mm", "out.mm", cmd);
34 
35 static TCLAP::ValueArg<std::string> argFormat(
36  "f", "format",
37  "Point cloud format. Mandatory flag.\n"
38  "Options: (xyz|xyzi|xyzirt)",
39  true, "xyz", "(xyz|xyzi|xyzirt)", cmd);
40 
41 static TCLAP::ValueArg<std::string> argLayer(
42  "l", "layer", "Target layer name (Default: \"raw\").", false, "raw", "raw",
43  cmd);
44 
45 static TCLAP::ValueArg<int> argIndexXYZ(
46  "", "column-x",
47  "Column index for the X coordinate in the input data (Default: 0).", false,
48  0, "column index", cmd);
49 
50 static TCLAP::ValueArg<int> argIndexI(
51  "", "column-i",
52  "Column index for the Intensity channel in the input data (Default: 3).",
53  false, 3, "column index", cmd);
54 
55 static TCLAP::ValueArg<int> argIndexR(
56  "", "column-r",
57  "Column index for the Ring channel in the input data (Default: 4).", false,
58  4, "column index", cmd);
59 
60 static TCLAP::ValueArg<int> argIndexT(
61  "", "column-t",
62  "Column index for the Timestamp channel in the input data (Default: 5).",
63  false, 5, "column index", cmd);
64 
65 static TCLAP::ValueArg<uint64_t> argID(
66  "", "id", "Metric map numeric ID (Default: none).", false, 0, "[ID]", cmd);
67 
68 static TCLAP::ValueArg<std::string> argLabel(
69  "", "label", "Metric map label string (Default: none).", false, "label",
70  "[label]", cmd);
71 
72 int main(int argc, char** argv)
73 {
74  try
75  {
76  // Parse arguments:
77  if (!cmd.parse(argc, argv)) return 1; // should exit.
78 
79  const auto& f = argInput.getValue();
80  ASSERT_FILE_EXISTS_(f);
81 
82  std::cout << "Reading data from '" << f << "'..." << std::endl;
83 
84  mrpt::math::CMatrixFloat data;
85  data.loadFromTextFile(f);
86 
87  const size_t nRows = data.size().at(0), nCols = data.size().at(1);
88 
89  std::cout << "Done: " << nRows << " rows, " << nCols << " columns."
90  << std::endl;
91 
92  mrpt::maps::CPointsMap::Ptr pc;
93  const auto format = argFormat.getValue();
94 
95  const auto idxX = argIndexXYZ.getValue();
96  const auto idxI = argIndexI.getValue();
97  const auto idxR = argIndexR.getValue();
98  const auto idxT = argIndexT.getValue();
99 
100  if (format == "xyz")
101  {
102  ASSERT_GE_(nCols, 3U);
103  pc = mrpt::maps::CSimplePointsMap::Create();
104  pc->reserve(nRows);
105  if (nCols > 3)
106  std::cout << "Warning: Only the first 3 columns from the file "
107  "will be used for the output format 'xyz'"
108  << std::endl;
109 
110  for (size_t i = 0; i < nRows; i++)
111  pc->insertPointFast(
112  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
113  }
114  else if (format == "xyzi")
115  {
116  ASSERT_GE_(nCols, 4U);
117  auto pts = mrpt::maps::CPointsMapXYZI::Create();
118  pts->reserve(nRows);
119  if (nCols > 4)
120  std::cout << "Warning: Only the first 4 columns from the file "
121  "will be used for the output format 'xyzi'"
122  << std::endl;
123 
124  for (size_t i = 0; i < nRows; i++)
125  {
126  pts->insertPointFast(
127  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
128  pts->insertPointField_Intensity(data(i, idxI));
129  }
130 
131  pc = pts;
132  }
133  else if (format == "xyzirt")
134  {
135  ASSERT_GE_(nCols, 6U);
136  auto pts = mrpt::maps::CPointsMapXYZI::Create();
137  pts->reserve(nRows);
138  if (nCols > 6)
139  std::cout << "Warning: Only the first 6 columns from the file "
140  "will be used for the output format 'xyzirt'"
141  << std::endl;
142 
143  for (size_t i = 0; i < nRows; i++)
144  {
145  pts->insertPointFast(
146  data(i, idxX + 0), data(i, idxX + 1), data(i, idxX + 2));
147  pts->insertPointField_Intensity(data(i, idxI));
148  pts->insertPointField_Ring(data(i, idxR));
149  pts->insertPointField_Timestamp(data(i, idxT));
150  }
151 
152  pc = pts;
153  }
154  else
155  {
156  THROW_EXCEPTION_FMT(
157  "Invalid --format set to '%s'. Valid values: (xyz|xyzi|xyzirt)",
158  format.c_str());
159  }
160 
161  // Save as mm file:
163  mm.layers["raw"] = std::move(pc);
164 
165  if (argID.isSet()) mm.id = argID.getValue();
166  if (argLabel.isSet()) mm.label = argLabel.getValue();
167 
168  std::cout << "Map contents: " << mm.contents_summary() << std::endl;
169  std::cout << "Saving map to: " << argOutput.getValue() << std::endl;
170 
171  if (!mm.save_to_file(argOutput.getValue()))
172  THROW_EXCEPTION_FMT(
173  "Error writing to target file '%s'",
174  argOutput.getValue().c_str());
175  }
176  catch (const std::exception& e)
177  {
178  std::cerr << mrpt::exception_to_str(e);
179  return 1;
180  }
181  return 0;
182 }
argIndexI
static TCLAP::ValueArg< int > argIndexI("", "column-i", "Column index for the Intensity channel in the input data (Default: 3).", false, 3, "column index", cmd)
kitti-run-seq.f
string f
Definition: kitti-run-seq.py:12
argFormat
static TCLAP::ValueArg< std::string > argFormat("f", "format", "Point cloud format. Mandatory flag.\n" "Options: (xyz|xyzi|xyzirt)", true, "xyz", "(xyz|xyzi|xyzirt)", cmd)
argIndexXYZ
static TCLAP::ValueArg< int > argIndexXYZ("", "column-x", "Column index for the X coordinate in the input data (Default: 0).", false, 0, "column index", cmd)
argID
static TCLAP::ValueArg< uint64_t > argID("", "id", "Metric map numeric ID (Default: none).", false, 0, "[ID]", cmd)
argIndexT
static TCLAP::ValueArg< int > argIndexT("", "column-t", "Column index for the Timestamp channel in the input data (Default: 5).", false, 5, "column index", cmd)
argInput
static TCLAP::ValueArg< std::string > argInput("i", "input", "Path to input TXT or CSV file. One point per row. Columns separated by " "spaces or commas. See docs for supported formats.", true, "input.txt", "input.txt", cmd)
mp2p_icp::metric_map_t::save_to_file
bool save_to_file(const std::string &fileName) const
Definition: metricmap.cpp:571
cmd
static TCLAP::CmdLine cmd("txt2mm")
mp2p_icp::metric_map_t::label
std::optional< std::string > label
Definition: metricmap.h:96
argIndexR
static TCLAP::ValueArg< int > argIndexR("", "column-r", "Column index for the Ring channel in the input data (Default: 4).", false, 4, "column index", cmd)
main
int main(int argc, char **argv)
Definition: apps/txt2mm/main.cpp:72
mp2p_icp::metric_map_t::contents_summary
virtual std::string contents_summary() const
Definition: metricmap.cpp:504
argLabel
static TCLAP::ValueArg< std::string > argLabel("", "label", "Metric map label string (Default: none).", false, "label", "[label]", cmd)
argLayer
static TCLAP::ValueArg< std::string > argLayer("l", "layer", "Target layer name (Default: \"raw\").", false, "raw", "raw", cmd)
argOutput
static TCLAP::ValueArg< std::string > argOutput("o", "output", "Output file to write to.", true, "out.mm", "out.mm", cmd)
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:48
mp2p_icp::metric_map_t::id
std::optional< uint64_t > id
Definition: metricmap.h:88
mp2p_icp::metric_map_t::layers
std::map< layer_name_t, mrpt::maps::CMetricMap::Ptr > layers
Definition: metricmap.h:75


mp2p_icp
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Tue Jul 2 2024 02:47:25