Main MRPT website > C++ reference for MRPT 1.5.7
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-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_CPointCloudColoured_H
11 #define opengl_CPointCloudColoured_H
12 
16 #include <mrpt/utils/adapters.h>
17 #include <mrpt/utils/color_maps.h>
18 
19 namespace mrpt
20 {
21  namespace opengl
22  {
23  // This must be added to any CSerializable derived class:
24  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CPointCloudColoured, CRenderizable, OPENGL_IMPEXP )
25  /** 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.
26  *
27  * To load from a points-map, CPointCloudColoured::loadFromPointsMap().
28  *
29  * This class uses smart optimizations while rendering to efficiently draw clouds of millions of points,
30  * as described in this page: http://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
31  *
32  * \sa opengl::COpenGLScene, opengl::CPointCloud
33  *
34  * <div align="center">
35  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
36  * <tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html preview_CPointCloudColoured.png </td> </tr>
37  * </table>
38  * </div>
39  *
40  * \ingroup mrpt_opengl_grp
41  */
43  public CRenderizable,
45  public mrpt::utils::PLY_Importer,
46  public mrpt::utils::PLY_Exporter
47  {
49 
50  public:
51  struct TPointColour
52  {
53  inline TPointColour() { }
54  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) { }
55  float x,y,z,R,G,B; // Float is precission enough for rendering
56  };
57 
58  private:
59  typedef std::vector<TPointColour> TListPointColour;
61 
64  inline iterator begin() { return m_points.begin(); }
65  inline const_iterator begin() const { return m_points.begin(); }
66  inline iterator end() { return m_points.end(); }
67  inline const_iterator end() const { return m_points.end(); }
68 
69 
70  float m_pointSize; //!< By default is 1.0
71  bool m_pointSmooth; //!< Default: false
72  mutable volatile size_t m_last_rendered_count, m_last_rendered_count_ongoing;
73 
74  /** Constructor
75  */
77  m_points(),
78  m_pointSize(1),
79  m_pointSmooth(false),
80  m_last_rendered_count(0),
81  m_last_rendered_count_ongoing(0)
82  {
83  }
84  /** Private, virtual destructor: only can be deleted from smart pointers */
85  virtual ~CPointCloudColoured() { }
86 
87  void markAllPointsAsNew(); //!< Do needed internal work if all points are new (octree rebuilt,...)
88 
89  public:
90 
91  /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
93  {
94  this->octree_getBoundingBox(bb_min, bb_max);
95  }
96 
97  /** @name Read/Write of the list of points to render
98  @{ */
99 
100  /** Inserts a new point into the point cloud. */
101  void push_back(float x,float y,float z, float R, float G, float B);
102 
103  /** Set the number of points, with undefined contents */
104  inline void resize(size_t N) { m_points.resize(N); markAllPointsAsNew(); }
105 
106  /** Like STL std::vector's reserve */
107  inline void reserve(size_t N) { m_points.reserve(N); }
108 
109  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
110  inline const TPointColour &operator [](size_t i) const {
111 #ifdef _DEBUG
112  ASSERT_BELOW_(i,size())
113 #endif
114  return m_points[i];
115  }
116 
117  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
118  inline const TPointColour &getPoint(size_t i) const {
119 #ifdef _DEBUG
120  ASSERT_BELOW_(i,size())
121 #endif
122  return m_points[i];
123  }
124 
125  /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
126  inline mrpt::math::TPoint3Df getPointf(size_t i) const {
127 #ifdef _DEBUG
128  ASSERT_BELOW_(i,size())
129 #endif
130  return mrpt::math::TPoint3Df(m_points[i].x,m_points[i].y,m_points[i].z);
131  }
132 
133  /** Write an individual point (checks for "i" in the valid range only in Debug). */
134  void setPoint(size_t i, const TPointColour &p );
135 
136  /** Like \a setPoint() but does not check for index out of bounds */
137  inline void setPoint_fast(const size_t i, const TPointColour &p ) {
138  m_points[i] = p;
139  markAllPointsAsNew();
140  }
141 
142  /** Like \a setPoint() but does not check for index out of bounds */
143  inline void setPoint_fast(const size_t i, const float x,const float y, const float z ) {
144  TPointColour &p = m_points[i];
145  p.x=x; p.y=y; p.z=z;
146  markAllPointsAsNew();
147  }
148 
149  /** Like \c setPointColor but without checking for out-of-index erors */
150  inline void setPointColor_fast(size_t index,float R, float G, float B)
151  {
152  m_points[index].R=R;
153  m_points[index].G=G;
154  m_points[index].B=B;
155  }
156  /** Like \c getPointColor but without checking for out-of-index erors */
157  inline void getPointColor_fast( size_t index, float &R, float &G, float &B ) const
158  {
159  R = m_points[index].R;
160  G = m_points[index].G;
161  B = m_points[index].B;
162  }
163 
164  inline size_t size() const { return m_points.size(); } //!< Return the number of points
165 
166  inline void clear() { m_points.clear(); markAllPointsAsNew(); } //!< Erase all the points
167 
168  /** Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapter. */
169  template <class POINTSMAP>
170  void loadFromPointsMap( const POINTSMAP *themap);
171  // Must be implemented at the end of the header.
172 
173  /** Get the number of elements actually rendered in the last render event. */
174  size_t getActuallyRendered() const { return m_last_rendered_count; }
175 
176  /** @} */
177 
178 
179  /** @name Modify the appearance of the rendered points
180  @{ */
181 
182  inline void setPointSize(float pointSize) { m_pointSize = pointSize; }
183  inline float getPointSize() const { return m_pointSize; }
184 
185  inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
186  inline void disablePointSmooth() { m_pointSmooth=false; }
187  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
188 
189  /** Regenerates the color of each point according the one coordinate (coord_index:0,1,2 for X,Y,Z) and the given color map. */
190  void recolorizeByCoordinate(const float coord_min, const float coord_max, const int coord_index = 2, const mrpt::utils::TColormap color_map = mrpt::utils::cmJET );
191  /** @} */
192 
193  /** Render */
194  void render() const MRPT_OVERRIDE;
195 
196  /** Render a subset of points (required by octree renderer) */
197  void render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
198 
199  protected:
200  /** @name PLY Import virtual methods to implement in base classes
201  @{ */
202  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
203  virtual void PLY_import_set_vertex_count(const size_t N) MRPT_OVERRIDE;
204  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face */
205  virtual void PLY_import_set_face_count(const size_t N) MRPT_OVERRIDE {
207  }
208  /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
209  * \param pt_color Will be NULL if the loaded file does not provide color info.
210  */
211  virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL) MRPT_OVERRIDE;
212  /** @} */
213 
214  /** @name PLY Export virtual methods to implement in base classes
215  @{ */
216  size_t PLY_export_get_vertex_count() const MRPT_OVERRIDE;
217  size_t PLY_export_get_face_count() const MRPT_OVERRIDE { return 0; }
218  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;
219  /** @} */
220  };
221  DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE( CPointCloudColoured, CRenderizable, OPENGL_IMPEXP )
222 
223  OPENGL_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in, CPointCloudColoured::TPointColour &o);
224  OPENGL_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out, const CPointCloudColoured::TPointColour &o);
225 
226  } // end namespace
227 
228  namespace utils
229  {
230  // Specialization must occur in the same namespace
231  MRPT_DECLARE_TTYPENAME_NAMESPACE(CPointCloudColoured::TPointColour, mrpt::opengl)
232  }
233 
234  namespace utils
235  {
236  /** Specialization mrpt::utils::PointCloudAdapter<mrpt::opengl::CPointCloudColoured> \ingroup mrpt_adapters_grp*/
237  template <>
239  {
240  private:
242  public:
243  typedef float coords_t; //!< The type of each point XYZ coordinates
244  static const int HAS_RGB = 1; //!< Has any color RGB info?
245  static const int HAS_RGBf = 1; //!< Has native RGB info (as floats)?
246  static const int HAS_RGBu8 = 0; //!< Has native RGB info (as uint8_t)?
247 
248  /** Constructor (accept a const ref for convenience) */
249  inline PointCloudAdapter(const mrpt::opengl::CPointCloudColoured &obj) : m_obj(*const_cast<mrpt::opengl::CPointCloudColoured*>(&obj)) { }
250  /** Get number of points */
251  inline size_t size() const { return m_obj.size(); }
252  /** Set number of points (to uninitialized values) */
253  inline void resize(const size_t N) { m_obj.resize(N); }
254 
255  /** Get XYZ coordinates of i'th point */
256  template <typename T>
257  inline void getPointXYZ(const size_t idx, T &x,T &y, T &z) const {
259  x=pc.x;
260  y=pc.y;
261  z=pc.z;
262  }
263  /** Set XYZ coordinates of i'th point */
264  inline void setPointXYZ(const size_t idx, const coords_t x,const coords_t y, const coords_t z) {
265  m_obj.setPoint_fast(idx, x,y,z);
266  }
267 
268  inline void setInvalidPoint(const size_t idx)
269  {
270  THROW_EXCEPTION("mrpt::opengl::CPointCloudColoured needs to be dense");
271  }
272 
273  /** Get XYZ_RGBf coordinates of i'th point */
274  template <typename T>
275  inline void getPointXYZ_RGBf(const size_t idx, T &x,T &y, T &z, float &r,float &g,float &b) const {
277  x=pc.x; y=pc.y; z=pc.z;
278  r=pc.R; g=pc.G; b=pc.B;
279  }
280  /** Set XYZ_RGBf coordinates of i'th point */
281  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) {
283  }
284 
285  /** Get XYZ_RGBu8 coordinates of i'th point */
286  template <typename T>
287  inline void getPointXYZ_RGBu8(const size_t idx, T &x,T &y, T &z, uint8_t &r,uint8_t &g,uint8_t &b) const {
289  x=pc.x; y=pc.y; z=pc.z;
290  r=pc.R*255; g=pc.G*255; b=pc.B*255;
291  }
292  /** Set XYZ_RGBu8 coordinates of i'th point */
293  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) {
294  m_obj.setPoint_fast(idx, mrpt::opengl::CPointCloudColoured::TPointColour(x,y,z,r/255.f,g/255.f,b/255.f) );
295  }
296 
297  /** Get RGBf color of i'th point */
298  inline void getPointRGBf(const size_t idx, float &r,float &g,float &b) const { m_obj.getPointColor_fast(idx,r,g,b); }
299  /** Set XYZ_RGBf coordinates of i'th point */
300  inline void setPointRGBf(const size_t idx, const float r,const float g,const float b) { m_obj.setPointColor_fast(idx,r,g,b); }
301 
302  /** Get RGBu8 color of i'th point */
303  inline void getPointRGBu8(const size_t idx, uint8_t &r,uint8_t &g,uint8_t &b) const {
304  float R,G,B;
305  m_obj.getPointColor_fast(idx,R,G,B);
306  r=R*255; g=G*255; b=B*255;
307  }
308  /** Set RGBu8 coordinates of i'th point */
309  inline void setPointRGBu8(const size_t idx,const uint8_t r,const uint8_t g,const uint8_t b) {
310  m_obj.setPointColor_fast(idx,r/255.f,g/255.f,b/255.f);
311  }
312 
313  }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
314  }
315  namespace opengl
316  {
317  // After declaring the adapter we can here implement this method:
318  template <class POINTSMAP>
319  void CPointCloudColoured::loadFromPointsMap( const POINTSMAP *themap)
320  {
322  const mrpt::utils::PointCloudAdapter<POINTSMAP> pc_src(*themap);
323  const size_t N=pc_src.size();
324  pc_dst.resize(N);
325  for (size_t i=0;i<N;i++)
326  {
327  float x,y,z,r,g,b;
328  pc_src.getPointXYZ_RGBf(i,x,y,z,r,g,b);
329  pc_dst.setPointXYZ_RGBf(i,x,y,z,r,g,b);
330  }
331  }
332  }
333 } // End of namespace
334 
335 #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)
GLdouble GLdouble z
Definition: glext.h:3734
OPENGL_IMPEXP mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
void clear()
Erase all the points.
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
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.
TColormap
Different colormaps for use in mrpt::utils::colormap()
Definition: color_maps.h:30
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CAngularObservationMeshPtr &pObj)
size_t size() const
Return the number of points.
#define THROW_EXCEPTION(msg)
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
void getPointColor_fast(size_t index, float &R, float &G, float &B) const
Like getPointColor but without checking for out-of-index erors.
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.
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
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 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.
unsigned char uint8_t
Definition: rptypes.h:43
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...
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...
Lightweight 3D point (float version).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
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...
GLuint index
Definition: glext.h:3891
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:123
GLubyte g
Definition: glext.h:5575
GLubyte GLubyte b
Definition: glext.h:5575
TListPointColour::iterator iterator
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)
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...
std::vector< TPointColour > TListPointColour
mrpt::math::TPoint3Df getPointf(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
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
const float R
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i&#39;th point.
A RGB color - floats in the range [0,1].
Definition: TColor.h:80
GLenum GLint GLint y
Definition: glext.h:3516
const TPointColour & getPoint(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
void setPoint_fast(const size_t i, const TPointColour &p)
Like setPoint() but does not check for index out of bounds.
GLsizeiptr size
Definition: glext.h:3779
An adapter to different kinds of point cloud object.
Definition: adapters.h:38
GLenum GLint x
Definition: glext.h:3516
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.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
GLfloat GLfloat p
Definition: glext.h:5587
void getPointRGBf(const size_t idx, float &r, float &g, float &b) const
Get RGBf color of i&#39;th point.
void resize(size_t N)
Set the number of points, with undefined contents.
void reserve(size_t N)
Like STL std::vector&#39;s reserve.
void getPointRGBu8(const size_t idx, uint8_t &r, uint8_t &g, uint8_t &b) const
Get RGBu8 color of i&#39;th point.
virtual ~CPointCloudColoured()
Private, virtual destructor: only can be deleted from smart pointers.



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019