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>
23 
24 #ifdef _WIN32
25 #define BETWEENFACTOR_VISIBILITY
26 #else
27 // This will trigger a LNKxxxx on MSVC, so disable for MSVC build
28 // Please refer to https://github.com/borglab/gtsam/blob/develop/Using-GTSAM-EXPORT.md
29 #define BETWEENFACTOR_VISIBILITY GTSAM_EXPORT
30 #endif
31 
32 namespace gtsam {
33 
39  template<class VALUE>
40  class BetweenFactor: public NoiseModelFactorN<VALUE, VALUE> {
41 
42  // Check that VALUE type is a testable Lie group
45 
46  public:
47 
48  typedef VALUE T;
49 
50  private:
51 
54 
57  public:
58 
59  // Provide access to the Matrix& version of evaluateError:
60  using Base::evaluateError;
61 
62  // shorthand for a smart pointer to a factor
63  typedef typename std::shared_ptr<BetweenFactor> shared_ptr;
64 
67 
70 
73  const SharedNoiseModel& model = nullptr) :
74  Base(model, key1, key2), measured_(measured) {
75  }
76 
78 
79  ~BetweenFactor() override {}
80 
83  return std::static_pointer_cast<gtsam::NonlinearFactor>(
85 
88 
90  void print(
91  const std::string& s = "",
92  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
93  std::cout << s << "BetweenFactor("
94  << keyFormatter(this->key1()) << ","
95  << keyFormatter(this->key2()) << ")\n";
96  traits<T>::Print(measured_, " measured: ");
97  this->noiseModel_->print(" noise model: ");
98  }
99 
101  bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
102  const This *e = dynamic_cast<const This*> (&expected);
103  return e != nullptr && Base::equals(*e, tol) && traits<T>::Equals(this->measured_, e->measured_, tol);
104  }
105 
109 
111  Vector evaluateError(const T& p1, const T& p2,
112  OptionalMatrixType H1, OptionalMatrixType H2) const override {
113  T hx = traits<T>::Between(p1, p2, H1, H2); // h(x)
114  // manifold equivalent of h(x)-z -> log(z,h(x))
115 #ifdef GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR
116  typename traits<T>::ChartJacobian::Jacobian Hlocal;
117  Vector rval = traits<T>::Local(measured_, hx, OptionalNone, (H1 || H2) ? &Hlocal : 0);
118  if (H1) *H1 = Hlocal * (*H1);
119  if (H2) *H2 = Hlocal * (*H2);
120  return rval;
121 #else
122  return traits<T>::Local(measured_, hx);
123 #endif
124  }
125 
129 
131  const VALUE& measured() const {
132  return measured_;
133  }
135 
136  private:
137 
138 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
139 
140  friend class boost::serialization::access;
141  template<class ARCHIVE>
142  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
143  // NoiseModelFactor2 instead of NoiseModelFactorN for backward compatibility
144  ar & boost::serialization::make_nvp("NoiseModelFactor2",
145  boost::serialization::base_object<Base>(*this));
146  ar & BOOST_SERIALIZATION_NVP(measured_);
147  }
148 #endif
149 
150  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
151  enum { NeedsToAlign = (sizeof(VALUE) % 16) == 0 };
152  public:
154  }; // \class BetweenFactor
155 
157  template<class VALUE>
158  struct traits<BetweenFactor<VALUE> > : public Testable<BetweenFactor<VALUE> > {};
159 
165  template<class VALUE>
166  class BetweenConstraint : public BetweenFactor<VALUE> {
167  public:
168  typedef std::shared_ptr<BetweenConstraint<VALUE> > shared_ptr;
169 
171  BetweenConstraint(const VALUE& measured, Key key1, Key key2, double mu = 1000.0) :
172  BetweenFactor<VALUE>(key1, key2, measured,
173  noiseModel::Constrained::All(traits<VALUE>::GetDimension(measured), std::abs(mu)))
174  {}
175 
176  private:
177 
179 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
180  friend class boost::serialization::access;
181  template<class ARCHIVE>
182  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
183  ar & boost::serialization::make_nvp("BetweenFactor",
184  boost::serialization::base_object<BetweenFactor<VALUE> >(*this));
185  }
186 #endif
187  }; // \class BetweenConstraint
188 
190  template<class VALUE>
191  struct traits<BetweenConstraint<VALUE> > : public Testable<BetweenConstraint<VALUE> > {};
192 
193 }
Concept check for values that can be used in unit tests.
~BetweenFactor() override
Definition: BetweenFactor.h:79
Vector3f p1
std::string serialize(const T &input)
serializes to a string
noiseModel::Diagonal::shared_ptr model
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print with optional string
Definition: BetweenFactor.h:90
Matrix expected
Definition: testMatrix.cpp:971
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
double mu
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Definition: BFloat16.h:88
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
NoiseModelFactorN< VALUE, VALUE > Base
Definition: BetweenFactor.h:53
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: BetweenFactor.h:82
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
Definition: types.h:293
#define OptionalNone
const SharedNoiseModel & noiseModel() const
access to the noise model
Matrix * OptionalMatrixType
std::shared_ptr< BetweenFactor > shared_ptr
Definition: BetweenFactor.h:63
Eigen::VectorXd Vector
Definition: Vector.h:38
Vector evaluateError(const T &p1, const T &p2, OptionalMatrixType H1, OptionalMatrixType H2) const override
evaluate error, returns vector of errors size of tangent space
BetweenConstraint(const VALUE &measured, Key key1, Key key2, double mu=1000.0)
BetweenFactor< Rot3 > Between
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
std::shared_ptr< BetweenConstraint< VALUE > > shared_ptr
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
Base class and basic functions for Lie types.
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
assert equality up to a tolerance
GTSAM_CONCEPT_ASSERT(IsTestable< VALUE >)
traits
Definition: chartTesting.h:28
SharedNoiseModel noiseModel_
Non-linear factor base classes.
std::shared_ptr< This > shared_ptr
BetweenFactor< VALUE > This
Definition: BetweenFactor.h:52
static Point3 p2
const G double tol
Definition: Group.h:86
#define abs(x)
Definition: datatypes.h:17
const VALUE & measured() const
return the measurement
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
BetweenFactor(Key key1, Key key2, const VALUE &measured, const SharedNoiseModel &model=nullptr)
Definition: BetweenFactor.h:72


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:58