gtsam
Loading...
Searching...
No Matches
InvDepthFactorVariant2.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
22#pragma once
23
31
32namespace gtsam {
33
38 : public NoiseModelFactorT<Vector2, Pose3, Vector3> {
39protected:
40
41 // Keep a copy of measurement and calibration for I/O
43 Cal3_S2::shared_ptr K_;
45
46public:
47
50
51 // Provide access to the Matrix& version of evaluateError:
53
56
58 typedef std::shared_ptr<This> shared_ptr;
59
62 measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
63 }
64
73 InvDepthFactorVariant2(const Key poseKey, const Key landmarkKey,
74 const Point2& measured, const Cal3_S2::shared_ptr& K, const Point3 referencePoint,
75 const SharedNoiseModel& model) :
76 Base(model, poseKey, landmarkKey), measured_(measured), K_(K), referencePoint_(referencePoint) {}
77
80
86 void print(const std::string& s = "InvDepthFactorVariant2",
87 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
88 Base::print(s, keyFormatter);
90 }
91
93 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
94 const This *e = dynamic_cast<const This*>(&p);
95 return e
96 && Base::equals(p, tol)
97 && traits<Point2>::Equals(this->measured_, e->measured_, tol)
98 && this->K_->equals(*e->K_, tol)
99 && traits<Point3>::Equals(this->referencePoint_, e->referencePoint_, tol);
100 }
101
102 Vector inverseDepthError(const Pose3& pose, const Vector3& landmark) const {
103 try {
104 // Calculate the 3D coordinates of the landmark in the world frame
105 double theta = landmark(0), phi = landmark(1), rho = landmark(2);
106 Point3 world_P_landmark = referencePoint_ + Point3(cos(theta)*cos(phi)/rho, sin(theta)*cos(phi)/rho, sin(phi)/rho);
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<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(&InvDepthFactorVariant2::inverseDepthError, this,
128 std::placeholders::_1, landmark), pose);
129 }
130 if (H2) {
132 std::bind(&InvDepthFactorVariant2::inverseDepthError, this, pose,
133 std::placeholders::_1), landmark);
134 }
135
136 return inverseDepthError(pose, landmark);
137 }
138
140 const Point2& imagePoint() const {
141 return measured_;
142 }
143
145 const Cal3_S2::shared_ptr calibration() const {
146 return K_;
147 }
148
150 const Point3& referencePoint() const {
151 return referencePoint_;
152 }
153
154private:
155
156#if GTSAM_ENABLE_BOOST_SERIALIZATION
158 friend class boost::serialization::access;
159 template<class ARCHIVE>
160 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
161 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
162 ar & BOOST_SERIALIZATION_NVP(measured_);
163 ar & BOOST_SERIALIZATION_NVP(K_);
164 ar & BOOST_SERIALIZATION_NVP(referencePoint_);
165 }
166#endif
167};
168
169} // \ 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
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
Cal3_S2::shared_ptr K_
shared pointer to calibration object
Definition InvDepthFactorVariant2.h:43
const Point2 & imagePoint() const
return the measurement
Definition InvDepthFactorVariant2.h:140
Point3 referencePoint_
the reference point/origin for this landmark
Definition InvDepthFactorVariant2.h:44
InvDepthFactorVariant2()
Default constructor.
Definition InvDepthFactorVariant2.h:61
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition InvDepthFactorVariant2.h:93
~InvDepthFactorVariant2() override
Virtual destructor.
Definition InvDepthFactorVariant2.h:79
const Cal3_S2::shared_ptr calibration() const
return the calibration object
Definition InvDepthFactorVariant2.h:145
const Point3 & referencePoint() const
return the calibration object
Definition InvDepthFactorVariant2.h:150
void print(const std::string &s="InvDepthFactorVariant2", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition InvDepthFactorVariant2.h:86
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition InvDepthFactorVariant2.h:58
NoiseModelFactorT< Vector2, Pose3, Vector3 > Base
shorthand for base class type
Definition InvDepthFactorVariant2.h:49
InvDepthFactorVariant2(const Key poseKey, const Key landmarkKey, const Point2 &measured, const Cal3_S2::shared_ptr &K, const Point3 referencePoint, const SharedNoiseModel &model)
Constructor.
Definition InvDepthFactorVariant2.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 InvDepthFactorVariant2.h:121
Point2 measured_
2D measurement
Definition InvDepthFactorVariant2.h:42
InvDepthFactorVariant2 This
shorthand for this class
Definition InvDepthFactorVariant2.h:55