Main MRPT website > C++ reference
MRPT logo
CPointCloud.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_CPointCloud_H
00030 #define opengl_CPointCloud_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 CPointCloud;
00041 
00042                 // This must be added to any CSerializable derived class:
00043                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CPointCloud, CRenderizable, OPENGL_IMPEXP )
00044 
00045 
00046                 /** A cloud of points, all with the same color or each depending on its value along a particular coordinate axis.
00047                   *  This class is just an OpenGL representation of a point cloud. For operating with maps of points, see mrpt::slam::CPointsMap and derived classes.
00048                   *
00049                   *  To load from a points-map, CPointCloud::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::CPlanarLaserScan, opengl::COpenGLScene, opengl::CPointCloudColoured, mrpt::slam::CPointsMap
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::CPointCloud </td> <td> \image html preview_CPointCloud.png </td> </tr>
00059                   *  </table>
00060                   *  </div>
00061                   *
00062                   * \ingroup mrpt_opengl_grp
00063                   */
00064                 class OPENGL_IMPEXP CPointCloud :
00065                         public CRenderizable,
00066                         public COctreePointRenderer<CPointCloud>,
00067                         public mrpt::utils::PLY_Importer,
00068                         public mrpt::utils::PLY_Exporter
00069                 {
00070                         DEFINE_SERIALIZABLE( CPointCloud )
00071                 protected:
00072                         enum Axis { None=0, Z, Y, X} m_colorFromDepth;
00073                         std::vector<float>      m_xs,m_ys,m_zs;
00074                         float           m_pointSize; //!< By default is 1.0
00075                         bool                    m_pointSmooth; //!< Default: false
00076 
00077                         mutable volatile size_t m_last_rendered_count, m_last_rendered_count_ongoing;
00078 
00079                         void markAllPointsAsNew(); //!< Do needed internal work if all points are new (octree rebuilt,...)
00080 
00081                 protected:
00082                         /** @name PLY Import virtual methods to implement in base classes
00083                             @{ */
00084                         /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
00085                         virtual void PLY_import_set_vertex_count(const size_t N);
00086 
00087                         /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_face */
00088                         virtual void PLY_import_set_face_count(const size_t N) {  }
00089 
00090                         /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point. 
00091                           *  \param pt_color Will be NULL if the loaded file does not provide color info.
00092                           */
00093                         virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL);
00094                         /** @} */
00095 
00096                         /** @name PLY Export virtual methods to implement in base classes
00097                             @{ */
00098 
00099                         /** In a base class, return the number of vertices */
00100                         virtual size_t PLY_export_get_vertex_count() const;
00101 
00102                         /** In a base class, return the number of faces */
00103                         virtual size_t PLY_export_get_face_count() const { return 0; }
00104 
00105                         /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point. 
00106                           *  \param pt_color Will be NULL if the loaded file does not provide color info.
00107                           */
00108                         virtual void PLY_export_get_vertex(
00109                                 const size_t idx, 
00110                                 mrpt::math::TPoint3Df &pt, 
00111                                 bool &pt_has_color,
00112                                 mrpt::utils::TColorf &pt_color) const;
00113 
00114                         /** @} */
00115 
00116 
00117                 public:
00118 
00119                         /** @name Read/Write of the list of points to render
00120                             @{ */
00121 
00122                         inline size_t size() const { return m_xs.size(); }
00123 
00124                         /** Set the number of points (with contents undefined) */
00125                         inline void resize(size_t N) { m_xs.resize(N); m_ys.resize(N); m_zs.resize(N);  }
00126 
00127                         /** Like STL std::vector's reserve */
00128                         inline void reserve(size_t N) { m_xs.reserve(N); m_ys.reserve(N); m_zs.reserve(N);  }
00129 
00130                         /** Set the list of (X,Y,Z) point coordinates, all at once, from three vectors with their coordinates */
00131                         void setAllPoints(const std::vector<float> &x, const std::vector<float> &y, const std::vector<float> &z)
00132                         {
00133                                 m_xs = x;
00134                                 m_ys = y;
00135                                 m_zs = z;
00136                                 markAllPointsAsNew();
00137                         }
00138 
00139                         /** Set the list of (X,Y,Z) point coordinates, DESTROYING the contents of the input vectors (via swap) */
00140                         void setAllPointsFast(std::vector<float> &x, std::vector<float> &y, std::vector<float> &z)
00141                         {
00142                                 this->clear();
00143                                 m_xs.swap(x);
00144                                 m_ys.swap(y);
00145                                 m_zs.swap(z);
00146                                 markAllPointsAsNew();
00147                         }
00148 
00149                         inline const std::vector<float> & getArrayX() const {return m_xs;} //!< Get a const reference to the internal array of X coordinates
00150                         inline const std::vector<float> & getArrayY() const {return m_ys;} //!< Get a const reference to the internal array of Y coordinates
00151                         inline const std::vector<float> & getArrayZ() const {return m_zs;} //!< Get a const reference to the internal array of Z coordinates
00152 
00153                         void clear();   //!< Empty the list of points.
00154 
00155                         /** Adds a new point to the cloud */
00156                         void insertPoint( float x,float y, float z );
00157 
00158                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00159                         inline mrpt::math::TPoint3D  operator [](size_t i) const {
00160 #ifdef _DEBUG
00161                                 ASSERT_BELOW_(i,size())
00162 #endif
00163                                 return mrpt::math::TPoint3D(m_xs[i],m_ys[i],m_zs[i]);
00164                         }
00165 
00166                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00167                         inline mrpt::math::TPoint3D getPoint(size_t i) const {
00168 #ifdef _DEBUG
00169                                 ASSERT_BELOW_(i,size())
00170 #endif
00171                                 return mrpt::math::TPoint3D(m_xs[i],m_ys[i],m_zs[i]);
00172                         }
00173 
00174                         /** Read access to each individual point (checks for "i" in the valid range only in Debug). */
00175                         inline mrpt::math::TPoint3Df getPointf(size_t i) const {
00176 #ifdef _DEBUG
00177                                 ASSERT_BELOW_(i,size())
00178 #endif
00179                                 return mrpt::math::TPoint3Df(m_xs[i],m_ys[i],m_zs[i]);
00180                         }
00181 
00182                         /** Write an individual point (checks for "i" in the valid range only in Debug). */
00183                         void setPoint(size_t i, const float x,const float y, const float z);
00184 
00185 
00186                         /** Load the points from a pointsMap (mrpt::slam::CPointsMap), passed as a pointer.
00187                           * Note that the method is a template since CPointsMap belongs to a different mrpt library.
00188                           */
00189                         template <class POINTSMAP>
00190                         inline void  loadFromPointsMap( const POINTSMAP *themap) {
00191                                 themap->getAllPoints(m_xs,m_ys,m_zs);
00192                                 markAllPointsAsNew();
00193                         }
00194 
00195                         /** Load the points from a list of TPoint3D
00196                           */
00197                         template<class LISTOFPOINTS> void  loadFromPointsList( LISTOFPOINTS &pointsList)
00198                         {
00199                                 MRPT_START
00200                                 const size_t N = pointsList.size();
00201 
00202                                 m_xs.resize(N);
00203                                 m_ys.resize(N);
00204                                 m_zs.resize(N);
00205 
00206                                 size_t idx;
00207                                 typename LISTOFPOINTS::const_iterator it;
00208                                 for ( idx=0,it=pointsList.begin() ; idx<N ; ++idx,++it)
00209                                 {
00210                                         m_xs[idx]=it->x;
00211                                         m_ys[idx]=it->y;
00212                                         m_zs[idx]=it->z;
00213                                 }
00214                                 markAllPointsAsNew();
00215                                 MRPT_END
00216                         }
00217 
00218                         /** Get the number of elements actually rendered in the last render event. */
00219                         size_t getActuallyRendered() const { return m_last_rendered_count; }
00220 
00221                         /** @} */
00222 
00223 
00224                         /** @name Modify the appearance of the rendered points
00225                             @{ */
00226                         inline void enableColorFromX(bool v=true) { m_colorFromDepth = v ? CPointCloud::X : CPointCloud::None;  }
00227                         inline void enableColorFromY(bool v=true) { m_colorFromDepth = v ? CPointCloud::Y : CPointCloud::None; }
00228                         inline void enableColorFromZ(bool v=true) { m_colorFromDepth = v ? CPointCloud::Z : CPointCloud::None; }
00229 
00230                         inline void setPointSize(float p) { m_pointSize=p; }  //!< By default is 1.0
00231                         inline float getPointSize() const { return m_pointSize; }
00232 
00233                         inline void enablePointSmooth(bool enable=true) { m_pointSmooth=enable; }
00234                         inline void disablePointSmooth() { m_pointSmooth=false; }
00235                         inline bool isPointSmoothEnabled() const { return m_pointSmooth; }
00236 
00237                         /** Sets the colors used as extremes when colorFromDepth is enabled. */
00238                         void  setGradientColors( const mrpt::utils::TColorf &colorMin, const mrpt::utils::TColorf &colorMax );
00239 
00240                         /** @} */
00241 
00242                         /** Render */
00243                         void  render() const;
00244 
00245 
00246                         /** Render a subset of points (required by octree renderer) */
00247                         void  render_subset(const bool all, const std::vector<size_t>& idxs, const float render_area_sqpixels ) const;
00248 
00249                 private:
00250                         /** Constructor */
00251                         CPointCloud();
00252 
00253                         /** Private, virtual destructor: only can be deleted from smart pointers */
00254                         virtual ~CPointCloud() { }
00255 
00256                         mutable float  m_min, m_max,m_max_m_min,m_max_m_min_inv;        //!< Buffer for min/max coords when m_colorFromDepth is true.
00257                         mutable mrpt::utils::TColorf m_col_slop,m_col_slop_inv; //!< Color linear function slope
00258                         mutable bool   m_minmax_valid;
00259 
00260                         mrpt::utils::TColorf    m_colorFromDepth_min, m_colorFromDepth_max;     //!< The colors used to interpolate when m_colorFromDepth is true.
00261 
00262                         inline void internal_render_one_point(size_t i) const;
00263                 };
00264 
00265         } // end namespace
00266 
00267 
00268 
00269 } // End of namespace
00270 
00271 
00272 #endif



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