AliceVision
Photogrammetric Computer Vision Framework
Pose3.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // This Source Code Form is subject to the terms of the Mozilla Public License,
5 // v. 2.0. If a copy of the MPL was not distributed with this file,
6 // You can obtain one at https://mozilla.org/MPL/2.0/.
7 
8 #pragma once
9 
10 #include <aliceVision/numeric/numeric.hpp>
11 #include <boost/math/constants/constants.hpp>
12 #include <aliceVision/geometry/lie.hpp>
13 
14 namespace aliceVision {
15 namespace geometry {
16 
17 // Define a 3D Pose as a SE3 matrix
18 class Pose3
19 {
20  protected:
21  SE3::Matrix _homogeneous{SE3::Matrix::Identity()};
22 
23  public:
24  Pose3() = default;
25 
26  Pose3(const Mat3& c_R_w, const Vec3& w_c)
27  {
28  _homogeneous.setIdentity();
29  _homogeneous.block<3, 3>(0, 0) = c_R_w;
30  _homogeneous.block<3, 1>(0, 3) = -c_R_w * w_c;
31  }
32 
33  Pose3(const Mat4& T)
34  : _homogeneous(T)
35  {}
36 
37  // Accessors
38  Mat3 rotation() const { return _homogeneous.block<3, 3>(0, 0); }
39 
40  void setRotation(const Mat3& rotation) { _homogeneous.block<3, 3>(0, 0) = rotation; }
41 
42  Vec3 translation() const { return _homogeneous.block<3, 1>(0, 3); }
43 
44  void setTranslation(const Vec3& translation) { _homogeneous.block<3, 1>(0, 3) = translation; }
45 
46  Vec3 center() const { return -rotation().transpose() * translation(); }
47 
48  void setCenter(const Vec3& center) { _homogeneous.block<3, 1>(0, 3) = -rotation() * center; }
49 
50  // Apply pose
51  inline Mat3X operator()(const Mat3X& p) const { return rotation() * (p.colwise() - center()); }
52 
53  // Composition
54  Pose3 operator*(const Pose3& P) const { return Pose3(_homogeneous * P._homogeneous); }
55 
56  // Operator ==
57  bool operator==(const Pose3& other) const
58  {
59  return AreMatNearEqual(rotation(), other.rotation(), 1e-6) && AreVecNearEqual(center(), other.center(), 1e-6);
60  }
61 
62  // Inverse
63  Pose3 inverse() const
64  {
65  // parameter is center ... which will inverse
66  return Pose3(rotation().transpose(), translation());
67  }
68 
70  double depth(const Vec3& X) const { return (rotation() * (X - center()))[2]; }
71 
73  Pose3 transformSRt(const double S, const Mat3& R, const Vec3& t) const
74  {
75  Pose3 pose;
76  pose.setRotation(rotation() * R.transpose());
77  pose.setCenter(S * R * center() + t);
78 
79  return pose;
80  }
81 
82  const SE3::Matrix& getHomogeneous() const { return _homogeneous; }
83 };
84 
91 inline Pose3 poseFromRT(const Mat3& R, const Vec3& t) { return Pose3(R, -R.transpose() * t); }
92 
93 inline Pose3 randomPose()
94 {
95  Vec3 vecR = Vec3::Random().normalized() * boost::math::constants::pi<double>();
96 
97  return geometry::Pose3(SO3::expm(vecR), Vec3::Random());
98 }
99 
100 } // namespace geometry
101 } // namespace aliceVision
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::geometry::Pose3::transformSRt
Pose3 transformSRt(const double S, const Mat3 &R, const Vec3 &t) const
Return the pose with the Scale, Rotation and translation applied.
Definition: Pose3.hpp:73
aliceVision::geometry::Pose3::depth
double depth(const Vec3 &X) const
Return the depth (distance) of a point respect to the camera center.
Definition: Pose3.hpp:70
aliceVision::geometry::Pose3
Definition: Pose3.hpp:18