1
// This file is part of Eigen, a lightweight C++ template library
2
// for linear algebra. Eigen itself is part of the KDE project.
4
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
6
// Eigen is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 3 of the License, or (at your option) any later version.
11
// Alternatively, you can redistribute it and/or
12
// modify it under the terms of the GNU General Public License as
13
// published by the Free Software Foundation; either version 2 of
14
// the License, or (at your option) any later version.
16
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19
// GNU General Public License for more details.
21
// You should have received a copy of the GNU Lesser General Public
22
// License and a copy of the GNU General Public License along with
23
// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
#ifndef EIGEN_ROTATION2D_H
26
#define EIGEN_ROTATION2D_H
28
/** \geometry_module \ingroup Geometry_Module
32
* \brief Represents a rotation/orientation in a 2 dimensional space.
34
* \param _Scalar the scalar type, i.e., the type of the coefficients
36
* This class is equivalent to a single scalar representing a counter clock wise rotation
37
* as a single angle in radian. It provides some additional features such as the automatic
38
* conversion from/to a 2x2 rotation matrix. Moreover this class aims to provide a similar
39
* interface to Quaternion in order to facilitate the writing of generic algorithms
40
* dealing with rotations.
42
* \sa class Quaternion, class Transform
44
template<typename _Scalar> struct ei_traits<Rotation2D<_Scalar> >
46
typedef _Scalar Scalar;
49
template<typename _Scalar>
50
class Rotation2D : public RotationBase<Rotation2D<_Scalar>,2>
52
typedef RotationBase<Rotation2D<_Scalar>,2> Base;
56
using Base::operator*;
59
/** the scalar type of the coefficients */
60
typedef _Scalar Scalar;
61
typedef Matrix<Scalar,2,1> Vector2;
62
typedef Matrix<Scalar,2,2> Matrix2;
70
/** Construct a 2D counter clock wise rotation from the angle \a a in radian. */
71
inline Rotation2D(Scalar a) : m_angle(a) {}
73
/** \returns the rotation angle */
74
inline Scalar angle() const { return m_angle; }
76
/** \returns a read-write reference to the rotation angle */
77
inline Scalar& angle() { return m_angle; }
79
/** \returns the inverse rotation */
80
inline Rotation2D inverse() const { return -m_angle; }
82
/** Concatenates two rotations */
83
inline Rotation2D operator*(const Rotation2D& other) const
84
{ return m_angle + other.m_angle; }
86
/** Concatenates two rotations */
87
inline Rotation2D& operator*=(const Rotation2D& other)
88
{ return m_angle += other.m_angle; return *this; }
90
/** Applies the rotation to a 2D vector */
91
Vector2 operator* (const Vector2& vec) const
92
{ return toRotationMatrix() * vec; }
94
template<typename Derived>
95
Rotation2D& fromRotationMatrix(const MatrixBase<Derived>& m);
96
Matrix2 toRotationMatrix(void) const;
98
/** \returns the spherical interpolation between \c *this and \a other using
99
* parameter \a t. It is in fact equivalent to a linear interpolation.
101
inline Rotation2D slerp(Scalar t, const Rotation2D& other) const
102
{ return m_angle * (1-t) + other.angle() * t; }
104
/** \returns \c *this with scalar type casted to \a NewScalarType
106
* Note that if \a NewScalarType is equal to the current scalar type of \c *this
107
* then this function smartly returns a const reference to \c *this.
109
template<typename NewScalarType>
110
inline typename ei_cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type cast() const
111
{ return typename ei_cast_return_type<Rotation2D,Rotation2D<NewScalarType> >::type(*this); }
113
/** Copy constructor with scalar type conversion */
114
template<typename OtherScalarType>
115
inline explicit Rotation2D(const Rotation2D<OtherScalarType>& other)
117
m_angle = Scalar(other.angle());
120
/** \returns \c true if \c *this is approximately equal to \a other, within the precision
121
* determined by \a prec.
123
* \sa MatrixBase::isApprox() */
124
bool isApprox(const Rotation2D& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
125
{ return ei_isApprox(m_angle,other.m_angle, prec); }
128
/** \ingroup Geometry_Module
129
* single precision 2D rotation type */
130
typedef Rotation2D<float> Rotation2Df;
131
/** \ingroup Geometry_Module
132
* double precision 2D rotation type */
133
typedef Rotation2D<double> Rotation2Dd;
135
/** Set \c *this from a 2x2 rotation matrix \a mat.
136
* In other words, this function extract the rotation angle
137
* from the rotation matrix.
139
template<typename Scalar>
140
template<typename Derived>
141
Rotation2D<Scalar>& Rotation2D<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
143
EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime==2 && Derived::ColsAtCompileTime==2,YOU_MADE_A_PROGRAMMING_MISTAKE)
144
m_angle = ei_atan2(mat.coeff(1,0), mat.coeff(0,0));
148
/** Constructs and \returns an equivalent 2x2 rotation matrix.
150
template<typename Scalar>
151
typename Rotation2D<Scalar>::Matrix2
152
Rotation2D<Scalar>::toRotationMatrix(void) const
154
Scalar sinA = ei_sin(m_angle);
155
Scalar cosA = ei_cos(m_angle);
156
return (Matrix2() << cosA, -sinA, sinA, cosA).finished();
159
#endif // EIGEN_ROTATION2D_H