gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
17 #pragma once
18 
19 #include <boost/lexical_cast.hpp>
22 
23 namespace gtsam {
24 
29  template<class POSE, class POINT>
30  class RangeFactor: public NoiseModelFactor2<POSE, POINT> {
31  private:
32 
33  double measured_;
34  boost::optional<POSE> body_P_sensor_;
35 
38 
39  typedef POSE Pose;
40  typedef POINT Point;
41 
42  // Concept requirements for this factor
43  GTSAM_CONCEPT_RANGE_MEASUREMENT_TYPE(POSE, POINT)
44 
45  public:
46 
47  RangeFactor() {} /* Default constructor */
48 
49  RangeFactor(Key poseKey, Key pointKey, double measured,
50  const SharedNoiseModel& model, boost::optional<POSE> body_P_sensor = boost::none) :
51  Base(model, poseKey, pointKey), measured_(measured), body_P_sensor_(body_P_sensor) {
52  }
53 
54  virtual ~RangeFactor() {}
55 
57  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
58  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
59  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
60 
62  Vector evaluateError(const POSE& pose, const POINT& point,
63  boost::optional<Matrix&> H1 = boost::none, boost::optional<Matrix&> H2 = boost::none) const {
64  double hx;
65  if(body_P_sensor_) {
66  if(H1) {
67  Matrix H0;
68  hx = pose.compose(*body_P_sensor_, H0).range(point, H1, H2);
69  *H1 = *H1 * H0;
70  } else {
71  hx = pose.compose(*body_P_sensor_).range(point, H1, H2);
72  }
73  } else {
74  hx = pose.range(point, H1, H2);
75  }
76  return (Vector(1) << hx - measured_);
77  }
78 
80  double measured() const {
81  return measured_;
82  }
83 
85  virtual bool equals(const NonlinearFactor& expected, double tol=1e-9) const {
86  const This *e = dynamic_cast<const This*> (&expected);
87  return e != NULL
88  && Base::equals(*e, tol)
89  && fabs(this->measured_ - e->measured_) < tol
90  && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
91  }
92 
94  void print(const std::string& s="", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
95  std::cout << s << "RangeFactor, range = " << measured_ << std::endl;
96  if(this->body_P_sensor_)
97  this->body_P_sensor_->print(" sensor pose in body frame: ");
98  Base::print("", keyFormatter);
99  }
100 
101  private:
102 
105  template<class ARCHIVE>
106  void serialize(ARCHIVE & ar, const unsigned int version) {
107  ar & boost::serialization::make_nvp("NoiseModelFactor2",
108  boost::serialization::base_object<Base>(*this));
109  ar & BOOST_SERIALIZATION_NVP(measured_);
110  ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
111  }
112  }; // RangeFactor
113 
114 } // 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
double measured() const
return the measured
Definition: RangeFactor.h:80
friend class boost::serialization::access
Serialization function.
Definition: RangeFactor.h:104
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:423
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print contents
Definition: RangeFactor.h:94
This is the base class for all factor types.
Definition: Factor.h:51
Vector evaluateError(const POSE &pose, const POINT &point, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
h(x)-z
Definition: RangeFactor.h:62
Concept-checking macros for geometric objects Each macro instantiates a concept check structure...
size_t Key
Integer nonlinear key type.
Definition: types.h:59
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: RangeFactor.h:57
Definition: RangeFactor.h:30
virtual bool equals(const NonlinearFactor &expected, double tol=1e-9) const
equals specialized to this factor
Definition: RangeFactor.h:85
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: NonlinearFactor.h:231
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