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