Translation.h 7.5 KB

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