21 #include <Eigen/Dense> 51 size_t N = m_modes.size();
52 double X = 0, Y = 0, Z = 0;
56 CListGaussianModes::const_iterator it;
59 for (it = m_modes.begin(); it != m_modes.end(); ++it)
62 sumW += w = exp(it->log_w);
63 X += it->val.mean.x() * w;
64 Y += it->val.mean.y() * w;
65 Z += it->val.mean.z() * w;
82 size_t N = m_modes.size();
95 CListGaussianModes::const_iterator it;
97 for (it = m_modes.begin(); it != m_modes.end(); ++it)
100 sumW += w = exp(it->log_w);
103 estMean_i -= estMean;
107 partCov += it->val.cov;
112 if (sumW != 0) estCov *= (1.0 / sumW);
121 uint32_t N = m_modes.size();
123 for (
const auto& m : m_modes)
141 for (
auto& m : m_modes)
146 if (version == 0) m.log_w = log(max(1e-300, m.log_w));
162 if (
this == &o)
return;
166 m_modes =
dynamic_cast<const CPointPDFSOG*
>(&o)->m_modes;
172 m_modes[0].log_w = 0;
185 if (!f)
return false;
187 for (
const auto& m_mode : m_modes)
189 f,
"%e %e %e %e %e %e %e %e %e %e\n", exp(m_mode.log_w),
190 m_mode.val.mean.x(), m_mode.val.mean.y(), m_mode.val.mean.z(),
191 m_mode.val.cov(0, 0), m_mode.val.cov(1, 1), m_mode.val.cov(2, 2),
192 m_mode.val.cov(0, 1), m_mode.val.cov(0, 2), m_mode.val.cov(1, 2));
202 for (
auto& m : m_modes) m.val.changeCoordinatesReference(newReferenceBase);
215 vector<double> logWeights(m_modes.size());
216 vector<size_t> outIdxs;
217 vector<double>::iterator itW;
218 CListGaussianModes::const_iterator it;
219 for (it = m_modes.begin(), itW = logWeights.begin(); it != m_modes.end();
223 CParticleFilterCapable::computeResampling(
224 CParticleFilter::prMultinomial,
230 size_t selectedIdx = outIdxs[0];
231 ASSERT_(selectedIdx < m_modes.size());
239 outSample.
x(selMode->mean.x() + vec[0]);
240 outSample.
y(selMode->mean.y() + vec[1]);
241 outSample.z(selMode->mean.z() + vec[2]);
251 const double minMahalanobisDistToDrop)
260 const auto* p1 =
dynamic_cast<const CPointPDFSOG*
>(&p1_);
261 const auto* p2 =
dynamic_cast<const CPointPDFSOG*
>(&p2_);
267 const double minMahalanobisDistToDrop2 =
square(minMahalanobisDistToDrop);
269 this->m_modes.clear();
273 for (
const auto& m : p1->m_modes)
284 ASSERT_(c(0, 0) != 0 && c(0, 0) != 0);
292 double a = -0.5 * (3 * log(
M_2PI) - log(covInv.
det()) +
293 (eta.transpose() * c.
asEigen() * eta)(0, 0));
295 for (
const auto& m2 : p2->m_modes)
297 auxSOG_Kernel_i = m2.val;
298 if (auxSOG_Kernel_i.
cov(2, 2) == 0)
300 auxSOG_Kernel_i.
cov(2, 2) = 1;
304 auxSOG_Kernel_i.
cov(0, 0) > 0 && auxSOG_Kernel_i.
cov(1, 1) > 0);
307 bool reallyComputeThisOne =
true;
308 if (minMahalanobisDistToDrop > 0)
311 double stdX2 = max(auxSOG_Kernel_i.
cov(0, 0), m.val.cov(0, 0));
313 square(auxSOG_Kernel_i.
mean.
x() - m.val.mean.x()) / stdX2;
315 double stdY2 = max(auxSOG_Kernel_i.
cov(1, 1), m.val.cov(1, 1));
317 square(auxSOG_Kernel_i.
mean.
y() - m.val.mean.y()) / stdY2;
322 max(auxSOG_Kernel_i.
cov(2, 2), m.val.cov(2, 2));
324 square(auxSOG_Kernel_i.
mean.z() - m.val.mean.z()) /
328 reallyComputeThisOne = mahaDist2 < minMahalanobisDistToDrop2;
331 if (reallyComputeThisOne)
346 newKernel.
val = auxGaussianProduct;
349 Eigen::Vector3d eta_i =
351 eta_i = covInv_i.
asEigen() * eta_i;
354 Eigen::Vector3d new_eta_i =
356 new_eta_i = new_covInv_i.
asEigen() * new_eta_i;
359 -0.5 * (3 * log(
M_2PI) - log(new_covInv_i.
det()) +
360 (eta_i.transpose() * auxSOG_Kernel_i.
cov.
asEigen() *
363 -0.5 * (3 * log(
M_2PI) - log(new_covInv_i.
det()) +
364 (new_eta_i.transpose() *
367 newKernel.
log_w = m.log_w + m2.log_w + a + a_i - new_a_i;
370 if (is2D) newKernel.
val.
cov(2, 2) = 0;
373 this->m_modes.push_back(newKernel);
392 for (
auto& m_mode : m_modes)
394 m_mode.val.cov(0, 1) = m_mode.val.cov(1, 0);
395 m_mode.val.cov(0, 2) = m_mode.val.cov(2, 0);
396 m_mode.val.cov(1, 2) = m_mode.val.cov(2, 1);
409 if (!m_modes.size())
return;
411 CListGaussianModes::iterator it;
412 double maxW = m_modes[0].log_w;
413 for (it = m_modes.begin(); it != m_modes.end(); ++it)
414 maxW = max(maxW, it->log_w);
416 for (it = m_modes.begin(); it != m_modes.end(); ++it) it->log_w -= maxW;
427 CListGaussianModes::const_iterator it;
431 double sumLinearWeights = 0;
432 for (it = m_modes.begin(); it != m_modes.end(); ++it)
433 sumLinearWeights += exp(it->log_w);
436 for (it = m_modes.begin(); it != m_modes.end(); ++it)
437 cum +=
square(exp(it->log_w) / sumLinearWeights);
442 return 1.0 / (m_modes.size() * cum);
450 float x_min,
float x_max,
float y_min,
float y_max,
float resolutionXY,
451 float z,
CMatrixD& outMatrix,
bool sumOverAllZs)
459 const auto Nx = (size_t)ceil((x_max - x_min) / resolutionXY);
460 const auto Ny = (size_t)ceil((y_max - y_min) / resolutionXY);
463 for (
size_t i = 0; i < Ny; i++)
465 const float y = y_min + i * resolutionXY;
466 for (
size_t j = 0; j < Nx; j++)
468 float x = x_min + j * resolutionXY;
469 outMatrix(i, j) = evaluatePDF(
CPoint3D(x, y, z), sumOverAllZs);
489 for (
const auto& m_mode : m_modes)
500 CMatrixD X(2, 1), MU(2, 1), COV(2, 2);
506 for (
const auto& m_mode : m_modes)
508 MU(0, 0) = m_mode.val.mean.x();
509 MU(1, 0) = m_mode.val.mean.y();
511 COV(0, 0) = m_mode.val.cov(0, 0);
512 COV(1, 1) = m_mode.val.cov(1, 1);
513 COV(0, 1) = COV(1, 0) = m_mode.val.cov(0, 1);
533 auto it_best = m_modes.end();
534 for (
auto it = m_modes.begin(); it != m_modes.end(); ++it)
535 if (it_best == m_modes.end() || it->log_w > it_best->log_w)
538 outVal = it_best->val;
A namespace of pseudo-random numbers generators of diferent distributions.
void serializeSymmetricMatrixTo(MAT &m, mrpt::serialization::CArchive &out)
Binary serialization of symmetric matrices, saving the space of duplicated values.
This class is a "CSerializable" wrapper for "CMatrixDynamic<double>".
void getMean(CPoint3D &mean_point) const override
int void fclose(FILE *f)
An OS-independent version of fclose.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
CPoint3D mean
The mean value.
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
void resize(const size_t N)
Resize the number of SOG modes.
Declares a class that represents a Probability Density function (PDF) of a 3D point ...
The struct for each mode:
size_type size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
void clear()
Clear all the gaussian modes.
void drawSingleSample(CPoint3D &outSample) const override
Draw a sample from the pdf.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
#define ASSERT_(f)
Defines an assertion mechanism.
void bayesianFusion(const CPointPDFGaussian &p1, const CPointPDFGaussian &p2)
Bayesian fusion of two points gauss.
CMatrixFixed< double, 3, 3 > CMatrixDouble33
This base provides a set of functions for maths stuff.
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
CMatrixFixed< double, 3, 1 > CMatrixDouble31
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
mrpt::math::CMatrixDouble33 cov
The 3x3 covariance matrix.
void deserializeSymmetricMatrixFrom(MAT &m, mrpt::serialization::CArchive &in)
Binary serialization of symmetric matrices, saving the space of duplicated values.
void normalizeWeights()
Normalize the weights in m_modes such as the maximum log-weight is 0.
Scalar det() const
Determinant of matrix.
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
double x() const
Common members of all points & poses classes.
Derived inverse_LLt() const
Returns the inverse of a symmetric matrix using LLt.
std::tuple< cov_mat_t, type_value > getCovarianceAndMean() const override
Returns an estimate of the pose covariance matrix (STATE_LENxSTATE_LEN cov matrix) and the mean...
A class used to store a 3D point.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
return_t square(const num_t x)
Inline function for the square of a number.
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
Virtual base class for "archives": classes abstracting I/O streams.
void copyFrom(const CPointPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
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...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
mrpt::vision::TStereoCalibResults out
This file implements matrix/vector text and binary serialization.
double ESS() const
Computes the "Effective sample size" (typical measure for Particle Filters), applied to the weights o...
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
EIGEN_MAP asEigen()
Get as an Eigen-compatible Eigen::Map object.
void evaluatePDFInArea(float x_min, float x_max, float y_min, float y_max, float resolutionXY, float z, mrpt::math::CMatrixD &outMatrix, bool sumOverAllZs=false)
Evaluates the PDF within a rectangular grid and saves the result in a matrix (each row contains value...
double log_w
The log-weight.
void getMostLikelyMode(CPointPDFGaussian &outVal) const
Return the Gaussian mode with the highest likelihood (or an empty Gaussian if there are no modes in t...
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
void enforceCovSymmetry()
Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Declares a class that represents a Probability Distribution function (PDF) of a 3D point (x...
double normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
double evaluatePDF(const CPoint3D &x, bool sumOverAllZs) const
Evaluates the PDF at a given point.
bool saveToTextFile(const std::string &file) const override
Save the density to a text file, with the following format: There is one row per Gaussian "mode"...
A gaussian distribution for 3D points.
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!)