gtsam
Loading...
Searching...
No Matches
InvDepthFactorVariant3.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
31namespace gtsam {
32
37 : public NoiseModelFactorT<Vector2, Pose3, Vector3> {
38protected:
39
40 // Keep a copy of measurement and calibration for I/O
42 Cal3_S2::shared_ptr K_;
43
44public:
45
48
49 // Provide access to the Matrix& version of evaluateError:
51
54
56 typedef std::shared_ptr<This> shared_ptr;
57
60 measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
61 }
62
73 InvDepthFactorVariant3a(const Key poseKey, const Key landmarkKey,
74 const Point2& measured, const Cal3_S2::shared_ptr& K, const SharedNoiseModel& model) :
75 Base(model, poseKey, landmarkKey), measured_(measured), K_(K) {}
76
79
85 void print(const std::string& s = "InvDepthFactorVariant3a",
86 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
87 Base::print(s, keyFormatter);
89 }
90
92 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
93 const This *e = dynamic_cast<const This*>(&p);
94 return e
95 && Base::equals(p, tol)
96 && traits<Point2>::Equals(this->measured_, e->measured_, tol)
97 && this->K_->equals(*e->K_, tol);
98 }
99
100 Vector inverseDepthError(const Pose3& pose, const Vector3& landmark) const {
101 try {
102 // Calculate the 3D coordinates of the landmark in the Pose frame
103 double theta = landmark(0), phi = landmark(1), rho = landmark(2);
104 Point3 pose_P_landmark(cos(phi)*sin(theta)/rho, sin(phi)/rho, cos(phi)*cos(theta)/rho);
105 // Convert the landmark to world coordinates
106 Point3 world_P_landmark = pose.transformFrom(pose_P_landmark);
107 // Project landmark into Pose2
108 PinholeCamera<Cal3_S2> camera(pose, *K_);
109 return camera.project(world_P_landmark) - measured_;
110 } catch( CheiralityException& e) {
111 std::cout << e.what()
112 << ": Inverse Depth Landmark [" << DefaultKeyFormatter(this->key<1>()) << "," << DefaultKeyFormatter(this->key<2>()) << "]"
113 << " moved behind camera [" << DefaultKeyFormatter(this->key<1>()) << "]"
114 << std::endl;
115 return Vector::Ones(2) * 2.0 * K_->fx();
116 }
117 return Vector{{0.0}};
118 }
119
121 Vector2 evaluateError(const Pose3& pose, const Vector3& landmark,
123 OptionalMatrixType H2) const override {
124
125 if(H1) {
127 std::bind(&InvDepthFactorVariant3a::inverseDepthError, this,
128 std::placeholders::_1, landmark),
129 pose);
130 }
131 if(H2) {
133 std::bind(&InvDepthFactorVariant3a::inverseDepthError, this, pose,
134 std::placeholders::_1),
135 landmark);
136 }
137
138 return inverseDepthError(pose, landmark);
139 }
140
142 const Point2& imagePoint() const {
143 return measured_;
144 }
145
147 const Cal3_S2::shared_ptr calibration() const {
148 return K_;
149 }
150
151private:
152
153#if GTSAM_ENABLE_BOOST_SERIALIZATION
155 friend class boost::serialization::access;
156 template<class ARCHIVE>
157 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
158 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
159 ar & BOOST_SERIALIZATION_NVP(measured_);
160 ar & BOOST_SERIALIZATION_NVP(K_);
161 }
162#endif
163};
164
169 : public NoiseModelFactorT<Vector2, Pose3, Pose3, Vector3> {
170protected:
171
172 // Keep a copy of measurement and calibration for I/O
174 Cal3_S2::shared_ptr K_;
175
176public:
177
180
183
185 typedef std::shared_ptr<This> shared_ptr;
186
189 measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
190 }
191
202 InvDepthFactorVariant3b(const Key poseKey1, const Key poseKey2, const Key landmarkKey,
203 const Point2& measured, const Cal3_S2::shared_ptr& K, const SharedNoiseModel& model) :
204 Base(model, poseKey1, poseKey2, landmarkKey), measured_(measured), K_(K) {}
205
208
214 void print(const std::string& s = "InvDepthFactorVariant3",
215 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
216 Base::print(s, keyFormatter);
218 }
219
221 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
222 const This *e = dynamic_cast<const This*>(&p);
223 return e
224 && Base::equals(p, tol)
225 && traits<Point2>::Equals(this->measured_, e->measured_, tol)
226 && this->K_->equals(*e->K_, tol);
227 }
228
229 Vector inverseDepthError(const Pose3& pose1, const Pose3& pose2, const Vector3& landmark) const {
230 try {
231 // Calculate the 3D coordinates of the landmark in the Pose1 frame
232 double theta = landmark(0), phi = landmark(1), rho = landmark(2);
233 Point3 pose1_P_landmark(cos(phi)*sin(theta)/rho, sin(phi)/rho, cos(phi)*cos(theta)/rho);
234 // Convert the landmark to world coordinates
235 Point3 world_P_landmark = pose1.transformFrom(pose1_P_landmark);
236 // Project landmark into Pose2
237 PinholeCamera<Cal3_S2> camera(pose2, *K_);
238 return camera.project(world_P_landmark) - measured_;
239 } catch( CheiralityException& e) {
240 std::cout << e.what()
241 << ": Inverse Depth Landmark [" << DefaultKeyFormatter(this->key<1>()) << "," << DefaultKeyFormatter(this->key<3>()) << "]"
242 << " moved behind camera " << DefaultKeyFormatter(this->key<2>())
243 << std::endl;
244 return Vector::Ones(2) * 2.0 * K_->fx();
245 }
246 return Vector{{0.0}};
247 }
248
250 Vector2 evaluateError(const Pose3& pose1, const Pose3& pose2,
251 const Vector3& landmark, OptionalMatrixType H1,
253 OptionalMatrixType H3) const override {
254
255 if(H1)
257 std::bind(&InvDepthFactorVariant3b::inverseDepthError, this,
258 std::placeholders::_1, pose2, landmark),
259 pose1);
260
261 if(H2)
263 std::bind(&InvDepthFactorVariant3b::inverseDepthError, this, pose1,
264 std::placeholders::_1, landmark),
265 pose2);
266
267 if(H3)
269 std::bind(&InvDepthFactorVariant3b::inverseDepthError, this, pose1,
270 pose2, std::placeholders::_1),
271 landmark);
272
273 return inverseDepthError(pose1, pose2, landmark);
274 }
275
277 const Point2& imagePoint() const {
278 return measured_;
279 }
280
282 const Cal3_S2::shared_ptr calibration() const {
283 return K_;
284 }
285
286private:
287
288#if GTSAM_ENABLE_BOOST_SERIALIZATION
289 friend class boost::serialization::access;
291 template<class ARCHIVE>
292 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
293 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
294 ar & BOOST_SERIALIZATION_NVP(measured_);
295 ar & BOOST_SERIALIZATION_NVP(K_);
296 }
297#endif
298};
299
300} // \ namespace gtsam
Numerical derivative helpers for manifold-valued functions.
The most common 5DOF 3D->2D calibration.
Base class for all pinhole cameras.
3D Pose manifold SO(3) x R^3 and group SE(3)
2D Point
Base class for noise model factors with N variables.
Non-linear factor base classes.
internal::MatrixMN< traits< internal::OutputType< Y, F, X > >::dimension, N >::type numericalDerivative11(F &&h, const X &x, double delta=1e-5)
New-style numerical derivatives using manifold_traits.
Definition numericalDerivative.h:180
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
The most common 5DOF 3D->2D calibration.
Definition Cal3_S2.h:35
A pinhole camera class that has a Pose3 and a Calibration.
Definition PinholeCamera.h:34
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
Point3 transformFrom(const Point3 &point, OptionalJacobian< 3, 6 > Hself={}, OptionalJacobian< 3, 3 > Hpoint={}) const
takes point in Pose coordinates and transforms it to world coordinates
Definition Pose3.cpp:180
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
Key key() const
Definition NoiseModelFactorN.h:307
Nonlinear factor base class.
Definition NonlinearFactor.h:70
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition InvDepthFactorVariant3.h:56
Cal3_S2::shared_ptr K_
shared pointer to calibration object
Definition InvDepthFactorVariant3.h:42
const Point2 & imagePoint() const
return the measurement
Definition InvDepthFactorVariant3.h:142
InvDepthFactorVariant3a()
Default constructor.
Definition InvDepthFactorVariant3.h:59
void print(const std::string &s="InvDepthFactorVariant3a", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition InvDepthFactorVariant3.h:85
~InvDepthFactorVariant3a() override
Virtual destructor.
Definition InvDepthFactorVariant3.h:78
Point2 measured_
2D measurement
Definition InvDepthFactorVariant3.h:41
const Cal3_S2::shared_ptr calibration() const
return the calibration object
Definition InvDepthFactorVariant3.h:147
NoiseModelFactorT< Vector2, Pose3, Vector3 > Base
shorthand for base class type
Definition InvDepthFactorVariant3.h:47
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition InvDepthFactorVariant3.h:92
InvDepthFactorVariant3a This
shorthand for this class
Definition InvDepthFactorVariant3.h:53
InvDepthFactorVariant3a(const Key poseKey, const Key landmarkKey, const Point2 &measured, const Cal3_S2::shared_ptr &K, const SharedNoiseModel &model)
Constructor TODO: Mark argument order standard (keys, measurement, parameters).
Definition InvDepthFactorVariant3.h:73
Vector2 evaluateError(const Pose3 &pose, const Vector3 &landmark, OptionalMatrixType H1, OptionalMatrixType H2) const override
Evaluate error h(x)-z and optionally derivatives.
Definition InvDepthFactorVariant3.h:121
void print(const std::string &s="InvDepthFactorVariant3", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition InvDepthFactorVariant3.h:214
const Point2 & imagePoint() const
return the measurement
Definition InvDepthFactorVariant3.h:277
InvDepthFactorVariant3b(const Key poseKey1, const Key poseKey2, const Key landmarkKey, const Point2 &measured, const Cal3_S2::shared_ptr &K, const SharedNoiseModel &model)
Constructor TODO: Mark argument order standard (keys, measurement, parameters).
Definition InvDepthFactorVariant3.h:202
NoiseModelFactorT< Vector2, Pose3, Pose3, Vector3 > Base
shorthand for base class type
Definition InvDepthFactorVariant3.h:179
Cal3_S2::shared_ptr K_
shared pointer to calibration object
Definition InvDepthFactorVariant3.h:174
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition InvDepthFactorVariant3.h:221
~InvDepthFactorVariant3b() override
Virtual destructor.
Definition InvDepthFactorVariant3.h:207
InvDepthFactorVariant3b This
shorthand for this class
Definition InvDepthFactorVariant3.h:182
InvDepthFactorVariant3b()
Default constructor.
Definition InvDepthFactorVariant3.h:188
Vector2 evaluateError(const Pose3 &pose1, const Pose3 &pose2, const Vector3 &landmark, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
Evaluate error h(x)-z and optionally derivatives.
Definition InvDepthFactorVariant3.h:250
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition InvDepthFactorVariant3.h:185
Point2 measured_
2D measurement
Definition InvDepthFactorVariant3.h:173
const Cal3_S2::shared_ptr calibration() const
return the calibration object
Definition InvDepthFactorVariant3.h:282