/src/Geometry_Eigen/Eigen/src/Eigen2Support/Geometry/Translation.h

http://github.com/Akranar/daguerreo · C Header · 196 lines · 101 code · 26 blank · 69 comment · 0 complexity · 7918364c7edde9001d10e831262cbf4a MD5 · raw file

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
  5. //
  6. // Eigen is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 3 of the License, or (at your option) any later version.
  10. //
  11. // Alternatively, you can redistribute it and/or
  12. // modify it under the terms of the GNU General Public License as
  13. // published by the Free Software Foundation; either version 2 of
  14. // the License, or (at your option) any later version.
  15. //
  16. // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
  17. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU Lesser General Public
  22. // License and a copy of the GNU General Public License along with
  23. // Eigen. If not, see <http://www.gnu.org/licenses/>.
  24. // no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
  25. /** \geometry_module \ingroup Geometry_Module
  26. *
  27. * \class Translation
  28. *
  29. * \brief Represents a translation transformation
  30. *
  31. * \param _Scalar the scalar type, i.e., the type of the coefficients.
  32. * \param _Dim the dimension of the space, can be a compile time value or Dynamic
  33. *
  34. * \note This class is not aimed to be used to store a translation transformation,
  35. * but rather to make easier the constructions and updates of Transform objects.
  36. *
  37. * \sa class Scaling, class Transform
  38. */
  39. template<typename _Scalar, int _Dim>
  40. class Translation
  41. {
  42. public:
  43. EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
  44. /** dimension of the space */
  45. enum { Dim = _Dim };
  46. /** the scalar type of the coefficients */
  47. typedef _Scalar Scalar;
  48. /** corresponding vector type */
  49. typedef Matrix<Scalar,Dim,1> VectorType;
  50. /** corresponding linear transformation matrix type */
  51. typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
  52. /** corresponding scaling transformation type */
  53. typedef Scaling<Scalar,Dim> ScalingType;
  54. /** corresponding affine transformation type */
  55. typedef Transform<Scalar,Dim> TransformType;
  56. protected:
  57. VectorType m_coeffs;
  58. public:
  59. /** Default constructor without initialization. */
  60. Translation() {}
  61. /** */
  62. inline Translation(const Scalar& sx, const Scalar& sy)
  63. {
  64. ei_assert(Dim==2);
  65. m_coeffs.x() = sx;
  66. m_coeffs.y() = sy;
  67. }
  68. /** */
  69. inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz)
  70. {
  71. ei_assert(Dim==3);
  72. m_coeffs.x() = sx;
  73. m_coeffs.y() = sy;
  74. m_coeffs.z() = sz;
  75. }
  76. /** Constructs and initialize the scaling transformation from a vector of scaling coefficients */
  77. explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {}
  78. const VectorType& vector() const { return m_coeffs; }
  79. VectorType& vector() { return m_coeffs; }
  80. /** Concatenates two translation */
  81. inline Translation operator* (const Translation& other) const
  82. { return Translation(m_coeffs + other.m_coeffs); }
  83. /** Concatenates a translation and a scaling */
  84. inline TransformType operator* (const ScalingType& other) const;
  85. /** Concatenates a translation and a linear transformation */
  86. inline TransformType operator* (const LinearMatrixType& linear) const;
  87. template<typename Derived>
  88. inline TransformType operator*(const RotationBase<Derived,Dim>& r) const
  89. { return *this * r.toRotationMatrix(); }
  90. /** Concatenates a linear transformation and a translation */
  91. // its a nightmare to define a templated friend function outside its declaration
  92. friend inline TransformType operator* (const LinearMatrixType& linear, const Translation& t)
  93. {
  94. TransformType res;
  95. res.matrix().setZero();
  96. res.linear() = linear;
  97. res.translation() = linear * t.m_coeffs;
  98. res.matrix().row(Dim).setZero();
  99. res(Dim,Dim) = Scalar(1);
  100. return res;
  101. }
  102. /** Concatenates a translation and an affine transformation */
  103. inline TransformType operator* (const TransformType& t) const;
  104. /** Applies translation to vector */
  105. inline VectorType operator* (const VectorType& other) const
  106. { return m_coeffs + other; }
  107. /** \returns the inverse translation (opposite) */
  108. Translation inverse() const { return Translation(-m_coeffs); }
  109. Translation& operator=(const Translation& other)
  110. {
  111. m_coeffs = other.m_coeffs;
  112. return *this;
  113. }
  114. /** \returns \c *this with scalar type casted to \a NewScalarType
  115. *
  116. * Note that if \a NewScalarType is equal to the current scalar type of \c *this
  117. * then this function smartly returns a const reference to \c *this.
  118. */
  119. template<typename NewScalarType>
  120. inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
  121. { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
  122. /** Copy constructor with scalar type conversion */
  123. template<typename OtherScalarType>
  124. inline explicit Translation(const Translation<OtherScalarType,Dim>& other)
  125. { m_coeffs = other.vector().template cast<Scalar>(); }
  126. /** \returns \c true if \c *this is approximately equal to \a other, within the precision
  127. * determined by \a prec.
  128. *
  129. * \sa MatrixBase::isApprox() */
  130. bool isApprox(const Translation& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
  131. { return m_coeffs.isApprox(other.m_coeffs, prec); }
  132. };
  133. /** \addtogroup Geometry_Module */
  134. //@{
  135. typedef Translation<float, 2> Translation2f;
  136. typedef Translation<double,2> Translation2d;
  137. typedef Translation<float, 3> Translation3f;
  138. typedef Translation<double,3> Translation3d;
  139. //@}
  140. template<typename Scalar, int Dim>
  141. inline typename Translation<Scalar,Dim>::TransformType
  142. Translation<Scalar,Dim>::operator* (const ScalingType& other) const
  143. {
  144. TransformType res;
  145. res.matrix().setZero();
  146. res.linear().diagonal() = other.coeffs();
  147. res.translation() = m_coeffs;
  148. res(Dim,Dim) = Scalar(1);
  149. return res;
  150. }
  151. template<typename Scalar, int Dim>
  152. inline typename Translation<Scalar,Dim>::TransformType
  153. Translation<Scalar,Dim>::operator* (const LinearMatrixType& linear) const
  154. {
  155. TransformType res;
  156. res.matrix().setZero();
  157. res.linear() = linear;
  158. res.translation() = m_coeffs;
  159. res.matrix().row(Dim).setZero();
  160. res(Dim,Dim) = Scalar(1);
  161. return res;
  162. }
  163. template<typename Scalar, int Dim>
  164. inline typename Translation<Scalar,Dim>::TransformType
  165. Translation<Scalar,Dim>::operator* (const TransformType& t) const
  166. {
  167. TransformType res = t;
  168. res.pretranslate(m_coeffs);
  169. return res;
  170. }