Main MRPT website > C++ reference
MRPT logo
CPointCloudColoured.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 
00029 #ifndef opengl_CPointCloudColoured_H
00030 #define opengl_CPointCloudColoured_H
00031 
00032 #include <mrpt/opengl/CRenderizable.h>
00033 #include <mrpt/opengl/COctreePointRenderer.h>
00034 #include <mrpt/utils/PLY_import_export.h>
00035 #include <mrpt/utils/adapters.h>
00036 
00037 namespace mrpt
00038 {
00039         namespace opengl
00040         {
00041                 class OPENGL_IMPEXP CPointCloudColoured;
00042 
00043                 // This must be added to any CSerializable derived class:
00044                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CPointCloudColoured, CRenderizable, OPENGL_IMPEXP )
00045 
00046 
00047                 /** 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.
00048                   *
00049                   *  To load from a points-map, CPointCloudColoured::loadFromPointsMap().
00050                   *
00051                   *   This class uses smart optimizations while rendering to efficiently draw clouds of millions of points,
00052                   *   as described in this page: http://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
00053                   *
00054                   *  \sa opengl::COpenGLScene, opengl::CPointCloud
00055                   *
00056                   *  <div align="center">
00057                   *  <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
00058                   *   <tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html preview_CPointCloudColoured.png </td> </tr>
00059                   *  </table>
00060                   *  </div>
00061                   *
00062                   * \ingroup mrpt_opengl_grp
00063                   */
00064                 class OPENGL_IMPEXP CPointCloudColoured :
00065                         public CRenderizable,
00066                         public COctreePointRenderer<CPointCloudColoured>,
00067                         public mrpt::utils::PLY_Importer,
00068                         public mrpt::utils::PLY_Exporter
00069                 {
00070                         DEFINE_SERIALIZABLE( CPointCloudColoured )
00071 
00072                 public:
00073                         struct TPointColour
00074                         {
00075                                 inline TPointColour() { }
00076                                 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) { }
00077                                 float x,y,z,R,G,B;      // Float is precission enough for rendering
00078                         };
00079 
00080                 private:
00081                         typedef std::vector<TPointColour> TListPointColour;
00082                         TListPointColour        m_points;
00083 
00084                         typedef TListPointColour::iterator iterator;
00085                         typedef TListPointColour::const_iterator const_iterator;
00086                         inline iterator begin() { return m_points.begin(); }
00087                         inline const_iterator begin() const { return m_points.begin(); }
00088                         inline iterator end() { return m_points.end(); }
00089                         inline const_iterator end() const { return m_points.end(); }
00090 
00091 
00092                         float                           m_pointSize; //!< By default is 1.0
00093                         bool                            m_pointSmooth; //!< Default: false
00094                         mutable volatile size_t m_last_rendered_count, m_last_rendered_count_ongoing;
00095 
00096                         /** Constructor
00097                           */
00098                         CPointCloudColoured( ) :
00099                                 m_points(),
00100                                 m_pointSize(1),
00101                                 m_pointSmooth(false),
00102                                 m_last_rendered_count(0),
00103                                 m_last_rendered_count_ongoing(0)
00104                         {
00105                         }
00106                         /** Private, virtual destructor: only can be deleted from smart pointers */
00107                         virtual ~CPointCloudColoured() { }
00108 
00109                         void markAllPointsAsNew(); //!< Do needed internal work if all points are new (octree rebuilt,...)
00110 
00111                 public:
00112 
00113                         /** @name Read/Write of the list of points to render
00114                             @{ */
00115 
00116                         /** Inserts a new point into the point cloud. */
00117                         void push_back(float x,float y,float z, float R, float G, float B);
00118 
00119                         /** Set the number of points, with undefined contents */
00120                         inline void resize(size_t N) { m_points.resize(N); markAllPointsAsNew(); }
00121 
00122                         /** Like STL std::vector's reserve */
00123                         inline void reserve(size_t N) { m_points.reserve(N); }
00124 
00125                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00126                         inline const TPointColour &operator [](size_t i) const {
00127 #ifdef _DEBUG
00128                                 ASSERT_BELOW_(i,size())
00129 #endif
00130                                 return m_points[i];
00131                         }
00132 
00133                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00134                         inline const TPointColour &getPoint(size_t i) const {
00135 #ifdef _DEBUG
00136                                 ASSERT_BELOW_(i,size())
00137 #endif
00138                                 return m_points[i];
00139                         }
00140 
00141                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00142                         inline mrpt::math::TPoint3Df getPointf(size_t i) const {
00143 #ifdef _DEBUG
00144                                 ASSERT_BELOW_(i,size())
00145 #endif
00146                                 return mrpt::math::TPoint3Df(m_points[i].x,m_points[i].y,m_points[i].z);
00147                         }
00148 
00149                         /** Write an individual point (checks for "i" in the valid range only in Debug). */
00150                         void setPoint(size_t i, const TPointColour &p );
00151 
00152                         /** Like \a setPoint() but does not check for index out of bounds */
00153                         inline void setPoint_fast(const size_t i, const TPointColour &p ) {
00154                                 m_points[i] = p;
00155                                 markAllPointsAsNew();
00156                         }
00157 
00158                         /** Like \a setPoint() but does not check for index out of bounds */
00159                         inline void setPoint_fast(const size_t i, const float x,const float y, const float z ) {
00160                                 TPointColour &p = m_points[i];
00161                                 p.x=x; p.y=y; p.z=z;
00162                                 markAllPointsAsNew();
00163                         }
00164 
00165                         /** Like \c setPointColor but without checking for out-of-index erors */
00166                         inline void setPointColor_fast(size_t index,float R, float G, float B)
00167                         {
00168                                 m_points[index].R=R;
00169                                 m_points[index].G=G;
00170                                 m_points[index].B=B;
00171                         }
00172                         /** Like \c getPointColor but without checking for out-of-index erors */
00173                         inline void getPointColor_fast( size_t index, float &R, float &G, float &B ) const
00174                         {
00175                                 R = m_points[index].R;
00176                                 G = m_points[index].G;
00177                                 B = m_points[index].B;
00178                         }
00179 
00180                         inline size_t size() const { return m_points.size(); } //!< Return the number of points
00181 
00182                         inline void clear() { m_points.clear(); markAllPointsAsNew(); }  //!< Erase all the points
00183 
00184                         /** Load the points from any other point map class supported by the adapter mrpt::utils::PointCloudAdapter. */
00185                         template <class POINTSMAP>
00186                         void  loadFromPointsMap( const POINTSMAP *themap);
00187                         // Must be implemented at the end of the header.
00188 
00189                         /** Get the number of elements actually rendered in the last render event. */
00190                         size_t getActuallyRendered() const { return m_last_rendered_count; }
00191 
00192                         /** @} */
00193 
00194 
00195                         /** @name Modify the appearance of the rendered points
00196                             @{ */
00197 
00198                         inline void setPointSize(float pointSize) { m_pointSize = pointSize; }
00199                         inline float getPointSize() const { return m_pointSize; }
00200 
00201                         inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
00202                         inline void disablePointSmooth() { m_pointSmooth=false; }
00203                         inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
00204 
00205                         /** @} */
00206 
00207 
00208                         /** Render */
00209                         void  render() const;
00210 
00211                         /** Render a subset of points (required by octree renderer) */
00212                         void  render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
00213 
00214                 protected:
00215                         /** @name PLY Import virtual methods to implement in base classes
00216                             @{ */
00217                         /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
00218                         virtual void PLY_import_set_vertex_count(const size_t N);
00219                         /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face */
00220                         virtual void PLY_import_set_face_count(const size_t N) {  }
00221                         /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
00222                           *  \param pt_color Will be NULL if the loaded file does not provide color info.
00223                           */
00224                         virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL);
00225                         /** @} */
00226 
00227                         /** @name PLY Export virtual methods to implement in base classes
00228                             @{ */
00229 
00230                         /** In a base class, return the number of vertices */
00231                         virtual size_t PLY_export_get_vertex_count() const;
00232 
00233                         /** In a base class, return the number of faces */
00234                         virtual size_t PLY_export_get_face_count() const { return 0; }
00235 
00236                         /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point.
00237                           *  \param pt_color Will be NULL if the loaded file does not provide color info.
00238                           */
00239                         virtual void PLY_export_get_vertex(
00240                                 const size_t idx,
00241                                 mrpt::math::TPoint3Df &pt,
00242                                 bool &pt_has_color,
00243                                 mrpt::utils::TColorf &pt_color) const;
00244 
00245                         /** @} */
00246 
00247 
00248                 };
00249 
00250                 OPENGL_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,  CPointCloudColoured::TPointColour &o);
00251                 OPENGL_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out, const CPointCloudColoured::TPointColour &o);
00252 
00253         } // end namespace
00254 
00255         namespace utils
00256         {
00257                 using namespace mrpt::opengl;
00258 
00259                 // Specialization must occur in the same namespace
00260                 MRPT_DECLARE_TTYPENAME(CPointCloudColoured::TPointColour)
00261         }
00262 
00263         namespace utils
00264         {
00265                 /** Specialization mrpt::utils::PointCloudAdapter<mrpt::opengl::CPointCloudColoured>  \ingroup mrpt_adapters_grp*/
00266                 template <>
00267                 class PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
00268                 {
00269                 private:
00270                         mrpt::opengl::CPointCloudColoured &m_obj;
00271                 public:
00272                         typedef float  coords_t;  //!< The type of each point XYZ coordinates
00273                         static const int HAS_RGB   = 1;  //!< Has any color RGB info?
00274                         static const int HAS_RGBf  = 1;  //!< Has native RGB info (as floats)?
00275                         static const int HAS_RGBu8 = 0;  //!< Has native RGB info (as uint8_t)?
00276 
00277                         /** Constructor (accept a const ref for convenience) */
00278                         inline PointCloudAdapter(const mrpt::opengl::CPointCloudColoured &obj) : m_obj(*const_cast<mrpt::opengl::CPointCloudColoured*>(&obj)) { }
00279                         /** Get number of points */
00280                         inline size_t size() const { return m_obj.size(); }
00281                         /** Set number of points (to uninitialized values) */
00282                         inline void resize(const size_t N) { m_obj.resize(N); }
00283 
00284                         /** Get XYZ coordinates of i'th point */
00285                         template <typename T>
00286                         inline void getPointXYZ(const size_t idx, T &x,T &y, T &z) const {
00287                                 const mrpt::opengl::CPointCloudColoured::TPointColour &pc = m_obj[idx];
00288                                 x=pc.x;
00289                                 y=pc.y;
00290                                 z=pc.z;
00291                         }
00292                         /** Set XYZ coordinates of i'th point */
00293                         inline void setPointXYZ(const size_t idx, const coords_t x,const coords_t y, const coords_t z) {
00294                                 m_obj.setPoint_fast(idx, x,y,z);
00295                         }
00296 
00297                         /** Get XYZ_RGBf coordinates of i'th point */
00298                         template <typename T>
00299                         inline void getPointXYZ_RGBf(const size_t idx, T &x,T &y, T &z, float &r,float &g,float &b) const {
00300                                 const mrpt::opengl::CPointCloudColoured::TPointColour &pc = m_obj[idx];
00301                                 x=pc.x; y=pc.y; z=pc.z;
00302                                 r=pc.R; g=pc.G; b=pc.B;
00303                         }
00304                         /** Set XYZ_RGBf coordinates of i'th point */
00305                         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) {
00306                                 m_obj.setPoint_fast(idx, mrpt::opengl::CPointCloudColoured::TPointColour(x,y,z,r,g,b) );
00307                         }
00308 
00309                         /** Get XYZ_RGBu8 coordinates of i'th point */
00310                         template <typename T>
00311                         inline void getPointXYZ_RGBu8(const size_t idx, T &x,T &y, T &z, uint8_t &r,uint8_t &g,uint8_t &b) const {
00312                                 const mrpt::opengl::CPointCloudColoured::TPointColour &pc = m_obj[idx];
00313                                 x=pc.x; y=pc.y; z=pc.z;
00314                                 r=pc.R*255; g=pc.G*255; b=pc.B*255;
00315                         }
00316                         /** Set XYZ_RGBu8 coordinates of i'th point */
00317                         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) {
00318                                 m_obj.setPoint_fast(idx, mrpt::opengl::CPointCloudColoured::TPointColour(x,y,z,r/255.f,g/255.f,b/255.f) );
00319                         }
00320 
00321                         /** Get RGBf color of i'th point */
00322                         inline void getPointRGBf(const size_t idx, float &r,float &g,float &b) const { m_obj.getPointColor_fast(idx,r,g,b); }
00323                         /** Set XYZ_RGBf coordinates of i'th point */
00324                         inline void setPointRGBf(const size_t idx, const float r,const float g,const float b) { m_obj.setPointColor_fast(idx,r,g,b); }
00325 
00326                         /** Get RGBu8 color of i'th point */
00327                         inline void getPointRGBu8(const size_t idx, uint8_t &r,uint8_t &g,uint8_t &b) const {
00328                                 float R,G,B;
00329                                 m_obj.getPointColor_fast(idx,R,G,B);
00330                                 r=R*255; g=G*255; b=B*255;
00331                         }
00332                         /** Set RGBu8 coordinates of i'th point */
00333                         inline void setPointRGBu8(const size_t idx,const uint8_t r,const uint8_t g,const uint8_t b) {
00334                                 m_obj.setPointColor_fast(idx,r/255.f,g/255.f,b/255.f);
00335                         }
00336 
00337                 }; // end of PointCloudAdapter<mrpt::opengl::CPointCloudColoured>
00338 
00339         }
00340 
00341         namespace opengl
00342         {
00343                 // After declaring the adapter we can here implement this method:
00344                 template <class POINTSMAP>
00345                 void  CPointCloudColoured::loadFromPointsMap( const POINTSMAP *themap)
00346                 {
00347                         mrpt::utils::PointCloudAdapter<CPointCloudColoured> pc_dst(*this);
00348                         const mrpt::utils::PointCloudAdapter<POINTSMAP>     pc_src(*themap);
00349                         const size_t N=pc_src.size();
00350                         pc_dst.resize(N);
00351                         for (size_t i=0;i<N;i++)
00352                         {
00353                                 float x,y,z,r,g,b;
00354                                 pc_src.getPointXYZ_RGBf(i,x,y,z,r,g,b);
00355                                 pc_dst.setPointXYZ_RGBf(i,x,y,z,r,g,b);
00356                         }
00357                 }
00358         }
00359 
00360 } // End of namespace
00361 
00362 
00363 #endif



Page generated by Doxygen 1.7.4 for MRPT 0.9.5 SVN:2717 at Sun Oct 16 16:08:03 PDT 2011 Hosted on:
SourceForge.net Logo