Main MRPT website > C++ reference for MRPT 1.5.6
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 
23 
24  // This must be added to any CSerializable derived class:
25  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CPointCloud, CRenderizable, OPENGL_IMPEXP )
26 
27 
28  /** A cloud of points, all with the same color or each depending on its value along a particular coordinate axis.
29  * This class is just an OpenGL representation of a point cloud. For operating with maps of points, see mrpt::maps::CPointsMap and derived classes.
30  *
31  * To load from a points-map, CPointCloud::loadFromPointsMap().
32  *
33  * This class uses smart optimizations while rendering to efficiently draw clouds of millions of points,
34  * as described in this page: http://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
35  *
36  * \sa opengl::CPlanarLaserScan, opengl::COpenGLScene, opengl::CPointCloudColoured, mrpt::maps::CPointsMap
37  *
38  * <div align="center">
39  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
40  * <tr> <td> mrpt::opengl::CPointCloud </td> <td> \image html preview_CPointCloud.png </td> </tr>
41  * </table>
42  * </div>
43  *
44  * \ingroup mrpt_opengl_grp
45  */
47  public CRenderizable,
49  public mrpt::utils::PLY_Importer,
50  public mrpt::utils::PLY_Exporter
51  {
52  DEFINE_SERIALIZABLE( CPointCloud )
53  protected:
54  enum Axis { colNone=0, colZ, colY, colX} m_colorFromDepth;
55  std::vector<float> m_xs,m_ys,m_zs;
56  float m_pointSize; //!< By default is 1.0
57  bool m_pointSmooth; //!< Default: false
58 
59  mutable volatile size_t m_last_rendered_count, m_last_rendered_count_ongoing;
60 
61  void markAllPointsAsNew(); //!< Do needed internal work if all points are new (octree rebuilt,...)
62 
63  protected:
64  /** @name PLY Import virtual methods to implement in base classes
65  @{ */
66  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
67  virtual void PLY_import_set_vertex_count(const size_t N) MRPT_OVERRIDE;
68 
69  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face */
70  virtual void PLY_import_set_face_count(const size_t N) MRPT_OVERRIDE {
72  }
73 
74  /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
75  * \param pt_color Will be NULL if the loaded file does not provide color info.
76  */
77  virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL) MRPT_OVERRIDE;
78  /** @} */
79 
80  /** @name PLY Export virtual methods to implement in base classes
81  @{ */
82  size_t PLY_export_get_vertex_count() const MRPT_OVERRIDE;
83  size_t PLY_export_get_face_count() const MRPT_OVERRIDE { return 0; }
84  void PLY_export_get_vertex(const size_t idx,mrpt::math::TPoint3Df &pt,bool &pt_has_color,mrpt::utils::TColorf &pt_color) const MRPT_OVERRIDE;
85  /** @} */
86 
87  public:
88  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
89  virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
90  {
91  this->octree_getBoundingBox(bb_min, bb_max);
92  }
93 
94  /** @name Read/Write of the list of points to render
95  @{ */
96 
97  inline size_t size() const { return m_xs.size(); }
98 
99  /** Set the number of points (with contents undefined) */
100  inline void resize(size_t N) { m_xs.resize(N); m_ys.resize(N); m_zs.resize(N); m_minmax_valid = false; markAllPointsAsNew(); }
101 
102  /** Like STL std::vector's reserve */
103  inline void reserve(size_t N) { m_xs.reserve(N); m_ys.reserve(N); m_zs.reserve(N); }
104 
105  /** Set the list of (X,Y,Z) point coordinates, all at once, from three vectors with their coordinates */
106  void setAllPoints(const std::vector<float> &x, const std::vector<float> &y, const std::vector<float> &z)
107  {
108  m_xs = x;
109  m_ys = y;
110  m_zs = z;
111  m_minmax_valid = false;
112  markAllPointsAsNew();
113  }
114 
115  /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of the input vectors (via swap) */
116  void setAllPointsFast(std::vector<float> &x, std::vector<float> &y, std::vector<float> &z)
117  {
118  this->clear();
119  m_xs.swap(x);
120  m_ys.swap(y);
121  m_zs.swap(z);
122  m_minmax_valid = false;
123  markAllPointsAsNew();
124  }
125 
126  inline const std::vector<float> & getArrayX() const {return m_xs;} //!< Get a const reference to the internal array of X coordinates
127  inline const std::vector<float> & getArrayY() const {return m_ys;} //!< Get a const reference to the internal array of Y coordinates
128  inline const std::vector<float> & getArrayZ() const {return m_zs;} //!< Get a const reference to the internal array of Z coordinates
129 
130  void clear(); //!< Empty the list of points.
131 
132  /** Adds a new point to the cloud */
133  void insertPoint( float x,float y, float z );
134 
135  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
136  inline mrpt::math::TPoint3D operator [](size_t i) const {
137 #ifdef _DEBUG
138  ASSERT_BELOW_(i,size())
139 #endif
140  return mrpt::math::TPoint3D(m_xs[i],m_ys[i],m_zs[i]);
141  }
142 
143  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
144  inline mrpt::math::TPoint3D getPoint(size_t i) const {
145 #ifdef _DEBUG
146  ASSERT_BELOW_(i,size())
147 #endif
148  return mrpt::math::TPoint3D(m_xs[i],m_ys[i],m_zs[i]);
149  }
150 
151  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
152  inline mrpt::math::TPoint3Df getPointf(size_t i) const {
153 #ifdef _DEBUG
154  ASSERT_BELOW_(i,size())
155 #endif
156  return mrpt::math::TPoint3Df(m_xs[i],m_ys[i],m_zs[i]);
157  }
158 
159  /** Write an individual point (checks for "i" in the valid range only in Debug). */
160  void setPoint(size_t i, const float x,const float y, const float z);
161 
162  /** Write an individual point (without checking validity of the index). */
163  inline void setPoint_fast(size_t i, const float x,const float y, const float z)
164  {
165  m_xs[i] = x;
166  m_ys[i] = y;
167  m_zs[i] = z;
168  m_minmax_valid = false;
169  markAllPointsAsNew();
170  }
171 
172 
173  /** Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapter. */
174  template <class POINTSMAP>
175  void loadFromPointsMap( const POINTSMAP *themap);
176  // Must be implemented at the end of the header.
177 
178  /** Load the points from a list of mrpt::math::TPoint3D
179  */
180  template<class LISTOFPOINTS> void loadFromPointsList( LISTOFPOINTS &pointsList)
181  {
182  MRPT_START
183  const size_t N = pointsList.size();
184 
185  m_xs.resize(N);
186  m_ys.resize(N);
187  m_zs.resize(N);
188 
189  size_t idx;
190  typename LISTOFPOINTS::const_iterator it;
191  for ( idx=0,it=pointsList.begin() ; idx<N ; ++idx,++it)
192  {
193  m_xs[idx]=it->x;
194  m_ys[idx]=it->y;
195  m_zs[idx]=it->z;
196  }
197  markAllPointsAsNew();
198  MRPT_END
199  }
200 
201  /** Get the number of elements actually rendered in the last render event. */
202  size_t getActuallyRendered() const { return m_last_rendered_count; }
203 
204  /** @} */
205 
206 
207  /** @name Modify the appearance of the rendered points
208  @{ */
209  inline void enableColorFromX(bool v=true) { m_colorFromDepth = v ? CPointCloud::colX : CPointCloud::colNone; }
210  inline void enableColorFromY(bool v=true) { m_colorFromDepth = v ? CPointCloud::colY : CPointCloud::colNone; }
211  inline void enableColorFromZ(bool v=true) { m_colorFromDepth = v ? CPointCloud::colZ : CPointCloud::colNone; }
212 
213  inline void setPointSize(float p) { m_pointSize=p; } //!< By default is 1.0
214  inline float getPointSize() const { return m_pointSize; }
215 
216  inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
217  inline void disablePointSmooth() { m_pointSmooth=false; }
218  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
219 
220  /** Sets the colors used as extremes when colorFromDepth is enabled. */
221  void setGradientColors( const mrpt::utils::TColorf &colorMin, const mrpt::utils::TColorf &colorMax );
222 
223  /** @} */
224 
225  /** Render */
226  void render() const MRPT_OVERRIDE;
227 
228  /** Render a subset of points (required by octree renderer) */
229  void render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
230 
231  private:
232  /** Constructor */
233  CPointCloud();
234 
235  /** Private, virtual destructor: only can be deleted from smart pointers */
236  virtual ~CPointCloud() { }
237 
238  mutable float m_min, m_max,m_max_m_min,m_max_m_min_inv; //!< Buffer for min/max coords when m_colorFromDepth is true.
239  mutable mrpt::utils::TColorf m_col_slop,m_col_slop_inv; //!< Color linear function slope
240  mutable bool m_minmax_valid;
241 
242  mrpt::utils::TColorf m_colorFromDepth_min, m_colorFromDepth_max; //!< The colors used to interpolate when m_colorFromDepth is true.
243 
244  inline void internal_render_one_point(size_t i) const;
245  };
247 
248  } // end namespace
249 
250 
251  namespace utils
252  {
253  /** Specialization mrpt::utils::PointCloudAdapter<mrpt::opengl::CPointCloud> \ingroup mrpt_adapters_grp */
254  template <>
255  class PointCloudAdapter<mrpt::opengl::CPointCloud> : public detail::PointCloudAdapterHelperNoRGB<mrpt::opengl::CPointCloud,float>
256  {
257  private:
259  public:
260  typedef float coords_t; //!< The type of each point XYZ coordinates
261  static const int HAS_RGB = 0; //!< Has any color RGB info?
262  static const int HAS_RGBf = 0; //!< Has native RGB info (as floats)?
263  static const int HAS_RGBu8 = 0; //!< Has native RGB info (as uint8_t)?
264 
265  /** Constructor (accept a const ref for convenience) */
266  inline PointCloudAdapter(const mrpt::opengl::CPointCloud &obj) : m_obj(*const_cast<mrpt::opengl::CPointCloud*>(&obj)) { }
267  /** Get number of points */
268  inline size_t size() const { return m_obj.size(); }
269  /** Set number of points (to uninitialized values) */
270  inline void resize(const size_t N) { m_obj.resize(N); }
271 
272  /** Get XYZ coordinates of i'th point */
273  template <typename T>
274  inline void getPointXYZ(const size_t idx, T &x,T &y, T &z) const {
275  x=m_obj.getArrayX()[idx];
276  y=m_obj.getArrayY()[idx];
277  z=m_obj.getArrayZ()[idx];
278  }
279  /** Set XYZ coordinates of i'th point */
280  inline void setPointXYZ(const size_t idx, const coords_t x,const coords_t y, const coords_t z) {
281  m_obj.setPoint_fast(idx,x,y,z);
282  }
283 
284  /** Set XYZ coordinates of i'th point */
285  inline void setInvalidPoint(const size_t idx) {
286  THROW_EXCEPTION("mrpt::opengl::CPointCloud needs to be dense");
287  }
288 
289  }; // end of PointCloudAdapter<mrpt::opengl::CPointCloud>
290  }
291 
292  namespace opengl
293  {
294  // After declaring the adapter we can here implement this method:
295  template <class POINTSMAP>
296  void CPointCloud::loadFromPointsMap( const POINTSMAP *themap)
297  {
298  ASSERT_(themap!=NULL)
300  const mrpt::utils::PointCloudAdapter<POINTSMAP> pc_src(*themap);
301  const size_t N=pc_src.size();
302  pc_dst.resize(N);
303  for (size_t i=0;i<N;i++)
304  {
305  float x,y,z;
306  pc_src.getPointXYZ(i,x,y,z);
307  pc_dst.setPointXYZ(i,x,y,z);
308  }
309  }
310  }
311 
312 } // End of namespace
313 
314 
315 #endif
const GLdouble * v
Definition: glew.h:1296
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1166
float coords_t
The type of each point XYZ coordinates.
Definition: CPointCloud.h:260
void resize(size_t N)
Set the number of points (with contents undefined)
Definition: CPointCloud.h:100
void loadFromPointsMap(const POINTSMAP *themap)
Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapte...
Definition: CPointCloud.h:296
void enableColorFromX(bool v=true)
Definition: CPointCloud.h:209
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
A helper base class for those PointCloudAdapter<> which do not handle RGB data; it declares needed in...
Definition: adapters.h:48
mrpt::utils::TColorf m_col_slop_inv
Color linear function slope.
Definition: CPointCloud.h:239
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
Definition: CPointCloud.h:202
#define THROW_EXCEPTION(msg)
void loadFromPointsList(LISTOFPOINTS &pointsList)
Load the points from a list of mrpt::math::TPoint3D.
Definition: CPointCloud.h:180
#define ASSERT_BELOW_(__A, __B)
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const MRPT_OVERRIDE
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CPointCloud.h:89
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:163
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:44
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: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:152
const Scalar * const_iterator
Definition: eigen_plugins.h:24
mrpt::utils::TColorf m_colorFromDepth_min
Definition: CPointCloud.h:242
void reserve(size_t N)
Like STL std::vector's reserve.
Definition: CPointCloud.h:103
const std::vector< float > & getArrayZ() const
Get a const reference to the internal array of Z coordinates.
Definition: CPointCloud.h:128
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
void enableColorFromY(bool v=true)
Definition: CPointCloud.h:210
bool m_pointSmooth
Default: false.
Definition: CPointCloud.h:57
Template class that implements the data structure and algorithms for Octree-based efficient rendering...
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i'th point.
Definition: CPointCloud.h:274
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
Lightweight 3D point (float version).
#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:213
void enableColorFromZ(bool v=true)
Definition: CPointCloud.h:211
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn't exist already.
Definition: ts_hash_map.h:123
GLint GLint GLint GLint GLint x
Definition: glew.h:1166
bool isPointSmoothEnabled() const
Definition: CPointCloud.h:218
GLhandleARB obj
Definition: glew.h:3276
PointCloudAdapter(const mrpt::opengl::CPointCloud &obj)
Constructor (accept a const ref for convenience)
Definition: CPointCloud.h:266
An adapter to different kinds of point cloud object.
const std::vector< float > & getArrayX() const
Get a const reference to the internal array of X coordinates.
Definition: CPointCloud.h:126
GLfloat GLfloat p
Definition: glew.h:10113
size_t size() const
Definition: CPointCloud.h:97
void setPointXYZ(const size_t idx, const coords_t x, const coords_t y, const coords_t z)
Set XYZ coordinates of i'th point.
Definition: CPointCloud.h:280
float getPointSize() const
Definition: CPointCloud.h:214
GLsizeiptr size
Definition: glew.h:1586
#define MRPT_START
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
GLdouble GLdouble z
Definition: glew.h:1464
void enablePointSmooth(bool enable=true)
Definition: CPointCloud.h:216
volatile size_t m_last_rendered_count_ongoing
Definition: CPointCloud.h:59
float m_pointSize
By default is 1.0.
Definition: CPointCloud.h:56
void setInvalidPoint(const size_t idx)
Set XYZ coordinates of i'th point.
Definition: CPointCloud.h:285
#define ASSERT_(f)
A RGB color - floats in the range [0,1].
Definition: TColor.h:80
virtual void PLY_import_set_face_count(const size_t N) MRPT_OVERRIDE
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face.
Definition: CPointCloud.h:70
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:144
An adapter to different kinds of point cloud object.
Definition: adapters.h:38
const std::vector< float > & getArrayY() const
Get a const reference to the internal array of Y coordinates.
Definition: CPointCloud.h:127
Lightweight 3D point.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
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:116
std::vector< float > m_zs
Definition: CPointCloud.h:55
A cloud of points, all with the same color or each depending on its value along a particular coordina...
Definition: CPointCloud.h:46
void resize(const size_t N)
Set number of points (to uninitialized values)
Definition: CPointCloud.h:270



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018