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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019