Scaling.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_SCALING_H
  10. #define EIGEN_SCALING_H
  11. namespace Eigen {
  12. /** \geometry_module \ingroup Geometry_Module
  13. *
  14. * \class UniformScaling
  15. *
  16. * \brief Represents a generic uniform scaling transformation
  17. *
  18. * \tparam _Scalar the scalar type, i.e., the type of the coefficients.
  19. *
  20. * This class represent a uniform scaling transformation. It is the return
  21. * type of Scaling(Scalar), and most of the time this is the only way it
  22. * is used. In particular, this class is not aimed to be used to store a scaling transformation,
  23. * but rather to make easier the constructions and updates of Transform objects.
  24. *
  25. * To represent an axis aligned scaling, use the DiagonalMatrix class.
  26. *
  27. * \sa Scaling(), class DiagonalMatrix, MatrixBase::asDiagonal(), class Translation, class Transform
  28. */
  29. namespace internal
  30. {
  31. // This helper helps nvcc+MSVC to properly parse this file.
  32. // See bug 1412.
  33. template <typename Scalar, int Dim, int Mode>
  34. struct uniformscaling_times_affine_returntype
  35. {
  36. enum
  37. {
  38. NewMode = int(Mode) == int(Isometry) ? Affine : Mode
  39. };
  40. typedef Transform <Scalar, Dim, NewMode> type;
  41. };
  42. }
  43. template<typename _Scalar>
  44. class UniformScaling
  45. {
  46. public:
  47. /** the scalar type of the coefficients */
  48. typedef _Scalar Scalar;
  49. protected:
  50. Scalar m_factor;
  51. public:
  52. /** Default constructor without initialization. */
  53. UniformScaling() {}
  54. /** Constructs and initialize a uniform scaling transformation */
  55. explicit inline UniformScaling(const Scalar& s) : m_factor(s) {}
  56. inline const Scalar& factor() const { return m_factor; }
  57. inline Scalar& factor() { return m_factor; }
  58. /** Concatenates two uniform scaling */
  59. inline UniformScaling operator* (const UniformScaling& other) const
  60. { return UniformScaling(m_factor * other.factor()); }
  61. /** Concatenates a uniform scaling and a translation */
  62. template<int Dim>
  63. inline Transform<Scalar,Dim,Affine> operator* (const Translation<Scalar,Dim>& t) const;
  64. /** Concatenates a uniform scaling and an affine transformation */
  65. template<int Dim, int Mode, int Options>
  66. inline typename
  67. internal::uniformscaling_times_affine_returntype<Scalar,Dim,Mode>::type
  68. operator* (const Transform<Scalar, Dim, Mode, Options>& t) const
  69. {
  70. typename internal::uniformscaling_times_affine_returntype<Scalar,Dim,Mode>::type res = t;
  71. res.prescale(factor());
  72. return res;
  73. }
  74. /** Concatenates a uniform scaling and a linear transformation matrix */
  75. // TODO returns an expression
  76. template<typename Derived>
  77. inline typename Eigen::internal::plain_matrix_type<Derived>::type operator* (const MatrixBase<Derived>& other) const
  78. { return other * m_factor; }
  79. template<typename Derived,int Dim>
  80. inline Matrix<Scalar,Dim,Dim> operator*(const RotationBase<Derived,Dim>& r) const
  81. { return r.toRotationMatrix() * m_factor; }
  82. /** \returns the inverse scaling */
  83. inline UniformScaling inverse() const
  84. { return UniformScaling(Scalar(1)/m_factor); }
  85. /** \returns \c *this with scalar type casted to \a NewScalarType
  86. *
  87. * Note that if \a NewScalarType is equal to the current scalar type of \c *this
  88. * then this function smartly returns a const reference to \c *this.
  89. */
  90. template<typename NewScalarType>
  91. inline UniformScaling<NewScalarType> cast() const
  92. { return UniformScaling<NewScalarType>(NewScalarType(m_factor)); }
  93. /** Copy constructor with scalar type conversion */
  94. template<typename OtherScalarType>
  95. inline explicit UniformScaling(const UniformScaling<OtherScalarType>& other)
  96. { m_factor = Scalar(other.factor()); }
  97. /** \returns \c true if \c *this is approximately equal to \a other, within the precision
  98. * determined by \a prec.
  99. *
  100. * \sa MatrixBase::isApprox() */
  101. bool isApprox(const UniformScaling& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
  102. { return internal::isApprox(m_factor, other.factor(), prec); }
  103. };
  104. /** \addtogroup Geometry_Module */
  105. //@{
  106. /** Concatenates a linear transformation matrix and a uniform scaling
  107. * \relates UniformScaling
  108. */
  109. // NOTE this operator is defined in MatrixBase and not as a friend function
  110. // of UniformScaling to fix an internal crash of Intel's ICC
  111. template<typename Derived,typename Scalar>
  112. EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Derived,Scalar,product)
  113. operator*(const MatrixBase<Derived>& matrix, const UniformScaling<Scalar>& s)
  114. { return matrix.derived() * s.factor(); }
  115. /** Constructs a uniform scaling from scale factor \a s */
  116. inline UniformScaling<float> Scaling(float s) { return UniformScaling<float>(s); }
  117. /** Constructs a uniform scaling from scale factor \a s */
  118. inline UniformScaling<double> Scaling(double s) { return UniformScaling<double>(s); }
  119. /** Constructs a uniform scaling from scale factor \a s */
  120. template<typename RealScalar>
  121. inline UniformScaling<std::complex<RealScalar> > Scaling(const std::complex<RealScalar>& s)
  122. { return UniformScaling<std::complex<RealScalar> >(s); }
  123. /** Constructs a 2D axis aligned scaling */
  124. template<typename Scalar>
  125. inline DiagonalMatrix<Scalar,2> Scaling(const Scalar& sx, const Scalar& sy)
  126. { return DiagonalMatrix<Scalar,2>(sx, sy); }
  127. /** Constructs a 3D axis aligned scaling */
  128. template<typename Scalar>
  129. inline DiagonalMatrix<Scalar,3> Scaling(const Scalar& sx, const Scalar& sy, const Scalar& sz)
  130. { return DiagonalMatrix<Scalar,3>(sx, sy, sz); }
  131. /** Constructs an axis aligned scaling expression from vector expression \a coeffs
  132. * This is an alias for coeffs.asDiagonal()
  133. */
  134. template<typename Derived>
  135. inline const DiagonalWrapper<const Derived> Scaling(const MatrixBase<Derived>& coeffs)
  136. { return coeffs.asDiagonal(); }
  137. /** \deprecated */
  138. typedef DiagonalMatrix<float, 2> AlignedScaling2f;
  139. /** \deprecated */
  140. typedef DiagonalMatrix<double,2> AlignedScaling2d;
  141. /** \deprecated */
  142. typedef DiagonalMatrix<float, 3> AlignedScaling3f;
  143. /** \deprecated */
  144. typedef DiagonalMatrix<double,3> AlignedScaling3d;
  145. //@}
  146. template<typename Scalar>
  147. template<int Dim>
  148. inline Transform<Scalar,Dim,Affine>
  149. UniformScaling<Scalar>::operator* (const Translation<Scalar,Dim>& t) const
  150. {
  151. Transform<Scalar,Dim,Affine> res;
  152. res.matrix().setZero();
  153. res.linear().diagonal().fill(factor());
  154. res.translation() = factor() * t.vector();
  155. res(Dim,Dim) = Scalar(1);
  156. return res;
  157. }
  158. } // end namespace Eigen
  159. #endif // EIGEN_SCALING_H