// 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 * * */ 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; public: inline const MatrixType matrix() const { return m_matrix; } inline MatrixType matrix() { return m_matrix; } inline const AffineMatrixRef affine() const { return m_matrix.template block(0,0); } inline AffineMatrixRef affine() { return m_matrix.template block(0,0); } inline const VectorRef translation() const { return m_matrix.template block(0,Dim); } inline VectorRef translation() { return m_matrix.template block(0,Dim); } template struct ProductReturnType { typedef typename ei_transform_product_impl::ResultType Type; }; template const typename ProductReturnType::Type operator * (const MatrixBase &other) const; 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); AffineMatrixType extractRotation() const; AffineMatrixType extractRotationNoShear() const; protected: }; template template const typename Transform::template ProductReturnType::Type 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<3,4>(0,0) = (other.asDiagonal().eval() * m_matrix.template block<3,4>(0,0)).lazy(); return *this; } /** Applies on the right 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 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; } /** \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(); } //---------- template template struct Transform::ei_transform_product_impl { typedef typename Transform::MatrixType MatrixType; typedef Product ResultType; static ResultType run(const Transform& tr, const Other& other) { return tr.matrix() * other; } }; template template struct Transform::ei_transform_product_impl { typedef typename Transform::MatrixType MatrixType; typedef Product ResultType; static ResultType run(const Transform& tr, const Other& other) { return tr.matrix() * other; } }; template template struct Transform::ei_transform_product_impl { typedef typename Transform::AffineMatrixRef MatrixType; typedef const CwiseBinaryOp< ei_scalar_sum_op, NestByValue,Other> >, NestByValue::VectorRef> > ResultType; static ResultType run(const Transform& tr, const Other& other) { return (tr.affine().nestByValue() * other).nestByValue() + tr.translation().nestByValue(); } }; #endif // EIGEN_TRANSFORM_H