gtsam
Loading...
Searching...
No Matches
FunctorizedFactor.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
17
18#pragma once
19
20#include <gtsam/base/Manifold.h>
21#include <gtsam/base/Testable.h>
24
25#include <cmath>
26
27namespace gtsam {
28
60template <typename R, typename T>
62 private:
63 using Base = NoiseModelFactorN<T>;
64
65 R measured_;
66 SharedNoiseModel noiseModel_;
67 std::function<R(T, OptionalMatrixType)> func_;
68
69 public:
70
71 // Provide access to the Matrix& version of evaluateError:
73
76
84 FunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model,
85 const std::function<R(T, OptionalMatrixType)> func)
86 : Base(model, key), measured_(z), noiseModel_(model), func_(func) {}
87
88 ~FunctorizedFactor() override {}
89
91 NonlinearFactor::shared_ptr clone() const override {
92 return std::static_pointer_cast<NonlinearFactor>(
93 NonlinearFactor::shared_ptr(new FunctorizedFactor<R, T>(*this)));
94 }
95
96 Vector evaluateError(const T &params, OptionalMatrixType H) const override {
97#ifdef GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR
99 if (H) {
100 Matrix Hprojection;
101 const R value = func_(params, &Hprojection);
103 const Vector error =
104 traits<R>::Local(measured_, value, OptionalNone, &Hlocal);
105 *H = Hlocal * Hprojection;
106 return error;
107 }
108 }
109#endif
110 R x = func_(params, H);
111 Vector error = traits<R>::Local(measured_, x);
112 return error;
113 }
114
117 void print(
118 const std::string &s = "",
119 const KeyFormatter &keyFormatter = DefaultKeyFormatter) const override {
120 Base::print(s, keyFormatter);
121 std::cout << s << (s != "" ? " " : "") << "FunctorizedFactor("
122 << keyFormatter(this->key1()) << ")" << std::endl;
123 traits<R>::Print(measured_, " measurement: ");
124 std::cout << " noise model sigmas: " << noiseModel_->sigmas().transpose()
125 << std::endl;
126 }
127
128 bool equals(const NonlinearFactor &other, double tol = 1e-9) const override {
129 const FunctorizedFactor<R, T> *e =
130 dynamic_cast<const FunctorizedFactor<R, T> *>(&other);
131 return e != nullptr && Base::equals(other, tol) &&
132 traits<R>::Equals(this->measured_, e->measured_, tol);
133 }
134
135
136 private:
137#if GTSAM_ENABLE_BOOST_SERIALIZATION
139 friend class boost::serialization::access;
140 template <class ARCHIVE>
141 void serialize(ARCHIVE &ar, const unsigned int /*version*/) {
142 // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
143 ar &boost::serialization::make_nvp(
144 "NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
145 ar &BOOST_SERIALIZATION_NVP(measured_);
146 ar &BOOST_SERIALIZATION_NVP(func_);
147 }
148#endif
149};
150
152template <typename R, typename T>
154 : public Testable<FunctorizedFactor<R, T>> {};
155
162template <typename T, typename R, typename FUNC>
164 const SharedNoiseModel &model,
165 const FUNC func) {
166 return FunctorizedFactor<R, T>(key, z, model, func);
167}
168
178template <typename R, typename T1, typename T2>
179class FunctorizedFactor2 : public NoiseModelFactorN<T1, T2> {
180 private:
181 using Base = NoiseModelFactorN<T1, T2>;
182
183 R measured_;
184 SharedNoiseModel noiseModel_;
185 using FunctionType = std::function<R(T1, T2, OptionalMatrixType, OptionalMatrixType)>;
186 FunctionType func_;
187
188 public:
189
190 // Provide access to the Matrix& version of evaluateError:
192
195
203 FunctorizedFactor2(Key key1, Key key2, const R &z,
204 const SharedNoiseModel &model, const FunctionType func)
205 : Base(model, key1, key2),
206 measured_(z),
207 noiseModel_(model),
208 func_(func) {}
209
210 ~FunctorizedFactor2() override {}
211
213 NonlinearFactor::shared_ptr clone() const override {
214 return std::static_pointer_cast<NonlinearFactor>(
215 NonlinearFactor::shared_ptr(new FunctorizedFactor2<R, T1, T2>(*this)));
216 }
217
218 Vector evaluateError(
219 const T1 &params1, const T2 &params2,
220 OptionalMatrixType H1, OptionalMatrixType H2) const override {
221#ifdef GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR
223 if (H1 || H2) {
224 Matrix Hprojection1, Hprojection2;
225 const R value = func_(params1, params2,
226 H1 ? &Hprojection1 : nullptr,
227 H2 ? &Hprojection2 : nullptr);
229 const Vector error =
230 traits<R>::Local(measured_, value, OptionalNone, &Hlocal);
231 if (H1) *H1 = Hlocal * Hprojection1;
232 if (H2) *H2 = Hlocal * Hprojection2;
233 return error;
234 }
235 }
236#endif
237 R x = func_(params1, params2, H1, H2);
238 Vector error = traits<R>::Local(measured_, x);
239 return error;
240 }
241
244 void print(
245 const std::string &s = "",
246 const KeyFormatter &keyFormatter = DefaultKeyFormatter) const override {
247 Base::print(s, keyFormatter);
248 std::cout << s << (s != "" ? " " : "") << "FunctorizedFactor2("
249 << keyFormatter(this->key1()) << ", "
250 << keyFormatter(this->key2()) << ")" << std::endl;
251 traits<R>::Print(measured_, " measurement: ");
252 std::cout << " noise model sigmas: " << noiseModel_->sigmas().transpose()
253 << std::endl;
254 }
255
256 bool equals(const NonlinearFactor &other, double tol = 1e-9) const override {
258 dynamic_cast<const FunctorizedFactor2<R, T1, T2> *>(&other);
259 return e && Base::equals(other, tol) &&
260 traits<R>::Equals(this->measured_, e->measured_, tol);
261 }
262
263
264 private:
265#if GTSAM_ENABLE_BOOST_SERIALIZATION
267 friend class boost::serialization::access;
268 template <class ARCHIVE>
269 void serialize(ARCHIVE &ar, const unsigned int /*version*/) {
270 // NoiseModelFactor2 instead of NoiseModelFactorN for backward compatibility
271 ar &boost::serialization::make_nvp(
272 "NoiseModelFactor2", boost::serialization::base_object<Base>(*this));
273 ar &BOOST_SERIALIZATION_NVP(measured_);
274 ar &BOOST_SERIALIZATION_NVP(func_);
275 }
276#endif
277};
278
280template <typename R, typename T1, typename T2>
281struct traits<FunctorizedFactor2<R, T1, T2>>
282 : public Testable<FunctorizedFactor2<R, T1, T2>> {};
283
290template <typename T1, typename T2, typename R, typename FUNC>
292 Key key1, Key key2, const R &z, const SharedNoiseModel &model,
293 const FUNC func) {
294 return FunctorizedFactor2<R, T1, T2>(key1, key2, z, model, func);
295}
296
297} // namespace gtsam
Concept check for values that can be used in unit tests.
Base class and basic functions for Manifold types.
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
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FunctorizedFactor2< R, T1, T2 > MakeFunctorizedFactor2(Key key1, Key key2, const R &z, const SharedNoiseModel &model, const FUNC func)
Helper function to create a functorized factor.
Definition FunctorizedFactor.h:291
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
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
FunctorizedFactor< R, T > MakeFunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model, const FUNC func)
Helper function to create a functorized factor.
Definition FunctorizedFactor.h:163
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
Detect whether a traits type provides Local with Jacobians.
Definition Manifold.h:145
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
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
virtual double error(const HybridValues &hybridValues) const
All factor types need to implement an error function.
Definition Factor.cpp:47
Factor which evaluates provided unary functor and uses the result to compute error with respect to th...
Definition FunctorizedFactor.h:61
FunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model, const std::function< R(T, OptionalMatrixType)> func)
Construct with given x and the parameters of the basis.
Definition FunctorizedFactor.h:84
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
Check if two factors are equal.
Definition FunctorizedFactor.h:128
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition FunctorizedFactor.h:117
FunctorizedFactor()
default constructor - only use for serialization
Definition FunctorizedFactor.h:75
NonlinearFactor::shared_ptr clone() const override
Definition FunctorizedFactor.h:91
Factor which evaluates provided binary functor and uses the result to compute error with respect to t...
Definition FunctorizedFactor.h:179
FunctorizedFactor2()
default constructor - only use for serialization
Definition FunctorizedFactor.h:194
NonlinearFactor::shared_ptr clone() const override
Definition FunctorizedFactor.h:213
FunctorizedFactor2(Key key1, Key key2, const R &z, const SharedNoiseModel &model, const FunctionType func)
Construct with given x and the parameters of the basis.
Definition FunctorizedFactor.h:203
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition FunctorizedFactor.h:244
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
Check if two factors are equal.
Definition FunctorizedFactor.h:256
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Key key() const
Definition NoiseModelFactorN.h:307
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