Main MRPT website > C++ reference for MRPT 1.9.9
CPoint2DPDFGaussian.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>
14 #include <mrpt/poses/CPoint3D.h>
15 #include <mrpt/math/CMatrixD.h>
17 #include <mrpt/utils/CStream.h>
19 #include <mrpt/system/os.h>
20 
21 using namespace mrpt::poses;
22 using namespace mrpt::utils;
23 using namespace mrpt::math;
24 using namespace mrpt::random;
25 using namespace mrpt::system;
26 
28 
29 /*---------------------------------------------------------------
30  Constructor
31  ---------------------------------------------------------------*/
33 /*---------------------------------------------------------------
34  Constructor
35  ---------------------------------------------------------------*/
37  const CPoint2D& init_Mean, const CMatrixDouble22& init_Cov)
38  : mean(init_Mean), cov(init_Cov)
39 {
40 }
41 
42 /*---------------------------------------------------------------
43  Constructor
44  ---------------------------------------------------------------*/
46  : mean(init_Mean), cov()
47 {
48 }
49 
50 /*---------------------------------------------------------------
51  writeToStream
52  ---------------------------------------------------------------*/
54  mrpt::utils::CStream& out, int* version) const
55 {
56  if (version)
57  *version = 0;
58  else
59  {
60  out << CPoint2D(mean) << cov;
61  }
62 }
63 
64 /*---------------------------------------------------------------
65  readFromStream
66  ---------------------------------------------------------------*/
68 {
69  switch (version)
70  {
71  case 0:
72  {
73  in >> mean >> cov;
74  }
75  break;
76  default:
78  };
79 }
80 
82 {
83  if (this == &o) return; // It may be used sometimes
84 
85  // Convert to gaussian pdf:
87 }
88 
89 /*---------------------------------------------------------------
90 
91  ---------------------------------------------------------------*/
93 {
95 
96  FILE* f = os::fopen(file.c_str(), "wt");
97  if (!f) return;
98 
99  os::fprintf(f, "%f %f\n", mean.x(), mean.y());
100 
101  os::fprintf(f, "%f %f\n", cov(0, 0), cov(0, 1));
102  os::fprintf(f, "%f %f\n", cov(1, 0), cov(1, 1));
103 
104  os::fclose(f);
105 
106  MRPT_END
107 }
108 
109 /*---------------------------------------------------------------
110  changeCoordinatesReference
111  ---------------------------------------------------------------*/
113  const CPose3D& newReferenceBase)
114 {
115  // Clip the 3x3 rotation matrix
116  const CMatrixDouble22 M =
117  newReferenceBase.getRotationMatrix().block(0, 0, 2, 2);
118 
119  // The mean:
120  mean = CPoint2D(newReferenceBase + mean);
121 
122  // The covariance:
123  cov = M * cov * M.transpose();
124 }
125 
126 /*---------------------------------------------------------------
127  bayesianFusion
128  ---------------------------------------------------------------*/
130  const CPoint2DPDFGaussian& p1, const CPoint2DPDFGaussian& p2)
131 {
132  MRPT_START
133 
134  CMatrixDouble22 C1_inv;
135  p1.cov.inv(C1_inv);
136 
137  CMatrixDouble22 C2_inv;
138  p2.cov.inv(C2_inv);
139 
140  CMatrixDouble22 L = C1_inv;
141  L += C2_inv;
142 
143  L.inv(cov); // The new cov.
144 
147  CMatrixDouble21 x = cov * (C1_inv * x1 + C2_inv * x2);
148 
149  mean.x(x.get_unsafe(0, 0));
150  mean.y(x.get_unsafe(1, 0));
151 
152  std::cout << "IN1: " << p1.mean << "\n" << p1.cov << "\n";
153  std::cout << "IN2: " << p2.mean << "\n" << p2.cov << "\n";
154  std::cout << "OUT: " << mean << "\n" << cov << "\n";
155 
156  MRPT_END
157 }
158 
159 /*---------------------------------------------------------------
160  productIntegralWith
161  ---------------------------------------------------------------*/
163  const CPoint2DPDFGaussian& p) const
164 {
165  MRPT_START
166  // --------------------------------------------------------------
167  // 12/APR/2009 - Jose Luis Blanco:
168  // The integral over all the variable space of the product of two
169  // Gaussians variables amounts to simply the evaluation of
170  // a normal PDF at (0,0), with mean=M1-M2 and COV=COV1+COV2
171  // ---------------------------------------------------------------
172  CMatrixDouble22 C = cov + p.cov; // Sum of covs:
173 
174  CMatrixDouble22 C_inv;
175  C.inv(C_inv);
176 
177  CMatrixDouble21 MU(UNINITIALIZED_MATRIX); // Diff. of means
178  MU.get_unsafe(0, 0) = mean.x() - p.mean.x();
179  MU.get_unsafe(1, 0) = mean.y() - p.mean.y();
180 
181  return std::pow(M_2PI, -0.5 * state_length) * (1.0 / std::sqrt(C.det())) *
182  exp(-0.5 * MU.multiply_HtCH_scalar(C_inv));
183 
184  MRPT_END
185 }
186 
187 /*---------------------------------------------------------------
188  productIntegralNormalizedWith
189  ---------------------------------------------------------------*/
191  const CPoint2DPDFGaussian& p) const
192 {
193  return std::exp(-0.5 * square(mahalanobisDistanceTo(p)));
194 }
195 
196 /*---------------------------------------------------------------
197  drawSingleSample
198  ---------------------------------------------------------------*/
200 {
201  MRPT_START
202 
203  // Eigen3 emits an out-of-array warning here, but it seems to be a false
204  // warning? (WTF)
205  CVectorDouble vec;
207 
208  ASSERT_(vec.size() == 2);
209  outSample.x(mean.x() + vec[0]);
210  outSample.y(mean.y() + vec[1]);
211 
212  MRPT_END
213 }
214 
215 /*---------------------------------------------------------------
216  bayesianFusion
217  ---------------------------------------------------------------*/
219  const CPoint2DPDF& p1_, const CPoint2DPDF& p2_,
220  const double& minMahalanobisDistToDrop)
221 {
222  MRPT_UNUSED_PARAM(minMahalanobisDistToDrop);
223  MRPT_START
224 
225  // p1: CPoint2DPDFGaussian, p2: CPosePDFGaussian:
228 
229  THROW_EXCEPTION("TODO!!!");
230 
231  MRPT_END
232 }
233 
234 /*---------------------------------------------------------------
235  mahalanobisDistanceTo
236  ---------------------------------------------------------------*/
238  const CPoint2DPDFGaussian& other) const
239 {
240  // The difference in means:
241  Eigen::Matrix<double, 2, 1> deltaX;
242  deltaX[0] = other.mean.x() - mean.x();
243  deltaX[1] = other.mean.y() - mean.y();
244 
245  // The inverse of the combined covs:
246  return std::sqrt(
247  deltaX.multiply_HtCH_scalar((other.cov + this->cov).inverse()));
248 }
249 
250 /** Returns the Mahalanobis distance from this PDF to some point */
252  const double x, const double y) const
253 {
254  // The difference in means:
255  Eigen::Matrix<double, 2, 1> deltaX;
256  deltaX[0] = x - mean.x();
257  deltaX[1] = y - mean.y();
258 
259  // The inverse of the combined covs:
260  return std::sqrt(deltaX.multiply_HtCH_scalar(this->cov.inverse()));
261 }
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
A numeric matrix of compile-time fixed size.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: types_math.h:70
A class used to store a 2D point.
Definition: CPoint2D.h:37
A gaussian distribution for 2D points.
CPoint2DPDFGaussian()
Default constructor.
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
void bayesianFusion(const CPoint2DPDFGaussian &p1, const CPoint2DPDFGaussian &p2)
Bayesian fusion of two points gauss.
double productIntegralWith(const CPoint2DPDFGaussian &p) const
Computes the "correspondence likelihood" of this PDF with another one: This is implemented as the int...
void drawSingleSample(CPoint2D &outSample) const override
Draw a sample from the pdf.
double mahalanobisDistanceTo(const CPoint2DPDFGaussian &other) const
Returns the Mahalanobis distance from this PDF to another PDF, that is, it's evaluation at (0,...
mrpt::math::CMatrixDouble22 cov
The 2x2 covariance matrix.
void saveToTextFile(const std::string &file) const override
Save PDF's particles to a text file, containing the 2D pose in the first line, then the covariance ma...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
void copyFrom(const CPoint2DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations)
double mahalanobisDistanceToPoint(const double x, const double y) const
Returns the Mahalanobis distance from this PDF to some point.
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...
double productIntegralNormalizedWith(const CPoint2DPDFGaussian &p) const
Computes the "correspondence likelihood" of this PDF with another one: This is implemented as the int...
Declares a class that represents a Probability Distribution function (PDF) of a 2D point (x,...
Definition: CPoint2DPDF.h:39
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:89
void getRotationMatrix(mrpt::math::CMatrixDouble33 &ROT) const
Get the 3x3 rotation matrix.
Definition: CPose3D.h:237
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
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.
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,...
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).
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:42
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
GLenum GLint GLint y
Definition: glext.h:3538
GLuint in
Definition: glext.h:7274
GLenum GLint x
Definition: glext.h:3538
GLfloat GLfloat p
Definition: glext.h:6305
GLsizei const GLchar ** string
Definition: glext.h:4101
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:272
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:254
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
This file implements matrix/vector text and binary serialization.
#define MRPT_START
Definition: mrpt_macros.h:425
#define ASSERT_(f)
Definition: mrpt_macros.h:309
#define M_2PI
Definition: mrpt_macros.h:437
#define MRPT_END
Definition: mrpt_macros.h:429
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: mrpt_macros.h:181
#define THROW_EXCEPTION(msg)
Definition: mrpt_macros.h:111
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
Definition: mrpt_macros.h:365
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:20
CMatrixFixedNumeric< double, 2, 1 > CMatrixDouble21
Definition: eigen_frwds.h:62
@ UNINITIALIZED_MATRIX
Definition: math_frwds.h:86
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
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:18
A namespace of pseudo-random numbers genrators of diferent distributions.
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:31
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST