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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020