gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BearingRangeFactor.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 
19 #pragma once
20 
22 #include <gtsam/base/Testable.h>
24 
25 namespace gtsam {
26 
31  template<class POSE, class POINT, class ROTATION = typename POSE::Rotation>
32  class BearingRangeFactor: public NoiseModelFactor2<POSE, POINT>
33  {
34  public:
37  typedef boost::shared_ptr<This> shared_ptr;
38 
39  private:
40  typedef POSE Pose;
41  typedef ROTATION Rot;
42  typedef POINT Point;
43 
44  // the measurement
45  Rot measuredBearing_;
46  double measuredRange_;
47 
49  GTSAM_CONCEPT_TESTABLE_TYPE(Rot)
50  GTSAM_CONCEPT_RANGE_MEASUREMENT_TYPE(Pose, Point)
51  GTSAM_CONCEPT_POSE_TYPE(Pose)
52 
53  public:
54 
55  BearingRangeFactor() {} /* Default constructor */
56  BearingRangeFactor(Key poseKey, Key pointKey, const Rot& measuredBearing, const double measuredRange,
57  const SharedNoiseModel& model) :
58  Base(model, poseKey, pointKey), measuredBearing_(measuredBearing), measuredRange_(measuredRange) {
59  }
60 
61  virtual ~BearingRangeFactor() {}
62 
64  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
65  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
66  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
67 
69  virtual void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
70  std::cout << s << "BearingRangeFactor("
71  << keyFormatter(this->key1()) << ","
72  << keyFormatter(this->key2()) << ")\n";
73  measuredBearing_.print("measured bearing: ");
74  std::cout << "measured range: " << measuredRange_ << std::endl;
75  this->noiseModel_->print("noise model:\n");
76  }
77 
79  virtual bool equals(const NonlinearFactor& expected, double tol=1e-9) const {
80  const This *e = dynamic_cast<const This*> (&expected);
81  return e != NULL && Base::equals(*e, tol) &&
82  fabs(this->measuredRange_ - e->measuredRange_) < tol &&
83  this->measuredBearing_.equals(e->measuredBearing_, tol);
84  }
85 
87  Vector evaluateError(const Pose& pose, const Point& point,
88  boost::optional<Matrix&> H1, boost::optional<Matrix&> H2) const {
89  Matrix H11, H21, H12, H22;
90  boost::optional<Matrix&> H11_ = H1 ? boost::optional<Matrix&>(H11) : boost::optional<Matrix&>();
91  boost::optional<Matrix&> H21_ = H1 ? boost::optional<Matrix&>(H21) : boost::optional<Matrix&>();
92  boost::optional<Matrix&> H12_ = H2 ? boost::optional<Matrix&>(H12) : boost::optional<Matrix&>();
93  boost::optional<Matrix&> H22_ = H2 ? boost::optional<Matrix&>(H22) : boost::optional<Matrix&>();
94 
95  Rot y1 = pose.bearing(point, H11_, H12_);
96  Vector e1 = Rot::Logmap(measuredBearing_.between(y1));
97 
98  double y2 = pose.range(point, H21_, H22_);
99  Vector e2 = (Vector(1) << y2 - measuredRange_);
100 
101  if (H1) *H1 = gtsam::stack(2, &H11, &H21);
102  if (H2) *H2 = gtsam::stack(2, &H12, &H22);
103  return concatVectors(2, &e1, &e2);
104  }
105 
107  const std::pair<Rot, double> measured() const {
108  return std::make_pair(measuredBearing_, measuredRange_);
109  }
110 
111  private:
112 
115  template<class ARCHIVE>
116  void serialize(ARCHIVE & ar, const unsigned int version) {
117  ar & boost::serialization::make_nvp("NoiseModelFactor2",
118  boost::serialization::base_object<Base>(*this));
119  ar & BOOST_SERIALIZATION_NVP(measuredBearing_);
120  ar & BOOST_SERIALIZATION_NVP(measuredRange_);
121  }
122  }; // BearingRangeFactor
123 
124 } // namespace gtsam
Non-linear factor base classes.
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.h:239
Matrix stack(size_t nrMatrices,...)
create a matrix by stacking other matrices Given a set of matrices: A1, A2, A3... ...
Definition: Matrix.cpp:458
const std::pair< Rot, double > measured() const
return the measured
Definition: BearingRangeFactor.h:107
Vector evaluateError(const Pose &pose, const Point &point, boost::optional< Matrix & > H1, boost::optional< Matrix & > H2) const
h(x)-z -> between(z,h(x)) for Rot manifold
Definition: BearingRangeFactor.h:87
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: BearingRangeFactor.h:69
Vector concatVectors(const std::list< Vector > &vs)
concatenate Vectors
Definition: Vector.cpp:330
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:423
BearingRangeFactor()
concept check by type
Definition: BearingRangeFactor.h:55
This is the base class for all factor types.
Definition: Factor.h:51
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: BearingRangeFactor.h:64
Concept-checking macros for geometric objects Each macro instantiates a concept check structure...
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:455
Concept check for values that can be used in unit tests.
size_t Key
Integer nonlinear key type.
Definition: types.h:59
virtual bool equals(const NonlinearFactor &expected, double tol=1e-9) const
equals
Definition: BearingRangeFactor.h:79
friend class boost::serialization::access
Serialization function.
Definition: BearingRangeFactor.h:114
Definition: BearingRangeFactor.h:32
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