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



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011