MRPT  1.9.9
CPointCloudColoured.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #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  * https://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() = default;
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 
67  using iterator = TListPointColour::iterator;
68  using const_iterator = TListPointColour::const_iterator;
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{1};
75  /** Default: false */
76  bool m_pointSmooth{false};
77  mutable volatile size_t m_last_rendered_count{0},
79 
80  public:
81  /** Constructor
82  */
84  /** Private, virtual destructor: only can be deleted from smart pointers */
85  ~CPointCloudColoured() override = default;
86  /** Do needed internal work if all points are new (octree rebuilt,...) */
87  void markAllPointsAsNew();
88 
89  public:
90  /** Evaluates the bounding box of this object (including possible children)
91  * in the coordinate frame of the object parent. */
94  mrpt::math::TPoint3D& bb_max) const override
95  {
96  this->octree_getBoundingBox(bb_min, bb_max);
97  }
98 
99  /** @name Read/Write of the list of points to render
100  @{ */
101 
102  /** Inserts a new point into the point cloud. */
103  void push_back(float x, float y, float z, float R, float G, float B);
104 
105  /** Set the number of points, with undefined contents */
106  inline void resize(size_t N)
107  {
108  m_points.resize(N);
110  }
111 
112  /** Like STL std::vector's reserve */
113  inline void reserve(size_t N) { m_points.reserve(N); }
114  /** Read access to each individual point (checks for "i" in the valid range
115  * only in Debug). */
116  inline const TPointColour& operator[](size_t i) const
117  {
118 #ifdef _DEBUG
119  ASSERT_BELOW_(i, size());
120 #endif
121  return m_points[i];
122  }
123 
124  /** Read access to each individual point (checks for "i" in the valid range
125  * only in Debug). */
126  inline const TPointColour& getPoint(size_t i) const
127  {
128 #ifdef _DEBUG
129  ASSERT_BELOW_(i, size());
130 #endif
131  return m_points[i];
132  }
133 
134  /** Read access to each individual point (checks for "i" in the valid range
135  * only in Debug). */
136  inline mrpt::math::TPoint3Df getPointf(size_t i) const
137  {
138 #ifdef _DEBUG
139  ASSERT_BELOW_(i, size());
140 #endif
141  return mrpt::math::TPoint3Df(
142  m_points[i].x, m_points[i].y, m_points[i].z);
143  }
144 
145  /** Write an individual point (checks for "i" in the valid range only in
146  * Debug). */
147  void setPoint(size_t i, const TPointColour& p);
148 
149  /** Like \a setPoint() but does not check for index out of bounds */
150  inline void setPoint_fast(const size_t i, const TPointColour& p)
151  {
152  m_points[i] = p;
154  }
155 
156  /** Like \a setPoint() but does not check for index out of bounds */
157  inline void setPoint_fast(
158  const size_t i, const float x, const float y, const float z)
159  {
160  TPointColour& p = m_points[i];
161  p.x = x;
162  p.y = y;
163  p.z = z;
165  }
166 
167  /** Like \c setPointColor but without checking for out-of-index erors */
168  inline void setPointColor_fast(size_t index, float R, float G, float B)
169  {
170  m_points[index].R = R;
171  m_points[index].G = G;
172  m_points[index].B = B;
173  }
174  /** Like \c getPointColor but without checking for out-of-index erors */
175  inline void getPointColor_fast(
176  size_t index, float& R, float& G, float& B) const
177  {
178  R = m_points[index].R;
179  G = m_points[index].G;
180  B = m_points[index].B;
181  }
182 
183  /** Return the number of points */
184  inline size_t size() const { return m_points.size(); }
185  /** Erase all the points */
186  inline void clear()
187  {
188  m_points.clear();
190  }
191 
192  /** Load the points from any other point map class supported by the adapter
193  * mrpt::opengl::PointCloudAdapter. */
194  template <class POINTSMAP>
195  void loadFromPointsMap(const POINTSMAP* themap);
196  // Must be implemented at the end of the header.
197 
198  /** Get the number of elements actually rendered in the last render event.
199  */
200  size_t getActuallyRendered() const { return m_last_rendered_count; }
201  /** @} */
202 
203  /** @name Modify the appearance of the rendered points
204  @{ */
205 
206  inline void setPointSize(float pointSize) { m_pointSize = pointSize; }
207  inline float getPointSize() const { return m_pointSize; }
208  inline void enablePointSmooth(bool enable = true)
209  {
210  m_pointSmooth = enable;
211  }
212  inline void disablePointSmooth() { m_pointSmooth = false; }
213  inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
214  /** Regenerates the color of each point according the one coordinate
215  * (coord_index:0,1,2 for X,Y,Z) and the given color map. */
217  const float coord_min, const float coord_max, const int coord_index = 2,
218  const mrpt::img::TColormap color_map = mrpt::img::cmJET);
219  /** @} */
220 
221  /** Render */
222  void render() const override;
223 
224  /** Render a subset of points (required by octree renderer) */
225  void render_subset(
226  const bool all, const std::vector<size_t>& idxs,
227  const float render_area_sqpixels) const;
228 
229  protected:
230  /** @name PLY Import virtual methods to implement in base classes
231  @{ */
232  /** In a base class, reserve memory to prepare subsequent calls to
233  * PLY_import_set_vertex */
234  void PLY_import_set_vertex_count(const size_t N) override;
235  /** In a base class, reserve memory to prepare subsequent calls to
236  * PLY_import_set_face */
237  void PLY_import_set_face_count(const size_t N) override
238  {
240  }
241  /** In a base class, will be called after PLY_import_set_vertex_count() once
242  * for each loaded point.
243  * \param pt_color Will be nullptr if the loaded file does not provide
244  * color info.
245  */
247  const size_t idx, const mrpt::math::TPoint3Df& pt,
248  const mrpt::img::TColorf* pt_color = nullptr) override;
249  /** @} */
250 
251  /** @name PLY Export virtual methods to implement in base classes
252  @{ */
253  size_t PLY_export_get_vertex_count() const override;
254  size_t PLY_export_get_face_count() const override { return 0; }
256  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
257  mrpt::img::TColorf& pt_color) const override;
258  /** @} */
259 };
260 
262  mrpt::serialization::CArchive& in, CPointCloudColoured::TPointColour& o);
265  const CPointCloudColoured::TPointColour& o);
266 
267 /** Specialization
268  * mrpt::opengl::PointCloudAdapter<mrpt::opengl::CPointCloudColoured> \ingroup
269  * mrpt_adapters_grp*/
270 template <>
272 {
273  private:
275 
276  public:
277  /** The type of each point XYZ coordinates */
278  using coords_t = float;
279  /** Has any color RGB info? */
280  static constexpr bool HAS_RGB = true;
281  /** Has native RGB info (as floats)? */
282  static constexpr bool HAS_RGBf = true;
283  /** Has native RGB info (as uint8_t)? */
284  static constexpr bool HAS_RGBu8 = false;
285 
286  /** Constructor (accept a const ref for convenience) */
288  : m_obj(*const_cast<mrpt::opengl::CPointCloudColoured*>(&obj))
289  {
290  }
291  /** Get number of points */
292  inline size_t size() const { return m_obj.size(); }
293  /** Set number of points (to uninitialized values) */
294  inline void resize(const size_t N) { m_obj.resize(N); }
295  /** Does nothing as of now */
296  inline void setDimensions(const size_t& height, const size_t& width) {}
297  /** Get XYZ coordinates of i'th point */
298  template <typename T>
299  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
300  {
302  x = pc.x;
303  y = pc.y;
304  z = pc.z;
305  }
306  /** Set XYZ coordinates of i'th point */
307  inline void setPointXYZ(
308  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
309  {
310  m_obj.setPoint_fast(idx, x, y, z);
311  }
312 
313  inline void setInvalidPoint(const size_t idx)
314  {
315  THROW_EXCEPTION("mrpt::opengl::CPointCloudColoured needs to be dense");
316  }
317 
318  /** Get XYZ_RGBf coordinates of i'th point */
319  template <typename T>
320  inline void getPointXYZ_RGBf(
321  const size_t idx, T& x, T& y, T& z, float& r, float& g, float& b) const
322  {
324  x = pc.x;
325  y = pc.y;
326  z = pc.z;
327  r = pc.R;
328  g = pc.G;
329  b = pc.B;
330  }
331  /** Set XYZ_RGBf coordinates of i'th point */
332  inline void setPointXYZ_RGBf(
333  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
334  const float r, const float g, const float b)
335  {
336  m_obj.setPoint_fast(
337  idx,
339  }
340 
341  /** Get XYZ_RGBu8 coordinates of i'th point */
342  template <typename T>
343  inline void getPointXYZ_RGBu8(
344  const size_t idx, T& x, T& y, T& z, uint8_t& r, uint8_t& g,
345  uint8_t& b) const
346  {
348  x = pc.x;
349  y = pc.y;
350  z = pc.z;
351  r = pc.R * 255;
352  g = pc.G * 255;
353  b = pc.B * 255;
354  }
355  /** Set XYZ_RGBu8 coordinates of i'th point */
356  inline void setPointXYZ_RGBu8(
357  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
358  const uint8_t r, const uint8_t g, const uint8_t b)
359  {
360  m_obj.setPoint_fast(
362  x, y, z, r / 255.f, g / 255.f, b / 255.f));
363  }
364 
365  /** Get RGBf color of i'th point */
366  inline void getPointRGBf(
367  const size_t idx, float& r, float& g, float& b) const
368  {
369  m_obj.getPointColor_fast(idx, r, g, b);
370  }
371  /** Set XYZ_RGBf coordinates of i'th point */
372  inline void setPointRGBf(
373  const size_t idx, const float r, const float g, const float b)
374  {
375  m_obj.setPointColor_fast(idx, r, g, b);
376  }
377 
378  /** Get RGBu8 color of i'th point */
379  inline void getPointRGBu8(
380  const size_t idx, uint8_t& r, uint8_t& g, uint8_t& b) const
381  {
382  float R, G, B;
383  m_obj.getPointColor_fast(idx, R, G, B);
384  r = R * 255;
385  g = G * 255;
386  b = B * 255;
387  }
388  /** Set RGBu8 coordinates of i'th point */
389  inline void setPointRGBu8(
390  const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
391  {
392  m_obj.setPointColor_fast(idx, r / 255.f, g / 255.f, b / 255.f);
393  }
394 
395 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
396 } // namespace opengl
397 namespace opengl
398 {
399 // After declaring the adapter we can here implement this method:
400 template <class POINTSMAP>
401 void CPointCloudColoured::loadFromPointsMap(const POINTSMAP* themap)
402 {
404  const mrpt::opengl::PointCloudAdapter<POINTSMAP> pc_src(*themap);
405  const size_t N = pc_src.size();
406  pc_dst.resize(N);
407  for (size_t i = 0; i < N; i++)
408  {
410  {
411  float x, y, z, r, g, b;
412  pc_src.getPointXYZ_RGBf(i, x, y, z, r, g, b);
413  pc_dst.setPointXYZ_RGBf(i, x, y, z, r, g, b);
414  }
415  else
416  {
417  float x, y, z;
418  pc_src.getPointXYZ(i, x, y, z);
419  pc_dst.setPointXYZ_RGBf(i, x, y, z, 0, 0, 0);
420  }
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
#define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS)
Declares a typename to be "namespace::type".
Definition: TTypeName.h:119
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:3879
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:67
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:122
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.
#define ASSERT_BELOW_(__A, __B)
Definition: exceptions.h:149
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:40
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:4085
GLenum GLsizei width
Definition: glext.h:3535
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 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).
Definition: TPoint3D.h:21
GLuint index
Definition: glext.h:4068
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...
void render() const override
Render.
GLubyte g
Definition: glext.h:6372
GLubyte GLubyte b
Definition: glext.h:6372
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.
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:54
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
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
mrpt::vision::TStereoCalibResults out
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
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:7391
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
const auto bb_max
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, mrpt::opengl::CLight &o)
Definition: CLight.cpp:116
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:3542
const TPointColour & getPoint(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void setPoint_fast(const size_t i, const TPointColour &p)
Like setPoint() but does not check for index out of bounds.
const auto bb_min
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:3542
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.
Definition: TPoint3D.h:90
float m_pointSize
By default is 1.0.
GLenum GLsizei GLsizei height
Definition: glext.h:3558
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.
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:6398
~CPointCloudColoured() override=default
Private, virtual destructor: only can be deleted from smart pointers.
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.
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: c1796881b Sat Nov 16 19:04:34 2019 +0100 at sáb nov 16 19:15:10 CET 2019