gtsam
Loading...
Searching...
No Matches
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
20
21#pragma once
22
30#include <optional>
31
32namespace gtsam {
33
40 template <class POSE = Pose3, class LANDMARK = Point3,
41 class CALIBRATION = Cal3_S2>
43 : public NoiseModelFactorT<Vector2, POSE, LANDMARK> {
44 protected:
45
46 // Keep a copy of measurement and calibration for I/O
48 std::shared_ptr<CALIBRATION> K_;
49 std::optional<POSE> body_P_sensor_;
50
51 // verbosity handling for Cheirality Exceptions
54
55 public:
56
59
60 // Provide access to the Matrix& version of evaluateError:
62
65
67 typedef std::shared_ptr<This> shared_ptr;
68
73
85 Key poseKey, Key pointKey, const std::shared_ptr<CALIBRATION>& K,
86 std::optional<POSE> body_P_sensor = {}) :
87 Base(model, poseKey, pointKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
88 throwCheirality_(false), verboseCheirality_(false) {}
89
103 Key poseKey, Key pointKey, const std::shared_ptr<CALIBRATION>& K,
105 std::optional<POSE> body_P_sensor = {}) :
106 Base(model, poseKey, pointKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
108
111
113 gtsam::NonlinearFactor::shared_ptr clone() const override {
114 return std::static_pointer_cast<gtsam::NonlinearFactor>(
115 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
116
122 void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
123 std::cout << s << "GenericProjectionFactor, z = ";
125 if(this->body_P_sensor_)
126 this->body_P_sensor_->print(" sensor pose in body frame: ");
127 Base::print("", keyFormatter);
128 }
129
131 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
132 const This *e = dynamic_cast<const This*>(&p);
133 return e
134 && Base::equals(p, tol)
135 && traits<Point2>::Equals(this->measured_, e->measured_, tol)
136 && this->K_->equals(*e->K_, tol)
138 }
139
141 Vector2 evaluateError(const Pose3& pose, const Point3& point,
143 OptionalMatrixType H2) const override {
144 try {
145 if(body_P_sensor_) {
146 if(H1) {
147 gtsam::Matrix H0;
148 PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_, H0), *K_);
149 Point2 reprojectionError(camera.project(point, H1, H2, {}) - measured_);
150 *H1 = *H1 * H0;
151 return reprojectionError;
152 } else {
153 PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_), *K_);
154 return camera.project(point, H1, H2, {}) - measured_;
155 }
156 } else {
157 PinholeCamera<CALIBRATION> camera(pose, *K_);
158 return camera.project(point, H1, H2, {}) - measured_;
159 }
160 } catch( CheiralityException& e) {
161 if (H1) *H1 = Matrix::Zero(2,6);
162 if (H2) *H2 = Matrix::Zero(2,3);
164 std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
165 " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
167 throw CheiralityException(this->key2());
168 }
169 return Vector2::Constant(2.0 * K_->fx());
170 }
171
173 const Point2& measured() const {
174 return measured_;
175 }
176
178 const std::shared_ptr<CALIBRATION> calibration() const {
179 return K_;
180 }
181
183 const std::optional<POSE>& body_P_sensor() const {
184 return body_P_sensor_;
185 }
186
188 inline bool verboseCheirality() const { return verboseCheirality_; }
189
191 inline bool throwCheirality() const { return throwCheirality_; }
192
193 private:
194
195#if GTSAM_ENABLE_BOOST_SERIALIZATION
197 friend class boost::serialization::access;
198 template<class ARCHIVE>
199 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
200 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
201 ar & BOOST_SERIALIZATION_NVP(measured_);
202 ar & BOOST_SERIALIZATION_NVP(K_);
203 ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
204 ar & BOOST_SERIALIZATION_NVP(throwCheirality_);
205 ar & BOOST_SERIALIZATION_NVP(verboseCheirality_);
206 }
207#endif
208};
209
211 template<class POSE, class LANDMARK, class CALIBRATION>
212 struct traits<GenericProjectionFactor<POSE, LANDMARK, CALIBRATION> > :
213 public Testable<GenericProjectionFactor<POSE, LANDMARK, CALIBRATION> > {
214 };
215
216} // \ namespace gtsam
The most common 5DOF 3D->2D calibration.
3D Point
Base class for all pinhole cameras.
3D Pose manifold SO(3) x R^3 and group SE(3)
Arbitrary-arity Jacobian factor with compile-time block dimensions.
Base class for noise model factors with N variables.
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
Matrix * OptionalMatrixType
This typedef will be used everywhere boost::optional<Matrix&> reference was used previously.
Definition NonlinearFactor.h:57
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
Vector2 Point2
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point2 to Vector2...
Definition Point2.h:32
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
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
The most common 5DOF 3D->2D calibration.
Definition Cal3_S2.h:35
Definition CalibratedCamera.h:35
A pinhole camera class that has a Pose3 and a Calibration.
Definition PinholeCamera.h:34
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
project a 3D point from world coordinates into the image
Definition PinholePose.h:112
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
virtual void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
Definition Factor.cpp:29
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
NoiseModelFactorT()
Definition NoiseModelFactorN.h:257
virtual Vector2 evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Nonlinear factor base class.
Definition NonlinearFactor.h:70
Non-linear factor for a constraint derived from a 2D measurement.
Definition ProjectionFactor.h:43
bool verboseCheirality() const
Definition ProjectionFactor.h:188
Vector2 evaluateError(const Pose3 &pose, const Point3 &point, OptionalMatrixType H1, OptionalMatrixType H2) const override
Evaluate error h(x)-z and optionally derivatives.
Definition ProjectionFactor.h:141
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition ProjectionFactor.h:122
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition ProjectionFactor.h:131
Point2 measured_
Definition ProjectionFactor.h:47
GenericProjectionFactor< POSE, LANDMARK, CALIBRATION > This
shorthand for this class
Definition ProjectionFactor.h:64
std::optional< POSE > body_P_sensor_
Definition ProjectionFactor.h:49
const Point2 & measured() const
Definition ProjectionFactor.h:173
std::shared_ptr< CALIBRATION > K_
Definition ProjectionFactor.h:48
GenericProjectionFactor(const Point2 &measured, const SharedNoiseModel &model, Key poseKey, Key pointKey, const std::shared_ptr< CALIBRATION > &K, std::optional< POSE > body_P_sensor={})
Constructor TODO: Mark argument order standard (keys, measurement, parameters).
Definition ProjectionFactor.h:84
NoiseModelFactorT< Vector2, POSE, LANDMARK > Base
shorthand for base class type
Definition ProjectionFactor.h:58
~GenericProjectionFactor() override
Virtual destructor.
Definition ProjectionFactor.h:110
GenericProjectionFactor(const Point2 &measured, const SharedNoiseModel &model, Key poseKey, Key pointKey, const std::shared_ptr< CALIBRATION > &K, bool throwCheirality, bool verboseCheirality, std::optional< POSE > body_P_sensor={})
Constructor with exception-handling flags TODO: Mark argument order standard (keys,...
Definition ProjectionFactor.h:102
GenericProjectionFactor()
Default constructor.
Definition ProjectionFactor.h:70
bool throwCheirality_
Definition ProjectionFactor.h:52
std::shared_ptr< This > shared_ptr
Definition ProjectionFactor.h:67
bool throwCheirality() const
Definition ProjectionFactor.h:191
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition ProjectionFactor.h:113
const std::optional< POSE > & body_P_sensor() const
Definition ProjectionFactor.h:183
bool verboseCheirality_
Definition ProjectionFactor.h:53
const std::shared_ptr< CALIBRATION > calibration() const
return the calibration object
Definition ProjectionFactor.h:178