source: pacpussensors/trunk/Vislab/lib3dv/eigen/Eigen/src/Geometry/Transform.h@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 55.6 KB
Line 
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// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6// Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
12#ifndef EIGEN_TRANSFORM_H
13#define EIGEN_TRANSFORM_H
14
15namespace Eigen {
16
17namespace internal {
18
19template<typename Transform>
20struct transform_traits
21{
22 enum
23 {
24 Dim = Transform::Dim,
25 HDim = Transform::HDim,
26 Mode = Transform::Mode,
27 IsProjective = (int(Mode)==int(Projective))
28 };
29};
30
31template< typename TransformType,
32 typename MatrixType,
33 int Case = transform_traits<TransformType>::IsProjective ? 0
34 : int(MatrixType::RowsAtCompileTime) == int(transform_traits<TransformType>::HDim) ? 1
35 : 2>
36struct transform_right_product_impl;
37
38template< typename Other,
39 int Mode,
40 int Options,
41 int Dim,
42 int HDim,
43 int OtherRows=Other::RowsAtCompileTime,
44 int OtherCols=Other::ColsAtCompileTime>
45struct transform_left_product_impl;
46
47template< typename Lhs,
48 typename Rhs,
49 bool AnyProjective =
50 transform_traits<Lhs>::IsProjective ||
51 transform_traits<Rhs>::IsProjective>
52struct transform_transform_product_impl;
53
54template< typename Other,
55 int Mode,
56 int Options,
57 int Dim,
58 int HDim,
59 int OtherRows=Other::RowsAtCompileTime,
60 int OtherCols=Other::ColsAtCompileTime>
61struct transform_construct_from_matrix;
62
63template<typename TransformType> struct transform_take_affine_part;
64
65template<int Mode> struct transform_make_affine;
66
67} // end namespace internal
68
69/** \geometry_module \ingroup Geometry_Module
70 *
71 * \class Transform
72 *
73 * \brief Represents an homogeneous transformation in a N dimensional space
74 *
75 * \tparam _Scalar the scalar type, i.e., the type of the coefficients
76 * \tparam _Dim the dimension of the space
77 * \tparam _Mode the type of the transformation. Can be:
78 * - #Affine: the transformation is stored as a (Dim+1)^2 matrix,
79 * where the last row is assumed to be [0 ... 0 1].
80 * - #AffineCompact: the transformation is stored as a (Dim)x(Dim+1) matrix.
81 * - #Projective: the transformation is stored as a (Dim+1)^2 matrix
82 * without any assumption.
83 * \tparam _Options has the same meaning as in class Matrix. It allows to specify DontAlign and/or RowMajor.
84 * These Options are passed directly to the underlying matrix type.
85 *
86 * The homography is internally represented and stored by a matrix which
87 * is available through the matrix() method. To understand the behavior of
88 * this class you have to think a Transform object as its internal
89 * matrix representation. The chosen convention is right multiply:
90 *
91 * \code v' = T * v \endcode
92 *
93 * Therefore, an affine transformation matrix M is shaped like this:
94 *
95 * \f$ \left( \begin{array}{cc}
96 * linear & translation\\
97 * 0 ... 0 & 1
98 * \end{array} \right) \f$
99 *
100 * Note that for a projective transformation the last row can be anything,
101 * and then the interpretation of different parts might be sightly different.
102 *
103 * However, unlike a plain matrix, the Transform class provides many features
104 * simplifying both its assembly and usage. In particular, it can be composed
105 * with any other transformations (Transform,Translation,RotationBase,DiagonalMatrix)
106 * and can be directly used to transform implicit homogeneous vectors. All these
107 * operations are handled via the operator*. For the composition of transformations,
108 * its principle consists to first convert the right/left hand sides of the product
109 * to a compatible (Dim+1)^2 matrix and then perform a pure matrix product.
110 * Of course, internally, operator* tries to perform the minimal number of operations
111 * according to the nature of each terms. Likewise, when applying the transform
112 * to points, the latters are automatically promoted to homogeneous vectors
113 * before doing the matrix product. The conventions to homogeneous representations
114 * are performed as follow:
115 *
116 * \b Translation t (Dim)x(1):
117 * \f$ \left( \begin{array}{cc}
118 * I & t \\
119 * 0\,...\,0 & 1
120 * \end{array} \right) \f$
121 *
122 * \b Rotation R (Dim)x(Dim):
123 * \f$ \left( \begin{array}{cc}
124 * R & 0\\
125 * 0\,...\,0 & 1
126 * \end{array} \right) \f$
127 *<!--
128 * \b Linear \b Matrix L (Dim)x(Dim):
129 * \f$ \left( \begin{array}{cc}
130 * L & 0\\
131 * 0\,...\,0 & 1
132 * \end{array} \right) \f$
133 *
134 * \b Affine \b Matrix A (Dim)x(Dim+1):
135 * \f$ \left( \begin{array}{c}
136 * A\\
137 * 0\,...\,0\,1
138 * \end{array} \right) \f$
139 *-->
140 * \b Scaling \b DiagonalMatrix S (Dim)x(Dim):
141 * \f$ \left( \begin{array}{cc}
142 * S & 0\\
143 * 0\,...\,0 & 1
144 * \end{array} \right) \f$
145 *
146 * \b Column \b point v (Dim)x(1):
147 * \f$ \left( \begin{array}{c}
148 * v\\
149 * 1
150 * \end{array} \right) \f$
151 *
152 * \b Set \b of \b column \b points V1...Vn (Dim)x(n):
153 * \f$ \left( \begin{array}{ccc}
154 * v_1 & ... & v_n\\
155 * 1 & ... & 1
156 * \end{array} \right) \f$
157 *
158 * The concatenation of a Transform object with any kind of other transformation
159 * always returns a Transform object.
160 *
161 * A little exception to the "as pure matrix product" rule is the case of the
162 * transformation of non homogeneous vectors by an affine transformation. In
163 * that case the last matrix row can be ignored, and the product returns non
164 * homogeneous vectors.
165 *
166 * Since, for instance, a Dim x Dim matrix is interpreted as a linear transformation,
167 * it is not possible to directly transform Dim vectors stored in a Dim x Dim matrix.
168 * The solution is either to use a Dim x Dynamic matrix or explicitly request a
169 * vector transformation by making the vector homogeneous:
170 * \code
171 * m' = T * m.colwise().homogeneous();
172 * \endcode
173 * Note that there is zero overhead.
174 *
175 * Conversion methods from/to Qt's QMatrix and QTransform are available if the
176 * preprocessor token EIGEN_QT_SUPPORT is defined.
177 *
178 * This class can be extended with the help of the plugin mechanism described on the page
179 * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_TRANSFORM_PLUGIN.
180 *
181 * \sa class Matrix, class Quaternion
182 */
183template<typename _Scalar, int _Dim, int _Mode, int _Options>
184class Transform
185{
186public:
187 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim==Dynamic ? Dynamic : (_Dim+1)*(_Dim+1))
188 enum {
189 Mode = _Mode,
190 Options = _Options,
191 Dim = _Dim, ///< space dimension in which the transformation holds
192 HDim = _Dim+1, ///< size of a respective homogeneous vector
193 Rows = int(Mode)==(AffineCompact) ? Dim : HDim
194 };
195 /** the scalar type of the coefficients */
196 typedef _Scalar Scalar;
197 typedef DenseIndex Index;
198 /** type of the matrix used to represent the transformation */
199 typedef typename internal::make_proper_matrix_type<Scalar,Rows,HDim,Options>::type MatrixType;
200 /** constified MatrixType */
201 typedef const MatrixType ConstMatrixType;
202 /** type of the matrix used to represent the linear part of the transformation */
203 typedef Matrix<Scalar,Dim,Dim,Options> LinearMatrixType;
204 /** type of read/write reference to the linear part of the transformation */
205 typedef Block<MatrixType,Dim,Dim,int(Mode)==(AffineCompact) && (Options&RowMajor)==0> LinearPart;
206 /** type of read reference to the linear part of the transformation */
207 typedef const Block<ConstMatrixType,Dim,Dim,int(Mode)==(AffineCompact) && (Options&RowMajor)==0> ConstLinearPart;
208 /** type of read/write reference to the affine part of the transformation */
209 typedef typename internal::conditional<int(Mode)==int(AffineCompact),
210 MatrixType&,
211 Block<MatrixType,Dim,HDim> >::type AffinePart;
212 /** type of read reference to the affine part of the transformation */
213 typedef typename internal::conditional<int(Mode)==int(AffineCompact),
214 const MatrixType&,
215 const Block<const MatrixType,Dim,HDim> >::type ConstAffinePart;
216 /** type of a vector */
217 typedef Matrix<Scalar,Dim,1> VectorType;
218 /** type of a read/write reference to the translation part of the rotation */
219 typedef Block<MatrixType,Dim,1,int(Mode)==(AffineCompact)> TranslationPart;
220 /** type of a read reference to the translation part of the rotation */
221 typedef const Block<ConstMatrixType,Dim,1,int(Mode)==(AffineCompact)> ConstTranslationPart;
222 /** corresponding translation type */
223 typedef Translation<Scalar,Dim> TranslationType;
224
225 // this intermediate enum is needed to avoid an ICE with gcc 3.4 and 4.0
226 enum { TransformTimeDiagonalMode = ((Mode==int(Isometry))?Affine:int(Mode)) };
227 /** The return type of the product between a diagonal matrix and a transform */
228 typedef Transform<Scalar,Dim,TransformTimeDiagonalMode> TransformTimeDiagonalReturnType;
229
230protected:
231
232 MatrixType m_matrix;
233
234public:
235
236 /** Default constructor without initialization of the meaningful coefficients.
237 * If Mode==Affine, then the last row is set to [0 ... 0 1] */
238 inline Transform()
239 {
240 check_template_params();
241 internal::transform_make_affine<(int(Mode)==Affine) ? Affine : AffineCompact>::run(m_matrix);
242 }
243
244 inline Transform(const Transform& other)
245 {
246 check_template_params();
247 m_matrix = other.m_matrix;
248 }
249
250 inline explicit Transform(const TranslationType& t)
251 {
252 check_template_params();
253 *this = t;
254 }
255 inline explicit Transform(const UniformScaling<Scalar>& s)
256 {
257 check_template_params();
258 *this = s;
259 }
260 template<typename Derived>
261 inline explicit Transform(const RotationBase<Derived, Dim>& r)
262 {
263 check_template_params();
264 *this = r;
265 }
266
267 inline Transform& operator=(const Transform& other)
268 { m_matrix = other.m_matrix; return *this; }
269
270 typedef internal::transform_take_affine_part<Transform> take_affine_part;
271
272 /** Constructs and initializes a transformation from a Dim^2 or a (Dim+1)^2 matrix. */
273 template<typename OtherDerived>
274 inline explicit Transform(const EigenBase<OtherDerived>& other)
275 {
276 EIGEN_STATIC_ASSERT((internal::is_same<Scalar,typename OtherDerived::Scalar>::value),
277 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY);
278
279 check_template_params();
280 internal::transform_construct_from_matrix<OtherDerived,Mode,Options,Dim,HDim>::run(this, other.derived());
281 }
282
283 /** Set \c *this from a Dim^2 or (Dim+1)^2 matrix. */
284 template<typename OtherDerived>
285 inline Transform& operator=(const EigenBase<OtherDerived>& other)
286 {
287 EIGEN_STATIC_ASSERT((internal::is_same<Scalar,typename OtherDerived::Scalar>::value),
288 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY);
289
290 internal::transform_construct_from_matrix<OtherDerived,Mode,Options,Dim,HDim>::run(this, other.derived());
291 return *this;
292 }
293
294 template<int OtherOptions>
295 inline Transform(const Transform<Scalar,Dim,Mode,OtherOptions>& other)
296 {
297 check_template_params();
298 // only the options change, we can directly copy the matrices
299 m_matrix = other.matrix();
300 }
301
302 template<int OtherMode,int OtherOptions>
303 inline Transform(const Transform<Scalar,Dim,OtherMode,OtherOptions>& other)
304 {
305 check_template_params();
306 // prevent conversions as:
307 // Affine | AffineCompact | Isometry = Projective
308 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(OtherMode==int(Projective), Mode==int(Projective)),
309 YOU_PERFORMED_AN_INVALID_TRANSFORMATION_CONVERSION)
310
311 // prevent conversions as:
312 // Isometry = Affine | AffineCompact
313 EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(OtherMode==int(Affine)||OtherMode==int(AffineCompact), Mode!=int(Isometry)),
314 YOU_PERFORMED_AN_INVALID_TRANSFORMATION_CONVERSION)
315
316 enum { ModeIsAffineCompact = Mode == int(AffineCompact),
317 OtherModeIsAffineCompact = OtherMode == int(AffineCompact)
318 };
319
320 if(ModeIsAffineCompact == OtherModeIsAffineCompact)
321 {
322 // We need the block expression because the code is compiled for all
323 // combinations of transformations and will trigger a compile time error
324 // if one tries to assign the matrices directly
325 m_matrix.template block<Dim,Dim+1>(0,0) = other.matrix().template block<Dim,Dim+1>(0,0);
326 makeAffine();
327 }
328 else if(OtherModeIsAffineCompact)
329 {
330 typedef typename Transform<Scalar,Dim,OtherMode,OtherOptions>::MatrixType OtherMatrixType;
331 internal::transform_construct_from_matrix<OtherMatrixType,Mode,Options,Dim,HDim>::run(this, other.matrix());
332 }
333 else
334 {
335 // here we know that Mode == AffineCompact and OtherMode != AffineCompact.
336 // if OtherMode were Projective, the static assert above would already have caught it.
337 // So the only possibility is that OtherMode == Affine
338 linear() = other.linear();
339 translation() = other.translation();
340 }
341 }
342
343 template<typename OtherDerived>
344 Transform(const ReturnByValue<OtherDerived>& other)
345 {
346 check_template_params();
347 other.evalTo(*this);
348 }
349
350 template<typename OtherDerived>
351 Transform& operator=(const ReturnByValue<OtherDerived>& other)
352 {
353 other.evalTo(*this);
354 return *this;
355 }
356
357 #ifdef EIGEN_QT_SUPPORT
358 inline Transform(const QMatrix& other);
359 inline Transform& operator=(const QMatrix& other);
360 inline QMatrix toQMatrix(void) const;
361 inline Transform(const QTransform& other);
362 inline Transform& operator=(const QTransform& other);
363 inline QTransform toQTransform(void) const;
364 #endif
365
366 /** shortcut for m_matrix(row,col);
367 * \sa MatrixBase::operator(Index,Index) const */
368 inline Scalar operator() (Index row, Index col) const { return m_matrix(row,col); }
369 /** shortcut for m_matrix(row,col);
370 * \sa MatrixBase::operator(Index,Index) */
371 inline Scalar& operator() (Index row, Index col) { return m_matrix(row,col); }
372
373 /** \returns a read-only expression of the transformation matrix */
374 inline const MatrixType& matrix() const { return m_matrix; }
375 /** \returns a writable expression of the transformation matrix */
376 inline MatrixType& matrix() { return m_matrix; }
377
378 /** \returns a read-only expression of the linear part of the transformation */
379 inline ConstLinearPart linear() const { return ConstLinearPart(m_matrix,0,0); }
380 /** \returns a writable expression of the linear part of the transformation */
381 inline LinearPart linear() { return LinearPart(m_matrix,0,0); }
382
383 /** \returns a read-only expression of the Dim x HDim affine part of the transformation */
384 inline ConstAffinePart affine() const { return take_affine_part::run(m_matrix); }
385 /** \returns a writable expression of the Dim x HDim affine part of the transformation */
386 inline AffinePart affine() { return take_affine_part::run(m_matrix); }
387
388 /** \returns a read-only expression of the translation vector of the transformation */
389 inline ConstTranslationPart translation() const { return ConstTranslationPart(m_matrix,0,Dim); }
390 /** \returns a writable expression of the translation vector of the transformation */
391 inline TranslationPart translation() { return TranslationPart(m_matrix,0,Dim); }
392
393 /** \returns an expression of the product between the transform \c *this and a matrix expression \a other.
394 *
395 * The right-hand-side \a other can be either:
396 * \li an homogeneous vector of size Dim+1,
397 * \li a set of homogeneous vectors of size Dim+1 x N,
398 * \li a transformation matrix of size Dim+1 x Dim+1.
399 *
400 * Moreover, if \c *this represents an affine transformation (i.e., Mode!=Projective), then \a other can also be:
401 * \li a point of size Dim (computes: \code this->linear() * other + this->translation()\endcode),
402 * \li a set of N points as a Dim x N matrix (computes: \code (this->linear() * other).colwise() + this->translation()\endcode),
403 *
404 * In all cases, the return type is a matrix or vector of same sizes as the right-hand-side \a other.
405 *
406 * If you want to interpret \a other as a linear or affine transformation, then first convert it to a Transform<> type,
407 * or do your own cooking.
408 *
409 * Finally, if you want to apply Affine transformations to vectors, then explicitly apply the linear part only:
410 * \code
411 * Affine3f A;
412 * Vector3f v1, v2;
413 * v2 = A.linear() * v1;
414 * \endcode
415 *
416 */
417 // note: this function is defined here because some compilers cannot find the respective declaration
418 template<typename OtherDerived>
419 EIGEN_STRONG_INLINE const typename OtherDerived::PlainObject
420 operator * (const EigenBase<OtherDerived> &other) const
421 { return internal::transform_right_product_impl<Transform, OtherDerived>::run(*this,other.derived()); }
422
423 /** \returns the product expression of a transformation matrix \a a times a transform \a b
424 *
425 * The left hand side \a other can be either:
426 * \li a linear transformation matrix of size Dim x Dim,
427 * \li an affine transformation matrix of size Dim x Dim+1,
428 * \li a general transformation matrix of size Dim+1 x Dim+1.
429 */
430 template<typename OtherDerived> friend
431 inline const typename internal::transform_left_product_impl<OtherDerived,Mode,Options,_Dim,_Dim+1>::ResultType
432 operator * (const EigenBase<OtherDerived> &a, const Transform &b)
433 { return internal::transform_left_product_impl<OtherDerived,Mode,Options,Dim,HDim>::run(a.derived(),b); }
434
435 /** \returns The product expression of a transform \a a times a diagonal matrix \a b
436 *
437 * The rhs diagonal matrix is interpreted as an affine scaling transformation. The
438 * product results in a Transform of the same type (mode) as the lhs only if the lhs
439 * mode is no isometry. In that case, the returned transform is an affinity.
440 */
441 template<typename DiagonalDerived>
442 inline const TransformTimeDiagonalReturnType
443 operator * (const DiagonalBase<DiagonalDerived> &b) const
444 {
445 TransformTimeDiagonalReturnType res(*this);
446 res.linear() *= b;
447 return res;
448 }
449
450 /** \returns The product expression of a diagonal matrix \a a times a transform \a b
451 *
452 * The lhs diagonal matrix is interpreted as an affine scaling transformation. The
453 * product results in a Transform of the same type (mode) as the lhs only if the lhs
454 * mode is no isometry. In that case, the returned transform is an affinity.
455 */
456 template<typename DiagonalDerived>
457 friend inline TransformTimeDiagonalReturnType
458 operator * (const DiagonalBase<DiagonalDerived> &a, const Transform &b)
459 {
460 TransformTimeDiagonalReturnType res;
461 res.linear().noalias() = a*b.linear();
462 res.translation().noalias() = a*b.translation();
463 if (Mode!=int(AffineCompact))
464 res.matrix().row(Dim) = b.matrix().row(Dim);
465 return res;
466 }
467
468 template<typename OtherDerived>
469 inline Transform& operator*=(const EigenBase<OtherDerived>& other) { return *this = *this * other; }
470
471 /** Concatenates two transformations */
472 inline const Transform operator * (const Transform& other) const
473 {
474 return internal::transform_transform_product_impl<Transform,Transform>::run(*this,other);
475 }
476
477 #ifdef __INTEL_COMPILER
478private:
479 // this intermediate structure permits to workaround a bug in ICC 11:
480 // error: template instantiation resulted in unexpected function type of "Eigen::Transform<double, 3, 32, 0>
481 // (const Eigen::Transform<double, 3, 2, 0> &) const"
482 // (the meaning of a name may have changed since the template declaration -- the type of the template is:
483 // "Eigen::internal::transform_transform_product_impl<Eigen::Transform<double, 3, 32, 0>,
484 // Eigen::Transform<double, 3, Mode, Options>, <expression>>::ResultType (const Eigen::Transform<double, 3, Mode, Options> &) const")
485 //
486 template<int OtherMode,int OtherOptions> struct icc_11_workaround
487 {
488 typedef internal::transform_transform_product_impl<Transform,Transform<Scalar,Dim,OtherMode,OtherOptions> > ProductType;
489 typedef typename ProductType::ResultType ResultType;
490 };
491
492public:
493 /** Concatenates two different transformations */
494 template<int OtherMode,int OtherOptions>
495 inline typename icc_11_workaround<OtherMode,OtherOptions>::ResultType
496 operator * (const Transform<Scalar,Dim,OtherMode,OtherOptions>& other) const
497 {
498 typedef typename icc_11_workaround<OtherMode,OtherOptions>::ProductType ProductType;
499 return ProductType::run(*this,other);
500 }
501 #else
502 /** Concatenates two different transformations */
503 template<int OtherMode,int OtherOptions>
504 inline typename internal::transform_transform_product_impl<Transform,Transform<Scalar,Dim,OtherMode,OtherOptions> >::ResultType
505 operator * (const Transform<Scalar,Dim,OtherMode,OtherOptions>& other) const
506 {
507 return internal::transform_transform_product_impl<Transform,Transform<Scalar,Dim,OtherMode,OtherOptions> >::run(*this,other);
508 }
509 #endif
510
511 /** \sa MatrixBase::setIdentity() */
512 void setIdentity() { m_matrix.setIdentity(); }
513
514 /**
515 * \brief Returns an identity transformation.
516 * \todo In the future this function should be returning a Transform expression.
517 */
518 static const Transform Identity()
519 {
520 return Transform(MatrixType::Identity());
521 }
522
523 template<typename OtherDerived>
524 inline Transform& scale(const MatrixBase<OtherDerived> &other);
525
526 template<typename OtherDerived>
527 inline Transform& prescale(const MatrixBase<OtherDerived> &other);
528
529 inline Transform& scale(const Scalar& s);
530 inline Transform& prescale(const Scalar& s);
531
532 template<typename OtherDerived>
533 inline Transform& translate(const MatrixBase<OtherDerived> &other);
534
535 template<typename OtherDerived>
536 inline Transform& pretranslate(const MatrixBase<OtherDerived> &other);
537
538 template<typename RotationType>
539 inline Transform& rotate(const RotationType& rotation);
540
541 template<typename RotationType>
542 inline Transform& prerotate(const RotationType& rotation);
543
544 Transform& shear(const Scalar& sx, const Scalar& sy);
545 Transform& preshear(const Scalar& sx, const Scalar& sy);
546
547 inline Transform& operator=(const TranslationType& t);
548 inline Transform& operator*=(const TranslationType& t) { return translate(t.vector()); }
549 inline Transform operator*(const TranslationType& t) const;
550
551 inline Transform& operator=(const UniformScaling<Scalar>& t);
552 inline Transform& operator*=(const UniformScaling<Scalar>& s) { return scale(s.factor()); }
553 inline Transform<Scalar,Dim,(int(Mode)==int(Isometry)?int(Affine):int(Mode))> operator*(const UniformScaling<Scalar>& s) const
554 {
555 Transform<Scalar,Dim,(int(Mode)==int(Isometry)?int(Affine):int(Mode)),Options> res = *this;
556 res.scale(s.factor());
557 return res;
558 }
559
560 inline Transform& operator*=(const DiagonalMatrix<Scalar,Dim>& s) { linear() *= s; return *this; }
561
562 template<typename Derived>
563 inline Transform& operator=(const RotationBase<Derived,Dim>& r);
564 template<typename Derived>
565 inline Transform& operator*=(const RotationBase<Derived,Dim>& r) { return rotate(r.toRotationMatrix()); }
566 template<typename Derived>
567 inline Transform operator*(const RotationBase<Derived,Dim>& r) const;
568
569 const LinearMatrixType rotation() const;
570 template<typename RotationMatrixType, typename ScalingMatrixType>
571 void computeRotationScaling(RotationMatrixType *rotation, ScalingMatrixType *scaling) const;
572 template<typename ScalingMatrixType, typename RotationMatrixType>
573 void computeScalingRotation(ScalingMatrixType *scaling, RotationMatrixType *rotation) const;
574
575 template<typename PositionDerived, typename OrientationType, typename ScaleDerived>
576 Transform& fromPositionOrientationScale(const MatrixBase<PositionDerived> &position,
577 const OrientationType& orientation, const MatrixBase<ScaleDerived> &scale);
578
579 inline Transform inverse(TransformTraits traits = (TransformTraits)Mode) const;
580
581 /** \returns a const pointer to the column major internal matrix */
582 const Scalar* data() const { return m_matrix.data(); }
583 /** \returns a non-const pointer to the column major internal matrix */
584 Scalar* data() { return m_matrix.data(); }
585
586 /** \returns \c *this with scalar type casted to \a NewScalarType
587 *
588 * Note that if \a NewScalarType is equal to the current scalar type of \c *this
589 * then this function smartly returns a const reference to \c *this.
590 */
591 template<typename NewScalarType>
592 inline typename internal::cast_return_type<Transform,Transform<NewScalarType,Dim,Mode,Options> >::type cast() const
593 { return typename internal::cast_return_type<Transform,Transform<NewScalarType,Dim,Mode,Options> >::type(*this); }
594
595 /** Copy constructor with scalar type conversion */
596 template<typename OtherScalarType>
597 inline explicit Transform(const Transform<OtherScalarType,Dim,Mode,Options>& other)
598 {
599 check_template_params();
600 m_matrix = other.matrix().template cast<Scalar>();
601 }
602
603 /** \returns \c true if \c *this is approximately equal to \a other, within the precision
604 * determined by \a prec.
605 *
606 * \sa MatrixBase::isApprox() */
607 bool isApprox(const Transform& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
608 { return m_matrix.isApprox(other.m_matrix, prec); }
609
610 /** Sets the last row to [0 ... 0 1]
611 */
612 void makeAffine()
613 {
614 internal::transform_make_affine<int(Mode)>::run(m_matrix);
615 }
616
617 /** \internal
618 * \returns the Dim x Dim linear part if the transformation is affine,
619 * and the HDim x Dim part for projective transformations.
620 */
621 inline Block<MatrixType,int(Mode)==int(Projective)?HDim:Dim,Dim> linearExt()
622 { return m_matrix.template block<int(Mode)==int(Projective)?HDim:Dim,Dim>(0,0); }
623 /** \internal
624 * \returns the Dim x Dim linear part if the transformation is affine,
625 * and the HDim x Dim part for projective transformations.
626 */
627 inline const Block<MatrixType,int(Mode)==int(Projective)?HDim:Dim,Dim> linearExt() const
628 { return m_matrix.template block<int(Mode)==int(Projective)?HDim:Dim,Dim>(0,0); }
629
630 /** \internal
631 * \returns the translation part if the transformation is affine,
632 * and the last column for projective transformations.
633 */
634 inline Block<MatrixType,int(Mode)==int(Projective)?HDim:Dim,1> translationExt()
635 { return m_matrix.template block<int(Mode)==int(Projective)?HDim:Dim,1>(0,Dim); }
636 /** \internal
637 * \returns the translation part if the transformation is affine,
638 * and the last column for projective transformations.
639 */
640 inline const Block<MatrixType,int(Mode)==int(Projective)?HDim:Dim,1> translationExt() const
641 { return m_matrix.template block<int(Mode)==int(Projective)?HDim:Dim,1>(0,Dim); }
642
643
644 #ifdef EIGEN_TRANSFORM_PLUGIN
645 #include EIGEN_TRANSFORM_PLUGIN
646 #endif
647
648protected:
649 #ifndef EIGEN_PARSED_BY_DOXYGEN
650 static EIGEN_STRONG_INLINE void check_template_params()
651 {
652 EIGEN_STATIC_ASSERT((Options & (DontAlign|RowMajor)) == Options, INVALID_MATRIX_TEMPLATE_PARAMETERS)
653 }
654 #endif
655
656};
657
658/** \ingroup Geometry_Module */
659typedef Transform<float,2,Isometry> Isometry2f;
660/** \ingroup Geometry_Module */
661typedef Transform<float,3,Isometry> Isometry3f;
662/** \ingroup Geometry_Module */
663typedef Transform<double,2,Isometry> Isometry2d;
664/** \ingroup Geometry_Module */
665typedef Transform<double,3,Isometry> Isometry3d;
666
667/** \ingroup Geometry_Module */
668typedef Transform<float,2,Affine> Affine2f;
669/** \ingroup Geometry_Module */
670typedef Transform<float,3,Affine> Affine3f;
671/** \ingroup Geometry_Module */
672typedef Transform<double,2,Affine> Affine2d;
673/** \ingroup Geometry_Module */
674typedef Transform<double,3,Affine> Affine3d;
675
676/** \ingroup Geometry_Module */
677typedef Transform<float,2,AffineCompact> AffineCompact2f;
678/** \ingroup Geometry_Module */
679typedef Transform<float,3,AffineCompact> AffineCompact3f;
680/** \ingroup Geometry_Module */
681typedef Transform<double,2,AffineCompact> AffineCompact2d;
682/** \ingroup Geometry_Module */
683typedef Transform<double,3,AffineCompact> AffineCompact3d;
684
685/** \ingroup Geometry_Module */
686typedef Transform<float,2,Projective> Projective2f;
687/** \ingroup Geometry_Module */
688typedef Transform<float,3,Projective> Projective3f;
689/** \ingroup Geometry_Module */
690typedef Transform<double,2,Projective> Projective2d;
691/** \ingroup Geometry_Module */
692typedef Transform<double,3,Projective> Projective3d;
693
694/**************************
695*** Optional QT support ***
696**************************/
697
698#ifdef EIGEN_QT_SUPPORT
699/** Initializes \c *this from a QMatrix assuming the dimension is 2.
700 *
701 * This function is available only if the token EIGEN_QT_SUPPORT is defined.
702 */
703template<typename Scalar, int Dim, int Mode,int Options>
704Transform<Scalar,Dim,Mode,Options>::Transform(const QMatrix& other)
705{
706 check_template_params();
707 *this = other;
708}
709
710/** Set \c *this from a QMatrix assuming the dimension is 2.
711 *
712 * This function is available only if the token EIGEN_QT_SUPPORT is defined.
713 */
714template<typename Scalar, int Dim, int Mode,int Options>
715Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::operator=(const QMatrix& other)
716{
717 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
718 m_matrix << other.m11(), other.m21(), other.dx(),
719 other.m12(), other.m22(), other.dy(),
720 0, 0, 1;
721 return *this;
722}
723
724/** \returns a QMatrix from \c *this assuming the dimension is 2.
725 *
726 * \warning this conversion might loss data if \c *this is not affine
727 *
728 * This function is available only if the token EIGEN_QT_SUPPORT is defined.
729 */
730template<typename Scalar, int Dim, int Mode, int Options>
731QMatrix Transform<Scalar,Dim,Mode,Options>::toQMatrix(void) const
732{
733 check_template_params();
734 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
735 return QMatrix(m_matrix.coeff(0,0), m_matrix.coeff(1,0),
736 m_matrix.coeff(0,1), m_matrix.coeff(1,1),
737 m_matrix.coeff(0,2), m_matrix.coeff(1,2));
738}
739
740/** Initializes \c *this from a QTransform assuming the dimension is 2.
741 *
742 * This function is available only if the token EIGEN_QT_SUPPORT is defined.
743 */
744template<typename Scalar, int Dim, int Mode,int Options>
745Transform<Scalar,Dim,Mode,Options>::Transform(const QTransform& other)
746{
747 check_template_params();
748 *this = other;
749}
750
751/** Set \c *this from a QTransform assuming the dimension is 2.
752 *
753 * This function is available only if the token EIGEN_QT_SUPPORT is defined.
754 */
755template<typename Scalar, int Dim, int Mode, int Options>
756Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::operator=(const QTransform& other)
757{
758 check_template_params();
759 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
760 if (Mode == int(AffineCompact))
761 m_matrix << other.m11(), other.m21(), other.dx(),
762 other.m12(), other.m22(), other.dy();
763 else
764 m_matrix << other.m11(), other.m21(), other.dx(),
765 other.m12(), other.m22(), other.dy(),
766 other.m13(), other.m23(), other.m33();
767 return *this;
768}
769
770/** \returns a QTransform from \c *this assuming the dimension is 2.
771 *
772 * This function is available only if the token EIGEN_QT_SUPPORT is defined.
773 */
774template<typename Scalar, int Dim, int Mode, int Options>
775QTransform Transform<Scalar,Dim,Mode,Options>::toQTransform(void) const
776{
777 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
778 if (Mode == int(AffineCompact))
779 return QTransform(m_matrix.coeff(0,0), m_matrix.coeff(1,0),
780 m_matrix.coeff(0,1), m_matrix.coeff(1,1),
781 m_matrix.coeff(0,2), m_matrix.coeff(1,2));
782 else
783 return QTransform(m_matrix.coeff(0,0), m_matrix.coeff(1,0), m_matrix.coeff(2,0),
784 m_matrix.coeff(0,1), m_matrix.coeff(1,1), m_matrix.coeff(2,1),
785 m_matrix.coeff(0,2), m_matrix.coeff(1,2), m_matrix.coeff(2,2));
786}
787#endif
788
789/*********************
790*** Procedural API ***
791*********************/
792
793/** Applies on the right the non uniform scale transformation represented
794 * by the vector \a other to \c *this and returns a reference to \c *this.
795 * \sa prescale()
796 */
797template<typename Scalar, int Dim, int Mode, int Options>
798template<typename OtherDerived>
799Transform<Scalar,Dim,Mode,Options>&
800Transform<Scalar,Dim,Mode,Options>::scale(const MatrixBase<OtherDerived> &other)
801{
802 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
803 EIGEN_STATIC_ASSERT(Mode!=int(Isometry), THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS)
804 linearExt().noalias() = (linearExt() * other.asDiagonal());
805 return *this;
806}
807
808/** Applies on the right a uniform scale of a factor \a c to \c *this
809 * and returns a reference to \c *this.
810 * \sa prescale(Scalar)
811 */
812template<typename Scalar, int Dim, int Mode, int Options>
813inline Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::scale(const Scalar& s)
814{
815 EIGEN_STATIC_ASSERT(Mode!=int(Isometry), THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS)
816 linearExt() *= s;
817 return *this;
818}
819
820/** Applies on the left the non uniform scale transformation represented
821 * by the vector \a other to \c *this and returns a reference to \c *this.
822 * \sa scale()
823 */
824template<typename Scalar, int Dim, int Mode, int Options>
825template<typename OtherDerived>
826Transform<Scalar,Dim,Mode,Options>&
827Transform<Scalar,Dim,Mode,Options>::prescale(const MatrixBase<OtherDerived> &other)
828{
829 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
830 EIGEN_STATIC_ASSERT(Mode!=int(Isometry), THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS)
831 m_matrix.template block<Dim,HDim>(0,0).noalias() = (other.asDiagonal() * m_matrix.template block<Dim,HDim>(0,0));
832 return *this;
833}
834
835/** Applies on the left a uniform scale of a factor \a c to \c *this
836 * and returns a reference to \c *this.
837 * \sa scale(Scalar)
838 */
839template<typename Scalar, int Dim, int Mode, int Options>
840inline Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::prescale(const Scalar& s)
841{
842 EIGEN_STATIC_ASSERT(Mode!=int(Isometry), THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS)
843 m_matrix.template topRows<Dim>() *= s;
844 return *this;
845}
846
847/** Applies on the right the translation matrix represented by the vector \a other
848 * to \c *this and returns a reference to \c *this.
849 * \sa pretranslate()
850 */
851template<typename Scalar, int Dim, int Mode, int Options>
852template<typename OtherDerived>
853Transform<Scalar,Dim,Mode,Options>&
854Transform<Scalar,Dim,Mode,Options>::translate(const MatrixBase<OtherDerived> &other)
855{
856 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
857 translationExt() += linearExt() * other;
858 return *this;
859}
860
861/** Applies on the left the translation matrix represented by the vector \a other
862 * to \c *this and returns a reference to \c *this.
863 * \sa translate()
864 */
865template<typename Scalar, int Dim, int Mode, int Options>
866template<typename OtherDerived>
867Transform<Scalar,Dim,Mode,Options>&
868Transform<Scalar,Dim,Mode,Options>::pretranslate(const MatrixBase<OtherDerived> &other)
869{
870 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
871 if(int(Mode)==int(Projective))
872 affine() += other * m_matrix.row(Dim);
873 else
874 translation() += other;
875 return *this;
876}
877
878/** Applies on the right the rotation represented by the rotation \a rotation
879 * to \c *this and returns a reference to \c *this.
880 *
881 * The template parameter \a RotationType is the type of the rotation which
882 * must be known by internal::toRotationMatrix<>.
883 *
884 * Natively supported types includes:
885 * - any scalar (2D),
886 * - a Dim x Dim matrix expression,
887 * - a Quaternion (3D),
888 * - a AngleAxis (3D)
889 *
890 * This mechanism is easily extendable to support user types such as Euler angles,
891 * or a pair of Quaternion for 4D rotations.
892 *
893 * \sa rotate(Scalar), class Quaternion, class AngleAxis, prerotate(RotationType)
894 */
895template<typename Scalar, int Dim, int Mode, int Options>
896template<typename RotationType>
897Transform<Scalar,Dim,Mode,Options>&
898Transform<Scalar,Dim,Mode,Options>::rotate(const RotationType& rotation)
899{
900 linearExt() *= internal::toRotationMatrix<Scalar,Dim>(rotation);
901 return *this;
902}
903
904/** Applies on the left the rotation represented by the rotation \a rotation
905 * to \c *this and returns a reference to \c *this.
906 *
907 * See rotate() for further details.
908 *
909 * \sa rotate()
910 */
911template<typename Scalar, int Dim, int Mode, int Options>
912template<typename RotationType>
913Transform<Scalar,Dim,Mode,Options>&
914Transform<Scalar,Dim,Mode,Options>::prerotate(const RotationType& rotation)
915{
916 m_matrix.template block<Dim,HDim>(0,0) = internal::toRotationMatrix<Scalar,Dim>(rotation)
917 * m_matrix.template block<Dim,HDim>(0,0);
918 return *this;
919}
920
921/** Applies on the right the shear transformation represented
922 * by the vector \a other to \c *this and returns a reference to \c *this.
923 * \warning 2D only.
924 * \sa preshear()
925 */
926template<typename Scalar, int Dim, int Mode, int Options>
927Transform<Scalar,Dim,Mode,Options>&
928Transform<Scalar,Dim,Mode,Options>::shear(const Scalar& sx, const Scalar& sy)
929{
930 EIGEN_STATIC_ASSERT(int(Dim)==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
931 EIGEN_STATIC_ASSERT(Mode!=int(Isometry), THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS)
932 VectorType tmp = linear().col(0)*sy + linear().col(1);
933 linear() << linear().col(0) + linear().col(1)*sx, tmp;
934 return *this;
935}
936
937/** Applies on the left the shear transformation represented
938 * by the vector \a other to \c *this and returns a reference to \c *this.
939 * \warning 2D only.
940 * \sa shear()
941 */
942template<typename Scalar, int Dim, int Mode, int Options>
943Transform<Scalar,Dim,Mode,Options>&
944Transform<Scalar,Dim,Mode,Options>::preshear(const Scalar& sx, const Scalar& sy)
945{
946 EIGEN_STATIC_ASSERT(int(Dim)==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
947 EIGEN_STATIC_ASSERT(Mode!=int(Isometry), THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS)
948 m_matrix.template block<Dim,HDim>(0,0) = LinearMatrixType(1, sx, sy, 1) * m_matrix.template block<Dim,HDim>(0,0);
949 return *this;
950}
951
952/******************************************************
953*** Scaling, Translation and Rotation compatibility ***
954******************************************************/
955
956template<typename Scalar, int Dim, int Mode, int Options>
957inline Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::operator=(const TranslationType& t)
958{
959 linear().setIdentity();
960 translation() = t.vector();
961 makeAffine();
962 return *this;
963}
964
965template<typename Scalar, int Dim, int Mode, int Options>
966inline Transform<Scalar,Dim,Mode,Options> Transform<Scalar,Dim,Mode,Options>::operator*(const TranslationType& t) const
967{
968 Transform res = *this;
969 res.translate(t.vector());
970 return res;
971}
972
973template<typename Scalar, int Dim, int Mode, int Options>
974inline Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::operator=(const UniformScaling<Scalar>& s)
975{
976 m_matrix.setZero();
977 linear().diagonal().fill(s.factor());
978 makeAffine();
979 return *this;
980}
981
982template<typename Scalar, int Dim, int Mode, int Options>
983template<typename Derived>
984inline Transform<Scalar,Dim,Mode,Options>& Transform<Scalar,Dim,Mode,Options>::operator=(const RotationBase<Derived,Dim>& r)
985{
986 linear() = internal::toRotationMatrix<Scalar,Dim>(r);
987 translation().setZero();
988 makeAffine();
989 return *this;
990}
991
992template<typename Scalar, int Dim, int Mode, int Options>
993template<typename Derived>
994inline Transform<Scalar,Dim,Mode,Options> Transform<Scalar,Dim,Mode,Options>::operator*(const RotationBase<Derived,Dim>& r) const
995{
996 Transform res = *this;
997 res.rotate(r.derived());
998 return res;
999}
1000
1001/************************
1002*** Special functions ***
1003************************/
1004
1005/** \returns the rotation part of the transformation
1006 *
1007 *
1008 * \svd_module
1009 *
1010 * \sa computeRotationScaling(), computeScalingRotation(), class SVD
1011 */
1012template<typename Scalar, int Dim, int Mode, int Options>
1013const typename Transform<Scalar,Dim,Mode,Options>::LinearMatrixType
1014Transform<Scalar,Dim,Mode,Options>::rotation() const
1015{
1016 LinearMatrixType result;
1017 computeRotationScaling(&result, (LinearMatrixType*)0);
1018 return result;
1019}
1020
1021
1022/** decomposes the linear part of the transformation as a product rotation x scaling, the scaling being
1023 * not necessarily positive.
1024 *
1025 * If either pointer is zero, the corresponding computation is skipped.
1026 *
1027 *
1028 *
1029 * \svd_module
1030 *
1031 * \sa computeScalingRotation(), rotation(), class SVD
1032 */
1033template<typename Scalar, int Dim, int Mode, int Options>
1034template<typename RotationMatrixType, typename ScalingMatrixType>
1035void Transform<Scalar,Dim,Mode,Options>::computeRotationScaling(RotationMatrixType *rotation, ScalingMatrixType *scaling) const
1036{
1037 JacobiSVD<LinearMatrixType> svd(linear(), ComputeFullU | ComputeFullV);
1038
1039 Scalar x = (svd.matrixU() * svd.matrixV().adjoint()).determinant(); // so x has absolute value 1
1040 VectorType sv(svd.singularValues());
1041 sv.coeffRef(0) *= x;
1042 if(scaling) scaling->lazyAssign(svd.matrixV() * sv.asDiagonal() * svd.matrixV().adjoint());
1043 if(rotation)
1044 {
1045 LinearMatrixType m(svd.matrixU());
1046 m.col(0) /= x;
1047 rotation->lazyAssign(m * svd.matrixV().adjoint());
1048 }
1049}
1050
1051/** decomposes the linear part of the transformation as a product rotation x scaling, the scaling being
1052 * not necessarily positive.
1053 *
1054 * If either pointer is zero, the corresponding computation is skipped.
1055 *
1056 *
1057 *
1058 * \svd_module
1059 *
1060 * \sa computeRotationScaling(), rotation(), class SVD
1061 */
1062template<typename Scalar, int Dim, int Mode, int Options>
1063template<typename ScalingMatrixType, typename RotationMatrixType>
1064void Transform<Scalar,Dim,Mode,Options>::computeScalingRotation(ScalingMatrixType *scaling, RotationMatrixType *rotation) const
1065{
1066 JacobiSVD<LinearMatrixType> svd(linear(), ComputeFullU | ComputeFullV);
1067
1068 Scalar x = (svd.matrixU() * svd.matrixV().adjoint()).determinant(); // so x has absolute value 1
1069 VectorType sv(svd.singularValues());
1070 sv.coeffRef(0) *= x;
1071 if(scaling) scaling->lazyAssign(svd.matrixU() * sv.asDiagonal() * svd.matrixU().adjoint());
1072 if(rotation)
1073 {
1074 LinearMatrixType m(svd.matrixU());
1075 m.col(0) /= x;
1076 rotation->lazyAssign(m * svd.matrixV().adjoint());
1077 }
1078}
1079
1080/** Convenient method to set \c *this from a position, orientation and scale
1081 * of a 3D object.
1082 */
1083template<typename Scalar, int Dim, int Mode, int Options>
1084template<typename PositionDerived, typename OrientationType, typename ScaleDerived>
1085Transform<Scalar,Dim,Mode,Options>&
1086Transform<Scalar,Dim,Mode,Options>::fromPositionOrientationScale(const MatrixBase<PositionDerived> &position,
1087 const OrientationType& orientation, const MatrixBase<ScaleDerived> &scale)
1088{
1089 linear() = internal::toRotationMatrix<Scalar,Dim>(orientation);
1090 linear() *= scale.asDiagonal();
1091 translation() = position;
1092 makeAffine();
1093 return *this;
1094}
1095
1096namespace internal {
1097
1098template<int Mode>
1099struct transform_make_affine
1100{
1101 template<typename MatrixType>
1102 static void run(MatrixType &mat)
1103 {
1104 static const int Dim = MatrixType::ColsAtCompileTime-1;
1105 mat.template block<1,Dim>(Dim,0).setZero();
1106 mat.coeffRef(Dim,Dim) = typename MatrixType::Scalar(1);
1107 }
1108};
1109
1110template<>
1111struct transform_make_affine<AffineCompact>
1112{
1113 template<typename MatrixType> static void run(MatrixType &) { }
1114};
1115
1116// selector needed to avoid taking the inverse of a 3x4 matrix
1117template<typename TransformType, int Mode=TransformType::Mode>
1118struct projective_transform_inverse
1119{
1120 static inline void run(const TransformType&, TransformType&)
1121 {}
1122};
1123
1124template<typename TransformType>
1125struct projective_transform_inverse<TransformType, Projective>
1126{
1127 static inline void run(const TransformType& m, TransformType& res)
1128 {
1129 res.matrix() = m.matrix().inverse();
1130 }
1131};
1132
1133} // end namespace internal
1134
1135
1136/**
1137 *
1138 * \returns the inverse transformation according to some given knowledge
1139 * on \c *this.
1140 *
1141 * \param hint allows to optimize the inversion process when the transformation
1142 * is known to be not a general transformation (optional). The possible values are:
1143 * - #Projective if the transformation is not necessarily affine, i.e., if the
1144 * last row is not guaranteed to be [0 ... 0 1]
1145 * - #Affine if the last row can be assumed to be [0 ... 0 1]
1146 * - #Isometry if the transformation is only a concatenations of translations
1147 * and rotations.
1148 * The default is the template class parameter \c Mode.
1149 *
1150 * \warning unless \a traits is always set to NoShear or NoScaling, this function
1151 * requires the generic inverse method of MatrixBase defined in the LU module. If
1152 * you forget to include this module, then you will get hard to debug linking errors.
1153 *
1154 * \sa MatrixBase::inverse()
1155 */
1156template<typename Scalar, int Dim, int Mode, int Options>
1157Transform<Scalar,Dim,Mode,Options>
1158Transform<Scalar,Dim,Mode,Options>::inverse(TransformTraits hint) const
1159{
1160 Transform res;
1161 if (hint == Projective)
1162 {
1163 internal::projective_transform_inverse<Transform>::run(*this, res);
1164 }
1165 else
1166 {
1167 if (hint == Isometry)
1168 {
1169 res.matrix().template topLeftCorner<Dim,Dim>() = linear().transpose();
1170 }
1171 else if(hint&Affine)
1172 {
1173 res.matrix().template topLeftCorner<Dim,Dim>() = linear().inverse();
1174 }
1175 else
1176 {
1177 eigen_assert(false && "Invalid transform traits in Transform::Inverse");
1178 }
1179 // translation and remaining parts
1180 res.matrix().template topRightCorner<Dim,1>()
1181 = - res.matrix().template topLeftCorner<Dim,Dim>() * translation();
1182 res.makeAffine(); // we do need this, because in the beginning res is uninitialized
1183 }
1184 return res;
1185}
1186
1187namespace internal {
1188
1189/*****************************************************
1190*** Specializations of take affine part ***
1191*****************************************************/
1192
1193template<typename TransformType> struct transform_take_affine_part {
1194 typedef typename TransformType::MatrixType MatrixType;
1195 typedef typename TransformType::AffinePart AffinePart;
1196 typedef typename TransformType::ConstAffinePart ConstAffinePart;
1197 static inline AffinePart run(MatrixType& m)
1198 { return m.template block<TransformType::Dim,TransformType::HDim>(0,0); }
1199 static inline ConstAffinePart run(const MatrixType& m)
1200 { return m.template block<TransformType::Dim,TransformType::HDim>(0,0); }
1201};
1202
1203template<typename Scalar, int Dim, int Options>
1204struct transform_take_affine_part<Transform<Scalar,Dim,AffineCompact, Options> > {
1205 typedef typename Transform<Scalar,Dim,AffineCompact,Options>::MatrixType MatrixType;
1206 static inline MatrixType& run(MatrixType& m) { return m; }
1207 static inline const MatrixType& run(const MatrixType& m) { return m; }
1208};
1209
1210/*****************************************************
1211*** Specializations of construct from matrix ***
1212*****************************************************/
1213
1214template<typename Other, int Mode, int Options, int Dim, int HDim>
1215struct transform_construct_from_matrix<Other, Mode,Options,Dim,HDim, Dim,Dim>
1216{
1217 static inline void run(Transform<typename Other::Scalar,Dim,Mode,Options> *transform, const Other& other)
1218 {
1219 transform->linear() = other;
1220 transform->translation().setZero();
1221 transform->makeAffine();
1222 }
1223};
1224
1225template<typename Other, int Mode, int Options, int Dim, int HDim>
1226struct transform_construct_from_matrix<Other, Mode,Options,Dim,HDim, Dim,HDim>
1227{
1228 static inline void run(Transform<typename Other::Scalar,Dim,Mode,Options> *transform, const Other& other)
1229 {
1230 transform->affine() = other;
1231 transform->makeAffine();
1232 }
1233};
1234
1235template<typename Other, int Mode, int Options, int Dim, int HDim>
1236struct transform_construct_from_matrix<Other, Mode,Options,Dim,HDim, HDim,HDim>
1237{
1238 static inline void run(Transform<typename Other::Scalar,Dim,Mode,Options> *transform, const Other& other)
1239 { transform->matrix() = other; }
1240};
1241
1242template<typename Other, int Options, int Dim, int HDim>
1243struct transform_construct_from_matrix<Other, AffineCompact,Options,Dim,HDim, HDim,HDim>
1244{
1245 static inline void run(Transform<typename Other::Scalar,Dim,AffineCompact,Options> *transform, const Other& other)
1246 { transform->matrix() = other.template block<Dim,HDim>(0,0); }
1247};
1248
1249/**********************************************************
1250*** Specializations of operator* with rhs EigenBase ***
1251**********************************************************/
1252
1253template<int LhsMode,int RhsMode>
1254struct transform_product_result
1255{
1256 enum
1257 {
1258 Mode =
1259 (LhsMode == (int)Projective || RhsMode == (int)Projective ) ? Projective :
1260 (LhsMode == (int)Affine || RhsMode == (int)Affine ) ? Affine :
1261 (LhsMode == (int)AffineCompact || RhsMode == (int)AffineCompact ) ? AffineCompact :
1262 (LhsMode == (int)Isometry || RhsMode == (int)Isometry ) ? Isometry : Projective
1263 };
1264};
1265
1266template< typename TransformType, typename MatrixType >
1267struct transform_right_product_impl< TransformType, MatrixType, 0 >
1268{
1269 typedef typename MatrixType::PlainObject ResultType;
1270
1271 static EIGEN_STRONG_INLINE ResultType run(const TransformType& T, const MatrixType& other)
1272 {
1273 return T.matrix() * other;
1274 }
1275};
1276
1277template< typename TransformType, typename MatrixType >
1278struct transform_right_product_impl< TransformType, MatrixType, 1 >
1279{
1280 enum {
1281 Dim = TransformType::Dim,
1282 HDim = TransformType::HDim,
1283 OtherRows = MatrixType::RowsAtCompileTime,
1284 OtherCols = MatrixType::ColsAtCompileTime
1285 };
1286
1287 typedef typename MatrixType::PlainObject ResultType;
1288
1289 static EIGEN_STRONG_INLINE ResultType run(const TransformType& T, const MatrixType& other)
1290 {
1291 EIGEN_STATIC_ASSERT(OtherRows==HDim, YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES);
1292
1293 typedef Block<ResultType, Dim, OtherCols, int(MatrixType::RowsAtCompileTime)==Dim> TopLeftLhs;
1294
1295 ResultType res(other.rows(),other.cols());
1296 TopLeftLhs(res, 0, 0, Dim, other.cols()).noalias() = T.affine() * other;
1297 res.row(OtherRows-1) = other.row(OtherRows-1);
1298
1299 return res;
1300 }
1301};
1302
1303template< typename TransformType, typename MatrixType >
1304struct transform_right_product_impl< TransformType, MatrixType, 2 >
1305{
1306 enum {
1307 Dim = TransformType::Dim,
1308 HDim = TransformType::HDim,
1309 OtherRows = MatrixType::RowsAtCompileTime,
1310 OtherCols = MatrixType::ColsAtCompileTime
1311 };
1312
1313 typedef typename MatrixType::PlainObject ResultType;
1314
1315 static EIGEN_STRONG_INLINE ResultType run(const TransformType& T, const MatrixType& other)
1316 {
1317 EIGEN_STATIC_ASSERT(OtherRows==Dim, YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES);
1318
1319 typedef Block<ResultType, Dim, OtherCols, true> TopLeftLhs;
1320 ResultType res(Replicate<typename TransformType::ConstTranslationPart, 1, OtherCols>(T.translation(),1,other.cols()));
1321 TopLeftLhs(res, 0, 0, Dim, other.cols()).noalias() += T.linear() * other;
1322
1323 return res;
1324 }
1325};
1326
1327/**********************************************************
1328*** Specializations of operator* with lhs EigenBase ***
1329**********************************************************/
1330
1331// generic HDim x HDim matrix * T => Projective
1332template<typename Other,int Mode, int Options, int Dim, int HDim>
1333struct transform_left_product_impl<Other,Mode,Options,Dim,HDim, HDim,HDim>
1334{
1335 typedef Transform<typename Other::Scalar,Dim,Mode,Options> TransformType;
1336 typedef typename TransformType::MatrixType MatrixType;
1337 typedef Transform<typename Other::Scalar,Dim,Projective,Options> ResultType;
1338 static ResultType run(const Other& other,const TransformType& tr)
1339 { return ResultType(other * tr.matrix()); }
1340};
1341
1342// generic HDim x HDim matrix * AffineCompact => Projective
1343template<typename Other, int Options, int Dim, int HDim>
1344struct transform_left_product_impl<Other,AffineCompact,Options,Dim,HDim, HDim,HDim>
1345{
1346 typedef Transform<typename Other::Scalar,Dim,AffineCompact,Options> TransformType;
1347 typedef typename TransformType::MatrixType MatrixType;
1348 typedef Transform<typename Other::Scalar,Dim,Projective,Options> ResultType;
1349 static ResultType run(const Other& other,const TransformType& tr)
1350 {
1351 ResultType res;
1352 res.matrix().noalias() = other.template block<HDim,Dim>(0,0) * tr.matrix();
1353 res.matrix().col(Dim) += other.col(Dim);
1354 return res;
1355 }
1356};
1357
1358// affine matrix * T
1359template<typename Other,int Mode, int Options, int Dim, int HDim>
1360struct transform_left_product_impl<Other,Mode,Options,Dim,HDim, Dim,HDim>
1361{
1362 typedef Transform<typename Other::Scalar,Dim,Mode,Options> TransformType;
1363 typedef typename TransformType::MatrixType MatrixType;
1364 typedef TransformType ResultType;
1365 static ResultType run(const Other& other,const TransformType& tr)
1366 {
1367 ResultType res;
1368 res.affine().noalias() = other * tr.matrix();
1369 res.matrix().row(Dim) = tr.matrix().row(Dim);
1370 return res;
1371 }
1372};
1373
1374// affine matrix * AffineCompact
1375template<typename Other, int Options, int Dim, int HDim>
1376struct transform_left_product_impl<Other,AffineCompact,Options,Dim,HDim, Dim,HDim>
1377{
1378 typedef Transform<typename Other::Scalar,Dim,AffineCompact,Options> TransformType;
1379 typedef typename TransformType::MatrixType MatrixType;
1380 typedef TransformType ResultType;
1381 static ResultType run(const Other& other,const TransformType& tr)
1382 {
1383 ResultType res;
1384 res.matrix().noalias() = other.template block<Dim,Dim>(0,0) * tr.matrix();
1385 res.translation() += other.col(Dim);
1386 return res;
1387 }
1388};
1389
1390// linear matrix * T
1391template<typename Other,int Mode, int Options, int Dim, int HDim>
1392struct transform_left_product_impl<Other,Mode,Options,Dim,HDim, Dim,Dim>
1393{
1394 typedef Transform<typename Other::Scalar,Dim,Mode,Options> TransformType;
1395 typedef typename TransformType::MatrixType MatrixType;
1396 typedef TransformType ResultType;
1397 static ResultType run(const Other& other, const TransformType& tr)
1398 {
1399 TransformType res;
1400 if(Mode!=int(AffineCompact))
1401 res.matrix().row(Dim) = tr.matrix().row(Dim);
1402 res.matrix().template topRows<Dim>().noalias()
1403 = other * tr.matrix().template topRows<Dim>();
1404 return res;
1405 }
1406};
1407
1408/**********************************************************
1409*** Specializations of operator* with another Transform ***
1410**********************************************************/
1411
1412template<typename Scalar, int Dim, int LhsMode, int LhsOptions, int RhsMode, int RhsOptions>
1413struct transform_transform_product_impl<Transform<Scalar,Dim,LhsMode,LhsOptions>,Transform<Scalar,Dim,RhsMode,RhsOptions>,false >
1414{
1415 enum { ResultMode = transform_product_result<LhsMode,RhsMode>::Mode };
1416 typedef Transform<Scalar,Dim,LhsMode,LhsOptions> Lhs;
1417 typedef Transform<Scalar,Dim,RhsMode,RhsOptions> Rhs;
1418 typedef Transform<Scalar,Dim,ResultMode,LhsOptions> ResultType;
1419 static ResultType run(const Lhs& lhs, const Rhs& rhs)
1420 {
1421 ResultType res;
1422 res.linear() = lhs.linear() * rhs.linear();
1423 res.translation() = lhs.linear() * rhs.translation() + lhs.translation();
1424 res.makeAffine();
1425 return res;
1426 }
1427};
1428
1429template<typename Scalar, int Dim, int LhsMode, int LhsOptions, int RhsMode, int RhsOptions>
1430struct transform_transform_product_impl<Transform<Scalar,Dim,LhsMode,LhsOptions>,Transform<Scalar,Dim,RhsMode,RhsOptions>,true >
1431{
1432 typedef Transform<Scalar,Dim,LhsMode,LhsOptions> Lhs;
1433 typedef Transform<Scalar,Dim,RhsMode,RhsOptions> Rhs;
1434 typedef Transform<Scalar,Dim,Projective> ResultType;
1435 static ResultType run(const Lhs& lhs, const Rhs& rhs)
1436 {
1437 return ResultType( lhs.matrix() * rhs.matrix() );
1438 }
1439};
1440
1441template<typename Scalar, int Dim, int LhsOptions, int RhsOptions>
1442struct transform_transform_product_impl<Transform<Scalar,Dim,AffineCompact,LhsOptions>,Transform<Scalar,Dim,Projective,RhsOptions>,true >
1443{
1444 typedef Transform<Scalar,Dim,AffineCompact,LhsOptions> Lhs;
1445 typedef Transform<Scalar,Dim,Projective,RhsOptions> Rhs;
1446 typedef Transform<Scalar,Dim,Projective> ResultType;
1447 static ResultType run(const Lhs& lhs, const Rhs& rhs)
1448 {
1449 ResultType res;
1450 res.matrix().template topRows<Dim>() = lhs.matrix() * rhs.matrix();
1451 res.matrix().row(Dim) = rhs.matrix().row(Dim);
1452 return res;
1453 }
1454};
1455
1456template<typename Scalar, int Dim, int LhsOptions, int RhsOptions>
1457struct transform_transform_product_impl<Transform<Scalar,Dim,Projective,LhsOptions>,Transform<Scalar,Dim,AffineCompact,RhsOptions>,true >
1458{
1459 typedef Transform<Scalar,Dim,Projective,LhsOptions> Lhs;
1460 typedef Transform<Scalar,Dim,AffineCompact,RhsOptions> Rhs;
1461 typedef Transform<Scalar,Dim,Projective> ResultType;
1462 static ResultType run(const Lhs& lhs, const Rhs& rhs)
1463 {
1464 ResultType res(lhs.matrix().template leftCols<Dim>() * rhs.matrix());
1465 res.matrix().col(Dim) += lhs.matrix().col(Dim);
1466 return res;
1467 }
1468};
1469
1470} // end namespace internal
1471
1472} // end namespace Eigen
1473
1474#endif // EIGEN_TRANSFORM_H
Note: See TracBrowser for help on using the repository browser.