Main MRPT website > C++ reference
MRPT logo
CProbabilityDensityFunction.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 #ifndef CProbabilityDensityFunction_H
00029 #define CProbabilityDensityFunction_H
00030 
00031 #include <mrpt/math/CMatrixD.h>
00032 #include <mrpt/math/CMatrixFixedNumeric.h>
00033 
00034 namespace mrpt
00035 {
00036         namespace poses { class CPose3D; }
00037 
00038         namespace utils
00039         {
00040                 using namespace mrpt::math;
00041 
00042                 /** A generic template for probability density distributions (PDFs).
00043                   * This template is used as base for many classes in mrpt::poses
00044                   *  Any derived class must implement \a getMean() and a getCovarianceAndMean().
00045                   *  Other methods such as \a getMean() or \a getCovariance() are implemented here for convenience.
00046                   * \sa mprt::poses::CPosePDF, mprt::poses::CPose3DPDF, mprt::poses::CPointPDF
00047                  * \ingroup mrpt_base_grp
00048                   */
00049                 template <class TDATA, size_t STATE_LEN>
00050                 class CProbabilityDensityFunction
00051                 {
00052                 public:
00053                         static const size_t state_length = STATE_LEN;   //!< The length of the variable, for example, 3 for a 3D point, 6 for a 3D pose (x y z yaw pitch roll).
00054                         typedef TDATA type_value;  //!< The type of the state the PDF represents
00055 
00056                          /** Returns the mean, or mathematical expectation of the probability density distribution (PDF).
00057                            * \sa getCovarianceAndMean
00058                            */
00059                         virtual void getMean(TDATA &mean_point) const = 0;
00060 
00061                         /** Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean, both at once.
00062                           * \sa getMean
00063                           */
00064                         virtual void getCovarianceAndMean(CMatrixFixedNumeric<double,STATE_LEN,STATE_LEN> &cov,TDATA  &mean_point) const = 0;
00065 
00066                         /** Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean, both at once.
00067                           * \sa getMean
00068                           */
00069                         inline void getCovarianceDynAndMean(CMatrixDouble &cov,TDATA  &mean_point) const
00070                         {
00071                                 CMatrixFixedNumeric<double,STATE_LEN,STATE_LEN> C;
00072                                 this->getCovarianceAndMean(C,mean_point);
00073                                 cov = C; // Convert to dynamic size matrix
00074                         }
00075 
00076                         /** Returns the mean, or mathematical expectation of the probability density distribution (PDF).
00077                            * \sa getCovariance
00078                            */
00079                         inline TDATA getMeanVal() const
00080                         {
00081                                 TDATA p;
00082                                 getMean(p);
00083                                 return p;
00084                         }
00085 
00086                         /** Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix)
00087                           * \sa getMean, getCovarianceAndMean
00088                           */
00089                         inline void getCovariance(CMatrixDouble &cov) const
00090                         {
00091                                 TDATA p;
00092                                 this->getCovarianceDynAndMean(cov,p);
00093                         }
00094 
00095                         /** Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix)
00096                           * \sa getMean, getCovarianceAndMean
00097                           */
00098                         inline void getCovariance(CMatrixFixedNumeric<double,STATE_LEN,STATE_LEN> &cov) const
00099                         {
00100                                 TDATA p;
00101                                 this->getCovarianceAndMean(cov,p);
00102                         }
00103 
00104                         /** Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix)
00105                           * \sa getMean
00106                           */
00107                         inline CMatrixFixedNumeric<double,STATE_LEN,STATE_LEN> getCovariance() const
00108                         {
00109                                 CMatrixFixedNumeric<double,STATE_LEN,STATE_LEN> cov;
00110                                 TDATA p;
00111                                 this->getCovarianceAndMean(cov,p);
00112                                 return cov;
00113                         }
00114 
00115                         /** Save PDF's particles to a text file. See derived classes for more information about the format of generated files.
00116                         */
00117                         virtual void  saveToTextFile(const std::string &file) const = 0;
00118 
00119                         /** Draws a single sample from the distribution
00120                           */
00121                         virtual void  drawSingleSample( TDATA &outPart ) const = 0;
00122 
00123                         /** Draws a number of samples from the distribution, and saves as a list of 1xSTATE_LEN vectors, where each row contains a (x,y,z,yaw,pitch,roll) datum.
00124                           * This base method just call N times to drawSingleSample, but derived classes should implemented optimized method for each particular PDF.
00125                           */
00126                         virtual void  drawManySamples( size_t N, std::vector<vector_double> & outSamples ) const
00127                         {
00128                                 outSamples.resize(N);
00129                                 TDATA   pnt;
00130                                 for (size_t i=0;i<N;i++)
00131                                 {
00132                                         this->drawSingleSample(pnt);
00133                                         pnt.getAsVector(outSamples[i]);
00134                                 }
00135                         }
00136 
00137                         /** This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
00138                           *   "to project" the current pdf. Result PDF substituted the currently stored one in the object.
00139                           */
00140                         virtual void  changeCoordinatesReference( const mrpt::poses::CPose3D &newReferenceBase ) = 0;
00141 
00142                         /** Compute the entropy of the estimated covariance matrix.
00143                           * \sa http://en.wikipedia.org/wiki/Multivariate_normal_distribution#Entropy
00144                           */
00145                         inline double  getCovarianceEntropy() const
00146                         {
00147                                 static const double ln_2PI= 1.8378770664093454835606594728112;
00148                                 return 0.5*( STATE_LEN + STATE_LEN * ln_2PI + log( std::max(getCovariance().det(), std::numeric_limits<double>::epsilon() ) ) );
00149                         }
00150 
00151                 }; // End of class def.
00152 
00153         } // End of namespace
00154 } // End of namespace
00155 
00156 #endif



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