gtsam  4.0.0
gtsam
EssentialMatrix.h
1 /*
2  * @file EssentialMatrix.h
3  * @brief EssentialMatrix class
4  * @author Frank Dellaert
5  * @date December 17, 2013
6  */
7 
8 #pragma once
9 
10 #include <gtsam/geometry/Pose3.h>
11 #include <gtsam/geometry/Unit3.h>
12 #include <gtsam/geometry/Point2.h>
13 #include <gtsam/base/Manifold.h>
14 #include <iosfwd>
15 
16 namespace gtsam {
17 
24 class GTSAM_EXPORT EssentialMatrix : private ProductManifold<Rot3, Unit3> {
25 
26 private:
28  Matrix3 E_;
29 
31  EssentialMatrix(const Base& base) :
32  Base(base), E_(direction().skew() * rotation().matrix()) {
33  }
34 
35 public:
36 
38  static Vector3 Homogeneous(const Point2& p) {
39  return Vector3(p.x(), p.y(), 1);
40  }
41 
44 
47  Base(Rot3(), Unit3(1, 0, 0)), E_(direction().skew()) {
48  }
49 
51  EssentialMatrix(const Rot3& aRb, const Unit3& aTb) :
52  Base(aRb, aTb), E_(direction().skew() * rotation().matrix()) {
53  }
54 
56  static EssentialMatrix FromRotationAndDirection(const Rot3& aRb, const Unit3& aTb,
57  OptionalJacobian<5, 3> H1 = boost::none,
58  OptionalJacobian<5, 2> H2 = boost::none);
59 
61  static EssentialMatrix FromPose3(const Pose3& _1P2_,
62  OptionalJacobian<5, 6> H = boost::none);
63 
65  template<typename Engine>
66  static EssentialMatrix Random(Engine & rng) {
67  return EssentialMatrix(Rot3::Random(rng), Unit3::Random(rng));
68  }
69 
70  virtual ~EssentialMatrix() {}
71 
73 
76 
78  void print(const std::string& s = "") const;
79 
81  bool equals(const EssentialMatrix& other, double tol = 1e-8) const {
82  return rotation().equals(other.rotation(), tol)
83  && direction().equals(other.direction(), tol);
84  }
85 
87 
90 
91  using Base::dimension;
92  using Base::dim;
93  using Base::Dim;
94 
96  EssentialMatrix retract(const TangentVector& v) const {
97  return Base::retract(v);
98  }
99 
101  TangentVector localCoordinates(const EssentialMatrix& other) const {
102  return Base::localCoordinates(other);
103  }
105 
108 
110  inline const Rot3& rotation() const {
111  return this->first;
112  }
113 
115  inline const Unit3& direction() const {
116  return this->second;
117  }
118 
120  inline const Matrix3& matrix() const {
121  return E_;
122  }
123 
125  inline const Unit3& epipole_a() const {
126  return direction();
127  }
128 
130  inline Unit3 epipole_b() const {
131  return rotation().unrotate(direction());
132  }
133 
141  Point3 transform_to(const Point3& p,
142  OptionalJacobian<3,5> DE = boost::none,
143  OptionalJacobian<3,3> Dpoint = boost::none) const;
144 
150  EssentialMatrix rotate(const Rot3& cRb, OptionalJacobian<5, 5> HE =
151  boost::none, OptionalJacobian<5, 3> HR = boost::none) const;
152 
158  friend EssentialMatrix operator*(const Rot3& cRb, const EssentialMatrix& E) {
159  return E.rotate(cRb);
160  }
161 
163  double error(const Vector3& vA, const Vector3& vB, //
164  OptionalJacobian<1,5> H = boost::none) const;
165 
167 
170 
172  GTSAM_EXPORT friend std::ostream& operator <<(std::ostream& os, const EssentialMatrix& E);
173 
175  GTSAM_EXPORT friend std::istream& operator >>(std::istream& is, EssentialMatrix& E);
176 
178 
179 private:
180 
183 
185  friend class boost::serialization::access;
186  template<class ARCHIVE>
187  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
188  ar & BOOST_SERIALIZATION_NVP(first);
189  ar & BOOST_SERIALIZATION_NVP(second);
190 
191  ar & boost::serialization::make_nvp("E11", E_(0,0));
192  ar & boost::serialization::make_nvp("E12", E_(0,1));
193  ar & boost::serialization::make_nvp("E13", E_(0,2));
194  ar & boost::serialization::make_nvp("E21", E_(1,0));
195  ar & boost::serialization::make_nvp("E22", E_(1,1));
196  ar & boost::serialization::make_nvp("E23", E_(1,2));
197  ar & boost::serialization::make_nvp("E31", E_(2,0));
198  ar & boost::serialization::make_nvp("E32", E_(2,1));
199  ar & boost::serialization::make_nvp("E33", E_(2,2));
200  }
201 
203 };
204 
205 template<>
206 struct traits<EssentialMatrix> : public internal::Manifold<EssentialMatrix> {};
207 
208 template<>
209 struct traits<const EssentialMatrix> : public internal::Manifold<EssentialMatrix> {};
210 
211 } // gtsam
212 
const Matrix3 & matrix() const
Return 3*3 matrix representation.
Definition: EssentialMatrix.h:120
TangentVector localCoordinates(const EssentialMatrix &other) const
Compute the coordinates in the tangent space.
Definition: EssentialMatrix.h:101
3D Pose
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
static EssentialMatrix Random(Engine &rng)
Random, using Rot3::Random and Unit3::Random.
Definition: EssentialMatrix.h:66
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
static Rot3 Random(boost::mt19937 &rng)
Random, generates a random axis, then random angle [-p,pi].
Definition: Rot3.cpp:37
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition: OptionalJacobian.h:39
EssentialMatrix rotate(const Rot3 &cRb, OptionalJacobian< 5, 5 > HE=boost::none, OptionalJacobian< 5, 3 > HR=boost::none) const
Given essential matrix E in camera frame B, convert to body frame C.
Definition: EssentialMatrix.cpp:73
double y() const
get y
Definition: Point2.h:106
istream & operator>>(istream &inputStream, Matrix &destinationMatrix)
Read a matrix from an input stream, such as a file.
Definition: Matrix.cpp:168
const Unit3 & direction() const
Direction.
Definition: EssentialMatrix.h:115
Definition: Pose3.h:37
const Unit3 & epipole_a() const
Return epipole in image_a , as Unit3 to allow for infinity.
Definition: EssentialMatrix.h:125
Definition: Point3.h:45
static Unit3 Random(boost::mt19937 &rng)
Random direction, using boost::uniform_on_sphere.
Definition: Unit3.cpp:56
bool equals(const EssentialMatrix &other, double tol=1e-8) const
assert equality up to a tolerance
Definition: EssentialMatrix.h:81
2D Point
Base class and basic functions for Manifold types.
EssentialMatrix retract(const TangentVector &v) const
Retract delta to manifold.
Definition: EssentialMatrix.h:96
Both ManifoldTraits and Testable.
Definition: Manifold.h:120
const Rot3 & rotation() const
Rotation.
Definition: EssentialMatrix.h:110
Definition: Point2.h:40
An essential matrix is like a Pose3, except with translation up to scale It is named after the 3*3 ma...
Definition: EssentialMatrix.h:24
friend EssentialMatrix operator*(const Rot3 &cRb, const EssentialMatrix &E)
Given essential matrix E in camera frame B, convert to body frame C.
Definition: EssentialMatrix.h:158
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Definition: Rot3.h:56
static Vector3 Homogeneous(const Point2 &p)
Static function to convert Point2 to homogeneous coordinates.
Definition: EssentialMatrix.h:38
EssentialMatrix()
Default constructor.
Definition: EssentialMatrix.h:46
double x() const
get x
Definition: Point2.h:103
EssentialMatrix(const Rot3 &aRb, const Unit3 &aTb)
Construct from rotation and translation.
Definition: EssentialMatrix.h:51
Helper class to construct the product manifold of two other manifolds, M1 and M2 Assumes nothing exce...
Definition: Manifold.h:175
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Unit3 epipole_b() const
Return epipole in image_b, as Unit3 to allow for infinity.
Definition: EssentialMatrix.h:130