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 
00036 namespace mrpt
00037 {
00038         namespace opengl
00039         {
00040                 class OPENGL_IMPEXP CPointCloudColoured;
00041 
00042                 // This must be added to any CSerializable derived class:
00043                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CPointCloudColoured, CRenderizable, OPENGL_IMPEXP )
00044 
00045 
00046                 /** 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.
00047                   *
00048                   *  To load from a points-map, CPointCloudColoured::loadFromPointsMap().
00049                   *
00050                   *   This class uses smart optimizations while rendering to efficiently draw clouds of millions of points,
00051                   *   as described in this page: http://www.mrpt.org/Efficiently_rendering_point_clouds_of_millions_of_points
00052                   *
00053                   *  \sa opengl::COpenGLScene, opengl::CPointCloud
00054                   *
00055                   *  <div align="center">
00056                   *  <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px; border-style: solid;">
00057                   *   <tr> <td> mrpt::opengl::CPointCloudColoured </td> <td> \image html preview_CPointCloudColoured.png </td> </tr>
00058                   *  </table>
00059                   *  </div>
00060                   *
00061                   * \ingroup mrpt_opengl_grp
00062                   */
00063                 class OPENGL_IMPEXP CPointCloudColoured :
00064                         public CRenderizable,
00065                         public COctreePointRenderer<CPointCloudColoured>,
00066                         public mrpt::utils::PLY_Importer,
00067                         public mrpt::utils::PLY_Exporter
00068                 {
00069                         DEFINE_SERIALIZABLE( CPointCloudColoured )
00070 
00071                 public:
00072                         struct TPointColour
00073                         {
00074                                 inline TPointColour() { }
00075                                 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) { }
00076                                 float x,y,z,R,G,B;      // Float is precission enough for rendering
00077                         };
00078 
00079                 private:
00080                         typedef std::vector<TPointColour> TListPointColour;
00081                         TListPointColour        m_points;
00082 
00083                         typedef TListPointColour::iterator iterator;
00084                         typedef TListPointColour::const_iterator const_iterator;
00085                         inline iterator begin() { return m_points.begin(); }
00086                         inline const_iterator begin() const { return m_points.begin(); }
00087                         inline iterator end() { return m_points.end(); }
00088                         inline const_iterator end() const { return m_points.end(); }
00089 
00090 
00091                         float                           m_pointSize; //!< By default is 1.0
00092                         bool                            m_pointSmooth; //!< Default: false
00093                         mutable volatile size_t m_last_rendered_count, m_last_rendered_count_ongoing;
00094 
00095                         /** Constructor
00096                           */
00097                         CPointCloudColoured( ) :
00098                                 m_points(),
00099                                 m_pointSize(1),
00100                                 m_pointSmooth(false),
00101                                 m_last_rendered_count(0),
00102                                 m_last_rendered_count_ongoing(0)
00103                         {
00104                         }
00105                         /** Private, virtual destructor: only can be deleted from smart pointers */
00106                         virtual ~CPointCloudColoured() { }
00107 
00108                         void markAllPointsAsNew(); //!< Do needed internal work if all points are new (octree rebuilt,...)
00109 
00110                 public:
00111 
00112                         /** @name Read/Write of the list of points to render
00113                             @{ */
00114 
00115                         /** Inserts a new point into the point cloud. */
00116                         void push_back(float x,float y,float z, float R, float G, float B);
00117 
00118                         /** Set the number of points, with undefined contents */
00119                         inline void resize(size_t N) { m_points.resize(N); }
00120 
00121                         /** Like STL std::vector's reserve */
00122                         inline void reserve(size_t N) { m_points.reserve(N); }
00123 
00124                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00125                         inline const TPointColour &operator [](size_t i) const {
00126 #ifdef _DEBUG
00127                                 ASSERT_BELOW_(i,size())
00128 #endif
00129                                 return m_points[i];
00130                         }
00131 
00132                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00133                         inline const TPointColour &getPoint(size_t i) const {
00134 #ifdef _DEBUG
00135                                 ASSERT_BELOW_(i,size())
00136 #endif
00137                                 return m_points[i];
00138                         }
00139 
00140                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00141                         inline mrpt::math::TPoint3Df getPointf(size_t i) const {
00142 #ifdef _DEBUG
00143                                 ASSERT_BELOW_(i,size())
00144 #endif
00145                                 return mrpt::math::TPoint3Df(m_points[i].x,m_points[i].y,m_points[i].z);
00146                         }
00147 
00148                         /** Write an individual point (checks for "i" in the valid range only in Debug). */
00149                         void setPoint(size_t i, const TPointColour &p );
00150 
00151                         inline size_t size() const { return m_points.size(); } //!< Return the number of points
00152 
00153                         inline void clear() { m_points.clear(); markAllPointsAsNew(); }  //!< Erase all the points
00154 
00155 
00156                         /** Load the points from a points map (passed as a pointer), depending on the type of point map passed: for the case of a mrpt::slam::CColouredPointMap the colours of individual points will be also copied.
00157                           *  The possible classes accepted as arguments are: mrpt::slam::CColouredPointsMap, or in general any mrpt::slam::CPointsMap.
00158                           * \note The method is a template since CPointsMap belongs to a different mrpt library.
00159                           */
00160                         template <class POINTSMAP>
00161                         void  loadFromPointsMap( const POINTSMAP *m)
00162                         {
00163                                 if (m->hasColorPoints())
00164                                 {
00165                                         size_t N = m->size();
00166                                         m_points.resize(N);
00167                                         for (size_t i=0;i<N;i++)
00168                                         {
00169                                                 m->getPoint(i,
00170                                                         m_points[i].x,
00171                                                         m_points[i].y,
00172                                                         m_points[i].z,
00173                                                         m_points[i].R,
00174                                                         m_points[i].G,
00175                                                         m_points[i].B );
00176                                         }
00177                                 }
00178                                 else
00179                                 {
00180                                         // Default colors:
00181                                         vector_float    xs,ys,zs;
00182                                         m->getAllPoints(xs,ys,zs);
00183 
00184                                         size_t N = xs.size();
00185                                         m_points.resize(N);
00186                                         const mrpt::utils::TColorf col(m_color);
00187                                         for (size_t i=0;i<N;i++)
00188                                         {
00189                                                 m_points[i].x = xs[i];
00190                                                 m_points[i].y = ys[i];
00191                                                 m_points[i].z = zs[i];
00192                                                 m_points[i].R = col.R;
00193                                                 m_points[i].G = col.G;
00194                                                 m_points[i].B = col.B;
00195                                         }
00196                                 }
00197                                 markAllPointsAsNew();
00198                         }
00199 
00200                         /** Get the number of elements actually rendered in the last render event. */
00201                         size_t getActuallyRendered() const { return m_last_rendered_count; }
00202 
00203                         /** @} */
00204 
00205 
00206                         /** @name Modify the appearance of the rendered points
00207                             @{ */
00208 
00209                         inline void setPointSize(float pointSize) { m_pointSize = pointSize; }
00210                         inline float getPointSize() const { return m_pointSize; }
00211 
00212                         inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
00213                         inline void disablePointSmooth() { m_pointSmooth=false; }
00214                         inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
00215 
00216                         /** @} */
00217 
00218 
00219                         /** Render */
00220                         void  render() const;
00221 
00222                         /** Render a subset of points (required by octree renderer) */
00223                         void  render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
00224 
00225                 protected:
00226                         /** @name PLY Import virtual methods to implement in base classes
00227                             @{ */
00228                         /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
00229                         virtual void PLY_import_set_vertex_count(const size_t N);
00230                         /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face */
00231                         virtual void PLY_import_set_face_count(const size_t N) {  } 
00232                         /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point. 
00233                           *  \param pt_color Will be NULL if the loaded file does not provide color info.
00234                           */
00235                         virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL);
00236                         /** @} */
00237 
00238                         /** @name PLY Export virtual methods to implement in base classes
00239                             @{ */
00240 
00241                         /** In a base class, return the number of vertices */
00242                         virtual size_t PLY_export_get_vertex_count() const;
00243 
00244                         /** In a base class, return the number of faces */
00245                         virtual size_t PLY_export_get_face_count() const { return 0; }
00246 
00247                         /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point. 
00248                           *  \param pt_color Will be NULL if the loaded file does not provide color info.
00249                           */
00250                         virtual void PLY_export_get_vertex(
00251                                 const size_t idx, 
00252                                 mrpt::math::TPoint3Df &pt, 
00253                                 bool &pt_has_color,
00254                                 mrpt::utils::TColorf &pt_color) const;
00255 
00256                         /** @} */
00257 
00258 
00259                 };
00260 
00261                 OPENGL_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,  CPointCloudColoured::TPointColour &o);
00262                 OPENGL_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out, const CPointCloudColoured::TPointColour &o);
00263 
00264         } // end namespace
00265 
00266         namespace utils
00267         {
00268                 using namespace mrpt::opengl;
00269 
00270                 // Specialization must occur in the same namespace
00271                 MRPT_DECLARE_TTYPENAME(CPointCloudColoured::TPointColour)
00272         }
00273 
00274 } // End of namespace
00275 
00276 
00277 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011