MRPT  1.9.9
CPose3DQuatPDFGaussian.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 
15 #include <mrpt/poses/CPose3D.h>
17 #include <mrpt/poses/CPose3DQuat.h>
21 #include <mrpt/system/os.h>
22 #include <Eigen/Dense>
23 
24 using namespace mrpt;
25 using namespace mrpt::poses;
26 using namespace mrpt::math;
27 using namespace mrpt::random;
28 using namespace mrpt::system;
29 using namespace std;
30 
32 
34 {
36 }
38 {
40 }
41 
43 
44 /** Default constructor - set all values to zero. */
46 // Un-initialized constructor:
48  [maybe_unused]] TConstructorFlags_Quaternions constructor_dummy_param)
50 {
51 }
52 
53 /** Constructor from a default mean value, covariance equals to zero. */
55  : mean(init_Mean), cov()
56 {
57 }
58 
59 /** Constructor with mean and covariance. */
61  const CPose3DQuat& init_Mean, const CMatrixDouble77& init_Cov)
62  : mean(init_Mean), cov(init_Cov)
63 {
64 }
65 
66 /** Constructor from a Gaussian 2D pose PDF (sets to 0 the missing variables).
67  */
70 {
71  this->copyFrom(CPose3DPDFGaussian(o));
72 }
73 
74 /** Constructor from an equivalent Gaussian in Euler angles representation. */
77 {
78  this->copyFrom(o);
79 }
80 
82 
83 uint8_t CPose3DQuatPDFGaussian::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 
110  // Convert to gaussian pdf:
112 }
113 
115 {
116  CPose3DPDFGaussian aux;
117  aux.copyFrom(o);
118  this->copyFrom(aux);
119 }
120 
122  const CVectorFixedDouble<6>& x, [[maybe_unused]] const double& dummy,
124 {
125  y[0] = x[0];
126  y[1] = x[1];
127  y[2] = x[2];
128 
129  CPose3D p(0, 0, 0, x[3], x[4], x[5]);
131  p.getAsQuaternion(q);
132  y[3] = q[0];
133  y[4] = q[1];
134  y[5] = q[2];
135  y[6] = q[3];
136 }
137 
139 {
141  { // Use Jacobians
143 
144  // Mean:
145  mean.x(o.mean.x());
146  mean.y(o.mean.y());
147  mean.z(o.mean.z());
148 
149  o.mean.getAsQuaternion(mean.quat(), dq_dr_sub);
150 
151  // Cov:
153  dq_dr(0, 0) = dq_dr(1, 1) = dq_dr(2, 2) = 1;
154  dq_dr.insertMatrix(3, 3, dq_dr_sub);
155  // Now for the covariance:
156  this->cov = mrpt::math::multiply_HCHt(dq_dr, o.cov);
157  }
158  else
159  {
160  // Use UT transformation:
161  // f: R^6 => R^7
162  const CVectorFixedDouble<6> x_mean(o.mean);
163 
164  const double dummy = 0;
166  x_mean, o.cov, &aux_poseypr2posequat, dummy, this->mean, this->cov);
167  }
168 }
169 
170 /*---------------------------------------------------------------
171  saveToTextFile
172  ---------------------------------------------------------------*/
173 bool CPose3DQuatPDFGaussian::saveToTextFile(const string& file) const
174 {
175  FILE* f = os::fopen(file.c_str(), "wt");
176  if (!f) return false;
177 
178  os::fprintf(
179  f, "%e %e %e %e %e %e %e\n", mean.x(), mean.y(), mean.z(),
180  mean.quat()[0], mean.quat()[1], mean.quat()[2], mean.quat()[3]);
181 
182  for (unsigned int i = 0; i < 7; i++)
183  os::fprintf(
184  f, "%e %e %e %e %e %e %e\n", cov(i, 0), cov(i, 1), cov(i, 2),
185  cov(i, 3), cov(i, 4), cov(i, 5), cov(i, 6));
186 
187  os::fclose(f);
188  return true;
189 }
190 
191 /*---------------------------------------------------------------
192  changeCoordinatesReference
193  ---------------------------------------------------------------*/
195  const CPose3D& newReferenceBase)
196 {
197  MRPT_START
198  changeCoordinatesReference(CPose3DQuat(newReferenceBase));
199  MRPT_END
200 }
201 
202 /*---------------------------------------------------------------
203  changeCoordinatesReference
204  ---------------------------------------------------------------*/
206  const CPose3DQuat& newReferenceBaseQuat)
207 {
208  MRPT_START
209 
210  // COV:
211  const CMatrixDouble77 OLD_COV = this->cov;
213 
215  newReferenceBaseQuat, // x
216  this->mean, // u
217  df_dx, df_du,
218  &this->mean // Output: newReferenceBaseQuat + this->mean;
219  );
220 
221  // this->cov = H1*this->cov*H1' + H2*Ap.cov*H2';
222  // df_dx: not used, since its COV are all zeros...
223  cov = mrpt::math::multiply_HCHt(df_du, OLD_COV);
224 
225  MRPT_END
226 }
227 
228 /*---------------------------------------------------------------
229  drawSingleSample
230  ---------------------------------------------------------------*/
232 {
233  MRPT_START
235  MRPT_END
236 }
237 
238 /*---------------------------------------------------------------
239  drawManySamples
240  ---------------------------------------------------------------*/
242  size_t N, vector<CVectorDouble>& outSamples) const
243 {
244  MRPT_START
245 
247 
248  for (auto& outSample : outSamples)
249  for (unsigned int k = 0; k < 7; k++) outSample[k] += mean[k];
250 
251  MRPT_END
252 }
253 
254 /*---------------------------------------------------------------
255  inverse
256  ---------------------------------------------------------------*/
258 {
260  auto& out = dynamic_cast<CPose3DQuatPDFGaussian&>(o);
261 
262  // COV:
264  double lx, ly, lz;
265  mean.inverseComposePoint(0, 0, 0, lx, ly, lz, nullptr, &df_dpose);
266 
268  jacob.insertMatrix(0, 0, df_dpose);
269  jacob(3, 3) = 1;
270  jacob(4, 4) = -1;
271  jacob(5, 5) = -1;
272  jacob(6, 6) = -1;
273 
274  // C(0:2,0:2): H C H^t
275  out.cov = mrpt::math::multiply_HCHt(jacob, this->cov);
276 
277  // Mean:
278  out.mean.x(lx);
279  out.mean.y(ly);
280  out.mean.z(lz);
281  this->mean.quat().conj(out.mean.quat());
282 }
283 
284 /*---------------------------------------------------------------
285  +=
286  ---------------------------------------------------------------*/
288 {
289  // COV:
290  const CMatrixDouble77 OLD_COV = this->cov;
292 
294  this->mean, // x
295  Ap, // u
296  df_dx, df_du,
297  &this->mean // Output: this->mean + Ap;
298  );
299 
300  // this->cov = H1*this->cov*H1' + H2*Ap.cov*H2';
301  cov = mrpt::math::multiply_HCHt(df_dx, OLD_COV);
302  // df_du: Nothing to do, since COV(Ap) = zeros
303 }
304 
305 /*---------------------------------------------------------------
306  +=
307  ---------------------------------------------------------------*/
309 {
310  // COV:
311  const CMatrixDouble77 OLD_COV = this->cov;
313 
315  this->mean, // x
316  Ap.mean, // u
317  df_dx, df_du,
318  &this->mean // Output: this->mean + Ap.mean;
319  );
320 
321  // this->cov = H1*this->cov*H1' + H2*Ap.cov*H2';
322  cov = mrpt::math::multiply_HCHt(df_dx, OLD_COV);
323  cov += mrpt::math::multiply_HCHt(df_du, Ap.cov);
324 }
325 
326 /*---------------------------------------------------------------
327  -=
328  ---------------------------------------------------------------*/
330 {
331  // THIS = THIS (-) Ap -->
332  // THIS = inverse(Ap) (+) THIS
333  CPose3DQuatPDFGaussian inv_Ap = -Ap;
334  *this = inv_Ap + *this;
335 }
336 
337 /*---------------------------------------------------------------
338  evaluatePDF
339  ---------------------------------------------------------------*/
341 {
342  return mrpt::math::normalPDF(
343  CMatrixDouble71(x), CMatrixDouble71(this->mean), this->cov);
344 }
345 
346 /*---------------------------------------------------------------
347  evaluateNormalizedPDF
348  ---------------------------------------------------------------*/
350 {
351  return mrpt::math::normalPDF(
352  CMatrixDouble71(x), CMatrixDouble71(this->mean), this->cov, true);
353 }
354 
355 /*---------------------------------------------------------------
356  enforceCovSymmetry
357  ---------------------------------------------------------------*/
359 {
360  // Differences, when they exist, appear in the ~15'th significant
361  // digit, so... just take one of them arbitrarily!
362  for (int i = 0; i < cov.rows() - 1; i++)
363  for (int j = i + 1; j < cov.rows(); j++) cov(i, j) = cov(j, i);
364 }
365 
366 /*---------------------------------------------------------------
367  mahalanobisDistanceTo
368  ---------------------------------------------------------------*/
370  const CPose3DQuatPDFGaussian& theOther)
371 {
372  MRPT_START
373  const CMatrixDouble77 COV2 = cov + theOther.cov;
375  CMatrixDouble71(this->mean) - CMatrixDouble71(theOther.mean), COV2);
376  MRPT_END
377 }
378 
379 /*---------------------------------------------------------------
380  operator <<
381  ---------------------------------------------------------------*/
383  ostream& out, const CPose3DQuatPDFGaussian& obj)
384 {
385  out << "Mean: " << obj.mean << "\n";
386  out << "Covariance:\n" << obj.cov << "\n";
387  return out;
388 }
389 
391  const CPose3DQuatPDFGaussian& p1, const CPose3DQuatPDFGaussian& p2)
392 {
393  return p1.mean == p2.mean && p1.cov == p2.cov;
394 }
395 
398 {
399  CPose3DQuatPDFGaussian res(x);
400  res += u;
401  return res;
402 }
403 
406 {
407  CPose3DQuatPDFGaussian res(x);
408  res -= u;
409  return res;
410 }
A namespace of pseudo-random numbers generators of diferent distributions.
void changeCoordinatesReference(const CPose3DQuat &newReferenceBase)
this = p (+) this.
mrpt::math::CQuaternionDouble & quat()
Read/Write access to the quaternion representing the 3D rotation.
Definition: CPose3DQuat.h:58
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 1x7 vectors, where each row contains a (x,y,z,qr,qx,qy,qz) datum.
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.
CMatrixFixed< double, 7, 1 > CMatrixDouble71
Definition: CMatrixFixed.h:379
void copyFrom(const CPose3DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
TConstructorFlags_Quaternions
Definition: CQuaternion.h:20
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.
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
std::ostream & operator<<(std::ostream &o, const CPoint2D &p)
Dumps a point as a string (x,y)
Definition: CPoint2D.cpp:102
double mahalanobisDistanceTo(const CPose3DQuatPDFGaussian &theOther)
Computes the Mahalanobis distance between the centers of two Gaussians.
void aux_poseypr2posequat(const CVectorFixedDouble< 6 > &x, [[maybe_unused]] const double &dummy, CVectorFixedDouble< 7 > &y)
void insertMatrix(const int row_start, const int col_start, const OTHERMATVEC &submat)
Copies the given input submatrix/vector into this matrix/vector, starting at the given top-left coord...
Definition: MatrixBase.h:210
mrpt::math::CMatrixDouble77 cov
The 7x7 covariance matrix.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
double evaluatePDF(const CPose3DQuat &x) const
Evaluates the PDF at a given point.
STL namespace.
void operator+=(const CPose3DQuat &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
void getAsQuaternion(mrpt::math::CQuaternionDouble &q, mrpt::optional_ref< mrpt::math::CMatrixDouble43 > out_dq_dr=std::nullopt) const
Returns the quaternion associated to the rotation of this object (NOTE: XYZ translation is ignored) ...
Definition: CPose3D.cpp:501
void USE_SUT_EULER2QUAT_CONVERSION(bool value)
If set to true (default), a Scaled Unscented Transform is used instead of a linear approximation with...
#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
double evaluateNormalizedPDF(const CPose3DQuat &x) const
Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
This base provides a set of functions for maths stuff.
void inverseComposePoint(const double gx, const double gy, const double gz, double &lx, double &ly, double &lz, mrpt::math::CMatrixFixed< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixed< double, 3, 7 > *out_jacobian_df_dpose=nullptr) const
Computes the 3D point L such as .
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:102
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
void enforceCovSymmetry()
Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
void conj(CQuaternion &q_out) const
Return the conjugate quaternion.
Definition: CQuaternion.h:412
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
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
void deserializeSymmetricMatrixFrom(MAT &m, mrpt::serialization::CArchive &in)
Binary serialization of symmetric matrices, saving the space of duplicated values.
void drawSingleSample(CPose3DQuat &outPart) const override
Draws a single sample from the distribution.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
void copyFrom(const CPose3DQuatPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
A class used to store a 3D pose as a translation (x,y,z) and a quaternion (qr,qx,qy,qz).
Definition: CPose3DQuat.h:45
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
void transform_gaussian_unscented(const VECTORLIKE1 &x_mean, const MATLIKE1 &x_cov, void(*functor)(const VECTORLIKE1 &x, const USERPARAM &fixed_param, VECTORLIKE3 &y), const USERPARAM &fixed_param, VECTORLIKE2 &y_mean, MATLIKE2 &y_cov, const bool *elem_do_wrap2pi=nullptr, const double alpha=1e-3, const double K=0, const double beta=2.0)
Scaled unscented transformation (SUT) for estimating the Gaussian distribution of a variable Y=f(X) f...
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...
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
bool saveToTextFile(const std::string &file) const override
Save the PDF to a text file, containing the 3D pose in the first line (x y z qr qx qy qz)...
mrpt::math::CMatrixDouble66 cov
The 6x6 covariance matrix.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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...
CPose3DQuatPDFGaussian()
Default constructor - set all values to zero.
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
bool USE_SUT_EULER2QUAT_CONVERSION_value
void operator-=(const CPose3DQuatPDFGaussian &Ap)
Makes: thisPDF = thisPDF - Ap, where "-" is pose inverse composition (both the mean, and the covariance matrix are updated).
This file implements matrix/vector text and binary serialization.
VECTORLIKE1::Scalar mahalanobisDistance(const VECTORLIKE1 &X, const VECTORLIKE2 &MU, const MAT &COV)
Computes the mahalanobis distance of a vector X given the mean MU and the covariance inverse COV_inv ...
Definition: data_utils.h:57
#define MRPT_END
Definition: exceptions.h:245
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 ...
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:257
void inverse(CPose3DQuatPDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
void getMean(CPose3DQuat &mean_pose) const override
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
Definition: CQuaternion.h:44
static void jacobiansPoseComposition(const CPose3DQuat &x, const CPose3DQuat &u, mrpt::math::CMatrixDouble77 &df_dx, mrpt::math::CMatrixDouble77 &df_du, CPose3DQuat *out_x_oplus_u=nullptr)
This static method computes the two Jacobians of a pose composition operation $f(x,u)= x u$.
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
double normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
Definition: math.cpp:33
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: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020