MRPT  1.9.9
CPointCloud.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://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  * http://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 {
49  protected:
50  enum Axis
51  {
52  colNone = 0,
57  std::vector<float> m_xs, m_ys, m_zs;
58  /** By default is 1.0 */
59  float m_pointSize;
60  /** Default: false */
62 
63  mutable volatile size_t m_last_rendered_count,
65 
66  /** Do needed internal work if all points are new (octree rebuilt,...) */
67  void markAllPointsAsNew();
68 
69  protected:
70  /** @name PLY Import virtual methods to implement in base classes
71  @{ */
72  /** In a base class, reserve memory to prepare subsequent calls to
73  * PLY_import_set_vertex */
74  virtual void PLY_import_set_vertex_count(const size_t N) override;
75 
76  /** In a base class, reserve memory to prepare subsequent calls to
77  * PLY_import_set_face */
78  virtual void PLY_import_set_face_count(const size_t N) override
79  {
81  }
82 
83  /** In a base class, will be called after PLY_import_set_vertex_count() once
84  * for each loaded point.
85  * \param pt_color Will be nullptr if the loaded file does not provide
86  * color info.
87  */
88  virtual void PLY_import_set_vertex(
89  const size_t idx, const mrpt::math::TPoint3Df& pt,
90  const mrpt::img::TColorf* pt_color = nullptr) override;
91  /** @} */
92 
93  /** @name PLY Export virtual methods to implement in base classes
94  @{ */
95  size_t PLY_export_get_vertex_count() const override;
96  size_t PLY_export_get_face_count() const override { return 0; }
98  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
99  mrpt::img::TColorf& pt_color) const override;
100  /** @} */
101 
102  public:
103  /** Evaluates the bounding box of this object (including possible children)
104  * in the coordinate frame of the object parent. */
105  virtual void getBoundingBox(
106  mrpt::math::TPoint3D& bb_min,
107  mrpt::math::TPoint3D& bb_max) const override
108  {
109  this->octree_getBoundingBox(bb_min, bb_max);
110  }
111 
112  /** @name Read/Write of the list of points to render
113  @{ */
114 
115  inline size_t size() const { return m_xs.size(); }
116  /** Set the number of points (with contents undefined) */
117  inline void resize(size_t N)
118  {
119  m_xs.resize(N);
120  m_ys.resize(N);
121  m_zs.resize(N);
122  m_minmax_valid = false;
124  }
125 
126  /** Like STL std::vector's reserve */
127  inline void reserve(size_t N)
128  {
129  m_xs.reserve(N);
130  m_ys.reserve(N);
131  m_zs.reserve(N);
132  }
133 
134  /** Set the list of (X,Y,Z) point coordinates, all at once, from three
135  * vectors with their coordinates */
137  const std::vector<float>& x, const std::vector<float>& y,
138  const std::vector<float>& z)
139  {
140  m_xs = x;
141  m_ys = y;
142  m_zs = z;
143  m_minmax_valid = false;
145  }
146 
147  /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of
148  * the input vectors (via swap) */
150  std::vector<float>& x, std::vector<float>& y, std::vector<float>& z)
151  {
152  this->clear();
153  m_xs.swap(x);
154  m_ys.swap(y);
155  m_zs.swap(z);
156  m_minmax_valid = false;
158  }
159 
160  /** Get a const reference to the internal array of X coordinates */
161  inline const std::vector<float>& getArrayX() const { return m_xs; }
162  /** Get a const reference to the internal array of Y coordinates */
163  inline const std::vector<float>& getArrayY() const { return m_ys; }
164  /** Get a const reference to the internal array of Z coordinates */
165  inline const std::vector<float>& getArrayZ() const { return m_zs; }
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 mrpt::math::TPoint3D operator[](size_t i) const
175  {
176 #ifdef _DEBUG
177  ASSERT_BELOW_(i, size());
178 #endif
179  return mrpt::math::TPoint3D(m_xs[i], m_ys[i], m_zs[i]);
180  }
181 
182  /** Read access to each individual point (checks for "i" in the valid range
183  * only in Debug). */
184  inline mrpt::math::TPoint3D getPoint(size_t i) const
185  {
186 #ifdef _DEBUG
187  ASSERT_BELOW_(i, size());
188 #endif
189  return mrpt::math::TPoint3D(m_xs[i], m_ys[i], m_zs[i]);
190  }
191 
192  /** Read access to each individual point (checks for "i" in the valid range
193  * only in Debug). */
194  inline mrpt::math::TPoint3Df getPointf(size_t i) const
195  {
196 #ifdef _DEBUG
197  ASSERT_BELOW_(i, size());
198 #endif
199  return mrpt::math::TPoint3Df(m_xs[i], m_ys[i], m_zs[i]);
200  }
201 
202  /** Write an individual point (checks for "i" in the valid range only in
203  * Debug). */
204  void setPoint(size_t i, const float x, const float y, const float z);
205 
206  /** Write an individual point (without checking validity of the index). */
207  inline void setPoint_fast(
208  size_t i, const float x, const float y, const float z)
209  {
210  m_xs[i] = x;
211  m_ys[i] = y;
212  m_zs[i] = z;
213  m_minmax_valid = false;
215  }
216 
217  /** Load the points from any other point map class supported by the adapter
218  * mrpt::opengl::PointCloudAdapter. */
219  template <class POINTSMAP>
220  void loadFromPointsMap(const POINTSMAP* themap);
221  // Must be implemented at the end of the header.
222 
223  /** Load the points from a list of mrpt::math::TPoint3D
224  */
225  template <class LISTOFPOINTS>
226  void loadFromPointsList(LISTOFPOINTS& pointsList)
227  {
228  MRPT_START
229  const size_t N = pointsList.size();
230 
231  m_xs.resize(N);
232  m_ys.resize(N);
233  m_zs.resize(N);
234 
235  size_t idx;
236  typename LISTOFPOINTS::const_iterator it;
237  for (idx = 0, it = pointsList.begin(); idx < N; ++idx, ++it)
238  {
239  m_xs[idx] = it->x;
240  m_ys[idx] = it->y;
241  m_zs[idx] = it->z;
242  }
244  MRPT_END
245  }
246 
247  /** Get the number of elements actually rendered in the last render event.
248  */
249  size_t getActuallyRendered() const { return m_last_rendered_count; }
250  /** @} */
251 
252  /** @name Modify the appearance of the rendered points
253  @{ */
254  inline void enableColorFromX(bool v = true)
255  {
257  }
258  inline void enableColorFromY(bool v = true)
259  {
261  }
262  inline void enableColorFromZ(bool v = true)
263  {
265  }
266 
267  /** By default is 1.0 */
268  inline void setPointSize(float p) { m_pointSize = p; }
269  inline float getPointSize() const { return m_pointSize; }
270  inline void enablePointSmooth(bool enable = true)
271  {
272  m_pointSmooth = enable;
273  }
274  inline void disablePointSmooth() { m_pointSmooth = false; }
275  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
276  /** Sets the colors used as extremes when colorFromDepth is enabled. */
277  void setGradientColors(
278  const mrpt::img::TColorf& colorMin, const mrpt::img::TColorf& colorMax);
279 
280  /** @} */
281 
282  /** Render */
283  void render() const override;
284 
285  /** Render a subset of points (required by octree renderer) */
286  void render_subset(
287  const bool all, const std::vector<size_t>& idxs,
288  const float render_area_sqpixels) const;
289 
290  /** Constructor */
291  CPointCloud();
292 
293  /** Private, virtual destructor: only can be deleted from smart pointers */
294  virtual ~CPointCloud() {}
295  private:
296  /** Buffer for min/max coords when m_colorFromDepth is true. */
298  /** Color linear function slope */
300  mutable bool m_minmax_valid;
301 
302  /** The colors used to interpolate when m_colorFromDepth is true. */
304 
305  inline void internal_render_one_point(size_t i) const;
306 };
307 
308 /** Specialization mrpt::opengl::PointCloudAdapter<mrpt::opengl::CPointCloud>
309  * \ingroup mrpt_adapters_grp */
310 template <>
312  : public detail::PointCloudAdapterHelperNoRGB<mrpt::opengl::CPointCloud,
313  float>
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 const int HAS_RGB = 0;
323  /** Has native RGB info (as floats)? */
324  static const int HAS_RGBf = 0;
325  /** Has native RGB info (as uint8_t)? */
326  static const int HAS_RGBu8 = 0;
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  {
340  }
341  /** Get XYZ coordinates of i'th point */
342  template <typename T>
343  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
344  {
345  x = m_obj.getArrayX()[idx];
346  y = m_obj.getArrayY()[idx];
347  z = m_obj.getArrayZ()[idx];
348  }
349  /** Set XYZ coordinates of i'th point */
350  inline void setPointXYZ(
351  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
352  {
353  m_obj.setPoint_fast(idx, x, y, z);
354  }
355 
356  /** Set XYZ coordinates of i'th point */
357  inline void setInvalidPoint(const size_t idx)
358  {
359  THROW_EXCEPTION("mrpt::opengl::CPointCloud needs to be dense");
360  }
361 
362 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloud>
363 
364 // After declaring the adapter we can here implement this method:
365 template <class POINTSMAP>
366 void CPointCloud::loadFromPointsMap(const POINTSMAP* themap)
367 {
368  ASSERT_(themap != nullptr);
370  const mrpt::opengl::PointCloudAdapter<POINTSMAP> pc_src(*themap);
371  const size_t N = pc_src.size();
372  pc_dst.resize(N);
373  for (size_t i = 0; i < N; i++)
374  {
375  float x, y, z;
376  pc_src.getPointXYZ(i, x, y, z);
377  pc_dst.setPointXYZ(i, x, y, z);
378  }
379 }
380 }
381 
std::vector< float > m_xs
Definition: CPointCloud.h:57
void render() const override
Render.
Definition: CPointCloud.cpp:75
const std::vector< float > & getArrayY() const
Get a const reference to the internal array of Y coordinates.
Definition: CPointCloud.h:163
void resize(size_t N)
Set the number of points (with contents undefined)
Definition: CPointCloud.h:117
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:194
mrpt::img::TColorf m_colorFromDepth_max
Definition: CPointCloud.h:303
void insertPoint(float x, float y, float z)
Adds a new point to the cloud.
#define MRPT_START
Definition: exceptions.h:262
GLdouble GLdouble z
Definition: glext.h:3872
void loadFromPointsMap(const POINTSMAP *themap)
Load the points from any other point map class supported by the adapter mrpt::opengl::PointCloudAdapt...
Definition: CPointCloud.h:366
void enableColorFromX(bool v=true)
Definition: CPointCloud.h:254
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:165
An adapter to different kinds of point cloud object.
std::vector< float > m_ys
Definition: CPointCloud.h:57
void loadFromPointsList(LISTOFPOINTS &pointsList)
Load the points from a list of mrpt::math::TPoint3D.
Definition: CPointCloud.h:226
volatile size_t m_last_rendered_count
Definition: CPointCloud.h:63
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:207
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:41
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:136
virtual 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:105
virtual ~CPointCloud()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CPointCloud.h:294
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:127
void clear()
Empty the list of points.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
GLenum GLsizei width
Definition: glext.h:3531
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:269
void enableColorFromY(bool v=true)
Definition: CPointCloud.h:258
bool m_pointSmooth
Default: false.
Definition: CPointCloud.h:61
Template class that implements the data structure and algorithms for Octree-based efficient rendering...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
const std::vector< float > & getArrayZ() const
Get a const reference to the internal array of Z coordinates.
Definition: CPointCloud.h:165
An adapter to different kinds of point cloud object.
Lightweight 3D point (float version).
CPointCloud()
Constructor.
Definition: CPointCloud.cpp:52
void setPointSize(float p)
By default is 1.0.
Definition: CPointCloud.h:268
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:174
void enableColorFromZ(bool v=true)
Definition: CPointCloud.h:262
const std::vector< float > & getArrayX() const
Get a const reference to the internal array of X coordinates.
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:350
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:96
float coords_t
The type of each point XYZ coordinates.
Definition: CPointCloud.h:320
virtual 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.
virtual 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:3678
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:299
enum mrpt::opengl::CPointCloud::Axis m_colorFromDepth
void enablePointSmooth(bool enable=true)
Definition: CPointCloud.h:270
volatile size_t m_last_rendered_count_ongoing
Definition: CPointCloud.h:63
bool isPointSmoothEnabled() const
Definition: CPointCloud.h:275
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:266
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
float m_pointSize
By default is 1.0.
Definition: CPointCloud.h:59
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:184
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
size_t PLY_export_get_vertex_count() const override
In a base class, return the number of vertices.
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:303
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:3538
float m_min
Buffer for min/max coords when m_colorFromDepth is true.
Definition: CPointCloud.h:297
A helper base class for those PointCloudAdapter<> which do not handle RGB data; it declares needed in...
GLenum GLint x
Definition: glext.h:3538
virtual 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:78
Lightweight 3D point.
GLenum GLsizei GLsizei height
Definition: glext.h:3554
PointCloudAdapter(const mrpt::opengl::CPointCloud &obj)
Constructor (accept a const ref for convenience)
Definition: CPointCloud.h:329
GLfloat GLfloat p
Definition: glext.h:6305
const Scalar * const_iterator
Definition: eigen_plugins.h:27
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
Definition: CPointCloud.h:249
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:149
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:299
void setInvalidPoint(const size_t idx)
Set XYZ coordinates of i&#39;th point.
Definition: CPointCloud.h:357
std::vector< float > m_zs
Definition: CPointCloud.h:57
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
Definition: CPointCloud.h:343
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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020