Main MRPT website > C++ reference for MRPT 1.9.9
CPose3DQuatPDFGaussianInf.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-2018, 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 "poses-precomp.h" // Precompiled headers
11 
13 #include <mrpt/math/types_math.h> // for CMatrixF...
14 #include <stdio.h> // for size_t
15 #include <algorithm> // for move, max
16 #include <exception> // for exception
17 #include <new> // for operator...
18 #include <ostream> // for operator<<
19 #include <string> // for allocator
20 #include <vector> // for vector
21 #include <mrpt/math/CMatrixFixedNumeric.h> // for CMatrixF...
22 #include <mrpt/math/CQuaternion.h> // for CQuatern...
23 #include <mrpt/poses/CPose3D.h> // for CPose3D
24 #include <mrpt/poses/CPose3DQuat.h> // for CPose3DQuat
25 #include <mrpt/poses/CPose3DQuatPDF.h> // for CPose3DQ...
26 #include <mrpt/random/RandomGenerators.h> // for CRandomG...
27 #include <mrpt/serialization/CSerializable.h> // for CSeriali...
28 #include <mrpt/system/os.h> // for fopen
31 
32 using namespace mrpt;
33 using namespace mrpt::system;
34 using namespace mrpt::poses;
35 using namespace mrpt::math;
36 using namespace mrpt::random;
37 
38 using namespace std;
39 
41 
42 /** Default constructor - set all values to zero. */
44 // Un-initialized constructor:
45 CPose3DQuatPDFGaussianInf::CPose3DQuatPDFGaussianInf(
46  TConstructorFlags_Quaternions constructor_dummy_param)
48 {
49  MRPT_UNUSED_PARAM(constructor_dummy_param);
50 }
51 
52 /** Constructor from a default mean value, covariance equals to zero. */
54  const CPose3DQuat& init_Mean)
55  : mean(init_Mean), cov_inv()
56 {
57 }
58 
59 /** Constructor with mean and covariance. */
61  const CPose3DQuat& init_Mean, const CMatrixDouble77& init_CovInv)
62  : mean(init_Mean), cov_inv(init_CovInv)
63 {
64 }
65 
69 {
70  out << mean;
71 
72  for (int r = 0; r < cov_inv.rows(); r++) out << cov_inv.get_unsafe(r, r);
73  for (int r = 0; r < cov_inv.rows(); r++)
74  for (int c = r + 1; c < cov_inv.cols(); c++)
75  out << cov_inv.get_unsafe(r, c);
76 }
79 {
80  switch (version)
81  {
82  case 0:
83  {
84  in >> mean;
85 
86  for (int r = 0; r < cov_inv.rows(); r++)
87  in >> cov_inv.get_unsafe(r, r);
88  for (int r = 0; r < cov_inv.rows(); r++)
89  for (int c = r + 1; c < cov_inv.cols(); c++)
90  {
91  double x;
92  in >> x;
93  cov_inv.get_unsafe(r, c) = cov_inv.get_unsafe(c, r) = x;
94  }
95  }
96  break;
97  default:
99  };
100 }
101 
103 {
104  if (this == &o) return; // It may be used sometimes
105 
106  // Convert to gaussian pdf:
108  o.getCovarianceAndMean(C, this->mean);
109  C.inv_fast(this->cov_inv);
110 }
111 
112 /*---------------------------------------------------------------
113  saveToTextFile
114  ---------------------------------------------------------------*/
115 bool CPose3DQuatPDFGaussianInf::saveToTextFile(const string& file) const
116 {
117  FILE* f = os::fopen(file.c_str(), "wt");
118  if (!f) return false;
119 
120  os::fprintf(
121  f, "%e %e %e %e %e %e %e\n", mean.x(), mean.y(), mean.z(),
122  mean.quat()[0], mean.quat()[1], mean.quat()[2], mean.quat()[3]);
123 
124  for (unsigned int i = 0; i < 7; i++)
125  os::fprintf(
126  f, "%e %e %e %e %e %e %e\n", cov_inv(i, 0), cov_inv(i, 1),
127  cov_inv(i, 2), cov_inv(i, 3), cov_inv(i, 4), cov_inv(i, 5),
128  cov_inv(i, 6));
129 
130  os::fclose(f);
131  return true;
132 }
133 
134 /*---------------------------------------------------------------
135  changeCoordinatesReference
136  ---------------------------------------------------------------*/
138  const CPose3D& newReferenceBase)
139 {
140  MRPT_START
141  changeCoordinatesReference(CPose3DQuat(newReferenceBase));
142  MRPT_END
143 }
144 
145 /*---------------------------------------------------------------
146  changeCoordinatesReference
147  ---------------------------------------------------------------*/
149  const CPose3DQuat& newReferenceBaseQuat)
150 {
151  MRPT_START
152 
153  // COV:
155  this->cov_inv.inv(OLD_COV);
156 
158 
160  newReferenceBaseQuat, // x
161  this->mean, // u
162  df_dx, df_du,
163  &this->mean // Output: newReferenceBaseQuat + this->mean;
164  );
165 
166  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
167  // df_dx: not used, since its COV are all zeros... // df_dx.multiply_HCHt(
168  // OLD_COV, cov );
170  df_du.multiply_HCHt(OLD_COV, NEW_COV);
171  NEW_COV.inv_fast(this->cov_inv);
172 
173  MRPT_END
174 }
175 
176 /*---------------------------------------------------------------
177  drawSingleSample
178  ---------------------------------------------------------------*/
180 {
181  MRPT_START
183  this->cov_inv.inv(COV);
184 
186  MRPT_END
187 }
188 
189 /*---------------------------------------------------------------
190  drawManySamples
191  ---------------------------------------------------------------*/
193  size_t N, vector<CVectorDouble>& outSamples) const
194 {
195  MRPT_START
197  this->cov_inv.inv(COV);
198 
199  getRandomGenerator().drawGaussianMultivariateMany(outSamples, N, COV);
200 
201  for (vector<CVectorDouble>::iterator it = outSamples.begin();
202  it != outSamples.end(); ++it)
203  for (unsigned int k = 0; k < 7; k++) (*it)[k] += mean[k];
204 
205  MRPT_END
206 }
207 
208 /*---------------------------------------------------------------
209  inverse
210  ---------------------------------------------------------------*/
212 {
215 
216  // COV:
218  double lx, ly, lz;
219  mean.inverseComposePoint(0, 0, 0, lx, ly, lz, nullptr, &df_dpose);
220 
222  jacob.insertMatrix(0, 0, df_dpose);
223  jacob.set_unsafe(3, 3, 1);
224  jacob.set_unsafe(4, 4, -1);
225  jacob.set_unsafe(5, 5, -1);
226  jacob.set_unsafe(6, 6, -1);
227 
228  // C(0:2,0:2): H C H^t
230  this->cov_inv.inv(COV);
231 
232  jacob.multiply_HCHt(COV, NEW_COV);
233  NEW_COV.inv_fast(out.cov_inv);
234 
235  // Mean:
236  out.mean.x(lx);
237  out.mean.y(ly);
238  out.mean.z(lz);
239  this->mean.quat().conj(out.mean.quat());
240 }
241 
242 /*---------------------------------------------------------------
243  +=
244  ---------------------------------------------------------------*/
246 {
247  // COV:
249  this->cov_inv.inv(OLD_COV);
250 
252 
254  this->mean, // x
255  Ap, // u
256  df_dx, df_du,
257  &this->mean // Output: this->mean + Ap;
258  );
259 
260  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
262  df_dx.multiply_HCHt(OLD_COV, NEW_COV);
263  NEW_COV.inv_fast(this->cov_inv);
264  // df_du: Nothing to do, since COV(Ap) = zeros
265 }
266 
267 /*---------------------------------------------------------------
268  +=
269  ---------------------------------------------------------------*/
271 {
272  // COV:
274  this->cov_inv.inv(OLD_COV);
275 
277 
279  this->mean, // x
280  Ap.mean, // u
281  df_dx, df_du,
282  &this->mean // Output: this->mean + Ap.mean;
283  );
284 
285  // this->cov = H1*this->cov*~H1 + H2*Ap.cov*~H2;
288  Ap.cov_inv.inv(Ap_cov);
289 
290  df_dx.multiply_HCHt(OLD_COV, NEW_COV);
291  df_du.multiply_HCHt(Ap_cov, NEW_COV, true); // Accumulate result
292 
293  NEW_COV.inv_fast(this->cov_inv);
294 }
295 
296 /*---------------------------------------------------------------
297  -=
298  ---------------------------------------------------------------*/
300 {
301  // THIS = THIS (-) Ap -->
302  // THIS = inverse(Ap) (+) THIS
303  CPose3DQuatPDFGaussianInf inv_Ap = -Ap;
304  *this = inv_Ap + *this;
305 }
306 
307 /*---------------------------------------------------------------
308  evaluatePDF
309  ---------------------------------------------------------------*/
311 {
313  CMatrixDouble71(x), CMatrixDouble71(this->mean), this->cov_inv);
314 }
315 
316 /*---------------------------------------------------------------
317  evaluateNormalizedPDF
318  ---------------------------------------------------------------*/
320  const CPose3DQuat& x) const
321 {
323  CMatrixDouble71(x), CMatrixDouble71(this->mean), this->cov_inv, true);
324 }
325 
326 /*---------------------------------------------------------------
327  operator <<
328  ---------------------------------------------------------------*/
330  ostream& out, const CPose3DQuatPDFGaussianInf& obj)
331 {
332  out << "Mean: " << obj.mean << "\n";
333  out << "Information:\n" << obj.cov_inv << "\n";
334  return out;
335 }
336 
339 {
340  return p1.mean == p2.mean && p1.cov_inv == p2.cov_inv;
341 }
342 
343 /** Pose composition for two 3D pose Gaussians \sa
344  * CPose3DQuatPDFGaussianInf::operator += */
347 {
349  res += u;
350  return res;
351 }
352 
353 /** Inverse pose composition for two 3D pose Gaussians \sa
354  * CPose3DQuatPDFGaussianInf::operator -= */
357 {
359  res -= u;
360  return res;
361 }
A namespace of pseudo-random numbers generators of diferent distributions.
void inverse(CPose3DQuatPDF &o) const override
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
mrpt::math::CQuaternionDouble & quat()
Read/Write access to the quaternion representing the 3D rotation.
Definition: CPose3DQuat.h:61
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
Scalar * iterator
Definition: eigen_plugins.h:26
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 .
#define MRPT_START
Definition: exceptions.h:262
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:25
TConstructorFlags_Quaternions
Definition: CQuaternion.h:22
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
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...
#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:364
Declares a class that represents a Probability Density function (PDF) of a 3D pose using a quaternion...
void copyFrom(const CPose3DQuatPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
STL namespace.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
CMatrixFixedNumeric< double, 7, 1 > CMatrixDouble71
Definition: eigen_frwds.h:67
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
double evaluateNormalizedPDF(const CPose3DQuat &x) const
Evaluates the ratio PDF(x) / PDF(MEAN), that is, the normalized PDF in the range [0,1].
void operator+=(const CPose3DQuat &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
CPose3DQuatPDFGaussianInf()
Default constructor - set all values to zero.
unsigned char uint8_t
Definition: rptypes.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
mrpt::math::CMatrixDouble77 cov_inv
The 7x7 information matrix (the inverse of the covariance)
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
void changeCoordinatesReference(const CPose3DQuat &newReferenceBase)
this = p (+) this.
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:85
void conj(CQuaternion &q_out) const
Return the conjugate quaternion.
Definition: CQuaternion.h:374
const GLubyte * c
Definition: glext.h:6313
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:315
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
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
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)...
double evaluatePDF(const CPose3DQuat &x) const
Evaluates the PDF at a given point.
MATRIXLIKE::Scalar normalPDFInf(const VECTORLIKE1 &x, const VECTORLIKE2 &mu, const MATRIXLIKE &cov_inv, const bool scaled_pdf=false)
Evaluates the multivariate normal (Gaussian) distribution at a given point "x".
Definition: distributions.h:41
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
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.
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:406
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
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.
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:166
void operator-=(const CPose3DQuatPDFGaussianInf &Ap)
Makes: thisPDF = thisPDF - Ap, where "-" is pose inverse composition (both the mean, and the covariance matrix are updated).
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
#define MRPT_END
Definition: exceptions.h:266
GLuint in
Definition: glext.h:7274
void drawSingleSample(CPose3DQuat &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:255
GLuint res
Definition: glext.h:7268
GLenum GLint x
Definition: glext.h:3538
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.
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:140
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019