gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeneralSFMFactor.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 
26 #include <gtsam/geometry/Point2.h>
27 #include <gtsam/geometry/Point3.h>
28 #include <iostream>
29 
30 namespace gtsam {
31 
37  template <class CAMERA, class LANDMARK>
38  class GeneralSFMFactor: public NoiseModelFactor2<CAMERA, LANDMARK> {
39  protected:
41 
42  public:
43 
44  typedef CAMERA Cam;
47  typedef Point2 Measurement;
48 
49  // shorthand for a smart pointer to a factor
50  typedef boost::shared_ptr<This> shared_ptr;
51 
59  GeneralSFMFactor(const Point2& measured, const SharedNoiseModel& model, Key cameraKey, Key landmarkKey) :
60  Base(model, cameraKey, landmarkKey), measured_(measured) {}
61 
62  GeneralSFMFactor():measured_(0.0,0.0) {}
63  GeneralSFMFactor(const Point2 & p):measured_(p) {}
64  GeneralSFMFactor(double x, double y):measured_(x,y) {}
65 
66  virtual ~GeneralSFMFactor() {}
67 
69  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
70  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
71  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
72 
78  void print(const std::string& s = "SFMFactor", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
79  Base::print(s, keyFormatter);
80  measured_.print(s + ".z");
81  }
82 
86  bool equals(const NonlinearFactor &p, double tol = 1e-9) const {
87  const This* e = dynamic_cast<const This*>(&p);
88  return e && Base::equals(p, tol) && this->measured_.equals(e->measured_, tol) ;
89  }
90 
92  Vector evaluateError(const Cam& camera, const Point3& point,
93  boost::optional<Matrix&> H1=boost::none, boost::optional<Matrix&> H2=boost::none) const {
94 
95  try {
96  Point2 reprojError(camera.project2(point,H1,H2) - measured_);
97  return reprojError.vector();
98  }
99  catch( CheiralityException& e) {
100  if (H1) *H1 = zeros(2, camera.dim());
101  if (H2) *H2 = zeros(2, point.dim());
102  std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2())
103  << " behind Camera " << DefaultKeyFormatter(this->key1()) << std::endl;
104  return zero(2);
105  }
106  }
107 
109  inline const Point2 measured() const {
110  return measured_;
111  }
112 
113  private:
116  template<class Archive>
117  void serialize(Archive & ar, const unsigned int version) {
118  ar & boost::serialization::make_nvp("NoiseModelFactor2",
119  boost::serialization::base_object<Base>(*this));
120  ar & BOOST_SERIALIZATION_NVP(measured_);
121  }
122  };
123 
128  template <class CALIBRATION>
129  class GeneralSFMFactor2: public NoiseModelFactor3<Pose3, Point3, CALIBRATION> {
130  protected:
132 
133  public:
134 
138  typedef Point2 Measurement;
139 
140  // shorthand for a smart pointer to a factor
141  typedef boost::shared_ptr<This> shared_ptr;
142 
151  GeneralSFMFactor2(const Point2& measured, const SharedNoiseModel& model, Key poseKey, Key landmarkKey, Key calibKey) :
152  Base(model, poseKey, landmarkKey, calibKey), measured_(measured) {}
154 
155  virtual ~GeneralSFMFactor2() {}
156 
158  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
159  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
160  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
161 
167  void print(const std::string& s = "SFMFactor2", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
168  Base::print(s, keyFormatter);
169  measured_.print(s + ".z");
170  }
171 
175  bool equals(const NonlinearFactor &p, double tol = 1e-9) const {
176  const This* e = dynamic_cast<const This*>(&p);
177  return e && Base::equals(p, tol) && this->measured_.equals(e->measured_, tol) ;
178  }
179 
181  Vector evaluateError(const Pose3& pose3, const Point3& point, const CALIBRATION &calib,
182  boost::optional<Matrix&> H1=boost::none,
183  boost::optional<Matrix&> H2=boost::none,
184  boost::optional<Matrix&> H3=boost::none) const
185  {
186  try {
187  Camera camera(pose3,calib);
188  Point2 reprojError(camera.project(point, H1, H2, H3) - measured_);
189  return reprojError.vector();
190  }
191  catch( CheiralityException& e) {
192  if (H1) *H1 = zeros(2, pose3.dim());
193  if (H2) *H2 = zeros(2, point.dim());
194  if (H3) *H3 = zeros(2, calib.dim());
195  std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2())
196  << " behind Camera " << DefaultKeyFormatter(this->key1()) << std::endl;
197  }
198  return zero(2);
199  }
200 
202  inline const Point2 measured() const {
203  return measured_;
204  }
205 
206  private:
209  template<class Archive>
210  void serialize(Archive & ar, const unsigned int version) {
211  ar & boost::serialization::make_nvp("NoiseModelFactor3",
212  boost::serialization::base_object<Base>(*this));
213  ar & BOOST_SERIALIZATION_NVP(measured_);
214  }
215  };
216 
217 
218 
219 } //namespace
Non-linear factor base classes.
GeneralSFMFactor()
default constructor
Definition: GeneralSFMFactor.h:62
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.h:239
PinholeCamera< CALIBRATION > Camera
typedef for camera type
Definition: GeneralSFMFactor.h:136
virtual ~GeneralSFMFactor()
destructor
Definition: GeneralSFMFactor.h:66
Point2 Measurement
typedef for the measurement
Definition: GeneralSFMFactor.h:138
void print(const std::string &s="") const
print with optional string
Definition: Point2.cpp:33
Definition: GeneralSFMFactor.h:38
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: GeneralSFMFactor.h:69
NoiseModelFactor3< Pose3, Point3, CALIBRATION > Base
typedef for the base class
Definition: GeneralSFMFactor.h:137
Vector evaluateError(const Pose3 &pose3, const Point3 &point, const CALIBRATION &calib, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none, boost::optional< Matrix & > H3=boost::none) const
h(x)-z
Definition: GeneralSFMFactor.h:181
const Point2 measured() const
return the measured
Definition: GeneralSFMFactor.h:202
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: GeneralSFMFactor.h:158
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
Vector evaluateError(const Cam &camera, const Point3 &point, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
h(x)-z
Definition: GeneralSFMFactor.h:92
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
GeneralSFMFactor(const Point2 &measured, const SharedNoiseModel &model, Key cameraKey, Key landmarkKey)
Constructor.
Definition: GeneralSFMFactor.h:59
Definition: Pose3.h:42
This is the base class for all factor types.
Definition: Factor.h:51
friend class boost::serialization::access
Serialization function.
Definition: GeneralSFMFactor.h:115
Vector2 vector() const
return vectorized form (column-wise). TODO: why does this function exist?
Definition: Point2.h:216
Point2 Measurement
typedef for the measurement
Definition: GeneralSFMFactor.h:47
2D Point
void print(const std::string &s="SFMFactor", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: GeneralSFMFactor.h:78
Base class for all pinhole cameras.
bool zero(const Vector &v)
check if all zero
Definition: Vector.cpp:39
void print(const std::string &s="SFMFactor2", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: GeneralSFMFactor.h:167
Point2 measured_
the 2D measurement
Definition: GeneralSFMFactor.h:131
Definition: PinholeCamera.h:40
GeneralSFMFactor2()
default constructor
Definition: GeneralSFMFactor.h:153
GeneralSFMFactor(double x, double y)
constructor that takes doubles x,y to make a Point2
Definition: GeneralSFMFactor.h:64
GeneralSFMFactor2(const Point2 &measured, const SharedNoiseModel &model, Key poseKey, Key landmarkKey, Key calibKey)
Constructor.
Definition: GeneralSFMFactor.h:151
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:455
GeneralSFMFactor(const Point2 &p)
constructor that takes a Point2
Definition: GeneralSFMFactor.h:63
Definition: CalibratedCamera.h:27
size_t Key
Integer nonlinear key type.
Definition: types.h:59
bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: GeneralSFMFactor.h:175
Calibrated camera for which only pose is unknown.
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:532
Point2 measured_
the 2D measurement
Definition: GeneralSFMFactor.h:40
friend class boost::serialization::access
Serialization function.
Definition: GeneralSFMFactor.h:208
bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: GeneralSFMFactor.h:86
3D Point
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: NonlinearFactor.h:231
CAMERA Cam
typedef for camera type
Definition: GeneralSFMFactor.h:44
virtual ~GeneralSFMFactor2()
destructor
Definition: GeneralSFMFactor.h:155
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:884
Nonlinear factor base class.
Definition: NonlinearFactor.h:54
Non-linear factor for a constraint derived from a 2D measurement.
Definition: GeneralSFMFactor.h:129
NoiseModelFactor2< CAMERA, LANDMARK > Base
typedef for the base class
Definition: GeneralSFMFactor.h:46
size_t dim() const
Dimensionality of the tangent space = 6 DOF.
Definition: Pose3.h:139
Definition: Point3.h:39
GeneralSFMFactor< CAMERA, LANDMARK > This
typedef for this object
Definition: GeneralSFMFactor.h:45
Matrix zeros(size_t m, size_t n)
Creates an zeros matrix, with matlab-like syntax.
Definition: Matrix.cpp: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
size_t dim() const
return dimensionality of tangent space, DOF = 3
Definition: Point3.h:128
const Point2 measured() const
return the measured
Definition: GeneralSFMFactor.h:109
A convenient base class for creating your own NoiseModelFactor with 3 variables.
Definition: NonlinearFactor.h:498