Main MRPT website > C++ reference
MRPT logo
CPosePDFGaussian.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 CPosePDFGaussian_H
29 #define CPosePDFGaussian_H
30 
31 #include <mrpt/poses/CPosePDF.h>
33 
34 namespace mrpt
35 {
36 namespace poses
37 {
38  using namespace mrpt::math;
39 
40  class CPose3DPDF;
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  *
47  * This class implements that PDF using a mono-modal Gaussian distribution. See mrpt::poses::CPosePDF for more details.
48  *
49  * \sa CPose2D, CPosePDF, CPosePDFParticles
50  * \ingroup poses_pdf_grp
51  */
53  {
54  // This must be added to any CSerializable derived class:
56 
57  protected:
58  /** Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
59  */
60  void assureSymmetry();
61 
62  public:
63  /** @name Data fields
64  @{ */
65 
66  CPose2D mean; //!< The mean value
67  CMatrixDouble33 cov; //!< The 3x3 covariance matrix
68 
69  /** @} */
70 
71  inline const CPose2D & getPoseMean() const { return mean; }
72  inline CPose2D & getPoseMean() { return mean; }
73 
74  /** Default constructor
75  */
77 
78  /** Constructor
79  */
80  explicit CPosePDFGaussian( const CPose2D &init_Mean );
81 
82  /** Constructor
83  */
84  CPosePDFGaussian( const CPose2D &init_Mean, const CMatrixDouble33 &init_Cov );
85 
86  /** Copy constructor, including transformations between other PDFs */
87  explicit CPosePDFGaussian( const CPosePDF &o ) { copyFrom( o ); }
88 
89  /** Copy constructor, including transformations between other PDFs */
90  explicit CPosePDFGaussian( const CPose3DPDF &o ) { copyFrom( o ); }
91 
92  /** Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
93  * \sa getCovariance
94  */
95  void getMean(CPose2D &mean_pose) const {
96  mean_pose = mean;
97  }
98 
99  /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once.
100  * \sa getMean
101  */
102  void getCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const {
103  mean_point = mean;
104  cov = this->cov;
105  }
106 
107  /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
108  */
109  void copyFrom(const CPosePDF &o);
110 
111  /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
112  */
113  void copyFrom(const CPose3DPDF &o);
114 
115  /** Save PDF's particles to a text file, containing the 2D pose in the first line, then the covariance matrix in next 3 lines.
116  */
117  void saveToTextFile(const std::string &file) const;
118 
119  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
120  * "to project" the current pdf. Result PDF substituted the currently stored one in the object.
121  */
122  void changeCoordinatesReference( const CPose3D &newReferenceBase );
123 
124  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
125  * "to project" the current pdf. Result PDF substituted the currently stored one in the object.
126  */
127  void changeCoordinatesReference( const CPose2D &newReferenceBase );
128 
129  /** 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$.
130  */
131  void rotateCov(const double ang);
132 
133  /** 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!).
134  */
135  void inverseComposition( const CPosePDFGaussian &x, const CPosePDFGaussian &ref );
136 
137  /** 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).
138  */
139  void inverseComposition(
140  const CPosePDFGaussian &x1,
141  const CPosePDFGaussian &x0,
142  const CMatrixDouble33 &COV_01
143  );
144 
145  /** Draws a single sample from the distribution
146  */
147  void drawSingleSample( CPose2D &outPart ) const;
148 
149  /** 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.
150  */
151  void drawManySamples( size_t N, std::vector<vector_double> & outSamples ) const;
152 
153  /** Bayesian fusion of two points gauss. distributions, then save the result in this object.
154  * The process is as follows:<br>
155  * - (x1,S1): Mean and variance of the p1 distribution.
156  * - (x2,S2): Mean and variance of the p2 distribution.
157  * - (x,S): Mean and variance of the resulting distribution.
158  *
159  * S = (S1<sup>-1</sup> + S2<sup>-1</sup>)<sup>-1</sup>;
160  * x = S * ( S1<sup>-1</sup>*x1 + S2<sup>-1</sup>*x2 );
161  */
162  void bayesianFusion(const CPosePDF &p1,const CPosePDF &p2, const double &minMahalanobisDistToDrop = 0 );
163 
164  /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
165  */
166  void inverse(CPosePDF &o) const;
167 
168  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated).
169  */
170  void operator += ( const CPose2D &Ap);
171 
172  /** Evaluates the PDF at a given point.
173  */
174  double evaluatePDF( const CPose2D &x ) const;
175 
176  /** Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
177  */
178  double evaluateNormalizedPDF( const CPose2D &x ) const;
179 
180  /** Computes the Mahalanobis distance between the centers of two Gaussians.
181  */
182  double mahalanobisDistanceTo( const CPosePDFGaussian& theOther );
183 
184  /** Substitutes the diagonal elements if (square) they are below some given minimum values (Use this before bayesianFusion, for example, to avoid inversion of singular matrixes, etc...)
185  */
186  void assureMinCovariance( const double & minStdXY, const double &minStdPhi );
187 
188  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated) (see formulas in jacobiansPoseComposition ).
189  */
190  void operator += ( const CPosePDFGaussian &Ap);
191 
192  /** Makes: thisPDF = thisPDF - Ap, where "-" is pose inverse composition (both the mean, and the covariance matrix are updated)
193  */
194  inline void operator -=( const CPosePDFGaussian &ref ) {
195  this->inverseComposition(*this,ref);
196  }
197 
198 
199  }; // End of class def.
200 
201 
202  /** Pose compose operator: RES = A (+) B , computing both the mean and the covariance */
204  CPosePDFGaussian res(a);
205  res+=b;
206  return res;
207  }
208 
209  /** Pose inverse compose operator: RES = A (-) B , computing both the mean and the covariance */
211  CPosePDFGaussian res;
212  res.inverseComposition(a,b);
213  return res;
214  }
215 
216  /** Dumps the mean and covariance matrix to a text stream.
217  */
218  std::ostream BASE_IMPEXP & operator << (std::ostream & out, const CPosePDFGaussian& obj);
219 
220  /** Returns the Gaussian distribution of \f$ \mathbf{C} \f$, for \f$ \mathbf{C} = \mathbf{A} \oplus \mathbf{B} \f$.
221  */
223 
224  bool BASE_IMPEXP operator==(const CPosePDFGaussian &p1,const CPosePDFGaussian &p2);
225 
226  } // End of namespace
227 } // End of namespace
228 
229 #endif



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