blob: 372a275de45664728deeaadfa582fdf87fd65cd8 [file] [log] [blame]
Gael Guennebaud99462972008-08-31 17:30:09 +00001namespace Eigen {
2
Gael Guennebaud091a49c2013-01-06 23:48:59 +01003/** \eigenManualPage TutorialGeometry Space transformations
Gael Guennebaud99462972008-08-31 17:30:09 +00004
Gael Guennebaud091a49c2013-01-06 23:48:59 +01005In 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.
Gael Guennebaud99462972008-08-31 17:30:09 +00006
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01007\eigenAutoToc
Gael Guennebaud99462972008-08-31 17:30:09 +00008
Benoit Jacob789ea9d2008-12-22 20:50:47 +00009Eigen's Geometry module provides two different kinds of geometric transformations:
10 - 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 +020011 - Projective or affine transformation matrices: see the Transform class. These are really matrices.
Benoit Jacob789ea9d2008-12-22 20:50:47 +000012
Hauke Heibel85fdcdf2010-08-17 20:03:50 +020013\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 +000014
15You can construct a Transform from an abstract transformation, like this:
16\code
17 Transform t(AngleAxis(angle,axis));
18\endcode
19or like this:
20\code
21 Transform t;
22 t = AngleAxis(angle,axis);
23\endcode
24But note that unfortunately, because of how C++ works, you can \b not do this:
25\code
26 Transform t = AngleAxis(angle,axis);
27\endcode
28<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.
29</span>
30
Gael Guennebaud99462972008-08-31 17:30:09 +000031\section TutorialGeoElementaryTransformations Transformation types
32
Gael Guennebaudf66fe262010-10-19 11:40:49 +020033<table class="manual">
34<tr><th>Transformation type</th><th>Typical initialization code</th></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000035<tr><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +000036\ref Rotation2D "2D rotation" from an angle</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +000037Rotation2D<float> rot2(angle_in_radian);\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020038<tr class="alt"><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +0000393D rotation as an \ref AngleAxis "angle + axis"</td><td>\code
Gael Guennebaud57207232011-11-01 09:40:51 +010040AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));\endcode
41<span class="note">The axis vector must be normalized.</span></td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000042<tr><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +0000433D rotation as a \ref Quaternion "quaternion"</td><td>\code
Gael Guennebaud5b71d442011-05-28 22:12:15 +020044Quaternion<float> q; q = AngleAxis<float>(angle_in_radian, axis);\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020045<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +000046N-D Scaling</td><td>\code
Gael Guennebaud17273732012-06-18 22:07:13 +020047Scaling(sx, sy)
48Scaling(sx, sy, sz)
49Scaling(s)
50Scaling(vecN)\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000051<tr><td>
52N-D Translation</td><td>\code
53Translation<float,2>(tx, ty)
54Translation<float,3>(tx, ty, tz)
55Translation<float,N>(s)
56Translation<float,N>(vecN)\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020057<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +000058N-D \ref TutorialGeoTransform "Affine transformation"</td><td>\code
Gael Guennebaudb5f32832010-10-06 13:27:14 +020059Transform<float,N,Affine> t = concatenation_of_any_transformations;
Gael Guennebaud17273732012-06-18 22:07:13 +020060Transform<float,3,Affine> t = Translation3f(p) * AngleAxisf(a,axis) * Scaling(s);\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000061<tr><td>
62N-D Linear transformations \n
63<em class=note>(pure rotations, \n scaling, etc.)</em></td><td>\code
64Matrix<float,N> t = concatenation_of_rotations_and_scalings;
Gael Guennebaud17273732012-06-18 22:07:13 +020065Matrix<float,2> t = Rotation2Df(a) * Scaling(s);
66Matrix<float,3> t = AngleAxisf(a,axis) * Scaling(s);\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000067</table>
68
69<strong>Notes on rotations</strong>\n To transform more than a single vector the preferred
70representations are rotation matrices, while for other usages Quaternion is the
71representation of choice as they are compact, fast and stable. Finally Rotation2D and
72AngleAxis are mainly convenient types to create other rotation objects.
73
Tim Holy16a2d892011-06-20 22:47:58 -050074<strong>Notes on Translation and Scaling</strong>\n Like AngleAxis, these classes were
Gael Guennebaud99462972008-08-31 17:30:09 +000075designed to simplify the creation/initialization of linear (Matrix) and affine (Transform)
76transformations. Nevertheless, unlike AngleAxis which is inefficient to use, these classes
77might still be interesting to write generic and efficient algorithms taking as input any
78kind of transformations.
79
80Any of the above transformation types can be converted to any other types of the same nature,
Gael Guennebaud582c1f92008-11-22 19:51:05 +000081or to a more generic type. Here are some additional examples:
Gael Guennebaudf66fe262010-10-19 11:40:49 +020082<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +000083<tr><td>\code
Gael Guennebaud5b71d442011-05-28 22:12:15 +020084Rotation2Df r; r = Matrix2f(..); // assumes a pure rotation matrix
85AngleAxisf aa; aa = Quaternionf(..);
86AngleAxisf aa; aa = Matrix3f(..); // assumes a pure rotation matrix
87Matrix2f m; m = Rotation2Df(..);
Gael Guennebaud17273732012-06-18 22:07:13 +020088Matrix3f m; m = Quaternionf(..); Matrix3f m; m = Scaling(..);
89Affine3f m; m = AngleAxis3f(..); Affine3f m; m = Scaling(..);
Gael Guennebaud5b71d442011-05-28 22:12:15 +020090Affine3f m; m = Translation3f(..); Affine3f m; m = Matrix3f(..);
Gael Guennebaud99462972008-08-31 17:30:09 +000091\endcode</td></tr>
92</table>
93
94
95<a href="#" class="top">top</a>\section TutorialGeoCommontransformationAPI Common API across transformation types
96
Gael Guennebauda4487ef2009-02-05 21:19:40 +000097To some extent, Eigen's \ref Geometry_Module "geometry module" allows you to write
Gael Guennebaud99462972008-08-31 17:30:09 +000098generic algorithms working on any kind of transformation representations:
Gael Guennebaudf66fe262010-10-19 11:40:49 +020099<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000100<tr><td>
101Concatenation of two transformations</td><td>\code
102gen1 * gen2;\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200103<tr class="alt"><td>Apply the transformation to a vector</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000104vec2 = gen1 * vec1;\endcode</td></tr>
105<tr><td>Get the inverse of the transformation</td><td>\code
106gen2 = gen1.inverse();\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200107<tr class="alt"><td>Spherical interpolation \n (Rotation2D and Quaternion only)</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000108rot3 = rot1.slerp(alpha,rot2);\endcode</td></tr>
109</table>
110
111
112
113<a href="#" class="top">top</a>\section TutorialGeoTransform Affine transformations
114Generic affine transformations are represented by the Transform class which internaly
115is a (Dim+1)^2 matrix. In Eigen we have chosen to not distinghish between points and
116vectors such that all points are actually represented by displacement vectors from the
117origin ( \f$ \mathbf{p} \equiv \mathbf{p}-0 \f$ ). With that in mind, real points and
118vector distinguish when the transformation is applied.
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200119<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000120<tr><td>
121Apply the transformation to a \b point </td><td>\code
122VectorNf p1, p2;
123p2 = t * p1;\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200124<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000125Apply the transformation to a \b vector </td><td>\code
126VectorNf vec1, vec2;
127vec2 = t.linear() * vec1;\endcode</td></tr>
128<tr><td>
129Apply a \em general transformation \n to a \b normal \b vector
Gael Guennebaudace2ed72013-08-12 13:38:25 +0200130(<a href="http://femto.cs.uiuc.edu/faqs/cga-faq.html#S5.27">explanations</a>)</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000131VectorNf n1, n2;
132MatrixNf normalMatrix = t.linear().inverse().transpose();
133n2 = (normalMatrix * n1).normalized();\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200134<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000135Apply a transformation with \em pure \em rotation \n to a \b normal \b vector
136(no scaling, no shear)</td><td>\code
137n2 = t.linear() * n1;\endcode</td></tr>
138<tr><td>
139OpenGL compatibility \b 3D </td><td>\code
140glLoadMatrixf(t.data());\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200141<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000142OpenGL compatibility \b 2D </td><td>\code
Jitse Niesene0a6ce52011-09-19 21:57:26 +0100143Affine3f aux(Affine3f::Identity());
Benoit Jacob9962c592010-04-22 14:11:18 -0400144aux.linear().topLeftCorner<2,2>() = t.linear();
Gael Guennebaud99462972008-08-31 17:30:09 +0000145aux.translation().start<2>() = t.translation();
146glLoadMatrixf(aux.data());\endcode</td></tr>
147</table>
148
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200149\b Component \b accessors
150<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000151<tr><td>
152full read-write access to the internal matrix</td><td>\code
153t.matrix() = matN1xN1; // N1 means N+1
154matN1xN1 = t.matrix();
155\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200156<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000157coefficient accessors</td><td>\code
158t(i,j) = scalar; <=> t.matrix()(i,j) = scalar;
159scalar = t(i,j); <=> scalar = t.matrix()(i,j);
160\endcode</td></tr>
161<tr><td>
162translation part</td><td>\code
163t.translation() = vecN;
164vecN = t.translation();
165\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200166<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000167linear part</td><td>\code
168t.linear() = matNxN;
169matNxN = t.linear();
170\endcode</td></tr>
171<tr><td>
172extract the rotation matrix</td><td>\code
Gael Guennebaud8719b1b2012-11-29 22:48:13 +0800173matNxN = t.rotation();
Gael Guennebaud99462972008-08-31 17:30:09 +0000174\endcode</td></tr>
175</table>
176
177
178\b Transformation \b creation \n
179While transformation objects can be created and updated concatenating elementary transformations,
180the Transform class also features a procedural API:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200181<table class="manual">
Tim Holy16a2d892011-06-20 22:47:58 -0500182<tr><th></th><th>procedural API</th><th>equivalent natural API </th></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +0000183<tr><td>Translation</td><td>\code
184t.translate(Vector_(tx,ty,..));
185t.pretranslate(Vector_(tx,ty,..));
186\endcode</td><td>\code
187t *= Translation_(tx,ty,..);
188t = Translation_(tx,ty,..) * t;
189\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200190<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 +0000191t.rotate(any_rotation);
192t.prerotate(any_rotation);
193\endcode</td><td>\code
194t *= any_rotation;
195t = any_rotation * t;
196\endcode</td></tr>
197<tr><td>Scaling</td><td>\code
198t.scale(Vector_(sx,sy,..));
199t.scale(s);
200t.prescale(Vector_(sx,sy,..));
201t.prescale(s);
202\endcode</td><td>\code
Gael Guennebaud17273732012-06-18 22:07:13 +0200203t *= Scaling(sx,sy,..);
204t *= Scaling(s);
205t = Scaling(sx,sy,..) * t;
206t = Scaling(s) * t;
Gael Guennebaud99462972008-08-31 17:30:09 +0000207\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200208<tr class="alt"><td>Shear transformation \n ( \b 2D \b only ! )</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000209t.shear(sx,sy);
210t.preshear(sx,sy);
211\endcode</td><td></td></tr>
212</table>
213
214Note 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 +0200215<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000216<tr><td>\code
217t.pretranslate(..).rotate(..).translate(..).scale(..);
218\endcode</td></tr>
219<tr><td>\code
Gael Guennebaud17273732012-06-18 22:07:13 +0200220t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling(..);
Gael Guennebaud99462972008-08-31 17:30:09 +0000221\endcode</td></tr>
222</table>
223
224
225
226<a href="#" class="top">top</a>\section TutorialGeoEulerAngles Euler angles
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200227<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000228<tr><td style="max-width:30em;">
229Euler angles might be convenient to create rotation objects.
Tim Holy16a2d892011-06-20 22:47:58 -0500230On 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 +0000231to create a rotation matrix according to the 2-1-2 convention.</td><td>\code
232Matrix3f m;
233m = AngleAxisf(angle1, Vector3f::UnitZ())
234* * AngleAxisf(angle2, Vector3f::UnitY())
235* * AngleAxisf(angle3, Vector3f::UnitZ());
236\endcode</td></tr>
237</table>
238
Gael Guennebaud99462972008-08-31 17:30:09 +0000239*/
240
241}