MRPT  1.9.9
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  TConstructorFlags_Poses constructor_dummy_param)
44 {
45  MRPT_UNUSED_PARAM(constructor_dummy_param);
46 }
47 
48 /*---------------------------------------------------------------
49  Constructor
50  ---------------------------------------------------------------*/
52  const CPose3D& init_Mean, const CMatrixDouble66& init_Cov)
53  : mean(init_Mean), cov_inv(init_Cov)
54 {
55 }
56 
57 /*---------------------------------------------------------------
58  Constructor
59  ---------------------------------------------------------------*/
61  : mean(init_Mean), cov_inv()
62 {
63 }
64 
65 /*---------------------------------------------------------------
66  CPose3DPDFGaussianInf
67  ---------------------------------------------------------------*/
70 {
71  this->copyFrom(o);
72 }
73 
74 /*---------------------------------------------------------------
75  copyFrom
76  ---------------------------------------------------------------*/
78 {
79  const CPose3DPDFGaussian p(o);
80  this->copyFrom(p);
81 }
82 
83 uint8_t CPose3DPDFGaussianInf::serializeGetVersion() const { return 0; }
86 {
87  out << mean;
89 }
91  mrpt::serialization::CArchive& in, uint8_t version)
92 {
93  switch (version)
94  {
95  case 0:
96  {
97  in >> mean;
99  }
100  break;
101  default:
103  };
104 }
105 
107 {
108  if (this == &o) return; // It may be used sometimes
109 
111  { // It's my same class:
112  const auto* ptr = dynamic_cast<const CPose3DPDFGaussianInf*>(&o);
113  ASSERT_(ptr != nullptr);
114  mean = ptr->mean;
115  cov_inv = ptr->cov_inv;
116  }
117  else
118  {
119  // Convert to gaussian pdf:
122  this->cov_inv = cov.inverse_LLt();
123  }
124 }
125 
127 {
129  { // cov is already inverted, but it's a 2D pose:
130  const auto* ptr = dynamic_cast<const CPosePDFGaussianInf*>(&o);
131  ASSERT_(ptr != nullptr);
132 
133  mean = CPose3D(ptr->mean);
134 
135  // 3x3 inv_cov -> 6x6 inv_cov
136  cov_inv.setZero();
137  cov_inv(0, 0) = ptr->cov_inv(0, 0);
138  cov_inv(1, 1) = ptr->cov_inv(1, 1);
139  cov_inv(3, 3) = ptr->cov_inv(2, 2);
140 
141  cov_inv(0, 1) = cov_inv(1, 0) = ptr->cov_inv(0, 1);
142  cov_inv(0, 3) = cov_inv(3, 0) = ptr->cov_inv(0, 2);
143  cov_inv(1, 3) = cov_inv(3, 1) = ptr->cov_inv(1, 2);
144  }
145  else
146  {
148  p.copyFrom(o);
149  this->copyFrom(p);
150  }
151 }
152 
153 /*---------------------------------------------------------------
154 
155  ---------------------------------------------------------------*/
156 bool CPose3DPDFGaussianInf::saveToTextFile(const string& file) const
157 {
158  FILE* f = os::fopen(file.c_str(), "wt");
159  if (!f) return false;
160 
161  os::fprintf(
162  f, "%e %e %e %e %e %e\n", mean.x(), mean.y(), mean.z(), mean.yaw(),
163  mean.pitch(), mean.roll());
164 
165  for (unsigned int i = 0; i < 6; i++)
166  os::fprintf(
167  f, "%e %e %e %e %e %e\n", cov_inv(i, 0), cov_inv(i, 1),
168  cov_inv(i, 2), cov_inv(i, 3), cov_inv(i, 4), cov_inv(i, 5));
169 
170  os::fclose(f);
171  return true;
172 }
173 
174 /*---------------------------------------------------------------
175  changeCoordinatesReference
176  ---------------------------------------------------------------*/
178  const CPose3D& newReferenceBase)
179 {
180  MRPT_START
181 
183  a.copyFrom(*this);
184  a.changeCoordinatesReference(newReferenceBase);
185  this->copyFrom(a);
186 
187  MRPT_END
188 }
189 
190 /*---------------------------------------------------------------
191  drawSingleSample
192  ---------------------------------------------------------------*/
194 {
195  MRPT_UNUSED_PARAM(outPart);
196  MRPT_START
197 
199 
200  CVectorDouble v;
202 
203  outPart.setFromValues(
204  mean.x() + v[0], mean.y() + v[1], mean.z() + v[2], mean.yaw() + v[3],
205  mean.pitch() + v[4], mean.roll() + v[5]);
206 
208  "__DEBUG_EXC_DUMP_drawSingleSample_COV_INV.txt"););
209 }
210 
211 /*---------------------------------------------------------------
212  drawManySamples
213  ---------------------------------------------------------------*/
215  size_t N, vector<CVectorDouble>& outSamples) const
216 {
217  MRPT_START
218 
220 
222 
223  for (auto& outSample : outSamples)
224  {
225  outSample[0] += mean.x();
226  outSample[1] += mean.y();
227  outSample[2] += mean.z();
228  outSample[3] = math::wrapToPi(outSample[3] + mean.yaw());
229  outSample[4] = math::wrapToPi(outSample[4] + mean.pitch());
230  outSample[5] = math::wrapToPi(outSample[5] + mean.roll());
231  }
232 
233  MRPT_END
234 }
235 
236 /*---------------------------------------------------------------
237  bayesianFusion
238  ---------------------------------------------------------------*/
240  const CPose3DPDF& p1_, const CPose3DPDF& p2_)
241 {
242  MRPT_UNUSED_PARAM(p1_);
243  MRPT_UNUSED_PARAM(p2_);
244 
245  THROW_EXCEPTION("TO DO!!!");
246 }
247 
248 /*---------------------------------------------------------------
249  inverse
250  ---------------------------------------------------------------*/
252 {
254  auto& out = dynamic_cast<CPose3DPDFGaussianInf&>(o);
255 
256  // This is like: b=(0,0,0)
257  // OUT = b - THIS
258  CPose3DPDFGaussianInf b; // Init: all zeros.
259  out = b - *this;
260 }
261 
262 /*---------------------------------------------------------------
263  +=
264  ---------------------------------------------------------------*/
266 {
267  // COV:
268  const CMatrixDouble66 OLD_COV_INV = this->cov_inv;
270 
272  this->mean, // x
273  Ap, // u
274  df_dx, df_du);
275 
276  // this->cov = H1*this->cov*H1' + H2*Ap.cov*H2';
277  // cov_inv = ... => The same than above!
278  cov_inv = mrpt::math::multiply_HCHt(df_dx, OLD_COV_INV);
279  // df_du: Nothing to do, since COV(Ap) = zeros
280 
281  // MEAN:
282  this->mean = this->mean + Ap;
283 }
284 
285 /*---------------------------------------------------------------
286  +=
287  ---------------------------------------------------------------*/
289 {
292  a.copyFrom(*this);
293  b.copyFrom(Ap);
294 
295  a += b;
296 
297  this->mean = a.mean;
298  cov_inv = a.cov.inverse_LLt();
299 }
300 
301 /*---------------------------------------------------------------
302  -=
303  ---------------------------------------------------------------*/
305 {
308  a.copyFrom(*this);
309  b.copyFrom(Ap);
310 
311  a -= b;
312 
313  this->mean = a.mean;
314  cov_inv = a.cov.inverse_LLt();
315 }
316 
317 /*---------------------------------------------------------------
318  evaluatePDF
319  ---------------------------------------------------------------*/
321 {
323  THROW_EXCEPTION("TO DO!!!");
324 }
325 
326 /*---------------------------------------------------------------
327  evaluateNormalizedPDF
328  ---------------------------------------------------------------*/
330 {
332  THROW_EXCEPTION("TO DO!!!");
333 }
334 
335 /*---------------------------------------------------------------
336  enforceCovSymmetry
337  ---------------------------------------------------------------*/
339 {
340  // Differences, when they exist, appear in the ~15'th significant
341  // digit, so... just take one of them arbitrarily!
342  for (int i = 0; i < cov_inv.rows() - 1; i++)
343  for (int j = i + 1; j < cov_inv.rows(); j++)
344  cov_inv(i, j) = cov_inv(j, i);
345 }
346 
347 /*---------------------------------------------------------------
348  mahalanobisDistanceTo
349  ---------------------------------------------------------------*/
351  const CPose3DPDFGaussianInf& theOther)
352 {
353  MRPT_START
354 
355  const CMatrixDouble66 cov = this->cov_inv.inverse_LLt();
356  const CMatrixDouble66 cov2 = theOther.cov_inv.inverse_LLt();
357 
358  CMatrixDouble66 COV_ = cov + cov2;
360 
361  for (int i = 0; i < 6; i++)
362  {
363  if (COV_(i, i) == 0)
364  {
365  if (MU(i, 0) != 0)
366  return std::numeric_limits<double>::infinity();
367  else
368  COV_(i, i) = 1; // Any arbitrary value since
369  // MU(i)=0, and this value doesn't
370  // affect the result.
371  }
372  }
373 
374  return std::sqrt(mrpt::math::multiply_HtCH_scalar(MU, COV_.inverse_LLt()));
375 
376  MRPT_END
377 }
378 
379 /*---------------------------------------------------------------
380  operator <<
381  ---------------------------------------------------------------*/
382 ostream& mrpt::poses::operator<<(ostream& out, const CPose3DPDFGaussianInf& obj)
383 {
384  out << "Mean: " << obj.mean << "\n";
385  out << "Inverse cov:\n" << obj.cov_inv << "\n";
386 
387  return out;
388 }
389 
390 /*---------------------------------------------------------------
391  getInvCovSubmatrix2D
392  ---------------------------------------------------------------*/
394 {
395  out_cov.setSize(3, 3);
396 
397  for (int i = 0; i < 3; i++)
398  {
399  int a = i == 2 ? 3 : i;
400  for (int j = i; j < 3; j++)
401  {
402  int b = j == 2 ? 3 : j;
403  double f = cov_inv(a, b);
404  out_cov(i, j) = f;
405  out_cov(j, i) = f;
406  }
407  }
408 }
409 
411  const CPose3DPDFGaussianInf& p1, const CPose3DPDFGaussianInf& p2)
412 {
413  return p1.mean == p2.mean && p1.cov_inv == p2.cov_inv;
414 }
415 
417  const CPose3DPDFGaussianInf& x, const CPose3DPDFGaussianInf& u)
418 {
419  CPose3DPDFGaussianInf res(x);
420  res += u;
421  return res;
422 }
423 
425  const CPose3DPDFGaussianInf& x, const CPose3DPDFGaussianInf& u)
426 {
427  CPose3DPDFGaussianInf res(x);
428  res -= u;
429  return res;
430 }
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:410
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
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:265
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
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
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 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020