betweenFactorMaxMix.h
Go to the documentation of this file.
1 /*
2  * betweenFactorMaxMix.h
3  *
4  * Created on: 14.08.2012
5  * Author: niko
6  */
7 
8 #ifndef BETWEENFACTORMAXMIX_H_
9 #define BETWEENFACTORMAXMIX_H_
10 
11 #include <gtsam/linear/NoiseModel.h>
12 #include <Eigen/Eigen>
13 #include <gtsam/config.h>
14 
15 #if GTSAM_VERSION_MAJOR > 4 || (GTSAM_VERSION_MAJOR==4 && GTSAM_VERSION_MINOR>=1)
16 namespace gtsam {
17 gtsam::Matrix inverse(const gtsam::Matrix & matrix)
18 {
19  return matrix.inverse();
20 }
21 }
22 #endif
23 
24 namespace vertigo {
25 
26  template<class VALUE>
27  class BetweenFactorMaxMix : public gtsam::NoiseModelFactor2<VALUE, VALUE>
28  {
29  public:
30  BetweenFactorMaxMix() : weight(0.0) {};
31  BetweenFactorMaxMix(gtsam::Key key1, gtsam::Key key2, const VALUE& measured, const gtsam::SharedNoiseModel& model, const gtsam::SharedNoiseModel& model2, double w)
32  : gtsam::NoiseModelFactor2<VALUE, VALUE>(model, key1, key2), weight(w), nullHypothesisModel(model2),
33  betweenFactor(key1, key2, measured, model) { };
34 
35  gtsam::Vector evaluateError(const VALUE& p1, const VALUE& p2,
36  boost::optional<gtsam::Matrix&> H1 = boost::none,
37  boost::optional<gtsam::Matrix&> H2 = boost::none) const
38  {
39 
40  // calculate error
41  gtsam::Vector error = betweenFactor.evaluateError(p1, p2, H1, H2);
42 
43 
44 
45  // which hypothesis is more likely
46  double m1 = this->noiseModel_->distance(error);
47  gtsam::noiseModel::Gaussian::shared_ptr g1 = this->noiseModel_;
48  gtsam::Matrix info1(g1->R().transpose()*g1->R());
49  double nu1 = 1.0/sqrt(gtsam::inverse(info1).determinant());
50  double l1 = nu1 * exp(-0.5*m1);
51 
52 #if GTSAM_VERSION_MAJOR > 4 || (GTSAM_VERSION_MAJOR==4 && GTSAM_VERSION_MINOR>=1)
53  double m2 = nullHypothesisModel->squaredMahalanobisDistance(error);
54 #else
55  double m2 = nullHypothesisModel->distance(error);
56 #endif
57  gtsam::noiseModel::Gaussian::shared_ptr g2 = nullHypothesisModel;
58  gtsam::Matrix info2(g2->R().transpose()*g2->R());
59  double nu2 = 1.0/sqrt(gtsam::inverse(info2).determinant());
60  double l2 = nu2 * exp(-0.5*m2);
61 
62  // if the null hypothesis is more likely, than proceed by applying the weight ...
63  if (l2>l1) {
64  if (H1) *H1 = *H1 * weight;
65  if (H2) *H2 = *H2 * weight;
66  error *= sqrt(weight);
67  }
68 
69  return error;
70  };
71 
72  private:
73  gtsam::BetweenFactor<VALUE> betweenFactor;
74  gtsam::SharedNoiseModel nullHypothesisModel;
75  double weight;
76 
77  };
78 }
79 
80 
81 #endif /* BETWEENFACTORMAXMIX_H_ */
GLM_FUNC_DECL vecType< T, P > sqrt(vecType< T, P > const &x)
GLM_FUNC_DECL T determinant(matType< T, P > const &m)
gtsam::BetweenFactor< VALUE > betweenFactor
gtsam::Vector evaluateError(const VALUE &p1, const VALUE &p2, boost::optional< gtsam::Matrix &> H1=boost::none, boost::optional< gtsam::Matrix &> H2=boost::none) const
gtsam::SharedNoiseModel nullHypothesisModel
GLM_FUNC_DECL genType exp(genType const &x)
BetweenFactorMaxMix(gtsam::Key key1, gtsam::Key key2, const VALUE &measured, const gtsam::SharedNoiseModel &model, const gtsam::SharedNoiseModel &model2, double w)
model
Definition: trace.py:4
GLM_FUNC_DECL matType< T, P > inverse(matType< T, P > const &m)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:27