Main MRPT website > C++ reference for MRPT 1.5.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-2017, 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>
15 #include <ostream>
16 
17 
18 namespace mrpt
19 {
20  namespace poses
21  {
22  // This must be added to any CSerializable derived class:
23  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CPosePDFSOG , CPosePDF )
24 
25  /** Declares a class that represents a Probability Density function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
26  * This class implements that PDF as the following multi-modal Gaussian distribution:
27  *
28  * \f$ p(\mathbf{x}) = \sum\limits_{i=1}^N \omega^i \mathcal{N}( \mathbf{x} ; \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  */
38  {
39  // This must be added to any CSerializable derived class:
41 
42  public:
43  /** The struct for each mode:
44  */
46  {
48  mean(),
49  cov(),
50  log_w(0)
51  { }
52 
55 
56  /** The log-weight
57  */
58  double log_w;
59 
60  public:
62 
63  friend std::ostream& operator<<(std::ostream& o, const TGaussianMode& mode) {
64  o << "Mean: " << mode.mean << std::endl
65  << "Covariance: " << std::endl << mode.cov << std::endl
66  << "Log-weight: " << mode.log_w << std::endl;
67  return o;
68  }
69  };
70 
74 
75  const CListGaussianModes& getSOGModes() const { return m_modes; }
76 
77  protected:
78  void assureSymmetry(); //!< Ensures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
79 
80  CListGaussianModes m_modes; //!< The list of SOG modes
81 
82  public:
83  /** Default constructor
84  * \param nModes The initial size of CPosePDFSOG::m_modes */
85  CPosePDFSOG( size_t nModes = 1 );
86 
87  size_t size() const { return m_modes.size(); } //!< Return the number of Gaussian modes.
88  bool empty() const { return m_modes.empty(); } //!< Return whether there is any Gaussian mode.
89 
90 
91  void clear(); //!< Clear the list of modes
92 
93  /** Access to individual beacons */
94  const TGaussianMode& operator [](size_t i) const {
95  ASSERT_(i<m_modes.size())
96  return m_modes[i];
97  }
98  /** Access to individual beacons */
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  ASSERT_(i<m_modes.size())
107  return m_modes[i];
108  }
109  /** Access to individual beacons */
110  TGaussianMode& get(size_t i) {
111  ASSERT_(i<m_modes.size())
112  return m_modes[i];
113  }
114 
115  /** Inserts a copy of the given mode into the SOG */
116  void push_back(const TGaussianMode& m) {
117  m_modes.push_back(m);
118  }
119 
120  iterator begin() { return m_modes.begin(); }
121  iterator end() { return m_modes.end(); }
122  const_iterator begin() const { return m_modes.begin(); }
123  const_iterator end()const { return m_modes.end(); }
124 
125  iterator erase(iterator i) { return m_modes.erase(i); }
126 
127  void resize(const size_t N); //!< Resize the number of SOG modes
128 
129  /** Merge very close modes so the overall number of modes is reduced while preserving the total distribution.
130  * This method uses the approach described in the paper:
131  * - "Kullback-Leibler Approach to Gaussian Mixture Reduction" AR Runnalls. IEEE Transactions on Aerospace and Electronic Systems, 2007.
132  *
133  * \param max_KLd The maximum KL-divergence to consider the merge of two nodes (and then stops the process).
134  */
135  void mergeModes( double max_KLd = 0.5, bool verbose = false );
136 
137  void getMean(CPose2D &mean_pose) const MRPT_OVERRIDE; //!< Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF) \sa getCovariance
138  void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov,CPose2D &mean_point) const MRPT_OVERRIDE; //!< Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once. \sa getMean
139  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 the mean. \sa getMean
140  void normalizeWeights(); //!< Normalize the weights in m_modes such as the maximum log-weight is 0
141 
142  void copyFrom(const CPosePDF &o) MRPT_OVERRIDE; //!< Copy operator, translating if necesary (for example, between particles and gaussian representations)
143 
144  /** Save the density to a text file, with the following format:
145  * There is one row per Gaussian "mode", and each row contains 10 elements:
146  * - w (The weight)
147  * - x_mean (gaussian mean value)
148  * - y_mean (gaussian mean value)
149  * - phi_mean (gaussian mean value)
150  * - C11 (Covariance elements)
151  * - C22 (Covariance elements)
152  * - C33 (Covariance elements)
153  * - C12 (Covariance elements)
154  * - C13 (Covariance elements)
155  * - C23 (Covariance elements)
156  */
157  void saveToTextFile(const std::string &file) const MRPT_OVERRIDE;
158 
159  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
160  * "to project" the current pdf. Result PDF substituted the currently stored one in the object. */
161  void changeCoordinatesReference(const CPose3D &newReferenceBase ) MRPT_OVERRIDE;
162 
163  void rotateAllCovariances(const double &ang); //!< Rotate all the covariance matrixes by replacing them 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$
164  void drawSingleSample( CPose2D &outPart ) const MRPT_OVERRIDE; //!< Draws a single sample from the distribution
165  void drawManySamples( size_t N, std::vector<mrpt::math::CVectorDouble> & outSamples ) const MRPT_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.
166  void inverse(CPosePDF &o) const MRPT_OVERRIDE; //!< Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
167 
168  void operator += ( const mrpt::poses::CPose2D &Ap); //!< Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated).
169 
170  double evaluatePDF( const mrpt::poses::CPose2D &x, bool sumOverAllPhis = false ) const; //!< Evaluates the PDF at a given point.
171  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,1].
172 
173  /** Evaluates the PDF within a rectangular grid (and a fixed orientation) and saves the result in a matrix (each row contains values for a fixed y-coordinate value). */
174  void evaluatePDFInArea(
175  const double & x_min,
176  const double & x_max,
177  const double & y_min,
178  const double & y_max,
179  const double & resolutionXY,
180  const double & phi,
181  mrpt::math::CMatrixD &outMatrix,
182  bool sumOverAllPhis = false );
183 
184  /** Bayesian fusion of two pose distributions, then save the result in this object (WARNING: Currently p1 must be a mrpt::poses::CPosePDFSOG object and p2 a mrpt::poses::CPosePDFGaussian object) */
185  void bayesianFusion(const CPosePDF &p1,const CPosePDF &p2, const double &minMahalanobisDistToDrop=0 ) MRPT_OVERRIDE;
186 
187  }; // End of class def.
189  } // End of namespace
190 } // End of namespace
191 #endif
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
Definition: CMatrixD.h:30
The struct for each mode:
Definition: CPosePDFSOG.h:45
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
Definition: CPosePDFSOG.h:37
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Scalar * iterator
Definition: eigen_plugins.h:23
mrpt::aligned_containers< TGaussianMode >::vector_t CListGaussianModes
Definition: CPosePDFSOG.h:71
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...
const Scalar * const_iterator
Definition: eigen_plugins.h:24
void push_back(const TGaussianMode &m)
Inserts a copy of the given mode into the SOG.
Definition: CPosePDFSOG.h:116
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
friend std::ostream & operator<<(std::ostream &o, const TGaussianMode &mode)
Definition: CPosePDFSOG.h:63
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:135
const_iterator end() const
Definition: CPosePDFSOG.h:123
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:70
const CListGaussianModes & getSOGModes() const
Definition: CPosePDFSOG.h:75
mrpt::math::CMatrixDouble33 cov
Definition: CPosePDFSOG.h:54
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:123
Eigen::Matrix< dataType, 4, 4 > inverse(Eigen::Matrix< dataType, 4, 4 > &pose)
Definition: Miscellaneous.h:74
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
bool empty() const
Return whether there is any Gaussian mode.
Definition: CPosePDFSOG.h:88
GLsizei const GLchar ** string
Definition: glext.h:3919
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:39
GLint mode
Definition: glext.h:5078
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...
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:36
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
#define ASSERT_(f)
size_t size() const
Return the number of Gaussian modes.
Definition: CPosePDFSOG.h:87
CListGaussianModes::iterator iterator
Definition: CPosePDFSOG.h:73
GLenum GLint x
Definition: glext.h:3516
const_iterator begin() const
Definition: CPosePDFSOG.h:122
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t
CListGaussianModes::const_iterator const_iterator
Definition: CPosePDFSOG.h:72
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
CListGaussianModes m_modes
The list of SOG modes.
Definition: CPosePDFSOG.h:80



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020