MRPT  1.9.9
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(const size_t N) override
85  {
87  }
88 
89  /** In a base class, will be called after PLY_import_set_vertex_count() once
90  * for each loaded point.
91  * \param pt_color Will be nullptr if the loaded file does not provide
92  * color info.
93  */
95  const size_t idx, const mrpt::math::TPoint3Df& pt,
96  const mrpt::img::TColorf* pt_color = nullptr) override;
97  /** @} */
98 
99  /** @name PLY Export virtual methods to implement in base classes
100  @{ */
101  size_t PLY_export_get_vertex_count() const override;
102  size_t PLY_export_get_face_count() const override { return 0; }
104  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
105  mrpt::img::TColorf& pt_color) const override;
106  /** @} */
107 
108  public:
109  /** Evaluates the bounding box of this object (including possible children)
110  * in the coordinate frame of the object parent. */
113  mrpt::math::TPoint3D& bb_max) const override
114  {
115  this->octree_getBoundingBox(bb_min, bb_max);
116  }
117 
118  /** @name Read/Write of the list of points to render
119  @{ */
120 
121  inline size_t size() const { return m_points.size(); }
122  /** Set the number of points (with contents undefined) */
123  inline void resize(size_t N)
124  {
125  m_points.resize(N);
126  m_minmax_valid = false;
128  }
129 
130  /** Like STL std::vector's reserve */
131  inline void reserve(size_t N) { m_points.reserve(N); }
132 
133  /** Set the list of (X,Y,Z) point coordinates, all at once, from three
134  * vectors with their coordinates */
135  template <typename T>
137  const std::vector<T>& x, const std::vector<T>& y,
138  const std::vector<T>& z)
139  {
140  const auto N = x.size();
141  m_points.resize(N);
142  for (size_t i = 0; i < N; i++)
143  m_points[i] = {static_cast<float>(x[i]), static_cast<float>(y[i]),
144  static_cast<float>(z[i])};
145  m_minmax_valid = false;
147  }
148 
149  /// \overload Prefer setAllPointsFast() instead
150  void setAllPoints(const std::vector<mrpt::math::TPoint3D>& pts);
151 
152  /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of
153  * the input vectors (via swap) */
154  void setAllPointsFast(std::vector<mrpt::math::TPoint3Df>& pts)
155  {
156  this->clear();
157  m_points.swap(pts);
158  m_minmax_valid = false;
161  }
162 
163  /** Get a const reference to the internal array of points */
164  inline const std::vector<mrpt::math::TPoint3Df>& getArrayPoints() const
165  {
166  return m_points;
167  }
168 
169  /** Empty the list of points. */
170  void clear();
171 
172  /** Adds a new point to the cloud */
173  void insertPoint(float x, float y, float z);
174 
175  /** Read access to each individual point (checks for "i" in the valid range
176  * only in Debug). */
177  inline const mrpt::math::TPoint3Df& operator[](size_t i) const
178  {
179 #ifdef _DEBUG
180  ASSERT_BELOW_(i, size());
181 #endif
182  return m_points[i];
183  }
184 
185  inline const mrpt::math::TPoint3Df& getPoint3Df(size_t i) const
186  {
187  return m_points[i];
188  }
189 
190  /** Write an individual point (checks for "i" in the valid range only in
191  * Debug). */
192  void setPoint(size_t i, const float x, const float y, const float z);
193 
194  /** Write an individual point (without checking validity of the index). */
195  inline void setPoint_fast(
196  size_t i, const float x, const float y, const float z)
197  {
198  m_points[i] = {x, y, z};
199  m_minmax_valid = false;
201  }
202 
203  /** Load the points from any other point map class supported by the adapter
204  * mrpt::opengl::PointCloudAdapter. */
205  template <class POINTSMAP>
206  void loadFromPointsMap(const POINTSMAP* themap);
207  // Must be implemented at the end of the header.
208 
209  /** Load the points from a list of mrpt::math::TPoint3D
210  */
211  template <class LISTOFPOINTS>
212  void loadFromPointsList(LISTOFPOINTS& pointsList)
213  {
214  MRPT_START
215  const size_t N = pointsList.size();
216  m_points.resize(N);
217  size_t idx;
218  typename LISTOFPOINTS::const_iterator it;
219  for (idx = 0, it = pointsList.begin(); idx < N; ++idx, ++it)
220  m_points[idx] = {static_cast<float>(it->x),
221  static_cast<float>(it->y),
222  static_cast<float>(it->z)};
225  MRPT_END
226  }
227 
228  /** Get the number of elements actually rendered in the last render event.
229  */
230  size_t getActuallyRendered() const { return m_last_rendered_count; }
231  /** @} */
232 
233  /** @name Modify the appearance of the rendered points
234  @{ */
235  inline void enableColorFromX(bool v = true)
236  {
239  }
240  inline void enableColorFromY(bool v = true)
241  {
244  }
245  inline void enableColorFromZ(bool v = true)
246  {
249  }
250 
251  inline void enablePointSmooth(bool enable = true)
252  {
253  m_pointSmooth = enable;
255  }
256  inline void disablePointSmooth() { m_pointSmooth = false; }
257  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
258  /** Sets the colors used as extremes when colorFromDepth is enabled. */
259  void setGradientColors(
260  const mrpt::img::TColorf& colorMin, const mrpt::img::TColorf& colorMax);
261 
262  /** @} */
263 
264  void onUpdateBuffers_Points() override;
265 
266  /** Render a subset of points (required by octree renderer) */
267  void render_subset(
268  const bool all, const std::vector<size_t>& idxs,
269  const float render_area_sqpixels) const;
270 
271  /** Constructor */
272  CPointCloud();
273 
274  /** Private, virtual destructor: only can be deleted from smart pointers */
275  ~CPointCloud() override = default;
276 
277  private:
278  /** Buffer for min/max coords when m_colorFromDepth is true. */
279  mutable float m_min{0}, m_max{0}, m_max_m_min{0}, m_max_m_min_inv{0};
280  /** Color linear function slope */
282  mutable bool m_minmax_valid{false};
283 
284  /** The colors used to interpolate when m_colorFromDepth is true. */
286  m_colorFromDepth_max = {0, 0, 1};
287 
288  inline void internal_render_one_point(size_t i) const;
289 };
290 
291 /** Specialization mrpt::opengl::PointCloudAdapter<mrpt::opengl::CPointCloud>
292  * \ingroup mrpt_adapters_grp */
293 template <>
295 {
296  private:
298 
299  public:
300  /** The type of each point XYZ coordinates */
301  using coords_t = float;
302  /** Has any color RGB info? */
303  static constexpr bool HAS_RGB = false;
304  /** Has native RGB info (as floats)? */
305  static constexpr bool HAS_RGBf = false;
306  /** Has native RGB info (as uint8_t)? */
307  static constexpr bool HAS_RGBu8 = false;
308 
309  /** Constructor (accept a const ref for convenience) */
311  : m_obj(*const_cast<mrpt::opengl::CPointCloud*>(&obj))
312  {
313  }
314  /** Get number of points */
315  inline size_t size() const { return m_obj.size(); }
316  /** Set number of points (to uninitialized values) */
317  inline void resize(const size_t N) { m_obj.resize(N); }
318  /** Does nothing as of now */
319  inline void setDimensions(size_t height, size_t width) {}
320  /** Get XYZ coordinates of i'th point */
321  template <typename T>
322  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
323  {
324  const auto& pt = m_obj[idx];
325  x = pt.x;
326  y = pt.y;
327  z = pt.z;
328  }
329  /** Set XYZ coordinates of i'th point */
330  inline void setPointXYZ(
331  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
332  {
333  m_obj.setPoint_fast(idx, x, y, z);
334  }
335 
336  /** Set XYZ coordinates of i'th point */
337  inline void setInvalidPoint(const size_t idx)
338  {
339  m_obj.setPoint_fast(idx, 0, 0, 0);
340  }
341 
342 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloud>
343 
344 // After declaring the adapter we can here implement this method:
345 template <class POINTSMAP>
346 void CPointCloud::loadFromPointsMap(const POINTSMAP* themap)
347 {
349  ASSERT_(themap != nullptr);
351  const mrpt::opengl::PointCloudAdapter<POINTSMAP> pc_src(*themap);
352  const size_t N = pc_src.size();
353  pc_dst.resize(N);
354  for (size_t i = 0; i < N; i++)
355  {
356  float x, y, z;
357  pc_src.getPointXYZ(i, x, y, z);
358  pc_dst.setPointXYZ(i, x, y, z);
359  }
360 }
361 } // namespace mrpt::opengl
void resize(size_t N)
Set the number of points (with contents undefined)
Definition: CPointCloud.h:123
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:111
mrpt::img::TColorf m_colorFromDepth_max
Definition: CPointCloud.h:286
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:346
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
void enableColorFromX(bool v=true)
Definition: CPointCloud.h:235
#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:212
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:195
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:136
void resize(const size_t N)
Set number of points (to uninitialized values)
Definition: CPointCloud.h:317
void reserve(size_t N)
Like STL std::vector&#39;s reserve.
Definition: CPointCloud.h:131
#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:319
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:240
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:185
void enableColorFromZ(bool v=true)
Definition: CPointCloud.h:245
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:177
const std::vector< mrpt::math::TPoint3Df > & getArrayPoints() const
Get a const reference to the internal array of points.
Definition: CPointCloud.h:164
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:330
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:102
float coords_t
The type of each point XYZ coordinates.
Definition: CPointCloud.h:301
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:281
void enablePointSmooth(bool enable=true)
Definition: CPointCloud.h:251
bool isPointSmoothEnabled() const
Definition: CPointCloud.h:257
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:154
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
void PLY_import_set_face_count(const size_t N) override
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face.
Definition: CPointCloud.h:84
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:285
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:279
#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:310
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
Definition: CPointCloud.h:230
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:281
void setInvalidPoint(const size_t idx)
Set XYZ coordinates of i&#39;th point.
Definition: CPointCloud.h:337
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
Definition: CPointCloud.h:322
A cloud of points, all with the same color or each depending on its value along a particular coordina...
Definition: CPointCloud.h:44
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020