InvDepthFactorVariant1.h
Go to the documentation of this file.
1 
11 #pragma once
12 
15 #include <gtsam/geometry/Cal3_S2.h>
16 #include <gtsam/geometry/Pose3.h>
17 #include <gtsam/geometry/Point2.h>
19 
20 namespace gtsam {
21 
25 class InvDepthFactorVariant1: public NoiseModelFactor2<Pose3, Vector6> {
26 protected:
27 
28  // Keep a copy of measurement and calibration for I/O
31 
32 public:
33 
36 
39 
41  typedef boost::shared_ptr<This> shared_ptr;
42 
45  measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
46  }
47 
56  InvDepthFactorVariant1(const Key poseKey, const Key landmarkKey,
57  const Point2& measured, const Cal3_S2::shared_ptr& K, const SharedNoiseModel& model) :
58  Base(model, poseKey, landmarkKey), measured_(measured), K_(K) {}
59 
62 
68  void print(const std::string& s = "InvDepthFactorVariant1",
69  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
70  Base::print(s, keyFormatter);
71  traits<Point2>::Print(measured_, s + ".z");
72  }
73 
75  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
76  const This *e = dynamic_cast<const This*>(&p);
77  return e
78  && Base::equals(p, tol)
79  && traits<Point2>::Equals(this->measured_, e->measured_, tol)
80  && this->K_->equals(*e->K_, tol);
81  }
82 
83  Vector inverseDepthError(const Pose3& pose, const Vector6& landmark) const {
84  try {
85  // Calculate the 3D coordinates of the landmark in the world frame
86  double x = landmark(0), y = landmark(1), z = landmark(2);
87  double theta = landmark(3), phi = landmark(4), rho = landmark(5);
88  Point3 world_P_landmark = Point3(x, y, z) + Point3(cos(theta)*cos(phi)/rho, sin(theta)*cos(phi)/rho, sin(phi)/rho);
89  // Project landmark into Pose2
90  PinholeCamera<Cal3_S2> camera(pose, *K_);
91  return camera.project(world_P_landmark) - measured_;
92  } catch( CheiralityException& e) {
93  std::cout << e.what()
94  << ": Inverse Depth Landmark [" << DefaultKeyFormatter(this->key2()) << "]"
95  << " moved behind camera [" << DefaultKeyFormatter(this->key1()) <<"]"
96  << std::endl;
97  return Vector::Ones(2) * 2.0 * K_->fx();
98  }
99  return (Vector(1) << 0.0).finished();
100  }
101 
103  Vector evaluateError(const Pose3& pose, const Vector6& landmark,
104  boost::optional<Matrix&> H1=boost::none,
105  boost::optional<Matrix&> H2=boost::none) const override {
106 
107  if (H1) {
108  (*H1) = numericalDerivative11<Vector, Pose3>(
109  boost::bind(&InvDepthFactorVariant1::inverseDepthError, this, _1,
110  landmark), pose);
111  }
112  if (H2) {
113  (*H2) = numericalDerivative11<Vector, Vector6>(
114  boost::bind(&InvDepthFactorVariant1::inverseDepthError, this, pose,
115  _1), landmark);
116  }
117 
118  return inverseDepthError(pose, landmark);
119  }
120 
122  const Point2& imagePoint() const {
123  return measured_;
124  }
125 
128  return K_;
129  }
130 
131 private:
132 
135  template<class ARCHIVE>
136  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
137  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
138  ar & BOOST_SERIALIZATION_NVP(measured_);
139  ar & BOOST_SERIALIZATION_NVP(K_);
140  }
141 };
142 
143 } // \ namespace gtsam
const gtsam::Key poseKey
Vector evaluateError(const Pose3 &pose, const Vector6 &landmark, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
Evaluate error h(x)-z and optionally derivatives.
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Scalar * y
InvDepthFactorVariant1()
Default constructor.
noiseModel::Diagonal::shared_ptr model
Vector2 Point2
Definition: Point2.h:27
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
void print(const std::string &s="InvDepthFactorVariant1", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
void serialize(ARCHIVE &ar, const unsigned int)
Some functions to compute numerical derivatives.
Rot2 theta
friend class boost::serialization::access
Serialization function.
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const Point2 & imagePoint() const
static Point3 landmark(0, 0, 5)
Base class for all pinhole cameras.
EIGEN_DEVICE_FUNC const CosReturnType cos() const
Eigen::VectorXd Vector
Definition: Vector.h:38
InvDepthFactorVariant1 This
shorthand for this class
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
NoiseModelFactor2< Pose3, Vector6 > Base
shorthand for base class type
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
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
traits
Definition: chartTesting.h:28
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Non-linear factor base classes.
Cal3_S2::shared_ptr K_
shared pointer to calibration object
float * p
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none, OptionalJacobian< 2, DimK > Dcal=boost::none) const
project a 3D point from world coordinates into the image
Definition: PinholePose.h:118
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
const Cal3_S2::shared_ptr calibration() const
EIGEN_DEVICE_FUNC const SinReturnType sin() const
const G double tol
Definition: Group.h:83
Vector3 Point3
Definition: Point3.h:35
Point3 measured
static const CalibratedCamera camera(kDefaultPose)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
2D Point
3D Pose
InvDepthFactorVariant1(const Key poseKey, const Key landmarkKey, const Point2 &measured, const Cal3_S2::shared_ptr &K, const SharedNoiseModel &model)
boost::shared_ptr< Cal3_S2 > shared_ptr
Definition: Cal3_S2.h:39
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:59
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:734
The most common 5DOF 3D->2D calibration.
const char * what() const noexceptoverride
Vector inverseDepthError(const Pose3 &pose, const Vector6 &landmark) const


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:14