Main MRPT website > C++ reference
MRPT logo
CPosePDFGaussian.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CPosePDFGaussian_H
10 #define CPosePDFGaussian_H
11 
12 #include <mrpt/poses/CPosePDF.h>
14 
15 namespace mrpt
16 {
17 namespace poses
18 {
19  using namespace mrpt::math;
20 
21  class CPose3DPDF;
22  class CPoint2DPDFGaussian;
23 
24  // This must be added to any CSerializable derived class:
26 
27  /** Declares a class that represents a Probability Density function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
28  *
29  * This class implements that PDF using a mono-modal Gaussian distribution. See mrpt::poses::CPosePDF for more details.
30  *
31  * \sa CPose2D, CPosePDF, CPosePDFParticles
32  * \ingroup poses_pdf_grp
33  */
35  {
36  // This must be added to any CSerializable derived class:
38 
39  protected:
40  /** Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
41  */
42  void assureSymmetry();
43 
44  public:
45  /** @name Data fields
46  @{ */
47 
48  CPose2D mean; //!< The mean value
49  CMatrixDouble33 cov; //!< The 3x3 covariance matrix
50 
51  /** @} */
52 
53  inline const CPose2D & getPoseMean() const { return mean; }
54  inline CPose2D & getPoseMean() { return mean; }
55 
56  /** Default constructor
57  */
59 
60  /** Constructor
61  */
62  explicit CPosePDFGaussian( const CPose2D &init_Mean );
63 
64  /** Constructor
65  */
66  CPosePDFGaussian( const CPose2D &init_Mean, const CMatrixDouble33 &init_Cov );
67 
68  /** Copy constructor, including transformations between other PDFs */
69  explicit CPosePDFGaussian( const CPosePDF &o ) { copyFrom( o ); }
70 
71  /** Copy constructor, including transformations between other PDFs */
72  explicit CPosePDFGaussian( const CPose3DPDF &o ) { copyFrom( o ); }
73 
74  /** Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
75  * \sa getCovariance
76  */
77  void getMean(CPose2D &mean_pose) const {
78  mean_pose = mean;
79  }
80 
81  /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once.
82  * \sa getMean
83  */
84  void getCovarianceAndMean(CMatrixDouble33 &cov,CPose2D &mean_point) const {
85  mean_point = mean;
86  cov = this->cov;
87  }
88 
89  /** Copy operator, translating if necesary (for example, between particles and gaussian representations) */
90  void copyFrom(const CPosePDF &o);
91 
92  /** Copy operator, translating if necesary (for example, between particles and gaussian representations) */
93  void copyFrom(const CPose3DPDF &o);
94 
95  /** Save PDF's particles to a text file, containing the 2D pose in the first line, then the covariance matrix in next 3 lines. */
96  void saveToTextFile(const std::string &file) const;
97 
98  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
99  * "to project" the current pdf. Result PDF substituted the currently stored one in the object.
100  */
101  void changeCoordinatesReference( const CPose3D &newReferenceBase );
102 
103  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
104  * "to project" the current pdf. Result PDF substituted the currently stored one in the object.
105  */
106  void changeCoordinatesReference( const CPose2D &newReferenceBase );
107 
108  /** 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$.
109  */
110  void rotateCov(const double ang);
111 
112  /** 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!). */
113  void inverseComposition( const CPosePDFGaussian &x, const CPosePDFGaussian &ref );
114 
115  /** 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). */
116  void inverseComposition(
117  const CPosePDFGaussian &x1,
118  const CPosePDFGaussian &x0,
119  const CMatrixDouble33 &COV_01
120  );
121 
122  /** Draws a single sample from the distribution
123  */
124  void drawSingleSample( CPose2D &outPart ) const;
125 
126  /** 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.
127  */
128  void drawManySamples( size_t N, std::vector<vector_double> & outSamples ) const;
129 
130  /** Bayesian fusion of two points gauss. distributions, then save the result in this object.
131  * The process is as follows:<br>
132  * - (x1,S1): Mean and variance of the p1 distribution.
133  * - (x2,S2): Mean and variance of the p2 distribution.
134  * - (x,S): Mean and variance of the resulting distribution.
135  *
136  * S = (S1<sup>-1</sup> + S2<sup>-1</sup>)<sup>-1</sup>;
137  * x = S * ( S1<sup>-1</sup>*x1 + S2<sup>-1</sup>*x2 );
138  */
139  void bayesianFusion(const CPosePDF &p1,const CPosePDF &p2, const double &minMahalanobisDistToDrop = 0 );
140 
141  /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
142  */
143  void inverse(CPosePDF &o) const;
144 
145  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated). */
146  void operator += ( const CPose2D &Ap);
147 
148  /** Evaluates the PDF at a given point. */
149  double evaluatePDF( const CPose2D &x ) const;
150 
151  /** Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1]. */
152  double evaluateNormalizedPDF( const CPose2D &x ) const;
153 
154  /** Computes the Mahalanobis distance between the centers of two Gaussians. */
155  double mahalanobisDistanceTo( const CPosePDFGaussian& theOther );
156 
157  /** 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...) */
158  void assureMinCovariance( const double & minStdXY, const double &minStdPhi );
159 
160  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated) (see formulas in jacobiansPoseComposition ). */
161  void operator += ( const CPosePDFGaussian &Ap);
162 
163  /** Makes: thisPDF = thisPDF - Ap, where "-" is pose inverse composition (both the mean, and the covariance matrix are updated) */
164  inline void operator -=( const CPosePDFGaussian &ref ) {
165  this->inverseComposition(*this,ref);
166  }
167 
168  /** Returns the PDF of the 2D point \f$ g = q \oplus l\f$ with "q"=this pose and "l" a point without uncertainty */
169  void composePoint(const mrpt::math::TPoint2D &l, CPoint2DPDFGaussian &g ) const;
170 
171 
172  }; // End of class def.
173 
174 
175  /** Pose compose operator: RES = A (+) B , computing both the mean and the covariance */
176  CPosePDFGaussian BASE_IMPEXP operator +( const CPosePDFGaussian &a, const CPosePDFGaussian &b );
177 
178  /** Pose inverse compose operator: RES = A (-) B , computing both the mean and the covariance */
179  CPosePDFGaussian BASE_IMPEXP operator -( const CPosePDFGaussian &a, const CPosePDFGaussian &b );
180 
181  /** Dumps the mean and covariance matrix to a text stream. */
182  std::ostream BASE_IMPEXP & operator << (std::ostream & out, const CPosePDFGaussian& obj);
183 
184  /** Returns the Gaussian distribution of \f$ \mathbf{C} \f$, for \f$ \mathbf{C} = \mathbf{A} \oplus \mathbf{B} \f$. */
186 
187  bool BASE_IMPEXP operator==(const CPosePDFGaussian &p1,const CPosePDFGaussian &p2);
188 
189  } // End of namespace
190 } // End of namespace
191 
192 #endif
void getMean(CPose2D &mean_pose) const
Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
CPosePDFGaussian(const CPosePDF &o)
Copy constructor, including transformations between other PDFs.
CPose2D mean
The mean value.
CPose2D BASE_IMPEXP operator-(const CPose2D &p)
Unary - operator: return the inverse pose "-p" (Note that is NOT the same than a pose with negative x...
A gaussian distribution for 2D points.
void saveToTextFile(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat=mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader=false, const std::string &userHeader=std::string()) const
Save matrix to a text file, compatible with MATLAB text format (see also the methods of matrix classe...
void getCovarianceAndMean(CMatrixDouble33 &cov, CPose2D &mean_point) const
Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once...
const CPose2D & getPoseMean() const
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArray.h:19
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
Definition: ops_matrices.h:214
CPosePDFGaussian(const CPose3DPDF &o)
Copy constructor, including transformations between other PDFs.
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:64
mrpt::math::TPoint2D BASE_IMPEXP operator+(const CPose2D &pose, const mrpt::math::TPoint2D &pnt)
Compose a 2D point from a new coordinate base given by a 2D pose.
Eigen::Matrix< dataType, 4, 4 > inverse(Eigen::Matrix< dataType, 4, 4 > &pose)
Definition: Miscellaneous.h:71
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
CMatrixDouble33 cov
The 3x3 covariance matrix.
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:41
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:129
A class used to store a 2D pose.
Definition: CPose2D.h:35
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:71
Lightweight 2D point.
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
Definition: CPose3DPDF.h:42
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
std::ostream & operator<<(std::ostream &o, const CPoint< DERIVEDCLASS > &p)
Dumps a point as a string [x,y] or [x,y,z].
Definition: CPoint.h:105



Page generated by Doxygen 1.8.14 for MRPT 1.1.0 SVN: at lun oct 28 00:54:49 CET 2019 Hosted on:
SourceForge.net Logo