OSVR-Core
SigmaPointGenerator.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_SigmaPointGenerator_h_GUID_1277DE61_21A1_42BD_0401_78E74169E598
26 #define INCLUDED_SigmaPointGenerator_h_GUID_1277DE61_21A1_42BD_0401_78E74169E598
27 
28 // Internal Includes
30 
31 // Library/third-party includes
32 #include <Eigen/Cholesky>
33 
34 // Standard includes
35 #include <cstddef>
36 
37 namespace osvr {
38 namespace kalman {
39  inline double computeNu(std::size_t L, double alpha) {
40  auto lambda = (L * (alpha * alpha - 1));
41  auto nu = std::sqrt(L + lambda);
42  return nu;
43  }
50  SigmaPointParameters(double alpha_ = 0.001, double beta_ = 2.,
51  double kappa_ = 0.)
52  : alpha(alpha_), beta(beta_), kappa(kappa_) {}
55  double alpha;
58  double beta;
61  double kappa;
62  };
65  std::size_t L)
66  : alphaSquared(p.alpha * p.alpha),
67  lambda(alphaSquared * (L + p.kappa) - L),
68  gamma(std::sqrt(L + lambda)), weightMean0(lambda / (L + lambda)),
69  weightCov0(weightMean0 + p.beta + 1 - alphaSquared),
70  weight(1. / (2. * (L + lambda))) {}
71 
72  private:
73  double alphaSquared;
74 
75  public:
77  double lambda;
79  double gamma;
80 
82  double weightMean0;
84  double weightCov0;
86  double weight;
87  };
88 
89  template <std::size_t Dim, std::size_t OrigDim = Dim>
91  public:
92  static_assert(OrigDim <= Dim, "Original, non-augmented dimension must "
93  "be equal or less than the full "
94  "dimension");
95  static const std::size_t L = Dim;
96  static const std::size_t OriginalDimension = OrigDim;
97  static const std::size_t NumSigmaPoints = L * 2 + 1;
102 
104  SigmaPointParameters params)
105  : p_(params, L), mean_(mean), covariance_(cov) {
106  weights_ = SigmaPointWeightVec::Constant(p_.weight);
107  weightsForCov_ = weights_;
108  weights_[0] = p_.weightMean0;
109  weightsForCov_[0] = p_.weightCov0;
110  scaledMatrixSqrt_ = cov.llt().matrixL();
112  sigmaPoints_ << mean,
113  (p_.gamma * scaledMatrixSqrt_).colwise() + mean,
114  (-p_.gamma * scaledMatrixSqrt_).colwise() + mean;
115  }
116 
117  SigmaPointsMat const &getSigmaPoints() const { return sigmaPoints_; }
118 
120  using ConstSigmaPointBlock =
122 
123  ConstSigmaPointBlock getSigmaPoint(std::size_t i) const {
124  return sigmaPoints_.template block<OrigDim, 1>(0, i);
125  }
126 
127  SigmaPointWeightVec const &getWeightsForMean() const {
128  return weights_;
129  }
130  SigmaPointWeightVec const &getWeightsForCov() const {
131  return weightsForCov_;
132  }
133 
134  MeanVec const &getMean() const { return mean_; }
135 
137 
139  ConstOrigMeanVec getOrigMean() const { return mean_.template head<OrigDim>(); }
140 
141  private:
143  MeanVec mean_;
144  CovMatrix covariance_;
145  CovMatrix scaledMatrixSqrt_;
146  SigmaPointsMat sigmaPoints_;
147  SigmaPointWeightVec weights_;
148  SigmaPointWeightVec weightsForCov_;
149  };
150 
151  template <std::size_t Dim>
153 
154  template <std::size_t XformedDim, typename SigmaPointsGenType>
156  public:
157  static const std::size_t DIMENSION = XformedDim;
158  using SigmaPointsGen = SigmaPointsGenType;
159  static const std::size_t NumSigmaPoints =
160  SigmaPointsGen::NumSigmaPoints;
161 
162  static const types::DimensionType OriginalDimension =
163  SigmaPointsGen::OriginalDimension;
166 
168 
172  SigmaPointsGen const &sigmaPoints,
173  TransformedSigmaPointsMat const &xformedPointsMat)
174  : xformedCov_(CovMat::Zero()), crossCov_(CrossCovMatrix::Zero()) {
176 #if 1
177  xformedMean_ = MeanVec::Zero();
178  for (std::size_t i = 0; i < NumSigmaPoints; ++i) {
179  auto weight = sigmaPoints.getWeightsForMean()[i];
180  xformedMean_ += weight * xformedPointsMat.col(i);
181  }
182 #else
183  xformedMean_ =
184  xformedPointsMat.rowwise() * sigmaPoints.getWeightsForMean();
185 #endif
186  TransformedSigmaPointsMat zeroMeanPoints =
187  xformedPointsMat.colwise() - xformedMean_;
188 
189  for (std::size_t i = 0; i < NumSigmaPoints; ++i) {
190  auto weight = sigmaPoints.getWeightsForCov()[i];
191  xformedCov_ += weight * zeroMeanPoints.col(i) *
192  zeroMeanPoints.col(i).transpose();
193  crossCov_ += weight * (sigmaPoints.getSigmaPoint(i) -
194  sigmaPoints.getOrigMean()) *
195  zeroMeanPoints.col(i).transpose();
196  }
197  }
198 
199  MeanVec const &getMean() const { return xformedMean_; }
200  CovMat const &getCov() const { return xformedCov_; }
201  // matrix of cross-covariance between original and transformed (such as
202  // state and measurement residuals)
203  CrossCovMatrix const &getCrossCov() const { return crossCov_; }
204 
205  private:
206  MeanVec xformedMean_;
207  CovMat xformedCov_;
208  CrossCovMatrix crossCov_;
209  };
210 } // namespace kalman
211 } // namespace osvr
212 
213 #endif // INCLUDED_SigmaPointGenerator_h_GUID_1277DE61_21A1_42BD_0401_78E74169E598
double alpha
double L; Primary scaling factor, typically in the range [1e-4, 1]
Definition: SigmaPointGenerator.h:55
AugmentedSigmaPointGenerator(MeanVec const &mean, CovMatrix const &cov, SigmaPointParameters params)
Definition: SigmaPointGenerator.h:103
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: SigmaPointGenerator.h:90
double weightCov0
Element 0 of weight vector for computing covariance.
Definition: SigmaPointGenerator.h:84
Expression of a fixed-size or dynamic-size sub-vector.
Definition: ForwardDeclarations.h:83
Definition: SigmaPointGenerator.h:155
double weightMean0
Element 0 of weight vector for computing means.
Definition: SigmaPointGenerator.h:82
double lambda
"Compound scaling factor"
Definition: SigmaPointGenerator.h:77
double gamma
Scales the matrix square root in computing sigma points.
Definition: SigmaPointGenerator.h:79
double kappa
Tertiary scaling factor, typically 0.
Definition: SigmaPointGenerator.h:61
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
double beta
Secondary scaling to emphasize the 0th sigma point in covariance weighting - 2 is optimal for gaussia...
Definition: SigmaPointGenerator.h:58
ConstOrigMeanVec getOrigMean() const
Get the "un-augmented" mean.
Definition: SigmaPointGenerator.h:139
std::size_t DimensionType
Type for dimensions.
Definition: FlexibleKalmanBase.h:51
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Definition: SigmaPointGenerator.h:63
double weight
Other elements of weight vector.
Definition: SigmaPointGenerator.h:86
ReconstructedDistributionFromSigmaPoints(SigmaPointsGen const &sigmaPoints, TransformedSigmaPointsMat const &xformedPointsMat)
Definition: SigmaPointGenerator.h:171
For further details on the scaling factors, refer to: Julier, S.
Definition: SigmaPointGenerator.h:49