Home | History | Annotate | Download | only in Geometry
      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 (at) free.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 // no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
     11 
     12 namespace Eigen {
     13 
     14 /** \geometry_module \ingroup Geometry_Module
     15   *
     16   * \class Scaling
     17   *
     18   * \brief Represents a possibly non uniform scaling transformation
     19   *
     20   * \param _Scalar the scalar type, i.e., the type of the coefficients.
     21   * \param _Dim the  dimension of the space, can be a compile time value or Dynamic
     22   *
     23   * \note This class is not aimed to be used to store a scaling transformation,
     24   * but rather to make easier the constructions and updates of Transform objects.
     25   *
     26   * \sa class Translation, class Transform
     27   */
     28 template<typename _Scalar, int _Dim>
     29 class Scaling
     30 {
     31 public:
     32   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
     33   /** dimension of the space */
     34   enum { Dim = _Dim };
     35   /** the scalar type of the coefficients */
     36   typedef _Scalar Scalar;
     37   /** corresponding vector type */
     38   typedef Matrix<Scalar,Dim,1> VectorType;
     39   /** corresponding linear transformation matrix type */
     40   typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
     41   /** corresponding translation type */
     42   typedef Translation<Scalar,Dim> TranslationType;
     43   /** corresponding affine transformation type */
     44   typedef Transform<Scalar,Dim> TransformType;
     45 
     46 protected:
     47 
     48   VectorType m_coeffs;
     49 
     50 public:
     51 
     52   /** Default constructor without initialization. */
     53   Scaling() {}
     54   /** Constructs and initialize a uniform scaling transformation */
     55   explicit inline Scaling(const Scalar& s) { m_coeffs.setConstant(s); }
     56   /** 2D only */
     57   inline Scaling(const Scalar& sx, const Scalar& sy)
     58   {
     59     ei_assert(Dim==2);
     60     m_coeffs.x() = sx;
     61     m_coeffs.y() = sy;
     62   }
     63   /** 3D only */
     64   inline Scaling(const Scalar& sx, const Scalar& sy, const Scalar& sz)
     65   {
     66     ei_assert(Dim==3);
     67     m_coeffs.x() = sx;
     68     m_coeffs.y() = sy;
     69     m_coeffs.z() = sz;
     70   }
     71   /** Constructs and initialize the scaling transformation from a vector of scaling coefficients */
     72   explicit inline Scaling(const VectorType& coeffs) : m_coeffs(coeffs) {}
     73 
     74   const VectorType& coeffs() const { return m_coeffs; }
     75   VectorType& coeffs() { return m_coeffs; }
     76 
     77   /** Concatenates two scaling */
     78   inline Scaling operator* (const Scaling& other) const
     79   { return Scaling(coeffs().cwise() * other.coeffs()); }
     80 
     81   /** Concatenates a scaling and a translation */
     82   inline TransformType operator* (const TranslationType& t) const;
     83 
     84   /** Concatenates a scaling and an affine transformation */
     85   inline TransformType operator* (const TransformType& t) const;
     86 
     87   /** Concatenates a scaling and a linear transformation matrix */
     88   // TODO returns an expression
     89   inline LinearMatrixType operator* (const LinearMatrixType& other) const
     90   { return coeffs().asDiagonal() * other; }
     91 
     92   /** Concatenates a linear transformation matrix and a scaling */
     93   // TODO returns an expression
     94   friend inline LinearMatrixType operator* (const LinearMatrixType& other, const Scaling& s)
     95   { return other * s.coeffs().asDiagonal(); }
     96 
     97   template<typename Derived>
     98   inline LinearMatrixType operator*(const RotationBase<Derived,Dim>& r) const
     99   { return *this * r.toRotationMatrix(); }
    100 
    101   /** Applies scaling to vector */
    102   inline VectorType operator* (const VectorType& other) const
    103   { return coeffs().asDiagonal() * other; }
    104 
    105   /** \returns the inverse scaling */
    106   inline Scaling inverse() const
    107   { return Scaling(coeffs().cwise().inverse()); }
    108 
    109   inline Scaling& operator=(const Scaling& other)
    110   {
    111     m_coeffs = other.m_coeffs;
    112     return *this;
    113   }
    114 
    115   /** \returns \c *this with scalar type casted to \a NewScalarType
    116     *
    117     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
    118     * then this function smartly returns a const reference to \c *this.
    119     */
    120   template<typename NewScalarType>
    121   inline typename internal::cast_return_type<Scaling,Scaling<NewScalarType,Dim> >::type cast() const
    122   { return typename internal::cast_return_type<Scaling,Scaling<NewScalarType,Dim> >::type(*this); }
    123 
    124   /** Copy constructor with scalar type conversion */
    125   template<typename OtherScalarType>
    126   inline explicit Scaling(const Scaling<OtherScalarType,Dim>& other)
    127   { m_coeffs = other.coeffs().template cast<Scalar>(); }
    128 
    129   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
    130     * determined by \a prec.
    131     *
    132     * \sa MatrixBase::isApprox() */
    133   bool isApprox(const Scaling& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
    134   { return m_coeffs.isApprox(other.m_coeffs, prec); }
    135 
    136 };
    137 
    138 /** \addtogroup Geometry_Module */
    139 //@{
    140 typedef Scaling<float, 2> Scaling2f;
    141 typedef Scaling<double,2> Scaling2d;
    142 typedef Scaling<float, 3> Scaling3f;
    143 typedef Scaling<double,3> Scaling3d;
    144 //@}
    145 
    146 template<typename Scalar, int Dim>
    147 inline typename Scaling<Scalar,Dim>::TransformType
    148 Scaling<Scalar,Dim>::operator* (const TranslationType& t) const
    149 {
    150   TransformType res;
    151   res.matrix().setZero();
    152   res.linear().diagonal() = coeffs();
    153   res.translation() = m_coeffs.cwise() * t.vector();
    154   res(Dim,Dim) = Scalar(1);
    155   return res;
    156 }
    157 
    158 template<typename Scalar, int Dim>
    159 inline typename Scaling<Scalar,Dim>::TransformType
    160 Scaling<Scalar,Dim>::operator* (const TransformType& t) const
    161 {
    162   TransformType res = t;
    163   res.prescale(m_coeffs);
    164   return res;
    165 }
    166 
    167 } // end namespace Eigen
    168