gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <boost/optional.hpp>
26 
27 namespace gtsam {
28 
34  template<class POSE, class LANDMARK, class CALIBRATION = Cal3_S2>
35  class GenericProjectionFactor: public NoiseModelFactor2<POSE, LANDMARK> {
36  protected:
37 
38  // Keep a copy of measurement and calibration for I/O
40  boost::shared_ptr<CALIBRATION> K_;
41  boost::optional<POSE> body_P_sensor_;
42 
43  // verbosity handling for Cheirality Exceptions
46 
47  public:
48 
51 
54 
56  typedef boost::shared_ptr<This> shared_ptr;
57 
60 
72  Key poseKey, Key pointKey, const boost::shared_ptr<CALIBRATION>& K,
73  boost::optional<POSE> body_P_sensor = boost::none) :
74  Base(model, poseKey, pointKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
75  throwCheirality_(false), verboseCheirality_(false) {}
76 
90  Key poseKey, Key pointKey, const boost::shared_ptr<CALIBRATION>& K,
92  boost::optional<POSE> body_P_sensor = boost::none) :
93  Base(model, poseKey, pointKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
94  throwCheirality_(throwCheirality), verboseCheirality_(verboseCheirality) {}
95 
98 
100  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
101  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
102  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
103 
109  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
110  std::cout << s << "GenericProjectionFactor, z = ";
111  measured_.print();
112  if(this->body_P_sensor_)
113  this->body_P_sensor_->print(" sensor pose in body frame: ");
114  Base::print("", keyFormatter);
115  }
116 
118  virtual bool equals(const NonlinearFactor& p, double tol = 1e-9) const {
119  const This *e = dynamic_cast<const This*>(&p);
120  return e
121  && Base::equals(p, tol)
122  && this->measured_.equals(e->measured_, tol)
123  && this->K_->equals(*e->K_, tol)
124  && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
125  }
126 
128  Vector evaluateError(const Pose3& pose, const Point3& point,
129  boost::optional<Matrix&> H1 = boost::none, boost::optional<Matrix&> H2 = boost::none) const {
130  try {
131  if(body_P_sensor_) {
132  if(H1) {
133  gtsam::Matrix H0;
135  Point2 reprojectionError(camera.project(point, H1, H2) - measured_);
136  *H1 = *H1 * H0;
137  return reprojectionError.vector();
138  } else {
140  Point2 reprojectionError(camera.project(point, H1, H2) - measured_);
141  return reprojectionError.vector();
142  }
143  } else {
144  PinholeCamera<CALIBRATION> camera(pose, *K_);
145  Point2 reprojectionError(camera.project(point, H1, H2) - measured_);
146  return reprojectionError.vector();
147  }
148  } catch( CheiralityException& e) {
149  if (H1) *H1 = zeros(2,6);
150  if (H2) *H2 = zeros(2,3);
151  if (verboseCheirality_)
152  std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
153  " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
154  if (throwCheirality_)
155  throw e;
156  }
157  return ones(2) * 2.0 * K_->fx();
158  }
159 
161  const Point2& measured() const {
162  return measured_;
163  }
164 
166  inline const boost::shared_ptr<CALIBRATION> calibration() const {
167  return K_;
168  }
169 
171  inline bool verboseCheirality() const { return verboseCheirality_; }
172 
174  inline bool throwCheirality() const { return throwCheirality_; }
175 
176  private:
177 
180  template<class ARCHIVE>
181  void serialize(ARCHIVE & ar, const unsigned int version) {
182  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
183  ar & BOOST_SERIALIZATION_NVP(measured_);
184  ar & BOOST_SERIALIZATION_NVP(K_);
185  ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
186  ar & BOOST_SERIALIZATION_NVP(throwCheirality_);
187  ar & BOOST_SERIALIZATION_NVP(verboseCheirality_);
188  }
189  };
190 } // \ namespace gtsam
Non-linear factor base classes.
Pose3 compose(const Pose3 &p2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
compose this transformation onto another (first *this and then p2)
Definition: Pose3.cpp:272
bool throwCheirality() const
return flag for throwing cheirality exceptions
Definition: ProjectionFactor.h:174
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.h:239
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: ProjectionFactor.h:100
virtual bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: ProjectionFactor.h:118
void print(const std::string &s="") const
print with optional string
Definition: Point2.cpp:33
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)
Constructor with exception-handling flags TODO: Mark argument order standard (keys, measurement, parameters)
Definition: ProjectionFactor.h:89
virtual ~GenericProjectionFactor()
Virtual destructor.
Definition: ProjectionFactor.h:97
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: ProjectionFactor.h:56
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: ProjectionFactor.h:109
bool verboseCheirality_
If true, prints text for Cheirality exceptions (default: false)
Definition: ProjectionFactor.h:45
NoiseModelFactor2< POSE, LANDMARK > Base
shorthand for base class type
Definition: ProjectionFactor.h:50
Definition: Point2.h:35
bool equals(const Point2 &q, double tol=1e-9) const
equals with an tolerance, prints out message if unequal
Definition: Point2.cpp:38
Point2 project(const Point3 &pw, boost::optional< Matrix & > Dpose=boost::none, boost::optional< Matrix & > Dpoint=boost::none, boost::optional< Matrix & > Dcal=boost::none) const
project a point from world coordinate to the image
Definition: PinholeCamera.h:299
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:423
Definition: Pose3.h:42
GenericProjectionFactor< POSE, LANDMARK, CALIBRATION > This
shorthand for this class
Definition: ProjectionFactor.h:53
This is the base class for all factor types.
Definition: Factor.h:51
Vector2 vector() const
return vectorized form (column-wise). TODO: why does this function exist?
Definition: Point2.h:216
GenericProjectionFactor()
Default constructor.
Definition: ProjectionFactor.h:59
Definition: PinholeCamera.h:40
bool verboseCheirality() const
return verbosity
Definition: ProjectionFactor.h:171
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:455
Definition: ProjectionFactor.h:35
friend class boost::serialization::access
Serialization function.
Definition: ProjectionFactor.h:179
Definition: CalibratedCamera.h:27
Vector evaluateError(const Pose3 &pose, const Point3 &point, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
Evaluate error h(x)-z and optionally derivatives.
Definition: ProjectionFactor.h:128
const Point2 & measured() const
return the measurement
Definition: ProjectionFactor.h:161
size_t Key
Integer nonlinear key type.
Definition: types.h:59
bool throwCheirality_
If true, rethrows Cheirality exceptions (default: false)
Definition: ProjectionFactor.h:44
Matrix ones(size_t m, size_t n)
Creates an ones matrix, with matlab-like syntax.
Definition: Matrix.cpp:45
Point2 measured_
2D measurement
Definition: ProjectionFactor.h:39
A simple camera class with a Cal3_S2 calibration.
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: NonlinearFactor.h:231
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:884
Nonlinear factor base class.
Definition: NonlinearFactor.h:54
boost::optional< POSE > body_P_sensor_
The pose of the sensor in the body frame.
Definition: ProjectionFactor.h:41
Definition: Point3.h:39
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)
Constructor TODO: Mark argument order standard (keys, measurement, parameters)
Definition: ProjectionFactor.h:71
Matrix zeros(size_t m, size_t n)
Creates an zeros matrix, with matlab-like syntax.
Definition: Matrix.cpp:40
boost::shared_ptr< CALIBRATION > K_
shared pointer to calibration object
Definition: ProjectionFactor.h:40
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
const boost::shared_ptr< CALIBRATION > calibration() const
return the calibration object
Definition: ProjectionFactor.h:166