MRPT  2.0.0
CPose3DPDFGaussianInf.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "poses-precomp.h" // Precompiled headers
11 
13 #include <mrpt/math/ops_matrices.h>
14 #include <mrpt/math/wrap2pi.h>
15 #include <mrpt/poses/CPose3D.h>
20 #include <mrpt/random.h>
22 #include <mrpt/system/os.h>
23 #include <Eigen/Dense>
24 
25 using namespace mrpt;
26 using namespace mrpt::poses;
27 using namespace mrpt::math;
28 using namespace mrpt::random;
29 using namespace mrpt::system;
30 using namespace std;
31 
33 
34 /*---------------------------------------------------------------
35  Constructor
36  ---------------------------------------------------------------*/
38 /*---------------------------------------------------------------
39  Constructor
40  ---------------------------------------------------------------*/
42  [maybe_unused]] TConstructorFlags_Poses constructor_dummy_param)
44 {
45 }
46 
47 /*---------------------------------------------------------------
48  Constructor
49  ---------------------------------------------------------------*/
51  const CPose3D& init_Mean, const CMatrixDouble66& init_Cov)
52  : mean(init_Mean), cov_inv(init_Cov)
53 {
54 }
55 
56 /*---------------------------------------------------------------
57  Constructor
58  ---------------------------------------------------------------*/
60  : mean(init_Mean), cov_inv()
61 {
62 }
63 
64 /*---------------------------------------------------------------
65  CPose3DPDFGaussianInf
66  ---------------------------------------------------------------*/
69 {
70  this->copyFrom(o);
71 }
72 
73 /*---------------------------------------------------------------
74  copyFrom
75  ---------------------------------------------------------------*/
77 {
78  const CPose3DPDFGaussian p(o);
79  this->copyFrom(p);
80 }
81 
82 uint8_t CPose3DPDFGaussianInf::serializeGetVersion() const { return 0; }
85 {
86  out << mean;
88 }
90  mrpt::serialization::CArchive& in, uint8_t version)
91 {
92  switch (version)
93  {
94  case 0:
95  {
96  in >> mean;
98  }
99  break;
100  default:
102  };
103 }
104 
106 {
107  if (this == &o) return; // It may be used sometimes
108 
110  { // It's my same class:
111  const auto* ptr = dynamic_cast<const CPose3DPDFGaussianInf*>(&o);
112  ASSERT_(ptr != nullptr);
113  mean = ptr->mean;
114  cov_inv = ptr->cov_inv;
115  }
116  else
117  {
118  // Convert to gaussian pdf:
121  this->cov_inv = cov.inverse_LLt();
122  }
123 }
124 
126 {
128  { // cov is already inverted, but it's a 2D pose:
129  const auto* ptr = dynamic_cast<const CPosePDFGaussianInf*>(&o);
130  ASSERT_(ptr != nullptr);
131 
132  mean = CPose3D(ptr->mean);
133 
134  // 3x3 inv_cov -> 6x6 inv_cov
135  cov_inv.setZero();
136  cov_inv(0, 0) = ptr->cov_inv(0, 0);
137  cov_inv(1, 1) = ptr->cov_inv(1, 1);
138  cov_inv(3, 3) = ptr->cov_inv(2, 2);
139 
140  cov_inv(0, 1) = cov_inv(1, 0) = ptr->cov_inv(0, 1);
141  cov_inv(0, 3) = cov_inv(3, 0) = ptr->cov_inv(0, 2);
142  cov_inv(1, 3) = cov_inv(3, 1) = ptr->cov_inv(1, 2);
143  }
144  else
145  {
147  p.copyFrom(o);
148  this->copyFrom(p);
149  }
150 }
151 
152 /*---------------------------------------------------------------
153 
154  ---------------------------------------------------------------*/
155 bool CPose3DPDFGaussianInf::saveToTextFile(const string& file) const
156 {
157  FILE* f = os::fopen(file.c_str(), "wt");
158  if (!f) return false;
159 
160  os::fprintf(
161  f, "%e %e %e %e %e %e\n", mean.x(), mean.y(), mean.z(), mean.yaw(),
162  mean.pitch(), mean.roll());
163 
164  for (unsigned int i = 0; i < 6; i++)
165  os::fprintf(
166  f, "%e %e %e %e %e %e\n", cov_inv(i, 0), cov_inv(i, 1),
167  cov_inv(i, 2), cov_inv(i, 3), cov_inv(i, 4), cov_inv(i, 5));
168 
169  os::fclose(f);
170  return true;
171 }
172 
173 /*---------------------------------------------------------------
174  changeCoordinatesReference
175  ---------------------------------------------------------------*/
177  const CPose3D& newReferenceBase)
178 {
179  MRPT_START
180 
182  a.copyFrom(*this);
183  a.changeCoordinatesReference(newReferenceBase);
184  this->copyFrom(a);
185 
186  MRPT_END
187 }
188 
189 /*---------------------------------------------------------------
190  drawSingleSample
191  ---------------------------------------------------------------*/
193  [maybe_unused]] CPose3D& outPart) const
194 {
195  MRPT_START
196 
198 
199  CVectorDouble v;
201 
202  outPart.setFromValues(
203  mean.x() + v[0], mean.y() + v[1], mean.z() + v[2], mean.yaw() + v[3],
204  mean.pitch() + v[4], mean.roll() + v[5]);
205 
207  "__DEBUG_EXC_DUMP_drawSingleSample_COV_INV.txt"););
208 }
209 
210 /*---------------------------------------------------------------
211  drawManySamples
212  ---------------------------------------------------------------*/
214  size_t N, vector<CVectorDouble>& outSamples) const
215 {
216  MRPT_START
217 
219 
221 
222  for (auto& outSample : outSamples)
223  {
224  outSample[0] += mean.x();
225  outSample[1] += mean.y();
226  outSample[2] += mean.z();
227  outSample[3] = math::wrapToPi(outSample[3] + mean.yaw());
228  outSample[4] = math::wrapToPi(outSample[4] + mean.pitch());
229  outSample[5] = math::wrapToPi(outSample[5] + mean.roll());
230  }
231 
232  MRPT_END
233 }
234 
235 /*---------------------------------------------------------------
236  bayesianFusion
237  ---------------------------------------------------------------*/
239  [[maybe_unused]] const CPose3DPDF& p1_,
240  [[maybe_unused]] const CPose3DPDF& p2_)
241 {
242  THROW_EXCEPTION("TO DO!!!");
243 }
244 
245 /*---------------------------------------------------------------
246  inverse
247  ---------------------------------------------------------------*/
249 {
251  auto& out = dynamic_cast<CPose3DPDFGaussianInf&>(o);
252 
253  // This is like: b=(0,0,0)
254  // OUT = b - THIS
255  CPose3DPDFGaussianInf b; // Init: all zeros.
256  out = b - *this;
257 }
258 
259 /*---------------------------------------------------------------
260  +=
261  ---------------------------------------------------------------*/
263 {
264  // COV:
265  const CMatrixDouble66 OLD_COV_INV = this->cov_inv;
267 
269  this->mean, // x
270  Ap, // u
271  df_dx, df_du);
272 
273  // this->cov = H1*this->cov*H1' + H2*Ap.cov*H2';
274  // cov_inv = ... => The same than above!
275  cov_inv = mrpt::math::multiply_HCHt(df_dx, OLD_COV_INV);
276  // df_du: Nothing to do, since COV(Ap) = zeros
277 
278  // MEAN:
279  this->mean = this->mean + Ap;
280 }
281 
282 /*---------------------------------------------------------------
283  +=
284  ---------------------------------------------------------------*/
286 {
289  a.copyFrom(*this);
290  b.copyFrom(Ap);
291 
292  a += b;
293 
294  this->mean = a.mean;
295  cov_inv = a.cov.inverse_LLt();
296 }
297 
298 /*---------------------------------------------------------------
299  -=
300  ---------------------------------------------------------------*/
302 {
305  a.copyFrom(*this);
306  b.copyFrom(Ap);
307 
308  a -= b;
309 
310  this->mean = a.mean;
311  cov_inv = a.cov.inverse_LLt();
312 }
313 
314 /*---------------------------------------------------------------
315  evaluatePDF
316  ---------------------------------------------------------------*/
318  [maybe_unused]] const CPose3D& x) const
319 {
320  THROW_EXCEPTION("TO DO!!!");
321 }
322 
323 /*---------------------------------------------------------------
324  evaluateNormalizedPDF
325  ---------------------------------------------------------------*/
327  [maybe_unused]] const CPose3D& x) const
328 {
329  THROW_EXCEPTION("TO DO!!!");
330 }
331 
332 /*---------------------------------------------------------------
333  enforceCovSymmetry
334  ---------------------------------------------------------------*/
336 {
337  // Differences, when they exist, appear in the ~15'th significant
338  // digit, so... just take one of them arbitrarily!
339  for (int i = 0; i < cov_inv.rows() - 1; i++)
340  for (int j = i + 1; j < cov_inv.rows(); j++)
341  cov_inv(i, j) = cov_inv(j, i);
342 }
343 
344 /*---------------------------------------------------------------
345  mahalanobisDistanceTo
346  ---------------------------------------------------------------*/
348  const CPose3DPDFGaussianInf& theOther)
349 {
350  MRPT_START
351 
352  const CMatrixDouble66 cov = this->cov_inv.inverse_LLt();
353  const CMatrixDouble66 cov2 = theOther.cov_inv.inverse_LLt();
354 
355  CMatrixDouble66 COV_ = cov + cov2;
357 
358  for (int i = 0; i < 6; i++)
359  {
360  if (COV_(i, i) == 0)
361  {
362  if (MU(i, 0) != 0)
363  return std::numeric_limits<double>::infinity();
364  else
365  COV_(i, i) = 1; // Any arbitrary value since
366  // MU(i)=0, and this value doesn't
367  // affect the result.
368  }
369  }
370 
371  return std::sqrt(mrpt::math::multiply_HtCH_scalar(MU, COV_.inverse_LLt()));
372 
373  MRPT_END
374 }
375 
376 /*---------------------------------------------------------------
377  operator <<
378  ---------------------------------------------------------------*/
379 ostream& mrpt::poses::operator<<(ostream& out, const CPose3DPDFGaussianInf& obj)
380 {
381  out << "Mean: " << obj.mean << "\n";
382  out << "Inverse cov:\n" << obj.cov_inv << "\n";
383 
384  return out;
385 }
386 
387 /*---------------------------------------------------------------
388  getInvCovSubmatrix2D
389  ---------------------------------------------------------------*/
391 {
392  out_cov.setSize(3, 3);
393 
394  for (int i = 0; i < 3; i++)
395  {
396  int a = i == 2 ? 3 : i;
397  for (int j = i; j < 3; j++)
398  {
399  int b = j == 2 ? 3 : j;
400  double f = cov_inv(a, b);
401  out_cov(i, j) = f;
402  out_cov(j, i) = f;
403  }
404  }
405 }
406 
408  const CPose3DPDFGaussianInf& p1, const CPose3DPDFGaussianInf& p2)
409 {
410  return p1.mean == p2.mean && p1.cov_inv == p2.cov_inv;
411 }
412 
414  const CPose3DPDFGaussianInf& x, const CPose3DPDFGaussianInf& u)
415 {
416  CPose3DPDFGaussianInf res(x);
417  res += u;
418  return res;
419 }
420 
422  const CPose3DPDFGaussianInf& x, const CPose3DPDFGaussianInf& u)
423 {
424  CPose3DPDFGaussianInf res(x);
425  res -= u;
426  return res;
427 }
A namespace of pseudo-random numbers generators of diferent distributions.
double evaluatePDF(const CPose3D &x) const
Evaluates the PDF at a given point.
MAT_C::Scalar multiply_HtCH_scalar(const VECTOR_H &H, const MAT_C &C)
r (scalar) = H^t*C*H (H: column vector, C: symmetric matrix)
Definition: ops_matrices.h:54
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
void serializeSymmetricMatrixTo(MAT &m, mrpt::serialization::CArchive &out)
Binary serialization of symmetric matrices, saving the space of duplicated values.
#define MRPT_START
Definition: exceptions.h:241
CPose3D mean
The mean value.
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
void inverse(CPose3DPDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
void copyFrom(const CPose3DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:275
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void enforceCovSymmetry()
Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
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:394
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:129
std::ostream & operator<<(std::ostream &o, const CPoint2D &p)
Dumps a point as a string (x,y)
Definition: CPoint2D.cpp:102
mrpt::math::CMatrixDouble66 cov_inv
The inverse of the 6x6 covariance matrix.
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:552
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:546
This file implements miscelaneous matrix and matrix/vector operations, and internal functions in mrpt...
STL namespace.
CPose3DPDFGaussianInf()
Default constructor - mean: all zeros, inverse covariance=all zeros -> so be careful! ...
#define MRPT_END_WITH_CLEAN_UP(stuff)
Definition: exceptions.h:247
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
Saves the vector/matrix to a file compatible with MATLAB/Octave text format.
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
double mahalanobisDistanceTo(const CPose3DPDFGaussianInf &theOther)
Computes the Mahalanobis distance between the centers of two Gaussians.
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.
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:102
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:356
CMatrixFixed< double, 6, 1 > CMatrixDouble61
Definition: CMatrixFixed.h:377
void deserializeSymmetricMatrixFrom(MAT &m, mrpt::serialization::CArchive &in)
Binary serialization of symmetric matrices, saving the space of duplicated values.
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
Definition: CObject.h:146
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
void copyFrom(const CPose3DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
Derived inverse_LLt() const
Returns the inverse of a symmetric matrix using LLt.
CMatrixDouble 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:149
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:50
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:38
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:558
constexpr size_type rows() const
Number of rows in the matrix.
Definition: CMatrixFixed.h:227
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:408
double evaluateNormalizedPDF(const CPose3D &x) const
Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
mrpt::math::CMatrixDouble66 cov
The 6x6 covariance matrix.
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)
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...
bool operator==(const CPoint< DERIVEDCLASS, DIM > &p1, const CPoint< DERIVEDCLASS, DIM > &p2)
Definition: CPoint.h:119
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
void drawGaussianMultivariate(std::vector< T > &out_result, const MATRIX &cov, const std::vector< T > *mean=nullptr)
Generate multidimensional random samples according to a given covariance matrix.
virtual std::tuple< cov_mat_t, type_value > getCovarianceAndMean() const =0
Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
This file implements matrix/vector text and binary serialization.
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
#define MRPT_END
Definition: exceptions.h:245
Declares a class that represents a Probability Density function (PDF) of a 3D pose as a Gaussian des...
double mean(const CONTAINER &v)
Computes the mean value of a vector.
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:257
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
bool 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 ...
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
Definition: CPose3DPDF.h:39
void multiply_HCHt(const MAT_H &H, const MAT_C &C, MAT_R &R, bool accumResultInOutput=false)
R = H * C * H^t.
Definition: ops_matrices.h:28



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020