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 namespace Eigen { 
00029 
00044 template<typename _Scalar, int _Dim>
00045 class Translation
00046 {
00047 public:
00048   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
00050   enum { Dim = _Dim };
00052   typedef _Scalar Scalar;
00054   typedef Matrix<Scalar,Dim,1> VectorType;
00056   typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
00058   typedef Transform<Scalar,Dim,Affine> AffineTransformType;
00060   typedef Transform<Scalar,Dim,Isometry> IsometryTransformType;
00061 
00062 protected:
00063 
00064   VectorType m_coeffs;
00065 
00066 public:
00067 
00069   Translation() {}
00071   inline Translation(const Scalar& sx, const Scalar& sy)
00072   {
00073     eigen_assert(Dim==2);
00074     m_coeffs.x() = sx;
00075     m_coeffs.y() = sy;
00076   }
00078   inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz)
00079   {
00080     eigen_assert(Dim==3);
00081     m_coeffs.x() = sx;
00082     m_coeffs.y() = sy;
00083     m_coeffs.z() = sz;
00084   }
00086   explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {}
00087 
00089   inline Scalar x() const { return m_coeffs.x(); }
00091   inline Scalar y() const { return m_coeffs.y(); }
00093   inline Scalar z() const { return m_coeffs.z(); }
00094 
00096   inline Scalar& x() { return m_coeffs.x(); }
00098   inline Scalar& y() { return m_coeffs.y(); }
00100   inline Scalar& z() { return m_coeffs.z(); }
00101 
00102   const VectorType& vector() const { return m_coeffs; }
00103   VectorType& vector() { return m_coeffs; }
00104 
00105   const VectorType& translation() const { return m_coeffs; }
00106   VectorType& translation() { return m_coeffs; }
00107 
00109   inline Translation operator* (const Translation& other) const
00110   { return Translation(m_coeffs + other.m_coeffs); }
00111 
00113   inline AffineTransformType operator* (const UniformScaling<Scalar>& other) const;
00114 
00116   template<typename OtherDerived>
00117   inline AffineTransformType operator* (const EigenBase<OtherDerived>& linear) const;
00118 
00120   template<typename Derived>
00121   inline IsometryTransformType operator*(const RotationBase<Derived,Dim>& r) const
00122   { return *this * IsometryTransformType(r); }
00123 
00125   // its a nightmare to define a templated friend function outside its declaration
00126   template<typename OtherDerived> friend
00127   inline AffineTransformType operator*(const EigenBase<OtherDerived>& linear, const Translation& t)
00128   {
00129     AffineTransformType res;
00130     res.matrix().setZero();
00131     res.linear() = linear.derived();
00132     res.translation() = linear.derived() * t.m_coeffs;
00133     res.matrix().row(Dim).setZero();
00134     res(Dim,Dim) = Scalar(1);
00135     return res;
00136   }
00137 
00139   template<int Mode, int Options>
00140   inline Transform<Scalar,Dim,Mode> operator* (const Transform<Scalar,Dim,Mode,Options>& t) const
00141   {
00142     Transform<Scalar,Dim,Mode> res = t;
00143     res.pretranslate(m_coeffs);
00144     return res;
00145   }
00146 
00148   inline VectorType operator* (const VectorType& other) const
00149   { return m_coeffs + other; }
00150 
00152   Translation inverse() const { return Translation(-m_coeffs); }
00153 
00154   Translation& operator=(const Translation& other)
00155   {
00156     m_coeffs = other.m_coeffs;
00157     return *this;
00158   }
00159 
00160   static const Translation Identity() { return Translation(VectorType::Zero()); }
00161 
00167   template<typename NewScalarType>
00168   inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
00169   { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
00170 
00172   template<typename OtherScalarType>
00173   inline explicit Translation(const Translation<OtherScalarType,Dim>& other)
00174   { m_coeffs = other.vector().template cast<Scalar>(); }
00175 
00180   bool isApprox(const Translation& other, typename NumTraits<Scalar>::Real prec = NumTraits<Scalar>::dummy_precision()) const
00181   { return m_coeffs.isApprox(other.m_coeffs, prec); }
00182 
00183 };
00184 
00187 typedef Translation<float, 2> Translation2f;
00188 typedef Translation<double,2> Translation2d;
00189 typedef Translation<float, 3> Translation3f;
00190 typedef Translation<double,3> Translation3d;
00192 
00193 template<typename Scalar, int Dim>
00194 inline typename Translation<Scalar,Dim>::AffineTransformType
00195 Translation<Scalar,Dim>::operator* (const UniformScaling<Scalar>& other) const
00196 {
00197   AffineTransformType res;
00198   res.matrix().setZero();
00199   res.linear().diagonal().fill(other.factor());
00200   res.translation() = m_coeffs;
00201   res(Dim,Dim) = Scalar(1);
00202   return res;
00203 }
00204 
00205 template<typename Scalar, int Dim>
00206 template<typename OtherDerived>
00207 inline typename Translation<Scalar,Dim>::AffineTransformType
00208 Translation<Scalar,Dim>::operator* (const EigenBase<OtherDerived>& linear) const
00209 {
00210   AffineTransformType res;
00211   res.matrix().setZero();
00212   res.linear() = linear.derived();
00213   res.translation() = m_coeffs;
00214   res.matrix().row(Dim).setZero();
00215   res(Dim,Dim) = Scalar(1);
00216   return res;
00217 }
00218 
00219 } // end namespace Eigen
00220 
00221 #endif // EIGEN_TRANSLATION_H