File indexing completed on 2025-04-19 09:06:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_ROTATION2D_H
0011 #define EIGEN_ROTATION2D_H
0012
0013 namespace RivetEigen {
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 namespace internal {
0033
0034 template<typename _Scalar> struct traits<Rotation2D<_Scalar> >
0035 {
0036 typedef _Scalar Scalar;
0037 };
0038 }
0039
0040 template<typename _Scalar>
0041 class Rotation2D : public RotationBase<Rotation2D<_Scalar>,2>
0042 {
0043 typedef RotationBase<Rotation2D<_Scalar>,2> Base;
0044
0045 public:
0046
0047 using Base::operator*;
0048
0049 enum { Dim = 2 };
0050
0051 typedef _Scalar Scalar;
0052 typedef Matrix<Scalar,2,1> Vector2;
0053 typedef Matrix<Scalar,2,2> Matrix2;
0054
0055 protected:
0056
0057 Scalar m_angle;
0058
0059 public:
0060
0061
0062 EIGEN_DEVICE_FUNC explicit inline Rotation2D(const Scalar& a) : m_angle(a) {}
0063
0064
0065 EIGEN_DEVICE_FUNC Rotation2D() {}
0066
0067
0068
0069
0070
0071 template<typename Derived>
0072 EIGEN_DEVICE_FUNC explicit Rotation2D(const MatrixBase<Derived>& m)
0073 {
0074 fromRotationMatrix(m.derived());
0075 }
0076
0077
0078 EIGEN_DEVICE_FUNC inline Scalar angle() const { return m_angle; }
0079
0080
0081 EIGEN_DEVICE_FUNC inline Scalar& angle() { return m_angle; }
0082
0083
0084 EIGEN_DEVICE_FUNC inline Scalar smallestPositiveAngle() const {
0085 Scalar tmp = numext::fmod(m_angle,Scalar(2*EIGEN_PI));
0086 return tmp<Scalar(0) ? tmp + Scalar(2*EIGEN_PI) : tmp;
0087 }
0088
0089
0090 EIGEN_DEVICE_FUNC inline Scalar smallestAngle() const {
0091 Scalar tmp = numext::fmod(m_angle,Scalar(2*EIGEN_PI));
0092 if(tmp>Scalar(EIGEN_PI)) tmp -= Scalar(2*EIGEN_PI);
0093 else if(tmp<-Scalar(EIGEN_PI)) tmp += Scalar(2*EIGEN_PI);
0094 return tmp;
0095 }
0096
0097
0098 EIGEN_DEVICE_FUNC inline Rotation2D inverse() const { return Rotation2D(-m_angle); }
0099
0100
0101 EIGEN_DEVICE_FUNC inline Rotation2D operator*(const Rotation2D& other) const
0102 { return Rotation2D(m_angle + other.m_angle); }
0103
0104
0105 EIGEN_DEVICE_FUNC inline Rotation2D& operator*=(const Rotation2D& other)
0106 { m_angle += other.m_angle; return *this; }
0107
0108
0109 EIGEN_DEVICE_FUNC Vector2 operator* (const Vector2& vec) const
0110 { return toRotationMatrix() * vec; }
0111
0112 template<typename Derived>
0113 EIGEN_DEVICE_FUNC Rotation2D& fromRotationMatrix(const MatrixBase<Derived>& m);
0114 EIGEN_DEVICE_FUNC Matrix2 toRotationMatrix() const;
0115
0116
0117
0118
0119
0120
0121
0122
0123 template<typename Derived>
0124 EIGEN_DEVICE_FUNC Rotation2D& operator=(const MatrixBase<Derived>& m)
0125 { return fromRotationMatrix(m.derived()); }
0126
0127
0128
0129
0130 EIGEN_DEVICE_FUNC inline Rotation2D slerp(const Scalar& t, const Rotation2D& other) const
0131 {
0132 Scalar dist = Rotation2D(other.m_angle-m_angle).smallestAngle();
0133 return Rotation2D(m_angle + dist*t);
0134 }
0135
0136
0137
0138
0139
0140
0141 template<typename NewScalarType>
0142 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type cast() const
0143 { return typename internal::cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type(*this); }
0144
0145
0146 template<typename OtherScalarType>
0147 EIGEN_DEVICE_FUNC inline explicit Rotation2D(const Rotation2D<OtherScalarType>& other)
0148 {
0149 m_angle = Scalar(other.angle());
0150 }
0151
0152 EIGEN_DEVICE_FUNC static inline Rotation2D Identity() { return Rotation2D(0); }
0153
0154
0155
0156
0157
0158 EIGEN_DEVICE_FUNC bool isApprox(const Rotation2D& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
0159 { return internal::isApprox(m_angle,other.m_angle, prec); }
0160
0161 };
0162
0163
0164
0165 typedef Rotation2D<float> Rotation2Df;
0166
0167
0168 typedef Rotation2D<double> Rotation2Dd;
0169
0170
0171
0172
0173
0174 template<typename Scalar>
0175 template<typename Derived>
0176 EIGEN_DEVICE_FUNC Rotation2D<Scalar>& Rotation2D<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
0177 {
0178 EIGEN_USING_STD(atan2)
0179 EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime==2 && Derived::ColsAtCompileTime==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
0180 m_angle = atan2(mat.coeff(1,0), mat.coeff(0,0));
0181 return *this;
0182 }
0183
0184
0185
0186 template<typename Scalar>
0187 typename Rotation2D<Scalar>::Matrix2
0188 EIGEN_DEVICE_FUNC Rotation2D<Scalar>::toRotationMatrix(void) const
0189 {
0190 EIGEN_USING_STD(sin)
0191 EIGEN_USING_STD(cos)
0192 Scalar sinA = sin(m_angle);
0193 Scalar cosA = cos(m_angle);
0194 return (Matrix2() << cosA, -sinA, sinA, cosA).finished();
0195 }
0196
0197 }
0198
0199 #endif