dcsam
Factored inference for discrete-continuous smoothing and mapping
SemanticBearingRangeFactor.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <gtsam/geometry/BearingRange.h>
12 #include <gtsam/geometry/Point2.h>
13 #include <gtsam/geometry/Point3.h>
14 #include <gtsam/geometry/Pose2.h>
15 #include <gtsam/geometry/Pose3.h>
16 #include <gtsam/nonlinear/NonlinearFactor.h>
17 #include <gtsam/nonlinear/Symbol.h>
18 #include <gtsam/sam/BearingRangeFactor.h>
19 #include <math.h>
20 
21 #include <algorithm>
22 #include <limits>
23 #include <type_traits>
24 #include <vector>
25 
26 #include "DCFactor.h"
27 
28 namespace dcsam {
29 
34 template <typename PoseType, typename PointType,
35  typename BearingType =
36  typename gtsam::Bearing<PoseType, PointType>::result_type,
37  typename RangeType =
38  typename gtsam::Range<PoseType, PointType>::result_type>
40  private:
42 
43  gtsam::BearingRangeFactor<PoseType, PointType> factor_;
44  std::vector<double> probs_;
45 
46  public:
47  using Base = DCFactor;
48 
50 
51  SemanticBearingRangeFactor(const gtsam::Key& poseKey,
52  const gtsam::Key& pointKey,
53  const gtsam::DiscreteKey& discreteKey,
54  const std::vector<double> measuredProbs,
55  const BearingType& measuredBearing,
56  const RangeType& measuredRange,
57  const gtsam::SharedNoiseModel& model)
58  : probs_(measuredProbs),
59  factor_(poseKey, pointKey, measuredBearing, measuredRange, model) {
60  gtsam::KeyVector keys{poseKey, pointKey};
61  gtsam::DiscreteKeys dks(discreteKey);
62  keys_ = keys;
63  discreteKeys_ = dks;
64  gtsam::BearingRangeFactor<PoseType, PointType> brfactor;
65  }
66 
67  virtual ~SemanticBearingRangeFactor() = default;
68 
70  Base::operator=(rhs);
71  this->factor_ = rhs.factor_;
72  this->probs_ = rhs.probs_;
73  this->keys_ = rhs.keys_;
74  this->discreteKeys_ = rhs.discreteKeys_;
75  return *this;
76  }
77 
78  // Error is the sum of the continuous and discrete negative
79  // log-likelihoods
80  double error(const gtsam::Values& continuousVals,
81  const DiscreteValues& discreteVals) const override {
82  size_t assignment = discreteVals.at(discreteKeys_[0].first);
83  double discrete_error = log(probs_[assignment]);
84 
85  // Subtraction because -log(p(A,B)) = -log p(A)p(B) = -log p(A) - log p(B)
86  return factor_.error(continuousVals) - discrete_error;
87  }
88 
89  // dim is the dimension of the underlying bearingrange factor
90  size_t dim() const override { return factor_.dim(); }
91 
92  boost::shared_ptr<gtsam::GaussianFactor> linearize(
93  const gtsam::Values& continuousVals,
94  const DiscreteValues& discreteVals) const override {
95  return factor_.linearize(continuousVals);
96  }
97 
98  bool equals(const DCFactor& other, double tol = 1e-9) const override {
99  // We attempt a dynamic cast from DCFactor to SemanticBearingRangeFactor.
100  // If it fails, return false.
101  if (!dynamic_cast<const SemanticBearingRangeFactor*>(&other)) return false;
102 
103  // If the cast is successful, we'll properly construct a
104  // SemanticBearingRangeFactor object from `other`
106  static_cast<const SemanticBearingRangeFactor&>(other));
107 
108  // compare the bearingrange factors
109  if (!(factor_.equals(f.factor_, tol))) return false;
110 
111  // If everything above passes, and the keys_, discreteKeys_ and probs_
112  // variables are identical, return true.
113  return (std::equal(keys_.begin(), keys_.end(), f.keys().begin()) &&
114  (discreteKeys_ == f.discreteKeys_) && (probs_ == f.probs_));
115  }
116 
117  double logNormalizingConstant(const gtsam::Values& values) const override {
118  return nonlinearFactorLogNormalizingConstant(this->factor_, values);
119  }
120 };
121 
122 } // namespace dcsam
Custom discrete-continuous factor.
Abstract class implementing a discrete-continuous factor.
Definition: DCFactor.h:33
gtsam::DiscreteKeys discreteKeys_
Definition: DCFactor.h:36
DCFactor & operator=(const DCFactor &rhs)
Definition: DCFactor.h:58
gtsam::Factor Base
Definition: DCFactor.h:39
double nonlinearFactorLogNormalizingConstant(const NonlinearFactorType &factor, const gtsam::Values &values) const
Definition: DCFactor.h:193
DCFactor()=default
Factor that represents bearing and range measurements that incorporate a semantic class measurement.
Definition: SemanticBearingRangeFactor.h:39
boost::shared_ptr< gtsam::GaussianFactor > linearize(const gtsam::Values &continuousVals, const DiscreteValues &discreteVals) const override
Definition: SemanticBearingRangeFactor.h:92
virtual ~SemanticBearingRangeFactor()=default
size_t dim() const override
Definition: SemanticBearingRangeFactor.h:90
SemanticBearingRangeFactor & operator=(const SemanticBearingRangeFactor &rhs)
Definition: SemanticBearingRangeFactor.h:69
SemanticBearingRangeFactor(const gtsam::Key &poseKey, const gtsam::Key &pointKey, const gtsam::DiscreteKey &discreteKey, const std::vector< double > measuredProbs, const BearingType &measuredBearing, const RangeType &measuredRange, const gtsam::SharedNoiseModel &model)
Definition: SemanticBearingRangeFactor.h:51
double error(const gtsam::Values &continuousVals, const DiscreteValues &discreteVals) const override
Definition: SemanticBearingRangeFactor.h:80
bool equals(const DCFactor &other, double tol=1e-9) const override
Definition: SemanticBearingRangeFactor.h:98
double logNormalizingConstant(const gtsam::Values &values) const override
Definition: SemanticBearingRangeFactor.h:117
Definition: DCContinuousFactor.h:24
gtsam::DiscreteFactor::Values DiscreteValues
Definition: DCSAM_types.h:19
const double tol
Definition: testDCSAM.cpp:40