Main MRPT website > C++ reference
MRPT logo

CPosePDFGaussianInf.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 #ifndef CPosePDFGaussianInf_H
00029 #define CPosePDFGaussianInf_H
00030 
00031 #include <mrpt/poses/CPosePDF.h>
00032 #include <mrpt/math/CMatrixFixedNumeric.h>
00033 
00034 namespace mrpt
00035 {
00036 namespace poses
00037 {
00038         using namespace mrpt::math;
00039 
00040         class CPose3DPDF;
00041 
00042         // This must be added to any CSerializable derived class:
00043         DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CPosePDFGaussianInf, CPosePDF )
00044 
00045         /** A Probability Density  function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$ as a Gaussian with a mean and the inverse of the covariance.
00046          *
00047          *   This class implements a PDF as a mono-modal Gaussian distribution in its <b>information form</b>, that is,
00048          *     keeping the inverse of the covariance matrix instead of the covariance matrix itself.
00049          *
00050          *  This class is the dual of CPosePDFGaussian.
00051      *
00052          * \sa CPose2D, CPosePDF, CPosePDFParticles
00053          */
00054         class BASE_IMPEXP CPosePDFGaussianInf : public CPosePDF
00055         {
00056                 // This must be added to any CSerializable derived class:
00057                 DEFINE_SERIALIZABLE( CPosePDFGaussianInf )
00058 
00059         protected:
00060                 /** Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
00061                   */
00062                 void  assureSymmetry();
00063 
00064          public:
00065                 /** @name Data fields
00066                         @{ */
00067 
00068                 CPose2D         mean;   //!< The mean value
00069                 CMatrixDouble33         cov_inv;        //!< The inverse of the 3x3 covariance matrix (the "information" matrix)
00070 
00071                 /** @} */
00072 
00073 
00074                 /** Default constructor (mean=all zeros, inverse covariance=all zeros -> so be careful!) */
00075                 CPosePDFGaussianInf();
00076 
00077                 /** Constructor with a mean value (inverse covariance=all zeros -> so be careful!) */
00078                 explicit CPosePDFGaussianInf( const CPose2D &init_Mean );
00079 
00080                 /** Constructor */
00081                 CPosePDFGaussianInf( const CPose2D &init_Mean, const CMatrixDouble33 &init_CovInv );
00082 
00083             /** Copy constructor, including transformations between other PDFs */
00084                 explicit CPosePDFGaussianInf( const CPosePDF &o ) { copyFrom( o ); }
00085 
00086                 /** Copy constructor, including transformations between other PDFs */
00087                 explicit CPosePDFGaussianInf( const CPose3DPDF &o ) { copyFrom( o ); }
00088 
00089 
00090                  /** Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
00091                    * \sa getCovariance
00092                    */
00093                 void getMean(CPose2D &mean_pose) const;
00094 
00095                 /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once.
00096                   * \sa getMean
00097                   */
00098                 void getCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const;
00099 
00100                 /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
00101                   */
00102                 void  copyFrom(const CPosePDF &o);
00103 
00104                 /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
00105                   */
00106                 void  copyFrom(const CPose3DPDF &o);
00107 
00108                 /** Save PDF's particles to a text file, containing the 2D pose in the first line, then the covariance matrix in next 3 lines.
00109                  */
00110                 void  saveToTextFile(const std::string &file) const;
00111 
00112                 /** This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
00113                   *   "to project" the current pdf. Result PDF substituted the currently stored one in the object.
00114                   */
00115                 void  changeCoordinatesReference( const CPose3D &newReferenceBase );
00116 
00117                 /** This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
00118                   *   "to project" the current pdf. Result PDF substituted the currently stored one in the object.
00119                   */
00120                 void  changeCoordinatesReference( const CPose2D &newReferenceBase );
00121 
00122                 /** Rotate the covariance matrix by replacing it by \f$ \mathbf{R}~\mathbf{COV}~\mathbf{R}^t \f$, where \f$ \mathbf{R} = \left[ \begin{array}{ccc} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha & \cos\alpha & 0 \\ 0 & 0 & 1 \end{array}\right] \f$.
00123                   */
00124                 void  rotateCov(const double ang);
00125 
00126                 /** Set \f$ this = x1 \ominus x0 \f$ , computing the mean using the "-" operator and the covariances through the corresponding Jacobians (For 'x0' and 'x1' being independent variables!).
00127                   */
00128                 void inverseComposition( const CPosePDFGaussianInf &x, const CPosePDFGaussianInf &ref  );
00129 
00130                 /** Set \f$ this = x1 \ominus x0 \f$ , computing the mean using the "-" operator and the covariances through the corresponding Jacobians (Given the 3x3 cross-covariance matrix of variables x0 and x1).
00131                   */
00132                 void inverseComposition(
00133                         const CPosePDFGaussianInf &x1,
00134                         const CPosePDFGaussianInf &x0,
00135                         const CMatrixDouble33  &COV_01
00136                         );
00137 
00138                 /** Draws a single sample from the distribution
00139                   */
00140                 void  drawSingleSample( CPose2D &outPart ) const;
00141 
00142                 /** Draws a number of samples from the distribution, and saves as a list of 1x3 vectors, where each row contains a (x,y,phi) datum.
00143                   */
00144                 void  drawManySamples( size_t N, std::vector<vector_double> & outSamples ) const;
00145 
00146                 /** Bayesian fusion of two points gauss. distributions, then save the result in this object.
00147                   *  The process is as follows:<br>
00148                   *             - (x1,S1): Mean and variance of the p1 distribution.
00149                   *             - (x2,S2): Mean and variance of the p2 distribution.
00150                   *             - (x,S): Mean and variance of the resulting distribution.
00151                   *
00152                   *    S = (S1<sup>-1</sup> + S2<sup>-1</sup>)<sup>-1</sup>;
00153                   *    x = S * ( S1<sup>-1</sup>*x1 + S2<sup>-1</sup>*x2 );
00154                   */
00155                 void  bayesianFusion(const  CPosePDF &p1,const  CPosePDF &p2, const double &minMahalanobisDistToDrop = 0 );
00156 
00157                 /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
00158                   */
00159                 void     inverse(CPosePDF &o) const;
00160 
00161                 /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated). */
00162                 void  operator += ( const CPose2D &Ap);
00163 
00164                 /** Evaluates the PDF at a given point.
00165                   */
00166                 double  evaluatePDF( const CPose2D &x ) const;
00167 
00168                 /** Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
00169                   */
00170                 double  evaluateNormalizedPDF( const CPose2D &x ) const;
00171 
00172                 /** Computes the Mahalanobis distance between the centers of two Gaussians.
00173                   */
00174                 double  mahalanobisDistanceTo( const CPosePDFGaussianInf& theOther );
00175 
00176                 /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated) (see formulas in jacobiansPoseComposition ).
00177                   */
00178                 void  operator += ( const CPosePDFGaussianInf &Ap);
00179 
00180                 /** Makes: thisPDF = thisPDF - Ap, where "-" is pose inverse composition (both the mean, and the covariance matrix are updated)
00181                   */
00182                 inline void operator -=( const CPosePDFGaussianInf &ref  ) {
00183                         this->inverseComposition(*this,ref);
00184                 }
00185 
00186 
00187 
00188                 /** This static method computes the pose composition Jacobians, with these formulas:
00189                         \code
00190                                 df_dx =
00191                                 [ 1, 0, -sin(phi_x)*x_u-cos(phi_x)*y_u ]
00192                                 [ 0, 1,  cos(phi_x)*x_u-sin(phi_x)*y_u ]
00193                                 [ 0, 0,                              1 ]
00194 
00195                                 df_du =
00196                                 [ cos(phi_x) , -sin(phi_x) ,  0  ]
00197                                 [ sin(phi_x) ,  cos(phi_x) ,  0  ]
00198                                 [         0  ,          0  ,  1  ]
00199                         \endcode
00200                   */
00201                 static void jacobiansPoseComposition(
00202                         const CPosePDFGaussianInf &x,
00203                         const CPosePDFGaussianInf &u,
00204                         CMatrixDouble33                  &df_dx,
00205                         CMatrixDouble33                  &df_du);
00206 
00207 
00208 
00209         }; // End of class def.
00210 
00211 
00212         /** Pose compose operator: RES = A (+) B , computing both the mean and the covariance */
00213         inline CPosePDFGaussianInf operator +( const CPosePDFGaussianInf &a, const CPosePDFGaussianInf &b  ) {
00214                 CPosePDFGaussianInf res(a);
00215                 res+=b;
00216                 return res;
00217         }
00218 
00219         /** Pose inverse compose operator: RES = A (-) B , computing both the mean and the covariance */
00220         inline CPosePDFGaussianInf operator -( const CPosePDFGaussianInf &a, const CPosePDFGaussianInf &b  ) {
00221                 CPosePDFGaussianInf res;
00222                 res.inverseComposition(a,b);
00223                 return res;
00224         }
00225 
00226         /** Dumps the mean and covariance matrix to a text stream.
00227           */
00228         std::ostream BASE_IMPEXP & operator << (std::ostream & out, const CPosePDFGaussianInf& obj);
00229 
00230         /** Returns the Gaussian distribution of \f$ \mathbf{C} \f$, for \f$ \mathbf{C} = \mathbf{A} \oplus \mathbf{B} \f$.
00231           */
00232         poses::CPosePDFGaussianInf      BASE_IMPEXP operator + ( const mrpt::poses::CPose2D &A, const mrpt::poses::CPosePDFGaussianInf &B  );
00233 
00234         bool BASE_IMPEXP operator==(const CPosePDFGaussianInf &p1,const CPosePDFGaussianInf &p2);
00235 
00236         } // End of namespace
00237 } // End of namespace
00238 
00239 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011