68 const double w = exp((it)->log_w);
83 size_t N = m_modes.size();
85 this->getMean(estMean2D);
99 sumW +=
w = exp((it)->log_w);
102 estMean_i -= estMeanMat;
104 temp.multiply_AAt(estMean_i);
112 estCov *= (1.0/sumW);
132 out << (it)->
cov(0,0) << (it)->
cov(1,1) << (it)->
cov(2,2);
133 out << (it)->
cov(0,1) << (it)->
cov(0,2) << (it)->
cov(1,2);
157 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
162 if (
version==0) (it)->log_w = log(max(1e-300,(it)->log_w));
178 in >> x0; (it)->
cov(0,0) = x0;
179 in >> x0; (it)->
cov(1,1) = x0;
180 in >> x0; (it)->
cov(2,2) = x0;
182 in >> x0; (it)->
cov(1,0) = x0; (it)->
cov(0,1) = x0;
183 in >> x0; (it)->
cov(2,0) = x0; (it)->
cov(0,2) = x0;
184 in >> x0; (it)->
cov(1,2) = x0; (it)->
cov(2,1) = x0;
200 if (
this == &o)
return;
204 m_modes =
static_cast<const CPosePDFSOG*
>(&o)->m_modes;
210 m_modes[0].log_w = 0;
231 (it)->
cov(0,0),(it)->
cov(1,1),(it)->
cov(2,2),
232 (it)->
cov(0,1),(it)->
cov(0,2),(it)->
cov(1,2) );
250 M(0,2) = 0; M(1,2) = 0;
251 M(2,0) = 0; M(2,1) = 0;
254 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
257 (it)->
mean.composeFrom(newReferenceBase, (it)->mean);
273 rot(0,0)=rot(1,1)=cos(ang);
278 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
300 std::vector<CVectorDouble> &outSamples )
const 340 double a = -0.5*( 3*log(
M_2PI) - log( covInv.det() ) + (eta.adjoint() * p2->
cov * eta)(0,0) );
342 this->m_modes.clear();
343 for (
const_iterator it =p1->m_modes.begin();it!=p1->m_modes.end();++it)
358 newKernel.
mean = auxGaussianProduct.
mean;
359 newKernel.
cov = auxGaussianProduct.
cov;
362 auxSOG_Kernel_i.
cov.inv(covInv_i);
365 eta_i = covInv_i * eta_i;
368 newKernel.
cov.inv(new_covInv_i);
371 new_eta_i = new_covInv_i * new_eta_i;
373 double a_i = -0.5*( 3*log(
M_2PI) - log( new_covInv_i.det() ) + (eta_i.adjoint() * auxSOG_Kernel_i.
cov * eta_i)(0,0) );
374 double new_a_i = -0.5*( 3*log(
M_2PI) - log( new_covInv_i.det() ) + (new_eta_i.adjoint() * newKernel.
cov * new_eta_i)(0,0) );
377 newKernel.
log_w = (it)->log_w +
a + a_i - new_a_i;
380 this->m_modes.push_back(newKernel );
399 out->
m_modes.resize(m_modes.size());
401 for (itSrc=m_modes.begin(),itDest=out->m_modes.begin();itSrc!=m_modes.end();++itSrc,++itDest)
404 (itDest)->
mean = -(itSrc)->mean;
407 (itDest)->
cov = (itSrc)->cov;
417 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
418 (it)->mean = (it)->
mean + Ap;
420 this->rotateAllCovariances( Ap.
phi() );
428 bool sumOverAllPhis )
const 456 MU(0,0) = (it)->
mean.x();
457 MU(1,0) = (it)->
mean.y();
459 COV(0,0) = (it)->
cov(0,0);
460 COV(1,1) = (it)->
cov(1,1);
461 COV(0,1) = COV(1,0) = (it)->
cov(0,1);
495 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
497 (it)->
cov(0,1) = (it)->
cov(1,0);
498 (it)->
cov(0,2) = (it)->
cov(2,0);
499 (it)->
cov(1,2) = (it)->
cov(2,1);
510 if (!m_modes.size())
return;
512 double maxW = m_modes[0].log_w;
513 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
514 maxW = max(maxW,(it)->log_w);
516 for (
iterator it=m_modes.begin();it!=m_modes.end();++it)
526 const double & x_min,
527 const double & x_max,
528 const double & y_min,
529 const double & y_max,
530 const double & resolutionXY,
533 bool sumOverAllPhis )
541 const size_t Nx = (size_t)ceil((x_max-x_min)/resolutionXY);
542 const size_t Ny = (size_t)ceil((y_max-y_min)/resolutionXY);
544 outMatrix.setSize(Ny,Nx);
546 for (
size_t i=0;i<Ny;i++)
548 double y = y_min + i*resolutionXY;
549 for (
size_t j=0;j<Nx;j++)
551 double x = x_min + j*resolutionXY;
552 outMatrix(i,j) = evaluatePDF(
CPose2D(
x,
y,phi),sumOverAllPhis);
569 size_t N = m_modes.size();
577 for (
size_t i=0;i<(N-1); )
584 for (
size_t j=0;j<N;j++)
585 sumW += exp(m_modes[j].log_w);
588 const double Wi = exp(m_modes[i].log_w) / sumW;
590 double min_Bij = std::numeric_limits<double>::max();
598 for (
size_t j=0;j<N;j++)
601 const double Wj = exp(m_modes[j].log_w) / sumW;
602 const double Wij_ = 1.0/(Wi+Wj);
605 Pij.add_Ac(m_modes[j].
cov, Wj*Wij_ );
613 AUX.multiply_AAt( MUij );
615 AUX *= Wi*Wj*Wij_*Wij_;
618 double Bij = (Wi+Wj)*log( Pij.det() ) - Wi*log(m_modes[i].
cov.det()) - Wj*log(m_modes[j].
cov.det());
621 cout <<
"try merge[" << i <<
", " << j <<
"] -> Bij: " << Bij << endl;
624 cout <<
"Pij: " << Pij << endl <<
" Pi: " << m_modes[i].cov << endl <<
" Pj: " << m_modes[j].cov << endl;
637 cout <<
"merge[" << i <<
", " << best_j <<
"] Tempting merge: KLd = " << min_Bij;
642 cout <<
" Accepted." << endl;
653 const double Wj = exp(Mj.
log_w) / sumW;
654 const double Wij_ = 1.0/(Wi+Wj);
655 const double Wi_ = Wi*Wij_;
656 const double Wj_ = Wj*Wij_;
664 Mij.
cov = min_Bij_COV;
668 m_modes.erase( m_modes.begin() + best_j );
673 cout <<
" Nope." << endl;
690 double best_log_w = -std::numeric_limits<double>::max();
694 if (i->log_w>best_log_w)
696 best_log_w = i->log_w;
703 mean_point = it_best->mean;
Computes weighted and un-weighted averages of SE(2) poses.
void normalizeWeights()
Normalize the weights in m_modes such as the maximum log-weight is 0.
double x() const
Common members of all points & poses classes.
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
void clear()
Clear the list of modes.
void mergeModes(double max_KLd=0.5, bool verbose=false)
Merge very close modes so the overall number of modes is reduced while preserving the total distribut...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
CPose2D mean
The mean value.
void bayesianFusion(const CPosePDF &p1, const CPosePDF &p2, const double &minMahalanobisDistToDrop=0) MRPT_OVERRIDE
Bayesian fusion of two points gauss.
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
CMatrixFixedNumeric< double, 3, 1 > CMatrixDouble31
The struct for each mode:
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fclose.
double evaluatePDF(const mrpt::poses::CPose2D &x, bool sumOverAllPhis=false) const
Evaluates the PDF at a given point.
#define THROW_EXCEPTION(msg)
EIGEN_STRONG_INLINE iterator begin()
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
void getCovariance(mrpt::math::CMatrixDouble &cov) const
Returns the estimate of the covariance matrix (STATE_LEN x STATE_LEN covariance matrix) ...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
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 1x3 vectors, where each row contains a (x,y,phi) datum.
void append(const mrpt::poses::CPose2D &p)
Adds a new pose to the computation.
IMPLEMENTS_SERIALIZABLE(CLogFileRecord_FullEval, CHolonomicLogFileRecord, mrpt::nav) IMPLEMENTS_SERIALIZABLE(CHolonomicFullEval
void getMean(CPose2D &mean_pose) const MRPT_OVERRIDE
Returns an estimate of the pose, (the mean, or mathematical expectation of the PDF) ...
mrpt::math::CMatrixDouble33 cov
The 3x3 covariance matrix.
void inverse(CPosePDF &o) const MRPT_OVERRIDE
Returns a new PDF such as: NEW_PDF = (0,0,0) - THIS_PDF.
void rotateAllCovariances(const double &ang)
Rotate all the covariance matrixes by replacing them by , where .
GLubyte GLubyte GLubyte GLubyte w
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
void wrapToPiInPlace(T &a)
Modifies the given angle to translate it into the ]-pi,pi] range.
This base provides a set of functions for maths stuff.
virtual const mrpt::utils::TRuntimeClassId * GetRuntimeClass() const
Returns information about the class of an object in runtime.
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
#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...
void evaluatePDFInArea(const double &x_min, const double &x_max, const double &y_min, const double &y_max, const double &resolutionXY, const double &phi, mrpt::math::CMatrixD &outMatrix, bool sumOverAllPhis=false)
Evaluates the PDF within a rectangular grid (and a fixed orientation) and saves the result in a matri...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void changeCoordinatesReference(const CPose3D &newReferenceBase) MRPT_OVERRIDE
this = p (+) this.
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
mrpt::math::CMatrixDouble33 cov
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...
CMatrixFixedNumeric< double, 3, 3 > CMatrixDouble33
virtual void getMean(TDATA &mean_point) const =0
Returns the mean, or mathematical expectation of the probability density distribution (PDF)...
GLsizei const GLchar ** string
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...
double evaluateNormalizedPDF(const mrpt::poses::CPose2D &x) const
Evaluates the ratio PDF(x) / max_PDF(x*), that is, the normalized PDF in the range [0...
void assureSymmetry()
Ensures the symmetry of the covariance matrix (eventually certain operations in the math-coprocessor ...
void copyFrom(const CPosePDF &o) MRPT_OVERRIDE
Copy operator, translating if necesary (for example, between particles and gaussian representations) ...
#define CLASS_ID(class_name)
Access to runtime class ID for a defined class name.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void getMostLikelyCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPose2D &mean_point) const
For the most likely Gaussian mode in the SOG, returns the pose covariance matrix (3x3 cov matrix) and...
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
const double & phi() const
Get the phi angle of the 2D pose (in radians)
void getHomogeneousMatrix(mrpt::math::CMatrixDouble44 &out_HM) const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPose2D &mean_point) const MRPT_OVERRIDE
Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once...
void bayesianFusion(const CPosePDF &p1, const CPosePDF &p2, const double &minMahalanobisDistToDrop=0) MRPT_OVERRIDE
Bayesian fusion of two pose distributions, then save the result in this object (WARNING: Currently p1...
CListGaussianModes::iterator iterator
void operator+=(const mrpt::poses::CPose2D &Ap)
Makes: thisPDF = thisPDF + Ap, where "+" is pose composition (both the mean, and the covariance matri...
unsigned __int32 uint32_t
double BASE_IMPEXP normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
double log_w
The log-weight.
GLubyte GLubyte GLubyte a
void saveToTextFile(const std::string &file) const MRPT_OVERRIDE
Save the density to a text file, with the following format: There is one row per Gaussian "mode"...
CListGaussianModes::const_iterator const_iterator
void resize(const size_t N)
Resize the number of SOG modes.
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
CListGaussianModes m_modes
The list of SOG modes.
void get_average(mrpt::poses::CPose2D &out_mean) const
Returns the average pose.
void drawSingleSample(CPose2D &outPart) const MRPT_OVERRIDE
Draws a single sample from the distribution.