MRPT  2.0.2
CPointCloud.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/math/TPoint3D.h>
16 
17 namespace mrpt::opengl
18 {
19 /** A cloud of points, all with the same color or each depending on its value
20  * along a particular coordinate axis.
21  * This class is just an OpenGL representation of a point cloud. For operating
22  * with maps of points, see mrpt::maps::CPointsMap and derived classes.
23  *
24  * To load from a points-map, CPointCloud::loadFromPointsMap().
25  *
26  * This class uses smart optimizations while rendering to efficiently draw
27  * clouds of millions of points,
28  * as described in this page:
29  * https://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
30  *
31  * \sa opengl::CPlanarLaserScan, opengl::COpenGLScene,
32  * opengl::CPointCloudColoured, mrpt::maps::CPointsMap
33  *
34  * <div align="center">
35  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
36  * border-style: solid;">
37  * <tr> <td> mrpt::opengl::CPointCloud </td> <td> \image html
38  * preview_CPointCloud.png </td> </tr>
39  * </table>
40  * </div>
41  *
42  * \ingroup mrpt_opengl_grp
43  */
45  public COctreePointRenderer<CPointCloud>,
48 {
51  protected:
52  enum Axis
53  {
54  colNone = 0,
58  };
59 
61 
62  /** Actually, an alias for the base class shader container of points. Kept
63  * to have an easy to use name. */
64  std::vector<mrpt::math::TPoint3Df>& m_points =
66 
67  /** Default: false */
68  bool m_pointSmooth = false;
69 
71 
72  /** Do needed internal work if all points are new (octree rebuilt,...) */
73  void markAllPointsAsNew();
74 
75  protected:
76  /** @name PLY Import virtual methods to implement in base classes
77  @{ */
78  /** In a base class, reserve memory to prepare subsequent calls to
79  * PLY_import_set_vertex */
80  void PLY_import_set_vertex_count(const size_t N) override;
81 
82  /** In a base class, reserve memory to prepare subsequent calls to
83  * PLY_import_set_face */
84  void PLY_import_set_face_count([[maybe_unused]] const size_t N) override {}
85 
86  /** In a base class, will be called after PLY_import_set_vertex_count() once
87  * for each loaded point.
88  * \param pt_color Will be nullptr if the loaded file does not provide
89  * color info.
90  */
92  const size_t idx, const mrpt::math::TPoint3Df& pt,
93  const mrpt::img::TColorf* pt_color = nullptr) override;
94  /** @} */
95 
96  /** @name PLY Export virtual methods to implement in base classes
97  @{ */
98  size_t PLY_export_get_vertex_count() const override;
99  size_t PLY_export_get_face_count() const override { return 0; }
101  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
102  mrpt::img::TColorf& pt_color) const override;
103  /** @} */
104 
105  public:
106  /** Evaluates the bounding box of this object (including possible children)
107  * in the coordinate frame of the object parent. */
110  mrpt::math::TPoint3D& bb_max) const override
111  {
112  this->octree_getBoundingBox(bb_min, bb_max);
113  }
114 
115  /** @name Read/Write of the list of points to render
116  @{ */
117 
118  inline size_t size() const { return m_points.size(); }
119  /** Set the number of points (with contents undefined) */
120  inline void resize(size_t N)
121  {
122  m_points.resize(N);
123  m_minmax_valid = false;
125  }
126 
127  /** Like STL std::vector's reserve */
128  inline void reserve(size_t N) { m_points.reserve(N); }
129 
130  /** Set the list of (X,Y,Z) point coordinates, all at once, from three
131  * vectors with their coordinates */
132  template <typename T>
134  const std::vector<T>& x, const std::vector<T>& y,
135  const std::vector<T>& z)
136  {
137  const auto N = x.size();
138  m_points.resize(N);
139  for (size_t i = 0; i < N; i++)
140  m_points[i] = {static_cast<float>(x[i]), static_cast<float>(y[i]),
141  static_cast<float>(z[i])};
142  m_minmax_valid = false;
144  }
145 
146  /// \overload Prefer setAllPointsFast() instead
147  void setAllPoints(const std::vector<mrpt::math::TPoint3D>& pts);
148 
149  /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of
150  * the input vectors (via swap) */
151  void setAllPointsFast(std::vector<mrpt::math::TPoint3Df>& pts)
152  {
153  this->clear();
154  m_points.swap(pts);
155  m_minmax_valid = false;
158  }
159 
160  /** Get a const reference to the internal array of points */
161  inline const std::vector<mrpt::math::TPoint3Df>& getArrayPoints() const
162  {
163  return m_points;
164  }
165 
166  /** Empty the list of points. */
167  void clear();
168 
169  /** Adds a new point to the cloud */
170  void insertPoint(float x, float y, float z);
171 
172  /** Read access to each individual point (checks for "i" in the valid range
173  * only in Debug). */
174  inline const mrpt::math::TPoint3Df& operator[](size_t i) const
175  {
176 #ifdef _DEBUG
177  ASSERT_BELOW_(i, size());
178 #endif
179  return m_points[i];
180  }
181 
182  inline const mrpt::math::TPoint3Df& getPoint3Df(size_t i) const
183  {
184  return m_points[i];
185  }
186 
187  /** Write an individual point (checks for "i" in the valid range only in
188  * Debug). */
189  void setPoint(size_t i, const float x, const float y, const float z);
190 
191  /** Write an individual point (without checking validity of the index). */
192  inline void setPoint_fast(
193  size_t i, const float x, const float y, const float z)
194  {
195  m_points[i] = {x, y, z};
196  m_minmax_valid = false;
198  }
199 
200  /** Load the points from any other point map class supported by the adapter
201  * mrpt::opengl::PointCloudAdapter. */
202  template <class POINTSMAP>
203  void loadFromPointsMap(const POINTSMAP* themap);
204  // Must be implemented at the end of the header.
205 
206  /** Load the points from a list of mrpt::math::TPoint3D
207  */
208  template <class LISTOFPOINTS>
209  void loadFromPointsList(LISTOFPOINTS& pointsList)
210  {
211  MRPT_START
212  const size_t N = pointsList.size();
213  m_points.resize(N);
214  size_t idx;
215  typename LISTOFPOINTS::const_iterator it;
216  for (idx = 0, it = pointsList.begin(); idx < N; ++idx, ++it)
217  m_points[idx] = {static_cast<float>(it->x),
218  static_cast<float>(it->y),
219  static_cast<float>(it->z)};
222  MRPT_END
223  }
224 
225  /** Get the number of elements actually rendered in the last render event.
226  */
227  size_t getActuallyRendered() const { return m_last_rendered_count; }
228  /** @} */
229 
230  /** @name Modify the appearance of the rendered points
231  @{ */
232  inline void enableColorFromX(bool v = true)
233  {
236  }
237  inline void enableColorFromY(bool v = true)
238  {
241  }
242  inline void enableColorFromZ(bool v = true)
243  {
246  }
247 
248  inline void enablePointSmooth(bool enable = true)
249  {
250  m_pointSmooth = enable;
252  }
253  inline void disablePointSmooth() { m_pointSmooth = false; }
254  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
255  /** Sets the colors used as extremes when colorFromDepth is enabled. */
256  void setGradientColors(
257  const mrpt::img::TColorf& colorMin, const mrpt::img::TColorf& colorMax);
258 
259  /** @} */
260 
261  void onUpdateBuffers_Points() override;
262 
263  /** Render a subset of points (required by octree renderer) */
264  void render_subset(
265  const bool all, const std::vector<size_t>& idxs,
266  const float render_area_sqpixels) const;
267 
268  /** Constructor */
269  CPointCloud();
270 
271  /** Private, virtual destructor: only can be deleted from smart pointers */
272  ~CPointCloud() override = default;
273 
274  private:
275  /** Buffer for min/max coords when m_colorFromDepth is true. */
276  mutable float m_min{0}, m_max{0}, m_max_m_min{0}, m_max_m_min_inv{0};
277  /** Color linear function slope */
279  mutable bool m_minmax_valid{false};
280 
281  /** The colors used to interpolate when m_colorFromDepth is true. */
283  m_colorFromDepth_max = {0, 0, 1};
284 
285  inline void internal_render_one_point(size_t i) const;
286 };
287 
288 /** Specialization mrpt::opengl::PointCloudAdapter<mrpt::opengl::CPointCloud>
289  * \ingroup mrpt_adapters_grp */
290 template <>
292 {
293  private:
295 
296  public:
297  /** The type of each point XYZ coordinates */
298  using coords_t = float;
299  /** Has any color RGB info? */
300  static constexpr bool HAS_RGB = false;
301  /** Has native RGB info (as floats)? */
302  static constexpr bool HAS_RGBf = false;
303  /** Has native RGB info (as uint8_t)? */
304  static constexpr bool HAS_RGBu8 = false;
305 
306  /** Constructor (accept a const ref for convenience) */
308  : m_obj(*const_cast<mrpt::opengl::CPointCloud*>(&obj))
309  {
310  }
311  /** Get number of points */
312  inline size_t size() const { return m_obj.size(); }
313  /** Set number of points (to uninitialized values) */
314  inline void resize(const size_t N) { m_obj.resize(N); }
315  /** Does nothing as of now */
316  inline void setDimensions(size_t height, size_t width) {}
317  /** Get XYZ coordinates of i'th point */
318  template <typename T>
319  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
320  {
321  const auto& pt = m_obj[idx];
322  x = pt.x;
323  y = pt.y;
324  z = pt.z;
325  }
326  /** Set XYZ coordinates of i'th point */
327  inline void setPointXYZ(
328  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
329  {
330  m_obj.setPoint_fast(idx, x, y, z);
331  }
332 
333  /** Set XYZ coordinates of i'th point */
334  inline void setInvalidPoint(const size_t idx)
335  {
336  m_obj.setPoint_fast(idx, 0, 0, 0);
337  }
338 
339 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloud>
340 
341 // After declaring the adapter we can here implement this method:
342 template <class POINTSMAP>
343 void CPointCloud::loadFromPointsMap(const POINTSMAP* themap)
344 {
346  ASSERT_(themap != nullptr);
348  const mrpt::opengl::PointCloudAdapter<POINTSMAP> pc_src(*themap);
349  const size_t N = pc_src.size();
350  pc_dst.resize(N);
351  for (size_t i = 0; i < N; i++)
352  {
353  float x, y, z;
354  pc_src.getPointXYZ(i, x, y, z);
355  pc_dst.setPointXYZ(i, x, y, z);
356  }
357 }
358 } // namespace mrpt::opengl
void resize(size_t N)
Set the number of points (with contents undefined)
Definition: CPointCloud.h:120
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CPointCloud.h:108
mrpt::img::TColorf m_colorFromDepth_max
Definition: CPointCloud.h:283
void insertPoint(float x, float y, float z)
Adds a new point to the cloud.
#define MRPT_START
Definition: exceptions.h:241
void loadFromPointsMap(const POINTSMAP *themap)
Load the points from any other point map class supported by the adapter mrpt::opengl::PointCloudAdapt...
Definition: CPointCloud.h:343
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
void enableColorFromX(bool v=true)
Definition: CPointCloud.h:232
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:149
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
An adapter to different kinds of point cloud object.
void loadFromPointsList(LISTOFPOINTS &pointsList)
Load the points from a list of mrpt::math::TPoint3D.
Definition: CPointCloud.h:209
void setPoint_fast(size_t i, const float x, const float y, const float z)
Write an individual point (without checking validity of the index).
Definition: CPointCloud.h:192
void PLY_import_set_face_count([[maybe_unused]] const size_t N) override
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face.
Definition: CPointCloud.h:84
void setAllPoints(const std::vector< T > &x, const std::vector< T > &y, const std::vector< T > &z)
Set the list of (X,Y,Z) point coordinates, all at once, from three vectors with their coordinates...
Definition: CPointCloud.h:133
void resize(const size_t N)
Set number of points (to uninitialized values)
Definition: CPointCloud.h:314
void reserve(size_t N)
Like STL std::vector&#39;s reserve.
Definition: CPointCloud.h:128
#define DEFINE_SCHEMA_SERIALIZABLE()
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void clear()
Empty the list of points.
~CPointCloud() override=default
Private, virtual destructor: only can be deleted from smart pointers.
void setDimensions(size_t height, size_t width)
Does nothing as of now.
Definition: CPointCloud.h:316
void setGradientColors(const mrpt::img::TColorf &colorMin, const mrpt::img::TColorf &colorMax)
Sets the colors used as extremes when colorFromDepth is enabled.
size_t m_last_rendered_count_ongoing
Definition: CPointCloud.h:70
void enableColorFromY(bool v=true)
Definition: CPointCloud.h:237
bool m_pointSmooth
Default: false.
Definition: CPointCloud.h:68
Template class that implements the data structure and algorithms for Octree-based efficient rendering...
std::vector< mrpt::math::TPoint3Df > & m_points
Actually, an alias for the base class shader container of points.
Definition: CPointCloud.h:64
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
An adapter to different kinds of point cloud object.
CPointCloud()
Constructor.
Definition: CPointCloud.cpp:50
const mrpt::math::TPoint3Df & getPoint3Df(size_t i) const
Definition: CPointCloud.h:182
void enableColorFromZ(bool v=true)
Definition: CPointCloud.h:242
const mrpt::math::TPoint3Df & operator[](size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
Definition: CPointCloud.h:174
const std::vector< mrpt::math::TPoint3Df > & getArrayPoints() const
Get a const reference to the internal array of points.
Definition: CPointCloud.h:161
A virtual base class that implements the capability of exporting 3D point clouds and faces to a file ...
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.
Definition: CPointCloud.h:327
void internal_render_one_point(size_t i) const
size_t PLY_export_get_face_count() const override
In a base class, return the number of faces.
Definition: CPointCloud.h:99
float coords_t
The type of each point XYZ coordinates.
Definition: CPointCloud.h:298
Renderizable generic renderer for objects using the points shader.
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.
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...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void render_subset(const bool all, const std::vector< size_t > &idxs, const float render_area_sqpixels) const
Render a subset of points (required by octree renderer)
mrpt::img::TColorf m_col_slop
Color linear function slope.
Definition: CPointCloud.h:278
void enablePointSmooth(bool enable=true)
Definition: CPointCloud.h:248
bool isPointSmoothEnabled() const
Definition: CPointCloud.h:254
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)
#define MRPT_END
Definition: exceptions.h:245
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
void setAllPointsFast(std::vector< mrpt::math::TPoint3Df > &pts)
Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of the input vectors (via swap) ...
Definition: CPointCloud.h:151
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void octree_getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
size_t PLY_export_get_vertex_count() const override
In a base class, return the number of vertices.
const auto bb_max
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...
mrpt::img::TColorf m_colorFromDepth_min
The colors used to interpolate when m_colorFromDepth is true.
Definition: CPointCloud.h:282
void setPoint(size_t i, const float x, const float y, const float z)
Write an individual point (checks for "i" in the valid range only in Debug).
float m_min
Buffer for min/max coords when m_colorFromDepth is true.
Definition: CPointCloud.h:276
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
const auto bb_min
void onUpdateBuffers_Points() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CPointCloud.cpp:52
PointCloudAdapter(const mrpt::opengl::CPointCloud &obj)
Constructor (accept a const ref for convenience)
Definition: CPointCloud.h:307
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
Definition: CPointCloud.h:227
A virtual base class that implements the capability of importing 3D point clouds and faces from a fil...
mrpt::img::TColorf m_col_slop_inv
Definition: CPointCloud.h:278
void setInvalidPoint(const size_t idx)
Set XYZ coordinates of i&#39;th point.
Definition: CPointCloud.h:334
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
Definition: CPointCloud.h:319
A cloud of points, all with the same color or each depending on its value along a particular coordina...
Definition: CPointCloud.h:44



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020