00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 namespace Eigen {
00014
00015
00016
00017
00018 template< typename Other,
00019 int Dim,
00020 int HDim,
00021 int OtherRows=Other::RowsAtCompileTime,
00022 int OtherCols=Other::ColsAtCompileTime>
00023 struct ei_transform_product_impl;
00024
00042 template<typename _Scalar, int _Dim>
00043 class Transform
00044 {
00045 public:
00046 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim==Dynamic ? Dynamic : (_Dim+1)*(_Dim+1))
00047 enum {
00048 Dim = _Dim,
00049 HDim = _Dim+1
00050 };
00052 typedef _Scalar Scalar;
00054 typedef Matrix<Scalar,HDim,HDim> MatrixType;
00056 typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
00058 typedef Block<MatrixType,Dim,Dim> LinearPart;
00060 typedef const Block<const MatrixType,Dim,Dim> ConstLinearPart;
00062 typedef Matrix<Scalar,Dim,1> VectorType;
00064 typedef Block<MatrixType,Dim,1> TranslationPart;
00066 typedef const Block<const MatrixType,Dim,1> ConstTranslationPart;
00068 typedef Translation<Scalar,Dim> TranslationType;
00070 typedef Scaling<Scalar,Dim> ScalingType;
00071
00072 protected:
00073
00074 MatrixType m_matrix;
00075
00076 public:
00077
00079 inline Transform() { }
00080
00081 inline Transform(const Transform& other)
00082 {
00083 m_matrix = other.m_matrix;
00084 }
00085
00086 inline explicit Transform(const TranslationType& t) { *this = t; }
00087 inline explicit Transform(const ScalingType& s) { *this = s; }
00088 template<typename Derived>
00089 inline explicit Transform(const RotationBase<Derived, Dim>& r) { *this = r; }
00090
00091 inline Transform& operator=(const Transform& other)
00092 { m_matrix = other.m_matrix; return *this; }
00093
00094 template<typename OtherDerived, bool BigMatrix>
00095 struct construct_from_matrix
00096 {
00097 static inline void run(Transform *transform, const MatrixBase<OtherDerived>& other)
00098 {
00099 transform->matrix() = other;
00100 }
00101 };
00102
00103 template<typename OtherDerived> struct construct_from_matrix<OtherDerived, true>
00104 {
00105 static inline void run(Transform *transform, const MatrixBase<OtherDerived>& other)
00106 {
00107 transform->linear() = other;
00108 transform->translation().setZero();
00109 transform->matrix()(Dim,Dim) = Scalar(1);
00110 transform->matrix().template block<1,Dim>(Dim,0).setZero();
00111 }
00112 };
00113
00115 template<typename OtherDerived>
00116 inline explicit Transform(const MatrixBase<OtherDerived>& other)
00117 {
00118 construct_from_matrix<OtherDerived, int(OtherDerived::RowsAtCompileTime) == Dim>::run(this, other);
00119 }
00120
00122 template<typename OtherDerived>
00123 inline Transform& operator=(const MatrixBase<OtherDerived>& other)
00124 { m_matrix = other; return *this; }
00125
00126 #ifdef EIGEN_QT_SUPPORT
00127 inline Transform(const QMatrix& other);
00128 inline Transform& operator=(const QMatrix& other);
00129 inline QMatrix toQMatrix(void) const;
00130 inline Transform(const QTransform& other);
00131 inline Transform& operator=(const QTransform& other);
00132 inline QTransform toQTransform(void) const;
00133 #endif
00134
00137 inline Scalar operator() (int row, int col) const { return m_matrix(row,col); }
00140 inline Scalar& operator() (int row, int col) { return m_matrix(row,col); }
00141
00143 inline const MatrixType& matrix() const { return m_matrix; }
00145 inline MatrixType& matrix() { return m_matrix; }
00146
00148 inline ConstLinearPart linear() const { return m_matrix.template block<Dim,Dim>(0,0); }
00150 inline LinearPart linear() { return m_matrix.template block<Dim,Dim>(0,0); }
00151
00153 inline ConstTranslationPart translation() const { return m_matrix.template block<Dim,1>(0,Dim); }
00155 inline TranslationPart translation() { return m_matrix.template block<Dim,1>(0,Dim); }
00156
00164
00165 template<typename OtherDerived>
00166 inline const typename ei_transform_product_impl<OtherDerived,_Dim,_Dim+1>::ResultType
00167 operator * (const MatrixBase<OtherDerived> &other) const
00168 { return ei_transform_product_impl<OtherDerived,Dim,HDim>::run(*this,other.derived()); }
00169
00172 template<typename OtherDerived>
00173 friend inline const typename ProductReturnType<OtherDerived,MatrixType>::Type
00174 operator * (const MatrixBase<OtherDerived> &a, const Transform &b)
00175 { return a.derived() * b.matrix(); }
00176
00178 inline const Transform
00179 operator * (const Transform& other) const
00180 { return Transform(m_matrix * other.matrix()); }
00181
00183 void setIdentity() { m_matrix.setIdentity(); }
00184 static const typename MatrixType::IdentityReturnType Identity()
00185 {
00186 return MatrixType::Identity();
00187 }
00188
00189 template<typename OtherDerived>
00190 inline Transform& scale(const MatrixBase<OtherDerived> &other);
00191
00192 template<typename OtherDerived>
00193 inline Transform& prescale(const MatrixBase<OtherDerived> &other);
00194
00195 inline Transform& scale(Scalar s);
00196 inline Transform& prescale(Scalar s);
00197
00198 template<typename OtherDerived>
00199 inline Transform& translate(const MatrixBase<OtherDerived> &other);
00200
00201 template<typename OtherDerived>
00202 inline Transform& pretranslate(const MatrixBase<OtherDerived> &other);
00203
00204 template<typename RotationType>
00205 inline Transform& rotate(const RotationType& rotation);
00206
00207 template<typename RotationType>
00208 inline Transform& prerotate(const RotationType& rotation);
00209
00210 Transform& shear(Scalar sx, Scalar sy);
00211 Transform& preshear(Scalar sx, Scalar sy);
00212
00213 inline Transform& operator=(const TranslationType& t);
00214 inline Transform& operator*=(const TranslationType& t) { return translate(t.vector()); }
00215 inline Transform operator*(const TranslationType& t) const;
00216
00217 inline Transform& operator=(const ScalingType& t);
00218 inline Transform& operator*=(const ScalingType& s) { return scale(s.coeffs()); }
00219 inline Transform operator*(const ScalingType& s) const;
00220 friend inline Transform operator*(const LinearMatrixType& mat, const Transform& t)
00221 {
00222 Transform res = t;
00223 res.matrix().row(Dim) = t.matrix().row(Dim);
00224 res.matrix().template block<Dim,HDim>(0,0) = (mat * t.matrix().template block<Dim,HDim>(0,0)).lazy();
00225 return res;
00226 }
00227
00228 template<typename Derived>
00229 inline Transform& operator=(const RotationBase<Derived,Dim>& r);
00230 template<typename Derived>
00231 inline Transform& operator*=(const RotationBase<Derived,Dim>& r) { return rotate(r.toRotationMatrix()); }
00232 template<typename Derived>
00233 inline Transform operator*(const RotationBase<Derived,Dim>& r) const;
00234
00235 LinearMatrixType rotation() const;
00236 template<typename RotationMatrixType, typename ScalingMatrixType>
00237 void computeRotationScaling(RotationMatrixType *rotation, ScalingMatrixType *scaling) const;
00238 template<typename ScalingMatrixType, typename RotationMatrixType>
00239 void computeScalingRotation(ScalingMatrixType *scaling, RotationMatrixType *rotation) const;
00240
00241 template<typename PositionDerived, typename OrientationType, typename ScaleDerived>
00242 Transform& fromPositionOrientationScale(const MatrixBase<PositionDerived> &position,
00243 const OrientationType& orientation, const MatrixBase<ScaleDerived> &scale);
00244
00245 inline const MatrixType inverse(TransformTraits traits = Affine) const;
00246
00248 const Scalar* data() const { return m_matrix.data(); }
00250 Scalar* data() { return m_matrix.data(); }
00251
00257 template<typename NewScalarType>
00258 inline typename internal::cast_return_type<Transform,Transform<NewScalarType,Dim> >::type cast() const
00259 { return typename internal::cast_return_type<Transform,Transform<NewScalarType,Dim> >::type(*this); }
00260
00262 template<typename OtherScalarType>
00263 inline explicit Transform(const Transform<OtherScalarType,Dim>& other)
00264 { m_matrix = other.matrix().template cast<Scalar>(); }
00265
00270 bool isApprox(const Transform& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
00271 { return m_matrix.isApprox(other.m_matrix, prec); }
00272
00273 #ifdef EIGEN_TRANSFORM_PLUGIN
00274 #include EIGEN_TRANSFORM_PLUGIN
00275 #endif
00276
00277 protected:
00278
00279 };
00280
00282 typedef Transform<float,2> Transform2f;
00284 typedef Transform<float,3> Transform3f;
00286 typedef Transform<double,2> Transform2d;
00288 typedef Transform<double,3> Transform3d;
00289
00290
00291
00292
00293
00294 #ifdef EIGEN_QT_SUPPORT
00295
00299 template<typename Scalar, int Dim>
00300 Transform<Scalar,Dim>::Transform(const QMatrix& other)
00301 {
00302 *this = other;
00303 }
00304
00309 template<typename Scalar, int Dim>
00310 Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const QMatrix& other)
00311 {
00312 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
00313 m_matrix << other.m11(), other.m21(), other.dx(),
00314 other.m12(), other.m22(), other.dy(),
00315 0, 0, 1;
00316 return *this;
00317 }
00318
00325 template<typename Scalar, int Dim>
00326 QMatrix Transform<Scalar,Dim>::toQMatrix(void) const
00327 {
00328 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
00329 return QMatrix(m_matrix.coeff(0,0), m_matrix.coeff(1,0),
00330 m_matrix.coeff(0,1), m_matrix.coeff(1,1),
00331 m_matrix.coeff(0,2), m_matrix.coeff(1,2));
00332 }
00333
00338 template<typename Scalar, int Dim>
00339 Transform<Scalar,Dim>::Transform(const QTransform& other)
00340 {
00341 *this = other;
00342 }
00343
00348 template<typename Scalar, int Dim>
00349 Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const QTransform& other)
00350 {
00351 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
00352 m_matrix << other.m11(), other.m21(), other.dx(),
00353 other.m12(), other.m22(), other.dy(),
00354 other.m13(), other.m23(), other.m33();
00355 return *this;
00356 }
00357
00362 template<typename Scalar, int Dim>
00363 QTransform Transform<Scalar,Dim>::toQTransform(void) const
00364 {
00365 EIGEN_STATIC_ASSERT(Dim==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
00366 return QTransform(m_matrix.coeff(0,0), m_matrix.coeff(1,0), m_matrix.coeff(2,0),
00367 m_matrix.coeff(0,1), m_matrix.coeff(1,1), m_matrix.coeff(2,1),
00368 m_matrix.coeff(0,2), m_matrix.coeff(1,2), m_matrix.coeff(2,2));
00369 }
00370 #endif
00371
00372
00373
00374
00375
00380 template<typename Scalar, int Dim>
00381 template<typename OtherDerived>
00382 Transform<Scalar,Dim>&
00383 Transform<Scalar,Dim>::scale(const MatrixBase<OtherDerived> &other)
00384 {
00385 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
00386 linear() = (linear() * other.asDiagonal()).lazy();
00387 return *this;
00388 }
00389
00394 template<typename Scalar, int Dim>
00395 inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::scale(Scalar s)
00396 {
00397 linear() *= s;
00398 return *this;
00399 }
00400
00405 template<typename Scalar, int Dim>
00406 template<typename OtherDerived>
00407 Transform<Scalar,Dim>&
00408 Transform<Scalar,Dim>::prescale(const MatrixBase<OtherDerived> &other)
00409 {
00410 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
00411 m_matrix.template block<Dim,HDim>(0,0) = (other.asDiagonal() * m_matrix.template block<Dim,HDim>(0,0)).lazy();
00412 return *this;
00413 }
00414
00419 template<typename Scalar, int Dim>
00420 inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::prescale(Scalar s)
00421 {
00422 m_matrix.template corner<Dim,HDim>(TopLeft) *= s;
00423 return *this;
00424 }
00425
00430 template<typename Scalar, int Dim>
00431 template<typename OtherDerived>
00432 Transform<Scalar,Dim>&
00433 Transform<Scalar,Dim>::translate(const MatrixBase<OtherDerived> &other)
00434 {
00435 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
00436 translation() += linear() * other;
00437 return *this;
00438 }
00439
00444 template<typename Scalar, int Dim>
00445 template<typename OtherDerived>
00446 Transform<Scalar,Dim>&
00447 Transform<Scalar,Dim>::pretranslate(const MatrixBase<OtherDerived> &other)
00448 {
00449 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,int(Dim))
00450 translation() += other;
00451 return *this;
00452 }
00453
00471 template<typename Scalar, int Dim>
00472 template<typename RotationType>
00473 Transform<Scalar,Dim>&
00474 Transform<Scalar,Dim>::rotate(const RotationType& rotation)
00475 {
00476 linear() *= ei_toRotationMatrix<Scalar,Dim>(rotation);
00477 return *this;
00478 }
00479
00487 template<typename Scalar, int Dim>
00488 template<typename RotationType>
00489 Transform<Scalar,Dim>&
00490 Transform<Scalar,Dim>::prerotate(const RotationType& rotation)
00491 {
00492 m_matrix.template block<Dim,HDim>(0,0) = ei_toRotationMatrix<Scalar,Dim>(rotation)
00493 * m_matrix.template block<Dim,HDim>(0,0);
00494 return *this;
00495 }
00496
00502 template<typename Scalar, int Dim>
00503 Transform<Scalar,Dim>&
00504 Transform<Scalar,Dim>::shear(Scalar sx, Scalar sy)
00505 {
00506 EIGEN_STATIC_ASSERT(int(Dim)==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
00507 VectorType tmp = linear().col(0)*sy + linear().col(1);
00508 linear() << linear().col(0) + linear().col(1)*sx, tmp;
00509 return *this;
00510 }
00511
00517 template<typename Scalar, int Dim>
00518 Transform<Scalar,Dim>&
00519 Transform<Scalar,Dim>::preshear(Scalar sx, Scalar sy)
00520 {
00521 EIGEN_STATIC_ASSERT(int(Dim)==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
00522 m_matrix.template block<Dim,HDim>(0,0) = LinearMatrixType(1, sx, sy, 1) * m_matrix.template block<Dim,HDim>(0,0);
00523 return *this;
00524 }
00525
00526
00527
00528
00529
00530 template<typename Scalar, int Dim>
00531 inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const TranslationType& t)
00532 {
00533 linear().setIdentity();
00534 translation() = t.vector();
00535 m_matrix.template block<1,Dim>(Dim,0).setZero();
00536 m_matrix(Dim,Dim) = Scalar(1);
00537 return *this;
00538 }
00539
00540 template<typename Scalar, int Dim>
00541 inline Transform<Scalar,Dim> Transform<Scalar,Dim>::operator*(const TranslationType& t) const
00542 {
00543 Transform res = *this;
00544 res.translate(t.vector());
00545 return res;
00546 }
00547
00548 template<typename Scalar, int Dim>
00549 inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const ScalingType& s)
00550 {
00551 m_matrix.setZero();
00552 linear().diagonal() = s.coeffs();
00553 m_matrix.coeffRef(Dim,Dim) = Scalar(1);
00554 return *this;
00555 }
00556
00557 template<typename Scalar, int Dim>
00558 inline Transform<Scalar,Dim> Transform<Scalar,Dim>::operator*(const ScalingType& s) const
00559 {
00560 Transform res = *this;
00561 res.scale(s.coeffs());
00562 return res;
00563 }
00564
00565 template<typename Scalar, int Dim>
00566 template<typename Derived>
00567 inline Transform<Scalar,Dim>& Transform<Scalar,Dim>::operator=(const RotationBase<Derived,Dim>& r)
00568 {
00569 linear() = ei_toRotationMatrix<Scalar,Dim>(r);
00570 translation().setZero();
00571 m_matrix.template block<1,Dim>(Dim,0).setZero();
00572 m_matrix.coeffRef(Dim,Dim) = Scalar(1);
00573 return *this;
00574 }
00575
00576 template<typename Scalar, int Dim>
00577 template<typename Derived>
00578 inline Transform<Scalar,Dim> Transform<Scalar,Dim>::operator*(const RotationBase<Derived,Dim>& r) const
00579 {
00580 Transform res = *this;
00581 res.rotate(r.derived());
00582 return res;
00583 }
00584
00585
00586
00587
00588
00596 template<typename Scalar, int Dim>
00597 typename Transform<Scalar,Dim>::LinearMatrixType
00598 Transform<Scalar,Dim>::rotation() const
00599 {
00600 LinearMatrixType result;
00601 computeRotationScaling(&result, (LinearMatrixType*)0);
00602 return result;
00603 }
00604
00605
00617 template<typename Scalar, int Dim>
00618 template<typename RotationMatrixType, typename ScalingMatrixType>
00619 void Transform<Scalar,Dim>::computeRotationScaling(RotationMatrixType *rotation, ScalingMatrixType *scaling) const
00620 {
00621 JacobiSVD<LinearMatrixType> svd(linear(), ComputeFullU|ComputeFullV);
00622 Scalar x = (svd.matrixU() * svd.matrixV().adjoint()).determinant();
00623 Matrix<Scalar, Dim, 1> sv(svd.singularValues());
00624 sv.coeffRef(0) *= x;
00625 if(scaling)
00626 {
00627 scaling->noalias() = svd.matrixV() * sv.asDiagonal() * svd.matrixV().adjoint();
00628 }
00629 if(rotation)
00630 {
00631 LinearMatrixType m(svd.matrixU());
00632 m.col(0) /= x;
00633 rotation->noalias() = m * svd.matrixV().adjoint();
00634 }
00635 }
00636
00648 template<typename Scalar, int Dim>
00649 template<typename ScalingMatrixType, typename RotationMatrixType>
00650 void Transform<Scalar,Dim>::computeScalingRotation(ScalingMatrixType *scaling, RotationMatrixType *rotation) const
00651 {
00652 JacobiSVD<LinearMatrixType> svd(linear(), ComputeFullU|ComputeFullV);
00653 Scalar x = (svd.matrixU() * svd.matrixV().adjoint()).determinant();
00654 Matrix<Scalar, Dim, 1> sv(svd.singularValues());
00655 sv.coeffRef(0) *= x;
00656 if(scaling)
00657 {
00658 scaling->noalias() = svd.matrixU() * sv.asDiagonal() * svd.matrixU().adjoint();
00659 }
00660 if(rotation)
00661 {
00662 LinearMatrixType m(svd.matrixU());
00663 m.col(0) /= x;
00664 rotation->noalias() = m * svd.matrixV().adjoint();
00665 }
00666 }
00667
00671 template<typename Scalar, int Dim>
00672 template<typename PositionDerived, typename OrientationType, typename ScaleDerived>
00673 Transform<Scalar,Dim>&
00674 Transform<Scalar,Dim>::fromPositionOrientationScale(const MatrixBase<PositionDerived> &position,
00675 const OrientationType& orientation, const MatrixBase<ScaleDerived> &scale)
00676 {
00677 linear() = ei_toRotationMatrix<Scalar,Dim>(orientation);
00678 linear() *= scale.asDiagonal();
00679 translation() = position;
00680 m_matrix.template block<1,Dim>(Dim,0).setZero();
00681 m_matrix(Dim,Dim) = Scalar(1);
00682 return *this;
00683 }
00684
00704 template<typename Scalar, int Dim>
00705 inline const typename Transform<Scalar,Dim>::MatrixType
00706 Transform<Scalar,Dim>::inverse(TransformTraits traits) const
00707 {
00708 if (traits == Projective)
00709 {
00710 return m_matrix.inverse();
00711 }
00712 else
00713 {
00714 MatrixType res;
00715 if (traits == Affine)
00716 {
00717 res.template corner<Dim,Dim>(TopLeft) = linear().inverse();
00718 }
00719 else if (traits == Isometry)
00720 {
00721 res.template corner<Dim,Dim>(TopLeft) = linear().transpose();
00722 }
00723 else
00724 {
00725 ei_assert("invalid traits value in Transform::inverse()");
00726 }
00727
00728 res.template corner<Dim,1>(TopRight) = - res.template corner<Dim,Dim>(TopLeft) * translation();
00729 res.template corner<1,Dim>(BottomLeft).setZero();
00730 res.coeffRef(Dim,Dim) = Scalar(1);
00731 return res;
00732 }
00733 }
00734
00735
00736
00737
00738
00739 template<typename Other, int Dim, int HDim>
00740 struct ei_transform_product_impl<Other,Dim,HDim, HDim,HDim>
00741 {
00742 typedef Transform<typename Other::Scalar,Dim> TransformType;
00743 typedef typename TransformType::MatrixType MatrixType;
00744 typedef typename ProductReturnType<MatrixType,Other>::Type ResultType;
00745 static ResultType run(const TransformType& tr, const Other& other)
00746 { return tr.matrix() * other; }
00747 };
00748
00749 template<typename Other, int Dim, int HDim>
00750 struct ei_transform_product_impl<Other,Dim,HDim, Dim,Dim>
00751 {
00752 typedef Transform<typename Other::Scalar,Dim> TransformType;
00753 typedef typename TransformType::MatrixType MatrixType;
00754 typedef TransformType ResultType;
00755 static ResultType run(const TransformType& tr, const Other& other)
00756 {
00757 TransformType res;
00758 res.translation() = tr.translation();
00759 res.matrix().row(Dim) = tr.matrix().row(Dim);
00760 res.linear() = (tr.linear() * other).lazy();
00761 return res;
00762 }
00763 };
00764
00765 template<typename Other, int Dim, int HDim>
00766 struct ei_transform_product_impl<Other,Dim,HDim, HDim,1>
00767 {
00768 typedef Transform<typename Other::Scalar,Dim> TransformType;
00769 typedef typename TransformType::MatrixType MatrixType;
00770 typedef typename ProductReturnType<MatrixType,Other>::Type ResultType;
00771 static ResultType run(const TransformType& tr, const Other& other)
00772 { return tr.matrix() * other; }
00773 };
00774
00775 template<typename Other, int Dim, int HDim>
00776 struct ei_transform_product_impl<Other,Dim,HDim, Dim,1>
00777 {
00778 typedef typename Other::Scalar Scalar;
00779 typedef Transform<Scalar,Dim> TransformType;
00780 typedef Matrix<Scalar,Dim,1> ResultType;
00781 static ResultType run(const TransformType& tr, const Other& other)
00782 { return ((tr.linear() * other) + tr.translation())
00783 * (Scalar(1) / ( (tr.matrix().template block<1,Dim>(Dim,0) * other).coeff(0) + tr.matrix().coeff(Dim,Dim))); }
00784 };
00785
00786 }