GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
InvDepthFactor3.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include <gtsam/geometry/Cal3_S2.h>
20 
21 namespace gtsam {
22 
26 template<class POSE, class LANDMARK, class INVDEPTH>
27 class InvDepthFactor3: public NoiseModelFactorN<POSE, LANDMARK, INVDEPTH> {
28 protected:
29 
30  // Keep a copy of measurement and calibration for I/O
32  std::shared_ptr<Cal3_S2> K_;
33 
34 public:
35 
37  typedef NoiseModelFactor3<POSE, LANDMARK, INVDEPTH> Base;
38 
39  // Provide access to the Matrix& version of evaluateError:
40  using Base::evaluateError;
41 
44 
46  typedef std::shared_ptr<This> shared_ptr;
47 
50  measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
51  }
52 
63  InvDepthFactor3(const Point2& measured, const SharedNoiseModel& model,
64  const Key poseKey, Key pointKey, Key invDepthKey, const Cal3_S2::shared_ptr& K) :
65  Base(model, poseKey, pointKey, invDepthKey), measured_(measured), K_(K) {}
66 
68  ~InvDepthFactor3() override {}
69 
75  void print(const std::string& s = "InvDepthFactor3",
76  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
77  Base::print(s, keyFormatter);
78  traits<Point2>::Print(measured_, s + ".z");
79  }
80 
82  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
83  const This *e = dynamic_cast<const This*>(&p);
84  return e && Base::equals(p, tol) && traits<Point2>::Equals(this->measured_, e->measured_, tol) && this->K_->equals(*e->K_, tol);
85  }
86 
88  Vector evaluateError(const POSE& pose, const Vector5& point, const INVDEPTH& invDepth,
89  OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override {
90  try {
91  InvDepthCamera3<Cal3_S2> camera(pose, K_);
92  return camera.project(point, invDepth, H1, H2, H3) - measured_;
93  } catch( CheiralityException& e) {
94  if (H1) *H1 = Matrix::Zero(2,6);
95  if (H2) *H2 = Matrix::Zero(2,5);
96  if (H3) *H3 = Matrix::Zero(2,1);
97  std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
98  " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
99  return Vector::Ones(2) * 2.0 * K_->fx();
100  }
101  return (Vector(1) << 0.0).finished();
102  }
103 
105  const Point2& imagePoint() const {
106  return measured_;
107  }
108 
110  inline const Cal3_S2::shared_ptr calibration() const {
111  return K_;
112  }
113 
114 private:
115 
116 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
117  friend class boost::serialization::access;
119  template<class ARCHIVE>
120  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
121  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
122  ar & BOOST_SERIALIZATION_NVP(measured_);
123  ar & BOOST_SERIALIZATION_NVP(K_);
124  }
125 #endif
126 };
127 } // \ namespace gtsam
NoiseModelFactor3< POSE, LANDMARK, INVDEPTH > Base
shorthand for base class type
Definition: InvDepthFactor3.h:37
Definition: NonlinearFactor.h:431
const Point2 & imagePoint() const
Definition: InvDepthFactor3.h:105
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
Vector2 Point2
Definition: Point2.h:32
Definition: Group.h:43
Definition: NonlinearFactor.h:68
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition: InvDepthFactor3.h:82
Matrix * OptionalMatrixType
Definition: NonlinearFactor.h:55
Definition: InvDepthFactor3.h:27
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
Inverse Depth Camera based on Civera09tro, Montiel06rss. Landmarks are initialized from the first cam...
Point2 measured_
2D measurement
Definition: InvDepthFactor3.h:31
The most common 5DOF 3D->2D calibration.
Definition: Cal3_S2.h:34
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: InvDepthFactor3.h:46
Definition: chartTesting.h:28
gtsam::Point2 project(const Vector5 &pw, double rho, OptionalJacobian< 2, 6 > H1={}, OptionalJacobian< 2, 5 > H2={}, OptionalJacobian< 2, 1 > H3={}) const
Definition: InvDepthCamera3.h:87
Non-linear factor base classes.
std::shared_ptr< Cal3_S2 > K_
shared pointer to calibration object
Definition: InvDepthFactor3.h:32
void print(const std::string &s="InvDepthFactor3", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: InvDepthFactor3.h:75
Definition: InvDepthCamera3.h:34
Definition: CalibratedCamera.h:34
InvDepthFactor3< POSE, LANDMARK, INVDEPTH > This
shorthand for this class
Definition: InvDepthFactor3.h:43
const Cal3_S2::shared_ptr calibration() const
Definition: InvDepthFactor3.h:110
Vector evaluateError(const POSE &pose, const Vector5 &point, const INVDEPTH &invDepth, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
Evaluate error h(x)-z and optionally derivatives.
Definition: InvDepthFactor3.h:88
InvDepthFactor3(const Point2 &measured, const SharedNoiseModel &model, const Key poseKey, Key pointKey, Key invDepthKey, const Cal3_S2::shared_ptr &K)
Definition: InvDepthFactor3.h:63
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
InvDepthFactor3()
Default constructor.
Definition: InvDepthFactor3.h:49
~InvDepthFactor3() override
Definition: InvDepthFactor3.h:68
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741
The most common 5DOF 3D->2D calibration.