Main MRPT website > C++ reference for MRPT 1.9.9
CPose3DPDFGaussianInf.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 
12 #include <mrpt/random.h>
13 #include <mrpt/math/wrap2pi.h>
14 #include <mrpt/utils/CStream.h>
15 #include <mrpt/poses/CPose3D.h>
20 #include <mrpt/system/os.h>
21 
22 using namespace mrpt;
23 using namespace mrpt::poses;
24 using namespace mrpt::math;
25 using namespace mrpt::random;
26 using namespace mrpt::utils;
27 using namespace mrpt::system;
28 using namespace std;
29 
31 
32 /*---------------------------------------------------------------
33  Constructor
34  ---------------------------------------------------------------*/
36 /*---------------------------------------------------------------
37  Constructor
38  ---------------------------------------------------------------*/
40  TConstructorFlags_Poses constructor_dummy_param)
42 {
43  MRPT_UNUSED_PARAM(constructor_dummy_param);
44 }
45 
46 /*---------------------------------------------------------------
47  Constructor
48  ---------------------------------------------------------------*/
50  const CPose3D& init_Mean, const CMatrixDouble66& init_Cov)
51  : mean(init_Mean), cov_inv(init_Cov)
52 {
53 }
54 
55 /*---------------------------------------------------------------
56  Constructor
57  ---------------------------------------------------------------*/
59  : mean(init_Mean), cov_inv()
60 {
61 }
62 
63 /*---------------------------------------------------------------
64  CPose3DPDFGaussianInf
65  ---------------------------------------------------------------*/
68 {
69  this->copyFrom(o);
70 }
71 
72 /*---------------------------------------------------------------
73  copyFrom
74  ---------------------------------------------------------------*/
76 {
77  const CPose3DPDFGaussian p(o);
78  this->copyFrom(p);
79 }
80 
81 /*---------------------------------------------------------------
82  writeToStream
83  ---------------------------------------------------------------*/
85  mrpt::utils::CStream& out, int* version) const
86 {
87  if (version)
88  *version = 0;
89  else
90  {
91  out << mean;
92  for (size_t r = 0; r < size(cov_inv, 1); r++)
93  out << cov_inv.get_unsafe(r, r);
94  for (size_t r = 0; r < size(cov_inv, 1); r++)
95  for (size_t c = r + 1; c < size(cov_inv, 2); c++)
96  out << cov_inv.get_unsafe(r, c);
97  }
98 }
99 
100 /*---------------------------------------------------------------
101  readFromStream
102  ---------------------------------------------------------------*/
104  mrpt::utils::CStream& in, int version)
105 {
106  switch (version)
107  {
108  case 0:
109  {
110  in >> mean;
111 
112  for (size_t r = 0; r < size(cov_inv, 1); r++)
113  in >> cov_inv.get_unsafe(r, r);
114  for (size_t r = 0; r < size(cov_inv, 1); r++)
115  for (size_t c = r + 1; c < size(cov_inv, 2); c++)
116  {
117  double x;
118  in >> x;
119  cov_inv.get_unsafe(r, c) = cov_inv.get_unsafe(c, r) = x;
120  }
121  }
122  break;
123  default:
125  };
126 }
127 
129 {
130  if (this == &o) return; // It may be used sometimes
131 
133  { // It's my same class:
134  const CPose3DPDFGaussianInf* ptr =
135  static_cast<const CPose3DPDFGaussianInf*>(&o);
136  mean = ptr->mean;
137  cov_inv = ptr->cov_inv;
138  }
139  else
140  {
141  // Convert to gaussian pdf:
144  cov.inv_fast(this->cov_inv);
145  }
146 }
147 
149 {
150  if (IS_CLASS(&o, CPosePDFGaussianInf))
151  { // cov is already inverted, but it's a 2D pose:
152  const CPosePDFGaussianInf* ptr =
153  static_cast<const CPosePDFGaussianInf*>(&o);
154 
155  mean = CPose3D(ptr->mean);
156 
157  // 3x3 inv_cov -> 6x6 inv_cov
158  cov_inv.zeros();
159  cov_inv(0, 0) = ptr->cov_inv(0, 0);
160  cov_inv(1, 1) = ptr->cov_inv(1, 1);
161  cov_inv(3, 3) = ptr->cov_inv(2, 2);
162 
163  cov_inv(0, 1) = cov_inv(1, 0) = ptr->cov_inv(0, 1);
164  cov_inv(0, 3) = cov_inv(3, 0) = ptr->cov_inv(0, 2);
165  cov_inv(1, 3) = cov_inv(3, 1) = ptr->cov_inv(1, 2);
166  }
167  else
168  {
170  p.copyFrom(o);
171  this->copyFrom(p);
172  }
173 }
174 
175 /*---------------------------------------------------------------
176 
177  ---------------------------------------------------------------*/
178 void CPose3DPDFGaussianInf::saveToTextFile(const string& file) const
179 {
180  FILE* f = os::fopen(file.c_str(), "wt");
181  if (!f) return;
182 
183  os::fprintf(
184  f, "%e %e %e %e %e %e\n", mean.x(), mean.y(), mean.z(), mean.yaw(),
185  mean.pitch(), mean.roll());
186 
187  for (unsigned int i = 0; i < 6; i++)
188  os::fprintf(
189  f, "%e %e %e %e %e %e\n", cov_inv(i, 0), cov_inv(i, 1),
190  cov_inv(i, 2), cov_inv(i, 3), cov_inv(i, 4), cov_inv(i, 5));
191 
192  os::fclose(f);
193 }
194 
195 /*---------------------------------------------------------------
196  changeCoordinatesReference
197  ---------------------------------------------------------------*/
199  const CPose3D& newReferenceBase)
200 {
201  MRPT_START
202 
204  a.copyFrom(*this);
205  a.changeCoordinatesReference(newReferenceBase);
206  this->copyFrom(a);
207 
208  MRPT_END
209 }
210 
211 /*---------------------------------------------------------------
212  drawSingleSample
213  ---------------------------------------------------------------*/
215 {
216  MRPT_UNUSED_PARAM(outPart);
217  MRPT_START
218 
220  this->cov_inv.inv(cov);
221 
224 
225  outPart.setFromValues(
226  mean.x() + v[0], mean.y() + v[1], mean.z() + v[2], mean.yaw() + v[3],
227  mean.pitch() + v[4], mean.roll() + v[5]);
228 
230  cov_inv.saveToTextFile(
231  "__DEBUG_EXC_DUMP_drawSingleSample_COV_INV.txt"););
232 }
233 
234 /*---------------------------------------------------------------
235  drawManySamples
236  ---------------------------------------------------------------*/
238  size_t N, vector<CVectorDouble>& outSamples) const
239 {
240  MRPT_START
241 
243  this->cov_inv.inv(cov);
244 
246 
247  for (vector<CVectorDouble>::iterator it = outSamples.begin();
248  it != outSamples.end(); ++it)
249  {
250  (*it)[0] += mean.x();
251  (*it)[1] += mean.y();
252  (*it)[2] += mean.z();
253  (*it)[3] = math::wrapToPi((*it)[3] + mean.yaw());
254  (*it)[4] = math::wrapToPi((*it)[4] + mean.pitch());
255  (*it)[5] = math::wrapToPi((*it)[5] + mean.roll());
256  }
257 
258  MRPT_END
259 }
260 
261 /*---------------------------------------------------------------
262  bayesianFusion
263  ---------------------------------------------------------------*/
265  const CPose3DPDF& p1_, const CPose3DPDF& p2_)
266 {
267  MRPT_UNUSED_PARAM(p1_);
268  MRPT_UNUSED_PARAM(p2_);
269 
270  THROW_EXCEPTION("TO DO!!!");
271 }
272 
273 /*---------------------------------------------------------------
274  inverse
275  ---------------------------------------------------------------*/
277 {
279  CPose3DPDFGaussianInf& out = static_cast<CPose3DPDFGaussianInf&>(o);
280 
281  // This is like: b=(0,0,0)
282  // OUT = b - THIS
283  CPose3DPDFGaussianInf b; // Init: all zeros.
284  out = b - *this;
285 }
286 
287 /*---------------------------------------------------------------
288  +=
289  ---------------------------------------------------------------*/
291 {
292  // COV:
293  const CMatrixDouble66 OLD_COV_INV = this->cov_inv;
295 
297  this->mean, // x
298  Ap, // u
299  df_dx, df_du);
300 
301  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
302  // cov_inv = ... => The same than above!
303  df_dx.multiply_HCHt(OLD_COV_INV, cov_inv);
304  // df_du: Nothing to do, since COV(Ap) = zeros
305 
306  // MEAN:
307  this->mean = this->mean + Ap;
308 }
309 
310 /*---------------------------------------------------------------
311  +=
312  ---------------------------------------------------------------*/
314 {
317  a.copyFrom(*this);
318  b.copyFrom(Ap);
319 
320  a += b;
321 
322  this->mean = a.mean;
323  a.cov.inv(this->cov_inv);
324 }
325 
326 /*---------------------------------------------------------------
327  -=
328  ---------------------------------------------------------------*/
330 {
333  a.copyFrom(*this);
334  b.copyFrom(Ap);
335 
336  a -= b;
337 
338  this->mean = a.mean;
339  a.cov.inv(this->cov_inv);
340 }
341 
342 /*---------------------------------------------------------------
343  evaluatePDF
344  ---------------------------------------------------------------*/
346 {
348  THROW_EXCEPTION("TO DO!!!");
349 }
350 
351 /*---------------------------------------------------------------
352  evaluateNormalizedPDF
353  ---------------------------------------------------------------*/
355 {
357  THROW_EXCEPTION("TO DO!!!");
358 }
359 
360 /*---------------------------------------------------------------
361  assureSymmetry
362  ---------------------------------------------------------------*/
364 {
365  // Differences, when they exist, appear in the ~15'th significant
366  // digit, so... just take one of them arbitrarily!
367  for (unsigned int i = 0; i < size(cov_inv, 1) - 1; i++)
368  for (unsigned int j = i + 1; j < size(cov_inv, 1); j++)
369  cov_inv.get_unsafe(i, j) = cov_inv.get_unsafe(j, i);
370 }
371 
372 /*---------------------------------------------------------------
373  mahalanobisDistanceTo
374  ---------------------------------------------------------------*/
376  const CPose3DPDFGaussianInf& theOther)
377 {
378  MRPT_START
379 
380  const CMatrixDouble66 cov = this->cov_inv.inv();
381  const CMatrixDouble66 cov2 = theOther.cov_inv.inv();
382 
383  CMatrixDouble66 COV_ = cov + cov2;
385 
386  for (int i = 0; i < 6; i++)
387  {
388  if (COV_.get_unsafe(i, i) == 0)
389  {
390  if (MU.get_unsafe(i, 0) != 0)
391  return std::numeric_limits<double>::infinity();
392  else
393  COV_.get_unsafe(i, i) = 1; // Any arbitrary value since
394  // MU(i)=0, and this value doesn't
395  // affect the result.
396  }
397  }
398 
399  return std::sqrt(MU.multiply_HtCH_scalar(COV_.inv()));
400 
401  MRPT_END
402 }
403 
404 /*---------------------------------------------------------------
405  operator <<
406  ---------------------------------------------------------------*/
407 ostream& mrpt::poses::operator<<(ostream& out, const CPose3DPDFGaussianInf& obj)
408 {
409  out << "Mean: " << obj.mean << "\n";
410  out << "Inverse cov:\n" << obj.cov_inv << "\n";
411 
412  return out;
413 }
414 
415 /*---------------------------------------------------------------
416  getInvCovSubmatrix2D
417  ---------------------------------------------------------------*/
419 {
420  out_cov.setSize(3, 3);
421 
422  for (int i = 0; i < 3; i++)
423  {
424  int a = i == 2 ? 3 : i;
425  for (int j = i; j < 3; j++)
426  {
427  int b = j == 2 ? 3 : j;
428  double f = cov_inv.get_unsafe(a, b);
429  out_cov.set_unsafe(i, j, f);
430  out_cov.set_unsafe(j, i, f);
431  }
432  }
433 }
434 
436  const CPose3DPDFGaussianInf& p1, const CPose3DPDFGaussianInf& p2)
437 {
438  return p1.mean == p2.mean && p1.cov_inv == p2.cov_inv;
439 }
440 
443 {
445  res += u;
446  return res;
447 }
448 
451 {
453  res -= u;
454  return res;
455 }
A namespace of pseudo-random numbers genrators of diferent distributions.
double evaluatePDF(const CPose3D &x) const
Evaluates the PDF at a given point.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
CPose3D mean
The mean value.
void inverse(CPose3DPDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
#define MRPT_END_WITH_CLEAN_UP(stuff)
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.
void getInvCovSubmatrix2D(mrpt::math::CMatrixDouble &out_cov) const
Returns a 3x3 matrix with submatrix of the inverse covariance for the variables (x,y,yaw) only.
mrpt::math::TPoint2D operator+(const CPose2D &pose, const mrpt::math::TPoint2D &pnt)
Compose a 2D point from a new coordinate base given by a 2D pose.
Definition: CPose2D.cpp:377
static void jacobiansPoseComposition(const CPose3D &x, const CPose3D &u, mrpt::math::CMatrixDouble66 &df_dx, mrpt::math::CMatrixDouble66 &df_du)
This static method computes the pose composition Jacobians.
Definition: CPose3DPDF.cpp:141
#define THROW_EXCEPTION(msg)
Scalar * iterator
Definition: eigen_plugins.h:26
void saveToTextFile(const std::string &file) const override
Save the PDF to a text file, containing the 3D pose in the first line, then the covariance matrix in ...
mrpt::math::CMatrixDouble66 cov_inv
The inverse of the 6x6 covariance matrix.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:539
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:533
STL namespace.
CPose3DPDFGaussianInf()
Default constructor - mean: all zeros, inverse covariance=all zeros -> so be careful! ...
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
double mahalanobisDistanceTo(const CPose3DPDFGaussianInf &theOther)
Computes the Mahalanobis distance between the centers of two Gaussians.
A numeric matrix of compile-time fixed size.
void bayesianFusion(const CPose3DPDF &p1, const CPose3DPDF &p2) override
Bayesian fusion of two points gauss.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
#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
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void drawGaussianMultivariateMany(VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean=nullptr)
Generate a given number of multidimensional random samples according to a given covariance matrix...
CPose2D operator-(const CPose2D &p)
Unary - operator: return the inverse pose "-p" (Note that is NOT the same than a pose with negative x...
Definition: CPose2D.cpp:328
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...
GLubyte GLubyte b
Definition: glext.h:6279
void copyFrom(const CPose3DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:53
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:41
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:545
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
double evaluateNormalizedPDF(const CPose3D &x) const
Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
void operator+=(const CPose3D &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
void operator-=(const CPose3DPDFGaussianInf &Ap)
Makes: thisPDF = thisPDF - Ap, where "-" is pose inverse composition (both the mean, and the covariance matrix are updated)
#define MRPT_START
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A Probability Density function (PDF) of a 2D pose as a Gaussian with a mean and the inverse of the c...
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:163
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void assureSymmetry()
Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:103
void setFromValues(const double x0, const double y0, const double z0, const double yaw=0, const double pitch=0, const double roll=0)
Set the pose from a 3D position (meters) and yaw/pitch/roll angles (radians) - This method recomputes...
Definition: CPose3D.cpp:248
Declares a class that represents a Probability Density function (PDF) of a 3D pose as a Gaussian des...
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
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 1x6 vectors, where each row contains a (x,y,phi) datum.
void drawSingleSample(CPose3D &outPart) const override
Draws a single sample from the distribution.
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
GLsizeiptr size
Definition: glext.h:3923
GLuint res
Definition: glext.h:7268
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
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
Definition: CPose3DPDF.h:42
mrpt::math::CMatrixDouble33 cov_inv
The inverse of the 3x3 covariance matrix (the "information" matrix)
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
CMatrixFixedNumeric< double, 6, 1 > CMatrixDouble61
Definition: eigen_frwds.h:63
std::ostream & operator<<(std::ostream &o, const CPoint< DERIVEDCLASS > &p)
Dumps a point as a string [x,y] or [x,y,z].
Definition: CPoint.h:137



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