Main MRPT website > C++ reference for MRPT 1.9.9
CPosePDFSOG.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-2018, 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 CPosePDFSOG_H
10 #define CPosePDFSOG_H
11 
12 #include <mrpt/poses/CPosePDF.h>
14 #include <mrpt/math/math_frwds.h>
16 #include <ostream>
17 
18 namespace mrpt
19 {
20 namespace poses
21 {
22 /** Declares a class that represents a Probability Density function (PDF) of a
23  * 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
24  * This class implements that PDF as the following multi-modal Gaussian
25  * distribution:
26  *
27  * \f$ p(\mathbf{x}) = \sum\limits_{i=1}^N \omega^i \mathcal{N}( \mathbf{x} ;
28  * \bar{\mathbf{x}}^i, \mathbf{\Sigma}^i ) \f$
29  *
30  * Where the number of modes N is the size of CPosePDFSOG::m_modes
31  *
32  * See mrpt::poses::CPosePDF for more details.
33  *
34  * \sa CPose2D, CPosePDF, CPosePDFParticles
35  * \ingroup poses_pdf_grp
36  */
37 class CPosePDFSOG : public CPosePDF
38 {
40 
41  public:
42  /** The struct for each mode:
43  */
45  {
46  TGaussianMode() : mean(), cov(), log_w(0) {}
49 
50  /** The log-weight
51  */
52  double log_w;
53 
54  public:
56 
57  friend std::ostream& operator<<(
58  std::ostream& o, const TGaussianMode& mode)
59  {
60  o << "Mean: " << mode.mean << std::endl
61  << "Covariance: " << std::endl
62  << mode.cov << std::endl
63  << "Log-weight: " << mode.log_w << std::endl;
64  return o;
65  }
66  };
67 
71 
72  const CListGaussianModes& getSOGModes() const { return m_modes; }
73  protected:
74  /** Ensures the symmetry of the covariance matrix (eventually certain
75  * operations in the math-coprocessor lead to non-symmetric matrixes!) */
76  void assureSymmetry();
77 
78  /** The list of SOG modes */
80 
81  public:
82  /** Default constructor
83  * \param nModes The initial size of CPosePDFSOG::m_modes */
84  CPosePDFSOG(size_t nModes = 1);
85 
86  /** Return the number of Gaussian modes. */
87  size_t size() const { return m_modes.size(); }
88  /** Return whether there is any Gaussian mode. */
89  bool empty() const { return m_modes.empty(); }
90  /** Clear the list of modes */
91  void clear();
92 
93  /** Access to individual beacons */
94  const TGaussianMode& operator[](size_t i) const
95  {
96  ASSERT_(i < m_modes.size());
97  return m_modes[i];
98  }
99  /** Access to individual beacons */
101  {
102  ASSERT_(i < m_modes.size());
103  return m_modes[i];
104  }
105 
106  /** Access to individual beacons */
107  const TGaussianMode& get(size_t i) const
108  {
109  ASSERT_(i < m_modes.size());
110  return m_modes[i];
111  }
112  /** Access to individual beacons */
113  TGaussianMode& get(size_t i)
114  {
115  ASSERT_(i < m_modes.size());
116  return m_modes[i];
117  }
118 
119  /** Inserts a copy of the given mode into the SOG */
120  void push_back(const TGaussianMode& m) { m_modes.push_back(m); }
121  iterator begin() { return m_modes.begin(); }
122  iterator end() { return m_modes.end(); }
123  const_iterator begin() const { return m_modes.begin(); }
124  const_iterator end() const { return m_modes.end(); }
125  iterator erase(iterator i) { return m_modes.erase(i); }
126  /** Resize the number of SOG modes */
127  void resize(const size_t N);
128 
129  /** Merge very close modes so the overall number of modes is reduced while
130  * preserving the total distribution.
131  * This method uses the approach described in the paper:
132  * - "Kullback-Leibler Approach to Gaussian Mixture Reduction" AR
133  * Runnalls. IEEE Transactions on Aerospace and Electronic Systems, 2007.
134  *
135  * \param max_KLd The maximum KL-divergence to consider the merge of two
136  * nodes (and then stops the process).
137  */
138  void mergeModes(double max_KLd = 0.5, bool verbose = false);
139 
140  /** Returns an estimate of the pose, (the mean, or mathematical expectation
141  * of the PDF) \sa getCovariance */
142  void getMean(CPose2D& mean_pose) const override;
143  /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and
144  * the mean, both at once. \sa getMean */
146  mrpt::math::CMatrixDouble33& cov, CPose2D& mean_point) const override;
147  /** For the most likely Gaussian mode in the SOG, returns the pose
148  * covariance matrix (3x3 cov matrix) and the mean. \sa getMean */
150  mrpt::math::CMatrixDouble33& cov, CPose2D& mean_point) const;
151  /** Normalize the weights in m_modes such as the maximum log-weight is 0 */
152  void normalizeWeights();
153 
154  /** Copy operator, translating if necesary (for example, between particles
155  * and gaussian representations) */
156  void copyFrom(const CPosePDF& o) override;
157 
158  /** Save the density to a text file, with the following format:
159  * There is one row per Gaussian "mode", and each row contains 10
160  * elements:
161  * - w (The weight)
162  * - x_mean (gaussian mean value)
163  * - y_mean (gaussian mean value)
164  * - phi_mean (gaussian mean value)
165  * - C11 (Covariance elements)
166  * - C22 (Covariance elements)
167  * - C33 (Covariance elements)
168  * - C12 (Covariance elements)
169  * - C13 (Covariance elements)
170  * - C23 (Covariance elements)
171  */
172  bool saveToTextFile(const std::string& file) const override;
173 
174  /** this = p (+) this. This can be used to convert a PDF from local
175  * coordinates to global, providing the point (newReferenceBase) from which
176  * "to project" the current pdf. Result PDF substituted the currently
177  * stored one in the object. */
178  void changeCoordinatesReference(const CPose3D& newReferenceBase) override;
179 
180  /** Rotate all the covariance matrixes by replacing them by \f$
181  * \mathbf{R}~\mathbf{COV}~\mathbf{R}^t \f$, where \f$ \mathbf{R} = \left[
182  * \begin{array}{ccc} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha &
183  * \cos\alpha & 0 \\ 0 & 0 & 1 \end{array}\right] \f$ */
184  void rotateAllCovariances(const double& ang);
185  /** Draws a single sample from the distribution */
186  void drawSingleSample(CPose2D& outPart) const override;
187  /** Draws a number of samples from the distribution, and saves as a list of
188  * 1x3 vectors, where each row contains a (x,y,phi) datum. */
189  void drawManySamples(
190  size_t N,
191  std::vector<mrpt::math::CVectorDouble>& outSamples) const override;
192  /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF */
193  void inverse(CPosePDF& o) const override;
194 
195  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the
196  * mean, and the covariance matrix are updated). */
197  void operator+=(const mrpt::poses::CPose2D& Ap);
198 
199  /** Evaluates the PDF at a given point. */
200  double evaluatePDF(
201  const mrpt::poses::CPose2D& x, bool sumOverAllPhis = false) const;
202  /** Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in
203  * the range [0,1]. */
204  double evaluateNormalizedPDF(const mrpt::poses::CPose2D& x) const;
205 
206  /** Evaluates the PDF within a rectangular grid (and a fixed orientation)
207  * and saves the result in a matrix (each row contains values for a fixed
208  * y-coordinate value). */
209  void evaluatePDFInArea(
210  const double& x_min, const double& x_max, const double& y_min,
211  const double& y_max, const double& resolutionXY, const double& phi,
212  mrpt::math::CMatrixD& outMatrix, bool sumOverAllPhis = false);
213 
214  /** Bayesian fusion of two pose distributions, then save the result in this
215  * object (WARNING: Currently p1 must be a mrpt::poses::CPosePDFSOG object
216  * and p2 a mrpt::poses::CPosePDFGaussian object) */
217  void bayesianFusion(
218  const CPosePDF& p1, const CPosePDF& p2,
219  const double minMahalanobisDistToDrop = 0) override;
220 
221 }; // End of class def.
222 } // End of namespace
223 } // End of namespace
224 #endif
void normalizeWeights()
Normalize the weights in m_modes such as the maximum log-weight is 0.
Scalar * iterator
Definition: eigen_plugins.h:26
void clear()
Clear the list of modes.
Definition: CPosePDFSOG.cpp:39
void inverse(CPosePDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
void mergeModes(double max_KLd=0.5, bool verbose=false)
Merge very close modes so the overall number of modes is reduced while preserving the total distribut...
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
Definition: CMatrixD.h:24
The struct for each mode:
Definition: CPosePDFSOG.h:44
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
Definition: CPosePDFSOG.h:37
CListGaussianModes::const_iterator const_iterator
Definition: CPosePDFSOG.h:69
double evaluatePDF(const mrpt::poses::CPose2D &x, bool sumOverAllPhis=false) const
Evaluates the PDF at a given point.
const TGaussianMode & operator[](size_t i) const
Access to individual beacons.
Definition: CPosePDFSOG.h:94
void getMean(CPose2D &mean_pose) const override
Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF) ...
Definition: CPosePDFSOG.cpp:49
void bayesianFusion(const CPosePDF &p1, const CPosePDF &p2, const double minMahalanobisDistToDrop=0) override
Bayesian fusion of two pose distributions, then save the result in this object (WARNING: Currently p1...
void push_back(const TGaussianMode &m)
Inserts a copy of the given mode into the SOG.
Definition: CPosePDFSOG.h:120
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
CPosePDFSOG(size_t nModes=1)
Default constructor.
Definition: CPosePDFSOG.cpp:35
void rotateAllCovariances(const double &ang)
Rotate all the covariance matrixes by replacing them by , where .
friend std::ostream & operator<<(std::ostream &o, const TGaussianMode &mode)
Definition: CPosePDFSOG.h:57
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
CListGaussianModes::iterator iterator
Definition: CPosePDFSOG.h:70
TGaussianMode & operator[](size_t i)
Access to individual beacons.
Definition: CPosePDFSOG.h:100
const_iterator end() const
Definition: CPosePDFSOG.h:124
void evaluatePDFInArea(const double &x_min, const double &x_max, const double &y_min, const double &y_max, const double &resolutionXY, const double &phi, mrpt::math::CMatrixD &outMatrix, bool sumOverAllPhis=false)
Evaluates the PDF within a rectangular grid (and a fixed orientation) and saves the result in a matri...
const CListGaussianModes & getSOGModes() const
Definition: CPosePDFSOG.h:72
bool saveToTextFile(const std::string &file) const override
Save the density to a text file, with the following format: There is one row per Gaussian "mode"...
mrpt::math::CMatrixDouble33 cov
Definition: CPosePDFSOG.h:48
bool empty() const
Return whether there is any Gaussian mode.
Definition: CPosePDFSOG.h:89
GLsizei const GLchar ** string
Definition: glext.h:4101
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:41
double evaluateNormalizedPDF(const mrpt::poses::CPose2D &x) const
Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in the range [0...
void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPose2D &mean_point) const override
Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once...
Definition: CPosePDFSOG.cpp:70
void assureSymmetry()
Ensures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
GLint mode
Definition: glext.h:5669
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...
mrpt::aligned_std_vector< TGaussianMode > CListGaussianModes
Definition: CPosePDFSOG.h:68
void getMostLikelyCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPose2D &mean_point) const
For the most likely Gaussian mode in the SOG, returns the pose covariance matrix (3x3 cov matrix) and...
iterator erase(iterator i)
Definition: CPosePDFSOG.h:125
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void drawManySamples(size_t N, std::vector< mrpt::math::CVectorDouble > &outSamples) const override
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.
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
void copyFrom(const CPosePDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
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:148
size_t size() const
Return the number of Gaussian modes.
Definition: CPosePDFSOG.h:87
GLenum GLint x
Definition: glext.h:3538
void operator+=(const mrpt::poses::CPose2D &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
const_iterator begin() const
Definition: CPosePDFSOG.h:123
const Scalar * const_iterator
Definition: eigen_plugins.h:27
void resize(const size_t N)
Resize the number of SOG modes.
Definition: CPosePDFSOG.cpp:43
void drawSingleSample(CPose2D &outPart) const override
Draws a single sample from the distribution.
CListGaussianModes m_modes
The list of SOG modes.
Definition: CPosePDFSOG.h:79



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019