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  this->insertPoint(
227  cloud.points[i].x, cloud.points[i].y, cloud.points[i].z,
228  cloud.points[i].intensity);
229  }
230 
231  /** Like CPointsMap::getPCLPointCloud() but for PointCloud<PointXYZI> */
232  template <class POINTCLOUD>
233  void getPCLPointCloudXYZI(POINTCLOUD& cloud) const
234  {
235  const size_t nThis = this->size();
236  this->getPCLPointCloud(cloud); // 1st: xyz data
237  // 2nd: I data
238  for (size_t i = 0; i < nThis; ++i)
239  cloud.points[i].intensity = m_intensity[i];
240  }
241  /** @} */
242 
243  protected:
244  /** The intensity/reflectance data */
246 
247  /** Clear the map, erasing all the points */
248  void internal_clear() override;
249 
250  /** @name Redefinition of PLY Import virtual methods from CPointsMap
251  @{ */
253  const size_t idx, const mrpt::math::TPoint3Df& pt,
254  const mrpt::img::TColorf* pt_color = nullptr) override;
255 
256  void PLY_import_set_vertex_count(const size_t N) override;
257  /** @} */
258 
259  /** @name Redefinition of PLY Export virtual methods from CPointsMap
260  @{ */
262  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
263  mrpt::img::TColorf& pt_color) const override;
264  /** @} */
265 
267  mrpt::maps::CPointsMap::TInsertionOptions insertionOpts;
268  mrpt::maps::CPointsMap::TLikelihoodOptions likelihoodOpts;
270 
271 }; // End of class def.
272 
273 } // namespace maps
274 
275 #include <mrpt/opengl/pointcloud_adapters.h>
276 namespace opengl
277 {
278 /** Specialization
279  * mrpt::opengl::PointCloudAdapter<mrpt::maps::CPointsMapXYZI> \ingroup
280  * mrpt_adapters_grp */
281 template <>
283 {
284  private:
286 
287  public:
288  /** The type of each point XYZ coordinates */
289  using coords_t = float;
290  /** Has any color RGB info? */
291  static constexpr bool HAS_RGB = true;
292  /** Has native RGB info (as floats)? */
293  static constexpr bool HAS_RGBf = true;
294  /** Has native RGB info (as uint8_t)? */
295  static constexpr bool HAS_RGBu8 = false;
296 
297  /** Constructor (accept a const ref for convenience) */
299  : m_obj(*const_cast<mrpt::maps::CPointsMapXYZI*>(&obj))
300  {
301  }
302  /** Get number of points */
303  inline size_t size() const { return m_obj.size(); }
304  /** Set number of points (to uninitialized values) */
305  inline void resize(const size_t N) { m_obj.resize(N); }
306  /** Does nothing as of now */
307  inline void setDimensions(size_t height, size_t width) {}
308  /** Get XYZ coordinates of i'th point */
309  template <typename T>
310  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
311  {
312  m_obj.getPointFast(idx, x, y, z);
313  }
314  /** Set XYZ coordinates of i'th point */
315  inline void setPointXYZ(
316  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
317  {
318  m_obj.setPointFast(idx, x, y, z);
319  }
320 
321  /** Get XYZ_RGBf coordinates of i'th point */
322  template <typename T>
323  inline void getPointXYZ_RGBAf(
324  const size_t idx, T& x, T& y, T& z, float& r, float& g, float& b,
325  float& a) const
326  {
327  m_obj.getPointRGB(idx, x, y, z, r, g, b);
328  a = 1.0f;
329  }
330  /** Set XYZ_RGBf coordinates of i'th point */
331  inline void setPointXYZ_RGBAf(
332  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
333  const float r, const float g, const float b,
334  [[maybe_unused]] const float a)
335  {
336  m_obj.setPointRGB(idx, x, y, z, r, g, b);
337  }
338 
339  /** Get XYZ_RGBu8 coordinates of i'th point */
340  template <typename T>
341  inline void getPointXYZ_RGBu8(
342  const size_t idx, T& x, T& y, T& z, uint8_t& r, uint8_t& g,
343  uint8_t& b) const
344  {
345  float I, Gignrd, Bignrd;
346  m_obj.getPoint(idx, x, y, z, I, Gignrd, Bignrd);
347  r = g = b = I * 255;
348  }
349  /** Set XYZ_RGBu8 coordinates of i'th point */
350  inline void setPointXYZ_RGBu8(
351  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
352  const uint8_t r, const uint8_t g, const uint8_t b)
353  {
354  m_obj.setPointRGB(idx, x, y, z, r / 255.f, g / 255.f, b / 255.f);
355  }
356 
357  /** Get RGBf color of i'th point */
358  inline void getPointRGBf(
359  const size_t idx, float& r, float& g, float& b) const
360  {
361  r = g = b = m_obj.getPointIntensity_fast(idx);
362  }
363  /** Set XYZ_RGBf coordinates of i'th point */
364  inline void setPointRGBf(
365  const size_t idx, const float r, const float g, const float b)
366  {
367  m_obj.setPointColor_fast(idx, r, g, b);
368  }
369 
370  /** Get RGBu8 color of i'th point */
371  inline void getPointRGBu8(
372  const size_t idx, uint8_t& r, uint8_t& g, uint8_t& b) const
373  {
374  float i = m_obj.getPointIntensity_fast(idx);
375  r = g = b = i * 255;
376  }
377  /** Set RGBu8 coordinates of i'th point */
378  inline void setPointRGBu8(
379  const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
380  {
381  m_obj.setPointColor_fast(idx, r / 255.f, g / 255.f, b / 255.f);
382  }
383 
384 }; // end of PointCloudAdapter<mrpt::maps::CPointsMapXYZI>
385 } // namespace opengl
386 } // 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 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 insertPoint(float x, float y, float z=0)
Provides a way to insert (append) individual points into the map: the missing fields of child classes...
Definition: CPointsMap.h:658
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 setDimensions(size_t height, size_t width)
Does nothing as of now.
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...
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: 7b5ddf9de Fri May 29 14:02:56 2020 +0200 at vie may 29 14:15:09 CEST 2020