gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 namespace gtsam {
25 
31  template<class VALUE>
32  class BetweenFactor: public NoiseModelFactor2<VALUE, VALUE> {
33 
34  public:
35 
36  typedef VALUE T;
37 
38  private:
39 
40  typedef BetweenFactor<VALUE> This;
42 
43  VALUE measured_;
46  GTSAM_CONCEPT_LIE_TYPE(T)
47  GTSAM_CONCEPT_TESTABLE_TYPE(T)
48 
49  public:
50 
51  // shorthand for a smart pointer to a factor
52  typedef typename boost::shared_ptr<BetweenFactor> shared_ptr;
53 
56 
58  BetweenFactor(Key key1, Key key2, const VALUE& measured,
59  const SharedNoiseModel& model) :
60  Base(model, key1, key2), measured_(measured) {
61  }
62 
63  virtual ~BetweenFactor() {}
64 
66  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
67  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
68  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
69 
73  virtual void print(const std::string& s, const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
74  std::cout << s << "BetweenFactor("
75  << keyFormatter(this->key1()) << ","
76  << keyFormatter(this->key2()) << ")\n";
77  measured_.print(" measured: ");
78  this->noiseModel_->print(" noise model: ");
79  }
80 
82  virtual bool equals(const NonlinearFactor& expected, double tol=1e-9) const {
83  const This *e = dynamic_cast<const This*> (&expected);
84  return e != NULL && Base::equals(*e, tol) && this->measured_.equals(e->measured_, tol);
85  }
86 
90  Vector evaluateError(const T& p1, const T& p2,
91  boost::optional<Matrix&> H1 = boost::none, boost::optional<Matrix&> H2 =
92  boost::none) const {
93  T hx = p1.between(p2, H1, H2); // h(x)
94  // manifold equivalent of h(x)-z -> log(z,h(x))
95  return measured_.localCoordinates(hx);
96  }
97 
99  const VALUE& measured() const {
100  return measured_;
101  }
102 
104  std::size_t size() const {
105  return 2;
106  }
107 
108  private:
109 
112  template<class ARCHIVE>
113  void serialize(ARCHIVE & ar, const unsigned int version) {
114  ar & boost::serialization::make_nvp("NoiseModelFactor2",
115  boost::serialization::base_object<Base>(*this));
116  ar & BOOST_SERIALIZATION_NVP(measured_);
117  }
118  }; // \class BetweenFactor
119 
125  template<class VALUE>
126  class BetweenConstraint : public BetweenFactor<VALUE> {
127  public:
128  typedef boost::shared_ptr<BetweenConstraint<VALUE> > shared_ptr;
129 
131  BetweenConstraint(const VALUE& measured, Key key1, Key key2, double mu = 1000.0) :
132  BetweenFactor<VALUE>(key1, key2, measured, noiseModel::Constrained::All(VALUE::Dim(), fabs(mu))) {}
133 
134  private:
135 
138  template<class ARCHIVE>
139  void serialize(ARCHIVE & ar, const unsigned int version) {
140  ar & boost::serialization::make_nvp("BetweenFactor",
141  boost::serialization::base_object<BetweenFactor<VALUE> >(*this));
142  }
143  }; // \class BetweenConstraint
144 
145 }
Non-linear factor base classes.
friend class boost::serialization::access
Serialization function.
Definition: BetweenFactor.h:111
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.h:239
BetweenFactor()
default constructor - only use for serialization
Definition: BetweenFactor.h:55
Vector evaluateError(const T &p1, const T &p2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
implement functions needed to derive from Factor
Definition: BetweenFactor.h:90
const VALUE & measured() const
return the measured
Definition: BetweenFactor.h:99
Base class and basic functions for Lie types.
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: BetweenFactor.h:66
Definition: BetweenFactor.h:32
boost::shared_ptr< BetweenFactor > shared_ptr
The measurement.
Definition: BetweenFactor.h:52
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:423
std::size_t size() const
number of variables attached to this factor
Definition: BetweenFactor.h:104
This is the base class for all factor types.
Definition: Factor.h:51
virtual void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
implement functions needed for Testable
Definition: BetweenFactor.h:73
Binary between constraint - forces between to a given value This constraint requires the underlying t...
Definition: BetweenFactor.h:126
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:455
virtual bool equals(const NonlinearFactor &expected, double tol=1e-9) const
equals
Definition: BetweenFactor.h:82
Concept check for values that can be used in unit tests.
BetweenConstraint(const VALUE &measured, Key key1, Key key2, double mu=1000.0)
Syntactic sugar for constrained version.
Definition: BetweenFactor.h:131
friend class boost::serialization::access
Serialization function.
Definition: BetweenFactor.h:137
size_t Key
Integer nonlinear key type.
Definition: types.h:59
BetweenFactor(Key key1, Key key2, const VALUE &measured, const SharedNoiseModel &model)
Constructor.
Definition: BetweenFactor.h:58
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:884
Nonlinear factor base class.
Definition: NonlinearFactor.h:54
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62