blob: 452abda107ac89729ce9116c06c4f5be5eef8d2e [file] [log] [blame]
Gael Guennebaud99462972008-08-31 17:30:09 +00001namespace Eigen {
2
Gael Guennebaud41ea92d2010-07-04 10:14:47 +02003/** \page TutorialGeometry Tutorial page 8 - Geometry
Gael Guennebaud99462972008-08-31 17:30:09 +00004 \ingroup Tutorial
5
Jitse Niesen26cfe5a2010-07-09 11:59:29 +01006\li \b Previous: \ref TutorialReductionsVisitorsBroadcasting
Gael Guennebaud41ea92d2010-07-04 10:14:47 +02007\li \b Next: \ref TutorialSparse
Gael Guennebaud99462972008-08-31 17:30:09 +00008
Tim Holy16a2d892011-06-20 22:47:58 -05009In this tutorial, we will briefly introduce the many possibilities offered by the \ref Geometry_Module "geometry module", namely 2D and 3D rotations and projective or affine transformations.
Gael Guennebaud99462972008-08-31 17:30:09 +000010
11\b Table \b of \b contents
12 - \ref TutorialGeoElementaryTransformations
13 - \ref TutorialGeoCommontransformationAPI
14 - \ref TutorialGeoTransform
15 - \ref TutorialGeoEulerAngles
16
Benoit Jacob789ea9d2008-12-22 20:50:47 +000017Eigen's Geometry module provides two different kinds of geometric transformations:
18 - 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.
Gael Guennebaud41ea92d2010-07-04 10:14:47 +020019 - Projective or affine transformation matrices: see the Transform class. These are really matrices.
Benoit Jacob789ea9d2008-12-22 20:50:47 +000020
Hauke Heibel85fdcdf2010-08-17 20:03:50 +020021\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.
Benoit Jacob789ea9d2008-12-22 20:50:47 +000022
23You can construct a Transform from an abstract transformation, like this:
24\code
25 Transform t(AngleAxis(angle,axis));
26\endcode
27or like this:
28\code
29 Transform t;
30 t = AngleAxis(angle,axis);
31\endcode
32But note that unfortunately, because of how C++ works, you can \b not do this:
33\code
34 Transform t = AngleAxis(angle,axis);
35\endcode
36<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.
37</span>
38
Gael Guennebaud99462972008-08-31 17:30:09 +000039\section TutorialGeoElementaryTransformations Transformation types
40
Gael Guennebaudf66fe262010-10-19 11:40:49 +020041<table class="manual">
42<tr><th>Transformation type</th><th>Typical initialization code</th></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000043<tr><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +000044\ref Rotation2D "2D rotation" from an angle</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +000045Rotation2D<float> rot2(angle_in_radian);\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020046<tr class="alt"><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +0000473D rotation as an \ref AngleAxis "angle + axis"</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +000048AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));\endcode</td></tr>
49<tr><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +0000503D rotation as a \ref Quaternion "quaternion"</td><td>\code
Gael Guennebaud5b71d442011-05-28 22:12:15 +020051Quaternion<float> q; q = AngleAxis<float>(angle_in_radian, axis);\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020052<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +000053N-D Scaling</td><td>\code
54Scaling<float,2>(sx, sy)
55Scaling<float,3>(sx, sy, sz)
56Scaling<float,N>(s)
57Scaling<float,N>(vecN)\endcode</td></tr>
58<tr><td>
59N-D Translation</td><td>\code
60Translation<float,2>(tx, ty)
61Translation<float,3>(tx, ty, tz)
62Translation<float,N>(s)
63Translation<float,N>(vecN)\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020064<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +000065N-D \ref TutorialGeoTransform "Affine transformation"</td><td>\code
Gael Guennebaudb5f32832010-10-06 13:27:14 +020066Transform<float,N,Affine> t = concatenation_of_any_transformations;
67Transform<float,3,Affine> t = Translation3f(p) * AngleAxisf(a,axis) * Scaling3f(s);\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000068<tr><td>
69N-D Linear transformations \n
70<em class=note>(pure rotations, \n scaling, etc.)</em></td><td>\code
71Matrix<float,N> t = concatenation_of_rotations_and_scalings;
72Matrix<float,2> t = Rotation2Df(a) * Scaling2f(s);
73Matrix<float,3> t = AngleAxisf(a,axis) * Scaling3f(s);\endcode</td></tr>
74</table>
75
76<strong>Notes on rotations</strong>\n To transform more than a single vector the preferred
77representations are rotation matrices, while for other usages Quaternion is the
78representation of choice as they are compact, fast and stable. Finally Rotation2D and
79AngleAxis are mainly convenient types to create other rotation objects.
80
Tim Holy16a2d892011-06-20 22:47:58 -050081<strong>Notes on Translation and Scaling</strong>\n Like AngleAxis, these classes were
Gael Guennebaud99462972008-08-31 17:30:09 +000082designed to simplify the creation/initialization of linear (Matrix) and affine (Transform)
83transformations. Nevertheless, unlike AngleAxis which is inefficient to use, these classes
84might still be interesting to write generic and efficient algorithms taking as input any
85kind of transformations.
86
87Any of the above transformation types can be converted to any other types of the same nature,
Gael Guennebaud582c1f92008-11-22 19:51:05 +000088or to a more generic type. Here are some additional examples:
Gael Guennebaudf66fe262010-10-19 11:40:49 +020089<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +000090<tr><td>\code
Gael Guennebaud5b71d442011-05-28 22:12:15 +020091Rotation2Df r; r = Matrix2f(..); // assumes a pure rotation matrix
92AngleAxisf aa; aa = Quaternionf(..);
93AngleAxisf aa; aa = Matrix3f(..); // assumes a pure rotation matrix
94Matrix2f m; m = Rotation2Df(..);
95Matrix3f m; m = Quaternionf(..); Matrix3f m; m = Scaling3f(..);
96Affine3f m; m = AngleAxis3f(..); Affine3f m; m = Scaling3f(..);
97Affine3f m; m = Translation3f(..); Affine3f m; m = Matrix3f(..);
Gael Guennebaud99462972008-08-31 17:30:09 +000098\endcode</td></tr>
99</table>
100
101
102<a href="#" class="top">top</a>\section TutorialGeoCommontransformationAPI Common API across transformation types
103
Gael Guennebauda4487ef2009-02-05 21:19:40 +0000104To some extent, Eigen's \ref Geometry_Module "geometry module" allows you to write
Gael Guennebaud99462972008-08-31 17:30:09 +0000105generic algorithms working on any kind of transformation representations:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200106<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000107<tr><td>
108Concatenation of two transformations</td><td>\code
109gen1 * gen2;\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200110<tr class="alt"><td>Apply the transformation to a vector</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000111vec2 = gen1 * vec1;\endcode</td></tr>
112<tr><td>Get the inverse of the transformation</td><td>\code
113gen2 = gen1.inverse();\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200114<tr class="alt"><td>Spherical interpolation \n (Rotation2D and Quaternion only)</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000115rot3 = rot1.slerp(alpha,rot2);\endcode</td></tr>
116</table>
117
118
119
120<a href="#" class="top">top</a>\section TutorialGeoTransform Affine transformations
121Generic affine transformations are represented by the Transform class which internaly
122is a (Dim+1)^2 matrix. In Eigen we have chosen to not distinghish between points and
123vectors such that all points are actually represented by displacement vectors from the
124origin ( \f$ \mathbf{p} \equiv \mathbf{p}-0 \f$ ). With that in mind, real points and
125vector distinguish when the transformation is applied.
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200126<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000127<tr><td>
128Apply the transformation to a \b point </td><td>\code
129VectorNf p1, p2;
130p2 = t * p1;\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200131<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000132Apply the transformation to a \b vector </td><td>\code
133VectorNf vec1, vec2;
134vec2 = t.linear() * vec1;\endcode</td></tr>
135<tr><td>
136Apply a \em general transformation \n to a \b normal \b vector
137(<a href="http://www.cgafaq.info/wiki/Transforming_normals">explanations</a>)</td><td>\code
138VectorNf n1, n2;
139MatrixNf normalMatrix = t.linear().inverse().transpose();
140n2 = (normalMatrix * n1).normalized();\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200141<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000142Apply a transformation with \em pure \em rotation \n to a \b normal \b vector
143(no scaling, no shear)</td><td>\code
144n2 = t.linear() * n1;\endcode</td></tr>
145<tr><td>
146OpenGL compatibility \b 3D </td><td>\code
147glLoadMatrixf(t.data());\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200148<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000149OpenGL compatibility \b 2D </td><td>\code
Jitse Niesene0a6ce52011-09-19 21:57:26 +0100150Affine3f aux(Affine3f::Identity());
Benoit Jacob9962c592010-04-22 14:11:18 -0400151aux.linear().topLeftCorner<2,2>() = t.linear();
Gael Guennebaud99462972008-08-31 17:30:09 +0000152aux.translation().start<2>() = t.translation();
153glLoadMatrixf(aux.data());\endcode</td></tr>
154</table>
155
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200156\b Component \b accessors
157<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000158<tr><td>
159full read-write access to the internal matrix</td><td>\code
160t.matrix() = matN1xN1; // N1 means N+1
161matN1xN1 = t.matrix();
162\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200163<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000164coefficient accessors</td><td>\code
165t(i,j) = scalar; <=> t.matrix()(i,j) = scalar;
166scalar = t(i,j); <=> scalar = t.matrix()(i,j);
167\endcode</td></tr>
168<tr><td>
169translation part</td><td>\code
170t.translation() = vecN;
171vecN = t.translation();
172\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200173<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000174linear part</td><td>\code
175t.linear() = matNxN;
176matNxN = t.linear();
177\endcode</td></tr>
178<tr><td>
179extract the rotation matrix</td><td>\code
180matNxN = t.extractRotation();
181\endcode</td></tr>
182</table>
183
184
185\b Transformation \b creation \n
186While transformation objects can be created and updated concatenating elementary transformations,
187the Transform class also features a procedural API:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200188<table class="manual">
Tim Holy16a2d892011-06-20 22:47:58 -0500189<tr><th></th><th>procedural API</th><th>equivalent natural API </th></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +0000190<tr><td>Translation</td><td>\code
191t.translate(Vector_(tx,ty,..));
192t.pretranslate(Vector_(tx,ty,..));
193\endcode</td><td>\code
194t *= Translation_(tx,ty,..);
195t = Translation_(tx,ty,..) * t;
196\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200197<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
Gael Guennebaud99462972008-08-31 17:30:09 +0000198t.rotate(any_rotation);
199t.prerotate(any_rotation);
200\endcode</td><td>\code
201t *= any_rotation;
202t = any_rotation * t;
203\endcode</td></tr>
204<tr><td>Scaling</td><td>\code
205t.scale(Vector_(sx,sy,..));
206t.scale(s);
207t.prescale(Vector_(sx,sy,..));
208t.prescale(s);
209\endcode</td><td>\code
210t *= Scaling_(sx,sy,..);
211t *= Scaling_(s);
212t = Scaling_(sx,sy,..) * t;
213t = Scaling_(s) * t;
214\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200215<tr class="alt"><td>Shear transformation \n ( \b 2D \b only ! )</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000216t.shear(sx,sy);
217t.preshear(sx,sy);
218\endcode</td><td></td></tr>
219</table>
220
221Note that in both API, any many transformations can be concatenated in a single expression as shown in the two following equivalent examples:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200222<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000223<tr><td>\code
224t.pretranslate(..).rotate(..).translate(..).scale(..);
225\endcode</td></tr>
226<tr><td>\code
227t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling_(..);
228\endcode</td></tr>
229</table>
230
231
232
233<a href="#" class="top">top</a>\section TutorialGeoEulerAngles Euler angles
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200234<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000235<tr><td style="max-width:30em;">
236Euler angles might be convenient to create rotation objects.
Tim Holy16a2d892011-06-20 22:47:58 -0500237On the other hand, since there exist 24 different conventions, they are pretty confusing to use. This example shows how
Gael Guennebaud99462972008-08-31 17:30:09 +0000238to create a rotation matrix according to the 2-1-2 convention.</td><td>\code
239Matrix3f m;
240m = AngleAxisf(angle1, Vector3f::UnitZ())
241* * AngleAxisf(angle2, Vector3f::UnitY())
242* * AngleAxisf(angle3, Vector3f::UnitZ());
243\endcode</td></tr>
244</table>
245
Gael Guennebaud41ea92d2010-07-04 10:14:47 +0200246\li \b Next: \ref TutorialSparse
247
Gael Guennebaud99462972008-08-31 17:30:09 +0000248*/
249
250}