Main MRPT website > C++ reference for MRPT 1.9.9
CPointPDFGaussian.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/poses/CPose3D.h>
16 #include <mrpt/system/os.h>
17 #include <mrpt/utils/CStream.h>
18 
19 using namespace mrpt::poses;
20 using namespace mrpt::utils;
21 using namespace mrpt::math;
22 using namespace mrpt::random;
23 using namespace mrpt::system;
24 
26 
27 /*---------------------------------------------------------------
28  Constructor
29  ---------------------------------------------------------------*/
31 /*---------------------------------------------------------------
32  Constructor
33  ---------------------------------------------------------------*/
35  const CPoint3D& init_Mean, const CMatrixDouble33& init_Cov)
36  : mean(init_Mean), cov(init_Cov)
37 {
38 }
39 
40 /*---------------------------------------------------------------
41  Constructor
42  ---------------------------------------------------------------*/
44  : mean(init_Mean), cov()
45 {
46  cov.zeros();
47 }
48 
49 /*---------------------------------------------------------------
50  getMean
51  Returns an estimate of the pose, (the mean, or mathematical expectation of the
52  PDF)
53  ---------------------------------------------------------------*/
55 /*---------------------------------------------------------------
56  getCovarianceAndMean
57  ---------------------------------------------------------------*/
59  CMatrixDouble33& C, CPoint3D& p) const
60 {
61  p = mean;
62  C = cov;
63 }
64 
65 /*---------------------------------------------------------------
66  writeToStream
67  ---------------------------------------------------------------*/
69  mrpt::utils::CStream& out, int* version) const
70 {
71  if (version)
72  *version = 1;
73  else
74  {
75  out << CPoint3D(mean) << cov;
76  }
77 }
78 
79 /*---------------------------------------------------------------
80  readFromStream
81  ---------------------------------------------------------------*/
83 {
84  switch (version)
85  {
86  case 0:
87  {
88  in >> mean;
89 
90  CMatrix c;
91  in >> c;
92  cov = c.cast<double>();
93  }
94  break;
95  case 1:
96  {
97  in >> mean >> cov;
98  }
99  break;
100  default:
102  };
103 }
104 
106 {
107  if (this == &o) return; // It may be used sometimes
108 
109  // Convert to gaussian pdf:
111 }
112 
113 /*---------------------------------------------------------------
114 
115  ---------------------------------------------------------------*/
117 {
118  MRPT_START
119 
120  FILE* f = os::fopen(file.c_str(), "wt");
121  if (!f) return;
122 
123  os::fprintf(f, "%f %f %f\n", mean.x(), mean.y(), mean.z());
124 
125  os::fprintf(f, "%f %f %f\n", cov(0, 0), cov(0, 1), cov(0, 2));
126  os::fprintf(f, "%f %f %f\n", cov(1, 0), cov(1, 1), cov(1, 2));
127  os::fprintf(f, "%f %f %f\n", cov(2, 0), cov(2, 1), cov(2, 2));
128 
129  os::fclose(f);
130 
131  MRPT_END
132 }
133 
134 /*---------------------------------------------------------------
135  changeCoordinatesReference
136  ---------------------------------------------------------------*/
138  const CPose3D& newReferenceBase)
139 {
140  const CMatrixDouble33& M = newReferenceBase.getRotationMatrix();
141 
142  // The mean:
143  mean = newReferenceBase + mean;
144 
145  // The covariance:
146  M.multiply_HCHt(CMatrixDouble33(cov), cov); // save in cov
147 }
148 
149 /*---------------------------------------------------------------
150  bayesianFusion
151  ---------------------------------------------------------------*/
153  const CPointPDFGaussian& p1, const CPointPDFGaussian& p2)
154 {
155  MRPT_START
156 
157  CMatrixDouble x1(3, 1), x2(3, 1), x(3, 1);
158  CMatrixDouble C1(p1.cov);
159  CMatrixDouble C2(p2.cov);
160  CMatrixDouble C1_inv = C1.inv();
161  CMatrixDouble C2_inv = C2.inv();
162  CMatrixDouble C;
163 
164  x1(0, 0) = p1.mean.x();
165  x1(1, 0) = p1.mean.y();
166  x1(2, 0) = p1.mean.z();
167  x2(0, 0) = p2.mean.x();
168  x2(1, 0) = p2.mean.y();
169  x2(2, 0) = p2.mean.z();
170 
171  C = (C1_inv + C2_inv).inv();
172  cov = C;
173 
174  x = C * (C1_inv * x1 + C2_inv * x2);
175 
176  mean.x(x(0, 0));
177  mean.y(x(1, 0));
178  mean.z(x(2, 0));
179 
180  // std::cout << "IN1: " << x1 << "\n" << C1 << "\n";
181  // std::cout << "IN2: " << x2 << "\n" << C2 << "\n";
182  // std::cout << "OUT: " << x << "\n" << C << "\n";
183 
184  MRPT_END
185 }
186 
187 /*---------------------------------------------------------------
188  productIntegralWith
189  ---------------------------------------------------------------*/
191 {
192  MRPT_START
193 
194  // --------------------------------------------------------------
195  // 12/APR/2009 - Jose Luis Blanco:
196  // The integral over all the variable space of the product of two
197  // Gaussians variables amounts to simply the evaluation of
198  // a normal PDF at (0,0), with mean=M1-M2 and COV=COV1+COV2
199  // ---------------------------------------------------------------
200  CMatrixDouble33 C = cov;
201  C += p.cov; // Sum of covs:
202  CMatrixDouble33 C_inv;
203  C.inv(C_inv);
204 
205  CMatrixDouble31 MU(UNINITIALIZED_MATRIX); // Diff. of means
206  MU.get_unsafe(0, 0) = mean.x() - p.mean.x();
207  MU.get_unsafe(1, 0) = mean.y() - p.mean.y();
208  MU.get_unsafe(2, 0) = mean.z() - p.mean.z();
209 
210  return std::pow(M_2PI, -0.5 * state_length) * (1.0 / std::sqrt(C.det())) *
211  exp(-0.5 * MU.multiply_HtCH_scalar(C_inv));
212 
213  MRPT_END
214 }
215 
216 /*---------------------------------------------------------------
217  productIntegralWith2D
218  ---------------------------------------------------------------*/
220  const CPointPDFGaussian& p) const
221 {
222  MRPT_START
223 
224  // --------------------------------------------------------------
225  // 12/APR/2009 - Jose Luis Blanco:
226  // The integral over all the variable space of the product of two
227  // Gaussians variables amounts to simply the evaluation of
228  // a normal PDF at (0,0), with mean=M1-M2 and COV=COV1+COV2
229  // ---------------------------------------------------------------
230  CMatrixDouble22 C = cov.block(0, 0, 2, 2);
231  C += p.cov.block(0, 0, 2, 2); // Sum of covs:
232 
233  CMatrixDouble22 C_inv;
234  C.inv(C_inv);
235 
236  CMatrixDouble21 MU(UNINITIALIZED_MATRIX); // Diff. of means
237  MU.get_unsafe(0, 0) = mean.x() - p.mean.x();
238  MU.get_unsafe(1, 0) = mean.y() - p.mean.y();
239 
240  return std::pow(M_2PI, -0.5 * (state_length - 1)) *
241  (1.0 / std::sqrt(C.det())) *
242  exp(-0.5 * MU.multiply_HtCH_scalar(C_inv));
243 
244  MRPT_END
245 }
246 
247 /*---------------------------------------------------------------
248  productIntegralNormalizedWith
249  ---------------------------------------------------------------*/
251  const CPointPDFGaussian& p) const
252 {
253  return std::exp(-0.5 * square(mahalanobisDistanceTo(p)));
254 }
255 
256 /*---------------------------------------------------------------
257  productIntegralNormalizedWith
258  ---------------------------------------------------------------*/
260  const CPointPDFGaussian& p) const
261 {
262  return std::exp(-0.5 * square(mahalanobisDistanceTo(p, true)));
263 }
264 
265 /*---------------------------------------------------------------
266  drawSingleSample
267  ---------------------------------------------------------------*/
269 {
270  MRPT_START
271 
272  CVectorDouble vec;
274 
275  ASSERT_(vec.size() == 3);
276  outSample.x(mean.x() + vec[0]);
277  outSample.y(mean.y() + vec[1]);
278  outSample.z(mean.z() + vec[2]);
279 
280  MRPT_END
281 }
282 
283 /*---------------------------------------------------------------
284  bayesianFusion
285  ---------------------------------------------------------------*/
287  const CPointPDF& p1_, const CPointPDF& p2_,
288  const double& minMahalanobisDistToDrop)
289 {
290  MRPT_UNUSED_PARAM(minMahalanobisDistToDrop);
291  MRPT_START
292 
293  // p1: CPointPDFGaussian, p2: CPosePDFGaussian:
296 
297  THROW_EXCEPTION("TODO!!!");
298 
299  MRPT_END
300 }
301 
302 /*---------------------------------------------------------------
303  mahalanobisDistanceTo
304  ---------------------------------------------------------------*/
306  const CPointPDFGaussian& other, bool only_2D) const
307 {
308  // The difference in means:
309  CMatrixDouble13 deltaX;
310  deltaX.get_unsafe(0, 0) = other.mean.x() - mean.x();
311  deltaX.get_unsafe(0, 1) = other.mean.y() - mean.y();
312  deltaX.get_unsafe(0, 2) = other.mean.z() - mean.z();
313 
314  // The inverse of the combined covs:
315  CMatrixDouble33 COV = other.cov;
316  COV += this->cov;
317 
318  if (!only_2D)
319  {
320  CMatrixDouble33 COV_inv;
321  COV.inv(COV_inv);
322  return sqrt(deltaX.multiply_HCHt_scalar(COV_inv));
323  }
324  else
325  {
326  CMatrixDouble22 C = COV.block(0, 0, 2, 2);
327  CMatrixDouble22 COV_inv;
328  C.inv(COV_inv);
329  CMatrixDouble12 deltaX2 = deltaX.block(0, 0, 1, 2);
330  return std::sqrt(deltaX2.multiply_HCHt_scalar(COV_inv));
331  }
332 }
void saveToTextFile(const std::string &file) const override
Save PDF&#39;s particles to a text file, containing the 2D pose in the first line, then the covariance ma...
void copyFrom(const CPointPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
A namespace of pseudo-random numbers genrators of diferent distributions.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
void getMean(CPoint3D &p) const override
Returns an estimate of the point, (the mean, or mathematical expectation of the PDF) ...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:272
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
CPoint3D mean
The mean value.
#define THROW_EXCEPTION(msg)
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
CMatrixFixedNumeric< double, 3, 3 > CMatrixDouble33
Definition: eigen_frwds.h:55
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
#define M_2PI
Definition: mrpt_macros.h:437
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
void bayesianFusion(const CPointPDFGaussian &p1, const CPointPDFGaussian &p2)
Bayesian fusion of two points gauss.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
const GLubyte * c
Definition: glext.h:6313
double productIntegralNormalizedWith(const CPointPDFGaussian &p) const
Computes the "correspondence likelihood" of this PDF with another one: This is implemented as the int...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
mrpt::math::CMatrixDouble33 cov
The 3x3 covariance matrix.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
GLsizei const GLchar ** string
Definition: glext.h:4101
A class used to store a 3D point.
Definition: CPoint3D.h:32
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
#define MRPT_START
double productIntegralNormalizedWith2D(const CPointPDFGaussian &p) const
Computes the "correspondence likelihood" of this PDF with another one: This is implemented as the int...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
double mahalanobisDistanceTo(const CPointPDFGaussian &other, bool only_2D=false) const
Returns the Mahalanobis distance from this PDF to another PDF, that is, it&#39;s evaluation at (0...
This file implements matrix/vector text and binary serialization.
double productIntegralWith(const CPointPDFGaussian &p) const
Computes the "correspondence likelihood" of this PDF with another one: This is implemented as the int...
GLuint in
Definition: glext.h:7274
void drawSingleSample(CPoint3D &outSample) const override
Draw a sample from the pdf.
#define ASSERT_(f)
static const size_t state_length
The length of the variable, for example, 3 for a 3D point, 6 for a 3D pose (x y z yaw pitch roll)...
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:254
virtual void getCovarianceAndMean(mrpt::math::CMatrixFixedNumeric< double, STATE_LEN, STATE_LEN > &cov, TDATA &mean_point) const =0
Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean...
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
void drawGaussianMultivariate(std::vector< T > &out_result, const mrpt::math::CMatrixTemplateNumeric< T > &cov, const std::vector< T > *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
GLenum GLint x
Definition: glext.h:3538
void getRotationMatrix(mrpt::math::CMatrixDouble33 &ROT) const
Get the 3x3 rotation matrix.
Definition: CPose3D.h:237
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:25
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Declares a class that represents a Probability Distribution function (PDF) of a 3D point (x...
Definition: CPointPDF.h:39
GLfloat GLfloat p
Definition: glext.h:6305
void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPoint3D &mean_point) const override
Returns an estimate of the point covariance matrix (3x3 cov matrix) and the mean, both at once...
CPointPDFGaussian()
Default constructor.
double productIntegralWith2D(const CPointPDFGaussian &p) const
Computes the "correspondence likelihood" of this PDF with another one: This is implemented as the int...
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
A gaussian distribution for 3D points.
EIGEN_STRONG_INLINE PlainObject inv() const



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019