ProjectionFactor.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
21 #pragma once
22 
25 #include <gtsam/geometry/Cal3_S2.h>
26 #include <boost/optional.hpp>
27 
28 namespace gtsam {
29 
35  template<class POSE, class LANDMARK, class CALIBRATION = Cal3_S2>
36  class GenericProjectionFactor: public NoiseModelFactor2<POSE, LANDMARK> {
37  protected:
38 
39  // Keep a copy of measurement and calibration for I/O
41  boost::shared_ptr<CALIBRATION> K_;
42  boost::optional<POSE> body_P_sensor_;
43 
44  // verbosity handling for Cheirality Exceptions
47 
48  public:
49 
52 
55 
57  typedef boost::shared_ptr<This> shared_ptr;
58 
61  measured_(0, 0), throwCheirality_(false), verboseCheirality_(false) {
62  }
63 
75  Key poseKey, Key pointKey, const boost::shared_ptr<CALIBRATION>& K,
76  boost::optional<POSE> body_P_sensor = boost::none) :
77  Base(model, poseKey, pointKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
78  throwCheirality_(false), verboseCheirality_(false) {}
79 
93  Key poseKey, Key pointKey, const boost::shared_ptr<CALIBRATION>& K,
95  boost::optional<POSE> body_P_sensor = boost::none) :
96  Base(model, poseKey, pointKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
97  throwCheirality_(throwCheirality), verboseCheirality_(verboseCheirality) {}
98 
101 
104  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
106 
112  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
113  std::cout << s << "GenericProjectionFactor, z = ";
114  traits<Point2>::Print(measured_);
115  if(this->body_P_sensor_)
116  this->body_P_sensor_->print(" sensor pose in body frame: ");
117  Base::print("", keyFormatter);
118  }
119 
121  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
122  const This *e = dynamic_cast<const This*>(&p);
123  return e
124  && Base::equals(p, tol)
125  && traits<Point2>::Equals(this->measured_, e->measured_, tol)
126  && this->K_->equals(*e->K_, tol)
127  && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
128  }
129 
132  boost::optional<Matrix&> H1 = boost::none, boost::optional<Matrix&> H2 = boost::none) const override {
133  try {
134  if(body_P_sensor_) {
135  if(H1) {
136  gtsam::Matrix H0;
137  PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_, H0), *K_);
138  Point2 reprojectionError(camera.project(point, H1, H2, boost::none) - measured_);
139  *H1 = *H1 * H0;
140  return reprojectionError;
141  } else {
142  PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_), *K_);
143  return camera.project(point, H1, H2, boost::none) - measured_;
144  }
145  } else {
147  return camera.project(point, H1, H2, boost::none) - measured_;
148  }
149  } catch( CheiralityException& e) {
150  if (H1) *H1 = Matrix::Zero(2,6);
151  if (H2) *H2 = Matrix::Zero(2,3);
152  if (verboseCheirality_)
153  std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
154  " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
155  if (throwCheirality_)
156  throw CheiralityException(this->key2());
157  }
158  return Vector2::Constant(2.0 * K_->fx());
159  }
160 
162  const Point2& measured() const {
163  return measured_;
164  }
165 
167  inline const boost::shared_ptr<CALIBRATION> calibration() const {
168  return K_;
169  }
170 
172  inline bool verboseCheirality() const { return verboseCheirality_; }
173 
175  inline bool throwCheirality() const { return throwCheirality_; }
176 
177  private:
178 
181  template<class ARCHIVE>
182  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
183  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
184  ar & BOOST_SERIALIZATION_NVP(measured_);
185  ar & BOOST_SERIALIZATION_NVP(K_);
186  ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
187  ar & BOOST_SERIALIZATION_NVP(throwCheirality_);
188  ar & BOOST_SERIALIZATION_NVP(verboseCheirality_);
189  }
190 
191  public:
193 };
194 
196  template<class POSE, class LANDMARK, class CALIBRATION>
197  struct traits<GenericProjectionFactor<POSE, LANDMARK, CALIBRATION> > :
198  public Testable<GenericProjectionFactor<POSE, LANDMARK, CALIBRATION> > {
199  };
200 
201 } // \ namespace gtsam
const gtsam::Key poseKey
bool equals(const This &other, double tol=1e-9) const
check equality
Definition: Factor.cpp:42
const boost::shared_ptr< CALIBRATION > calibration() const
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
NoiseModelFactor2< POSE, LANDMARK > Base
shorthand for base class type
noiseModel::Diagonal::shared_ptr model
Vector2 Point2
Definition: Point2.h:27
void serialize(ARCHIVE &ar, const unsigned int)
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const gtsam::Key pointKey
GenericProjectionFactor(const Point2 &measured, const SharedNoiseModel &model, Key poseKey, Key pointKey, const boost::shared_ptr< CALIBRATION > &K, boost::optional< POSE > body_P_sensor=boost::none)
Point3 point(10, 0,-5)
Point2 measured_
2D measurement
Base class for all pinhole cameras.
Class compose(const Class &g) const
Definition: Lie.h:47
Eigen::VectorXd Vector
Definition: Vector.h:38
const Point2 & measured() const
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
friend class boost::serialization::access
Serialization function.
boost::shared_ptr< This > shared_ptr
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
boost::optional< POSE > body_P_sensor_
The pose of the sensor in the body frame.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Vector evaluateError(const Pose3 &pose, const Point3 &point, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
Evaluate error h(x)-z and optionally derivatives.
traits
Definition: chartTesting.h:28
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Non-linear factor base classes.
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
float * p
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none, OptionalJacobian< 2, DimK > Dcal=boost::none) const
project a 3D point from world coordinates into the image
Definition: PinholePose.h:118
GenericProjectionFactor< POSE, LANDMARK, CALIBRATION > This
shorthand for this class
GenericProjectionFactor(const Point2 &measured, const SharedNoiseModel &model, Key poseKey, Key pointKey, const boost::shared_ptr< CALIBRATION > &K, bool throwCheirality, bool verboseCheirality, boost::optional< POSE > body_P_sensor=boost::none)
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW
Definition: types.h:269
const G double tol
Definition: Group.h:83
Vector3 Point3
Definition: Point3.h:35
gtsam::NonlinearFactor::shared_ptr clone() const override
static const CalibratedCamera camera(kDefaultPose)
bool verboseCheirality_
If true, prints text for Cheirality exceptions (default: false)
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
bool throwCheirality_
If true, rethrows Cheirality exceptions (default: false)
boost::shared_ptr< CALIBRATION > K_
shared pointer to calibration object
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:59
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:734
The most common 5DOF 3D->2D calibration.
const char * what() const noexceptoverride
GenericProjectionFactor()
Default constructor.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:44