Main MRPT website > C++ reference for MRPT 1.9.9
CPointPDFParticles.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 
12 #include <mrpt/utils/CStream.h>
14 #include <mrpt/poses/CPoint3D.h>
15 #include <mrpt/poses/CPose3D.h>
16 #include <mrpt/math/ops_containers.h> // maximum()
17 #include <mrpt/system/os.h>
18 
19 using namespace mrpt;
20 using namespace mrpt::poses;
21 using namespace mrpt::utils;
22 using namespace mrpt::math;
23 using namespace mrpt::system;
24 
27 
29 {
30  setSize(numParticles);
31 }
32 
33 /** Clear all the particles (free memory) */
35 /*---------------------------------------------------------------
36  setSize
37  ---------------------------------------------------------------*/
39  size_t numberParticles, const CPoint3D& defaultValue)
40 {
41  // Free old particles: automatic via smart ptr
42  m_particles.resize(numberParticles);
43  for (auto& it : m_particles)
44  {
45  it.log_w = 0;
46  it.d.reset(new TSimple3DPoint(defaultValue));
47  }
48 }
49 
50 /*---------------------------------------------------------------
51  getMean
52  Returns an estimate of the pose, (the mean, or mathematical expectation of the
53  PDF)
54  ---------------------------------------------------------------*/
56 {
58  if (m_particles.empty())
59  THROW_EXCEPTION("Cannot compute mean since there are zero particles.")
60 
62  double sumW = 0;
63  double x = 0, y = 0, z = 0;
64  for (it = m_particles.begin(); it != m_particles.end(); it++)
65  {
66  const double w = exp(it->log_w);
67  x += it->d->x * w;
68  y += it->d->y * w;
69  z += it->d->z * w;
70  sumW += w;
71  }
72 
73  ASSERT_(sumW != 0)
74 
75  sumW = 1.0 / sumW;
76 
77  p.x(x * sumW);
78  p.y(y * sumW);
79  p.z(z * sumW);
80 
81  MRPT_END
82 }
83 
84 /*---------------------------------------------------------------
85  getEstimatedCovariance
86  ---------------------------------------------------------------*/
89 {
91 
92  getMean(mean);
93  cov.zeros();
94 
95  size_t i, n = m_particles.size();
96  double var_x = 0, var_y = 0, var_p = 0, var_xy = 0, var_xp = 0, var_yp = 0;
97 
98  double lin_w_sum = 0;
99 
100  for (i = 0; i < n; i++) lin_w_sum += exp(m_particles[i].log_w);
101  if (lin_w_sum == 0) lin_w_sum = 1;
102 
103  for (i = 0; i < n; i++)
104  {
105  double w = exp(m_particles[i].log_w) / lin_w_sum;
106 
107  double err_x = m_particles[i].d->x - mean.x();
108  double err_y = m_particles[i].d->y - mean.y();
109  double err_phi = m_particles[i].d->z - mean.z();
110 
111  var_x += square(err_x) * w;
112  var_y += square(err_y) * w;
113  var_p += square(err_phi) * w;
114  var_xy += err_x * err_y * w;
115  var_xp += err_x * err_phi * w;
116  var_yp += err_y * err_phi * w;
117  }
118 
119  if (n >= 2)
120  {
121  // Unbiased estimation of variance:
122  cov(0, 0) = var_x;
123  cov(1, 1) = var_y;
124  cov(2, 2) = var_p;
125 
126  cov(1, 0) = cov(0, 1) = var_xy;
127  cov(2, 0) = cov(0, 2) = var_xp;
128  cov(1, 2) = cov(2, 1) = var_yp;
129  }
130 
131  MRPT_END
132 }
133 
134 /*---------------------------------------------------------------
135  writeToStream
136  ---------------------------------------------------------------*/
138  mrpt::utils::CStream& out, int* version) const
139 {
140  if (version)
141  *version = 0;
142  else
143  {
144  uint32_t N = size();
145  out << N;
146 
147  for (CParticleList::const_iterator it = m_particles.begin();
148  it != m_particles.end(); ++it)
149  out << it->log_w << it->d->x << it->d->y << it->d->z;
150  }
151 }
152 
153 /*---------------------------------------------------------------
154  readFromStream
155  ---------------------------------------------------------------*/
157 {
158  switch (version)
159  {
160  case 0:
161  {
162  uint32_t N;
163  in >> N;
164  setSize(N);
165 
166  for (CParticleList::iterator it = m_particles.begin();
167  it != m_particles.end(); ++it)
168  in >> it->log_w >> it->d->x >> it->d->y >> it->d->z;
169  }
170  break;
171  default:
173  };
174 }
175 
177 {
178  if (this == &o) return; // It may be used sometimes
179 
180  // Convert to samples:
181  THROW_EXCEPTION("NO");
182 }
183 
184 /*---------------------------------------------------------------
185 
186  ---------------------------------------------------------------*/
188 {
189  MRPT_START
190 
191  FILE* f = os::fopen(file.c_str(), "wt");
192  if (!f) return;
193 
194  size_t i, N = m_particles.size();
195  for (i = 0; i < N; i++)
196  os::fprintf(
197  f, "%f %f %f %e\n", m_particles[i].d->x, m_particles[i].d->y,
198  m_particles[i].d->z, m_particles[i].log_w);
199 
200  os::fclose(f);
201 
202  MRPT_END
203 }
204 
205 /*---------------------------------------------------------------
206  changeCoordinatesReference
207  ---------------------------------------------------------------*/
209  const CPose3D& newReferenceBase)
210 {
211  TPoint3D pt;
212  for (CParticleList::iterator it = m_particles.begin();
213  it != m_particles.end(); ++it)
214  {
215  newReferenceBase.composePoint(
216  it->d->x, it->d->y, it->d->z, // In
217  pt.x, pt.y, pt.z // Out
218  );
219  it->d->x = pt.x;
220  it->d->y = pt.y;
221  it->d->z = pt.z;
222  }
223 }
224 
225 /*---------------------------------------------------------------
226  computeKurtosis
227  ---------------------------------------------------------------*/
229 {
230  MRPT_START
231 
232  // kurtosis = \mu^4 / (\sigma^2) -3
233  CVectorDouble kurts, mu4, m, var;
234  kurts.assign(3, .0);
235  mu4.assign(3, .0);
236  m.assign(3, .0);
237  var.assign(3, .0);
238 
239  // Means:
240  for (CParticleList::iterator it = m_particles.begin();
241  it != m_particles.end(); ++it)
242  {
243  m[0] += it->d->x;
244  m[1] += it->d->y;
245  m[2] += it->d->z;
246  }
247  m *= 1.0 / m_particles.size();
248 
249  // variances:
250  for (CParticleList::iterator it = m_particles.begin();
251  it != m_particles.end(); ++it)
252  {
253  var[0] += square(it->d->x - m[0]);
254  var[1] += square(it->d->y - m[1]);
255  var[2] += square(it->d->z - m[2]);
256  }
257  var *= 1.0 / m_particles.size();
258  var[0] = square(var[0]);
259  var[1] = square(var[1]);
260  var[2] = square(var[2]);
261 
262  // Moment:
263  for (CParticleList::iterator it = m_particles.begin();
264  it != m_particles.end(); ++it)
265  {
266  mu4[0] += pow(it->d->x - m[0], 4.0);
267  mu4[1] += pow(it->d->y - m[1], 4.0);
268  mu4[2] += pow(it->d->z - m[2], 4.0);
269  }
270  mu4 *= 1.0 / m_particles.size();
271 
272  // Kurtosis's
273  kurts.array() = mu4.array() / var.array();
274 
275  return math::maximum(kurts);
276 
277  MRPT_END
278 }
279 
280 /*---------------------------------------------------------------
281  drawSingleSample
282  ---------------------------------------------------------------*/
284 {
285  MRPT_UNUSED_PARAM(outSample);
286  THROW_EXCEPTION("TO DO!")
287 }
288 
289 /*---------------------------------------------------------------
290  bayesianFusion
291  ---------------------------------------------------------------*/
293  const CPointPDF& p1_, const CPointPDF& p2_,
294  const double& minMahalanobisDistToDrop)
295 {
296  MRPT_UNUSED_PARAM(p1_);
297  MRPT_UNUSED_PARAM(p2_);
298  MRPT_UNUSED_PARAM(minMahalanobisDistToDrop);
299  MRPT_START
300 
301  THROW_EXCEPTION("TODO!!!");
302 
303  MRPT_END
304 }
305 
306 /*---------------------------------------------------------------
307  writeToStream
308  ---------------------------------------------------------------*/
310  mrpt::utils::CStream& out, int* version) const
311 {
312  MRPT_UNUSED_PARAM(out);
313  MRPT_UNUSED_PARAM(version);
314  THROW_EXCEPTION("Shouldn't arrive here!");
315 }
316 
317 /*---------------------------------------------------------------
318  readFromStream
319  ---------------------------------------------------------------*/
321 {
323  MRPT_UNUSED_PARAM(version);
324  THROW_EXCEPTION("Shouldn't arrive here!");
325 }
void drawSingleSample(CPoint3D &outSample) const override
Draw a sample from the pdf.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble z
Definition: glext.h:3872
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
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.
#define THROW_EXCEPTION(msg)
GLenum GLsizei n
Definition: glext.h:5074
Scalar * iterator
Definition: eigen_plugins.h:26
This file implements several operations that operate element-wise on individual or pairs of container...
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
const Scalar * const_iterator
Definition: eigen_plugins.h:27
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void setSize(size_t numberParticles, const CPoint3D &defaultValue=CPoint3D(0, 0, 0))
Erase all the previous particles and change the number of particles, with a given initial value...
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
Data within each particle.
double x
X,Y,Z coordinates.
void getMean(CPoint3D &mean_point) const override
Returns an estimate of the point, (the mean, or mathematical expectation of the PDF) ...
CONTAINER::Scalar maximum(const CONTAINER &v)
void bayesianFusion(const CPointPDF &p1, const CPointPDF &p2, const double &minMahalanobisDistToDrop=0) override
Bayesian fusion of two point distributions (product of two distributions->new distribution), then save the result in this object (WARNING: See implementing classes to see classes that can and cannot be mixtured!)
GLsizei const GLchar ** string
Definition: glext.h:4101
A class used to store a 3D point.
Definition: CPoint3D.h:32
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
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
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...
void copyFrom(const CPointPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixedNumeric< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixedNumeric< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:453
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
GLenum GLint GLint y
Definition: glext.h:3538
void clear()
Clear all the particles (free memory)
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:254
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 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...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
GLsizeiptr size
Definition: glext.h:3923
void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPoint3D &mean_point) const override
Returns an estimate of the point covariance matrix (3x3 cov matrix) and the mean, both at once...
GLenum GLint x
Definition: glext.h:3538
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:47
Declares a class that represents a Probability Distribution function (PDF) of a 3D point (x...
Definition: CPointPDF.h:39
double computeKurtosis()
Compute the kurtosis of the distribution.
GLfloat GLfloat p
Definition: glext.h:6305
A probability distribution of a 2D/3D point, represented as a set of random samples (particles)...
void saveToTextFile(const std::string &file) const override
Save PDF&#39;s particles to a text file, where each line is: X Y Z LOG_W.
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.



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