MRPT  2.0.4
CPointsMapXYZI.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/maps/CPointsMap.h>
12 #include <mrpt/math/CMatrixF.h>
14 #include <mrpt/obs/obs_frwds.h>
17 
18 namespace mrpt
19 {
20 namespace maps
21 {
22 /** A map of 3D points with reflectance/intensity (float).
23  * \sa mrpt::maps::CPointsMap, mrpt::maps::CMetricMap
24  * \ingroup mrpt_maps_grp
25  */
26 class CPointsMapXYZI : public CPointsMap
27 {
29 
30  public:
31  CPointsMapXYZI() = default;
32 
36  {
37  impl_copyFrom(o);
38  return *this;
39  }
41  {
42  impl_copyFrom(o);
43  return *this;
44  }
45 
46  /** @name Pure virtual interfaces to be implemented by any class derived
47  from CPointsMap
48  @{ */
49 
50  void reserve(size_t newLength) override; // See base class docs
51  void resize(size_t newLength) override; // See base class docs
52  void setSize(size_t newLength) override; // See base class docs
53 
54  /** The virtual method for \a insertPoint() *without* calling
55  * mark_as_modified() */
56  void insertPointFast(float x, float y, float z = 0) override;
57 
58  /** Get all the data fields for one point as a vector: [X Y Z I]
59  * Unlike getPointAllFields(), this method does not check for index out of
60  * bounds
61  * \sa getPointAllFields, setPointAllFields, setPointAllFieldsFast
62  */
64  const size_t index, std::vector<float>& point_data) const override
65  {
66  point_data.resize(4);
67  point_data[0] = m_x[index];
68  point_data[1] = m_y[index];
69  point_data[2] = m_z[index];
70  point_data[3] = m_intensity[index];
71  }
72 
73  /** Set all the data fields for one point as a vector: [X Y Z I]
74  * Unlike setPointAllFields(), this method does not check for index out of
75  * bounds
76  * \sa setPointAllFields, getPointAllFields, getPointAllFieldsFast
77  */
79  const size_t index, const std::vector<float>& point_data) override
80  {
81  ASSERT_(point_data.size() == 4);
82  m_x[index] = point_data[0];
83  m_y[index] = point_data[1];
84  m_z[index] = point_data[2];
85  m_intensity[index] = point_data[3];
86  }
87 
88  /** Loads from a Kitti dataset Velodyne scan binary file.
89  * The file can be gz compressed (only enabled if the filename ends in ".gz"
90  * to prevent spurious false autodetection of gzip files).
91  * \return true on success */
92  bool loadFromKittiVelodyneFile(const std::string& filename);
93 
94  bool saveToKittiVelodyneFile(const std::string& filename) const;
95 
96  /** See CPointsMap::loadFromRangeScan() */
97  void loadFromRangeScan(
98  const mrpt::obs::CObservation2DRangeScan& rangeScan,
99  const mrpt::poses::CPose3D* robotPose = nullptr) override;
100  /** See CPointsMap::loadFromRangeScan() */
101  void loadFromRangeScan(
102  const mrpt::obs::CObservation3DRangeScan& rangeScan,
103  const mrpt::poses::CPose3D* robotPose = nullptr) override;
104 
105  protected:
106  // See base class
107  void impl_copyFrom(const CPointsMap& obj) override;
108  // See base class
110  const CPointsMap& anotherMap, const size_t nPreviousPoints) override;
111 
112  // Friend methods:
113  template <class Derived>
115  template <class Derived>
116  friend struct detail::pointmap_traits;
117 
118  public:
119  /** @} */
120 
121  /** Save to a text file. In each line contains X Y Z (meters) I (intensity)
122  * Returns false if any error occured, true elsewere.
123  */
124  bool saveXYZI_to_text_file(const std::string& file) const;
125 
126  /** Loads from a text file, each line having "X Y Z I", I in [0,1].
127  * Returns false if any error occured, true elsewere. */
128  bool loadXYZI_from_text_file(const std::string& file);
129 
130  /** Changes a given point from map. First index is 0.
131  * \exception Throws std::exception on index out of bound.
132  */
133  void setPointRGB(
134  size_t index, float x, float y, float z, float R_intensity,
135  float G_ignored, float B_ignored) override;
136 
137  /** Adds a new point given its coordinates and color (colors range is [0,1])
138  */
139  void insertPointRGB(
140  float x, float y, float z, float R_intensity, float G_ignored,
141  float B_ignored) override;
142 
143  /** Changes the intensity of a given point from the map. First index is 0.
144  * \exception Throws std::exception on index out of bound.
145  */
146  void setPointIntensity(size_t index, float intensity);
147 
148  /** Like \c setPointColor but without checking for out-of-index erors */
149  inline void setPointColor_fast(size_t index, float R, float G, float B)
150  {
151  m_intensity[index] = R;
152  }
153 
154  /** Retrieves a point and its color (colors range is [0,1])
155  */
156  void getPointRGB(
157  size_t index, float& x, float& y, float& z, float& R_intensity,
158  float& G_intensity, float& B_intensity) const override;
159 
160  /** Retrieves a point intensity (range [0,1]) */
161  float getPointIntensity(size_t index) const;
162 
163  /** Like \c getPointColor but without checking for out-of-index erors */
164  inline float getPointIntensity_fast(size_t index) const
165  {
166  return m_intensity[index];
167  }
168 
169  /** Returns true if the point map has a color field for each point */
170  bool hasColorPoints() const override { return true; }
171 
172  /** Override of the default 3D scene builder to account for the individual
173  * points' color.
174  */
175  void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr& outObj) const override;
176 
177  /** @name PCL library support
178  @{ */
179 
180  /** Save the point cloud as a PCL PCD file, in either ASCII or binary format
181  * \note This method requires user code to include PCL before MRPT headers.
182  * \return false on any error */
183 #if defined(PCL_LINEAR_VERSION)
184  inline bool savePCDFile(
185  const std::string& filename, bool save_as_binary) const
186  {
187  pcl::PointCloud<pcl::PointXYZI> cloud;
188 
189  const size_t nThis = this->size();
190 
191  // Fill in the cloud data
192  cloud.width = nThis;
193  cloud.height = 1;
194  cloud.is_dense = false;
195  cloud.points.resize(cloud.width * cloud.height);
196 
197  for (size_t i = 0; i < nThis; ++i)
198  {
199  cloud.points[i].x = m_x[i];
200  cloud.points[i].y = m_y[i];
201  cloud.points[i].z = m_z[i];
202  cloud.points[i].intensity = this->m_intensity[i];
203  }
204 
205  return 0 == pcl::io::savePCDFile(filename, cloud, save_as_binary);
206  }
207 #endif
208 
209  /** Loads a PCL point cloud (WITH XYZI information) into this MRPT class.
210  * Usage example:
211  * \code
212  * pcl::PointCloud<pcl::PointXYZI> cloud;
213  * mrpt::maps::CPointsMapXYZI pc;
214  *
215  * pc.setFromPCLPointCloudXYZI(cloud);
216  * \endcode
217  * \sa CPointsMap::setFromPCLPointCloud()
218  */
219  template <class POINTCLOUD>
220  void setFromPCLPointCloudXYZI(const POINTCLOUD& cloud)
221  {
222  const size_t N = cloud.points.size();
223  clear();
224  reserve(N);
225  for (size_t i = 0; i < N; ++i)
226  {
227  const auto& pt = cloud.points[i];
228  m_x.push_back(pt.x);
229  m_y.push_back(pt.y);
230  m_z.push_back(pt.z);
231  m_intensity.push_back(pt.intensity);
232  }
234  }
235 
236  /** Like CPointsMap::getPCLPointCloud() but for PointCloud<PointXYZI> */
237  template <class POINTCLOUD>
238  void getPCLPointCloudXYZI(POINTCLOUD& cloud) const
239  {
240  const size_t nThis = this->size();
241  this->getPCLPointCloud(cloud); // 1st: xyz data
242  // 2nd: I data
243  for (size_t i = 0; i < nThis; ++i)
244  cloud.points[i].intensity = m_intensity[i];
245  }
246  /** @} */
247 
248  protected:
249  /** The intensity/reflectance data */
251 
252  /** Clear the map, erasing all the points */
253  void internal_clear() override;
254 
255  /** @name Redefinition of PLY Import virtual methods from CPointsMap
256  @{ */
258  const size_t idx, const mrpt::math::TPoint3Df& pt,
259  const mrpt::img::TColorf* pt_color = nullptr) override;
260 
261  void PLY_import_set_vertex_count(const size_t N) override;
262  /** @} */
263 
264  /** @name Redefinition of PLY Export virtual methods from CPointsMap
265  @{ */
267  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
268  mrpt::img::TColorf& pt_color) const override;
269  /** @} */
270 
272  mrpt::maps::CPointsMap::TInsertionOptions insertionOpts;
273  mrpt::maps::CPointsMap::TLikelihoodOptions likelihoodOpts;
275 
276 }; // End of class def.
277 
278 } // namespace maps
279 
280 #include <mrpt/opengl/pointcloud_adapters.h>
281 namespace opengl
282 {
283 /** Specialization
284  * mrpt::opengl::PointCloudAdapter<mrpt::maps::CPointsMapXYZI> \ingroup
285  * mrpt_adapters_grp */
286 template <>
288 {
289  private:
291 
292  public:
293  /** The type of each point XYZ coordinates */
294  using coords_t = float;
295  /** Has any color RGB info? */
296  static constexpr bool HAS_RGB = true;
297  /** Has native RGB info (as floats)? */
298  static constexpr bool HAS_RGBf = true;
299  /** Has native RGB info (as uint8_t)? */
300  static constexpr bool HAS_RGBu8 = false;
301 
302  /** Constructor (accept a const ref for convenience) */
304  : m_obj(*const_cast<mrpt::maps::CPointsMapXYZI*>(&obj))
305  {
306  }
307  /** Get number of points */
308  inline size_t size() const { return m_obj.size(); }
309  /** Set number of points (to uninitialized values) */
310  inline void resize(const size_t N) { m_obj.resize(N); }
311  /** Does nothing as of now */
312  inline void setDimensions(size_t /*height*/, size_t /*width*/) {}
313  /** Get XYZ coordinates of i'th point */
314  template <typename T>
315  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
316  {
317  m_obj.getPointFast(idx, x, y, z);
318  }
319  /** Set XYZ coordinates of i'th point */
320  inline void setPointXYZ(
321  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
322  {
323  m_obj.setPointFast(idx, x, y, z);
324  }
325 
326  /** Get XYZ_RGBf coordinates of i'th point */
327  template <typename T>
328  inline void getPointXYZ_RGBAf(
329  const size_t idx, T& x, T& y, T& z, float& r, float& g, float& b,
330  float& a) const
331  {
332  m_obj.getPointRGB(idx, x, y, z, r, g, b);
333  a = 1.0f;
334  }
335  /** Set XYZ_RGBf coordinates of i'th point */
336  inline void setPointXYZ_RGBAf(
337  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
338  const float r, const float g, const float b,
339  [[maybe_unused]] const float a)
340  {
341  m_obj.setPointRGB(idx, x, y, z, r, g, b);
342  }
343 
344  /** Get XYZ_RGBu8 coordinates of i'th point */
345  template <typename T>
346  inline void getPointXYZ_RGBu8(
347  const size_t idx, T& x, T& y, T& z, uint8_t& r, uint8_t& g,
348  uint8_t& b) const
349  {
350  float I, Gignrd, Bignrd;
351  m_obj.getPoint(idx, x, y, z, I, Gignrd, Bignrd);
352  r = g = b = I * 255;
353  }
354  /** Set XYZ_RGBu8 coordinates of i'th point */
355  inline void setPointXYZ_RGBu8(
356  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
357  const uint8_t r, const uint8_t g, const uint8_t b)
358  {
359  m_obj.setPointRGB(idx, x, y, z, r / 255.f, g / 255.f, b / 255.f);
360  }
361 
362  /** Get RGBf color of i'th point */
363  inline void getPointRGBf(
364  const size_t idx, float& r, float& g, float& b) const
365  {
366  r = g = b = m_obj.getPointIntensity_fast(idx);
367  }
368  /** Set XYZ_RGBf coordinates of i'th point */
369  inline void setPointRGBf(
370  const size_t idx, const float r, const float g, const float b)
371  {
372  m_obj.setPointColor_fast(idx, r, g, b);
373  }
374 
375  /** Get RGBu8 color of i'th point */
376  inline void getPointRGBu8(
377  const size_t idx, uint8_t& r, uint8_t& g, uint8_t& b) const
378  {
379  float i = m_obj.getPointIntensity_fast(idx);
380  r = g = b = i * 255;
381  }
382  /** Set RGBu8 coordinates of i'th point */
383  inline void setPointRGBu8(
384  const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
385  {
386  m_obj.setPointColor_fast(idx, r / 255.f, g / 255.f, b / 255.f);
387  }
388 
389 }; // end of PointCloudAdapter<mrpt::maps::CPointsMapXYZI>
390 } // namespace opengl
391 } // namespace mrpt
void clear()
Erase all the contents of the map.
Definition: CMetricMap.cpp:30
void setPointRGB(size_t index, float x, float y, float z, float R_intensity, float G_ignored, float B_ignored) override
Changes a given point from map.
PointCloudAdapter(const mrpt::maps::CPointsMapXYZI &obj)
Constructor (accept a const ref for convenience)
void getPoint(size_t index, float &x, float &y, float &z) const
Access to a given point from map, as a 2D point.
Definition: CPointsMap.cpp:198
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
void getPointFast(size_t index, float &x, float &y, float &z) const
Just like getPoint() but without checking out-of-bound index and without returning the point weight...
Definition: CPointsMap.h:499
void PLY_export_get_vertex(const size_t idx, mrpt::math::TPoint3Df &pt, bool &pt_has_color, mrpt::img::TColorf &pt_color) const override
In a base class, will be called after PLY_export_get_vertex_count() once for each exported point...
void setSize(size_t newLength) override
Resizes all point buffers so they can hold the given number of points, erasing all previous contents ...
CPointsMap & operator=(const CPointsMap &o)
Definition: CPointsMap.h:122
bool loadXYZI_from_text_file(const std::string &file)
Loads from a text file, each line having "X Y Z I", I in [0,1].
void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::img::TColorf *pt_color=nullptr) override
In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point...
CPointsMapXYZI(const CPointsMap &o)
bool hasColorPoints() const override
Returns true if the point map has a color field for each point.
void setPointAllFieldsFast(const size_t index, const std::vector< float > &point_data) override
Set all the data fields for one point as a vector: [X Y Z I] Unlike setPointAllFields(), this method does not check for index out of bounds.
const double G
mrpt::aligned_std_vector< float > m_intensity
The intensity/reflectance data.
void setPointXYZ(const size_t idx, const coords_t x, const coords_t y, const coords_t z)
Set XYZ coordinates of i&#39;th point.
#define MAP_DEFINITION_START(_CLASS_NAME_)
Add a MAP_DEFINITION_START() ...
void setFromPCLPointCloudXYZI(const POINTCLOUD &cloud)
Save the point cloud as a PCL PCD file, in either ASCII or binary format.
void setDimensions(size_t, size_t)
Does nothing as of now.
void resize(const size_t N)
Set number of points (to uninitialized values)
A range or depth 3D scan measurement, as from a time-of-flight range camera or a structured-light dep...
void getPointXYZ_RGBAf(const size_t idx, T &x, T &y, T &z, float &r, float &g, float &b, float &a) const
Get XYZ_RGBf coordinates of i&#39;th point.
void getPCLPointCloudXYZI(POINTCLOUD &cloud) const
Like CPointsMap::getPCLPointCloud() but for PointCloud<PointXYZI>
void setPointIntensity(size_t index, float intensity)
Changes the intensity of a given point from the map.
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
void internal_clear() override
Clear the map, erasing all the points.
void setPointRGBf(const size_t idx, const float r, const float g, const float b)
Set XYZ_RGBf coordinates of i&#39;th point.
bool loadFromKittiVelodyneFile(const std::string &filename)
Loads from a Kitti dataset Velodyne scan binary file.
void addFrom_classSpecific(const CPointsMap &anotherMap, const size_t nPreviousPoints) override
Auxiliary method called from within addFrom() automatically, to finish the copying of class-specific ...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
With this struct options are provided to the observation insertion process.
Definition: CPointsMap.h:222
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans or other sensors...
Definition: CPointsMap.h:67
void setPointXYZ_RGBu8(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const uint8_t r, const uint8_t g, const uint8_t b)
Set XYZ_RGBu8 coordinates of i&#39;th point.
void impl_copyFrom(const CPointsMap &obj) override
Virtual assignment operator, copies as much common data (XYZ, color,...) as possible from the source ...
void PLY_import_set_vertex_count(const size_t N) override
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex.
An adapter to different kinds of point cloud object.
void resize(size_t newLength) override
Resizes all point buffers so they can hold the given number of points: newly created points are set t...
float getPointIntensity_fast(size_t index) const
Like getPointColor but without checking for out-of-index erors.
void getPointRGB(size_t index, float &x, float &y, float &z, float &R_intensity, float &G_intensity, float &B_intensity) const override
Retrieves a point and its color (colors range is [0,1])
void getPointXYZ_RGBu8(const size_t idx, T &x, T &y, T &z, uint8_t &r, uint8_t &g, uint8_t &b) const
Get XYZ_RGBu8 coordinates of i&#39;th point.
void getPCLPointCloud(POINTCLOUD &cloud) const
Use to convert this MRPT point cloud object into a PCL point cloud object (PointCloud<PointXYZ>).
Definition: CPointsMap.h:1022
void setPointFast(size_t index, float x, float y, float z)
Changes the coordinates of the given point (0-based index), without checking for out-of-bounds and wi...
Definition: CPointsMap.h:163
void loadFromRangeScan(const mrpt::obs::CObservation2DRangeScan &rangeScan, const mrpt::poses::CPose3D *robotPose=nullptr) override
See CPointsMap::loadFromRangeScan()
void setPointRGBu8(const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
Set RGBu8 coordinates of i&#39;th point.
void insertPointRGB(float x, float y, float z, float R_intensity, float G_ignored, float B_ignored) override
Adds a new point given its coordinates and color (colors range is [0,1])
A map of 3D points with reflectance/intensity (float).
CPointsMapXYZI & operator=(const CPointsMapXYZI &o)
mrpt::aligned_std_vector< float > m_z
Definition: CPointsMap.h:1135
float getPointIntensity(size_t index) const
Retrieves a point intensity (range [0,1])
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A "CObservation"-derived class that represents a 2D range scan measurement (typically from a laser sc...
void mark_as_modified() const
Users normally don&#39;t need to call this.
Definition: CPointsMap.h:1126
mrpt::aligned_std_vector< float > m_y
Definition: CPointsMap.h:1135
bool saveToKittiVelodyneFile(const std::string &filename) const
const float R
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Override of the default 3D scene builder to account for the individual points&#39; color.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
Options used when evaluating "computeObservationLikelihood" in the derived classes.
Definition: CPointsMap.h:280
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
void getPointRGBf(const size_t idx, float &r, float &g, float &b) const
Get RGBf color of i&#39;th point.
float coords_t
The type of each point XYZ coordinates.
CPointsMapXYZI & operator=(const CPointsMap &o)
void getPointRGBu8(const size_t idx, uint8_t &r, uint8_t &g, uint8_t &b) const
Get RGBu8 color of i&#39;th point.
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void getPointAllFieldsFast(const size_t index, std::vector< float > &point_data) const override
Get all the data fields for one point as a vector: [X Y Z I] Unlike getPointAllFields(), this method does not check for index out of bounds.
void insertPointFast(float x, float y, float z=0) override
The virtual method for insertPoint() without calling mark_as_modified()
CPointsMapXYZI(const CPointsMapXYZI &o)
mrpt::aligned_std_vector< float > m_x
The point coordinates.
Definition: CPointsMap.h:1135
#define MAP_DEFINITION_END(_CLASS_NAME_)
size_t size() const
Save the point cloud as a PCL PCD file, in either ASCII or binary format.
Definition: CPointsMap.h:459
void setPointColor_fast(size_t index, float R, float G, float B)
Like setPointColor but without checking for out-of-index erors.
bool saveXYZI_to_text_file(const std::string &file) const
Save to a text file.
void setPointXYZ_RGBAf(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const float r, const float g, const float b, [[maybe_unused]] const float a)
Set XYZ_RGBf coordinates of i&#39;th point.
void reserve(size_t newLength) override
Reserves memory for a given number of points: the size of the map does not change, it only reserves the memory.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020