farm-ng-core
rotation3.h
Go to the documentation of this file.
1 // Copyright (c) 2011, Hauke Strasdat
2 // Copyright (c) 2012, Steven Lovegrove
3 // Copyright (c) 2021, farm-ng, inc.
4 //
5 // Use of this source code is governed by an MIT-style
6 // license that can be found in the LICENSE file or at
7 // https://opensource.org/licenses/MIT.
8 
9 #pragma once
10 
13 #include "sophus/lie/lie_group.h"
15 
16 namespace sophus {
17 
18 // definition: origin and distance preserving mapping in R^2
19 template <class TScalar>
20 class Rotation3 : public lie::Group<Rotation3, TScalar, lie::Rotation3Impl> {
21  public:
22  using Scalar = TScalar;
24 
25  using Tangent = typename Base::Tangent;
26  using Params = typename Base::Params;
27  using Point = typename Base::Point;
28 
29  Rotation3() = default;
30 
31  explicit Rotation3(UninitTag /*unused*/) {}
32 
33  template <class TOtherScalar>
34  auto cast() const -> Rotation3<TOtherScalar> {
36  this->params_.template cast<TOtherScalar>());
37  }
38 
39  /// Construct x-axis rotation.
40  ///
41  static auto fromRx(TScalar const& x) -> Rotation3 {
42  return Rotation3::exp(Eigen::Vector3<TScalar>(x, TScalar(0), TScalar(0)));
43  }
44 
45  /// Construct y-axis rotation.
46  ///
47  static auto fromRy(TScalar const& y) -> Rotation3 {
48  return Rotation3::exp(Eigen::Vector3<TScalar>(TScalar(0), y, TScalar(0)));
49  }
50 
51  /// Construct z-axis rotation.
52  ///
53  static auto fromRz(TScalar const& z) -> Rotation3 {
54  return Rotation3::exp(Eigen::Vector3<TScalar>(TScalar(0), TScalar(0), z));
55  }
56 
58  return Rotation3::fromParams(q.params());
59  }
60 
61  static auto fitFromQuaternion(Quaternion<TScalar> const& q) -> Rotation3 {
62  return Rotation3::fromParams(q.params().normalized());
63  }
64 
65  static auto fromRotationMatrix(Eigen::Matrix3<TScalar> const& mat_r)
66  -> Rotation3 {
68  isOrthogonal(mat_r),
69  "R is not orthogonal:\n {}",
70  mat_r * mat_r.transpose());
72  mat_r.determinant() > TScalar(0),
73  "det(R) is not positive: {}",
74  mat_r.determinant());
75  return Rotation3::fromParams(Eigen::Quaternion<TScalar>(mat_r).coeffs());
76  }
77 
78  static auto fitFromMatrix(Eigen::Matrix3<TScalar> const& mat_r) -> Rotation3 {
80  }
81 
82  [[nodiscard]] auto rotationMatrix() const -> Eigen::Matrix3<TScalar> {
83  return this->matrix();
84  }
85 
86  [[nodiscard]] auto unitQuaternion() const -> Quaternion<TScalar> {
88  }
89 
90  auto setUnitQuaternion(Quaternion<TScalar> const& z) const -> void {
91  this->setParams(z);
92  }
93 };
94 
95 /// Construct rotation which would take unit direction vector ``from`` into
96 /// ``to`` such that ``to = rotThroughPoints(from,to) * from``. I.e. that the
97 /// rotated point ``from`` is colinear with ``to`` (equal up to scale)
98 ///
99 /// The axis of rotation is perpendicular to both ``from`` and ``to``.
100 ///
101 template <class TScalar>
103  UnitVector3<TScalar> const& from, UnitVector3<TScalar> const& to)
104  -> Rotation3<TScalar> {
105  using std::abs;
106  using std::atan2;
107  Eigen::Vector<TScalar, 3> from_cross_to = from.vector().cross(to.vector());
108  TScalar n = from_cross_to.norm();
109  if (abs(n) < sophus::kEpsilon<TScalar>) {
110  return Rotation3<TScalar>();
111  }
112  // https://stackoverflow.com/a/32724066
113  TScalar angle = atan2(n, from.vector().dot(to.vector()));
114 
115  return Rotation3<TScalar>::exp(angle * from_cross_to / n);
116 }
117 
118 /// Construct rotation which would take direction vector ``from`` into ``to``
119 /// such that ``to \propto rotThroughPoints(from,to) * from``. I.e. that the
120 /// rotated point ``from`` is colinear with ``to`` (equal up to scale)
121 ///
122 /// The axis of rotation is perpendicular to both ``from`` and ``to``.
123 ///
124 /// Precondition: Neither ``from`` nor ``to`` must be zero. This is
125 // unchecked.
126 template <class TScalar>
128  Eigen::Vector<TScalar, 3> const& from, Eigen::Vector<TScalar, 3> const& to)
129  -> Rotation3<TScalar> {
130  using std::abs;
131  using std::atan2;
132  Eigen::Vector<TScalar, 3> from_cross_to = from.cross(to);
133  TScalar n = from_cross_to.norm();
134  if (abs(n) < sophus::kEpsilon<TScalar>) {
135  return Rotation3<TScalar>();
136  }
137  // https://stackoverflow.com/a/32724066
138  TScalar angle = atan2(n, from.dot(to));
139 
140  return Rotation3<TScalar>::exp(angle * from_cross_to / n);
141 }
142 
145 static_assert(concepts::Rotation3<Rotation3F64>);
146 
147 namespace details {
148 template <class TT>
149 class Cast<sophus::Rotation3<TT>> {
150  public:
151  template <class TTo>
152  static auto impl(sophus::Rotation3<TT> const& v) {
153  return v.template cast<typename TTo::Scalar>();
154  }
155  template <class TTo>
156  static auto implScalar(sophus::Rotation3<TT> const& v) {
157  return v.template cast<TTo>();
158  }
159 };
160 } // namespace details
161 
162 template <class TScalar>
163 using SO3 = Rotation3<TScalar>; // NOLINT
164 using SO3f = Rotation3<float>; // NOLINT
165 using SO3d = Rotation3<double>; // NOLINT
166 
167 } // namespace sophus
sophus::rotThroughPoints
auto rotThroughPoints(UnitVector3< TScalar > const &from, UnitVector3< TScalar > const &to) -> Rotation3< TScalar >
Construct rotation which would take unit direction vector from into to such that to = rotThroughPoint...
Definition: rotation3.h:102
rotation3.h
sophus::Rotation3::Params
typename Base::Params Params
Definition: rotation3.h:26
sophus::Rotation3::fitFromQuaternion
static auto fitFromQuaternion(Quaternion< TScalar > const &q) -> Rotation3
Definition: rotation3.h:61
sophus::Rotation3::fitFromMatrix
static auto fitFromMatrix(Eigen::Matrix3< TScalar > const &mat_r) -> Rotation3
Definition: rotation3.h:78
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::Params
Eigen::Vector< Scalar, kNumParams > Params
Definition: lie_group.h:69
sophus::Rotation3::fromUnitQuaternion
static auto fromUnitQuaternion(Quaternion< TScalar > const &q) -> Rotation3
Definition: rotation3.h:57
Eigen
Definition: params.h:72
sophus::Rotation3::Rotation3
Rotation3(UninitTag)
Definition: rotation3.h:31
SOPHUS_ASSERT
#define SOPHUS_ASSERT(...)
Definition: common.h:40
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::Tangent
Eigen::Vector< Scalar, kDof > Tangent
Definition: lie_group.h:68
sophus::lie::Group
Definition: lie_group.h:24
sophus::Rotation3
Definition: rotation3.h:20
sophus::UnitVector
Definition: lie_group.h:14
orthogonal.h
sophus::Rotation3::cast
auto cast() const -> Rotation3< TOtherScalar >
Definition: rotation3.h:34
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::Point
Eigen::Vector< Scalar, kPointDim > Point
Definition: lie_group.h:70
sophus::Quaternion::fromParams
static auto fromParams(Params const &params) -> Quaternion
Definition: quaternion.h:118
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::fromParams
static auto fromParams(Params const &params) -> Derived
Definition: lie_group.h:79
sophus::Rotation3::Tangent
typename Base::Tangent Tangent
Definition: rotation3.h:25
sophus::Rotation3::unitQuaternion
auto unitQuaternion() const -> Quaternion< TScalar >
Definition: rotation3.h:86
sophus::Rotation3::Rotation3
Rotation3()=default
sophus::Rotation3::Scalar
TScalar Scalar
Definition: rotation3.h:22
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::params_
Params params_
Definition: lie_group.h:265
sophus::makeRotationMatrix
auto makeRotationMatrix(Eigen::MatrixBase< TD > const &r) -> std::enable_if_t< std::is_floating_point< typename TD::Scalar >::value, Eigen::Matrix< typename TD::Scalar, TD::RowsAtCompileTime, TD::RowsAtCompileTime >>
Takes in arbitrary square matrix (2x2 or larger) and returns closest orthogonal matrix with positive ...
Definition: orthogonal.h:67
sophus::Rotation3::fromRotationMatrix
static auto fromRotationMatrix(Eigen::Matrix3< TScalar > const &mat_r) -> Rotation3
Definition: rotation3.h:65
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::setParams
void setParams(Params const &params)
Definition: lie_group.h:241
sophus::Rotation3::setUnitQuaternion
auto setUnitQuaternion(Quaternion< TScalar > const &z) const -> void
Definition: rotation3.h:90
sophus::Rotation3::rotationMatrix
auto rotationMatrix() const -> Eigen::Matrix3< TScalar >
Definition: rotation3.h:82
sophus::Rotation3::fromRy
static auto fromRy(TScalar const &y) -> Rotation3
Construct y-axis rotation.
Definition: rotation3.h:47
sophus::Rotation3::fromRx
static auto fromRx(TScalar const &x) -> Rotation3
Construct x-axis rotation.
Definition: rotation3.h:41
sophus::Rotation3::Point
typename Base::Point Point
Definition: rotation3.h:27
lie_group.h
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::matrix
auto matrix() const -> Eigen::Matrix< Scalar, kAmbientDim, kAmbientDim >
Definition: lie_group.h:176
group_accessors.h
sophus::isOrthogonal
auto isOrthogonal(Eigen::MatrixBase< TD > const &r) -> bool
Takes in arbitrary square matrix and returns true if it is orthogonal.
Definition: orthogonal.h:24
sophus::lie::Group< Rotation3, TScalar, lie::Rotation3Impl >::exp
static auto exp(Tangent const &tangent) -> Derived
Definition: lie_group.h:93
sophus::Rotation3::fromRz
static auto fromRz(TScalar const &z) -> Rotation3
Construct z-axis rotation.
Definition: rotation3.h:53
sophus::UninitTag
Definition: common.h:70
sophus::Quaternion
Definition: group_accessors.h:18