GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
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 
18 #pragma once
19 
20 #include <gtsam/base/Testable.h>
22 
23 #include <cmath>
24 
25 namespace gtsam {
26 
58 template <typename R, typename T>
60  private:
61  using Base = NoiseModelFactorN<T>;
62 
63  R measured_;
64  SharedNoiseModel noiseModel_;
65  std::function<R(T, OptionalMatrixType)> func_;
66 
67  public:
68 
69  // Provide access to the Matrix& version of evaluateError:
70  using Base::evaluateError;
71 
74 
82  FunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model,
83  const std::function<R(T, OptionalMatrixType)> func)
84  : Base(model, key), measured_(z), noiseModel_(model), func_(func) {}
85 
86  ~FunctorizedFactor() override {}
87 
89  NonlinearFactor::shared_ptr clone() const override {
90  return std::static_pointer_cast<NonlinearFactor>(
91  NonlinearFactor::shared_ptr(new FunctorizedFactor<R, T>(*this)));
92  }
93 
94  Vector evaluateError(const T &params, OptionalMatrixType H) const override {
95  R x = func_(params, H);
96  Vector error = traits<R>::Local(measured_, x);
97  return error;
98  }
99 
102  void print(
103  const std::string &s = "",
104  const KeyFormatter &keyFormatter = DefaultKeyFormatter) const override {
105  Base::print(s, keyFormatter);
106  std::cout << s << (s != "" ? " " : "") << "FunctorizedFactor("
107  << keyFormatter(this->key1()) << ")" << std::endl;
108  traits<R>::Print(measured_, " measurement: ");
109  std::cout << " noise model sigmas: " << noiseModel_->sigmas().transpose()
110  << std::endl;
111  }
112 
113  bool equals(const NonlinearFactor &other, double tol = 1e-9) const override {
114  const FunctorizedFactor<R, T> *e =
115  dynamic_cast<const FunctorizedFactor<R, T> *>(&other);
116  return e != nullptr && Base::equals(other, tol) &&
117  traits<R>::Equals(this->measured_, e->measured_, tol);
118  }
120 
121  private:
122 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
123 
124  friend class boost::serialization::access;
125  template <class ARCHIVE>
126  void serialize(ARCHIVE &ar, const unsigned int /*version*/) {
127  // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
128  ar &boost::serialization::make_nvp(
129  "NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
130  ar &BOOST_SERIALIZATION_NVP(measured_);
131  ar &BOOST_SERIALIZATION_NVP(func_);
132  }
133 #endif
134 };
135 
137 template <typename R, typename T>
139  : public Testable<FunctorizedFactor<R, T>> {};
140 
147 template <typename T, typename R, typename FUNC>
149  const SharedNoiseModel &model,
150  const FUNC func) {
151  return FunctorizedFactor<R, T>(key, z, model, func);
152 }
153 
163 template <typename R, typename T1, typename T2>
164 class FunctorizedFactor2 : public NoiseModelFactorN<T1, T2> {
165  private:
167 
168  R measured_;
169  SharedNoiseModel noiseModel_;
170  using FunctionType = std::function<R(T1, T2, OptionalMatrixType, OptionalMatrixType)>;
171  FunctionType func_;
172 
173  public:
174 
175  // Provide access to the Matrix& version of evaluateError:
176  using Base::evaluateError;
177 
180 
188  FunctorizedFactor2(Key key1, Key key2, const R &z,
189  const SharedNoiseModel &model, const FunctionType func)
190  : Base(model, key1, key2),
191  measured_(z),
192  noiseModel_(model),
193  func_(func) {}
194 
195  ~FunctorizedFactor2() override {}
196 
198  NonlinearFactor::shared_ptr clone() const override {
199  return std::static_pointer_cast<NonlinearFactor>(
200  NonlinearFactor::shared_ptr(new FunctorizedFactor2<R, T1, T2>(*this)));
201  }
202 
203  Vector evaluateError(
204  const T1 &params1, const T2 &params2,
205  OptionalMatrixType H1, OptionalMatrixType H2) const override {
206  R x = func_(params1, params2, H1, H2);
207  Vector error = traits<R>::Local(measured_, x);
208  return error;
209  }
210 
213  void print(
214  const std::string &s = "",
215  const KeyFormatter &keyFormatter = DefaultKeyFormatter) const override {
216  Base::print(s, keyFormatter);
217  std::cout << s << (s != "" ? " " : "") << "FunctorizedFactor2("
218  << keyFormatter(this->key1()) << ", "
219  << keyFormatter(this->key2()) << ")" << std::endl;
220  traits<R>::Print(measured_, " measurement: ");
221  std::cout << " noise model sigmas: " << noiseModel_->sigmas().transpose()
222  << std::endl;
223  }
224 
225  bool equals(const NonlinearFactor &other, double tol = 1e-9) const override {
227  dynamic_cast<const FunctorizedFactor2<R, T1, T2> *>(&other);
228  return e && Base::equals(other, tol) &&
229  traits<R>::Equals(this->measured_, e->measured_, tol);
230  }
232 
233  private:
234 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
235 
236  friend class boost::serialization::access;
237  template <class ARCHIVE>
238  void serialize(ARCHIVE &ar, const unsigned int /*version*/) {
239  // NoiseModelFactor2 instead of NoiseModelFactorN for backward compatibility
240  ar &boost::serialization::make_nvp(
241  "NoiseModelFactor2", boost::serialization::base_object<Base>(*this));
242  ar &BOOST_SERIALIZATION_NVP(measured_);
243  ar &BOOST_SERIALIZATION_NVP(func_);
244  }
245 #endif
246 };
247 
249 template <typename R, typename T1, typename T2>
250 struct traits<FunctorizedFactor2<R, T1, T2>>
251  : public Testable<FunctorizedFactor2<R, T1, T2>> {};
252 
259 template <typename T1, typename T2, typename R, typename FUNC>
261  Key key1, Key key2, const R &z, const SharedNoiseModel &model,
262  const FUNC func) {
263  return FunctorizedFactor2<R, T1, T2>(key1, key2, z, model, func);
264 }
265 
266 } // namespace gtsam
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition: FunctorizedFactor.h:213
Definition: NonlinearFactor.h:431
double error(const Values &c) const override
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition: FunctorizedFactor.h:102
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
Definition: FunctorizedFactor.h:113
NonlinearFactor::shared_ptr clone() const override
Definition: FunctorizedFactor.h:89
Concept check for values that can be used in unit tests.
FunctorizedFactor2(Key key1, Key key2, const R &z, const SharedNoiseModel &model, const FunctionType func)
Definition: FunctorizedFactor.h:188
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
Definition: Factor.h:69
Definition: Testable.h:152
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Definition: Group.h:43
Key key() const
Definition: NonlinearFactor.h:582
Definition: NonlinearFactor.h:68
FunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model, const std::function< R(T, OptionalMatrixType)> func)
Definition: FunctorizedFactor.h:82
Definition: FunctorizedFactor.h:59
FunctorizedFactor2< R, T1, T2 > MakeFunctorizedFactor2(Key key1, Key key2, const R &z, const SharedNoiseModel &model, const FUNC func)
Definition: FunctorizedFactor.h:260
Matrix * OptionalMatrixType
Definition: NonlinearFactor.h:55
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
Definition: FunctorizedFactor.h:225
FunctorizedFactor2()
Definition: FunctorizedFactor.h:179
FunctorizedFactor< R, T > MakeFunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model, const FUNC func)
Definition: FunctorizedFactor.h:148
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
Definition: chartTesting.h:28
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: FunctorizedFactor.h:164
Non-linear factor base classes.
FunctorizedFactor()
Definition: FunctorizedFactor.h:73
NonlinearFactor::shared_ptr clone() const override
Definition: FunctorizedFactor.h:198
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741