Eigen  5.0.1
 
Loading...
Searching...
No Matches
Rotation2D.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_ROTATION2D_H
12#define EIGEN_ROTATION2D_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
35
36namespace internal {
37
38template <typename Scalar_>
39struct traits<Rotation2D<Scalar_> > {
40 using Scalar = Scalar_;
41};
42} // end namespace internal
43
44template <typename Scalar_>
45class Rotation2D : public RotationBase<Rotation2D<Scalar_>, 2> {
46 using Base = RotationBase<Rotation2D<Scalar_>, 2>;
47
48 public:
49 using Base::operator*;
50
51 enum { Dim = 2 };
53 using Scalar = Scalar_;
54 using Vector2 = Matrix<Scalar, 2, 1>;
55 using Matrix2 = Matrix<Scalar, 2, 2>;
56
57 protected:
58 Scalar m_angle;
59
60 public:
62 EIGEN_DEVICE_FUNC explicit inline Rotation2D(const Scalar& a) : m_angle(a) {}
63
65 EIGEN_DEVICE_FUNC Rotation2D() {}
66
71 template <typename Derived>
72 EIGEN_DEVICE_FUNC explicit Rotation2D(const MatrixBase<Derived>& m) {
73 fromRotationMatrix(m.derived());
74 }
75
77 EIGEN_DEVICE_FUNC inline Scalar angle() const { return m_angle; }
78
80 EIGEN_DEVICE_FUNC inline Scalar& angle() { return m_angle; }
81
83 EIGEN_DEVICE_FUNC inline Scalar smallestPositiveAngle() const {
84 Scalar tmp = numext::fmod(m_angle, Scalar(2 * EIGEN_PI));
85 return tmp < Scalar(0) ? tmp + Scalar(2 * EIGEN_PI) : tmp;
86 }
87
89 EIGEN_DEVICE_FUNC inline Scalar smallestAngle() const {
90 Scalar tmp = numext::fmod(m_angle, Scalar(2 * EIGEN_PI));
91 if (tmp > Scalar(EIGEN_PI))
92 tmp -= Scalar(2 * EIGEN_PI);
93 else if (tmp < -Scalar(EIGEN_PI))
94 tmp += Scalar(2 * EIGEN_PI);
95 return tmp;
96 }
97
99 EIGEN_DEVICE_FUNC inline Rotation2D inverse() const { return Rotation2D(-m_angle); }
100
102 EIGEN_DEVICE_FUNC inline Rotation2D operator*(const Rotation2D& other) const {
103 return Rotation2D(m_angle + other.m_angle);
104 }
105
107 EIGEN_DEVICE_FUNC inline Rotation2D& operator*=(const Rotation2D& other) {
108 m_angle += other.m_angle;
109 return *this;
110 }
111
113 EIGEN_DEVICE_FUNC Vector2 operator*(const Vector2& vec) const { return toRotationMatrix() * vec; }
114
115 template <typename Derived>
116 EIGEN_DEVICE_FUNC Rotation2D& fromRotationMatrix(const MatrixBase<Derived>& m);
117 EIGEN_DEVICE_FUNC Matrix2 toRotationMatrix() const;
118
126 template <typename Derived>
127 EIGEN_DEVICE_FUNC Rotation2D& operator=(const MatrixBase<Derived>& m) {
128 return fromRotationMatrix(m.derived());
129 }
130
134 EIGEN_DEVICE_FUNC inline Rotation2D slerp(const Scalar& t, const Rotation2D& other) const {
135 Scalar dist = Rotation2D(other.m_angle - m_angle).smallestAngle();
136 return Rotation2D(m_angle + dist * t);
137 }
138
144 template <typename NewScalarType>
145 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Rotation2D, Rotation2D<NewScalarType> >::type cast()
146 const {
147 return typename internal::cast_return_type<Rotation2D, Rotation2D<NewScalarType> >::type(*this);
148 }
149
151 template <typename OtherScalarType>
152 EIGEN_DEVICE_FUNC inline explicit Rotation2D(const Rotation2D<OtherScalarType>& other) {
153 m_angle = Scalar(other.angle());
154 }
155
156 EIGEN_DEVICE_FUNC static inline Rotation2D Identity() { return Rotation2D(0); }
157
162 EIGEN_DEVICE_FUNC bool isApprox(const Rotation2D& other, const typename NumTraits<Scalar>::Real& prec =
163 NumTraits<Scalar>::dummy_precision()) const {
164 return internal::isApprox(m_angle, other.m_angle, prec);
165 }
166};
167
174
179template <typename Scalar>
180template <typename Derived>
181EIGEN_DEVICE_FUNC Rotation2D<Scalar>& Rotation2D<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat) {
182 EIGEN_USING_STD(atan2)
183 EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime == 2 && Derived::ColsAtCompileTime == 2,
184 YOU_MADE_A_PROGRAMMING_MISTAKE)
185 m_angle = atan2(mat.coeff(1, 0), mat.coeff(0, 0));
186 return *this;
187}
188
191template <typename Scalar>
192typename Rotation2D<Scalar>::Matrix2 EIGEN_DEVICE_FUNC Rotation2D<Scalar>::toRotationMatrix(void) const {
193 EIGEN_USING_STD(sin)
194 EIGEN_USING_STD(cos)
195 Scalar sinA = sin(m_angle);
196 Scalar cosA = cos(m_angle);
197 return (Matrix2() << cosA, -sinA, sinA, cosA).finished();
198}
199
200} // end namespace Eigen
201
202#endif // EIGEN_ROTATION2D_H
const std::enable_if_t< std::is_same< typename LhsDerived::Scalar, typename RhsDerived::Scalar >::value, Eigen::CwiseBinaryOp< Eigen::internal::scalar_atan2_op< typename LhsDerived::Scalar, typename RhsDerived::Scalar >, const LhsDerived, const RhsDerived > > atan2(const Eigen::ArrayBase< LhsDerived > &x, const Eigen::ArrayBase< RhsDerived > &exponents)
Definition GlobalFunctions.h:222
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Represents a rotation/orientation in a 2 dimensional space.
Definition Rotation2D.h:45
Rotation2D inverse() const
Definition Rotation2D.h:99
Scalar smallestPositiveAngle() const
Definition Rotation2D.h:83
Scalar smallestAngle() const
Definition Rotation2D.h:89
Rotation2D(const Scalar &a)
Definition Rotation2D.h:62
Rotation2D & operator=(const MatrixBase< Derived > &m)
Definition Rotation2D.h:127
Vector2 operator*(const Vector2 &vec) const
Definition Rotation2D.h:113
Scalar angle() const
Definition Rotation2D.h:77
Scalar_ Scalar
Definition Rotation2D.h:53
Matrix2 toRotationMatrix() const
Definition Rotation2D.h:192
Rotation2D operator*(const Rotation2D &other) const
Definition Rotation2D.h:102
internal::cast_return_type< Rotation2D, Rotation2D< NewScalarType > >::type cast() const
Definition Rotation2D.h:145
Rotation2D(const Rotation2D< OtherScalarType > &other)
Definition Rotation2D.h:152
Rotation2D()
Definition Rotation2D.h:65
Rotation2D & operator*=(const Rotation2D &other)
Definition Rotation2D.h:107
Scalar & angle()
Definition Rotation2D.h:80
Rotation2D slerp(const Scalar &t, const Rotation2D &other) const
Definition Rotation2D.h:134
bool isApprox(const Rotation2D &other, const typename NumTraits< Scalar >::Real &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Rotation2D.h:162
Rotation2D(const MatrixBase< Derived > &m)
Definition Rotation2D.h:72
Common base class for compact rotation representations.
Definition RotationBase.h:33
Rotation2D< double > Rotation2Dd
Definition Rotation2D.h:173
Rotation2D< float > Rotation2Df
Definition Rotation2D.h:170
Matrix< Type, 2, 2 > Matrix2
2×2 matrix of type Type.
Definition Matrix.h:521