Transform.h 59 KB

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