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