Main MRPT website > C++ reference
MRPT logo
CPosePDFSOG.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CPosePDFSOG_H
29 #define CPosePDFSOG_H
30 
31 #include <mrpt/poses/CPosePDF.h>
32 #include <mrpt/math/CMatrix.h>
33 #include <mrpt/math/CMatrixD.h>
34 
35 
36 namespace mrpt
37 {
38  namespace poses
39  {
40  using namespace mrpt::math;
41 
42  // This must be added to any CSerializable derived class:
44 
45  /** Declares a class that represents a Probability Density function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
46  * This class implements that PDF as the following multi-modal Gaussian distribution:
47  *
48  * \f$ p(\mathbf{x}) = \sum\limits_{i=1}^N \omega^i \mathcal{N}( \mathbf{x} ; \bar{\mathbf{x}}^i, \mathbf{\Sigma}^i ) \f$
49  *
50  * Where the number of modes N is the size of CPosePDFSOG::m_modes
51  *
52  * See mrpt::poses::CPosePDF for more details.
53  *
54  * \sa CPose2D, CPosePDF, CPosePDFParticles
55  * \ingroup poses_pdf_grp
56  */
58  {
59  // This must be added to any CSerializable derived class:
61 
62  public:
63  /** The struct for each mode:
64  */
66  {
67  TGaussianMode() :
68  mean(),
69  cov(),
70  log_w(0)
71  { }
72 
75 
76  /** The log-weight
77  */
78  double log_w;
79 
80  public:
81  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
82  };
83 
87 
88  protected:
89  /** Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
90  */
91  void assureSymmetry();
92 
93  /** The list of SOG modes */
94  CListGaussianModes m_modes;
95 
96  public:
97  /** Default constructor
98  * \param nModes The initial size of CPosePDFSOG::m_modes
99  */
100  CPosePDFSOG( size_t nModes = 1 );
101 
102  size_t size() const { return m_modes.size(); } //!< Return the number of Gaussian modes.
103  bool empty() const { return m_modes.empty(); } //!< Return whether there is any Gaussian mode.
104 
105  /** Clear the list of modes */
106  void clear();
107 
108  /** Access to individual beacons */
109  const TGaussianMode& operator [](size_t i) const {
110  ASSERT_(i<m_modes.size())
111  return m_modes[i];
112  }
113  /** Access to individual beacons */
114  TGaussianMode& operator [](size_t i) {
115  ASSERT_(i<m_modes.size())
116  return m_modes[i];
117  }
118 
119  /** Access to individual beacons */
120  const TGaussianMode& get(size_t i) const {
121  ASSERT_(i<m_modes.size())
122  return m_modes[i];
123  }
124  /** Access to individual beacons */
125  TGaussianMode& get(size_t i) {
126  ASSERT_(i<m_modes.size())
127  return m_modes[i];
128  }
129 
130  /** Inserts a copy of the given mode into the SOG */
131  void push_back(const TGaussianMode& m) {
132  m_modes.push_back(m);
133  }
134 
135  iterator begin() { return m_modes.begin(); }
136  iterator end() { return m_modes.end(); }
137  const_iterator begin() const { return m_modes.begin(); }
138  const_iterator end()const { return m_modes.end(); }
139 
140  iterator erase(iterator i) { return m_modes.erase(i); }
141 
142  void resize(const size_t N); //!< Resize the number of SOG modes
143 
144  /** Merge very close modes so the overall number of modes is reduced while preserving the total distribution.
145  * This method uses the approach described in the paper:
146  * - "Kullback-Leibler Approach to Gaussian Mixture Reduction" AR Runnalls. IEEE Transactions on Aerospace and Electronic Systems, 2007.
147  *
148  * \param max_KLd The maximum KL-divergence to consider the merge of two nodes (and then stops the process).
149  */
150  void mergeModes( double max_KLd = 0.5, bool verbose = false );
151 
152  /** Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
153  * \sa getCovariance
154  */
155  void getMean(CPose2D &mean_pose) const;
156 
157  /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once.
158  * \sa getMean
159  */
160  void getCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const;
161 
162  /** For the most likely Gaussian mode in the SOG, returns the pose covariance matrix (3x3 cov matrix) and the mean.
163  * \sa getMean
164  */
165  void getMostLikelyCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const;
166 
167  /** Normalize the weights in m_modes such as the maximum log-weight is 0.
168  */
169  void normalizeWeights();
170 
171  /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
172  */
173  void copyFrom(const CPosePDF &o);
174 
175  /** Save the density to a text file, with the following format:
176  * There is one row per Gaussian "mode", and each row contains 10 elements:
177  * - w (The weight)
178  * - x_mean (gaussian mean value)
179  * - y_mean (gaussian mean value)
180  * - phi_mean (gaussian mean value)
181  * - C11 (Covariance elements)
182  * - C22 (Covariance elements)
183  * - C33 (Covariance elements)
184  * - C12 (Covariance elements)
185  * - C13 (Covariance elements)
186  * - C23 (Covariance elements)
187  *
188  */
189  void saveToTextFile(const std::string &file) const;
190 
191  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
192  * "to project" the current pdf. Result PDF substituted the currently stored one in the object.
193  */
194  void changeCoordinatesReference(const CPose3D &newReferenceBase );
195 
196  /** 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$.
197  */
198  void rotateAllCovariances(const double &ang);
199 
200  /** Draws a single sample from the distribution
201  */
202  void drawSingleSample( CPose2D &outPart ) const;
203 
204  /** 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.
205  */
206  void drawManySamples( size_t N, std::vector<vector_double> & outSamples ) const;
207 
208  /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
209  */
210  void inverse(CPosePDF &o) const;
211 
212  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated).
213  */
214  void operator += ( const CPose2D &Ap);
215 
216  /** Evaluates the PDF at a given point.
217  */
218  double evaluatePDF( const CPose2D &x, bool sumOverAllPhis = false ) const;
219 
220  /** Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in the range [0,1].
221  */
222  double evaluateNormalizedPDF( const CPose2D &x ) const;
223 
224  /** 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).
225  */
226  void evaluatePDFInArea(
227  const double & x_min,
228  const double & x_max,
229  const double & y_min,
230  const double & y_max,
231  const double & resolutionXY,
232  const double & phi,
233  CMatrixD &outMatrix,
234  bool sumOverAllPhis = false );
235 
236  /** 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)
237  */
238  void bayesianFusion(const CPosePDF &p1,const CPosePDF &p2, const double &minMahalanobisDistToDrop=0 );
239 
240 
241  }; // End of class def.
242 
243  } // End of namespace
244 } // End of namespace
245 
246 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013