gtsam
Loading...
Searching...
No Matches
InvDepthFactorVariant1.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, Vector6> {
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
71 InvDepthFactorVariant1(const Key poseKey, const Key landmarkKey,
72 const Point2& measured, const Cal3_S2::shared_ptr& K, const SharedNoiseModel& model) :
73 Base(model, poseKey, landmarkKey), measured_(measured), K_(K) {}
74
77
83 void print(const std::string& s = "InvDepthFactorVariant1",
84 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
85 Base::print(s, keyFormatter);
87 }
88
90 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
91 const This *e = dynamic_cast<const This*>(&p);
92 return e
93 && Base::equals(p, tol)
94 && traits<Point2>::Equals(this->measured_, e->measured_, tol)
95 && this->K_->equals(*e->K_, tol);
96 }
97
98 Vector inverseDepthError(const Pose3& pose, const Vector6& landmark) const {
99 try {
100 // Calculate the 3D coordinates of the landmark in the world frame
101 double x = landmark(0), y = landmark(1), z = landmark(2);
102 double theta = landmark(3), phi = landmark(4), rho = landmark(5);
103 Point3 world_P_landmark = Point3(x, y, z) + Point3(cos(theta)*cos(phi)/rho, sin(theta)*cos(phi)/rho, sin(phi)/rho);
104 // Project landmark into Pose2
105 PinholeCamera<Cal3_S2> camera(pose, *K_);
106 return camera.project(world_P_landmark) - measured_;
107 } catch( CheiralityException& e) {
108 std::cout << e.what()
109 << ": Inverse Depth Landmark [" << DefaultKeyFormatter(this->key<2>()) << "]"
110 << " moved behind camera [" << DefaultKeyFormatter(this->key<1>()) <<"]"
111 << std::endl;
112 return Vector::Ones(2) * 2.0 * K_->fx();
113 }
114 return Vector{{0.0}};
115 }
116
118 Vector2 evaluateError(const Pose3& pose, const Vector6& landmark,
120 OptionalMatrixType H2) const override {
121
122 if (H1) {
124 std::bind(&InvDepthFactorVariant1::inverseDepthError, this,
125 std::placeholders::_1, landmark),
126 pose);
127 }
128 if (H2) {
130 std::bind(&InvDepthFactorVariant1::inverseDepthError, this, pose,
131 std::placeholders::_1), landmark);
132 }
133
134 return inverseDepthError(pose, landmark);
135 }
136
138 const Point2& imagePoint() const {
139 return measured_;
140 }
141
143 const Cal3_S2::shared_ptr calibration() const {
144 return K_;
145 }
146
147private:
148
149#if GTSAM_ENABLE_BOOST_SERIALIZATION
151 friend class boost::serialization::access;
152 template<class ARCHIVE>
153 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
154 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
155 ar & BOOST_SERIALIZATION_NVP(measured_);
156 ar & BOOST_SERIALIZATION_NVP(K_);
157 }
158#endif
159};
160
161} // \ 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
~InvDepthFactorVariant1() override
Virtual destructor.
Definition InvDepthFactorVariant1.h:76
const Point2 & imagePoint() const
return the measurement
Definition InvDepthFactorVariant1.h:138
Vector2 evaluateError(const Pose3 &pose, const Vector6 &landmark, OptionalMatrixType H1, OptionalMatrixType H2) const override
Evaluate error h(x)-z and optionally derivatives.
Definition InvDepthFactorVariant1.h:118
InvDepthFactorVariant1(const Key poseKey, const Key landmarkKey, const Point2 &measured, const Cal3_S2::shared_ptr &K, const SharedNoiseModel &model)
Constructor.
Definition InvDepthFactorVariant1.h:71
void print(const std::string &s="InvDepthFactorVariant1", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition InvDepthFactorVariant1.h:83
InvDepthFactorVariant1()
Default constructor.
Definition InvDepthFactorVariant1.h:59
InvDepthFactorVariant1 This
shorthand for this class
Definition InvDepthFactorVariant1.h:53
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition InvDepthFactorVariant1.h:56
const Cal3_S2::shared_ptr calibration() const
return the calibration object
Definition InvDepthFactorVariant1.h:143
Cal3_S2::shared_ptr K_
shared pointer to calibration object
Definition InvDepthFactorVariant1.h:42
Point2 measured_
2D measurement
Definition InvDepthFactorVariant1.h:41
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition InvDepthFactorVariant1.h:90
NoiseModelFactorT< Vector2, Pose3, Vector6 > Base
shorthand for base class type
Definition InvDepthFactorVariant1.h:47