MRPT  1.9.9
CPosePDFSOG.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/math/CMatrixFixed.h>
12 #include <mrpt/math/math_frwds.h>
13 #include <mrpt/poses/CPosePDF.h>
14 #include <ostream>
15 #include <vector>
16 
17 namespace mrpt::poses
18 {
19 /** Declares a class that represents a Probability Density function (PDF) of a
20  * 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
21  * This class implements that PDF as the following multi-modal Gaussian
22  * distribution:
23  *
24  * \f$ p(\mathbf{x}) = \sum\limits_{i=1}^N \omega^i \mathcal{N}( \mathbf{x} ;
25  * \bar{\mathbf{x}}^i, \mathbf{\Sigma}^i ) \f$
26  *
27  * Where the number of modes N is the size of CPosePDFSOG::m_modes
28  *
29  * See mrpt::poses::CPosePDF for more details.
30  *
31  * \sa CPose2D, CPosePDF, CPosePDFParticles
32  * \ingroup poses_pdf_grp
33  */
34 class CPosePDFSOG : public CPosePDF
35 {
37 
38  public:
39  /** The struct for each mode:
40  */
42  {
43  TGaussianMode() : mean(), cov() {}
46 
47  /** The log-weight
48  */
49  double log_w{0};
50 
51  public:
52  ;
53 
54  friend std::ostream& operator<<(
55  std::ostream& o, const TGaussianMode& mode)
56  {
57  o << "Mean: " << mode.mean << std::endl
58  << "Covariance: " << std::endl
59  << mode.cov << std::endl
60  << "Log-weight: " << mode.log_w << std::endl;
61  return o;
62  }
63  };
64 
65  using CListGaussianModes = std::vector<TGaussianMode>;
66  using const_iterator = CListGaussianModes::const_iterator;
67  using iterator = CListGaussianModes::iterator;
68 
69  const CListGaussianModes& getSOGModes() const { return m_modes; }
70 
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 enforceCovSymmetry();
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  void getMean(CPose2D& mean_pose) const override;
139 
140  std::tuple<cov_mat_t, type_value> getCovarianceAndMean() const override;
141 
142  /** For the most likely Gaussian mode in the SOG, returns the pose
143  * covariance matrix (3x3 cov matrix) and the mean. \sa getMean */
145  mrpt::math::CMatrixDouble33& cov, CPose2D& mean_point) const;
146  /** Normalize the weights in m_modes such as the maximum log-weight is 0 */
147  void normalizeWeights();
148 
149  /** Copy operator, translating if necesary (for example, between particles
150  * and gaussian representations) */
151  void copyFrom(const CPosePDF& o) override;
152 
153  /** Save the density to a text file, with the following format:
154  * There is one row per Gaussian "mode", and each row contains 10
155  * elements:
156  * - w (The weight)
157  * - x_mean (gaussian mean value)
158  * - y_mean (gaussian mean value)
159  * - phi_mean (gaussian mean value)
160  * - C11 (Covariance elements)
161  * - C22 (Covariance elements)
162  * - C33 (Covariance elements)
163  * - C12 (Covariance elements)
164  * - C13 (Covariance elements)
165  * - C23 (Covariance elements)
166  */
167  bool saveToTextFile(const std::string& file) const override;
168 
169  /** this = p (+) this. This can be used to convert a PDF from local
170  * coordinates to global, providing the point (newReferenceBase) from which
171  * "to project" the current pdf. Result PDF substituted the currently
172  * stored one in the object. */
173  void changeCoordinatesReference(const CPose3D& newReferenceBase) override;
174 
175  /** Rotate all the covariance matrixes by replacing them by \f$
176  * \mathbf{R}~\mathbf{COV}~\mathbf{R}^t \f$, where \f$ \mathbf{R} = \left[
177  * \begin{array}{ccc} \cos\alpha & -\sin\alpha & 0 \\ \sin\alpha &
178  * \cos\alpha & 0 \\ 0 & 0 & 1 \end{array}\right] \f$ */
179  void rotateAllCovariances(double ang);
180  /** Draws a single sample from the distribution */
181  void drawSingleSample(CPose2D& outPart) const override;
182  /** Draws a number of samples from the distribution, and saves as a list of
183  * 1x3 vectors, where each row contains a (x,y,phi) datum. */
184  void drawManySamples(
185  size_t N,
186  std::vector<mrpt::math::CVectorDouble>& outSamples) const override;
187  /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF */
188  void inverse(CPosePDF& o) const override;
189 
190  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the
191  * mean, and the covariance matrix are updated). */
192  void operator+=(const mrpt::poses::CPose2D& Ap);
193 
194  /** Evaluates the PDF at a given point. */
195  double evaluatePDF(
196  const mrpt::poses::CPose2D& x, bool sumOverAllPhis = false) const;
197  /** Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in
198  * the range [0,1]. */
199  double evaluateNormalizedPDF(const mrpt::poses::CPose2D& x) const;
200 
201  /** Evaluates the PDF within a rectangular grid (and a fixed orientation)
202  * and saves the result in a matrix (each row contains values for a fixed
203  * y-coordinate value). */
204  void evaluatePDFInArea(
205  double x_min, double x_max, double y_min, double y_max,
206  double resolutionXY, double phi, mrpt::math::CMatrixDouble& outMatrix,
207  bool sumOverAllPhis = false);
208 
209  /** Bayesian fusion of two pose distributions, then save the result in this
210  * object (WARNING: Currently p1 must be a mrpt::poses::CPosePDFSOG object
211  * and p2 a mrpt::poses::CPosePDFGaussian object) */
212  void bayesianFusion(
213  const CPosePDF& p1, const CPosePDF& p2,
214  const double minMahalanobisDistToDrop = 0) override;
215 
216 }; // End of class def.
217 } // namespace mrpt::poses
void normalizeWeights()
Normalize the weights in m_modes such as the maximum log-weight is 0.
void clear()
Clear the list of modes.
Definition: CPosePDFSOG.cpp:40
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...
The struct for each mode:
Definition: CPosePDFSOG.h:41
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
Definition: CPosePDFSOG.h:34
void rotateAllCovariances(double ang)
Rotate all the covariance matrixes by replacing them by , where .
CListGaussianModes::const_iterator const_iterator
Definition: CPosePDFSOG.h:66
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
Definition: CPosePDFSOG.cpp:50
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 enforceCovSymmetry()
Ensures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
void push_back(const TGaussianMode &m)
Inserts a copy of the given mode into the SOG.
Definition: CPosePDFSOG.h:118
CPosePDFSOG(size_t nModes=1)
Default constructor.
Definition: CPosePDFSOG.cpp:36
std::tuple< cov_mat_t, type_value > getCovarianceAndMean() const override
Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean...
Definition: CPosePDFSOG.cpp:68
friend std::ostream & operator<<(std::ostream &o, const TGaussianMode &mode)
Definition: CPosePDFSOG.h:54
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
CListGaussianModes::iterator iterator
Definition: CPosePDFSOG.h:67
TGaussianMode & operator[](size_t i)
Access to individual beacons.
Definition: CPosePDFSOG.h:98
const_iterator end() const
Definition: CPosePDFSOG.h:122
const CListGaussianModes & getSOGModes() const
Definition: CPosePDFSOG.h:69
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:45
bool empty() const
Return whether there is any Gaussian mode.
Definition: CPosePDFSOG.h:87
CMatrixDouble 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:149
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:38
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 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:39
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
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) ...
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
size_t size() const
Return the number of Gaussian modes.
Definition: CPosePDFSOG.h:85
void operator+=(const mrpt::poses::CPose2D &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
void evaluatePDFInArea(double x_min, double x_max, double y_min, double y_max, double resolutionXY, double phi, mrpt::math::CMatrixDouble &outMatrix, bool sumOverAllPhis=false)
Evaluates the PDF within a rectangular grid (and a fixed orientation) and saves the result in a matri...
const_iterator begin() const
Definition: CPosePDFSOG.h:121
void resize(const size_t N)
Resize the number of SOG modes.
Definition: CPosePDFSOG.cpp:44
void drawSingleSample(CPose2D &outPart) const override
Draws a single sample from the distribution.
std::vector< TGaussianMode > CListGaussianModes
Definition: CPosePDFSOG.h:65
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: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020