// 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 // // 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 . #ifndef EIGEN_TRANSFORM_H #define EIGEN_TRANSFORM_H /** \class Transform * * \brief Represents an homogeneous transformation in a N dimensional space * * \param _Scalar the scalar type, i.e., the type of the coefficients * \param _Dim the dimension of the space * * The homography is internally represented and stored as a (Dim+1)^2 matrix which * is available through the matrix() method. * * Conversion methods from/to Qt's QMatrix are available if the preprocessor token * EIGEN_QT_SUPPORT is defined. * */ template class Transform { public: enum { Dim = _Dim, HDim = _Dim+1 }; /** the scalar type of the coefficients */ typedef _Scalar Scalar; typedef Matrix MatrixType; typedef Matrix AffineMatrixType; typedef Block AffineMatrixRef; typedef Matrix VectorType; typedef Block VectorRef; protected: MatrixType m_matrix; template struct ei_transform_product_impl; // FIXME these specializations of ei_transform_product_impl does not work with gcc 3.3 and 3.4 because // Dim depends on a template parameter. Replacing Dim by 3 (for the 3D case) works. // note that these specializations have to be defined here, // otherwise some compilers (at least ICC and NVCC) complain about // the use of Dim in the specialization parameters. template struct ei_transform_product_impl { typedef typename Transform::MatrixType MatrixType; typedef typename ProductReturnType::Type ResultType; static ResultType run(const Transform& tr, const Other& other) { return tr.matrix() * other; } }; template struct ei_transform_product_impl { typedef typename Transform::MatrixType MatrixType; typedef typename ProductReturnType::Type ResultType; static ResultType run(const Transform& tr, const Other& other) { return tr.matrix() * other; } }; template struct ei_transform_product_impl { typedef typename Transform::AffineMatrixRef MatrixType; typedef const CwiseUnaryOp< ei_scalar_multiple_op, NestByValue, NestByValue,Other>::Type >, NestByValue::VectorRef> > > > ResultType; // FIXME shall we offer an optimized version when the last row is know to be 0,0...,0,1 ? static ResultType run(const Transform& tr, const Other& other) { return ((tr.affine().nestByValue() * other).nestByValue() + tr.translation().nestByValue()).nestByValue() * (Scalar(1) / ( (tr.matrix().template block<1,Dim>(Dim,0) * other).coeff(0) + tr.matrix().coeff(Dim,Dim))); } }; public: /** Default constructor without initialization of the coefficients. */ Transform() { } inline Transform(const Transform& other) { m_matrix = other.m_matrix; } inline Transform& operator=(const Transform& other) { m_matrix = other.m_matrix; return *this; } template inline explicit Transform(const MatrixBase& other) { m_matrix = other; } template inline Transform& operator=(const MatrixBase& other) { m_matrix = other; return *this; } #ifdef EIGEN_QT_SUPPORT inline Transform(const QMatrix& other); inline Transform& operator=(const QMatrix& other); inline QMatrix toQMatrix(void) const; #endif /** \returns a read-only expression of the transformation matrix */ inline const MatrixType& matrix() const { return m_matrix; } /** \returns a writable expression of the transformation matrix */ inline MatrixType& matrix() { return m_matrix; } /** \returns a read-only expression of the affine (linear) part of the transformation */ inline const AffineMatrixRef affine() const { return m_matrix.template block(0,0); } /** \returns a writable expression of the affine (linear) part of the transformation */ inline AffineMatrixRef affine() { return m_matrix.template block(0,0); } /** \returns a read-only expression of the translation vector of the transformation */ inline const VectorRef translation() const { return m_matrix.template block(0,Dim); } /** \returns a writable expression of the translation vector of the transformation */ inline VectorRef translation() { return m_matrix.template block(0,Dim); } template const typename ei_transform_product_impl::ResultType operator * (const MatrixBase &other) const; /** Contatenates two transformations */ const typename ProductReturnType::Type operator * (const Transform& other) const { return m_matrix * other.matrix(); } void setIdentity() { m_matrix.setIdentity(); } template Transform& scale(const MatrixBase &other); template Transform& prescale(const MatrixBase &other); template Transform& translate(const MatrixBase &other); template Transform& pretranslate(const MatrixBase &other); template Transform& rotate(const RotationType& rotation); template Transform& prerotate(const RotationType& rotation); template Transform& shear(Scalar sx, Scalar sy); template Transform& preshear(Scalar sx, Scalar sy); AffineMatrixType extractRotation() const; AffineMatrixType extractRotationNoShear() const; template Transform& fromPositionOrientationScale(const MatrixBase &position, const OrientationType& orientation, const MatrixBase &scale); const Inverse inverse() const { return m_matrix.inverse(); } protected: }; #ifdef EIGEN_QT_SUPPORT /** Initialises \c *this from a QMatrix assuming the dimension is 2. */ template Transform::Transform(const QMatrix& other) { *this = other; } /** Set \c *this from a QMatrix assuming the dimension is 2. */ template Transform& Transform::operator=(const QMatrix& other) { EIGEN_STATIC_ASSERT(Dim==2, you_did_a_programming_error); m_matrix << other.m11(), other.m21(), other.dx(), other.m12(), other.m22(), other.dy(), 0, 0, 1; return *this; } /** \returns a QMatrix from \c *this assuming the dimension is 2. */ template QMatrix Transform::toQMatrix(void) const { EIGEN_STATIC_ASSERT(Dim==2, you_did_a_programming_error); return QMatrix( other.coeffRef(0,0), other.coeffRef(1,0), other.coeffRef(0,1), other.coeffRef(1,1), other.coeffRef(0,2), other.coeffRef(1,2), } #endif template template const typename Transform::template ei_transform_product_impl::ResultType Transform::operator*(const MatrixBase &other) const { return ei_transform_product_impl::run(*this,other.derived()); } /** Applies on the right the non uniform scale transformation represented * by the vector \a other to \c *this and returns a reference to \c *this. * \sa prescale() */ template template Transform& Transform::scale(const MatrixBase &other) { EIGEN_STATIC_ASSERT(int(OtherDerived::IsVectorAtCompileTime) && int(OtherDerived::SizeAtCompileTime)==int(Dim), you_did_a_programming_error); affine() = (affine() * other.asDiagonal()).lazy(); return *this; } /** Applies on the left the non uniform scale transformation represented * by the vector \a other to \c *this and returns a reference to \c *this. * \sa scale() */ template template Transform& Transform::prescale(const MatrixBase &other) { EIGEN_STATIC_ASSERT(int(OtherDerived::IsVectorAtCompileTime) && int(OtherDerived::SizeAtCompileTime)==int(Dim), you_did_a_programming_error); m_matrix.template block(0,0) = (other.asDiagonal() * m_matrix.template block(0,0)).lazy(); return *this; } /** Applies on the right the translation matrix represented by the vector \a other * to \c *this and returns a reference to \c *this. * \sa pretranslate() */ template template Transform& Transform::translate(const MatrixBase &other) { EIGEN_STATIC_ASSERT(int(OtherDerived::IsVectorAtCompileTime) && int(OtherDerived::SizeAtCompileTime)==int(Dim), you_did_a_programming_error); translation() += affine() * other; return *this; } /** Applies on the left the translation matrix represented by the vector \a other * to \c *this and returns a reference to \c *this. * \sa translate() */ template template Transform& Transform::pretranslate(const MatrixBase &other) { EIGEN_STATIC_ASSERT(int(OtherDerived::IsVectorAtCompileTime) && int(OtherDerived::SizeAtCompileTime)==int(Dim), you_did_a_programming_error); translation() += other; return *this; } /** Applies on the right the rotation represented by the rotation \a rotation * to \c *this and returns a reference to \c *this. * * The template parameter \a RotationType is the type of the rotation which * must be registered by ToRotationMatrix<>. * * Natively supported types includes: * - any scalar (2D), * - a Dim x Dim matrix expression, * - Quaternion (3D), * - AngleAxis (3D) * * This mechanism is easily extendable to support user types such as Euler angles, * or a pair of Quaternion for 4D rotations. * * \sa rotate(Scalar), class Quaternion, class AngleAxis, class ToRotationMatrix, prerotate(RotationType) */ template template Transform& Transform::rotate(const RotationType& rotation) { affine() *= ToRotationMatrix::convert(rotation); return *this; } /** Applies on the left the rotation represented by the rotation \a rotation * to \c *this and returns a reference to \c *this. * * See rotate(RotationType) for further details. * * \sa rotate(RotationType), rotate(Scalar) */ template template Transform& Transform::prerotate(const RotationType& rotation) { m_matrix.template block(0,0) = ToRotationMatrix::convert(rotation) * m_matrix.template block(0,0); return *this; } /** Applies on the right the shear transformation represented * by the vector \a other to \c *this and returns a reference to \c *this. * \warning 2D only. * \sa preshear() */ template template Transform& Transform::shear(Scalar sx, Scalar sy) { EIGEN_STATIC_ASSERT(int(OtherDerived::IsVectorAtCompileTime) && int(OtherDerived::SizeAtCompileTime)==int(Dim) && int(Dim)==2, you_did_a_programming_error); VectorType tmp = affine().col(0)*sy + affine().col(1); affine() << affine().col(0) + affine().col(1)*sx, tmp; return *this; } /** Applies on the left the shear transformation represented * by the vector \a other to \c *this and returns a reference to \c *this. * \warning 2D only. * \sa shear() */ template template Transform& Transform::preshear(Scalar sx, Scalar sy) { EIGEN_STATIC_ASSERT(int(OtherDerived::IsVectorAtCompileTime) && int(OtherDerived::SizeAtCompileTime)==int(Dim), you_did_a_programming_error); m_matrix.template block(0,0) = AffineMatrixType(1, sx, sy, 1) * m_matrix.template block(0,0); return *this; } /** \returns the rotation part of the transformation using a QR decomposition. * \sa extractRotationNoShear() */ template typename Transform::AffineMatrixType Transform::extractRotation() const { return affine().qr().matrixQ(); } /** \returns the rotation part of the transformation assuming no shear in * the affine part. * \sa extractRotation() */ template typename Transform::AffineMatrixType Transform::extractRotationNoShear() const { return affine().cwiseAbs2() .verticalRedux(ei_scalar_sum_op()).cwiseSqrt(); } /** Convenient method to set \c *this from a position, orientation and scale * of a 3D object. */ template template Transform& Transform::fromPositionOrientationScale(const MatrixBase &position, const OrientationType& orientation, const MatrixBase &scale) { affine() = ToRotationMatrix::convert(orientation); translation() = position; m_matrix(Dim,Dim) = 1.; m_matrix.template block<1,Dim>(Dim,0).setZero(); affine() *= scale.asDiagonal(); return *this; } #endif // EIGEN_TRANSFORM_H