gtsam  4.0.0
gtsam
RangeFactor.h
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 
19 #pragma once
20 
21 #include <gtsam/nonlinear/ExpressionFactor.h>
22 
23 namespace gtsam {
24 
25 // forward declaration of Range functor, assumed partially specified
26 template <typename A1, typename A2>
27 struct Range;
28 
34 template <typename A1, typename A2 = A1, typename T = double>
35 class RangeFactor : public ExpressionFactor2<T, A1, A2> {
36  private:
37  typedef RangeFactor<A1, A2> This;
39 
40  public:
43 
44  RangeFactor(Key key1, Key key2, T measured, const SharedNoiseModel& model)
45  : Base(key1, key2, model, measured) {
46  this->initialize(expression(key1, key2));
47  }
48 
50  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
51  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
52  gtsam::NonlinearFactor::shared_ptr(new This(*this)));
53  }
54 
55  // Return measurement expression
56  virtual Expression<T> expression(Key key1, Key key2) const {
57  Expression<A1> a1_(key1);
58  Expression<A2> a2_(key2);
59  return Expression<T>(Range<A1, A2>(), a1_, a2_);
60  }
61 
63  void print(const std::string& s = "",
64  const KeyFormatter& kf = DefaultKeyFormatter) const {
65  std::cout << s << "RangeFactor" << std::endl;
66  Base::print(s, kf);
67  }
68 }; // \ RangeFactor
69 
71 template <typename A1, typename A2, typename T>
72 struct traits<RangeFactor<A1, A2, T> >
73  : public Testable<RangeFactor<A1, A2, T> > {};
74 
79 template <typename A1, typename A2 = A1,
80  typename T = typename Range<A1, A2>::result_type>
81 class RangeFactorWithTransform : public ExpressionFactor2<T, A1, A2> {
82  private:
85 
86  A1 body_T_sensor_;
87 
88  public:
91 
93  const SharedNoiseModel& model,
94  const A1& body_T_sensor)
95  : Base(key1, key2, model, measured), body_T_sensor_(body_T_sensor) {
96  this->initialize(expression(key1, key2));
97  }
98 
99  virtual ~RangeFactorWithTransform() {}
100 
102  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
103  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
104  gtsam::NonlinearFactor::shared_ptr(new This(*this)));
105  }
106 
107  // Return measurement expression
108  virtual Expression<T> expression(Key key1, Key key2) const {
109  Expression<A1> body_T_sensor__(body_T_sensor_);
110  Expression<A1> nav_T_body_(key1);
111  Expression<A1> nav_T_sensor_(traits<A1>::Compose, nav_T_body_,
112  body_T_sensor__);
113  Expression<A2> a2_(key2);
114  return Expression<T>(Range<A1, A2>(), nav_T_sensor_, a2_);
115  }
116 
118  void print(const std::string& s = "",
119  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
120  std::cout << s << "RangeFactorWithTransform" << std::endl;
121  this->body_T_sensor_.print(" sensor pose in body frame: ");
122  Base::print(s, keyFormatter);
123  }
124 
125  private:
127  friend class boost::serialization::access;
128  template <typename ARCHIVE>
129  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
130  ar& boost::serialization::make_nvp(
131  "Base", boost::serialization::base_object<Base>(*this));
132  ar& BOOST_SERIALIZATION_NVP(body_T_sensor_);
133  }
134 }; // \ RangeFactorWithTransform
135 
137 template <typename A1, typename A2, typename T>
138 struct traits<RangeFactorWithTransform<A1, A2, T> >
139  : public Testable<RangeFactorWithTransform<A1, A2, T> > {};
140 
141 } // \ namespace gtsam
Expression class that supports automatic differentiation.
Definition: Expression.h:49
Definition: RangeFactor.h:35
Nonlinear factor base class.
Definition: NonlinearFactor.h:52
Definition: BearingRange.h:38
This is the base class for all factor types.
Definition: Factor.h:51
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:1072
virtual Expression< T > expression(Key key1, Key key2) const
Recreate expression from given keys_ and measured_, used in load Needed to deserialize a derived fact...
Definition: RangeFactor.h:108
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print relies on Testable traits being defined for T
Definition: ExpressionFactor.h:64
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: RangeFactor.h:102
RangeFactor()
default constructor
Definition: RangeFactor.h:42
virtual Expression< T > expression(Key key1, Key key2) const
Recreate expression from given keys_ and measured_, used in load Needed to deserialize a derived fact...
Definition: RangeFactor.h:56
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
Binary specialization of ExpressionFactor meant as a base class for binary factors Enforces expressio...
Definition: ExpressionFactor.h:204
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print contents
Definition: RangeFactor.h:118
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
const T & measured() const
return the measurement
Definition: ExpressionFactor.h:61
Definition: RangeFactor.h:81
void initialize(const Expression< T > &expression)
Initialize with constructor arguments.
Definition: ExpressionFactor.h:151
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: RangeFactor.h:50
void print(const std::string &s="", const KeyFormatter &kf=DefaultKeyFormatter) const
print
Definition: RangeFactor.h:63
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33