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