g2o
Loading...
Searching...
No Matches
unscented.h
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef G2O_UNSCENTED_
28#define G2O_UNSCENTED_
29
30#include <Eigen/Cholesky>
31#include <Eigen/Core>
32#include <cassert>
33
34namespace g2o {
35
36template <class SampleType>
37struct SigmaPoint {
39 SigmaPoint(const SampleType& sample, double wi, double wp)
40 : _sample(sample), _wi(wi), _wp(wp) {}
41 SigmaPoint() : _wi(0), _wp(0) {}
42 SampleType _sample;
43 double _wi;
44 double _wp;
45};
46
47template <class SampleType, class CovarianceType>
48bool sampleUnscented(std::vector<SigmaPoint<SampleType>>& sigmaPoints,
49 const SampleType& mean, const CovarianceType& covariance) {
50 const int dim = mean.size();
51 const int numPoints = 2 * dim + 1;
52 assert(covariance.rows() == covariance.cols() &&
53 covariance.cols() == mean.size() && "Dimension Mismatch");
54 const double alpha = cst(1e-3);
55 const double beta = 2;
56 const double lambda = alpha * alpha * dim;
57 const double wi = cst(1) / (2 * (dim + lambda));
58
59 sigmaPoints.resize(numPoints);
60 sigmaPoints[0] = SigmaPoint<SampleType>(
61 mean, lambda / (dim + lambda),
62 lambda / (dim + lambda) + (1. - alpha * alpha + beta));
63 Eigen::LLT<CovarianceType> cholDecomp;
64 cholDecomp.compute(covariance * (dim + lambda));
65 if (cholDecomp.info() == Eigen::NumericalIssue) return false;
66 const CovarianceType& L = cholDecomp.matrixL();
67 int k = 1;
68 for (int i = 0; i < dim; i++) {
69 SampleType s(L.col(i));
70 sigmaPoints[k++] = SigmaPoint<SampleType>(mean + s, wi, wi);
71 sigmaPoints[k++] = SigmaPoint<SampleType>(mean - s, wi, wi);
72 }
73 return true;
74}
75
76template <class SampleType, class CovarianceType>
78 SampleType& mean, CovarianceType& covariance,
79 const std::vector<SigmaPoint<SampleType>>& sigmaPoints) {
80 mean.fill(0);
81 covariance.fill(0);
82 for (size_t i = 0; i < sigmaPoints.size(); i++) {
83 mean += sigmaPoints[i]._wi * sigmaPoints[i]._sample;
84 }
85 for (size_t i = 0; i < sigmaPoints.size(); i++) {
86 SampleType delta = sigmaPoints[i]._sample - mean;
87 covariance += sigmaPoints[i]._wp * (delta * delta.transpose());
88 }
89}
90} // namespace g2o
91
92#endif
void reconstructGaussian(SampleType &mean, CovarianceType &covariance, const std::vector< SigmaPoint< SampleType > > &sigmaPoints)
Definition unscented.h:77
constexpr double cst(long double v)
Definition misc.h:60
bool sampleUnscented(std::vector< SigmaPoint< SampleType > > &sigmaPoints, const SampleType &mean, const CovarianceType &covariance)
Definition unscented.h:48
SampleType _sample
Definition unscented.h:42
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition unscented.h:38
SigmaPoint(const SampleType &sample, double wi, double wp)
Definition unscented.h:39