InvDepthFactorVariant2.h
Go to the documentation of this file.
1 
12 #pragma once
13 
16 #include <gtsam/geometry/Cal3_S2.h>
17 #include <gtsam/geometry/Pose3.h>
18 #include <gtsam/geometry/Point2.h>
20 
21 namespace gtsam {
22 
26 class InvDepthFactorVariant2: public NoiseModelFactor2<Pose3, Vector3> {
27 protected:
28 
29  // Keep a copy of measurement and calibration for I/O
33 
34 public:
35 
38 
41 
43  typedef boost::shared_ptr<This> shared_ptr;
44 
47  measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
48  }
49 
58  InvDepthFactorVariant2(const Key poseKey, const Key landmarkKey,
60  const SharedNoiseModel& model) :
61  Base(model, poseKey, landmarkKey), measured_(measured), K_(K), referencePoint_(referencePoint) {}
62 
65 
71  void print(const std::string& s = "InvDepthFactorVariant2",
72  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
73  Base::print(s, keyFormatter);
74  traits<Point2>::Print(measured_, s + ".z");
75  }
76 
78  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
79  const This *e = dynamic_cast<const This*>(&p);
80  return e
81  && Base::equals(p, tol)
82  && traits<Point2>::Equals(this->measured_, e->measured_, tol)
83  && this->K_->equals(*e->K_, tol)
84  && traits<Point3>::Equals(this->referencePoint_, e->referencePoint_, tol);
85  }
86 
88  try {
89  // Calculate the 3D coordinates of the landmark in the world frame
90  double theta = landmark(0), phi = landmark(1), rho = landmark(2);
91  Point3 world_P_landmark = referencePoint_ + Point3(cos(theta)*cos(phi)/rho, sin(theta)*cos(phi)/rho, sin(phi)/rho);
92  // Project landmark into Pose2
93  PinholeCamera<Cal3_S2> camera(pose, *K_);
94  return camera.project(world_P_landmark) - measured_;
95  } catch( CheiralityException& e) {
96  std::cout << e.what()
97  << ": Inverse Depth Landmark [" << DefaultKeyFormatter(this->key2()) << "]"
98  << " moved behind camera [" << DefaultKeyFormatter(this->key1()) <<"]"
99  << std::endl;
100  return Vector::Ones(2) * 2.0 * K_->fx();
101  }
102  return (Vector(1) << 0.0).finished();
103  }
104 
107  boost::optional<Matrix&> H1=boost::none,
108  boost::optional<Matrix&> H2=boost::none) const override {
109 
110  if (H1) {
111  (*H1) = numericalDerivative11<Vector, Pose3>(
112  boost::bind(&InvDepthFactorVariant2::inverseDepthError, this, _1,
113  landmark), pose);
114  }
115  if (H2) {
116  (*H2) = numericalDerivative11<Vector, Vector3>(
117  boost::bind(&InvDepthFactorVariant2::inverseDepthError, this, pose,
118  _1), landmark);
119  }
120 
121  return inverseDepthError(pose, landmark);
122  }
123 
125  const Point2& imagePoint() const {
126  return measured_;
127  }
128 
131  return K_;
132  }
133 
135  const Point3& referencePoint() const {
136  return referencePoint_;
137  }
138 
139 private:
140 
143  template<class ARCHIVE>
144  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
145  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
146  ar & BOOST_SERIALIZATION_NVP(measured_);
147  ar & BOOST_SERIALIZATION_NVP(K_);
148  ar & BOOST_SERIALIZATION_NVP(referencePoint_);
149  }
150 };
151 
152 } // \ namespace gtsam
const gtsam::Key poseKey
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
equals
Eigen::Vector3d Vector3
Definition: Vector.h:43
noiseModel::Diagonal::shared_ptr model
Vector inverseDepthError(const Pose3 &pose, const Vector3 &landmark) const
Vector2 Point2
Definition: Point2.h:27
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Some functions to compute numerical derivatives.
const Cal3_S2::shared_ptr calibration() const
Cal3_S2::shared_ptr K_
shared pointer to calibration object
Rot2 theta
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
static Point3 landmark(0, 0, 5)
const Point3 & referencePoint() const
Base class for all pinhole cameras.
EIGEN_DEVICE_FUNC const CosReturnType cos() const
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Eigen::VectorXd Vector
Definition: Vector.h:38
Point3 referencePoint_
the reference point/origin for this landmark
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
Vector evaluateError(const Pose3 &pose, const Vector3 &landmark, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
Evaluate error h(x)-z and optionally derivatives.
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
const Point2 & imagePoint() const
traits
Definition: chartTesting.h:28
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Non-linear factor base classes.
friend class boost::serialization::access
Serialization function.
InvDepthFactorVariant2()
Default constructor.
NoiseModelFactor2< Pose3, Vector3 > Base
shorthand for base class type
float * p
InvDepthFactorVariant2(const Key poseKey, const Key landmarkKey, const Point2 &measured, const Cal3_S2::shared_ptr &K, const Point3 referencePoint, const SharedNoiseModel &model)
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
InvDepthFactorVariant2 This
shorthand for this class
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)
2D Point
3D Pose
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
void print(const std::string &s="InvDepthFactorVariant2", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
void serialize(ARCHIVE &ar, const unsigned int)
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:734
The most common 5DOF 3D->2D calibration.
const char * what() const noexceptoverride


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