Main MRPT website > C++ reference for MRPT 1.3.2
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-2015, 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);
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) {
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);
78  /** @} */
79 
80  /** @name PLY Export virtual methods to implement in base classes
81  @{ */
82 
83  /** In a base class, return the number of vertices */
84  virtual size_t PLY_export_get_vertex_count() const;
85 
86  /** In a base class, return the number of faces */
87  virtual size_t PLY_export_get_face_count() const { return 0; }
88 
89  /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point.
90  * \param pt_color Will be NULL if the loaded file does not provide color info.
91  */
92  virtual void PLY_export_get_vertex(
93  const size_t idx,
95  bool &pt_has_color,
96  mrpt::utils::TColorf &pt_color) const;
97 
98  /** @} */
99 
100 
101  public:
102 
103  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
104  virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
105  {
106  this->octree_getBoundingBox(bb_min, bb_max);
107  }
108 
109  /** @name Read/Write of the list of points to render
110  @{ */
111 
112  inline size_t size() const { return m_xs.size(); }
113 
114  /** Set the number of points (with contents undefined) */
115  inline void resize(size_t N) { m_xs.resize(N); m_ys.resize(N); m_zs.resize(N); m_minmax_valid = false; markAllPointsAsNew(); }
116 
117  /** Like STL std::vector's reserve */
118  inline void reserve(size_t N) { m_xs.reserve(N); m_ys.reserve(N); m_zs.reserve(N); }
119 
120  /** Set the list of (X,Y,Z) point coordinates, all at once, from three vectors with their coordinates */
121  void setAllPoints(const std::vector<float> &x, const std::vector<float> &y, const std::vector<float> &z)
122  {
123  m_xs = x;
124  m_ys = y;
125  m_zs = z;
126  m_minmax_valid = false;
127  markAllPointsAsNew();
128  }
129 
130  /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of the input vectors (via swap) */
131  void setAllPointsFast(std::vector<float> &x, std::vector<float> &y, std::vector<float> &z)
132  {
133  this->clear();
134  m_xs.swap(x);
135  m_ys.swap(y);
136  m_zs.swap(z);
137  m_minmax_valid = false;
138  markAllPointsAsNew();
139  }
140 
141  inline const std::vector<float> & getArrayX() const {return m_xs;} //!< Get a const reference to the internal array of X coordinates
142  inline const std::vector<float> & getArrayY() const {return m_ys;} //!< Get a const reference to the internal array of Y coordinates
143  inline const std::vector<float> & getArrayZ() const {return m_zs;} //!< Get a const reference to the internal array of Z coordinates
144 
145  void clear(); //!< Empty the list of points.
146 
147  /** Adds a new point to the cloud */
148  void insertPoint( float x,float y, float z );
149 
150  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
151  inline mrpt::math::TPoint3D operator [](size_t i) const {
152 #ifdef _DEBUG
153  ASSERT_BELOW_(i,size())
154 #endif
155  return mrpt::math::TPoint3D(m_xs[i],m_ys[i],m_zs[i]);
156  }
157 
158  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
159  inline mrpt::math::TPoint3D getPoint(size_t i) const {
160 #ifdef _DEBUG
161  ASSERT_BELOW_(i,size())
162 #endif
163  return mrpt::math::TPoint3D(m_xs[i],m_ys[i],m_zs[i]);
164  }
165 
166  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
167  inline mrpt::math::TPoint3Df getPointf(size_t i) const {
168 #ifdef _DEBUG
169  ASSERT_BELOW_(i,size())
170 #endif
171  return mrpt::math::TPoint3Df(m_xs[i],m_ys[i],m_zs[i]);
172  }
173 
174  /** Write an individual point (checks for "i" in the valid range only in Debug). */
175  void setPoint(size_t i, const float x,const float y, const float z);
176 
177  /** Write an individual point (without checking validity of the index). */
178  inline void setPoint_fast(size_t i, const float x,const float y, const float z)
179  {
180  m_xs[i] = x;
181  m_ys[i] = y;
182  m_zs[i] = z;
183  m_minmax_valid = false;
184  markAllPointsAsNew();
185  }
186 
187 
188  /** Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapter. */
189  template <class POINTSMAP>
190  void loadFromPointsMap( const POINTSMAP *themap);
191  // Must be implemented at the end of the header.
192 
193  /** Load the points from a list of mrpt::math::TPoint3D
194  */
195  template<class LISTOFPOINTS> void loadFromPointsList( LISTOFPOINTS &pointsList)
196  {
197  MRPT_START
198  const size_t N = pointsList.size();
199 
200  m_xs.resize(N);
201  m_ys.resize(N);
202  m_zs.resize(N);
203 
204  size_t idx;
205  typename LISTOFPOINTS::const_iterator it;
206  for ( idx=0,it=pointsList.begin() ; idx<N ; ++idx,++it)
207  {
208  m_xs[idx]=it->x;
209  m_ys[idx]=it->y;
210  m_zs[idx]=it->z;
211  }
212  markAllPointsAsNew();
213  MRPT_END
214  }
215 
216  /** Get the number of elements actually rendered in the last render event. */
217  size_t getActuallyRendered() const { return m_last_rendered_count; }
218 
219  /** @} */
220 
221 
222  /** @name Modify the appearance of the rendered points
223  @{ */
224  inline void enableColorFromX(bool v=true) { m_colorFromDepth = v ? CPointCloud::colX : CPointCloud::colNone; }
225  inline void enableColorFromY(bool v=true) { m_colorFromDepth = v ? CPointCloud::colY : CPointCloud::colNone; }
226  inline void enableColorFromZ(bool v=true) { m_colorFromDepth = v ? CPointCloud::colZ : CPointCloud::colNone; }
227 
228  inline void setPointSize(float p) { m_pointSize=p; } //!< By default is 1.0
229  inline float getPointSize() const { return m_pointSize; }
230 
231  inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
232  inline void disablePointSmooth() { m_pointSmooth=false; }
233  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
234 
235  /** Sets the colors used as extremes when colorFromDepth is enabled. */
236  void setGradientColors( const mrpt::utils::TColorf &colorMin, const mrpt::utils::TColorf &colorMax );
237 
238  /** @} */
239 
240  /** Render */
241  void render() const;
242 
243 
244  /** Render a subset of points (required by octree renderer) */
245  void render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
246 
247  private:
248  /** Constructor */
249  CPointCloud();
250 
251  /** Private, virtual destructor: only can be deleted from smart pointers */
252  virtual ~CPointCloud() { }
253 
254  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.
255  mutable mrpt::utils::TColorf m_col_slop,m_col_slop_inv; //!< Color linear function slope
256  mutable bool m_minmax_valid;
257 
258  mrpt::utils::TColorf m_colorFromDepth_min, m_colorFromDepth_max; //!< The colors used to interpolate when m_colorFromDepth is true.
259 
260  inline void internal_render_one_point(size_t i) const;
261  };
263 
264  } // end namespace
265 
266 
267  namespace utils
268  {
269  /** Specialization mrpt::utils::PointCloudAdapter<mrpt::opengl::CPointCloud> \ingroup mrpt_adapters_grp */
270  template <>
271  class PointCloudAdapter<mrpt::opengl::CPointCloud> : public detail::PointCloudAdapterHelperNoRGB<mrpt::opengl::CPointCloud,float>
272  {
273  private:
275  public:
276  typedef float coords_t; //!< The type of each point XYZ coordinates
277  static const int HAS_RGB = 0; //!< Has any color RGB info?
278  static const int HAS_RGBf = 0; //!< Has native RGB info (as floats)?
279  static const int HAS_RGBu8 = 0; //!< Has native RGB info (as uint8_t)?
280 
281  /** Constructor (accept a const ref for convenience) */
282  inline PointCloudAdapter(const mrpt::opengl::CPointCloud &obj) : m_obj(*const_cast<mrpt::opengl::CPointCloud*>(&obj)) { }
283  /** Get number of points */
284  inline size_t size() const { return m_obj.size(); }
285  /** Set number of points (to uninitialized values) */
286  inline void resize(const size_t N) { m_obj.resize(N); }
287 
288  /** Get XYZ coordinates of i'th point */
289  template <typename T>
290  inline void getPointXYZ(const size_t idx, T &x,T &y, T &z) const {
291  x=m_obj.getArrayX()[idx];
292  y=m_obj.getArrayY()[idx];
293  z=m_obj.getArrayZ()[idx];
294  }
295  /** Set XYZ coordinates of i'th point */
296  inline void setPointXYZ(const size_t idx, const coords_t x,const coords_t y, const coords_t z) {
297  m_obj.setPoint_fast(idx,x,y,z);
298  }
299 
300  }; // end of PointCloudAdapter<mrpt::opengl::CPointCloud>
301  }
302 
303  namespace opengl
304  {
305  // After declaring the adapter we can here implement this method:
306  template <class POINTSMAP>
307  void CPointCloud::loadFromPointsMap( const POINTSMAP *themap)
308  {
309  ASSERT_(themap!=NULL)
311  const mrpt::utils::PointCloudAdapter<POINTSMAP> pc_src(*themap);
312  const size_t N=pc_src.size();
313  pc_dst.resize(N);
314  for (size_t i=0;i<N;i++)
315  {
316  float x,y,z;
317  pc_src.getPointXYZ(i,x,y,z);
318  pc_dst.setPointXYZ(i,x,y,z);
319  }
320  }
321  }
322 
323 } // End of namespace
324 
325 
326 #endif
float coords_t
The type of each point XYZ coordinates.
Definition: CPointCloud.h:276
void resize(size_t N)
Set the number of points (with contents undefined)
Definition: CPointCloud.h:115
void loadFromPointsMap(const POINTSMAP *themap)
Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapte...
Definition: CPointCloud.h:307
void enableColorFromX(bool v=true)
Definition: CPointCloud.h:224
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:255
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
Definition: CPointCloud.h:217
void loadFromPointsList(LISTOFPOINTS &pointsList)
Load the points from a list of mrpt::math::TPoint3D.
Definition: CPointCloud.h:195
#define ASSERT_BELOW_(__A, __B)
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:178
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: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:167
const Scalar * const_iterator
Definition: eigen_plugins.h:24
virtual ~CPointCloud()
Private, virtual destructor: only can be deleted from smart pointers.
Definition: CPointCloud.h:252
mrpt::utils::TColorf m_colorFromDepth_min
Definition: CPointCloud.h:258
void reserve(size_t N)
Like STL std::vector&#39;s reserve.
Definition: CPointCloud.h:118
const std::vector< float > & getArrayZ() const
Get a const reference to the internal array of Z coordinates.
Definition: CPointCloud.h:143
virtual size_t PLY_export_get_face_count() const
In a base class, return the number of faces.
Definition: CPointCloud.h:87
void enableColorFromY(bool v=true)
Definition: CPointCloud.h:225
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&#39;th point.
Definition: CPointCloud.h:290
#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:228
void enableColorFromZ(bool v=true)
Definition: CPointCloud.h:226
bool isPointSmoothEnabled() const
Definition: CPointCloud.h:233
PointCloudAdapter(const mrpt::opengl::CPointCloud &obj)
Constructor (accept a const ref for convenience)
Definition: CPointCloud.h:282
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:141
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CPointCloud.h:104
size_t size() const
Definition: CPointCloud.h:112
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:296
float getPointSize() const
Definition: CPointCloud.h:229
#define MRPT_START
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 enablePointSmooth(bool enable=true)
Definition: CPointCloud.h:231
volatile size_t m_last_rendered_count_ongoing
Definition: CPointCloud.h:59
size_t size(const MATRIXLIKE &m, int dim)
Definition: bits.h:38
virtual void PLY_import_set_face_count(const size_t N)
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face.
Definition: CPointCloud.h:70
float m_pointSize
By default is 1.0.
Definition: CPointCloud.h:56
#define ASSERT_(f)
A RGB color - floats in the range [0,1].
Definition: TColor.h:52
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:159
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:142
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:131
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:286



Page generated by Doxygen 1.8.11 for MRPT 1.3.2 SVN: at Mon May 9 06:50:38 UTC 2016