gtsam
Loading...
Searching...
No Matches
TriangulationFactor.h
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
17
18#pragma once
19
22
23#include <type_traits>
24
25namespace gtsam {
26
32template<class CAMERA>
34
35public:
36
38 using Camera = CAMERA;
40 using Measurement = typename CAMERA::Measurement;
41
42protected:
43
46
49
50 // Keep a copy of measurement and calibration for I/O
51 const CAMERA camera_;
53
54 // verbosity handling for Cheirality Exceptions
55 const bool throwCheirality_;
56 const bool verboseCheirality_;
57
58public:
60 using shared_ptr = std::shared_ptr<This>;
61
62 // Provide access to the Matrix& version of evaluateError:
63 using NoiseModelFactor1<Point3>::evaluateError;
64
69
79 TriangulationFactor(const CAMERA& camera, const Measurement& measured,
80 const SharedNoiseModel& model, Key pointKey, bool throwCheirality = false,
81 bool verboseCheirality = false) :
82 Base(model, pointKey), camera_(camera), measured_(measured), throwCheirality_(
84 if (model && !noiseModel::matchesDimension(*model, measured_))
85 throw std::invalid_argument(
86 "TriangulationFactor must be created with "
87 + std::to_string((int) traits<Measurement>::dimension)
88 + "-dimensional noise model.");
89 }
90
93 }
94
96 gtsam::NonlinearFactor::shared_ptr clone() const override {
97 return std::static_pointer_cast<gtsam::NonlinearFactor>(
98 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
99 }
100
106 void print(const std::string& s = "", const KeyFormatter& keyFormatter =
107 DefaultKeyFormatter) const override {
108 std::cout << s << "TriangulationFactor,";
109 camera_.print("camera");
111 Base::print("", keyFormatter);
112 }
113
115 bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
116 const This *e = dynamic_cast<const This*>(&p);
117 return e && Base::equals(p, tol) && this->camera_.equals(e->camera_, tol)
118 && traits<Measurement>::Equals(this->measured_, e->measured_, tol);
119 }
120
122 Vector evaluateError(const Point3& point, OptionalMatrixType H2) const override {
123 try {
124 const Measurement predicted = camera_.project2(point, OptionalNone, H2);
126 !std::is_base_of_v<
129 if (H2) {
131 const Vector error =
132 traits<Measurement>::Local(measured_, predicted, {}, Hlocal);
133 *H2 = Hlocal * *H2;
134 return error;
135 }
136 }
137 return traits<Measurement>::Local(measured_, predicted);
138 } catch (CheiralityException& e) {
139 if (H2)
140 *H2 = Matrix::Zero(traits<Measurement>::dimension, 3);
142 std::cout << e.what() << ": Landmark "
143 << DefaultKeyFormatter(this->key()) << " moved behind camera"
144 << std::endl;
146 throw e;
147 return camera_.defaultErrorWhenTriangulatingBehindCamera();
148 }
149 }
150
153 mutable Matrix A;
154 mutable Vector b;
155
161 std::shared_ptr<GaussianFactor> linearize(const Values& x) const override {
162 // Only linearize if the factor is active
163 if (!this->active(x))
164 return std::shared_ptr<JacobianFactor>();
165
166 // Allocate memory for Jacobian factor, do only once
167 if (Ab.rows() == 0) {
168 std::vector<size_t> dimensions(1, 3);
172 }
173
174 // Would be even better if we could pass blocks to project
175 const Point3& point = x.at<Point3>(key());
176 b = -evaluateError(point, &A);
177 if (noiseModel_)
178 this->noiseModel_->WhitenSystem(A, b);
179
180 Ab(0) = A;
181 Ab(1) = b;
182
183 return std::make_shared<JacobianFactor>(this->keys_, Ab);
184 }
185
187 const Measurement& measured() const {
188 return measured_;
189 }
190
192 inline bool verboseCheirality() const {
193 return verboseCheirality_;
194 }
195
197 inline bool throwCheirality() const {
198 return throwCheirality_;
199 }
200
201private:
202
203#if GTSAM_ENABLE_BOOST_SERIALIZATION
205 friend class boost::serialization::access;
206 template<class ARCHIVE>
207 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
208 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
209 ar & BOOST_SERIALIZATION_NVP(camera_);
210 ar & BOOST_SERIALIZATION_NVP(measured_);
211 ar & BOOST_SERIALIZATION_NVP(throwCheirality_);
212 ar & BOOST_SERIALIZATION_NVP(verboseCheirality_);
213 }
214#endif
215};
216} // \ namespace gtsam
Calibrated camera for which only pose is unknown.
Base class for noise model factors with N variables.
#define OptionalNone
These typedefs and aliases will help with making the evaluateError interface independent of boost TOD...
Definition NonlinearFactor.h:51
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
NoiseModelFactorT< Vector, ValueTypes... > NoiseModelFactorN
Noise model factor with N value types and dynamic-sized error vector.
Definition NoiseModelFactorN.h:561
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
bool matchesDimension(const Base &model, const T &measured)
Return true if the model dimension matches the manifold dimension.
Definition NoiseModel.h:170
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Detect whether a traits type provides Local with Jacobians.
Definition Manifold.h:145
tag to assert a type is a vector space
Definition VectorSpace.h:21
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
Definition CalibratedCamera.h:35
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
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
Key key() const
Definition NoiseModelFactorN.h:307
Nonlinear factor base class.
Definition NonlinearFactor.h:70
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition NonlinearFactor.h:143
double error(const Values &c) const override
Calculate the error of the factor.
Definition NonlinearFactor.cpp:146
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition Values-inl.h:260
VerticalBlockMatrix Ab
thread-safe (?) scratch memory for linearize
Definition TriangulationFactor.h:152
const bool throwCheirality_
If true, rethrows Cheirality exceptions (default: false).
Definition TriangulationFactor.h:55
bool throwCheirality() const
return flag for throwing cheirality exceptions
Definition TriangulationFactor.h:197
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition TriangulationFactor.h:106
Vector evaluateError(const Point3 &point, OptionalMatrixType H2) const override
Evaluate error h(x)-z and optionally derivatives.
Definition TriangulationFactor.h:122
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition TriangulationFactor.h:60
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition TriangulationFactor.h:96
TriangulationFactor(const CAMERA &camera, const Measurement &measured, const SharedNoiseModel &model, Key pointKey, bool throwCheirality=false, bool verboseCheirality=false)
Constructor with exception-handling flags.
Definition TriangulationFactor.h:79
typename CAMERA::Measurement Measurement
shorthand for measurement type, e.g. Point2 or StereoPoint2
Definition TriangulationFactor.h:40
const Measurement & measured() const
return the measurement
Definition TriangulationFactor.h:187
bool verboseCheirality() const
return verbosity
Definition TriangulationFactor.h:192
CAMERA Camera
CAMERA type.
Definition TriangulationFactor.h:38
std::shared_ptr< GaussianFactor > linearize(const Values &x) const override
Linearize to a JacobianFactor, does not support constrained noise model !
Definition TriangulationFactor.h:161
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Definition TriangulationFactor.h:115
~TriangulationFactor() override
Virtual destructor.
Definition TriangulationFactor.h:92
NoiseModelFactorN< Point3 > Base
shorthand for base class type
Definition TriangulationFactor.h:45
const bool verboseCheirality_
If true, prints text for Cheirality exceptions (default: false).
Definition TriangulationFactor.h:56
TriangulationFactor()
Default constructor.
Definition TriangulationFactor.h:66
TriangulationFactor< CAMERA > This
shorthand for this class
Definition TriangulationFactor.h:48
const Measurement measured_
2D measurement
Definition TriangulationFactor.h:52
const CAMERA camera_
CAMERA in which this landmark was seen.
Definition TriangulationFactor.h:51