Main MRPT website > C++ reference for MRPT 1.9.9
CPose3DQuatPDFGaussian.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 
16 #include "mrpt/poses/CPose3D.h" // for CPose3D
17 #include "mrpt/poses/CPose3DQuat.h" // for CPose3D...
18 #include "mrpt/poses/CPose3DQuatPDF.h" // for CPose3D...
19 #include <mrpt/system/os.h>
20 #include <mrpt/utils/CStream.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 
33 {
35 }
37 {
39 }
40 
41 
43 
44 /** Default constructor - set all values to zero. */
46 // Un-initialized constructor:
48  TConstructorFlags_Quaternions constructor_dummy_param)
50 {
51  MRPT_UNUSED_PARAM(constructor_dummy_param);
52 }
53 
54 /** Constructor from a default mean value, covariance equals to zero. */
56  : mean(init_Mean), cov()
57 {
58 }
59 
60 /** Constructor with mean and covariance. */
62  const CPose3DQuat& init_Mean, const CMatrixDouble77& init_Cov)
63  : mean(init_Mean), cov(init_Cov)
64 {
65 }
66 
67 /** Constructor from a Gaussian 2D pose PDF (sets to 0 the missing variables).
68  */
71 {
72  this->copyFrom(CPose3DPDFGaussian(o));
73 }
74 
75 /** Constructor from an equivalent Gaussian in Euler angles representation. */
78 {
79  this->copyFrom(o);
80 }
81 
82 /*---------------------------------------------------------------
83  getMean
84  Returns an estimate of the pose, (the mean, or mathematical expectation of the
85  PDF)
86  ---------------------------------------------------------------*/
88 /*---------------------------------------------------------------
89  getCovarianceAndMean
90  ---------------------------------------------------------------*/
92  CMatrixDouble77& C, CPose3DQuat& p) const
93 {
94  C = cov;
95  p = mean;
96 }
97 
98 /*---------------------------------------------------------------
99  writeToStream
100  ---------------------------------------------------------------*/
102  mrpt::utils::CStream& out, int* version) const
103 {
104  if (version)
105  *version = 0;
106  else
107  {
108  out << mean;
109 
110  for (size_t r = 0; r < size(cov, 1); r++) out << cov.get_unsafe(r, r);
111  for (size_t r = 0; r < size(cov, 1); r++)
112  for (size_t c = r + 1; c < size(cov, 2); c++)
113  out << cov.get_unsafe(r, c);
114  }
115 }
116 
117 /*---------------------------------------------------------------
118  readFromStream
119  ---------------------------------------------------------------*/
121  mrpt::utils::CStream& in, int version)
122 {
123  switch (version)
124  {
125  case 0:
126  {
127  in >> mean;
128 
129  for (size_t r = 0; r < size(cov, 1); r++)
130  in >> cov.get_unsafe(r, r);
131  for (size_t r = 0; r < size(cov, 1); r++)
132  for (size_t c = r + 1; c < size(cov, 2); c++)
133  {
134  double x;
135  in >> x;
136  cov.get_unsafe(r, c) = cov.get_unsafe(c, r) = x;
137  }
138  }
139  break;
140  default:
142  };
143 }
144 
146 {
147  if (this == &o) return; // It may be used sometimes
148 
149  // Convert to gaussian pdf:
151 }
152 
154 {
155  CPose3DPDFGaussian aux;
156  aux.copyFrom(o);
157  this->copyFrom(aux);
158 }
159 
161  const CArrayDouble<6>& x, const double& dummy, CArrayDouble<7>& y)
162 {
163  MRPT_UNUSED_PARAM(dummy);
164  y[0] = x[0];
165  y[1] = x[1];
166  y[2] = x[2];
167 
168  CPose3D p(0, 0, 0, x[3], x[4], x[5]);
170  p.getAsQuaternion(q);
171  y[3] = q[0];
172  y[4] = q[1];
173  y[5] = q[2];
174  y[6] = q[3];
175 }
176 
178 {
180  { // Use Jacobians
182 
183  // Mean:
184  mean.x(o.mean.x());
185  mean.y(o.mean.y());
186  mean.z(o.mean.z());
187 
188  o.mean.getAsQuaternion(mean.quat(), &dq_dr_sub);
189 
190 // Cov:
191 #if 1
193  dq_dr.get_unsafe(0, 0) = dq_dr.get_unsafe(1, 1) =
194  dq_dr.get_unsafe(2, 2) = 1;
195  dq_dr.insertMatrix(3, 3, dq_dr_sub);
196  // Now for the covariance:
197  dq_dr.multiply_HCHt(o.cov, this->cov);
198 #else
202  o.cov.extractMatrix(3, 3, cov_R);
203  o.cov.extractMatrix(0, 0, cov_T);
204  o.cov.extractMatrix(0, 3, cov_TR);
205 
206  // [ S_T | S_TR * H^t ]
207  // [ -----------------+---------------- ]
208  // [ (S_TR * H^t)^t | H * S_R * H^t ]
209 
210  // top-left:
211  this->cov.insertMatrix(0, 0, cov_T);
212 
213  // diagonals:
215  cov_TQ.multiply_ABt(cov_TR, dq_dr_sub);
216  this->cov.insertMatrix(0, 3, cov_TQ);
217  this->cov.insertMatrixTranspose(3, 0, cov_TQ);
218 
219  // bottom-right:
221  dq_dr_sub.multiply_HCHt(cov_R, cov_q);
222  this->cov.insertMatrix(3, 3, cov_q);
223 #endif
224  }
225  else
226  {
227  // Use UT transformation:
228  // f: R^6 => R^7
229  const CArrayDouble<6> x_mean(o.mean);
230 
231  static const double dummy = 0;
233  x_mean, o.cov, &aux_poseypr2posequat, dummy, this->mean, this->cov);
234  }
235 }
236 
237 /*---------------------------------------------------------------
238  saveToTextFile
239  ---------------------------------------------------------------*/
240 void CPose3DQuatPDFGaussian::saveToTextFile(const string& file) const
241 {
242  FILE* f = os::fopen(file.c_str(), "wt");
243  if (!f) return;
244 
245  os::fprintf(
246  f, "%e %e %e %e %e %e %e\n", mean.x(), mean.y(), mean.z(),
247  mean.quat()[0], mean.quat()[1], mean.quat()[2], mean.quat()[3]);
248 
249  for (unsigned int i = 0; i < 7; i++)
250  os::fprintf(
251  f, "%e %e %e %e %e %e %e\n", cov(i, 0), cov(i, 1), cov(i, 2),
252  cov(i, 3), cov(i, 4), cov(i, 5), cov(i, 6));
253 
254  os::fclose(f);
255 }
256 
257 /*---------------------------------------------------------------
258  changeCoordinatesReference
259  ---------------------------------------------------------------*/
261  const CPose3D& newReferenceBase)
262 {
263  MRPT_START
264  changeCoordinatesReference(CPose3DQuat(newReferenceBase));
265  MRPT_END
266 }
267 
268 /*---------------------------------------------------------------
269  changeCoordinatesReference
270  ---------------------------------------------------------------*/
272  const CPose3DQuat& newReferenceBaseQuat)
273 {
274  MRPT_START
275 
276  // COV:
277  const CMatrixDouble77 OLD_COV = this->cov;
279 
281  newReferenceBaseQuat, // x
282  this->mean, // u
283  df_dx, df_du,
284  &this->mean // Output: newReferenceBaseQuat + this->mean;
285  );
286 
287  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
288  // df_dx: not used, since its COV are all zeros... // df_dx.multiply_HCHt(
289  // OLD_COV, cov );
290  df_du.multiply_HCHt(OLD_COV, cov);
291 
292  MRPT_END
293 }
294 
295 /*---------------------------------------------------------------
296  drawSingleSample
297  ---------------------------------------------------------------*/
299 {
300  MRPT_START
302  MRPT_END
303 }
304 
305 /*---------------------------------------------------------------
306  drawManySamples
307  ---------------------------------------------------------------*/
309  size_t N, vector<CVectorDouble>& outSamples) const
310 {
311  MRPT_START
312 
314 
315  for (vector<CVectorDouble>::iterator it = outSamples.begin();
316  it != outSamples.end(); ++it)
317  for (unsigned int k = 0; k < 7; k++) (*it)[k] += mean[k];
318 
319  MRPT_END
320 }
321 
322 /*---------------------------------------------------------------
323  inverse
324  ---------------------------------------------------------------*/
326 {
328  CPose3DQuatPDFGaussian& out = static_cast<CPose3DQuatPDFGaussian&>(o);
329 
330  // COV:
332  double lx, ly, lz;
333  mean.inverseComposePoint(0, 0, 0, lx, ly, lz, nullptr, &df_dpose);
334 
336  jacob.insertMatrix(0, 0, df_dpose);
337  jacob.set_unsafe(3, 3, 1);
338  jacob.set_unsafe(4, 4, -1);
339  jacob.set_unsafe(5, 5, -1);
340  jacob.set_unsafe(6, 6, -1);
341 
342  // C(0:2,0:2): H C H^t
343  jacob.multiply_HCHt(this->cov, out.cov);
344 
345  // Mean:
346  out.mean.x(lx);
347  out.mean.y(ly);
348  out.mean.z(lz);
349  this->mean.quat().conj(out.mean.quat());
350 }
351 
352 /*---------------------------------------------------------------
353  +=
354  ---------------------------------------------------------------*/
356 {
357  // COV:
358  const CMatrixDouble77 OLD_COV = this->cov;
360 
362  this->mean, // x
363  Ap, // u
364  df_dx, df_du,
365  &this->mean // Output: this->mean + Ap;
366  );
367 
368  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
369  df_dx.multiply_HCHt(OLD_COV, cov);
370  // df_du: Nothing to do, since COV(Ap) = zeros
371 }
372 
373 /*---------------------------------------------------------------
374  +=
375  ---------------------------------------------------------------*/
377 {
378  // COV:
379  const CMatrixDouble77 OLD_COV = this->cov;
381 
383  this->mean, // x
384  Ap.mean, // u
385  df_dx, df_du,
386  &this->mean // Output: this->mean + Ap.mean;
387  );
388 
389  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
390  df_dx.multiply_HCHt(OLD_COV, cov);
391  df_du.multiply_HCHt(Ap.cov, cov, true); // Accumulate result
392 }
393 
394 /*---------------------------------------------------------------
395  -=
396  ---------------------------------------------------------------*/
398 {
399  // THIS = THIS (-) Ap -->
400  // THIS = inverse(Ap) (+) THIS
401  CPose3DQuatPDFGaussian inv_Ap = -Ap;
402  *this = inv_Ap + *this;
403 }
404 
405 /*---------------------------------------------------------------
406  evaluatePDF
407  ---------------------------------------------------------------*/
409 {
410  return mrpt::math::normalPDF(
411  CMatrixDouble71(x), CMatrixDouble71(this->mean), this->cov);
412 }
413 
414 /*---------------------------------------------------------------
415  evaluateNormalizedPDF
416  ---------------------------------------------------------------*/
418 {
419  return mrpt::math::normalPDF(
420  CMatrixDouble71(x), CMatrixDouble71(this->mean), this->cov, true);
421 }
422 
423 /*---------------------------------------------------------------
424  assureSymmetry
425  ---------------------------------------------------------------*/
427 {
428  // Differences, when they exist, appear in the ~15'th significant
429  // digit, so... just take one of them arbitrarily!
430  for (unsigned int i = 0; i < size(cov, 1) - 1; i++)
431  for (unsigned int j = i + 1; j < size(cov, 1); j++)
432  cov.get_unsafe(i, j) = cov.get_unsafe(j, i);
433 }
434 
435 /*---------------------------------------------------------------
436  mahalanobisDistanceTo
437  ---------------------------------------------------------------*/
439  const CPose3DQuatPDFGaussian& theOther)
440 {
441  MRPT_START
442  const CMatrixDouble77 COV2 = cov + theOther.cov;
444  CMatrixDouble71(this->mean) - CMatrixDouble71(theOther.mean), COV2);
445  MRPT_END
446 }
447 
448 /*---------------------------------------------------------------
449  operator <<
450  ---------------------------------------------------------------*/
452  ostream& out, const CPose3DQuatPDFGaussian& obj)
453 {
454  out << "Mean: " << obj.mean << "\n";
455  out << "Covariance:\n" << obj.cov << "\n";
456  return out;
457 }
458 
460  const CPose3DQuatPDFGaussian& p1, const CPose3DQuatPDFGaussian& p2)
461 {
462  return p1.mean == p2.mean && p1.cov == p2.cov;
463 }
464 
467 {
469  res += u;
470  return res;
471 }
472 
475 {
477  res -= u;
478  return res;
479 }
A namespace of pseudo-random numbers genrators 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:60
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
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.
void inverseComposePoint(const double gx, const double gy, const double gz, double &lx, double &ly, double &lz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 7 > *out_jacobian_df_dpose=nullptr) const
Computes the 3D point L such as .
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
CPose3D mean
The mean value.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
void copyFrom(const CPose3DPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
TConstructorFlags_Quaternions
Definition: CQuaternion.h:22
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.
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
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:3721
Scalar * iterator
Definition: eigen_plugins.h:26
double mahalanobisDistanceTo(const CPose3DQuatPDFGaussian &theOther)
Computes the Mahalanobis distance between the centers of two Gaussians.
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...
mrpt::math::CMatrixDouble77 cov
The 7x7 covariance matrix.
double evaluatePDF(const CPose3DQuat &x) const
Evaluates the PDF at a given point.
void 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)...
void getAsQuaternion(mrpt::math::CQuaternionDouble &q, mrpt::math::CMatrixFixedNumeric< double, 4, 3 > *out_dq_dr=nullptr) const
Returns the quaternion associated to the rotation of this object (NOTE: XYZ translation is ignored) ...
Definition: CPose3D.cpp:584
STL namespace.
void operator+=(const CPose3DQuat &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
CMatrixFixedNumeric< double, 7, 1 > CMatrixDouble71
Definition: eigen_frwds.h:65
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
void USE_SUT_EULER2QUAT_CONVERSION(bool value)
If set to true (default), a Scaled Unscented Transform is used instead of a linear approximation with...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
double evaluateNormalizedPDF(const CPose3DQuat &x) const
Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
void getCovarianceAndMean(mrpt::math::CMatrixDouble77 &cov, CPose3DQuat &mean_point) const override
Returns an estimate of the pose covariance matrix (7x7 cov matrix) and the mean, both at once...
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void conj(CQuaternion &q_out) const
Return the conjugate quaternion.
Definition: CQuaternion.h:374
const GLubyte * c
Definition: glext.h:6313
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
#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 aux_poseypr2posequat(const CArrayDouble< 6 > &x, const double &dummy, CArrayDouble< 7 > &y)
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
void drawSingleSample(CPose3DQuat &outPart) const override
Draws a single sample from the distribution.
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:48
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:41
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
mrpt::math::CMatrixDouble66 cov
The 6x6 covariance matrix.
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:163
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:88
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).
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:59
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
A partial specialization of CArrayNumeric for double numbers.
Definition: CArrayNumeric.h:87
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
GLenum GLint GLint y
Definition: glext.h:3538
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
void inverse(CPose3DQuatPDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
GLsizei const GLfloat * value
Definition: glext.h:4117
GLsizeiptr size
Definition: glext.h:3923
void getMean(CPose3DQuat &mean_pose) const override
Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF).
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
A quaternion, which can represent a 3D rotation as pair , with a real part "r" and a 3D vector ...
Definition: CQuaternion.h:46
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:989
GLfloat GLfloat p
Definition: glext.h:6305
void assureSymmetry()
Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
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