You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
9.4 KiB

  1. namespace Eigen {
  2. /** \eigenManualPage TutorialGeometry Space transformations
  3. In this page, we will introduce the many possibilities offered by the \ref Geometry_Module "geometry module" to deal with 2D and 3D rotations and projective or affine transformations.
  4. \eigenAutoToc
  5. Eigen's Geometry module provides two different kinds of geometric transformations:
  6. - Abstract transformations, such as rotations (represented by \ref AngleAxis "angle and axis" or by a \ref Quaternion "quaternion"), \ref Translation "translations", \ref Scaling "scalings". These transformations are NOT represented as matrices, but you can nevertheless mix them with matrices and vectors in expressions, and convert them to matrices if you wish.
  7. - Projective or affine transformation matrices: see the Transform class. These are really matrices.
  8. \note If you are working with OpenGL 4x4 matrices then Affine3f and Affine3d are what you want. Since Eigen defaults to column-major storage, you can directly use the Transform::data() method to pass your transformation matrix to OpenGL.
  9. You can construct a Transform from an abstract transformation, like this:
  10. \code
  11. Transform t(AngleAxis(angle,axis));
  12. \endcode
  13. or like this:
  14. \code
  15. Transform t;
  16. t = AngleAxis(angle,axis);
  17. \endcode
  18. But note that unfortunately, because of how C++ works, you can \b not do this:
  19. \code
  20. Transform t = AngleAxis(angle,axis);
  21. \endcode
  22. <span class="note">\b Explanation: In the C++ language, this would require Transform to have a non-explicit conversion constructor from AngleAxis, but we really don't want to allow implicit casting here.
  23. </span>
  24. \section TutorialGeoElementaryTransformations Transformation types
  25. <table class="manual">
  26. <tr><th>Transformation type</th><th>Typical initialization code</th></tr>
  27. <tr><td>
  28. \ref Rotation2D "2D rotation" from an angle</td><td>\code
  29. Rotation2D<float> rot2(angle_in_radian);\endcode</td></tr>
  30. <tr class="alt"><td>
  31. 3D rotation as an \ref AngleAxis "angle + axis"</td><td>\code
  32. AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));\endcode
  33. <span class="note">The axis vector must be normalized.</span></td></tr>
  34. <tr><td>
  35. 3D rotation as a \ref Quaternion "quaternion"</td><td>\code
  36. Quaternion<float> q; q = AngleAxis<float>(angle_in_radian, axis);\endcode</td></tr>
  37. <tr class="alt"><td>
  38. N-D Scaling</td><td>\code
  39. Scaling(sx, sy)
  40. Scaling(sx, sy, sz)
  41. Scaling(s)
  42. Scaling(vecN)\endcode</td></tr>
  43. <tr><td>
  44. N-D Translation</td><td>\code
  45. Translation<float,2>(tx, ty)
  46. Translation<float,3>(tx, ty, tz)
  47. Translation<float,N>(s)
  48. Translation<float,N>(vecN)\endcode</td></tr>
  49. <tr class="alt"><td>
  50. N-D \ref TutorialGeoTransform "Affine transformation"</td><td>\code
  51. Transform<float,N,Affine> t = concatenation_of_any_transformations;
  52. Transform<float,3,Affine> t = Translation3f(p) * AngleAxisf(a,axis) * Scaling(s);\endcode</td></tr>
  53. <tr><td>
  54. N-D Linear transformations \n
  55. <em class=note>(pure rotations, \n scaling, etc.)</em></td><td>\code
  56. Matrix<float,N> t = concatenation_of_rotations_and_scalings;
  57. Matrix<float,2> t = Rotation2Df(a) * Scaling(s);
  58. Matrix<float,3> t = AngleAxisf(a,axis) * Scaling(s);\endcode</td></tr>
  59. </table>
  60. <strong>Notes on rotations</strong>\n To transform more than a single vector the preferred
  61. representations are rotation matrices, while for other usages Quaternion is the
  62. representation of choice as they are compact, fast and stable. Finally Rotation2D and
  63. AngleAxis are mainly convenient types to create other rotation objects.
  64. <strong>Notes on Translation and Scaling</strong>\n Like AngleAxis, these classes were
  65. designed to simplify the creation/initialization of linear (Matrix) and affine (Transform)
  66. transformations. Nevertheless, unlike AngleAxis which is inefficient to use, these classes
  67. might still be interesting to write generic and efficient algorithms taking as input any
  68. kind of transformations.
  69. Any of the above transformation types can be converted to any other types of the same nature,
  70. or to a more generic type. Here are some additional examples:
  71. <table class="manual">
  72. <tr><td>\code
  73. Rotation2Df r; r = Matrix2f(..); // assumes a pure rotation matrix
  74. AngleAxisf aa; aa = Quaternionf(..);
  75. AngleAxisf aa; aa = Matrix3f(..); // assumes a pure rotation matrix
  76. Matrix2f m; m = Rotation2Df(..);
  77. Matrix3f m; m = Quaternionf(..); Matrix3f m; m = Scaling(..);
  78. Affine3f m; m = AngleAxis3f(..); Affine3f m; m = Scaling(..);
  79. Affine3f m; m = Translation3f(..); Affine3f m; m = Matrix3f(..);
  80. \endcode</td></tr>
  81. </table>
  82. <a href="#" class="top">top</a>\section TutorialGeoCommontransformationAPI Common API across transformation types
  83. To some extent, Eigen's \ref Geometry_Module "geometry module" allows you to write
  84. generic algorithms working on any kind of transformation representations:
  85. <table class="manual">
  86. <tr><td>
  87. Concatenation of two transformations</td><td>\code
  88. gen1 * gen2;\endcode</td></tr>
  89. <tr class="alt"><td>Apply the transformation to a vector</td><td>\code
  90. vec2 = gen1 * vec1;\endcode</td></tr>
  91. <tr><td>Get the inverse of the transformation</td><td>\code
  92. gen2 = gen1.inverse();\endcode</td></tr>
  93. <tr class="alt"><td>Spherical interpolation \n (Rotation2D and Quaternion only)</td><td>\code
  94. rot3 = rot1.slerp(alpha,rot2);\endcode</td></tr>
  95. </table>
  96. <a href="#" class="top">top</a>\section TutorialGeoTransform Affine transformations
  97. Generic affine transformations are represented by the Transform class which internaly
  98. is a (Dim+1)^2 matrix. In Eigen we have chosen to not distinghish between points and
  99. vectors such that all points are actually represented by displacement vectors from the
  100. origin ( \f$ \mathbf{p} \equiv \mathbf{p}-0 \f$ ). With that in mind, real points and
  101. vector distinguish when the transformation is applied.
  102. <table class="manual">
  103. <tr><td>
  104. Apply the transformation to a \b point </td><td>\code
  105. VectorNf p1, p2;
  106. p2 = t * p1;\endcode</td></tr>
  107. <tr class="alt"><td>
  108. Apply the transformation to a \b vector </td><td>\code
  109. VectorNf vec1, vec2;
  110. vec2 = t.linear() * vec1;\endcode</td></tr>
  111. <tr><td>
  112. Apply a \em general transformation \n to a \b normal \b vector
  113. (<a href="http://femto.cs.uiuc.edu/faqs/cga-faq.html#S5.27">explanations</a>)</td><td>\code
  114. VectorNf n1, n2;
  115. MatrixNf normalMatrix = t.linear().inverse().transpose();
  116. n2 = (normalMatrix * n1).normalized();\endcode</td></tr>
  117. <tr class="alt"><td>
  118. Apply a transformation with \em pure \em rotation \n to a \b normal \b vector
  119. (no scaling, no shear)</td><td>\code
  120. n2 = t.linear() * n1;\endcode</td></tr>
  121. <tr><td>
  122. OpenGL compatibility \b 3D </td><td>\code
  123. glLoadMatrixf(t.data());\endcode</td></tr>
  124. <tr class="alt"><td>
  125. OpenGL compatibility \b 2D </td><td>\code
  126. Affine3f aux(Affine3f::Identity());
  127. aux.linear().topLeftCorner<2,2>() = t.linear();
  128. aux.translation().start<2>() = t.translation();
  129. glLoadMatrixf(aux.data());\endcode</td></tr>
  130. </table>
  131. \b Component \b accessors
  132. <table class="manual">
  133. <tr><td>
  134. full read-write access to the internal matrix</td><td>\code
  135. t.matrix() = matN1xN1; // N1 means N+1
  136. matN1xN1 = t.matrix();
  137. \endcode</td></tr>
  138. <tr class="alt"><td>
  139. coefficient accessors</td><td>\code
  140. t(i,j) = scalar; <=> t.matrix()(i,j) = scalar;
  141. scalar = t(i,j); <=> scalar = t.matrix()(i,j);
  142. \endcode</td></tr>
  143. <tr><td>
  144. translation part</td><td>\code
  145. t.translation() = vecN;
  146. vecN = t.translation();
  147. \endcode</td></tr>
  148. <tr class="alt"><td>
  149. linear part</td><td>\code
  150. t.linear() = matNxN;
  151. matNxN = t.linear();
  152. \endcode</td></tr>
  153. <tr><td>
  154. extract the rotation matrix</td><td>\code
  155. matNxN = t.rotation();
  156. \endcode</td></tr>
  157. </table>
  158. \b Transformation \b creation \n
  159. While transformation objects can be created and updated concatenating elementary transformations,
  160. the Transform class also features a procedural API:
  161. <table class="manual">
  162. <tr><th></th><th>procedural API</th><th>equivalent natural API </th></tr>
  163. <tr><td>Translation</td><td>\code
  164. t.translate(Vector_(tx,ty,..));
  165. t.pretranslate(Vector_(tx,ty,..));
  166. \endcode</td><td>\code
  167. t *= Translation_(tx,ty,..);
  168. t = Translation_(tx,ty,..) * t;
  169. \endcode</td></tr>
  170. <tr class="alt"><td>\b Rotation \n <em class="note">In 2D and for the procedural API, any_rotation can also \n be an angle in radian</em></td><td>\code
  171. t.rotate(any_rotation);
  172. t.prerotate(any_rotation);
  173. \endcode</td><td>\code
  174. t *= any_rotation;
  175. t = any_rotation * t;
  176. \endcode</td></tr>
  177. <tr><td>Scaling</td><td>\code
  178. t.scale(Vector_(sx,sy,..));
  179. t.scale(s);
  180. t.prescale(Vector_(sx,sy,..));
  181. t.prescale(s);
  182. \endcode</td><td>\code
  183. t *= Scaling(sx,sy,..);
  184. t *= Scaling(s);
  185. t = Scaling(sx,sy,..) * t;
  186. t = Scaling(s) * t;
  187. \endcode</td></tr>
  188. <tr class="alt"><td>Shear transformation \n ( \b 2D \b only ! )</td><td>\code
  189. t.shear(sx,sy);
  190. t.preshear(sx,sy);
  191. \endcode</td><td></td></tr>
  192. </table>
  193. Note that in both API, any many transformations can be concatenated in a single expression as shown in the two following equivalent examples:
  194. <table class="manual">
  195. <tr><td>\code
  196. t.pretranslate(..).rotate(..).translate(..).scale(..);
  197. \endcode</td></tr>
  198. <tr><td>\code
  199. t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling(..);
  200. \endcode</td></tr>
  201. </table>
  202. <a href="#" class="top">top</a>\section TutorialGeoEulerAngles Euler angles
  203. <table class="manual">
  204. <tr><td style="max-width:30em;">
  205. Euler angles might be convenient to create rotation objects.
  206. On the other hand, since there exist 24 different conventions, they are pretty confusing to use. This example shows how
  207. to create a rotation matrix according to the 2-1-2 convention.</td><td>\code
  208. Matrix3f m;
  209. m = AngleAxisf(angle1, Vector3f::UnitZ())
  210. * * AngleAxisf(angle2, Vector3f::UnitY())
  211. * * AngleAxisf(angle3, Vector3f::UnitZ());
  212. \endcode</td></tr>
  213. </table>
  214. */
  215. }