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:
62 
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 
83  const std::function<R(T, OptionalMatrixType)> func)
84  : Base(model, key), measured_(z), noiseModel_(model), func_(func) {}
85 
86  ~FunctorizedFactor() override {}
87 
90  return std::static_pointer_cast<NonlinearFactor>(
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) {
152 }
153 
163 template <typename R, typename T1, typename T2>
164 class FunctorizedFactor2 : public NoiseModelFactorN<T1, T2> {
165  private:
167 
170  using FunctionType = std::function<R(T1, T2, OptionalMatrixType, OptionalMatrixType)>;
172 
173  public:
174 
175  // Provide access to the Matrix& version of evaluateError:
176  using Base::evaluateError;
177 
180 
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 
199  return std::static_pointer_cast<NonlinearFactor>(
201  }
202 
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>
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) {
264 }
265 
266 } // namespace gtsam
R measured_
value that is compared with functor return value
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
R measured_
value that is compared with functor return value
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
NonlinearFactor::shared_ptr clone() const override
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)
std::string serialize(const T &input)
serializes to a string
noiseModel::Diagonal::shared_ptr model
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
std::function< R(T1, T2, OptionalMatrixType, OptionalMatrixType)> FunctionType
Rot2 R(Rot2::fromAngle(0.1))
FunctionType func_
functor instance
FunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model, const std::function< R(T, OptionalMatrixType)> func)
static const Pose3 T2(Rot3::Rodrigues(0.3, 0.2, 0.1), P2)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange [*:*] noreverse nowriteback set trange [*:*] noreverse nowriteback set urange [*:*] noreverse nowriteback set vrange [*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
FunctorizedFactor2< R, T1, T2 > MakeFunctorizedFactor2(Key key1, Key key2, const R &z, const SharedNoiseModel &model, const FUNC func)
static const SmartProjectionParams params
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Matrix * OptionalMatrixType
Eigen::VectorXd Vector
Definition: Vector.h:38
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
Vector evaluateError(const T1 &params1, const T2 &params2, OptionalMatrixType H1, OptionalMatrixType H2) const override
std::function< R(T, OptionalMatrixType)> func_
functor instance
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
FunctorizedFactor< R, T > MakeFunctorizedFactor(Key key, const R &z, const SharedNoiseModel &model, const FUNC func)
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
traits
Definition: chartTesting.h:28
int func(const int &a)
Definition: testDSF.cpp:221
double error(const Values &c) const override
Non-linear factor base classes.
static const Similarity3 T1(R, Point3(3.5, -8.2, 4.2), 1)
Vector evaluateError(const T &params, OptionalMatrixType H) const override
std::shared_ptr< This > shared_ptr
SharedNoiseModel noiseModel_
noise model
const G double tol
Definition: Group.h:86
NonlinearFactor::shared_ptr clone() const override
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:62
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741
SharedNoiseModel noiseModel_
noise model


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:15