g2o
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/Core>
31 #include <Eigen/Cholesky>
32 #include<Eigen/StdVector>
33 
34 namespace g2o {
35 
36  template <class SampleType>
37  struct 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 
47 
48  template <class SampleType, class CovarianceType>
49  bool sampleUnscented(std::vector<SigmaPoint <SampleType>, Eigen::aligned_allocator<SigmaPoint <SampleType> > >& sigmaPoints, const SampleType& mean, const CovarianceType& covariance){
50 
51  const int dim = mean.size();
52  const int numPoints = 2 * dim + 1;
53  assert (covariance.rows() == covariance.cols() && covariance.cols() == mean.size() && "Dimension Mismatch");
54  const double alpha = 1e-3;
55  const double beta = 2.;
56  const double lambda = alpha * alpha * dim;
57  const double wi = 1./(2. * (dim + lambda) );
58 
59  sigmaPoints.resize(numPoints);
60  sigmaPoints[0] = SigmaPoint<SampleType>(mean,
61  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)
66  return false;
67  const CovarianceType& L=cholDecomp.matrixL();
68  int k=1;
69  for (int i=0; i<dim; i++) {
70  SampleType s(L.col(i));
71  sigmaPoints[k++]=SigmaPoint<SampleType>(mean + s, wi, wi);
72  sigmaPoints[k++]=SigmaPoint<SampleType>(mean - s, wi, wi);
73  }
74  return true;
75  }
76 
77  template <class SampleType, class CovarianceType>
78  void reconstructGaussian(SampleType& mean, CovarianceType& covariance,
79  const std::vector<SigmaPoint<SampleType>, Eigen::aligned_allocator<SigmaPoint <SampleType> > >& sigmaPoints){
80 
81  mean.fill(0);
82  covariance.fill(0);
83  for (size_t i=0; i<sigmaPoints.size(); i++){
84  mean += sigmaPoints[i]._wi * sigmaPoints[i]._sample;
85  }
86  for (size_t i=0; i<sigmaPoints.size(); i++){
87  SampleType delta = sigmaPoints[i]._sample - mean;
88  covariance += sigmaPoints[i]._wp * ( delta* delta.transpose() ) ;
89  }
90 
91  }
92 }
93 
94 #endif
SigmaPoint(const SampleType &sample, double wi, double wp)
Definition: unscented.h:39
void reconstructGaussian(SampleType &mean, CovarianceType &covariance, const std::vector< SigmaPoint< SampleType >, Eigen::aligned_allocator< SigmaPoint< SampleType > > > &sigmaPoints)
Definition: unscented.h:78
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: unscented.h:38
bool sampleUnscented(std::vector< SigmaPoint< SampleType >, Eigen::aligned_allocator< SigmaPoint< SampleType > > > &sigmaPoints, const SampleType &mean, const CovarianceType &covariance)
Definition: unscented.h:49
SampleType _sample
Definition: unscented.h:42