Main MRPT website > C++ reference for MRPT 1.5.6
CPose3DPDFParticles.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 
14 #include <mrpt/poses/CPose3D.h>
15 #include <mrpt/math/wrap2pi.h>
16 #include <mrpt/utils/CStream.h>
18 #include <mrpt/system/os.h>
19 
20 using namespace mrpt;
21 using namespace mrpt::poses;
22 using namespace mrpt::math;
23 using namespace mrpt::utils;
24 
26 
27 /*---------------------------------------------------------------
28  Constructor
29  ---------------------------------------------------------------*/
31 {
32  m_particles.resize(M);
33 
34  for (auto &it : m_particles)
35  {
36  it.log_w = .0;
37  it.d.reset(new CPose3D());
38  }
39 
40  CPose3D nullPose(0,0,0);
41  resetDeterministic( nullPose );
42 }
43 
45 {
47 
50 
51  if (this == &o) return; // It may be used sometimes
52 
54  {
55  const CPose3DPDFParticles *pdf = dynamic_cast<const CPose3DPDFParticles*>( &o );
56  ASSERT_(pdf);
57 
58  m_particles = pdf->m_particles;
59  }
60  else
62  {
63  THROW_EXCEPTION("TO DO!!");
64  }
65 
66  MRPT_END
67 }
68 
69 /*---------------------------------------------------------------
70  getMean
71  Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF), computed
72  as a weighted average over all m_particles.
73  ---------------------------------------------------------------*/
75 {
77 
78  // Default to (0,..,0)
79  p = CPose3D();
80  if (m_particles.empty())
81  return;
82 
83  // Calc average on SE(3)
84  mrpt::poses::SE_average<3> se_averager;
85  for (CPose3DPDFParticles::CParticleList::const_iterator it=m_particles.begin();it!=m_particles.end();++it)
86  {
87  const double w = exp(it->log_w);
88  se_averager.append( *it->d,w );
89  }
90  se_averager.get_average(p);
91 
92  MRPT_END
93 }
94 
95 /*---------------------------------------------------------------
96  getCovarianceAndMean
97  ---------------------------------------------------------------*/
99 {
100  MRPT_START
101 
102  getMean(mean); // First! the mean value:
103 
104  // Now the covariance:
105  cov.zeros();
106  CVectorDouble vars; vars.assign(6,0.0); // The diagonal of the final covariance matrix
107 
108  // Elements off the diagonal of the covariance matrix:
109  double std_xy = 0,std_xz = 0,std_xya= 0,std_xp = 0,std_xr = 0;
110  double std_yz = 0,std_yya = 0,std_yp = 0,std_yr = 0;
111  double std_zya = 0,std_zp = 0,std_zr = 0;
112  double std_yap = 0,std_yar = 0;
113  double std_pr = 0;
114 
115  // Mean values in [0, 2pi] range:
116  double mean_yaw = mean.yaw();
117  double mean_pitch = mean.pitch();
118  double mean_roll = mean.roll();
119  if (mean_yaw<0) mean_yaw += M_2PI;
120  if (mean_pitch<0) mean_pitch += M_2PI;
121  if (mean_roll<0) mean_roll += M_2PI;
122 
123  // Enought information to estimate the covariance?
124  if (m_particles.size()<2) return;
125 
126 
127  // Sum all weight values:
128  double W = 0;
129  for (CPose3DPDFParticles::CParticleList::const_iterator it=m_particles.begin();it!=m_particles.end();++it)
130  W += exp(it->log_w);
131 
132  ASSERT_(W>0);
133 
134  // Compute covariance:
135  for (CPose3DPDFParticles::CParticleList::const_iterator it=m_particles.begin();it!=m_particles.end();++it)
136  {
137  double w = exp( it->log_w ) / W;
138 
139  // Manage 1 PI range:
140  double err_yaw = wrapToPi( fabs(it->d->yaw() - mean_yaw) );
141  double err_pitch = wrapToPi( fabs(it->d->pitch() - mean_pitch) );
142  double err_roll = wrapToPi( fabs(it->d->roll() - mean_roll) );
143 
144  double err_x = it->d->x() - mean.x();
145  double err_y = it->d->y() - mean.y();
146  double err_z = it->d->z() - mean.z();
147 
148  vars[0] += square(err_x)*w;
149  vars[1] += square(err_y)*w;
150  vars[2] += square(err_z)*w;
151  vars[3] += square(err_yaw)*w;
152  vars[4] += square(err_pitch)*w;
153  vars[5] += square(err_roll)*w;
154 
155  std_xy += err_x*err_y * w;
156  std_xz += err_x*err_z * w;
157  std_xya += err_x*err_yaw * w;
158  std_xp += err_x*err_pitch * w;
159  std_xr += err_x*err_roll * w;
160 
161  std_yz += err_y*err_z * w;
162  std_yya += err_y*err_yaw * w;
163  std_yp += err_y*err_pitch * w;
164  std_yr += err_y*err_roll * w;
165 
166  std_zya += err_z*err_yaw * w;
167  std_zp += err_z*err_pitch * w;
168  std_zr += err_z*err_roll * w;
169 
170  std_yap += err_yaw*err_pitch * w;
171  std_yar += err_yaw*err_roll * w;
172 
173  std_pr += err_pitch*err_roll * w;
174  } // end for it
175 
176  // Unbiased estimation of variance:
177  cov(0,0) = vars[0];
178  cov(1,1) = vars[1];
179  cov(2,2) = vars[2];
180  cov(3,3) = vars[3];
181  cov(4,4) = vars[4];
182  cov(5,5) = vars[5];
183 
184  cov(1,0) = cov(0,1) = std_xy;
185  cov(2,0) = cov(0,2) = std_xz;
186  cov(3,0) = cov(0,3) = std_xya;
187  cov(4,0) = cov(0,4) = std_xp;
188  cov(5,0) = cov(0,5) = std_xr;
189 
190  cov(2,1) = cov(1,2) = std_yz;
191  cov(3,1) = cov(1,3) = std_yya;
192  cov(4,1) = cov(1,4) = std_yp;
193  cov(5,1) = cov(1,5) = std_yr;
194 
195  cov(3,2) = cov(2,3) = std_zya;
196  cov(4,2) = cov(2,4) = std_zp;
197  cov(5,2) = cov(2,5) = std_zr;
198 
199  cov(4,3) = cov(3,4) = std_yap;
200  cov(5,3) = cov(3,5) = std_yar;
201 
202  cov(5,4) = cov(4,5) = std_pr;
203 
204 
205  MRPT_END
206 }
207 
208 
209 /*---------------------------------------------------------------
210  writeToStream
211  ---------------------------------------------------------------*/
213 {
214  if (version)
215  *version = 0;
216  else
217  {
218  writeParticlesToStream(out);
219  }
220 }
221 
222 /*---------------------------------------------------------------
223  readFromStream
224  ---------------------------------------------------------------*/
226 {
227  switch(version)
228  {
229  case 0:
230  {
231  readParticlesFromStream(in);
232  } break;
233  default:
235 
236  };
237 }
238 
239 /*---------------------------------------------------------------
240  saveToTextFile
241  Save PDF's m_particles to a text file. In each line it
242  will go: "x y phi weight"
243  ---------------------------------------------------------------*/
245 {
246  using namespace mrpt::system;
247 
248  FILE *f=os::fopen(file.c_str(),"wt");
249  if (!f) return;
250 
251  os::fprintf(f,"%% x y z yaw[rad] pitch[rad] roll[rad] log_weight\n");
252 
253  for (const auto &p : m_particles)
254  os::fprintf(f,"%f %f %f %f %f %f %e\n",
255  p.d->x(),
256  p.d->y(),
257  p.d->z(),
258  p.d->yaw(),
259  p.d->pitch(),
260  p.d->roll(),
261  p.log_w );
262 
263  os::fclose(f);
264 }
265 
266 
267 /*---------------------------------------------------------------
268  getParticlePose
269  ---------------------------------------------------------------*/
271 {
272  return *m_particles[i].d;
273 }
274 
275 /*---------------------------------------------------------------
276  changeCoordinatesReference
277  ---------------------------------------------------------------*/
279 {
280  for (CParticleList::iterator it=m_particles.begin();it!=m_particles.end();++it)
281  it->d->composeFrom(newReferenceBase, *it->d);
282 }
283 
284 /*---------------------------------------------------------------
285  drawSingleSample
286  ---------------------------------------------------------------*/
288 {
289  MRPT_UNUSED_PARAM(outPart);
290  THROW_EXCEPTION("TO DO!");
291 }
292 
293 /*---------------------------------------------------------------
294  drawManySamples
295  ---------------------------------------------------------------*/
297  size_t N,
298  std::vector<CVectorDouble> &outSamples ) const
299 {
300  MRPT_UNUSED_PARAM(N); MRPT_UNUSED_PARAM(outSamples);
301  THROW_EXCEPTION("TO DO!");
302 }
303 
304 /*---------------------------------------------------------------
305  +=
306  ---------------------------------------------------------------*/
308 {
309  MRPT_UNUSED_PARAM(Ap);
310  THROW_EXCEPTION("TO DO!");
311 }
312 
313 /*---------------------------------------------------------------
314  append
315  ---------------------------------------------------------------*/
317 {
319  THROW_EXCEPTION("TO DO!");
320 }
321 
322 /*---------------------------------------------------------------
323  inverse
324  ---------------------------------------------------------------*/
326 {
327  MRPT_START
329  CPose3DPDFParticles * out = static_cast<CPose3DPDFParticles*> (&o);
330 
331  // Prepare the output:
332  out->copyFrom(*this);
333 
335  CPose3D zero(0,0,0);
336 
337  for (it=out->m_particles.begin();it!=out->m_particles.end();++it)
338  *it->d = zero - *it->d;
339 
340  MRPT_END
341 }
342 
343 /*---------------------------------------------------------------
344  getMostLikelyParticle
345  ---------------------------------------------------------------*/
347 {
348  CPose3DPDFParticles::CParticleList::const_iterator it, itMax=m_particles.begin();
349  double max_w = -1e300;
350 
351  for (it=m_particles.begin();it!=m_particles.end();++it)
352  {
353  if (it->log_w > max_w)
354  {
355  itMax = it;
356  max_w = it->log_w;
357  }
358  }
359 
360  return *itMax->d;
361 }
362 
363 /*---------------------------------------------------------------
364  bayesianFusion
365  ---------------------------------------------------------------*/
367 {
369  THROW_EXCEPTION("Not implemented yet!");
370 }
371 
372 
373 /*---------------------------------------------------------------
374  resetDeterministic
375  Reset PDF to a single point and set the number of m_particles
376  ---------------------------------------------------------------*/
378  size_t particlesCount)
379 {
381 
382  if (particlesCount>0)
383  {
384  clearParticles();
385  m_particles.resize(particlesCount);
386  for (it = m_particles.begin(); it != m_particles.end(); ++it)
387  it->d.reset(new CPose3D());
388  }
389 
390  for (it=m_particles.begin();it!=m_particles.end();++it)
391  {
392  *it->d = location;
393  it->log_w = 0;
394  }
395 }
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:393
void drawManySamples(size_t N, std::vector< mrpt::math::CVectorDouble > &outSamples) const MRPT_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.
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
Definition: os.cpp:255
void append(const mrpt::poses::CPose3D &p)
Adds a new pose to the computation.
void getMean(CPose3D &mean_pose) const MRPT_OVERRIDE
Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF), computed as a weighted average over all m_particles.
int BASE_IMPEXP void BASE_IMPEXP 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.
#define THROW_EXCEPTION(msg)
Scalar * iterator
Definition: eigen_plugins.h:23
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1797
void inverse(CPose3DPDF &o) const MRPT_OVERRIDE
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:35
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:412
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
GLuint in
Definition: glew.h:7146
#define M_2PI
Definition: mrpt_macros.h:380
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:52
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:391
void append(CPose3DPDFParticles &o)
Appends (add to the list) a set of m_particles to the existing ones, and then normalize weights...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
CParticleList m_particles
The array of particles.
A numeric matrix of compile-time fixed size.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:113
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
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:135
void operator+=(const CPose3D &Ap)
Appends (pose-composition) a given pose "p" to each particle.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void drawSingleSample(CPose3D &outPart) const MRPT_OVERRIDE
Draws a single sample from the distribution (WARNING: weights are assumed to be normalized!) ...
int version
Definition: mrpt_jpeglib.h:898
GLfloat GLfloat p
Definition: glew.h:10113
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:51
void getCovarianceAndMean(mrpt::math::CMatrixDouble66 &cov, CPose3D &mean_point) const MRPT_OVERRIDE
Returns an estimate of the pose covariance matrix (6x6 cov matrix) and the mean, both at once...
#define CLASS_ID(class_name)
Access to runtime class ID for a defined class name.
Definition: CObject.h:92
#define MRPT_START
Computes weighted and un-weighted averages of SE(3) poses.
GLsizei const GLcharARB ** string
Definition: glew.h:3293
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
void resetDeterministic(const CPose3D &location, size_t particlesCount=0)
Reset the PDF to a single point: All m_particles will be set exactly to the supplied pose...
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
GLint location
Definition: glew.h:1742
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:392
CPose3D getParticlePose(int i) const
Returns the pose of the i'th particle.
#define ASSERT_(f)
void bayesianFusion(const CPose3DPDF &p1, const CPose3DPDF &p2) MRPT_OVERRIDE
Bayesian fusion.
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
void changeCoordinatesReference(const CPose3D &newReferenceBase) MRPT_OVERRIDE
this = p (+) this.
void saveToTextFile(const std::string &file) const MRPT_OVERRIDE
Save PDF's m_particles to a text file. In each line it will go: "x y z".
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
CPose3D getMostLikelyParticle() const
Returns the particle with the highest weight.
Declares a class that represents a Probability Density Function (PDF) of a 3D pose (6D actually)...
Definition: CPose3DPDF.h:40
Declares a class that represents a Probability Density function (PDF) of a 3D pose.
void get_average(mrpt::poses::CPose3D &out_mean) const
Returns the average pose.
void copyFrom(const CPose3DPDF &o) MRPT_OVERRIDE
Copy operator, translating if necesary (for example, between m_particles and gaussian representations...
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f
Definition: glew.h:5092



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018