1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_TRANSLATION_H
11 #define EIGEN_TRANSLATION_H
12 
13 namespace Eigen {
14 
15 /** \geometry_module \ingroup Geometry_Module
16   *
17   * \class Translation
18   *
19   * \brief Represents a translation transformation
20   *
21   * \param _Scalar the scalar type, i.e., the type of the coefficients.
22   * \param _Dim the  dimension of the space, can be a compile time value or Dynamic
23   *
24   * \note This class is not aimed to be used to store a translation transformation,
25   * but rather to make easier the constructions and updates of Transform objects.
26   *
27   * \sa class Scaling, class Transform
28   */
29 template<typename _Scalar, int _Dim>
30 class Translation
31 {
32 public:
33   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
34   /** dimension of the space */
35   enum { Dim = _Dim };
36   /** the scalar type of the coefficients */
37   typedef _Scalar Scalar;
38   /** corresponding vector type */
39   typedef Matrix<Scalar,Dim,1> VectorType;
40   /** corresponding linear transformation matrix type */
41   typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
42   /** corresponding affine transformation type */
43   typedef Transform<Scalar,Dim,Affine> AffineTransformType;
44   /** corresponding isometric transformation type */
45   typedef Transform<Scalar,Dim,Isometry> IsometryTransformType;
46 
47 protected:
48 
49   VectorType m_coeffs;
50 
51 public:
52 
53   /** Default constructor without initialization. */
Translation()54   Translation() {}
55   /**  */
Translation(const Scalar & sx,const Scalar & sy)56   inline Translation(const Scalar& sx, const Scalar& sy)
57   {
58     eigen_assert(Dim==2);
59     m_coeffs.x() = sx;
60     m_coeffs.y() = sy;
61   }
62   /**  */
Translation(const Scalar & sx,const Scalar & sy,const Scalar & sz)63   inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz)
64   {
65     eigen_assert(Dim==3);
66     m_coeffs.x() = sx;
67     m_coeffs.y() = sy;
68     m_coeffs.z() = sz;
69   }
70   /** Constructs and initialize the translation transformation from a vector of translation coefficients */
Translation(const VectorType & vector)71   explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {}
72 
73   /** \brief Retruns the x-translation by value. **/
x()74   inline Scalar x() const { return m_coeffs.x(); }
75   /** \brief Retruns the y-translation by value. **/
y()76   inline Scalar y() const { return m_coeffs.y(); }
77   /** \brief Retruns the z-translation by value. **/
z()78   inline Scalar z() const { return m_coeffs.z(); }
79 
80   /** \brief Retruns the x-translation as a reference. **/
x()81   inline Scalar& x() { return m_coeffs.x(); }
82   /** \brief Retruns the y-translation as a reference. **/
y()83   inline Scalar& y() { return m_coeffs.y(); }
84   /** \brief Retruns the z-translation as a reference. **/
z()85   inline Scalar& z() { return m_coeffs.z(); }
86 
vector()87   const VectorType& vector() const { return m_coeffs; }
vector()88   VectorType& vector() { return m_coeffs; }
89 
translation()90   const VectorType& translation() const { return m_coeffs; }
translation()91   VectorType& translation() { return m_coeffs; }
92 
93   /** Concatenates two translation */
94   inline Translation operator* (const Translation& other) const
95   { return Translation(m_coeffs + other.m_coeffs); }
96 
97   /** Concatenates a translation and a uniform scaling */
98   inline AffineTransformType operator* (const UniformScaling<Scalar>& other) const;
99 
100   /** Concatenates a translation and a linear transformation */
101   template<typename OtherDerived>
102   inline AffineTransformType operator* (const EigenBase<OtherDerived>& linear) const;
103 
104   /** Concatenates a translation and a rotation */
105   template<typename Derived>
106   inline IsometryTransformType operator*(const RotationBase<Derived,Dim>& r) const
107   { return *this * IsometryTransformType(r); }
108 
109   /** \returns the concatenation of a linear transformation \a l with the translation \a t */
110   // its a nightmare to define a templated friend function outside its declaration
111   template<typename OtherDerived> friend
112   inline AffineTransformType operator*(const EigenBase<OtherDerived>& linear, const Translation& t)
113   {
114     AffineTransformType res;
115     res.matrix().setZero();
116     res.linear() = linear.derived();
117     res.translation() = linear.derived() * t.m_coeffs;
118     res.matrix().row(Dim).setZero();
119     res(Dim,Dim) = Scalar(1);
120     return res;
121   }
122 
123   /** Concatenates a translation and a transformation */
124   template<int Mode, int Options>
125   inline Transform<Scalar,Dim,Mode> operator* (const Transform<Scalar,Dim,Mode,Options>& t) const
126   {
127     Transform<Scalar,Dim,Mode> res = t;
128     res.pretranslate(m_coeffs);
129     return res;
130   }
131 
132   /** Applies translation to vector */
133   inline VectorType operator* (const VectorType& other) const
134   { return m_coeffs + other; }
135 
136   /** \returns the inverse translation (opposite) */
inverse()137   Translation inverse() const { return Translation(-m_coeffs); }
138 
139   Translation& operator=(const Translation& other)
140   {
141     m_coeffs = other.m_coeffs;
142     return *this;
143   }
144 
Identity()145   static const Translation Identity() { return Translation(VectorType::Zero()); }
146 
147   /** \returns \c *this with scalar type casted to \a NewScalarType
148     *
149     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
150     * then this function smartly returns a const reference to \c *this.
151     */
152   template<typename NewScalarType>
cast()153   inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
154   { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
155 
156   /** Copy constructor with scalar type conversion */
157   template<typename OtherScalarType>
Translation(const Translation<OtherScalarType,Dim> & other)158   inline explicit Translation(const Translation<OtherScalarType,Dim>& other)
159   { m_coeffs = other.vector().template cast<Scalar>(); }
160 
161   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
162     * determined by \a prec.
163     *
164     * \sa MatrixBase::isApprox() */
165   bool isApprox(const Translation& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
166   { return m_coeffs.isApprox(other.m_coeffs, prec); }
167 
168 };
169 
170 /** \addtogroup Geometry_Module */
171 //@{
172 typedef Translation<float, 2> Translation2f;
173 typedef Translation<double,2> Translation2d;
174 typedef Translation<float, 3> Translation3f;
175 typedef Translation<double,3> Translation3d;
176 //@}
177 
178 template<typename Scalar, int Dim>
179 inline typename Translation<Scalar,Dim>::AffineTransformType
180 Translation<Scalar,Dim>::operator* (const UniformScaling<Scalar>& other) const
181 {
182   AffineTransformType res;
183   res.matrix().setZero();
184   res.linear().diagonal().fill(other.factor());
185   res.translation() = m_coeffs;
186   res(Dim,Dim) = Scalar(1);
187   return res;
188 }
189 
190 template<typename Scalar, int Dim>
191 template<typename OtherDerived>
192 inline typename Translation<Scalar,Dim>::AffineTransformType
193 Translation<Scalar,Dim>::operator* (const EigenBase<OtherDerived>& linear) const
194 {
195   AffineTransformType res;
196   res.matrix().setZero();
197   res.linear() = linear.derived();
198   res.translation() = m_coeffs;
199   res.matrix().row(Dim).setZero();
200   res(Dim,Dim) = Scalar(1);
201   return res;
202 }
203 
204 } // end namespace Eigen
205 
206 #endif // EIGEN_TRANSLATION_H
207