Main MRPT website > C++ reference
MRPT logo

Translation.h

Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_TRANSLATION_H
00026 #define EIGEN_TRANSLATION_H
00027 
00028 /** \geometry_module \ingroup Geometry_Module
00029   *
00030   * \class Translation
00031   *
00032   * \brief Represents a translation transformation
00033   *
00034   * \param _Scalar the scalar type, i.e., the type of the coefficients.
00035   * \param _Dim the  dimension of the space, can be a compile time value or Dynamic
00036   *
00037   * \note This class is not aimed to be used to store a translation transformation,
00038   * but rather to make easier the constructions and updates of Transform objects.
00039   *
00040   * \sa class Scaling, class Transform
00041   */
00042 template<typename _Scalar, int _Dim>
00043 class Translation
00044 {
00045 public:
00046   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
00047   /** dimension of the space */
00048   enum { Dim = _Dim };
00049   /** the scalar type of the coefficients */
00050   typedef _Scalar Scalar;
00051   /** corresponding vector type */
00052   typedef Matrix<Scalar,Dim,1> VectorType;
00053   /** corresponding linear transformation matrix type */
00054   typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
00055   /** corresponding affine transformation type */
00056   typedef Transform<Scalar,Dim,Affine> AffineTransformType;
00057 
00058 protected:
00059 
00060   VectorType m_coeffs;
00061 
00062 public:
00063 
00064   /** Default constructor without initialization. */
00065   Translation() {}
00066   /**  */
00067   inline Translation(const Scalar& sx, const Scalar& sy)
00068   {
00069     eigen_assert(Dim==2);
00070     m_coeffs.x() = sx;
00071     m_coeffs.y() = sy;
00072   }
00073   /**  */
00074   inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz)
00075   {
00076     eigen_assert(Dim==3);
00077     m_coeffs.x() = sx;
00078     m_coeffs.y() = sy;
00079     m_coeffs.z() = sz;
00080   }
00081   /** Constructs and initialize the translation transformation from a vector of translation coefficients */
00082   explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {}
00083 
00084   /** \brief Retruns the x-translation by value. **/
00085   inline Scalar x() const { return m_coeffs.x(); }
00086   /** \brief Retruns the y-translation by value. **/
00087   inline Scalar y() const { return m_coeffs.y(); }
00088   /** \brief Retruns the z-translation by value. **/
00089   inline Scalar z() const { return m_coeffs.z(); }
00090 
00091   /** \brief Retruns the x-translation as a reference. **/
00092   inline Scalar& x() { return m_coeffs.x(); }
00093   /** \brief Retruns the y-translation as a reference. **/
00094   inline Scalar& y() { return m_coeffs.y(); }
00095   /** \brief Retruns the z-translation as a reference. **/
00096   inline Scalar& z() { return m_coeffs.z(); }
00097 
00098   const VectorType& vector() const { return m_coeffs; }
00099   VectorType& vector() { return m_coeffs; }
00100 
00101   const VectorType& translation() const { return m_coeffs; }
00102   VectorType& translation() { return m_coeffs; }
00103 
00104   /** Concatenates two translation */
00105   inline Translation operator* (const Translation& other) const
00106   { return Translation(m_coeffs + other.m_coeffs); }
00107 
00108   /** Concatenates a translation and a uniform scaling */
00109   inline AffineTransformType operator* (const UniformScaling<Scalar>& other) const;
00110 
00111   /** Concatenates a translation and a linear transformation */
00112   template<typename OtherDerived>
00113   inline AffineTransformType operator* (const EigenBase<OtherDerived>& linear) const;
00114 
00115   /** Concatenates a translation and a rotation */
00116   template<typename Derived>
00117   inline AffineTransformType operator*(const RotationBase<Derived,Dim>& r) const
00118   { return *this * r.toRotationMatrix(); }
00119 
00120   /** \returns the concatenation of a linear transformation \a l with the translation \a t */
00121   // its a nightmare to define a templated friend function outside its declaration
00122   template<typename OtherDerived> friend
00123   inline AffineTransformType operator*(const EigenBase<OtherDerived>& linear, const Translation& t)
00124   {
00125     AffineTransformType res;
00126     res.matrix().setZero();
00127     res.linear() = linear.derived();
00128     res.translation() = linear.derived() * t.m_coeffs;
00129     res.matrix().row(Dim).setZero();
00130     res(Dim,Dim) = Scalar(1);
00131     return res;
00132   }
00133 
00134   /** Concatenates a translation and a transformation */
00135   template<int Mode>
00136   inline Transform<Scalar,Dim,Mode> operator* (const Transform<Scalar,Dim,Mode>& t) const
00137   {
00138     Transform<Scalar,Dim,Mode> res = t;
00139     res.pretranslate(m_coeffs);
00140     return res;
00141   }
00142 
00143   /** Applies translation to vector */
00144   inline VectorType operator* (const VectorType& other) const
00145   { return m_coeffs + other; }
00146 
00147   /** \returns the inverse translation (opposite) */
00148   Translation inverse() const { return Translation(-m_coeffs); }
00149 
00150   Translation& operator=(const Translation& other)
00151   {
00152     m_coeffs = other.m_coeffs;
00153     return *this;
00154   }
00155 
00156   static const Translation Identity() { return Translation(VectorType::Zero()); }
00157 
00158   /** \returns \c *this with scalar type casted to \a NewScalarType
00159     *
00160     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
00161     * then this function smartly returns a const reference to \c *this.
00162     */
00163   template<typename NewScalarType>
00164   inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
00165   { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
00166 
00167   /** Copy constructor with scalar type conversion */
00168   template<typename OtherScalarType>
00169   inline explicit Translation(const Translation<OtherScalarType,Dim>& other)
00170   { m_coeffs = other.vector().template cast<Scalar>(); }
00171 
00172   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
00173     * determined by \a prec.
00174     *
00175     * \sa MatrixBase::isApprox() */
00176   bool isApprox(const Translation& other, typename NumTraits<Scalar>::Real prec = NumTraits<Scalar>::dummy_precision()) const
00177   { return m_coeffs.isApprox(other.m_coeffs, prec); }
00178 
00179 };
00180 
00181 /** \addtogroup Geometry_Module */
00182 //@{
00183 typedef Translation<float, 2> Translation2f;
00184 typedef Translation<double,2> Translation2d;
00185 typedef Translation<float, 3> Translation3f;
00186 typedef Translation<double,3> Translation3d;
00187 //@}
00188 
00189 template<typename Scalar, int Dim>
00190 inline typename Translation<Scalar,Dim>::AffineTransformType
00191 Translation<Scalar,Dim>::operator* (const UniformScaling<Scalar>& other) const
00192 {
00193   AffineTransformType res;
00194   res.matrix().setZero();
00195   res.linear().diagonal().fill(other.factor());
00196   res.translation() = m_coeffs;
00197   res(Dim,Dim) = Scalar(1);
00198   return res;
00199 }
00200 
00201 template<typename Scalar, int Dim>
00202 template<typename OtherDerived>
00203 inline typename Translation<Scalar,Dim>::AffineTransformType
00204 Translation<Scalar,Dim>::operator* (const EigenBase<OtherDerived>& linear) const
00205 {
00206   AffineTransformType res;
00207   res.matrix().setZero();
00208   res.linear() = linear.derived();
00209   res.translation() = m_coeffs;
00210   res.matrix().row(Dim).setZero();
00211   res(Dim,Dim) = Scalar(1);
00212   return res;
00213 }
00214 
00215 #endif // EIGEN_TRANSLATION_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011