Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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>
18
19namespace mrpt
20{
21 namespace opengl
22 {
23 // This must be added to any CSerializable derived class:
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:
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
62 typedef TListPointColour::iterator iterator;
63 typedef TListPointColour::const_iterator const_iterator;
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 */
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 @{ */
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 };
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
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 /** Get XYZ_RGBf coordinates of i'th point */
269 template <typename T>
270 inline void getPointXYZ_RGBf(const size_t idx, T &x,T &y, T &z, float &r,float &g,float &b) const {
272 x=pc.x; y=pc.y; z=pc.z;
273 r=pc.R; g=pc.G; b=pc.B;
274 }
275 /** Set XYZ_RGBf coordinates of i'th point */
276 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) {
278 }
279
280 /** Get XYZ_RGBu8 coordinates of i'th point */
281 template <typename T>
282 inline void getPointXYZ_RGBu8(const size_t idx, T &x,T &y, T &z, uint8_t &r,uint8_t &g,uint8_t &b) const {
284 x=pc.x; y=pc.y; z=pc.z;
285 r=pc.R*255; g=pc.G*255; b=pc.B*255;
286 }
287 /** Set XYZ_RGBu8 coordinates of i'th point */
288 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) {
289 m_obj.setPoint_fast(idx, mrpt::opengl::CPointCloudColoured::TPointColour(x,y,z,r/255.f,g/255.f,b/255.f) );
290 }
291
292 /** Get RGBf color of i'th point */
293 inline void getPointRGBf(const size_t idx, float &r,float &g,float &b) const { m_obj.getPointColor_fast(idx,r,g,b); }
294 /** Set XYZ_RGBf coordinates of i'th point */
295 inline void setPointRGBf(const size_t idx, const float r,const float g,const float b) { m_obj.setPointColor_fast(idx,r,g,b); }
296
297 /** Get RGBu8 color of i'th point */
298 inline void getPointRGBu8(const size_t idx, uint8_t &r,uint8_t &g,uint8_t &b) const {
299 float R,G,B;
300 m_obj.getPointColor_fast(idx,R,G,B);
301 r=R*255; g=G*255; b=B*255;
302 }
303 /** Set RGBu8 coordinates of i'th point */
304 inline void setPointRGBu8(const size_t idx,const uint8_t r,const uint8_t g,const uint8_t b) {
305 m_obj.setPointColor_fast(idx,r/255.f,g/255.f,b/255.f);
306 }
307
308 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
309 }
310 namespace opengl
311 {
312 // After declaring the adapter we can here implement this method:
313 template <class POINTSMAP>
314 void CPointCloudColoured::loadFromPointsMap( const POINTSMAP *themap)
315 {
317 const mrpt::utils::PointCloudAdapter<POINTSMAP> pc_src(*themap);
318 const size_t N=pc_src.size();
319 pc_dst.resize(N);
320 for (size_t i=0;i<N;i++)
321 {
322 float x,y,z,r,g,b;
323 pc_src.getPointXYZ_RGBf(i,x,y,z,r,g,b);
324 pc_dst.setPointXYZ_RGBf(i,x,y,z,r,g,b);
325 }
326 }
327 }
328} // End of namespace
329
330#endif
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
#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...
#define MRPT_DECLARE_TTYPENAME_NAMESPACE(_TYPE, __NS)
Definition: TTypeName.h:64
Template class that implements the data structure and algorithms for Octree-based efficient rendering...
A cloud of points, each one with an individual colour (R,G,B).
void clear()
Erase all the points.
std::vector< TPointColour > TListPointColour
TListPointColour::const_iterator const_iterator
void getPointColor_fast(size_t index, float &R, float &G, float &B) const
Like getPointColor but without checking for out-of-index erors.
mrpt::math::TPoint3Df getPointf(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
size_t getActuallyRendered() const
Get the number of elements actually rendered in the last render event.
float m_pointSize
By default is 1.0.
void setPoint_fast(const size_t i, const TPointColour &p)
Like setPoint() but does not check for index out of bounds.
void render() const MRPT_OVERRIDE
Render.
void reserve(size_t N)
Like STL std::vector's reserve.
virtual ~CPointCloudColoured()
Private, virtual destructor: only can be deleted from smart pointers.
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,...
void enablePointSmooth(bool enable=true)
void markAllPointsAsNew()
Do needed internal work if all points are new (octree rebuilt,...)
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...
const TPointColour & getPoint(size_t i) const
Read access to each individual point (checks for "i" in the valid range only in Debug).
void resize(size_t N)
Set the number of points, with undefined contents.
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.
size_t size() const
Return the number of points.
void push_back(float x, float y, float z, float R, float G, float B)
Inserts a new point into the point cloud.
virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color=NULL) MRPT_OVERRIDE
In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
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
In a base class, will be called after PLY_export_get_vertex_count() once for each exported point.
void setPointColor_fast(size_t index, float R, float G, float B)
Like setPointColor but without checking for out-of-index erors.
size_t PLY_export_get_vertex_count() const MRPT_OVERRIDE
In a base class, return the number of vertices.
void setPoint(size_t i, const TPointColour &p)
Write an individual point (checks for "i" in the valid range only in Debug).
TListPointColour::iterator iterator
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:45
void getPointRGBu8(const size_t idx, uint8_t &r, uint8_t &g, uint8_t &b) const
Get RGBu8 color of i'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'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'th point.
void setPointRGBf(const size_t idx, const float r, const float g, const float b)
Set XYZ_RGBf coordinates of i'th point.
PointCloudAdapter(const mrpt::opengl::CPointCloudColoured &obj)
Constructor (accept a const ref for convenience)
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i'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'th point.
void resize(const size_t N)
Set number of points (to uninitialized values)
void getPointRGBf(const size_t idx, float &r, float &g, float &b) const
Get RGBf color of i'th point.
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'th point.
void setPointRGBu8(const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
Set RGBu8 coordinates of i'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'th point.
An adapter to different kinds of point cloud object.
Definition: adapters.h:38
TColormap
Different colormaps.
Definition: color_maps.h:49
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
#define ASSERT_BELOW_(__A, __B)
Definition: mrpt_macros.h:266
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
Definition: mrpt_macros.h:290
The namespace for 3D scene representation and rendering.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
STL namespace.
unsigned char uint8_t
Definition: pstdint.h:143
Lightweight 3D point.
Lightweight 3D point (float version).
TPointColour(float _x, float _y, float _z, float _R, float _G, float _B)
A RGB color - floats in the range [0,1].
Definition: TColor.h:53



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Wed Mar 22 06:16:42 UTC 2023