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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020