Main MRPT website > C++ reference for MRPT 1.3.2
CPointCloudColoured.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_CPointCloudColoured_H
11 #define opengl_CPointCloudColoured_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( CPointCloudColoured, CRenderizable, OPENGL_IMPEXP )
26 
27 
28  /** A cloud of points, each one with an individual colour (R,G,B). The alpha component is shared by all the points and is stored in the base member m_color_A.
29  *
30  * To load from a points-map, CPointCloudColoured::loadFromPointsMap().
31  *
32  * This class uses smart optimizations while rendering to efficiently draw clouds of millions of points,
33  * as described in this page: http://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
34  *
35  * \sa opengl::COpenGLScene, opengl::CPointCloud
36  *
37  * <div align="center">
38  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
39  * <tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html preview_CPointCloudColoured.png </td> </tr>
40  * </table>
41  * </div>
42  *
43  * \ingroup mrpt_opengl_grp
44  */
46  public CRenderizable,
48  public mrpt::utils::PLY_Importer,
49  public mrpt::utils::PLY_Exporter
50  {
51  DEFINE_SERIALIZABLE( CPointCloudColoured )
52 
53  public:
54  struct TPointColour
55  {
56  inline TPointColour() { }
57  inline TPointColour(float _x,float _y,float _z,float _R,float _G,float _B ) : x(_x),y(_y),z(_z),R(_R),G(_G),B(_B) { }
58  float x,y,z,R,G,B; // Float is precission enough for rendering
59  };
60 
61  private:
62  typedef std::vector<TPointColour> TListPointColour;
63  TListPointColour m_points;
64 
67  inline iterator begin() { return m_points.begin(); }
68  inline const_iterator begin() const { return m_points.begin(); }
69  inline iterator end() { return m_points.end(); }
70  inline const_iterator end() const { return m_points.end(); }
71 
72 
73  float m_pointSize; //!< By default is 1.0
74  bool m_pointSmooth; //!< Default: false
75  mutable volatile size_t m_last_rendered_count, m_last_rendered_count_ongoing;
76 
77  /** Constructor
78  */
80  m_points(),
81  m_pointSize(1),
82  m_pointSmooth(false),
83  m_last_rendered_count(0),
84  m_last_rendered_count_ongoing(0)
85  {
86  }
87  /** Private, virtual destructor: only can be deleted from smart pointers */
88  virtual ~CPointCloudColoured() { }
89 
90  void markAllPointsAsNew(); //!< Do needed internal work if all points are new (octree rebuilt,...)
91 
92  public:
93 
94  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
95  virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
96  {
97  this->octree_getBoundingBox(bb_min, bb_max);
98  }
99 
100  /** @name Read/Write of the list of points to render
101  @{ */
102 
103  /** Inserts a new point into the point cloud. */
104  void push_back(float x,float y,float z, float R, float G, float B);
105 
106  /** Set the number of points, with undefined contents */
107  inline void resize(size_t N) { m_points.resize(N); markAllPointsAsNew(); }
108 
109  /** Like STL std::vector's reserve */
110  inline void reserve(size_t N) { m_points.reserve(N); }
111 
112  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
113  inline const TPointColour &operator [](size_t i) const {
114 #ifdef _DEBUG
115  ASSERT_BELOW_(i,size())
116 #endif
117  return m_points[i];
118  }
119 
120  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
121  inline const TPointColour &getPoint(size_t i) const {
122 #ifdef _DEBUG
123  ASSERT_BELOW_(i,size())
124 #endif
125  return m_points[i];
126  }
127 
128  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
129  inline mrpt::math::TPoint3Df getPointf(size_t i) const {
130 #ifdef _DEBUG
131  ASSERT_BELOW_(i,size())
132 #endif
133  return mrpt::math::TPoint3Df(m_points[i].x,m_points[i].y,m_points[i].z);
134  }
135 
136  /** Write an individual point (checks for "i" in the valid range only in Debug). */
137  void setPoint(size_t i, const TPointColour &p );
138 
139  /** Like \a setPoint() but does not check for index out of bounds */
140  inline void setPoint_fast(const size_t i, const TPointColour &p ) {
141  m_points[i] = p;
142  markAllPointsAsNew();
143  }
144 
145  /** Like \a setPoint() but does not check for index out of bounds */
146  inline void setPoint_fast(const size_t i, const float x,const float y, const float z ) {
147  TPointColour &p = m_points[i];
148  p.x=x; p.y=y; p.z=z;
149  markAllPointsAsNew();
150  }
151 
152  /** Like \c setPointColor but without checking for out-of-index erors */
153  inline void setPointColor_fast(size_t index,float R, float G, float B)
154  {
155  m_points[index].R=R;
156  m_points[index].G=G;
157  m_points[index].B=B;
158  }
159  /** Like \c getPointColor but without checking for out-of-index erors */
160  inline void getPointColor_fast( size_t index, float &R, float &G, float &B ) const
161  {
162  R = m_points[index].R;
163  G = m_points[index].G;
164  B = m_points[index].B;
165  }
166 
167  inline size_t size() const { return m_points.size(); } //!< Return the number of points
168 
169  inline void clear() { m_points.clear(); markAllPointsAsNew(); } //!< Erase all the points
170 
171  /** Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapter. */
172  template <class POINTSMAP>
173  void loadFromPointsMap( const POINTSMAP *themap);
174  // Must be implemented at the end of the header.
175 
176  /** Get the number of elements actually rendered in the last render event. */
177  size_t getActuallyRendered() const { return m_last_rendered_count; }
178 
179  /** @} */
180 
181 
182  /** @name Modify the appearance of the rendered points
183  @{ */
184 
185  inline void setPointSize(float pointSize) { m_pointSize = pointSize; }
186  inline float getPointSize() const { return m_pointSize; }
187 
188  inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
189  inline void disablePointSmooth() { m_pointSmooth=false; }
190  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
191 
192  /** @} */
193 
194 
195  /** Render */
196  void render() const;
197 
198  /** Render a subset of points (required by octree renderer) */
199  void render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
200 
201  protected:
202  /** @name PLY Import virtual methods to implement in base classes
203  @{ */
204  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
205  virtual void PLY_import_set_vertex_count(const size_t N);
206  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face */
207  virtual void PLY_import_set_face_count(const size_t N) {
209  }
210  /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
211  * \param pt_color Will be NULL if the loaded file does not provide color info.
212  */
213  virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL);
214  /** @} */
215 
216  /** @name PLY Export virtual methods to implement in base classes
217  @{ */
218 
219  /** In a base class, return the number of vertices */
220  virtual size_t PLY_export_get_vertex_count() const;
221 
222  /** In a base class, return the number of faces */
223  virtual size_t PLY_export_get_face_count() const { return 0; }
224 
225  /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point.
226  * \param pt_color Will be NULL if the loaded file does not provide color info.
227  */
228  virtual void PLY_export_get_vertex(
229  const size_t idx,
231  bool &pt_has_color,
232  mrpt::utils::TColorf &pt_color) const;
233 
234  /** @} */
235 
236 
237  };
239 
242 
243  } // end namespace
244 
245  namespace utils
246  {
247  // Specialization must occur in the same namespace
249  }
250 
251  namespace utils
252  {
253  /** Specialization mrpt::utils::PointCloudAdapter<mrpt::opengl::CPointCloudColoured> \ingroup mrpt_adapters_grp*/
254  template <>
256  {
257  private:
259  public:
260  typedef float coords_t; //!< The type of each point XYZ coordinates
261  static const int HAS_RGB = 1; //!< Has any color RGB info?
262  static const int HAS_RGBf = 1; //!< 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::CPointCloudColoured &obj) : m_obj(*const_cast<mrpt::opengl::CPointCloudColoured*>(&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 {
276  x=pc.x;
277  y=pc.y;
278  z=pc.z;
279  }
280  /** Set XYZ coordinates of i'th point */
281  inline void setPointXYZ(const size_t idx, const coords_t x,const coords_t y, const coords_t z) {
282  m_obj.setPoint_fast(idx, x,y,z);
283  }
284 
285  /** Get XYZ_RGBf coordinates of i'th point */
286  template <typename T>
287  inline void getPointXYZ_RGBf(const size_t idx, T &x,T &y, T &z, float &r,float &g,float &b) const {
289  x=pc.x; y=pc.y; z=pc.z;
290  r=pc.R; g=pc.G; b=pc.B;
291  }
292  /** Set XYZ_RGBf coordinates of i'th point */
293  inline void setPointXYZ_RGBf(const size_t idx, const coords_t x,const coords_t y, const coords_t z, const float r,const float g,const float b) {
295  }
296 
297  /** Get XYZ_RGBu8 coordinates of i'th point */
298  template <typename T>
299  inline void getPointXYZ_RGBu8(const size_t idx, T &x,T &y, T &z, uint8_t &r,uint8_t &g,uint8_t &b) const {
301  x=pc.x; y=pc.y; z=pc.z;
302  r=pc.R*255; g=pc.G*255; b=pc.B*255;
303  }
304  /** Set XYZ_RGBu8 coordinates of i'th point */
305  inline void setPointXYZ_RGBu8(const size_t idx, const coords_t x,const coords_t y, const coords_t z, const uint8_t r,const uint8_t g,const uint8_t b) {
306  m_obj.setPoint_fast(idx, mrpt::opengl::CPointCloudColoured::TPointColour(x,y,z,r/255.f,g/255.f,b/255.f) );
307  }
308 
309  /** Get RGBf color of i'th point */
310  inline void getPointRGBf(const size_t idx, float &r,float &g,float &b) const { m_obj.getPointColor_fast(idx,r,g,b); }
311  /** Set XYZ_RGBf coordinates of i'th point */
312  inline void setPointRGBf(const size_t idx, const float r,const float g,const float b) { m_obj.setPointColor_fast(idx,r,g,b); }
313 
314  /** Get RGBu8 color of i'th point */
315  inline void getPointRGBu8(const size_t idx, uint8_t &r,uint8_t &g,uint8_t &b) const {
316  float R,G,B;
317  m_obj.getPointColor_fast(idx,R,G,B);
318  r=R*255; g=G*255; b=B*255;
319  }
320  /** Set RGBu8 coordinates of i'th point */
321  inline void setPointRGBu8(const size_t idx,const uint8_t r,const uint8_t g,const uint8_t b) {
322  m_obj.setPointColor_fast(idx,r/255.f,g/255.f,b/255.f);
323  }
324 
325  }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
326 
327  }
328 
329  namespace opengl
330  {
331  // After declaring the adapter we can here implement this method:
332  template <class POINTSMAP>
333  void CPointCloudColoured::loadFromPointsMap( const POINTSMAP *themap)
334  {
336  const mrpt::utils::PointCloudAdapter<POINTSMAP> pc_src(*themap);
337  const size_t N=pc_src.size();
338  pc_dst.resize(N);
339  for (size_t i=0;i<N;i++)
340  {
341  float x,y,z,r,g,b;
342  pc_src.getPointXYZ_RGBf(i,x,y,z,r,g,b);
343  pc_dst.setPointXYZ_RGBf(i,x,y,z,r,g,b);
344  }
345  }
346  }
347 
348 } // End of namespace
349 
350 
351 #endif
#define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS)
Definition: TTypeName.h:64
PointCloudAdapter(const mrpt::opengl::CPointCloudColoured &obj)
Constructor (accept a const ref for convenience)
OPENGL_IMPEXP mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
void clear()
Erase all the points.
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CAngularObservationMeshPtr &pObj)
void getPointXYZ_RGBf(const size_t idx, T &x, T &y, T &z, float &r, float &g, float &b) const
Get XYZ_RGBf coordinates of i&#39;th point.
A cloud of points, each one with an individual colour (R,G,B).
#define ASSERT_BELOW_(__A, __B)
Scalar * iterator
Definition: eigen_plugins.h:23
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:44
EIGEN_STRONG_INLINE void push_back(Scalar val)
Insert an element at the end of the container (for 1D vectors/arrays)
void setPointXYZ_RGBf(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const float r, const float g, const float b)
Set XYZ_RGBf coordinates of i&#39;th point.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
mrpt::math::TPoint3Df getPointf(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
void getPointColor_fast(size_t index, float &R, float &G, float &B) const
Like getPointColor but without checking for out-of-index erors.
void resize(const size_t N)
Set number of points (to uninitialized values)
void setPointRGBu8(const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
Set RGBu8 coordinates of i&#39;th point.
void loadFromPointsMap(const POINTSMAP *themap)
Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapte...
Template class that implements the data structure and algorithms for Octree-based efficient rendering...
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...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
TPointColour(float _x, float _y, float _z, float _R, float _G, float _B)
#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...
virtual size_t PLY_export_get_face_count() const
In a base class, return the number of faces.
Lightweight 3D point (float version).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
TListPointColour::iterator iterator
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
An adapter to different kinds of point cloud object.
void setPointRGBf(const size_t idx, const float r, const float g, const float b)
Set XYZ_RGBf coordinates of i&#39;th point.
void setPointXYZ_RGBu8(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const uint8_t r, const uint8_t g, const uint8_t b)
Set XYZ_RGBu8 coordinates of i&#39;th point.
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.
void enablePointSmooth(bool enable=true)
void getPointRGBu8(const size_t idx, uint8_t &r, uint8_t &g, uint8_t &b) const
Get RGBu8 color of i&#39;th point.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t size() const
Return the number of points.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
std::vector< TPointColour > TListPointColour
void setPointColor_fast(size_t index, float R, float G, float B)
Like setPointColor but without checking for out-of-index erors.
TListPointColour::const_iterator const_iterator
size_t size(const MATRIXLIKE &m, int dim)
Definition: bits.h:38
The namespace for 3D scene representation and rendering.
A RGB color - floats in the range [0,1].
Definition: TColor.h:52
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.
void setPoint_fast(const size_t i, const TPointColour &p)
Like setPoint() but does not check for index out of bounds.
An adapter to different kinds of point cloud object.
Definition: adapters.h:38
void getPointXYZ_RGBu8(const size_t idx, T &x, T &y, T &z, uint8_t &r, uint8_t &g, uint8_t &b) const
Get XYZ_RGBu8 coordinates of i&#39;th point.
void setPoint_fast(const size_t i, const float x, const float y, const float z)
Like setPoint() but does not check for index out of bounds.
Lightweight 3D point.
float m_pointSize
By default is 1.0.
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
void resize(size_t N)
Set the number of points, with undefined contents.
void getPointRGBf(const size_t idx, float &r, float &g, float &b) const
Get RGBf color of i&#39;th point.
void reserve(size_t N)
Like STL std::vector&#39;s reserve.
const TPointColour & getPoint(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
virtual ~CPointCloudColoured()
Private, virtual destructor: only can be deleted from smart pointers.



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