Main MRPT website > C++ reference for 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
17 {
18 namespace opengl
19 {
20 /** A cloud of points, all with the same color or each depending on its value
21  * along a particular coordinate axis.
22  * This class is just an OpenGL representation of a point cloud. For operating
23  * with maps of points, see mrpt::maps::CPointsMap and derived classes.
24  *
25  * To load from a points-map, CPointCloud::loadFromPointsMap().
26  *
27  * This class uses smart optimizations while rendering to efficiently draw
28  * clouds of millions of points,
29  * as described in this page:
30  * http://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
31  *
32  * \sa opengl::CPlanarLaserScan, opengl::COpenGLScene,
33  * opengl::CPointCloudColoured, mrpt::maps::CPointsMap
34  *
35  * <div align="center">
36  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
37  * border-style: solid;">
38  * <tr> <td> mrpt::opengl::CPointCloud </td> <td> \image html
39  * preview_CPointCloud.png </td> </tr>
40  * </table>
41  * </div>
42  *
43  * \ingroup mrpt_opengl_grp
44  */
45 class CPointCloud : public CRenderizable,
46  public COctreePointRenderer<CPointCloud>,
49 {
51  protected:
52  enum Axis
53  {
54  colNone = 0,
59  std::vector<float> m_xs, m_ys, m_zs;
60  /** By default is 1.0 */
61  float m_pointSize;
62  /** Default: false */
64 
65  mutable volatile size_t m_last_rendered_count,
67 
68  /** Do needed internal work if all points are new (octree rebuilt,...) */
69  void markAllPointsAsNew();
70 
71  protected:
72  /** @name PLY Import virtual methods to implement in base classes
73  @{ */
74  /** In a base class, reserve memory to prepare subsequent calls to
75  * PLY_import_set_vertex */
76  virtual void PLY_import_set_vertex_count(const size_t N) override;
77 
78  /** In a base class, reserve memory to prepare subsequent calls to
79  * PLY_import_set_face */
80  virtual void PLY_import_set_face_count(const size_t N) override
81  {
83  }
84 
85  /** In a base class, will be called after PLY_import_set_vertex_count() once
86  * for each loaded point.
87  * \param pt_color Will be nullptr if the loaded file does not provide
88  * color info.
89  */
90  virtual void PLY_import_set_vertex(
91  const size_t idx, const mrpt::math::TPoint3Df& pt,
92  const mrpt::img::TColorf* pt_color = nullptr) override;
93  /** @} */
94 
95  /** @name PLY Export virtual methods to implement in base classes
96  @{ */
97  size_t PLY_export_get_vertex_count() const override;
98  size_t PLY_export_get_face_count() const override { return 0; }
100  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
101  mrpt::img::TColorf& pt_color) const override;
102  /** @} */
103 
104  public:
105  /** Evaluates the bounding box of this object (including possible children)
106  * in the coordinate frame of the object parent. */
107  virtual void getBoundingBox(
108  mrpt::math::TPoint3D& bb_min,
109  mrpt::math::TPoint3D& bb_max) const override
110  {
111  this->octree_getBoundingBox(bb_min, bb_max);
112  }
113 
114  /** @name Read/Write of the list of points to render
115  @{ */
116 
117  inline size_t size() const { return m_xs.size(); }
118  /** Set the number of points (with contents undefined) */
119  inline void resize(size_t N)
120  {
121  m_xs.resize(N);
122  m_ys.resize(N);
123  m_zs.resize(N);
124  m_minmax_valid = false;
126  }
127 
128  /** Like STL std::vector's reserve */
129  inline void reserve(size_t N)
130  {
131  m_xs.reserve(N);
132  m_ys.reserve(N);
133  m_zs.reserve(N);
134  }
135 
136  /** Set the list of (X,Y,Z) point coordinates, all at once, from three
137  * vectors with their coordinates */
139  const std::vector<float>& x, const std::vector<float>& y,
140  const std::vector<float>& z)
141  {
142  m_xs = x;
143  m_ys = y;
144  m_zs = z;
145  m_minmax_valid = false;
147  }
148 
149  /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of
150  * the input vectors (via swap) */
152  std::vector<float>& x, std::vector<float>& y, std::vector<float>& z)
153  {
154  this->clear();
155  m_xs.swap(x);
156  m_ys.swap(y);
157  m_zs.swap(z);
158  m_minmax_valid = false;
160  }
161 
162  /** Get a const reference to the internal array of X coordinates */
163  inline const std::vector<float>& getArrayX() const { return m_xs; }
164  /** Get a const reference to the internal array of Y coordinates */
165  inline const std::vector<float>& getArrayY() const { return m_ys; }
166  /** Get a const reference to the internal array of Z coordinates */
167  inline const std::vector<float>& getArrayZ() const { return m_zs; }
168  /** Empty the list of points. */
169  void clear();
170 
171  /** Adds a new point to the cloud */
172  void insertPoint(float x, float y, float z);
173 
174  /** Read access to each individual point (checks for "i" in the valid range
175  * only in Debug). */
176  inline mrpt::math::TPoint3D operator[](size_t i) const
177  {
178 #ifdef _DEBUG
179  ASSERT_BELOW_(i, size());
180 #endif
181  return mrpt::math::TPoint3D(m_xs[i], m_ys[i], m_zs[i]);
182  }
183 
184  /** Read access to each individual point (checks for "i" in the valid range
185  * only in Debug). */
186  inline mrpt::math::TPoint3D getPoint(size_t i) const
187  {
188 #ifdef _DEBUG
189  ASSERT_BELOW_(i, size());
190 #endif
191  return mrpt::math::TPoint3D(m_xs[i], m_ys[i], m_zs[i]);
192  }
193 
194  /** Read access to each individual point (checks for "i" in the valid range
195  * only in Debug). */
196  inline mrpt::math::TPoint3Df getPointf(size_t i) const
197  {
198 #ifdef _DEBUG
199  ASSERT_BELOW_(i, size());
200 #endif
201  return mrpt::math::TPoint3Df(m_xs[i], m_ys[i], m_zs[i]);
202  }
203 
204  /** Write an individual point (checks for "i" in the valid range only in
205  * Debug). */
206  void setPoint(size_t i, const float x, const float y, const float z);
207 
208  /** Write an individual point (without checking validity of the index). */
209  inline void setPoint_fast(
210  size_t i, const float x, const float y, const float z)
211  {
212  m_xs[i] = x;
213  m_ys[i] = y;
214  m_zs[i] = z;
215  m_minmax_valid = false;
217  }
218 
219  /** Load the points from any other point map class supported by the adapter
220  * mrpt::opengl::PointCloudAdapter. */
221  template <class POINTSMAP>
222  void loadFromPointsMap(const POINTSMAP* themap);
223  // Must be implemented at the end of the header.
224 
225  /** Load the points from a list of mrpt::math::TPoint3D
226  */
227  template <class LISTOFPOINTS>
228  void loadFromPointsList(LISTOFPOINTS& pointsList)
229  {
230  MRPT_START
231  const size_t N = pointsList.size();
232 
233  m_xs.resize(N);
234  m_ys.resize(N);
235  m_zs.resize(N);
236 
237  size_t idx;
238  typename LISTOFPOINTS::const_iterator it;
239  for (idx = 0, it = pointsList.begin(); idx < N; ++idx, ++it)
240  {
241  m_xs[idx] = it->x;
242  m_ys[idx] = it->y;
243  m_zs[idx] = it->z;
244  }
246  MRPT_END
247  }
248 
249  /** Get the number of elements actually rendered in the last render event.
250  */
251  size_t getActuallyRendered() const { return m_last_rendered_count; }
252  /** @} */
253 
254  /** @name Modify the appearance of the rendered points
255  @{ */
256  inline void enableColorFromX(bool v = true)
257  {
259  }
260  inline void enableColorFromY(bool v = true)
261  {
263  }
264  inline void enableColorFromZ(bool v = true)
265  {
267  }
268 
269  /** By default is 1.0 */
270  inline void setPointSize(float p) { m_pointSize = p; }
271  inline float getPointSize() const { return m_pointSize; }
272  inline void enablePointSmooth(bool enable = true)
273  {
274  m_pointSmooth = enable;
275  }
276  inline void disablePointSmooth() { m_pointSmooth = false; }
277  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
278  /** Sets the colors used as extremes when colorFromDepth is enabled. */
279  void setGradientColors(
280  const mrpt::img::TColorf& colorMin, const mrpt::img::TColorf& colorMax);
281 
282  /** @} */
283 
284  /** Render */
285  void render() const override;
286 
287  /** Render a subset of points (required by octree renderer) */
288  void render_subset(
289  const bool all, const std::vector<size_t>& idxs,
290  const float render_area_sqpixels) const;
291 
292  /** Constructor */
293  CPointCloud();
294 
295  /** Private, virtual destructor: only can be deleted from smart pointers */
296  virtual ~CPointCloud() {}
297  private:
298  /** Buffer for min/max coords when m_colorFromDepth is true. */
300  /** Color linear function slope */
302  mutable bool m_minmax_valid;
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  : public detail::PointCloudAdapterHelperNoRGB<mrpt::opengl::CPointCloud,
315  float>
316 {
317  private:
319 
320  public:
321  /** The type of each point XYZ coordinates */
322  using coords_t = float;
323  /** Has any color RGB info? */
324  static const int HAS_RGB = 0;
325  /** Has native RGB info (as floats)? */
326  static const int HAS_RGBf = 0;
327  /** Has native RGB info (as uint8_t)? */
328  static const int HAS_RGBu8 = 0;
329 
330  /** Constructor (accept a const ref for convenience) */
332  : m_obj(*const_cast<mrpt::opengl::CPointCloud*>(&obj))
333  {
334  }
335  /** Get number of points */
336  inline size_t size() const { return m_obj.size(); }
337  /** Set number of points (to uninitialized values) */
338  inline void resize(const size_t N) { m_obj.resize(N); }
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 opengl
379 } // namespace mrpt
std::vector< float > m_xs
Definition: CPointCloud.h:59
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:165
void resize(size_t N)
Set the number of points (with contents undefined)
Definition: CPointCloud.h:119
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:196
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: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:364
void enableColorFromX(bool v=true)
Definition: CPointCloud.h:256
#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:59
void loadFromPointsList(LISTOFPOINTS &pointsList)
Load the points from a list of mrpt::math::TPoint3D.
Definition: CPointCloud.h:228
volatile size_t m_last_rendered_count
Definition: CPointCloud.h:65
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:209
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
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:138
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:107
virtual ~CPointCloud()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CPointCloud.h:296
void resize(const size_t N)
Set number of points (to uninitialized values)
Definition: CPointCloud.h:338
void reserve(size_t N)
Like STL std::vector&#39;s reserve.
Definition: CPointCloud.h:129
void clear()
Empty the list of points.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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:271
void enableColorFromY(bool v=true)
Definition: CPointCloud.h:260
bool m_pointSmooth
Default: false.
Definition: CPointCloud.h:63
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:167
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:270
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:176
void enableColorFromZ(bool v=true)
Definition: CPointCloud.h:264
const std::vector< float > & getArrayX() const
Get a const reference to the internal array of X coordinates.
Definition: CPointCloud.h:163
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:98
float coords_t
The type of each point XYZ coordinates.
Definition: CPointCloud.h:322
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:301
enum mrpt::opengl::CPointCloud::Axis m_colorFromDepth
void enablePointSmooth(bool enable=true)
Definition: CPointCloud.h:272
volatile size_t m_last_rendered_count_ongoing
Definition: CPointCloud.h:65
bool isPointSmoothEnabled() const
Definition: CPointCloud.h:277
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)
#define MRPT_END
Definition: exceptions.h:266
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
float m_pointSize
By default is 1.0.
Definition: CPointCloud.h:61
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:186
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: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:3538
float m_min
Buffer for min/max coords when m_colorFromDepth is true.
Definition: CPointCloud.h:299
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:80
Lightweight 3D point.
PointCloudAdapter(const mrpt::opengl::CPointCloud &obj)
Constructor (accept a const ref for convenience)
Definition: CPointCloud.h:331
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:251
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:151
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:59
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:45
#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: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019