50 size_t N = m_modes.size();
51 double X = 0, Y = 0, Z = 0;
58 for (it = m_modes.begin(); it != m_modes.end(); ++it)
61 sumW +=
w = exp(it->log_w);
62 X += it->val.mean.x() *
w;
63 Y += it->val.mean.y() *
w;
64 Z += it->val.mean.z() *
w;
85 size_t N = m_modes.size();
100 for (it = m_modes.begin(); it != m_modes.end(); ++it)
103 sumW +=
w = exp(it->log_w);
108 estMean_i -= estMean;
109 partCov.multiply_AAt(estMean_i);
110 partCov += it->val.cov;
115 if (sumW != 0) estCov *= (1.0 / sumW);
133 for (it = m_modes.begin(); it != m_modes.end(); ++it)
137 out << it->val.cov(0, 0) << it->val.cov(1, 1) << it->val.cov(2, 2);
138 out << it->val.cov(0, 1) << it->val.cov(0, 2) << it->val.cov(1, 2);
161 for (it = m_modes.begin(); it != m_modes.end(); ++it)
166 if (version == 0) it->log_w = log(max(1e-300, it->log_w));
171 it->val.cov(0, 0) =
x;
173 it->val.cov(1, 1) =
x;
175 it->val.cov(2, 2) =
x;
178 it->val.cov(1, 0) =
x;
179 it->val.cov(0, 1) =
x;
181 it->val.cov(2, 0) =
x;
182 it->val.cov(0, 2) =
x;
184 it->val.cov(1, 2) =
x;
185 it->val.cov(2, 1) =
x;
198 if (
this == &o)
return;
202 m_modes =
static_cast<const CPointPDFSOG*
>(&o)->m_modes;
208 m_modes[0].log_w = 0;
224 it != m_modes.end(); ++it)
226 f,
"%e %e %e %e %e %e %e %e %e %e\n", exp(it->log_w),
227 it->val.mean.x(), it->val.mean.y(), it->val.mean.z(),
228 it->val.cov(0, 0), it->val.cov(1, 1), it->val.cov(2, 2),
229 it->val.cov(0, 1), it->val.cov(0, 2), it->val.cov(1, 2));
240 it->val.changeCoordinatesReference(newReferenceBase);
253 vector<double> logWeights(m_modes.size());
254 vector<size_t> outIdxs;
257 for (it = m_modes.begin(), itW = logWeights.begin(); it != m_modes.end();
261 CParticleFilterCapable::computeResampling(
262 CParticleFilter::prMultinomial,
268 size_t selectedIdx = outIdxs[0];
269 ASSERT_(selectedIdx < m_modes.size());
277 outSample.
x(selMode->
mean.
x() + vec[0]);
278 outSample.
y(selMode->
mean.
y() + vec[1]);
279 outSample.z(selMode->
mean.z() + vec[2]);
289 const double& minMahalanobisDistToDrop)
305 float minMahalanobisDistToDrop2 =
square(minMahalanobisDistToDrop);
307 this->m_modes.clear();
312 it1 != p1->
m_modes.end(); ++it1)
317 if (
c.get_unsafe(2, 2) == 0)
320 c.set_unsafe(2, 2, 1);
332 double a = -0.5 * (3 * log(
M_2PI) - log(covInv.det()) +
333 eta.multiply_HtCH_scalar(
337 it2 != p2->
m_modes.end(); ++it2)
339 auxSOG_Kernel_i = (*it2).val;
340 if (auxSOG_Kernel_i.
cov.get_unsafe(2, 2) == 0)
342 auxSOG_Kernel_i.
cov.set_unsafe(2, 2, 1);
346 auxSOG_Kernel_i.
cov(0, 0) > 0 && auxSOG_Kernel_i.
cov(1, 1) > 0)
349 bool reallyComputeThisOne =
true;
350 if (minMahalanobisDistToDrop > 0)
356 max(auxSOG_Kernel_i.
cov.get_unsafe(0, 0),
357 (*it1).val.cov.get_unsafe(0, 0));
359 square(auxSOG_Kernel_i.
mean.
x() - (*it1).val.mean.x()) /
363 max(auxSOG_Kernel_i.
cov.get_unsafe(1, 1),
364 (*it1).val.cov.get_unsafe(1, 1));
366 square(auxSOG_Kernel_i.
mean.
y() - (*it1).val.mean.y()) /
372 max(auxSOG_Kernel_i.
cov.get_unsafe(2, 2),
373 (*it1).val.cov.get_unsafe(2, 2));
375 square(auxSOG_Kernel_i.
mean.z() - (*it1).val.mean.z()) /
379 reallyComputeThisOne = mahaDist2 < minMahalanobisDistToDrop2;
382 if (reallyComputeThisOne)
397 newKernel.
val = auxGaussianProduct;
401 eta_i = covInv_i * eta_i;
405 new_eta_i = new_covInv_i * new_eta_i;
409 (3 * log(
M_2PI) - log(new_covInv_i.det()) +
410 (eta_i.adjoint() * auxSOG_Kernel_i.
cov * eta_i)(0, 0));
412 -0.5 * (3 * log(
M_2PI) - log(new_covInv_i.det()) +
413 (new_eta_i.adjoint() * newKernel.
val.
cov *
417 (it1)->log_w + (it2)->log_w +
a + a_i - new_a_i;
420 if (is2D) newKernel.
val.
cov(2, 2) = 0;
423 this->m_modes.push_back(newKernel);
445 it->val.cov(0, 1) = it->val.cov(1, 0);
446 it->val.cov(0, 2) = it->val.cov(2, 0);
447 it->val.cov(1, 2) = it->val.cov(2, 1);
460 if (!m_modes.size())
return;
463 double maxW = m_modes[0].log_w;
464 for (it = m_modes.begin(); it != m_modes.end(); ++it)
465 maxW = max(maxW, it->log_w);
467 for (it = m_modes.begin(); it != m_modes.end(); ++it) it->log_w -= maxW;
482 double sumLinearWeights = 0;
483 for (it = m_modes.begin(); it != m_modes.end(); ++it)
484 sumLinearWeights += exp(it->log_w);
487 for (it = m_modes.begin(); it != m_modes.end(); ++it)
488 cum +=
square(exp(it->log_w) / sumLinearWeights);
493 return 1.0 / (m_modes.size() * cum);
501 float x_min,
float x_max,
float y_min,
float y_max,
float resolutionXY,
502 float z,
CMatrixD& outMatrix,
bool sumOverAllZs)
510 const size_t Nx = (size_t)ceil((x_max - x_min) / resolutionXY);
511 const size_t Ny = (size_t)ceil((y_max - y_min) / resolutionXY);
512 outMatrix.setSize(Ny, Nx);
514 for (
size_t i = 0; i < Ny; i++)
516 const float y = y_min + i * resolutionXY;
517 for (
size_t j = 0; j < Nx; j++)
519 float x = x_min + j * resolutionXY;
520 outMatrix(i, j) = evaluatePDF(
CPoint3D(
x,
y,
z), sumOverAllZs);
541 it != m_modes.end(); ++it)
552 CMatrixD X(2, 1), MU(2, 1), COV(2, 2);
559 it != m_modes.end(); ++it)
561 MU(0, 0) = it->val.mean.x();
562 MU(1, 0) = it->val.mean.y();
564 COV(0, 0) = it->val.cov(0, 0);
565 COV(1, 1) = it->val.cov(1, 1);
566 COV(0, 1) = COV(1, 0) = it->val.cov(0, 1);
587 for (
const_iterator it = m_modes.begin(); it != m_modes.end(); ++it)
588 if (it_best == m_modes.end() || it->log_w > it_best->log_w)
591 outVal = it_best->val;
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
A class used to store a 3D point.
A gaussian distribution for 3D points.
mrpt::math::CMatrixDouble33 cov
The 3x3 covariance matrix.
void bayesianFusion(const CPointPDFGaussian &p1, const CPointPDFGaussian &p2)
Bayesian fusion of two points gauss.
CPoint3D mean
The mean value.
Declares a class that represents a Probability Distribution function (PDF) of a 3D point (x,...
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
Declares a class that represents a Probability Density function (PDF) of a 3D point .
void copyFrom(const CPointPDF &o) override
Copy operator, translating if necesary (for example, between particles and gaussian representations)
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...
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...
CListGaussianModes m_modes
The list of SOG modes.
void changeCoordinatesReference(const CPose3D &newReferenceBase) override
this = p (+) this.
void getMean(CPoint3D &mean_point) const override
Returns an estimate of the point, (the mean, or mathematical expectation of the PDF)
std::deque< TGaussianMode >::const_iterator const_iterator
void 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",...
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.
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),...
void drawSingleSample(CPoint3D &outSample) const override
Draw a sample from the pdf.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
double ESS() const
Computes the "Effective sample size" (typical measure for Particle Filters), applied to the weights o...
void normalizeWeights()
Normalize the weights in m_modes such as the maximum log-weight is 0.
double evaluatePDF(const CPoint3D &x, bool sumOverAllZs) const
Evaluates the PDF at a given point.
void resize(const size_t N)
Resize the number of SOG modes.
void getMostLikelyMode(CPointPDFGaussian &outVal) const
Return the Gaussian mode with the highest likelihood (or an empty Gaussian if there are no modes in t...
void assureSymmetry()
Assures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
void clear()
Clear all the gaussian modes.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
double x() const
Common members of all points & poses classes.
Declares a class that represents a probability density function (pdf) of a 2D pose (x,...
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.
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,...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
EIGEN_STRONG_INLINE bool empty() const
const Scalar * const_iterator
GLubyte GLubyte GLubyte GLubyte w
GLubyte GLubyte GLubyte a
GLsizei const GLchar ** string
int void fclose(FILE *f)
An OS-independent version of fclose.
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
double normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
This base provides a set of functions for maths stuff.
CMatrixFixedNumeric< double, 3, 1 > CMatrixDouble31
T square(const T x)
Inline function for the square of a number.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
A namespace of pseudo-random numbers genrators of diferent distributions.
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
unsigned __int32 uint32_t
The struct for each mode:
double log_w
The log-weight.