1
// This file is part of Eigen, a lightweight C++ template library
2
// for linear algebra. Eigen itself is part of the KDE project.
4
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
26
#ifndef EIGEN_TRANSFORM_H
27
#define EIGEN_TRANSFORM_H
29
/** Represents some traits of a transformation */
30
enum TransformTraits {
31
Isometry, ///< the transformation is a concatenation of translations and rotations
32
Affine, ///< the transformation is affine (linear transformation + translation)
33
Projective ///< the transformation might not be affine
36
// Note that we have to pass Dim and HDim because it is not allowed to use a template
37
// parameter to define a template specialization. To be more precise, in the following
38
// specializations, it is not allowed to use Dim+1 instead of HDim.
39
template< typename Other,
42
int OtherRows=Other::RowsAtCompileTime,
43
int OtherCols=Other::ColsAtCompileTime>
44
struct ei_transform_product_impl;
46
/** \geometry_module \ingroup Geometry_Module
50
* \brief Represents an homogeneous transformation in a N dimensional space
52
* \param _Scalar the scalar type, i.e., the type of the coefficients
53
* \param _Dim the dimension of the space
55
* The homography is internally represented and stored as a (Dim+1)^2 matrix which
56
* is available through the matrix() method.
58
* Conversion methods from/to Qt's QMatrix and QTransform are available if the
59
* preprocessor token EIGEN_QT_SUPPORT is defined.
61
* \sa class Matrix, class Quaternion
63
template<typename _Scalar, int _Dim>
67
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim==Dynamic ? Dynamic : (_Dim+1)*(_Dim+1))
69
Dim = _Dim, ///< space dimension in which the transformation holds
70
HDim = _Dim+1 ///< size of a respective homogeneous vector
72
/** the scalar type of the coefficients */
73
typedef _Scalar Scalar;
74
/** type of the matrix used to represent the transformation */
75
typedef Matrix<Scalar,HDim,HDim> MatrixType;
76
/** type of the matrix used to represent the linear part of the transformation */
77
typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
78
/** type of read/write reference to the linear part of the transformation */
79
typedef Block<MatrixType,Dim,Dim> LinearPart;
80
/** type of a vector */
81
typedef Matrix<Scalar,Dim,1> VectorType;
82
/** type of a read/write reference to the translation part of the rotation */
83
typedef Block<MatrixType,Dim,1> TranslationPart;
84
/** corresponding translation type */
85
typedef Translation<Scalar,Dim> TranslationType;
86
/** corresponding scaling transformation type */
87
typedef Scaling<Scalar,Dim> ScalingType;
95
/** Default constructor without initialization of the coefficients. */
96
inline Transform() { }
98
inline Transform(const Transform& other)
100
m_matrix = other.m_matrix;
103
inline explicit Transform(const TranslationType& t) { *this = t; }
104
inline explicit Transform(const ScalingType& s) { *this = s; }
105
template<typename Derived>
106
inline explicit Transform(const RotationBase<Derived, Dim>& r) { *this = r; }
108
inline Transform& operator=(const Transform& other)
109
{ m_matrix = other.m_matrix; return *this; }
111
template<typename OtherDerived, bool BigMatrix> // MSVC 2005 will commit suicide if BigMatrix has a default value
112
struct construct_from_matrix
114
static inline void run(Transform *transform, const MatrixBase<OtherDerived>& other)
116
transform->matrix() = other;
120
template<typename OtherDerived> struct construct_from_matrix<OtherDerived, true>
122
static inline void run(Transform *transform, const MatrixBase<OtherDerived>& other)
124
transform->linear() = other;
125
transform->translation().setZero();
126
transform->matrix()(Dim,Dim) = Scalar(1);
127
transform->matrix().template block<1,Dim>(Dim,0).setZero();
131
/** Constructs and initializes a transformation from a Dim^2 or a (Dim+1)^2 matrix. */
132
template<typename OtherDerived>
133
inline explicit Transform(const MatrixBase<OtherDerived>& other)
135
construct_from_matrix<OtherDerived, int(OtherDerived::RowsAtCompileTime) == Dim>::run(this, other);
138
/** Set \c *this from a (Dim+1)^2 matrix. */
139
template<typename OtherDerived>
140
inline Transform& operator=(const MatrixBase<OtherDerived>& other)
141
{ m_matrix = other; return *this; }
143
#ifdef EIGEN_QT_SUPPORT
144
inline Transform(const QMatrix& other);
145
inline Transform& operator=(const QMatrix& other);
146
inline QMatrix toQMatrix(void) const;
147
inline Transform(const QTransform& other);
148
inline Transform& operator=(const QTransform& other);
149
inline QTransform toQTransform(void) const;
152
/** shortcut for m_matrix(row,col);
153
* \sa MatrixBase::operaror(int,int) const */
154
inline Scalar operator() (int row, int col) const { return m_matrix(row,col); }
155
/** shortcut for m_matrix(row,col);
156
* \sa MatrixBase::operaror(int,int) */
157
inline Scalar& operator() (int row, int col) { return m_matrix(row,col); }
159
/** \returns a read-only expression of the transformation matrix */
160
inline const MatrixType& matrix() const { return m_matrix; }
161
/** \returns a writable expression of the transformation matrix */
162
inline MatrixType& matrix() { return m_matrix; }
164
/** \returns a read-only expression of the linear (linear) part of the transformation */
165
inline const LinearPart linear() const { return m_matrix.template block<Dim,Dim>(0,0); }
166
/** \returns a writable expression of the linear (linear) part of the transformation */
167
inline LinearPart linear() { return m_matrix.template block<Dim,Dim>(0,0); }
169
/** \returns a read-only expression of the translation vector of the transformation */
170
inline const TranslationPart translation() const { return m_matrix.template block<Dim,1>(0,Dim); }
171
/** \returns a writable expression of the translation vector of the transformation */
172
inline TranslationPart translation() { return m_matrix.template block<Dim,1>(0,Dim); }
174
/** \returns an expression of the product between the transform \c *this and a matrix expression \a other
176
* The right hand side \a other might be either:
177
* \li a vector of size Dim,
178
* \li an homogeneous vector of size Dim+1,
179
* \li a transformation matrix of size Dim+1 x Dim+1.
181
// note: this function is defined here because some compilers cannot find the respective declaration
182
template<typename OtherDerived>
183
inline const typename ei_transform_product_impl<OtherDerived,_Dim,_Dim+1>::ResultType
184
operator * (const MatrixBase<OtherDerived> &other) const
185
{ return ei_transform_product_impl<OtherDerived,Dim,HDim>::run(*this,other.derived()); }
187
/** \returns the product expression of a transformation matrix \a a times a transform \a b
188
* The transformation matrix \a a must have a Dim+1 x Dim+1 sizes. */
189
template<typename OtherDerived>
190
friend inline const typename ProductReturnType<OtherDerived,MatrixType>::Type
191
operator * (const MatrixBase<OtherDerived> &a, const Transform &b)
192
{ return a.derived() * b.matrix(); }
194
/** Contatenates two transformations */
195
inline const Transform
196
operator * (const Transform& other) const
197
{ return Transform(m_matrix * other.matrix()); }
199
/** \sa MatrixBase::setIdentity() */
200
void setIdentity() { m_matrix.setIdentity(); }
201
static const typename MatrixType::IdentityReturnType Identity()
203
return MatrixType::Identity();
206
template<typename OtherDerived>
207
inline Transform& scale(const MatrixBase<OtherDerived> &other);
209
template<typename OtherDerived>
210
inline Transform& prescale(const MatrixBase<OtherDerived> &other);
212
inline Transform& scale(Scalar s);
213
inline Transform& prescale(Scalar s);
215
template<typename OtherDerived>
216
inline Transform& translate(const MatrixBase<OtherDerived> &other);
218
template<typename OtherDerived>
219
inline Transform& pretranslate(const MatrixBase<OtherDerived> &other);
221
template<typename RotationType>
222
inline Transform& rotate(const RotationType& rotation);
224
template<typename RotationType>
225
inline Transform& prerotate(const RotationType& rotation);
227
Transform& shear(Scalar sx, Scalar sy);
228
Transform& preshear(Scalar sx, Scalar sy);
230
inline Transform& operator=(const TranslationType& t);
231
inline Transform& operator*=(const TranslationType& t) { return translate(t.vector()); }
232
inline Transform operator*(const TranslationType& t) const;
234
inline Transform& operator=(const ScalingType& t);
235
inline Transform& operator*=(const ScalingType& s) { return scale(s.coeffs()); }
236
inline Transform operator*(const ScalingType& s) const;
237
friend inline Transform operator*(const LinearMatrixType& mat, const Transform& t)
240
res.matrix().row(Dim) = t.matrix().row(Dim);
241
res.matrix().template block<Dim,HDim>(0,0) = (mat * t.matrix().template block<Dim,HDim>(0,0)).lazy();
245
template<typename Derived>
246
inline Transform& operator=(const RotationBase<Derived,Dim>& r);
247
template<typename Derived>
248
inline Transform& operator*=(const RotationBase<Derived,Dim>& r) { return rotate(r.toRotationMatrix()); }
249
template<typename Derived>
250
inline Transform operator*(const RotationBase<Derived,Dim>& r) const;
252
LinearMatrixType rotation() const;
253
template<typename RotationMatrixType, typename ScalingMatrixType>
254
void computeRotationScaling(RotationMatrixType *rotation, ScalingMatrixType *scaling) const;
255
template<typename ScalingMatrixType, typename RotationMatrixType>
256
void computeScalingRotation(ScalingMatrixType *scaling, RotationMatrixType *rotation) const;
258
template<typename PositionDerived, typename OrientationType, typename ScaleDerived>
259
Transform& fromPositionOrientationScale(const MatrixBase<PositionDerived> &position,
260
const OrientationType& orientation, const MatrixBase<ScaleDerived> &scale);
262
inline const MatrixType inverse(TransformTraits traits = Affine) const;
264
/** \returns a const pointer to the column major internal matrix */
265
const Scalar* data() const { return m_matrix.data(); }
266
/** \returns a non-const pointer to the column major internal matrix */
267
Scalar* data() { return m_matrix.data(); }
269
/** \returns \c *this with scalar type casted to \a NewScalarType
271
* Note that if \a NewScalarType is equal to the current scalar type of \c *this
272
* then this function smartly returns a const reference to \c *this.
274
template<typename NewScalarType>
275
inline typename ei_cast_return_type<Transform,Transform<NewScalarType,Dim> >::type cast() const
276
{ return typename ei_cast_return_type<Transform,Transform<NewScalarType,Dim> >::type(*this); }
278
/** Copy constructor with scalar type conversion */
279
template<typename OtherScalarType>
280
inline explicit Transform(const Transform<OtherScalarType,Dim>& other)
281
{ m_matrix = other.matrix().template cast<Scalar>(); }
283
/** \returns \c true if \c *this is approximately equal to \a other, within the precision
284
* determined by \a prec.
286
* \sa MatrixBase::isApprox() */
287
bool isApprox(const Transform& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
288
{ return m_matrix.isApprox(other.m_matrix, prec); }
290
#ifdef EIGEN_TRANSFORM_PLUGIN
291
#include EIGEN_TRANSFORM_PLUGIN
298
/** \ingroup Geometry_Module */
299
typedef Transform<float,2> Transform2f;
300
/** \ingroup Geometry_Module */
301
typedef Transform<float,3> Transform3f;
302
/** \ingroup Geometry_Module */
303
typedef Transform<double,2> Transform2d;
304
/** \ingroup Geometry_Module */
305
typedef Transform<double,3> Transform3d;
307
/**************************
308
*** Optional QT support ***
309
**************************/
311
#ifdef EIGEN_QT_SUPPORT
312
/** Initialises \c *this from a QMatrix assuming the dimension is 2.
314
* This function is available only if the token EIGEN_QT_SUPPORT is defined.
316
template<typename Scalar, int Dim>
317
Transform<Scalar,Dim>::Transform(const QMatrix& other)
322
/** Set \c *this from a QMatrix assuming the dimension is 2.
324
* This function is available only if the token EIGEN_QT_SUPPORT is defined.
326
template<typename Scalar, int Dim>
327
Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const QMatrix& other)
329
EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
330
m_matrix << other.m11(), other.m21(), other.dx(),
331
other.m12(), other.m22(), other.dy(),
336
/** \returns a QMatrix from \c *this assuming the dimension is 2.
338
* \warning this convertion might loss data if \c *this is not affine
340
* This function is available only if the token EIGEN_QT_SUPPORT is defined.
342
template<typename Scalar, int Dim>
343
QMatrix Transform<Scalar,Dim>::toQMatrix(void) const
345
EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
346
return QMatrix(m_matrix.coeff(0,0), m_matrix.coeff(1,0),
347
m_matrix.coeff(0,1), m_matrix.coeff(1,1),
348
m_matrix.coeff(0,2), m_matrix.coeff(1,2));
351
/** Initialises \c *this from a QTransform assuming the dimension is 2.
353
* This function is available only if the token EIGEN_QT_SUPPORT is defined.
355
template<typename Scalar, int Dim>
356
Transform<Scalar,Dim>::Transform(const QTransform& other)
361
/** Set \c *this from a QTransform assuming the dimension is 2.
363
* This function is available only if the token EIGEN_QT_SUPPORT is defined.
365
template<typename Scalar, int Dim>
366
Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const QTransform& other)
368
EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
369
m_matrix << other.m11(), other.m21(), other.dx(),
370
other.m12(), other.m22(), other.dy(),
371
other.m13(), other.m23(), other.m33();
375
/** \returns a QTransform from \c *this assuming the dimension is 2.
377
* This function is available only if the token EIGEN_QT_SUPPORT is defined.
379
template<typename Scalar, int Dim>
380
QTransform Transform<Scalar,Dim>::toQTransform(void) const
382
EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
383
return QTransform(m_matrix.coeff(0,0), m_matrix.coeff(1,0), m_matrix.coeff(2,0),
384
m_matrix.coeff(0,1), m_matrix.coeff(1,1), m_matrix.coeff(2,1),
385
m_matrix.coeff(0,2), m_matrix.coeff(1,2), m_matrix.coeff(2,2));
389
/*********************
390
*** Procedural API ***
391
*********************/
393
/** Applies on the right the non uniform scale transformation represented
394
* by the vector \a other to \c *this and returns a reference to \c *this.
397
template<typename Scalar, int Dim>
398
template<typename OtherDerived>
399
Transform<Scalar,Dim>&
400
Transform<Scalar,Dim>::scale(const MatrixBase<OtherDerived> &other)
402
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
403
linear() = (linear() * other.asDiagonal()).lazy();
407
/** Applies on the right a uniform scale of a factor \a c to \c *this
408
* and returns a reference to \c *this.
409
* \sa prescale(Scalar)
411
template<typename Scalar, int Dim>
412
inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::scale(Scalar s)
418
/** Applies on the left the non uniform scale transformation represented
419
* by the vector \a other to \c *this and returns a reference to \c *this.
422
template<typename Scalar, int Dim>
423
template<typename OtherDerived>
424
Transform<Scalar,Dim>&
425
Transform<Scalar,Dim>::prescale(const MatrixBase<OtherDerived> &other)
427
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
428
m_matrix.template block<Dim,HDim>(0,0) = (other.asDiagonal() * m_matrix.template block<Dim,HDim>(0,0)).lazy();
432
/** Applies on the left a uniform scale of a factor \a c to \c *this
433
* and returns a reference to \c *this.
436
template<typename Scalar, int Dim>
437
inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::prescale(Scalar s)
439
m_matrix.template corner<Dim,HDim>(TopLeft) *= s;
443
/** Applies on the right the translation matrix represented by the vector \a other
444
* to \c *this and returns a reference to \c *this.
447
template<typename Scalar, int Dim>
448
template<typename OtherDerived>
449
Transform<Scalar,Dim>&
450
Transform<Scalar,Dim>::translate(const MatrixBase<OtherDerived> &other)
452
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
453
translation() += linear() * other;
457
/** Applies on the left the translation matrix represented by the vector \a other
458
* to \c *this and returns a reference to \c *this.
461
template<typename Scalar, int Dim>
462
template<typename OtherDerived>
463
Transform<Scalar,Dim>&
464
Transform<Scalar,Dim>::pretranslate(const MatrixBase<OtherDerived> &other)
466
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
467
translation() += other;
471
/** Applies on the right the rotation represented by the rotation \a rotation
472
* to \c *this and returns a reference to \c *this.
474
* The template parameter \a RotationType is the type of the rotation which
475
* must be known by ei_toRotationMatrix<>.
477
* Natively supported types includes:
479
* - a Dim x Dim matrix expression,
480
* - a Quaternion (3D),
483
* This mechanism is easily extendable to support user types such as Euler angles,
484
* or a pair of Quaternion for 4D rotations.
486
* \sa rotate(Scalar), class Quaternion, class AngleAxis, prerotate(RotationType)
488
template<typename Scalar, int Dim>
489
template<typename RotationType>
490
Transform<Scalar,Dim>&
491
Transform<Scalar,Dim>::rotate(const RotationType& rotation)
493
linear() *= ei_toRotationMatrix<Scalar,Dim>(rotation);
497
/** Applies on the left the rotation represented by the rotation \a rotation
498
* to \c *this and returns a reference to \c *this.
500
* See rotate() for further details.
504
template<typename Scalar, int Dim>
505
template<typename RotationType>
506
Transform<Scalar,Dim>&
507
Transform<Scalar,Dim>::prerotate(const RotationType& rotation)
509
m_matrix.template block<Dim,HDim>(0,0) = ei_toRotationMatrix<Scalar,Dim>(rotation)
510
* m_matrix.template block<Dim,HDim>(0,0);
514
/** Applies on the right the shear transformation represented
515
* by the vector \a other to \c *this and returns a reference to \c *this.
519
template<typename Scalar, int Dim>
520
Transform<Scalar,Dim>&
521
Transform<Scalar,Dim>::shear(Scalar sx, Scalar sy)
523
EIGEN_STATIC_ASSERT(int(Dim)==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
524
VectorType tmp = linear().col(0)*sy + linear().col(1);
525
linear() << linear().col(0) + linear().col(1)*sx, tmp;
529
/** Applies on the left the shear transformation represented
530
* by the vector \a other to \c *this and returns a reference to \c *this.
534
template<typename Scalar, int Dim>
535
Transform<Scalar,Dim>&
536
Transform<Scalar,Dim>::preshear(Scalar sx, Scalar sy)
538
EIGEN_STATIC_ASSERT(int(Dim)==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
539
m_matrix.template block<Dim,HDim>(0,0) = LinearMatrixType(1, sx, sy, 1) * m_matrix.template block<Dim,HDim>(0,0);
543
/******************************************************
544
*** Scaling, Translation and Rotation compatibility ***
545
******************************************************/
547
template<typename Scalar, int Dim>
548
inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const TranslationType& t)
550
linear().setIdentity();
551
translation() = t.vector();
552
m_matrix.template block<1,Dim>(Dim,0).setZero();
553
m_matrix(Dim,Dim) = Scalar(1);
557
template<typename Scalar, int Dim>
558
inline Transform<Scalar,Dim> Transform<Scalar,Dim>::operator*(const TranslationType& t) const
560
Transform res = *this;
561
res.translate(t.vector());
565
template<typename Scalar, int Dim>
566
inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const ScalingType& s)
569
linear().diagonal() = s.coeffs();
570
m_matrix.coeffRef(Dim,Dim) = Scalar(1);
574
template<typename Scalar, int Dim>
575
inline Transform<Scalar,Dim> Transform<Scalar,Dim>::operator*(const ScalingType& s) const
577
Transform res = *this;
578
res.scale(s.coeffs());
582
template<typename Scalar, int Dim>
583
template<typename Derived>
584
inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const RotationBase<Derived,Dim>& r)
586
linear() = ei_toRotationMatrix<Scalar,Dim>(r);
587
translation().setZero();
588
m_matrix.template block<1,Dim>(Dim,0).setZero();
589
m_matrix.coeffRef(Dim,Dim) = Scalar(1);
593
template<typename Scalar, int Dim>
594
template<typename Derived>
595
inline Transform<Scalar,Dim> Transform<Scalar,Dim>::operator*(const RotationBase<Derived,Dim>& r) const
597
Transform res = *this;
598
res.rotate(r.derived());
602
/************************
603
*** Special functions ***
604
************************/
606
/** \returns the rotation part of the transformation
611
* \sa computeRotationScaling(), computeScalingRotation(), class SVD
613
template<typename Scalar, int Dim>
614
typename Transform<Scalar,Dim>::LinearMatrixType
615
Transform<Scalar,Dim>::rotation() const
617
LinearMatrixType result;
618
computeRotationScaling(&result, (LinearMatrixType*)0);
623
/** decomposes the linear part of the transformation as a product rotation x scaling, the scaling being
624
* not necessarily positive.
626
* If either pointer is zero, the corresponding computation is skipped.
632
* \sa computeScalingRotation(), rotation(), class SVD
634
template<typename Scalar, int Dim>
635
template<typename RotationMatrixType, typename ScalingMatrixType>
636
void Transform<Scalar,Dim>::computeRotationScaling(RotationMatrixType *rotation, ScalingMatrixType *scaling) const
638
linear().svd().computeRotationScaling(rotation, scaling);
641
/** decomposes the linear part of the transformation as a product rotation x scaling, the scaling being
642
* not necessarily positive.
644
* If either pointer is zero, the corresponding computation is skipped.
650
* \sa computeRotationScaling(), rotation(), class SVD
652
template<typename Scalar, int Dim>
653
template<typename ScalingMatrixType, typename RotationMatrixType>
654
void Transform<Scalar,Dim>::computeScalingRotation(ScalingMatrixType *scaling, RotationMatrixType *rotation) const
656
linear().svd().computeScalingRotation(scaling, rotation);
659
/** Convenient method to set \c *this from a position, orientation and scale
662
template<typename Scalar, int Dim>
663
template<typename PositionDerived, typename OrientationType, typename ScaleDerived>
664
Transform<Scalar,Dim>&
665
Transform<Scalar,Dim>::fromPositionOrientationScale(const MatrixBase<PositionDerived> &position,
666
const OrientationType& orientation, const MatrixBase<ScaleDerived> &scale)
668
linear() = ei_toRotationMatrix<Scalar,Dim>(orientation);
669
linear() *= scale.asDiagonal();
670
translation() = position;
671
m_matrix.template block<1,Dim>(Dim,0).setZero();
672
m_matrix(Dim,Dim) = Scalar(1);
678
* \returns the inverse transformation matrix according to some given knowledge
681
* \param traits allows to optimize the inversion process when the transformion
682
* is known to be not a general transformation. The possible values are:
683
* - Projective if the transformation is not necessarily affine, i.e., if the
684
* last row is not guaranteed to be [0 ... 0 1]
685
* - Affine is the default, the last row is assumed to be [0 ... 0 1]
686
* - Isometry if the transformation is only a concatenations of translations
689
* \warning unless \a traits is always set to NoShear or NoScaling, this function
690
* requires the generic inverse method of MatrixBase defined in the LU module. If
691
* you forget to include this module, then you will get hard to debug linking errors.
693
* \sa MatrixBase::inverse()
695
template<typename Scalar, int Dim>
696
inline const typename Transform<Scalar,Dim>::MatrixType
697
Transform<Scalar,Dim>::inverse(TransformTraits traits) const
699
if (traits == Projective)
701
return m_matrix.inverse();
706
if (traits == Affine)
708
res.template corner<Dim,Dim>(TopLeft) = linear().inverse();
710
else if (traits == Isometry)
712
res.template corner<Dim,Dim>(TopLeft) = linear().transpose();
716
ei_assert("invalid traits value in Transform::inverse()");
718
// translation and remaining parts
719
res.template corner<Dim,1>(TopRight) = - res.template corner<Dim,Dim>(TopLeft) * translation();
720
res.template corner<1,Dim>(BottomLeft).setZero();
721
res.coeffRef(Dim,Dim) = Scalar(1);
726
/*****************************************************
727
*** Specializations of operator* with a MatrixBase ***
728
*****************************************************/
730
template<typename Other, int Dim, int HDim>
731
struct ei_transform_product_impl<Other,Dim,HDim, HDim,HDim>
733
typedef Transform<typename Other::Scalar,Dim> TransformType;
734
typedef typename TransformType::MatrixType MatrixType;
735
typedef typename ProductReturnType<MatrixType,Other>::Type ResultType;
736
static ResultType run(const TransformType& tr, const Other& other)
737
{ return tr.matrix() * other; }
740
template<typename Other, int Dim, int HDim>
741
struct ei_transform_product_impl<Other,Dim,HDim, Dim,Dim>
743
typedef Transform<typename Other::Scalar,Dim> TransformType;
744
typedef typename TransformType::MatrixType MatrixType;
745
typedef TransformType ResultType;
746
static ResultType run(const TransformType& tr, const Other& other)
749
res.translation() = tr.translation();
750
res.matrix().row(Dim) = tr.matrix().row(Dim);
751
res.linear() = (tr.linear() * other).lazy();
756
template<typename Other, int Dim, int HDim>
757
struct ei_transform_product_impl<Other,Dim,HDim, HDim,1>
759
typedef Transform<typename Other::Scalar,Dim> TransformType;
760
typedef typename TransformType::MatrixType MatrixType;
761
typedef typename ProductReturnType<MatrixType,Other>::Type ResultType;
762
static ResultType run(const TransformType& tr, const Other& other)
763
{ return tr.matrix() * other; }
766
template<typename Other, int Dim, int HDim>
767
struct ei_transform_product_impl<Other,Dim,HDim, Dim,1>
769
typedef typename Other::Scalar Scalar;
770
typedef Transform<Scalar,Dim> TransformType;
771
typedef typename TransformType::LinearPart MatrixType;
772
typedef const CwiseUnaryOp<
773
ei_scalar_multiple_op<Scalar>,
774
NestByValue<CwiseBinaryOp<
775
ei_scalar_sum_op<Scalar>,
776
NestByValue<typename ProductReturnType<NestByValue<MatrixType>,Other>::Type >,
777
NestByValue<typename TransformType::TranslationPart> > >
779
// FIXME should we offer an optimized version when the last row is known to be 0,0...,0,1 ?
780
static ResultType run(const TransformType& tr, const Other& other)
781
{ return ((tr.linear().nestByValue() * other).nestByValue() + tr.translation().nestByValue()).nestByValue()
782
* (Scalar(1) / ( (tr.matrix().template block<1,Dim>(Dim,0) * other).coeff(0) + tr.matrix().coeff(Dim,Dim))); }
785
#endif // EIGEN_TRANSFORM_H