// 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 /** Represents some traits of a transformation */ enum TransformTraits { Isometry, ///< the transformation is a concatenation of translations and rotations Affine, ///< the transformation is affine (linear transformation + translation) Projective ///< the transformation might not be affine }; // Note that we have to pass Dim and HDim because it is not allowed to use a template // parameter to define a template specialization. To be more precise, in the following // specializations, it is not allowed to use Dim+1 instead of HDim. template< typename Other, int Dim, int HDim, int OtherRows=Other::RowsAtCompileTime, int OtherCols=Other::ColsAtCompileTime> struct ei_transform_product_impl; /** \geometry_module \ingroup GeometryModule * * \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 and QTransform are available if the * preprocessor token EIGEN_QT_SUPPORT is defined. * * \sa class Matrix, class Quaternion */ template class Transform #ifdef EIGEN_VECTORIZE : public ei_with_aligned_operator_new<_Scalar,_Dim==Dynamic ? Dynamic : (_Dim+1)*(_Dim+1)> #endif { public: enum { Dim = _Dim, ///< space dimension in which the transformation holds HDim = _Dim+1 ///< size of a respective homogeneous vector }; /** the scalar type of the coefficients */ typedef _Scalar Scalar; /** type of the matrix used to represent the transformation */ typedef Matrix MatrixType; /** type of the matrix used to represent the linear part of the transformation */ typedef Matrix LinearMatrixType; /** type of read/write reference to the linear part of the transformation */ typedef Block LinearPart; /** type of a vector */ typedef Matrix VectorType; /** type of a read/write reference to the translation part of the rotation */ typedef Block TranslationPart; /** corresponding translation type */ typedef Translation TranslationType; /** corresponding scaling transformation type */ typedef Scaling ScalingType; protected: MatrixType m_matrix; public: /** Default constructor without initialization of the coefficients. */ inline 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 struct construct_from_matrix { static inline void run(Transform *transform, const MatrixBase& other) { transform->matrix() = other; } }; template struct construct_from_matrix { static inline void run(Transform *transform, const MatrixBase& other) { transform->linear() = other; transform->translation().setZero(); transform->matrix()(Dim,Dim) = Scalar(1); transform->matrix().template block<1,Dim>(Dim,0).setZero(); } }; /** Constructs and initializes a transformation from a Dim^2 or a (Dim+1)^2 matrix. */ template inline explicit Transform(const MatrixBase& other) { construct_from_matrix::run(this, other); } /** Set \c *this from a (Dim+1)^2 matrix. */ 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; inline Transform(const QTransform& other); inline Transform& operator=(const QTransform& other); inline QTransform toQTransform(void) const; #endif /** shortcut for m_matrix(row,col); * \sa MatrixBase::operaror(int,int) const */ inline Scalar operator() (int row, int col) const { return m_matrix(row,col); } /** shortcut for m_matrix(row,col); * \sa MatrixBase::operaror(int,int) */ inline Scalar& operator() (int row, int col) { return m_matrix(row,col); } /** \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 linear (linear) part of the transformation */ inline const LinearPart linear() const { return m_matrix.template block(0,0); } /** \returns a writable expression of the linear (linear) part of the transformation */ inline LinearPart linear() { return m_matrix.template block(0,0); } /** \returns a read-only expression of the translation vector of the transformation */ inline const TranslationPart translation() const { return m_matrix.template block(0,Dim); } /** \returns a writable expression of the translation vector of the transformation */ inline TranslationPart translation() { return m_matrix.template block(0,Dim); } /** \returns an expression of the product between the transform \c *this and a matrix expression \a other * * The right hand side \a other might be either: * \li a vector of size Dim, * \li an homogeneous vector of size Dim+1, * \li a transformation matrix of size Dim+1 x Dim+1. */ // note: this function is defined here because some compilers cannot find the respective declaration template inline const typename ei_transform_product_impl::ResultType operator * (const MatrixBase &other) const { return ei_transform_product_impl::run(*this,other.derived()); } /** Contatenates two transformations */ inline const typename ProductReturnType::Type operator * (const Transform& other) const { return m_matrix * other.matrix(); } /** \sa MatrixBase::setIdentity() */ void setIdentity() { m_matrix.setIdentity(); } template inline Transform& scale(const MatrixBase &other); template inline Transform& prescale(const MatrixBase &other); inline Transform& scale(Scalar s); inline Transform& prescale(Scalar s); template inline Transform& translate(const MatrixBase &other); template inline Transform& pretranslate(const MatrixBase &other); template inline Transform& rotate(const RotationType& rotation); template inline Transform& prerotate(const RotationType& rotation); Transform& shear(Scalar sx, Scalar sy); Transform& preshear(Scalar sx, Scalar sy); inline Transform& operator=(const TranslationType& t); inline Transform& operator*=(const TranslationType& t) { return translate(t.vector()); } inline Transform operator*(const TranslationType& t) const; inline Transform& operator=(const ScalingType& t); inline Transform& operator*=(const ScalingType& s) { return scale(s.coeffs()); } inline Transform operator*(const ScalingType& s) const; friend inline Transform operator*(const LinearMatrixType& mat, const Transform& t) { Transform res = t; res.matrix().row(Dim) = t.matrix().row(Dim); res.matrix().template block(0,0) = (mat * t.matrix().template block(0,0)).lazy(); return res; } // template // inline Transform& operator=(const Rotation& t); template inline Transform& operator*=(const RotationBase& r) { return rotate(r.toRotationMatrix()); } template inline Transform operator*(const RotationBase& r) const; LinearMatrixType extractRotation(TransformTraits traits = Affine) const; template Transform& fromPositionOrientationScale(const MatrixBase &position, const OrientationType& orientation, const MatrixBase &scale); inline const MatrixType inverse(TransformTraits traits = Affine) const; /** \returns a const pointer to the column major internal matrix */ const Scalar* data() const { return m_matrix.data(); } /** \returns a non-const pointer to the column major internal matrix */ Scalar* data() { return m_matrix.data(); } /** \returns \c *this with scalar type casted to \a NewScalarType * * Note that if \a NewScalarType is equal to the current scalar type of \c *this * then this function smartly returns a const reference to \c *this. */ template inline typename ei_cast_return_type >::type cast() const { return typename ei_cast_return_type >::type(*this); } /** Copy constructor with scalar type conversion */ template inline explicit Transform(const Transform& other) { m_matrix = other.matrix().template cast(); } /** \returns \c true if \c *this is approximately equal to \a other, within the precision * determined by \a prec. * * \sa MatrixBase::isApprox() */ bool isApprox(const Transform& other, typename NumTraits::Real prec = precision()) const { return m_matrix.isApprox(other.m_matrix, prec); } protected: }; /** \ingroup GeometryModule */ typedef Transform Transform2f; /** \ingroup GeometryModule */ typedef Transform Transform3f; /** \ingroup GeometryModule */ typedef Transform Transform2d; /** \ingroup GeometryModule */ typedef Transform Transform3d; /************************** *** Optional QT support *** **************************/ #ifdef EIGEN_QT_SUPPORT /** Initialises \c *this from a QMatrix assuming the dimension is 2. * * This function is available only if the token EIGEN_QT_SUPPORT is defined. */ template Transform::Transform(const QMatrix& other) { *this = other; } /** Set \c *this from a QMatrix assuming the dimension is 2. * * This function is available only if the token EIGEN_QT_SUPPORT is defined. */ template Transform& Transform::operator=(const QMatrix& other) { EIGEN_STATIC_ASSERT(Dim==2, you_made_a_programming_mistake) 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. * * \warning this convertion might loss data if \c *this is not affine * * This function is available only if the token EIGEN_QT_SUPPORT is defined. */ template QMatrix Transform::toQMatrix(void) const { EIGEN_STATIC_ASSERT(Dim==2, you_made_a_programming_mistake) 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)); } /** Initialises \c *this from a QTransform assuming the dimension is 2. * * This function is available only if the token EIGEN_QT_SUPPORT is defined. */ template Transform::Transform(const QTransform& other) { *this = other; } /** Set \c *this from a QTransform assuming the dimension is 2. * * This function is available only if the token EIGEN_QT_SUPPORT is defined. */ template Transform& Transform::operator=(const QTransform& other) { EIGEN_STATIC_ASSERT(Dim==2, you_made_a_programming_mistake) m_matrix << other.m11(), other.m21(), other.dx(), other.m12(), other.m22(), other.dy(), other.m13(), other.m23(), other.m33(); return *this; } /** \returns a QTransform from \c *this assuming the dimension is 2. * * This function is available only if the token EIGEN_QT_SUPPORT is defined. */ template QMatrix Transform::toQTransform(void) const { EIGEN_STATIC_ASSERT(Dim==2, you_made_a_programming_mistake) return QTransform(other.coeffRef(0,0), other.coeffRef(1,0), other.coeffRef(2,0) other.coeffRef(0,1), other.coeffRef(1,1), other.coeffRef(2,1) other.coeffRef(0,2), other.coeffRef(1,2), other.coeffRef(2,2); } #endif /********************* *** Procedural API *** *********************/ /** 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_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim)) linear() = (linear() * other.asDiagonal()).lazy(); return *this; } /** Applies on the right a uniform scale of a factor \a c to \c *this * and returns a reference to \c *this. * \sa prescale(Scalar) */ template inline Transform& Transform::scale(Scalar s) { linear() *= s; 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_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim)) m_matrix.template block(0,0) = (other.asDiagonal() * m_matrix.template block(0,0)).lazy(); return *this; } /** Applies on the left a uniform scale of a factor \a c to \c *this * and returns a reference to \c *this. * \sa scale(Scalar) */ template inline Transform& Transform::prescale(Scalar s) { m_matrix.template corner(TopLeft) *= s; 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_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim)) translation() += linear() * 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_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim)) 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 known by ei_toRotationMatrix<>. * * Natively supported types includes: * - any scalar (2D), * - a Dim x Dim matrix expression, * - a Quaternion (3D), * - a 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, prerotate(RotationType) */ template template Transform& Transform::rotate(const RotationType& rotation) { linear() *= ei_toRotationMatrix(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() for further details. * * \sa rotate() */ template template Transform& Transform::prerotate(const RotationType& rotation) { m_matrix.template block(0,0) = ei_toRotationMatrix(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 Transform& Transform::shear(Scalar sx, Scalar sy) { EIGEN_STATIC_ASSERT(int(Dim)==2, you_made_a_programming_mistake) VectorType tmp = linear().col(0)*sy + linear().col(1); linear() << linear().col(0) + linear().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 Transform& Transform::preshear(Scalar sx, Scalar sy) { EIGEN_STATIC_ASSERT(int(Dim)==2, you_made_a_programming_mistake) m_matrix.template block(0,0) = LinearMatrixType(1, sx, sy, 1) * m_matrix.template block(0,0); return *this; } /****************************************************** *** Scaling, Translation and Rotation compatibility *** ******************************************************/ template inline Transform& Transform::operator=(const TranslationType& t) { setIdentity(); translation() = t.vector(); return *this; } template inline Transform Transform::operator*(const TranslationType& t) const { Transform res = *this; res.translate(t.vector()); return res; } template inline Transform& Transform::operator=(const ScalingType& s) { m_matrix.setZero(); linear().diagonal() = s.coeffs(); m_matrix(Dim,Dim) = Scalar(1); return *this; } template inline Transform Transform::operator*(const ScalingType& s) const { Transform res = *this; res.scale(s.coeffs()); return res; } template template inline Transform Transform::operator*(const RotationBase& r) const { Transform res = *this; res.rotate(r.derived()); return res; } /*************************** *** Specialial functions *** ***************************/ /** \returns the rotation part of the transformation * * \param traits allows to optimize the extraction process when the transformion * is known to be not a general aafine transformation. The possible values are: * - Affine which use a QR decomposition (default), * - Isometry which simply returns the linear part ! * * \warning this function consider the scaling is positive * * \warning to use this method in the general case (traits==GenericAffine), you need * to include the QR module. * * \sa inverse(), class QR */ template typename Transform::LinearMatrixType Transform::extractRotation(TransformTraits traits) const { ei_assert(traits!=Projective && "you cannot extract a rotation from a non affine transformation"); if (traits == Affine) { // FIXME maybe QR should be fixed to return a R matrix with a positive diagonal ?? QR qr(linear()); LinearMatrixType matQ = qr.matrixQ(); LinearMatrixType matR = qr.matrixR(); for (int i=0 ; i template Transform& Transform::fromPositionOrientationScale(const MatrixBase &position, const OrientationType& orientation, const MatrixBase &scale) { linear() = ei_toRotationMatrix(orientation); linear() *= scale.asDiagonal(); translation() = position; m_matrix(Dim,Dim) = 1.; m_matrix.template block<1,Dim>(Dim,0).setZero(); return *this; } /** \returns the inverse transformation matrix according to some given knowledge * on \c *this. * * \param traits allows to optimize the inversion process when the transformion * is known to be not a general transformation. The possible values are: * - Projective if the transformation is not necessarily affine, i.e., if the * last row is not guaranteed to be [0 ... 0 1] * - Affine is the default, the last row is assumed to be [0 ... 0 1] * - Isometry if the transformation is only a concatenations of translations * and rotations. * * \warning unless \a traits is always set to NoShear or NoScaling, this function * requires the generic inverse method of MatrixBase defined in the LU module. If * you forget to include this module, then you will get hard to debug linking errors. * * \sa MatrixBase::inverse() */ template inline const typename Transform::MatrixType Transform::inverse(TransformTraits traits) const { if (traits == Projective) { return m_matrix.inverse(); } else { MatrixType res; if (traits == Affine) { res.template corner(TopLeft) = linear().inverse(); } else if (traits == Isometry) { res.template corner(TopLeft) = linear().transpose(); } else { ei_assert("invalid traits value in Transform::inverse()"); } // translation and remaining parts res.template corner(TopRight) = - res.template corner(TopLeft) * translation(); res.template corner<1,Dim>(BottomLeft).setZero(); res.coeffRef(Dim,Dim) = Scalar(1); return res; } } /***************************************************** *** Specializations of operator* with a MatrixBase *** *****************************************************/ template struct ei_transform_product_impl { typedef Transform TransformType; typedef typename TransformType::MatrixType MatrixType; typedef typename ProductReturnType::Type ResultType; static ResultType run(const TransformType& tr, const Other& other) { return tr.matrix() * other; } }; template struct ei_transform_product_impl { typedef Transform TransformType; typedef typename TransformType::MatrixType MatrixType; typedef TransformType ResultType; static ResultType run(const TransformType& tr, const Other& other) { TransformType res; res.translation() = tr.translation(); res.matrix().row(Dim) = tr.matrix().row(Dim); res.linear() = (tr.linear() * other).lazy(); return res; } }; template struct ei_transform_product_impl { typedef Transform TransformType; typedef typename TransformType::MatrixType MatrixType; typedef typename ProductReturnType::Type ResultType; static ResultType run(const TransformType& tr, const Other& other) { return tr.matrix() * other; } }; template struct ei_transform_product_impl { typedef typename Other::Scalar Scalar; typedef Transform TransformType; typedef typename TransformType::LinearPart MatrixType; typedef const CwiseUnaryOp< ei_scalar_multiple_op, NestByValue, NestByValue,Other>::Type >, NestByValue > > > ResultType; // FIXME should we offer an optimized version when the last row is known to be 0,0...,0,1 ? static ResultType run(const TransformType& tr, const Other& other) { return ((tr.linear().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))); } }; #endif // EIGEN_TRANSFORM_H