Main MRPT website > C++ reference
MRPT logo
CPosePDFSOG.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 CPosePDFSOG_H
00029 #define CPosePDFSOG_H
00030 
00031 #include <mrpt/poses/CPosePDF.h>
00032 #include <mrpt/math/CMatrix.h>
00033 #include <mrpt/math/CMatrixD.h>
00034 
00035 
00036 namespace mrpt
00037 {
00038         namespace poses
00039         {
00040                 using namespace mrpt::math;
00041 
00042                 // This must be added to any CSerializable derived class:
00043                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CPosePDFSOG , CPosePDF )
00044 
00045                 /** Declares a class that represents a Probability Density  function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
00046                  *   This class implements that PDF as the following multi-modal Gaussian distribution:
00047                  *
00048                  * \f$ p(\mathbf{x}) = \sum\limits_{i=1}^N \omega^i \mathcal{N}( \mathbf{x} ; \bar{\mathbf{x}}^i, \mathbf{\Sigma}^i )  \f$
00049                  *
00050                  *  Where the number of modes N is the size of CPosePDFSOG::m_modes
00051                  *
00052                  *  See mrpt::poses::CPosePDF for more details.
00053                  *
00054                  * \sa CPose2D, CPosePDF, CPosePDFParticles
00055                  * \ingroup poses_pdf_grp
00056                  */
00057                 class BASE_IMPEXP CPosePDFSOG : public CPosePDF
00058                 {
00059                         // This must be added to any CSerializable derived class:
00060                         DEFINE_SERIALIZABLE( CPosePDFSOG )
00061 
00062                 public:
00063                         /** The struct for each mode:
00064                          */
00065                         struct BASE_IMPEXP TGaussianMode
00066                         {
00067                                 TGaussianMode() :
00068                                         mean(),
00069                                         cov(),
00070                                         log_w(0)
00071                                 { }
00072 
00073                                 CPose2D                 mean;
00074                                 CMatrixDouble33 cov;
00075 
00076                                 /** The log-weight
00077                                   */
00078                                 double          log_w;
00079 
00080                         public:
00081                           EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00082                         };
00083 
00084                         typedef mrpt::aligned_containers<TGaussianMode>::vector_t       CListGaussianModes;
00085                         typedef CListGaussianModes::const_iterator const_iterator;
00086                         typedef CListGaussianModes::iterator iterator;
00087 
00088                 protected:
00089                         /** Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
00090                           */
00091                         void  assureSymmetry();
00092 
00093                         /** The list of SOG modes */
00094                         CListGaussianModes      m_modes;
00095 
00096                  public:
00097                         /** Default constructor
00098                           * \param nModes The initial size of CPosePDFSOG::m_modes
00099                           */
00100                         CPosePDFSOG( size_t nModes = 1 );
00101 
00102                         size_t size() const { return m_modes.size(); } //!< Return the number of Gaussian modes.
00103                         bool empty() const { return m_modes.empty(); } //!< Return whether there is any Gaussian mode.
00104 
00105                         /** Clear the list of modes */
00106                         void clear();
00107 
00108                         /** Access to individual beacons */
00109                         const TGaussianMode& operator [](size_t i) const {
00110                                 ASSERT_(i<m_modes.size())
00111                                 return  m_modes[i];
00112                         }
00113                         /** Access to individual beacons */
00114                         TGaussianMode& operator [](size_t i) {
00115                                 ASSERT_(i<m_modes.size())
00116                                 return  m_modes[i];
00117                         }
00118 
00119                         /** Access to individual beacons */
00120                         const TGaussianMode& get(size_t i) const {
00121                                 ASSERT_(i<m_modes.size())
00122                                 return  m_modes[i];
00123                         }
00124                         /** Access to individual beacons */
00125                         TGaussianMode& get(size_t i) {
00126                                 ASSERT_(i<m_modes.size())
00127                                 return  m_modes[i];
00128                         }
00129 
00130                         /** Inserts a copy of the given mode into the SOG */
00131                         void push_back(const TGaussianMode& m) {
00132                                 m_modes.push_back(m);
00133                         }
00134 
00135                         iterator begin() { return m_modes.begin(); }
00136                         iterator end() { return m_modes.end(); }
00137                         const_iterator begin() const { return m_modes.begin(); }
00138                         const_iterator end()const { return m_modes.end(); }
00139 
00140                         iterator erase(iterator i) { return m_modes.erase(i); }
00141 
00142                         void resize(const size_t N); //!< Resize the number of SOG modes
00143 
00144                         /** Merge very close modes so the overall number of modes is reduced while preserving the total distribution.
00145                           *  This method uses the approach described in the paper:
00146                           *  - "Kullback-Leibler Approach to Gaussian Mixture Reduction" AR Runnalls. IEEE Transactions on Aerospace and Electronic Systems, 2007.
00147                           *
00148                           *  \param max_KLd The maximum KL-divergence to consider the merge of two nodes (and then stops the process).
00149                           */
00150                         void mergeModes( double max_KLd = 0.5, bool verbose = false );
00151 
00152                          /** Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
00153                            * \sa getCovariance
00154                            */
00155                         void getMean(CPose2D &mean_pose) const;
00156 
00157                         /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once.
00158                           * \sa getMean
00159                           */
00160                         void getCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const;
00161 
00162                         /** For the most likely Gaussian mode in the SOG, returns the pose covariance matrix (3x3 cov matrix) and the mean.
00163                           * \sa getMean
00164                           */
00165                         void getMostLikelyCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const;
00166 
00167                         /** Normalize the weights in m_modes such as the maximum log-weight is 0.
00168                           */
00169                         void  normalizeWeights();
00170 
00171                         /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
00172                           */
00173                         void  copyFrom(const CPosePDF &o);
00174 
00175                         /** Save the density to a text file, with the following format:
00176                           *  There is one row per Gaussian "mode", and each row contains 10 elements:
00177                           *   - w (The weight)
00178                           *   - x_mean (gaussian mean value)
00179                           *   - y_mean (gaussian mean value)
00180                           *   - phi_mean (gaussian mean value)
00181                           *   - C11 (Covariance elements)
00182                           *   - C22 (Covariance elements)
00183                           *   - C33 (Covariance elements)
00184                           *   - C12 (Covariance elements)
00185                           *   - C13 (Covariance elements)
00186                           *   - C23 (Covariance elements)
00187                           *
00188                          */
00189                         void  saveToTextFile(const std::string &file) const;
00190 
00191                         /** This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
00192                           *   "to project" the current pdf. Result PDF substituted the currently stored one in the object.
00193                           */
00194                         void  changeCoordinatesReference(const CPose3D &newReferenceBase );
00195 
00196                         /** Rotate all the covariance matrixes by replacing them 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$.
00197                           */
00198                         void  rotateAllCovariances(const double &ang);
00199 
00200                         /** Draws a single sample from the distribution
00201                           */
00202                         void  drawSingleSample( CPose2D &outPart ) const;
00203 
00204                         /** 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.
00205                           */
00206                         void  drawManySamples( size_t N, std::vector<vector_double> & outSamples ) const;
00207 
00208                         /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
00209                           */
00210                         void     inverse(CPosePDF &o) const;
00211 
00212                         /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated).
00213                           */
00214                         void  operator += ( const CPose2D &Ap);
00215 
00216                         /** Evaluates the PDF at a given point.
00217                           */
00218                         double  evaluatePDF( const CPose2D &x, bool sumOverAllPhis = false ) const;
00219 
00220                         /** Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in the range [0,1].
00221                           */
00222                         double  evaluateNormalizedPDF( const CPose2D &x ) const;
00223 
00224                         /** Evaluates the PDF within a rectangular grid (and a fixed orientation) and saves the result in a matrix (each row contains values for a fixed y-coordinate value).
00225                           */
00226                         void  evaluatePDFInArea(
00227                                 const double &          x_min,
00228                                 const double &          x_max,
00229                                 const double &          y_min,
00230                                 const double &          y_max,
00231                                 const double &          resolutionXY,
00232                                 const double &          phi,
00233                                 CMatrixD        &outMatrix,
00234                                 bool            sumOverAllPhis = false );
00235 
00236                         /** Bayesian fusion of two pose distributions, then save the result in this object (WARNING: Currently p1 must be a mrpt::poses::CPosePDFSOG object and p2 a mrpt::poses::CPosePDFGaussian object)
00237                           */
00238                         void  bayesianFusion(const  CPosePDF &p1,const  CPosePDF &p2, const double &minMahalanobisDistToDrop=0 );
00239 
00240 
00241                 }; // End of class def.
00242 
00243         } // End of namespace
00244 } // End of namespace
00245 
00246 #endif



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