Main MRPT website > C++ reference for MRPT 1.3.2
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-2015, 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 
16 
17 namespace mrpt
18 {
19  namespace poses
20  {
21  // This must be added to any CSerializable derived class:
22  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CPosePDFSOG , CPosePDF )
23 
24  /** Declares a class that represents a Probability Density function (PDF) of a 2D pose \f$ p(\mathbf{x}) = [x ~ y ~ \phi ]^t \f$.
25  * This class implements that PDF as the following multi-modal Gaussian distribution:
26  *
27  * \f$ p(\mathbf{x}) = \sum\limits_{i=1}^N \omega^i \mathcal{N}( \mathbf{x} ; \bar{\mathbf{x}}^i, \mathbf{\Sigma}^i ) \f$
28  *
29  * Where the number of modes N is the size of CPosePDFSOG::m_modes
30  *
31  * See mrpt::poses::CPosePDF for more details.
32  *
33  * \sa CPose2D, CPosePDF, CPosePDFParticles
34  * \ingroup poses_pdf_grp
35  */
37  {
38  // This must be added to any CSerializable derived class:
40 
41  public:
42  /** The struct for each mode:
43  */
45  {
47  mean(),
48  cov(),
49  log_w(0)
50  { }
51 
54 
55  /** The log-weight
56  */
57  double log_w;
58 
59  public:
61  };
62 
66 
67  protected:
68  /** Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor lead to non-symmetric matrixes!)
69  */
70  void assureSymmetry();
71 
72  /** The list of SOG modes */
73  CListGaussianModes m_modes;
74 
75  public:
76  /** Default constructor
77  * \param nModes The initial size of CPosePDFSOG::m_modes
78  */
79  CPosePDFSOG( size_t nModes = 1 );
80 
81  size_t size() const { return m_modes.size(); } //!< Return the number of Gaussian modes.
82  bool empty() const { return m_modes.empty(); } //!< Return whether there is any Gaussian mode.
83 
84  /** Clear the list of modes */
85  void clear();
86 
87  /** Access to individual beacons */
88  const TGaussianMode& operator [](size_t i) const {
89  ASSERT_(i<m_modes.size())
90  return m_modes[i];
91  }
92  /** Access to individual beacons */
93  TGaussianMode& operator [](size_t i) {
94  ASSERT_(i<m_modes.size())
95  return m_modes[i];
96  }
97 
98  /** Access to individual beacons */
99  const TGaussianMode& get(size_t i) const {
100  ASSERT_(i<m_modes.size())
101  return m_modes[i];
102  }
103  /** Access to individual beacons */
104  TGaussianMode& get(size_t i) {
105  ASSERT_(i<m_modes.size())
106  return m_modes[i];
107  }
108 
109  /** Inserts a copy of the given mode into the SOG */
110  void push_back(const TGaussianMode& m) {
111  m_modes.push_back(m);
112  }
113 
114  iterator begin() { return m_modes.begin(); }
115  iterator end() { return m_modes.end(); }
116  const_iterator begin() const { return m_modes.begin(); }
117  const_iterator end()const { return m_modes.end(); }
118 
119  iterator erase(iterator i) { return m_modes.erase(i); }
120 
121  void resize(const size_t N); //!< Resize the number of SOG modes
122 
123  /** Merge very close modes so the overall number of modes is reduced while preserving the total distribution.
124  * This method uses the approach described in the paper:
125  * - "Kullback-Leibler Approach to Gaussian Mixture Reduction" AR Runnalls. IEEE Transactions on Aerospace and Electronic Systems, 2007.
126  *
127  * \param max_KLd The maximum KL-divergence to consider the merge of two nodes (and then stops the process).
128  */
129  void mergeModes( double max_KLd = 0.5, bool verbose = false );
130 
131  /** Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
132  * \sa getCovariance
133  */
134  void getMean(CPose2D &mean_pose) const;
135 
136  /** Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once.
137  * \sa getMean
138  */
139  void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov,CPose2D &mean_point) const;
140 
141  /** For the most likely Gaussian mode in the SOG, returns the pose covariance matrix (3x3 cov matrix) and the mean.
142  * \sa getMean
143  */
144  void getMostLikelyCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov,CPose2D &mean_point) const;
145 
146  /** Normalize the weights in m_modes such as the maximum log-weight is 0.
147  */
148  void normalizeWeights();
149 
150  /** Copy operator, translating if necesary (for example, between particles and gaussian representations)
151  */
152  void copyFrom(const CPosePDF &o);
153 
154  /** Save the density to a text file, with the following format:
155  * There is one row per Gaussian "mode", and each row contains 10 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  */
168  void saveToTextFile(const std::string &file) const;
169 
170  /** this = p (+) this. This can be used to convert a PDF from local coordinates to global, providing the point (newReferenceBase) from which
171  * "to project" the current pdf. Result PDF substituted the currently stored one in the object.
172  */
173  void changeCoordinatesReference(const CPose3D &newReferenceBase );
174 
175  /** 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$.
176  */
177  void rotateAllCovariances(const double &ang);
178 
179  /** Draws a single sample from the distribution
180  */
181  void drawSingleSample( CPose2D &outPart ) const;
182 
183  /** 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.
184  */
185  void drawManySamples( size_t N, std::vector<mrpt::math::CVectorDouble> & outSamples ) const;
186 
187  /** Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF
188  */
189  void inverse(CPosePDF &o) const;
190 
191  /** Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matrix are updated).
192  */
193  void operator += ( const mrpt::poses::CPose2D &Ap);
194 
195  /** Evaluates the PDF at a given point.
196  */
197  double evaluatePDF( const mrpt::poses::CPose2D &x, bool sumOverAllPhis = false ) const;
198 
199  /** Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in the range [0,1].
200  */
201  double evaluateNormalizedPDF( const mrpt::poses::CPose2D &x ) const;
202 
203  /** 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).
204  */
205  void evaluatePDFInArea(
206  const double & x_min,
207  const double & x_max,
208  const double & y_min,
209  const double & y_max,
210  const double & resolutionXY,
211  const double & phi,
212  mrpt::math::CMatrixD &outMatrix,
213  bool sumOverAllPhis = false );
214 
215  /** 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)
216  */
217  void bayesianFusion(const CPosePDF &p1,const CPosePDF &p2, const double &minMahalanobisDistToDrop=0 );
218 
219 
220  }; // End of class def.
222 
223  } // End of namespace
224 } // End of namespace
225 
226 #endif
bool empty() const
Return whether there is any Gaussian mode.
Definition: CPosePDFSOG.h:82
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
Definition: CMatrixD.h:30
const_iterator end() const
Definition: CPosePDFSOG.h:117
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:36
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Definition: memory.h:112
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_iterator begin() const
Definition: CPosePDFSOG.h:116
Scalar * iterator
Definition: eigen_plugins.h:23
mrpt::aligned_containers< TGaussianMode >::vector_t CListGaussianModes
Definition: CPosePDFSOG.h:63
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:110
A numeric matrix of compile-time fixed size.
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
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:70
mrpt::math::CMatrixDouble33 cov
Definition: CPosePDFSOG.h:53
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...
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:39
size_t size() const
Return the number of Gaussian modes.
Definition: CPosePDFSOG.h:81
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:119
A class used to store a 2D pose.
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)
CListGaussianModes::iterator iterator
Definition: CPosePDFSOG.h:65
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t
CListGaussianModes::const_iterator const_iterator
Definition: CPosePDFSOG.h:64
CListGaussianModes m_modes
The list of SOG modes.
Definition: CPosePDFSOG.h:73



Page generated by Doxygen 1.8.11 for MRPT 1.3.2 SVN: at Wed May 25 02:34:21 UTC 2016