StereoFactor.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 
23 
24 namespace gtsam {
25 
30 template<class POSE, class LANDMARK>
31 class GenericStereoFactor: public NoiseModelFactor2<POSE, LANDMARK> {
32 private:
33 
34  // Keep a copy of measurement and calibration for I/O
37  boost::optional<POSE> body_P_sensor_;
38 
39  // verbosity handling for Cheirality Exceptions
42 
43 public:
44 
45  // shorthand for base class type
48  typedef boost::shared_ptr<GenericStereoFactor> shared_ptr;
49  typedef POSE CamPose;
50 
54  GenericStereoFactor() : K_(new Cal3_S2Stereo(444, 555, 666, 777, 888, 1.0)),
55  throwCheirality_(false), verboseCheirality_(false) {}
56 
67  Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr& K,
68  boost::optional<POSE> body_P_sensor = boost::none) :
69  Base(model, poseKey, landmarkKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
70  throwCheirality_(false), verboseCheirality_(false) {}
71 
84  Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr& K,
86  boost::optional<POSE> body_P_sensor = boost::none) :
87  Base(model, poseKey, landmarkKey), measured_(measured), K_(K), body_P_sensor_(body_P_sensor),
88  throwCheirality_(throwCheirality), verboseCheirality_(verboseCheirality) {}
89 
91  ~GenericStereoFactor() override {}
92 
95  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
97 
103  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
104  Base::print(s, keyFormatter);
105  measured_.print(s + ".z");
106  if(this->body_P_sensor_)
107  this->body_P_sensor_->print(" sensor pose in body frame: ");
108  }
109 
113  bool equals(const NonlinearFactor& f, double tol = 1e-9) const override {
114  const GenericStereoFactor* e = dynamic_cast<const GenericStereoFactor*> (&f);
115  return e
116  && Base::equals(f)
117  && measured_.equals(e->measured_, tol)
118  && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
119  }
120 
123  boost::optional<Matrix&> H1 = boost::none, boost::optional<Matrix&> H2 = boost::none) const override {
124  try {
125  if(body_P_sensor_) {
126  if(H1) {
127  gtsam::Matrix H0;
128  StereoCamera stereoCam(pose.compose(*body_P_sensor_, H0), K_);
129  StereoPoint2 reprojectionError(stereoCam.project(point, H1, H2) - measured_);
130  *H1 = *H1 * H0;
131  return reprojectionError.vector();
132  } else {
133  StereoCamera stereoCam(pose.compose(*body_P_sensor_), K_);
134  return (stereoCam.project(point, H1, H2) - measured_).vector();
135  }
136  } else {
137  StereoCamera stereoCam(pose, K_);
138  return (stereoCam.project(point, H1, H2) - measured_).vector();
139  }
140  } catch(StereoCheiralityException& e) {
141  if (H1) *H1 = Matrix::Zero(3,6);
142  if (H2) *H2 = Z_3x3;
143  if (verboseCheirality_)
144  std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
145  " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
146  if (throwCheirality_)
147  throw StereoCheiralityException(this->key2());
148  }
149  return Vector3::Constant(2.0 * K_->fx());
150  }
151 
153  const StereoPoint2& measured() const {
154  return measured_;
155  }
156 
158  inline const Cal3_S2Stereo::shared_ptr calibration() const {
159  return K_;
160  }
161 
163  inline bool verboseCheirality() const { return verboseCheirality_; }
164 
166  inline bool throwCheirality() const { return throwCheirality_; }
167 
168 private:
171  template<class Archive>
172  void serialize(Archive & ar, const unsigned int /*version*/) {
173  ar & boost::serialization::make_nvp("NoiseModelFactor2",
174  boost::serialization::base_object<Base>(*this));
175  ar & BOOST_SERIALIZATION_NVP(measured_);
176  ar & BOOST_SERIALIZATION_NVP(K_);
177  ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
178  ar & BOOST_SERIALIZATION_NVP(throwCheirality_);
179  ar & BOOST_SERIALIZATION_NVP(verboseCheirality_);
180  }
181 };
182 
184 template<class T1, class T2>
185 struct traits<GenericStereoFactor<T1, T2> > : public Testable<GenericStereoFactor<T1, T2> > {};
186 
187 } // \ namespace gtsam
const gtsam::Key poseKey
NoiseModelFactor2< POSE, LANDMARK > Base
typedef for base class
Definition: StereoFactor.h:46
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
StereoPoint2 measured_
the measurement
Definition: StereoFactor.h:35
noiseModel::Diagonal::shared_ptr model
GenericStereoFactor(const StereoPoint2 &measured, const SharedNoiseModel &model, Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr &K, boost::optional< POSE > body_P_sensor=boost::none)
Definition: StereoFactor.h:66
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Pose2 T2(M_PI/2.0, Point2(0.0, 2.0))
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: StereoFactor.h:103
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
Cal3_S2Stereo::shared_ptr K_
shared pointer to calibration
Definition: StereoFactor.h:36
Point3 point(10, 0,-5)
bool verboseCheirality() const
Definition: StereoFactor.h:163
Class compose(const Class &g) const
Definition: Lie.h:47
boost::shared_ptr< Cal3_S2Stereo > shared_ptr
Definition: Cal3_S2Stereo.h:38
Eigen::VectorXd Vector
Definition: Vector.h:38
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
const StereoPoint2 & measured() const
Definition: StereoFactor.h:153
boost::shared_ptr< This > shared_ptr
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: StereoFactor.h:94
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
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
void print(const std::string &s="") const
boost::optional< POSE > body_P_sensor_
The pose of the sensor in the body frame.
Definition: StereoFactor.h:37
const Cal3_S2Stereo::shared_ptr calibration() const
Definition: StereoFactor.h:158
bool throwCheirality() const
Definition: StereoFactor.h:166
traits
Definition: chartTesting.h:28
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Definition: StereoFactor.h:113
bool throwCheirality_
If true, rethrows Cheirality exceptions (default: false)
Definition: StereoFactor.h:40
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Vector evaluateError(const Pose3 &pose, const Point3 &point, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
Definition: StereoFactor.h:122
A Stereo Camera based on two Simple Cameras.
Vector3 vector() const
Definition: StereoPoint2.h:115
Non-linear factor base classes.
bool equals(const StereoPoint2 &q, double tol=1e-9) const
Definition: StereoPoint2.h:64
const G double tol
Definition: Group.h:83
StereoPoint2 project(const Point3 &point) const
Project 3D point to StereoPoint2 (uL,uR,v)
Vector3 Point3
Definition: Point3.h:35
GenericStereoFactor(const StereoPoint2 &measured, const SharedNoiseModel &model, Key poseKey, Key landmarkKey, const Cal3_S2Stereo::shared_ptr &K, bool throwCheirality, bool verboseCheirality, boost::optional< POSE > body_P_sensor=boost::none)
Definition: StereoFactor.h:83
Pose2 T1(M_PI/4.0, Point2(sqrt(0.5), sqrt(0.5)))
GenericStereoFactor< POSE, LANDMARK > This
typedef for this class (with templates)
Definition: StereoFactor.h:47
bool verboseCheirality_
If true, prints text for Cheirality exceptions (default: false)
Definition: StereoFactor.h:41
POSE CamPose
typedef for Pose Lie Value type
Definition: StereoFactor.h:49
friend class boost::serialization::access
Definition: StereoFactor.h:170
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
boost::shared_ptr< GenericStereoFactor > shared_ptr
typedef for shared pointer to this object
Definition: StereoFactor.h:48
static StereoCamera stereoCam(camPose, K)
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:734
void serialize(Archive &ar, const unsigned int)
Definition: StereoFactor.h:172


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:44:50