diff options
author | Gael Guennebaud <g.gael@free.fr> | 2008-08-30 21:36:04 +0000 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2008-08-30 21:36:04 +0000 |
commit | 9c450a52a28e10f4caf87a968612fa8d007ce4b1 (patch) | |
tree | c95245adcfa486f6cf7d75bba3456bd226d778d3 /Eigen/src/Geometry/Rotation2D.h | |
parent | 6ba991aa3a2ddd1a1ed1d64956aeab5cab680e54 (diff) |
Split Rotation.h to Rotation2D.h and RotationBase.h,
and more code factorization based on RotationBase.
Added notes about the main aim of the Translation and Scaling classes.
Diffstat (limited to 'Eigen/src/Geometry/Rotation2D.h')
-rw-r--r-- | Eigen/src/Geometry/Rotation2D.h | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/Eigen/src/Geometry/Rotation2D.h b/Eigen/src/Geometry/Rotation2D.h new file mode 100644 index 000000000..1c3cbc05d --- /dev/null +++ b/Eigen/src/Geometry/Rotation2D.h @@ -0,0 +1,134 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_ROTATION2D_H +#define EIGEN_ROTATION2D_H + +/** \geometry_module \ingroup GeometryModule + * + * \class Rotation2D + * + * \brief Represents a rotation/orientation in a 2 dimensional space. + * + * \param _Scalar the scalar type, i.e., the type of the coefficients + * + * This class is equivalent to a single scalar representing a counter clock wise rotation + * as a single angle in radian. It provides some additional features such as the automatic + * conversion from/to a 2x2 rotation matrix. Moreover this class aims to provide a similar + * interface to Quaternion in order to facilitate the writing of generic algorithms + * dealing with rotations. + * + * \sa class Quaternion, class Transform + */ +template<typename _Scalar> struct ei_traits<Rotation2D<_Scalar> > +{ + typedef _Scalar Scalar; +}; + +template<typename _Scalar> +class Rotation2D : public RotationBase<Rotation2D<_Scalar>,2> +{ + typedef RotationBase<Rotation2D<_Scalar>,2> Base; + using Base::operator*; + +public: + enum { Dim = 2 }; + /** the scalar type of the coefficients */ + typedef _Scalar Scalar; + typedef Matrix<Scalar,2,1> Vector2; + typedef Matrix<Scalar,2,2> Matrix2; + +protected: + + Scalar m_angle; + +public: + + /** Construct a 2D counter clock wise rotation from the angle \a a in radian. */ + inline Rotation2D(Scalar a) : m_angle(a) {} + + /** \returns the rotation angle */ + inline Scalar angle() const { return m_angle; } + + /** \returns a read-write reference to the rotation angle */ + inline Scalar& angle() { return m_angle; } + + /** \returns the inverse rotation */ + inline Rotation2D inverse() const { return -m_angle; } + + /** Concatenates two rotations */ + inline Rotation2D operator*(const Rotation2D& other) const + { return m_angle + other.m_angle; } + + /** Concatenates two rotations */ + inline Rotation2D& operator*=(const Rotation2D& other) + { return m_angle += other.m_angle; } + + /** Applies the rotation to a 2D vector */ + Vector2 operator* (const Vector2& vec) const + { return toRotationMatrix() * vec; } + + template<typename Derived> + Rotation2D& fromRotationMatrix(const MatrixBase<Derived>& m); + Matrix2 toRotationMatrix(void) const; + + /** \returns the spherical interpolation between \c *this and \a other using + * parameter \a t. It is in fact equivalent to a linear interpolation. + */ + inline Rotation2D slerp(Scalar t, const Rotation2D& other) const + { return m_angle * (1-t) + other.angle() * t; } +}; + +/** \ingroup GeometryModule + * single precision 2D rotation type */ +typedef Rotation2D<float> Rotation2Df; +/** \ingroup GeometryModule + * double precision 2D rotation type */ +typedef Rotation2D<double> Rotation2Dd; + +/** Set \c *this from a 2x2 rotation matrix \a mat. + * In other words, this function extract the rotation angle + * from the rotation matrix. + */ +template<typename Scalar> +template<typename Derived> +Rotation2D<Scalar>& Rotation2D<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat) +{ + EIGEN_STATIC_ASSERT(Derived::RowsAtCompileTime==2 && Derived::ColsAtCompileTime==2,you_did_a_programming_error); + m_angle = ei_atan2(mat.coeff(1,0), mat.coeff(0,0)); + return *this; +} + +/** Constructs and \returns an equivalent 2x2 rotation matrix. + */ +template<typename Scalar> +typename Rotation2D<Scalar>::Matrix2 +Rotation2D<Scalar>::toRotationMatrix(void) const +{ + Scalar sinA = ei_sin(m_angle); + Scalar cosA = ei_cos(m_angle); + return (Matrix2() << cosA, -sinA, sinA, cosA).finished(); +} + +#endif // EIGEN_ROTATION2D_H |