Main MRPT website > C++ reference for MRPT 1.9.9
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 /** A cloud of points, each one with an individual colour (R,G,B). The alpha
24  * component is shared by all the points and is stored in the base member
25  * 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
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::COpenGLScene, opengl::CPointCloud
35  *
36  * <div align="center">
37  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
38  * border-style: solid;">
39  * <tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html
40  * preview_CPointCloudColoured.png </td> </tr>
41  * </table>
42  * </div>
43  *
44  * \ingroup mrpt_opengl_grp
45  */
47  public COctreePointRenderer<CPointCloudColoured>,
50 {
52 
53  public:
54  struct TPointColour
55  {
56  inline TPointColour() {}
57  inline TPointColour(
58  float _x, float _y, float _z, float _R, float _G, float _B)
59  : x(_x), y(_y), z(_z), R(_R), G(_G), B(_B)
60  {
61  }
62  float x, y, z, R, G, B; // Float is precission enough for rendering
63  };
64 
65  private:
66  typedef std::vector<TPointColour> TListPointColour;
68 
71  inline iterator begin() { return m_points.begin(); }
72  inline const_iterator begin() const { return m_points.begin(); }
73  inline iterator end() { return m_points.end(); }
74  inline const_iterator end() const { return m_points.end(); }
75  /** By default is 1.0 */
76  float m_pointSize;
77  /** Default: false */
79  mutable volatile size_t m_last_rendered_count,
81 
82  public:
83  /** Constructor
84  */
86  : m_points(),
87  m_pointSize(1),
88  m_pointSmooth(false),
91  {
92  }
93  /** Private, virtual destructor: only can be deleted from smart pointers */
94  virtual ~CPointCloudColoured() {}
95  /** Do needed internal work if all points are new (octree rebuilt,...) */
96  void markAllPointsAsNew();
97 
98  public:
99  /** Evaluates the bounding box of this object (including possible children)
100  * in the coordinate frame of the object parent. */
101  virtual void getBoundingBox(
102  mrpt::math::TPoint3D& bb_min,
103  mrpt::math::TPoint3D& bb_max) const override
104  {
105  this->octree_getBoundingBox(bb_min, bb_max);
106  }
107 
108  /** @name Read/Write of the list of points to render
109  @{ */
110 
111  /** Inserts a new point into the point cloud. */
112  void push_back(float x, float y, float z, float R, float G, float B);
113 
114  /** Set the number of points, with undefined contents */
115  inline void resize(size_t N)
116  {
117  m_points.resize(N);
119  }
120 
121  /** Like STL std::vector's reserve */
122  inline void reserve(size_t N) { m_points.reserve(N); }
123  /** Read access to each individual point (checks for "i" in the valid range
124  * only in Debug). */
125  inline const TPointColour& operator[](size_t i) const
126  {
127 #ifdef _DEBUG
128  ASSERT_BELOW_(i, size())
129 #endif
130  return m_points[i];
131  }
132 
133  /** Read access to each individual point (checks for "i" in the valid range
134  * only in Debug). */
135  inline const TPointColour& getPoint(size_t i) const
136  {
137 #ifdef _DEBUG
138  ASSERT_BELOW_(i, size())
139 #endif
140  return m_points[i];
141  }
142 
143  /** Read access to each individual point (checks for "i" in the valid range
144  * only in Debug). */
145  inline mrpt::math::TPoint3Df getPointf(size_t i) const
146  {
147 #ifdef _DEBUG
148  ASSERT_BELOW_(i, size())
149 #endif
150  return mrpt::math::TPoint3Df(
151  m_points[i].x, m_points[i].y, m_points[i].z);
152  }
153 
154  /** Write an individual point (checks for "i" in the valid range only in
155  * Debug). */
156  void setPoint(size_t i, const TPointColour& p);
157 
158  /** Like \a setPoint() but does not check for index out of bounds */
159  inline void setPoint_fast(const size_t i, const TPointColour& p)
160  {
161  m_points[i] = p;
163  }
164 
165  /** Like \a setPoint() but does not check for index out of bounds */
166  inline void setPoint_fast(
167  const size_t i, const float x, const float y, const float z)
168  {
169  TPointColour& p = m_points[i];
170  p.x = x;
171  p.y = y;
172  p.z = z;
174  }
175 
176  /** Like \c setPointColor but without checking for out-of-index erors */
177  inline void setPointColor_fast(size_t index, float R, float G, float B)
178  {
179  m_points[index].R = R;
180  m_points[index].G = G;
181  m_points[index].B = B;
182  }
183  /** Like \c getPointColor but without checking for out-of-index erors */
184  inline void getPointColor_fast(
185  size_t index, float& R, float& G, float& B) const
186  {
187  R = m_points[index].R;
188  G = m_points[index].G;
189  B = m_points[index].B;
190  }
191 
192  /** Return the number of points */
193  inline size_t size() const { return m_points.size(); }
194  /** Erase all the points */
195  inline void clear()
196  {
197  m_points.clear();
199  }
200 
201  /** Load the points from any other point map class supported by the adapter
202  * mrpt::utils::PointCloudAdapter. */
203  template <class POINTSMAP>
204  void loadFromPointsMap(const POINTSMAP* themap);
205  // Must be implemented at the end of the header.
206 
207  /** Get the number of elements actually rendered in the last render event.
208  */
209  size_t getActuallyRendered() const { return m_last_rendered_count; }
210  /** @} */
211 
212  /** @name Modify the appearance of the rendered points
213  @{ */
214 
215  inline void setPointSize(float pointSize) { m_pointSize = pointSize; }
216  inline float getPointSize() const { return m_pointSize; }
217  inline void enablePointSmooth(bool enable = true)
218  {
219  m_pointSmooth = enable;
220  }
221  inline void disablePointSmooth() { m_pointSmooth = false; }
222  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
223  /** Regenerates the color of each point according the one coordinate
224  * (coord_index:0,1,2 for X,Y,Z) and the given color map. */
226  const float coord_min, const float coord_max, const int coord_index = 2,
227  const mrpt::utils::TColormap color_map = mrpt::utils::cmJET);
228  /** @} */
229 
230  /** Render */
231  void render() const override;
232 
233  /** Render a subset of points (required by octree renderer) */
234  void render_subset(
235  const bool all, const std::vector<size_t>& idxs,
236  const float render_area_sqpixels) const;
237 
238  protected:
239  /** @name PLY Import virtual methods to implement in base classes
240  @{ */
241  /** In a base class, reserve memory to prepare subsequent calls to
242  * PLY_import_set_vertex */
243  virtual void PLY_import_set_vertex_count(const size_t N) override;
244  /** In a base class, reserve memory to prepare subsequent calls to
245  * PLY_import_set_face */
246  virtual void PLY_import_set_face_count(const size_t N) override
247  {
249  }
250  /** In a base class, will be called after PLY_import_set_vertex_count() once
251  * for each loaded point.
252  * \param pt_color Will be nullptr if the loaded file does not provide
253  * color info.
254  */
255  virtual void PLY_import_set_vertex(
256  const size_t idx, const mrpt::math::TPoint3Df& pt,
257  const mrpt::utils::TColorf* pt_color = nullptr) override;
258  /** @} */
259 
260  /** @name PLY Export virtual methods to implement in base classes
261  @{ */
262  size_t PLY_export_get_vertex_count() const override;
263  size_t PLY_export_get_face_count() const override { return 0; }
265  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
266  mrpt::utils::TColorf& pt_color) const override;
267  /** @} */
268 };
269 
271  mrpt::utils::CStream& in, CPointCloudColoured::TPointColour& o);
273  mrpt::utils::CStream& out, const CPointCloudColoured::TPointColour& o);
274 
275 } // end namespace
276 
277 namespace utils
278 {
279 // Specialization must occur in the same namespace
281  CPointCloudColoured::TPointColour, mrpt::opengl)
282 }
283 
284 namespace utils
285 {
286 /** Specialization
287  * mrpt::utils::PointCloudAdapter<mrpt::opengl::CPointCloudColoured> \ingroup
288  * mrpt_adapters_grp*/
289 template <>
291 {
292  private:
294 
295  public:
296  /** The type of each point XYZ coordinates */
297  typedef float coords_t;
298  /** Has any color RGB info? */
299  static const int HAS_RGB = 1;
300  /** Has native RGB info (as floats)? */
301  static const int HAS_RGBf = 1;
302  /** Has native RGB info (as uint8_t)? */
303  static const int HAS_RGBu8 = 0;
304 
305  /** Constructor (accept a const ref for convenience) */
307  : m_obj(*const_cast<mrpt::opengl::CPointCloudColoured*>(&obj))
308  {
309  }
310  /** Get number of points */
311  inline size_t size() const { return m_obj.size(); }
312  /** Set number of points (to uninitialized values) */
313  inline void resize(const size_t N) { m_obj.resize(N); }
314  /** Get XYZ coordinates of i'th point */
315  template <typename T>
316  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
317  {
319  x = pc.x;
320  y = pc.y;
321  z = pc.z;
322  }
323  /** Set XYZ coordinates of i'th point */
324  inline void setPointXYZ(
325  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
326  {
327  m_obj.setPoint_fast(idx, x, y, z);
328  }
329 
330  inline void setInvalidPoint(const size_t idx)
331  {
332  THROW_EXCEPTION("mrpt::opengl::CPointCloudColoured needs to be dense");
333  }
334 
335  /** Get XYZ_RGBf coordinates of i'th point */
336  template <typename T>
337  inline void getPointXYZ_RGBf(
338  const size_t idx, T& x, T& y, T& z, float& r, float& g, float& b) const
339  {
341  x = pc.x;
342  y = pc.y;
343  z = pc.z;
344  r = pc.R;
345  g = pc.G;
346  b = pc.B;
347  }
348  /** Set XYZ_RGBf coordinates of i'th point */
349  inline void setPointXYZ_RGBf(
350  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
351  const float r, const float g, const float b)
352  {
353  m_obj.setPoint_fast(
354  idx,
356  }
357 
358  /** Get XYZ_RGBu8 coordinates of i'th point */
359  template <typename T>
360  inline void getPointXYZ_RGBu8(
361  const size_t idx, T& x, T& y, T& z, uint8_t& r, uint8_t& g,
362  uint8_t& b) const
363  {
365  x = pc.x;
366  y = pc.y;
367  z = pc.z;
368  r = pc.R * 255;
369  g = pc.G * 255;
370  b = pc.B * 255;
371  }
372  /** Set XYZ_RGBu8 coordinates of i'th point */
373  inline void setPointXYZ_RGBu8(
374  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
375  const uint8_t r, const uint8_t g, const uint8_t b)
376  {
377  m_obj.setPoint_fast(
379  x, y, z, r / 255.f, g / 255.f, b / 255.f));
380  }
381 
382  /** Get RGBf color of i'th point */
383  inline void getPointRGBf(
384  const size_t idx, float& r, float& g, float& b) const
385  {
386  m_obj.getPointColor_fast(idx, r, g, b);
387  }
388  /** Set XYZ_RGBf coordinates of i'th point */
389  inline void setPointRGBf(
390  const size_t idx, const float r, const float g, const float b)
391  {
392  m_obj.setPointColor_fast(idx, r, g, b);
393  }
394 
395  /** Get RGBu8 color of i'th point */
396  inline void getPointRGBu8(
397  const size_t idx, uint8_t& r, uint8_t& g, uint8_t& b) const
398  {
399  float R, G, B;
400  m_obj.getPointColor_fast(idx, R, G, B);
401  r = R * 255;
402  g = G * 255;
403  b = B * 255;
404  }
405  /** Set RGBu8 coordinates of i'th point */
406  inline void setPointRGBu8(
407  const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
408  {
409  m_obj.setPointColor_fast(idx, r / 255.f, g / 255.f, b / 255.f);
410  }
411 
412 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
413 }
414 namespace opengl
415 {
416 // After declaring the adapter we can here implement this method:
417 template <class POINTSMAP>
418 void CPointCloudColoured::loadFromPointsMap(const POINTSMAP* themap)
419 {
421  const mrpt::utils::PointCloudAdapter<POINTSMAP> pc_src(*themap);
422  const size_t N = pc_src.size();
423  pc_dst.resize(N);
424  for (size_t i = 0; i < N; i++)
425  {
426  float x, y, z, r, g, b;
427  pc_src.getPointXYZ_RGBf(i, x, y, z, r, g, b);
428  pc_dst.setPointXYZ_RGBf(i, x, y, z, r, g, b);
429  }
430 }
431 }
432 } // End of namespace
433 
434 #endif
#define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS)
Definition: TTypeName.h:79
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...
PointCloudAdapter(const mrpt::opengl::CPointCloudColoured &obj)
Constructor (accept a const ref for convenience)
GLdouble GLdouble z
Definition: glext.h:3872
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.
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:31
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:26
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:43
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:27
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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:41
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...
void setPoint(size_t i, const TPointColour &p)
Write an individual point (checks for "i" in the valid range only in Debug).
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
TPointColour(float _x, float _y, float _z, float _R, float _G, float _B)
Lightweight 3D point (float version).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
GLuint index
Definition: glext.h:4054
void recolorizeByCoordinate(const float coord_min, const float coord_max, const int coord_index=2, const mrpt::utils::TColormap color_map=mrpt::utils::cmJET)
Regenerates the color of each point according the one coordinate (coord_index:0,1,2 for X,Y,Z) and the given color map.
void render() const override
Render.
GLubyte g
Definition: glext.h:6279
GLubyte GLubyte b
Definition: glext.h:6279
size_t PLY_export_get_vertex_count() const override
In a base class, return the number of vertices.
TListPointColour::iterator iterator
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 setPointRGBf(const size_t idx, const float r, const float g, const float b)
Set XYZ_RGBf coordinates of i&#39;th point.
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...
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)
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:126
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:3705
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
void push_back(float x, float y, float z, float R, float G, float B)
Inserts a new point into the point cloud.
const float R
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)
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
void octree_getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
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:78
size_t PLY_export_get_face_count() const override
In a base class, return the number of faces.
GLenum GLint GLint y
Definition: glext.h:3538
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.
An adapter to different kinds of point cloud object.
Definition: adapters.h:41
const TPointColour & operator[](size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
GLenum GLint x
Definition: glext.h:3538
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.
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.
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
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 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.
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.
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)



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