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