gtsam
Loading...
Searching...
No Matches
StereoFactor.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
18
19#pragma once
20
26
27#include <optional>
28
29namespace gtsam {
30
35template<class POSE, class LANDMARK>
37 : public NoiseModelFactorT<Vector3, POSE, LANDMARK> {
38private:
39
40 // Keep a copy of measurement and calibration for I/O
41 StereoPoint2 measured_;
42 Cal3_S2Stereo::shared_ptr K_;
43 std::optional<POSE> body_P_sensor_;
44
45 // verbosity handling for Cheirality Exceptions
46 bool throwCheirality_;
47 bool verboseCheirality_;
48
49public:
50
51 // shorthand for base class type
54 typedef std::shared_ptr<GenericStereoFactor> shared_ptr;
55 typedef POSE CamPose;
56
57 // Provide access to the Matrix& version of evaluateError:
59
63 GenericStereoFactor() : K_(new Cal3_S2Stereo(444, 555, 666, 777, 888, 1.0)),
64 throwCheirality_(false), verboseCheirality_(false) {}
65
76 Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr& K,
77 std::optional<POSE> body_P_sensor = {}) :
78 Base(model, poseKey, landmarkKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
79 throwCheirality_(false), verboseCheirality_(false) {}
80
93 Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr& K,
95 std::optional<POSE> body_P_sensor = {}) :
96 Base(model, poseKey, landmarkKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
97 throwCheirality_(throwCheirality), verboseCheirality_(verboseCheirality) {}
98
101
103 gtsam::NonlinearFactor::shared_ptr clone() const override {
104 return std::static_pointer_cast<gtsam::NonlinearFactor>(
105 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
106
112 void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
113 Base::print(s, keyFormatter);
114 measured_.print(s + ".z");
115 if(this->body_P_sensor_)
116 this->body_P_sensor_->print(" sensor pose in body frame: ");
117 }
118
122 bool equals(const NonlinearFactor& f, double tol = 1e-9) const override {
123 const GenericStereoFactor* e = dynamic_cast<const GenericStereoFactor*> (&f);
124 return e
125 && Base::equals(f)
126 && measured_.equals(e->measured_, 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
131 Vector3 evaluateError(const Pose3& pose, const Point3& point,
133 OptionalMatrixType H2) const override {
134 try {
135 if(body_P_sensor_) {
136 if(H1) {
137 gtsam::Matrix H0;
138 StereoCamera stereoCam(pose.compose(*body_P_sensor_, H0), K_);
139 StereoPoint2 reprojectionError(stereoCam.project(point, H1, H2) - measured_);
140 *H1 = *H1 * H0;
141 return reprojectionError.vector();
142 } else {
143 StereoCamera stereoCam(pose.compose(*body_P_sensor_), K_);
144 return (stereoCam.project(point, H1, H2) - measured_).vector();
145 }
146 } else {
147 StereoCamera stereoCam(pose, K_);
148 return (stereoCam.project(point, H1, H2) - measured_).vector();
149 }
150 } catch(StereoCheiralityException& e) {
151 if (H1) *H1 = Matrix::Zero(3,6);
152 if (H2) *H2 = Z_3x3;
153 if (verboseCheirality_)
154 std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
155 " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
156 if (throwCheirality_)
157 throw StereoCheiralityException(this->key2());
158 }
159 return Vector3::Constant(2.0 * K_->fx());
160 }
161
163 const StereoPoint2& measured() const {
164 return measured_;
165 }
166
168 inline const Cal3_S2Stereo::shared_ptr calibration() const {
169 return K_;
170 }
171
173 inline bool verboseCheirality() const { return verboseCheirality_; }
174
176 inline bool throwCheirality() const { return throwCheirality_; }
177
179 const std::optional<POSE>& body_P_sensor() const {
180 return body_P_sensor_;
181 }
182
183private:
184#if GTSAM_ENABLE_BOOST_SERIALIZATION
186 friend class boost::serialization::access;
187 template<class Archive>
188 void serialize(Archive & ar, const unsigned int /*version*/) {
189 // NoiseModelFactor2 instead of NoiseModelFactorN for backward compatibility
190 ar & boost::serialization::make_nvp("NoiseModelFactor2",
191 boost::serialization::base_object<Base>(*this));
192 ar & BOOST_SERIALIZATION_NVP(measured_);
193 ar & BOOST_SERIALIZATION_NVP(K_);
194 ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
195 ar & BOOST_SERIALIZATION_NVP(throwCheirality_);
196 ar & BOOST_SERIALIZATION_NVP(verboseCheirality_);
197 }
198#endif
199};
200
202template<class T1, class T2>
203struct traits<GenericStereoFactor<T1, T2> > : public Testable<GenericStereoFactor<T1, T2> > {};
204
205} // \ namespace gtsam
Macros for Matrix constants to avoid excessive template instantiation.
A Stereo Camera based on two Simple Cameras.
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
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, stereo version.
Definition Cal3_S2Stereo.h:30
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
Definition StereoCamera.h:26
A stereo camera class, parameterize by left camera pose and stereo calibration.
Definition StereoCamera.h:47
StereoPoint2 project(const Point3 &point) const
Project 3D point to StereoPoint2 (uL,uR,v).
Definition StereoCamera.cpp:32
A 2D stereo point, v will be same for rectified images.
Definition StereoPoint2.h:34
Vector3 vector() const
convert to vector
Definition StereoPoint2.h:119
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 Vector3 evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Nonlinear factor base class.
Definition NonlinearFactor.h:70
A Generic Stereo Factor.
Definition StereoFactor.h:37
bool throwCheirality() const
Definition StereoFactor.h:176
GenericStereoFactor(const StereoPoint2 &measured, const SharedNoiseModel &model, Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr &K, std::optional< POSE > body_P_sensor={})
Constructor.
Definition StereoFactor.h:75
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
equals
Definition StereoFactor.h:122
const Cal3_S2Stereo::shared_ptr calibration() const
return the calibration object
Definition StereoFactor.h:168
const std::optional< gtsam::Pose3 > & body_P_sensor() const
Definition StereoFactor.h:179
GenericStereoFactor< POSE, LANDMARK > This
typedef for this class (with templates)
Definition StereoFactor.h:53
Vector3 evaluateError(const Pose3 &pose, const Point3 &point, OptionalMatrixType H1, OptionalMatrixType H2) const override
h(x)-z
Definition StereoFactor.h:131
~GenericStereoFactor() override
Virtual destructor.
Definition StereoFactor.h:100
bool verboseCheirality() const
Definition StereoFactor.h:173
std::shared_ptr< GenericStereoFactor > shared_ptr
typedef for shared pointer to this object
Definition StereoFactor.h:54
NoiseModelFactorT< Vector3, POSE, LANDMARK > Base
base class
Definition StereoFactor.h:52
const StereoPoint2 & measured() const
Definition StereoFactor.h:163
GenericStereoFactor()
Default constructor.
Definition StereoFactor.h:63
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition StereoFactor.h:112
POSE CamPose
typedef for Pose Lie Value type
Definition StereoFactor.h:55
GenericStereoFactor(const StereoPoint2 &measured, const SharedNoiseModel &model, Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr &K, bool throwCheirality, bool verboseCheirality, std::optional< POSE > body_P_sensor={})
Constructor with exception-handling flags.
Definition StereoFactor.h:92
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition StereoFactor.h:103