TutorialGeometry.dox 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 \n
  113. </td><td>\code
  114. VectorNf n1, n2;
  115. MatrixNf normalMatrix = t.linear().inverse().transpose();
  116. n2 = (normalMatrix * n1).normalized();\endcode</td></tr>
  117. <tr><td colspan="2">(See subject 5.27 of this <a href="http://www.faqs.org/faqs/graphics/algorithms-faq">faq</a> for the explanations)</td></tr>
  118. <tr class="alt"><td>
  119. Apply a transformation with \em pure \em rotation \n to a \b normal \b vector
  120. (no scaling, no shear)</td><td>\code
  121. n2 = t.linear() * n1;\endcode</td></tr>
  122. <tr><td>
  123. OpenGL compatibility \b 3D </td><td>\code
  124. glLoadMatrixf(t.data());\endcode</td></tr>
  125. <tr class="alt"><td>
  126. OpenGL compatibility \b 2D </td><td>\code
  127. Affine3f aux(Affine3f::Identity());
  128. aux.linear().topLeftCorner<2,2>() = t.linear();
  129. aux.translation().start<2>() = t.translation();
  130. glLoadMatrixf(aux.data());\endcode</td></tr>
  131. </table>
  132. \b Component \b accessors
  133. <table class="manual">
  134. <tr><td>
  135. full read-write access to the internal matrix</td><td>\code
  136. t.matrix() = matN1xN1; // N1 means N+1
  137. matN1xN1 = t.matrix();
  138. \endcode</td></tr>
  139. <tr class="alt"><td>
  140. coefficient accessors</td><td>\code
  141. t(i,j) = scalar; <=> t.matrix()(i,j) = scalar;
  142. scalar = t(i,j); <=> scalar = t.matrix()(i,j);
  143. \endcode</td></tr>
  144. <tr><td>
  145. translation part</td><td>\code
  146. t.translation() = vecN;
  147. vecN = t.translation();
  148. \endcode</td></tr>
  149. <tr class="alt"><td>
  150. linear part</td><td>\code
  151. t.linear() = matNxN;
  152. matNxN = t.linear();
  153. \endcode</td></tr>
  154. <tr><td>
  155. extract the rotation matrix</td><td>\code
  156. matNxN = t.rotation();
  157. \endcode</td></tr>
  158. </table>
  159. \b Transformation \b creation \n
  160. While transformation objects can be created and updated concatenating elementary transformations,
  161. the Transform class also features a procedural API:
  162. <table class="manual">
  163. <tr><th></th><th>procedural API</th><th>equivalent natural API </th></tr>
  164. <tr><td>Translation</td><td>\code
  165. t.translate(Vector_(tx,ty,..));
  166. t.pretranslate(Vector_(tx,ty,..));
  167. \endcode</td><td>\code
  168. t *= Translation_(tx,ty,..);
  169. t = Translation_(tx,ty,..) * t;
  170. \endcode</td></tr>
  171. <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
  172. t.rotate(any_rotation);
  173. t.prerotate(any_rotation);
  174. \endcode</td><td>\code
  175. t *= any_rotation;
  176. t = any_rotation * t;
  177. \endcode</td></tr>
  178. <tr><td>Scaling</td><td>\code
  179. t.scale(Vector_(sx,sy,..));
  180. t.scale(s);
  181. t.prescale(Vector_(sx,sy,..));
  182. t.prescale(s);
  183. \endcode</td><td>\code
  184. t *= Scaling(sx,sy,..);
  185. t *= Scaling(s);
  186. t = Scaling(sx,sy,..) * t;
  187. t = Scaling(s) * t;
  188. \endcode</td></tr>
  189. <tr class="alt"><td>Shear transformation \n ( \b 2D \b only ! )</td><td>\code
  190. t.shear(sx,sy);
  191. t.preshear(sx,sy);
  192. \endcode</td><td></td></tr>
  193. </table>
  194. Note that in both API, any many transformations can be concatenated in a single expression as shown in the two following equivalent examples:
  195. <table class="manual">
  196. <tr><td>\code
  197. t.pretranslate(..).rotate(..).translate(..).scale(..);
  198. \endcode</td></tr>
  199. <tr><td>\code
  200. t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling(..);
  201. \endcode</td></tr>
  202. </table>
  203. <a href="#" class="top">top</a>\section TutorialGeoEulerAngles Euler angles
  204. <table class="manual">
  205. <tr><td style="max-width:30em;">
  206. Euler angles might be convenient to create rotation objects.
  207. On the other hand, since there exist 24 different conventions, they are pretty confusing to use. This example shows how
  208. to create a rotation matrix according to the 2-1-2 convention.</td><td>\code
  209. Matrix3f m;
  210. m = AngleAxisf(angle1, Vector3f::UnitZ())
  211. * * AngleAxisf(angle2, Vector3f::UnitY())
  212. * * AngleAxisf(angle3, Vector3f::UnitZ());
  213. \endcode</td></tr>
  214. </table>
  215. */
  216. }