blob: 90038cd2a1fa212237006d2ef0b94fc93bdf207a [file] [log] [blame]
Gael Guennebaud99462972008-08-31 17:30:09 +00001namespace Eigen {
2
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01003/** \eigenManualPage TutorialGeometry Geometry
Gael Guennebaud99462972008-08-31 17:30:09 +00004
Jitse Niesen26cfe5a2010-07-09 11:59:29 +01005\li \b Previous: \ref TutorialReductionsVisitorsBroadcasting
Gael Guennebaud41ea92d2010-07-04 10:14:47 +02006\li \b Next: \ref TutorialSparse
Gael Guennebaud99462972008-08-31 17:30:09 +00007
Tim Holy16a2d892011-06-20 22:47:58 -05008In 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 +00009
Gael Guennebaud93ee82b2013-01-05 16:37:11 +010010\eigenAutoToc
Gael Guennebaud99462972008-08-31 17:30:09 +000011
Benoit Jacob789ea9d2008-12-22 20:50:47 +000012Eigen's Geometry module provides two different kinds of geometric transformations:
13 - 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 +020014 - Projective or affine transformation matrices: see the Transform class. These are really matrices.
Benoit Jacob789ea9d2008-12-22 20:50:47 +000015
Hauke Heibel85fdcdf2010-08-17 20:03:50 +020016\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 +000017
18You can construct a Transform from an abstract transformation, like this:
19\code
20 Transform t(AngleAxis(angle,axis));
21\endcode
22or like this:
23\code
24 Transform t;
25 t = AngleAxis(angle,axis);
26\endcode
27But note that unfortunately, because of how C++ works, you can \b not do this:
28\code
29 Transform t = AngleAxis(angle,axis);
30\endcode
31<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.
32</span>
33
Gael Guennebaud99462972008-08-31 17:30:09 +000034\section TutorialGeoElementaryTransformations Transformation types
35
Gael Guennebaudf66fe262010-10-19 11:40:49 +020036<table class="manual">
37<tr><th>Transformation type</th><th>Typical initialization code</th></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000038<tr><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +000039\ref Rotation2D "2D rotation" from an angle</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +000040Rotation2D<float> rot2(angle_in_radian);\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020041<tr class="alt"><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +0000423D rotation as an \ref AngleAxis "angle + axis"</td><td>\code
Gael Guennebaud57207232011-11-01 09:40:51 +010043AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));\endcode
44<span class="note">The axis vector must be normalized.</span></td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000045<tr><td>
Gael Guennebaud6825c8d2008-09-01 06:33:19 +0000463D rotation as a \ref Quaternion "quaternion"</td><td>\code
Gael Guennebaud5b71d442011-05-28 22:12:15 +020047Quaternion<float> q; q = AngleAxis<float>(angle_in_radian, axis);\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020048<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +000049N-D Scaling</td><td>\code
Gael Guennebaud17273732012-06-18 22:07:13 +020050Scaling(sx, sy)
51Scaling(sx, sy, sz)
52Scaling(s)
53Scaling(vecN)\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000054<tr><td>
55N-D Translation</td><td>\code
56Translation<float,2>(tx, ty)
57Translation<float,3>(tx, ty, tz)
58Translation<float,N>(s)
59Translation<float,N>(vecN)\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020060<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +000061N-D \ref TutorialGeoTransform "Affine transformation"</td><td>\code
Gael Guennebaudb5f32832010-10-06 13:27:14 +020062Transform<float,N,Affine> t = concatenation_of_any_transformations;
Gael Guennebaud17273732012-06-18 22:07:13 +020063Transform<float,3,Affine> t = Translation3f(p) * AngleAxisf(a,axis) * Scaling(s);\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000064<tr><td>
65N-D Linear transformations \n
66<em class=note>(pure rotations, \n scaling, etc.)</em></td><td>\code
67Matrix<float,N> t = concatenation_of_rotations_and_scalings;
Gael Guennebaud17273732012-06-18 22:07:13 +020068Matrix<float,2> t = Rotation2Df(a) * Scaling(s);
69Matrix<float,3> t = AngleAxisf(a,axis) * Scaling(s);\endcode</td></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +000070</table>
71
72<strong>Notes on rotations</strong>\n To transform more than a single vector the preferred
73representations are rotation matrices, while for other usages Quaternion is the
74representation of choice as they are compact, fast and stable. Finally Rotation2D and
75AngleAxis are mainly convenient types to create other rotation objects.
76
Tim Holy16a2d892011-06-20 22:47:58 -050077<strong>Notes on Translation and Scaling</strong>\n Like AngleAxis, these classes were
Gael Guennebaud99462972008-08-31 17:30:09 +000078designed to simplify the creation/initialization of linear (Matrix) and affine (Transform)
79transformations. Nevertheless, unlike AngleAxis which is inefficient to use, these classes
80might still be interesting to write generic and efficient algorithms taking as input any
81kind of transformations.
82
83Any of the above transformation types can be converted to any other types of the same nature,
Gael Guennebaud582c1f92008-11-22 19:51:05 +000084or to a more generic type. Here are some additional examples:
Gael Guennebaudf66fe262010-10-19 11:40:49 +020085<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +000086<tr><td>\code
Gael Guennebaud5b71d442011-05-28 22:12:15 +020087Rotation2Df r; r = Matrix2f(..); // assumes a pure rotation matrix
88AngleAxisf aa; aa = Quaternionf(..);
89AngleAxisf aa; aa = Matrix3f(..); // assumes a pure rotation matrix
90Matrix2f m; m = Rotation2Df(..);
Gael Guennebaud17273732012-06-18 22:07:13 +020091Matrix3f m; m = Quaternionf(..); Matrix3f m; m = Scaling(..);
92Affine3f m; m = AngleAxis3f(..); Affine3f m; m = Scaling(..);
Gael Guennebaud5b71d442011-05-28 22:12:15 +020093Affine3f m; m = Translation3f(..); Affine3f m; m = Matrix3f(..);
Gael Guennebaud99462972008-08-31 17:30:09 +000094\endcode</td></tr>
95</table>
96
97
98<a href="#" class="top">top</a>\section TutorialGeoCommontransformationAPI Common API across transformation types
99
Gael Guennebauda4487ef2009-02-05 21:19:40 +0000100To some extent, Eigen's \ref Geometry_Module "geometry module" allows you to write
Gael Guennebaud99462972008-08-31 17:30:09 +0000101generic algorithms working on any kind of transformation representations:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200102<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000103<tr><td>
104Concatenation of two transformations</td><td>\code
105gen1 * gen2;\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200106<tr class="alt"><td>Apply the transformation to a vector</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000107vec2 = gen1 * vec1;\endcode</td></tr>
108<tr><td>Get the inverse of the transformation</td><td>\code
109gen2 = gen1.inverse();\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200110<tr class="alt"><td>Spherical interpolation \n (Rotation2D and Quaternion only)</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000111rot3 = rot1.slerp(alpha,rot2);\endcode</td></tr>
112</table>
113
114
115
116<a href="#" class="top">top</a>\section TutorialGeoTransform Affine transformations
117Generic affine transformations are represented by the Transform class which internaly
118is a (Dim+1)^2 matrix. In Eigen we have chosen to not distinghish between points and
119vectors such that all points are actually represented by displacement vectors from the
120origin ( \f$ \mathbf{p} \equiv \mathbf{p}-0 \f$ ). With that in mind, real points and
121vector distinguish when the transformation is applied.
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200122<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000123<tr><td>
124Apply the transformation to a \b point </td><td>\code
125VectorNf p1, p2;
126p2 = t * p1;\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200127<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000128Apply the transformation to a \b vector </td><td>\code
129VectorNf vec1, vec2;
130vec2 = t.linear() * vec1;\endcode</td></tr>
131<tr><td>
132Apply a \em general transformation \n to a \b normal \b vector
133(<a href="http://www.cgafaq.info/wiki/Transforming_normals">explanations</a>)</td><td>\code
134VectorNf n1, n2;
135MatrixNf normalMatrix = t.linear().inverse().transpose();
136n2 = (normalMatrix * n1).normalized();\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200137<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000138Apply a transformation with \em pure \em rotation \n to a \b normal \b vector
139(no scaling, no shear)</td><td>\code
140n2 = t.linear() * n1;\endcode</td></tr>
141<tr><td>
142OpenGL compatibility \b 3D </td><td>\code
143glLoadMatrixf(t.data());\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200144<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000145OpenGL compatibility \b 2D </td><td>\code
Jitse Niesene0a6ce52011-09-19 21:57:26 +0100146Affine3f aux(Affine3f::Identity());
Benoit Jacob9962c592010-04-22 14:11:18 -0400147aux.linear().topLeftCorner<2,2>() = t.linear();
Gael Guennebaud99462972008-08-31 17:30:09 +0000148aux.translation().start<2>() = t.translation();
149glLoadMatrixf(aux.data());\endcode</td></tr>
150</table>
151
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200152\b Component \b accessors
153<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000154<tr><td>
155full read-write access to the internal matrix</td><td>\code
156t.matrix() = matN1xN1; // N1 means N+1
157matN1xN1 = t.matrix();
158\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200159<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000160coefficient accessors</td><td>\code
161t(i,j) = scalar; <=> t.matrix()(i,j) = scalar;
162scalar = t(i,j); <=> scalar = t.matrix()(i,j);
163\endcode</td></tr>
164<tr><td>
165translation part</td><td>\code
166t.translation() = vecN;
167vecN = t.translation();
168\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200169<tr class="alt"><td>
Gael Guennebaud99462972008-08-31 17:30:09 +0000170linear part</td><td>\code
171t.linear() = matNxN;
172matNxN = t.linear();
173\endcode</td></tr>
174<tr><td>
175extract the rotation matrix</td><td>\code
Gael Guennebaud8719b1b2012-11-29 22:48:13 +0800176matNxN = t.rotation();
Gael Guennebaud99462972008-08-31 17:30:09 +0000177\endcode</td></tr>
178</table>
179
180
181\b Transformation \b creation \n
182While transformation objects can be created and updated concatenating elementary transformations,
183the Transform class also features a procedural API:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200184<table class="manual">
Tim Holy16a2d892011-06-20 22:47:58 -0500185<tr><th></th><th>procedural API</th><th>equivalent natural API </th></tr>
Gael Guennebaud99462972008-08-31 17:30:09 +0000186<tr><td>Translation</td><td>\code
187t.translate(Vector_(tx,ty,..));
188t.pretranslate(Vector_(tx,ty,..));
189\endcode</td><td>\code
190t *= Translation_(tx,ty,..);
191t = Translation_(tx,ty,..) * t;
192\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200193<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 +0000194t.rotate(any_rotation);
195t.prerotate(any_rotation);
196\endcode</td><td>\code
197t *= any_rotation;
198t = any_rotation * t;
199\endcode</td></tr>
200<tr><td>Scaling</td><td>\code
201t.scale(Vector_(sx,sy,..));
202t.scale(s);
203t.prescale(Vector_(sx,sy,..));
204t.prescale(s);
205\endcode</td><td>\code
Gael Guennebaud17273732012-06-18 22:07:13 +0200206t *= Scaling(sx,sy,..);
207t *= Scaling(s);
208t = Scaling(sx,sy,..) * t;
209t = Scaling(s) * t;
Gael Guennebaud99462972008-08-31 17:30:09 +0000210\endcode</td></tr>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200211<tr class="alt"><td>Shear transformation \n ( \b 2D \b only ! )</td><td>\code
Gael Guennebaud99462972008-08-31 17:30:09 +0000212t.shear(sx,sy);
213t.preshear(sx,sy);
214\endcode</td><td></td></tr>
215</table>
216
217Note 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 +0200218<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000219<tr><td>\code
220t.pretranslate(..).rotate(..).translate(..).scale(..);
221\endcode</td></tr>
222<tr><td>\code
Gael Guennebaud17273732012-06-18 22:07:13 +0200223t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling(..);
Gael Guennebaud99462972008-08-31 17:30:09 +0000224\endcode</td></tr>
225</table>
226
227
228
229<a href="#" class="top">top</a>\section TutorialGeoEulerAngles Euler angles
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200230<table class="manual">
Gael Guennebaud99462972008-08-31 17:30:09 +0000231<tr><td style="max-width:30em;">
232Euler angles might be convenient to create rotation objects.
Tim Holy16a2d892011-06-20 22:47:58 -0500233On 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 +0000234to create a rotation matrix according to the 2-1-2 convention.</td><td>\code
235Matrix3f m;
236m = AngleAxisf(angle1, Vector3f::UnitZ())
237* * AngleAxisf(angle2, Vector3f::UnitY())
238* * AngleAxisf(angle3, Vector3f::UnitZ());
239\endcode</td></tr>
240</table>
241
Gael Guennebaud41ea92d2010-07-04 10:14:47 +0200242\li \b Next: \ref TutorialSparse
243
Gael Guennebaud99462972008-08-31 17:30:09 +0000244*/
245
246}