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