MRPT  2.0.2
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-2020, 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>
12 #include <mrpt/math/TPoint3D.h>
17 
18 namespace mrpt::opengl
19 {
20 /** A cloud of points, each one with an individual colour (R,G,B). The alpha
21  * component is shared by all the points and is stored in the base member
22  * m_color_A.
23  *
24  * To load from a points-map, CPointCloudColoured::loadFromPointsMap().
25  *
26  * This class uses smart optimizations while rendering to efficiently draw
27  * clouds of millions of points,
28  * as described in this page:
29  * https://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
30  *
31  * \sa opengl::COpenGLScene, opengl::CPointCloud
32  *
33  * <div align="center">
34  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
35  * border-style: solid;">
36  * <tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html
37  * preview_CPointCloudColoured.png </td> </tr>
38  * </table>
39  * </div>
40  *
41  * \ingroup mrpt_opengl_grp
42  */
44  public COctreePointRenderer<CPointCloudColoured>,
47 {
49 
50  private:
51  /** Actually, an alias for the base class shader container of points. Kept
52  * to have an easy to use name. */
53  std::vector<mrpt::math::TPoint3Df>& m_points =
55 
56  std::vector<mrpt::img::TColor>& m_point_colors =
58 
60 
61  public:
62  void onUpdateBuffers_Points() override;
63 
64  CPointCloudColoured() = default;
65  virtual ~CPointCloudColoured() override = default;
66 
67  void markAllPointsAsNew();
68 
69  public:
70  /** Evaluates the bounding box of this object (including possible children)
71  * in the coordinate frame of the object parent. */
74  mrpt::math::TPoint3D& bb_max) const override
75  {
76  this->octree_getBoundingBox(bb_min, bb_max);
77  }
78 
79  /** @name Read/Write of the list of points to render
80  @{ */
81 
82  /** Inserts a new point into the point cloud. */
83  void push_back(
84  float x, float y, float z, float R, float G, float B, float A = 1);
85 
86  /** Set the number of points, with undefined contents */
87  inline void resize(size_t N)
88  {
89  m_points.resize(N);
90  m_point_colors.resize(N);
93  }
94 
95  /** Like STL std::vector's reserve */
96  inline void reserve(size_t N)
97  {
98  m_points.reserve(N);
99  m_point_colors.reserve(N);
100  }
101 
102  inline const mrpt::math::TPoint3Df& getPoint3Df(size_t i) const
103  {
104  return m_points[i];
105  }
106 
107  /** Write an individual point (checks for "i" in the valid range only in
108  * Debug). */
109  void setPoint(size_t i, const mrpt::math::TPointXYZfRGBAu8& p);
110 
111  /** Like \a setPoint() but does not check for index out of bounds */
112  inline void setPoint_fast(
113  const size_t i, const mrpt::math::TPointXYZfRGBAu8& p)
114  {
115  m_points[i] = p.pt;
116  m_point_colors[i] = mrpt::img::TColor(p.r, p.g, p.b, p.a);
118  }
119 
120  /** Like \a setPoint() but does not check for index out of bounds */
121  inline void setPoint_fast(
122  const size_t i, const float x, const float y, const float z)
123  {
124  m_points[i] = {x, y, z};
126  }
127 
128  /** Like \c setPointColor but without checking for out-of-index erors */
129  inline void setPointColor_fast(
130  size_t index, float R, float G, float B, float A = 1)
131  {
132  m_point_colors[index].R = f2u8(R);
133  m_point_colors[index].G = f2u8(G);
134  m_point_colors[index].B = f2u8(B);
135  m_point_colors[index].A = f2u8(A);
136  }
138  size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0xff)
139  {
140  m_point_colors[index].R = r;
141  m_point_colors[index].G = g;
142  m_point_colors[index].B = b;
143  m_point_colors[index].A = a;
144  }
145  /** Like \c getPointColor but without checking for out-of-index erors */
146  inline void getPointColor_fast(
147  size_t index, float& R, float& G, float& B) const
148  {
149  R = u8tof(m_point_colors[index].R);
150  G = u8tof(m_point_colors[index].G);
151  B = u8tof(m_point_colors[index].B);
152  }
153  inline void getPointColor_fast(
154  size_t index, uint8_t& r, uint8_t& g, uint8_t& b) const
155  {
156  r = m_point_colors[index].R;
157  g = m_point_colors[index].B;
158  b = m_point_colors[index].B;
159  }
160  inline mrpt::img::TColor getPointColor(size_t index) const
161  {
162  return m_point_colors[index];
163  }
164 
165  /** Return the number of points */
166  inline size_t size() const { return m_points.size(); }
167  /** Erase all the points */
168  inline void clear()
169  {
170  m_points.clear();
171  m_point_colors.clear();
174  }
175 
176  /** Load the points from any other point map class supported by the adapter
177  * mrpt::opengl::PointCloudAdapter. */
178  template <class POINTSMAP>
179  void loadFromPointsMap(const POINTSMAP* themap);
180  // Must be implemented at the end of the header.
181 
182  /** Get the number of elements actually rendered in the last render event.
183  */
184  size_t getActuallyRendered() const { return m_last_rendered_count; }
185  /** @} */
186 
187  /** @name Modify the appearance of the rendered points
188  @{ */
189 
190  /** Regenerates the color of each point according the one coordinate
191  * (coord_index:0,1,2 for X,Y,Z) and the given color map. */
193  const float coord_min, const float coord_max, const int coord_index = 2,
194  const mrpt::img::TColormap color_map = mrpt::img::cmJET);
195 
196  /** @} */
197 
198  /** Render a subset of points (required by octree renderer) */
199  void render_subset(
200  const bool all, const std::vector<size_t>& idxs,
201  const float render_area_sqpixels) const;
202 
203  protected:
204  /** @name PLY Import virtual methods to implement in base classes
205  @{ */
206  /** In a base class, reserve memory to prepare subsequent calls to
207  * PLY_import_set_vertex */
208  void PLY_import_set_vertex_count(const size_t N) override;
209  /** In a base class, reserve memory to prepare subsequent calls to
210  * PLY_import_set_face */
211  void PLY_import_set_face_count([[maybe_unused]] const size_t N) override {}
212  /** In a base class, will be called after PLY_import_set_vertex_count() once
213  * for each loaded point.
214  * \param pt_color Will be nullptr if the loaded file does not provide
215  * color info.
216  */
218  const size_t idx, const mrpt::math::TPoint3Df& pt,
219  const mrpt::img::TColorf* pt_color = nullptr) override;
220  /** @} */
221 
222  /** @name PLY Export virtual methods to implement in base classes
223  @{ */
224  size_t PLY_export_get_vertex_count() const override;
225  size_t PLY_export_get_face_count() const override { return 0; }
227  const size_t idx, mrpt::math::TPoint3Df& pt, bool& pt_has_color,
228  mrpt::img::TColorf& pt_color) const override;
229  /** @} */
230 };
231 
232 /** Specialization
233  * mrpt::opengl::PointCloudAdapter<mrpt::opengl::CPointCloudColoured> \ingroup
234  * mrpt_adapters_grp*/
235 template <>
237 {
238  private:
240 
241  public:
242  /** The type of each point XYZ coordinates */
243  using coords_t = float;
244  /** Has any color RGB info? */
245  static constexpr bool HAS_RGB = true;
246  /** Has native RGB info (as floats)? */
247  static constexpr bool HAS_RGBf = true;
248  /** Has native RGB info (as uint8_t)? */
249  static constexpr bool HAS_RGBu8 = false;
250 
251  /** Constructor (accept a const ref for convenience) */
253  : m_obj(*const_cast<mrpt::opengl::CPointCloudColoured*>(&obj))
254  {
255  }
256  /** Get number of points */
257  inline size_t size() const { return m_obj.size(); }
258  /** Set number of points (to uninitialized values) */
259  inline void resize(const size_t N) { m_obj.resize(N); }
260  /** Does nothing as of now */
261  inline void setDimensions(size_t height, size_t width) {}
262  /** Get XYZ coordinates of i'th point */
263  template <typename T>
264  inline void getPointXYZ(const size_t idx, T& x, T& y, T& z) const
265  {
266  const auto& p = m_obj.getPoint3Df(idx);
267  x = p.x;
268  y = p.y;
269  z = p.z;
270  }
271  /** Set XYZ coordinates of i'th point */
272  inline void setPointXYZ(
273  const size_t idx, const coords_t x, const coords_t y, const coords_t z)
274  {
275  m_obj.setPoint_fast(idx, x, y, z);
276  }
277 
278  inline void setInvalidPoint(const size_t idx)
279  {
280  m_obj.setPoint_fast(idx, 0, 0, 0);
281  }
282 
283  /** Get XYZ_RGBf coordinates of i'th point */
284  template <typename T>
285  inline void getPointXYZ_RGBAf(
286  const size_t idx, T& x, T& y, T& z, float& Rf, float& Gf, float& Bf,
287  float& Af) const
288  {
289  const auto& pt = m_obj.getPoint3Df(idx);
290  const auto& col = m_obj.getPointColor(idx);
291  x = pt.x;
292  y = pt.y;
293  z = pt.z;
294  Rf = u8tof(col.R);
295  Gf = u8tof(col.G);
296  Bf = u8tof(col.B);
297  Af = u8tof(col.A);
298  }
299  /** Set XYZ_RGBf coordinates of i'th point */
300  inline void setPointXYZ_RGBAf(
301  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
302  const float Rf, const float Gf, const float Bf, const float Af)
303  {
304  m_obj.setPoint(
306  x, y, z, f2u8(Rf), f2u8(Gf), f2u8(Bf), f2u8(Af)));
307  }
308 
309  /** Get XYZ_RGBu8 coordinates of i'th point */
310  template <typename T>
311  inline void getPointXYZ_RGBu8(
312  const size_t idx, T& x, T& y, T& z, uint8_t& r, uint8_t& g,
313  uint8_t& b) const
314  {
315  const auto& pt = m_obj.getPoint3Df(idx);
316  const auto& col = m_obj.getPointColor(idx);
317  x = pt.x;
318  y = pt.y;
319  z = pt.z;
320  r = col.R;
321  g = col.G;
322  b = col.B;
323  }
324  /** Set XYZ_RGBu8 coordinates of i'th point */
325  inline void setPointXYZ_RGBu8(
326  const size_t idx, const coords_t x, const coords_t y, const coords_t z,
327  const uint8_t r, const uint8_t g, const uint8_t b,
328  const uint8_t a = 0xff)
329  {
330  m_obj.setPoint_fast(
331  idx, mrpt::math::TPointXYZfRGBAu8(x, y, z, r, g, b, a));
332  }
333 
334  /** Get RGBf color of i'th point */
335  inline void getPointRGBf(
336  const size_t idx, float& r, float& g, float& b) const
337  {
338  m_obj.getPointColor_fast(idx, r, g, b);
339  }
340  /** Set XYZ_RGBf coordinates of i'th point */
341  inline void setPointRGBf(
342  const size_t idx, const float r, const float g, const float b)
343  {
344  m_obj.setPointColor_fast(idx, r, g, b);
345  }
346 
347  /** Get RGBu8 color of i'th point */
348  inline void getPointRGBu8(
349  const size_t idx, uint8_t& r, uint8_t& g, uint8_t& b) const
350  {
351  m_obj.getPointColor_fast(idx, r, g, b);
352  }
353  /** Set RGBu8 coordinates of i'th point */
354  inline void setPointRGBu8(
355  const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
356  {
357  m_obj.setPointColor_u8_fast(idx, r, g, b);
358  }
359 
360 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
361 
362 // After declaring the adapter we can here implement this method:
363 template <class POINTSMAP>
364 void CPointCloudColoured::loadFromPointsMap(const POINTSMAP* themap)
365 {
368  const mrpt::opengl::PointCloudAdapter<POINTSMAP> pc_src(*themap);
369  const size_t N = pc_src.size();
370  pc_dst.resize(N);
371  for (size_t i = 0; i < N; i++)
372  {
374  {
375  float x, y, z, r, g, b, a;
376  pc_src.getPointXYZ_RGBAf(i, x, y, z, r, g, b, a);
377  pc_dst.setPointXYZ_RGBAf(i, x, y, z, r, g, b, a);
378  }
379  else
380  {
381  float x, y, z;
382  pc_src.getPointXYZ(i, x, y, z);
383  pc_dst.setPointXYZ_RGBAf(i, x, y, z, 0, 0, 0, 1);
384  }
385  }
386 }
387 } // namespace mrpt::opengl
std::vector< mrpt::math::TPoint3Df > & m_points
Actually, an alias for the base class shader container of points.
TColormap
Different colormaps for use in mrpt::img::colormap()
Definition: color_maps.h:30
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
void getPointRGBf(const size_t idx, float &r, float &g, float &b) const
Get RGBf color of i&#39;th point.
void clear()
Erase all the points.
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
mrpt::math::TPoint3Df pt
Definition: TPoint3D.h:332
const double G
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
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).
const mrpt::math::TPoint3Df & getPoint3Df(size_t i) const
void getPointColor_fast(size_t index, float &R, float &G, float &B) const
Like getPointColor but without checking for out-of-index erors.
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 onUpdateBuffers_Points() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
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...
std::vector< mrpt::img::TColor > m_color_buffer_data
std::vector< mrpt::img::TColor > & m_point_colors
void push_back(float x, float y, float z, float R, float G, float B, float A=1)
Inserts a new point into the point cloud.
void setDimensions(size_t height, size_t width)
Does nothing as of now.
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.
mrpt::img::TColor getPointColor(size_t index) const
void setPointColor_fast(size_t index, float R, float G, float B, float A=1)
Like setPointColor but without checking for out-of-index erors.
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 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 setPointXYZ_RGBAf(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const float Rf, const float Gf, const float Bf, const float Af)
Set XYZ_RGBf coordinates of i&#39;th point.
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.
XYZ point (float) + RGBA(u8)
Definition: TPoint3D.h:330
uint8_t R
Definition: TColor.h:51
Renderizable generic renderer for objects using the points shader.
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds) ...
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)
void PLY_import_set_face_count([[maybe_unused]] const size_t N) override
In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face.
const float R
void setPointColor_u8_fast(size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t a=0xff)
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
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 getPointXYZ_RGBAf(const size_t idx, T &x, T &y, T &z, float &Rf, float &Gf, float &Bf, float &Af) const
Get XYZ_RGBf coordinates of i&#39;th point.
float u8tof(const uint8_t v)
converts a uint8_t [0,255] into a float [0,1]
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)
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void octree_getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
const auto bb_max
size_t PLY_export_get_face_count() const override
In a base class, return the number of faces.
virtual ~CPointCloudColoured() override=default
void setPoint(size_t i, const mrpt::math::TPointXYZfRGBAu8 &p)
Write an 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...
const auto bb_min
A RGB color - 8bit.
Definition: TColor.h:25
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.
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, const uint8_t a=0xff)
Set XYZ_RGBu8 coordinates of i&#39;th point.
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.
void getPointColor_fast(size_t index, uint8_t &r, uint8_t &g, uint8_t &b) const
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)
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 setPoint_fast(const size_t i, const mrpt::math::TPointXYZfRGBAu8 &p)
Like setPoint() but does not check for index out of bounds.



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020