gtsam
Loading...
Searching...
No Matches
BetweenFactor.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
16#pragma once
17
18#include <ostream>
19
20#include <gtsam/base/Testable.h>
21#include <gtsam/base/Lie.h>
25
26namespace gtsam {
27
42 template<class VALUE>
44 : public NoiseModelFactorT<typename traits<VALUE>::TangentVector, VALUE,
45 VALUE> {
46
47 // Check that VALUE type is a testable Lie group
48 GTSAM_CONCEPT_ASSERT(IsTestable<VALUE>);
49 GTSAM_CONCEPT_ASSERT(IsLieGroup<VALUE>);
50
51 public:
52
53 typedef VALUE T;
54 using ErrorVector = typename traits<VALUE>::TangentVector;
55
56 private:
57
58 typedef BetweenFactor<VALUE> This;
60
61 VALUE measured_;
62
63 public:
64
65 // Provide access to the Matrix& version of evaluateError:
67
68 // shorthand for a smart pointer to a factor
69 typedef typename std::shared_ptr<BetweenFactor> shared_ptr;
70
73
76
78 BetweenFactor(Key key1, Key key2, const VALUE& measured,
79 const SharedNoiseModel& model = nullptr) :
80 Base(noiseModel::validOrDefault(measured, model), key1, key2),
81 measured_(measured) {
82 }
83
85
86 ~BetweenFactor() override {}
87
89 gtsam::NonlinearFactor::shared_ptr clone() const override {
90 return std::static_pointer_cast<gtsam::NonlinearFactor>(
91 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
92
95
97 void print(
98 const std::string& s = "",
99 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
100 std::cout << s << "BetweenFactor("
101 << keyFormatter(this->key1()) << ","
102 << keyFormatter(this->key2()) << ")\n";
103 traits<T>::Print(measured_, " measured: ");
104 this->noiseModel_->print(" noise model: ");
105 }
106
108 bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
109 const This *e = dynamic_cast<const This*> (&expected);
110 return e != nullptr && Base::equals(*e, tol) && traits<T>::Equals(this->measured_, e->measured_, tol);
111 }
112
116
118 ErrorVector evaluateError(const T& p1, const T& p2,
119 OptionalMatrixType H1, OptionalMatrixType H2) const override {
120 T hx = traits<T>::Between(p1, p2, H1, H2); // h(x)
121 // manifold equivalent of h(x)-z -> log(z,h(x))
122#ifdef GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR
124 if (H1 || H2) {
126 Vector error =
127 traits<T>::Local(measured_, hx, OptionalNone, &Hlocal);
128 if (H1) *H1 = Hlocal * (*H1);
129 if (H2) *H2 = Hlocal * (*H2);
130 return error;
131 }
132 }
133#endif
134 return traits<T>::Local(measured_, hx);
135 }
136
140
142 const VALUE& measured() const {
143 return measured_;
144 }
145
146
147 private:
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 // NoiseModelFactor2 instead of NoiseModelFactorN for backward compatibility
155 ar & boost::serialization::make_nvp("NoiseModelFactor2",
156 boost::serialization::base_object<Base>(*this));
157 ar & BOOST_SERIALIZATION_NVP(measured_);
158 }
159#endif
160 }; // \class BetweenFactor
161
163 template<class VALUE>
164 struct traits<BetweenFactor<VALUE> > : public Testable<BetweenFactor<VALUE> > {};
165
171 template<class VALUE>
172 class BetweenConstraint : public BetweenFactor<VALUE> {
173 public:
174 typedef std::shared_ptr<BetweenConstraint<VALUE> > shared_ptr;
175
177 BetweenConstraint(const VALUE& measured, Key key1, Key key2, double mu = 1000.0) :
178 BetweenFactor<VALUE>(key1, key2, measured,
179 noiseModel::Constrained::All(traits<VALUE>::GetDimension(measured), std::abs(mu)))
180 {}
181
182 private:
183
185#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 ar & boost::serialization::make_nvp("BetweenFactor",
190 boost::serialization::base_object<BetweenFactor<VALUE> >(*this));
191 }
192#endif
193 }; // \class BetweenConstraint
194
196 template<class VALUE>
197 struct traits<BetweenConstraint<VALUE> > : public Testable<BetweenConstraint<VALUE> > {};
198
199}
Concept check for values that can be used in unit tests.
Base class and basic functions for Lie types.
Arbitrary-arity Jacobian factor with compile-time block dimensions.
Base class for noise model factors with N variables.
Non-linear factor base classes.
#define OptionalNone
These typedefs and aliases will help with making the evaluateError interface independent of boost TOD...
Definition NonlinearFactor.h:51
STL namespace.
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
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
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Lie Group Concept.
Definition Lie.h:377
Detect whether a traits type provides Local with Jacobians.
Definition Manifold.h:145
A testable concept check that should be placed in applicable unit tests and in generic algorithms.
Definition Testable.h:59
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
A Constrained constrained model is a specialization of Diagonal which allows some or all of the sigma...
Definition NoiseModel.h:436
virtual ErrorVector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Nonlinear factor base class.
Definition NonlinearFactor.h:70
double error(const Values &c) const override
Calculate the error of the factor.
Definition NonlinearFactor.cpp:146
A class for a measurement predicted by "between(config[key1],config[key2])".
Definition BetweenFactor.h:45
BetweenFactor()
default constructor - only use for serialization
Definition BetweenFactor.h:75
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
assert equality up to a tolerance
Definition BetweenFactor.h:108
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition BetweenFactor.h:89
const double & measured() const
Definition BetweenFactor.h:142
BetweenFactor(Key key1, Key key2, const VALUE &measured, const SharedNoiseModel &model=nullptr)
Constructor.
Definition BetweenFactor.h:78
ErrorVector evaluateError(const T &p1, const T &p2, OptionalMatrixType H1, OptionalMatrixType H2) const override
evaluate error, returns vector of errors size of tangent space
Definition BetweenFactor.h:118
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print with optional string
Definition BetweenFactor.h:97
Binary between constraint - forces between to a given value This constraint requires the underlying t...
Definition BetweenFactor.h:172
BetweenConstraint(const VALUE &measured, Key key1, Key key2, double mu=1000.0)
Syntactic sugar for constrained version.
Definition BetweenFactor.h:177