namespace Eigen {
/** \eigenManualPage TutorialGeometry Space transformations
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.
\eigenAutoToc
Eigen's Geometry module provides two different kinds of geometric transformations:
- 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.
- Projective or affine transformation matrices: see the Transform class. These are really matrices.
\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.
You can construct a Transform from an abstract transformation, like this:
\code
Transform t(AngleAxis(angle,axis));
\endcode
or like this:
\code
Transform t;
t = AngleAxis(angle,axis);
\endcode
But note that unfortunately, because of how C++ works, you can \b not do this:
\code
Transform t = AngleAxis(angle,axis);
\endcode
\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.
\section TutorialGeoElementaryTransformations Transformation types
Transformation type | Typical initialization code |
\ref Rotation2D "2D rotation" from an angle | \code
Rotation2D rot2(angle_in_radian);\endcode |
3D rotation as an \ref AngleAxis "angle + axis" | \code
AngleAxis aa(angle_in_radian, Vector3f(ax,ay,az));\endcode
The axis vector must be normalized. |
3D rotation as a \ref Quaternion "quaternion" | \code
Quaternion q; q = AngleAxis(angle_in_radian, axis);\endcode |
N-D Scaling | \code
Scaling(sx, sy)
Scaling(sx, sy, sz)
Scaling(s)
Scaling(vecN)\endcode |
N-D Translation | \code
Translation(tx, ty)
Translation(tx, ty, tz)
Translation(s)
Translation(vecN)\endcode |
N-D \ref TutorialGeoTransform "Affine transformation" | \code
Transform t = concatenation_of_any_transformations;
Transform t = Translation3f(p) * AngleAxisf(a,axis) * Scaling(s);\endcode |
N-D Linear transformations \n
(pure rotations, \n scaling, etc.) | \code
Matrix t = concatenation_of_rotations_and_scalings;
Matrix t = Rotation2Df(a) * Scaling(s);
Matrix t = AngleAxisf(a,axis) * Scaling(s);\endcode |
Notes on rotations\n To transform more than a single vector the preferred
representations are rotation matrices, while for other usages Quaternion is the
representation of choice as they are compact, fast and stable. Finally Rotation2D and
AngleAxis are mainly convenient types to create other rotation objects.
Notes on Translation and Scaling\n Like AngleAxis, these classes were
designed to simplify the creation/initialization of linear (Matrix) and affine (Transform)
transformations. Nevertheless, unlike AngleAxis which is inefficient to use, these classes
might still be interesting to write generic and efficient algorithms taking as input any
kind of transformations.
Any of the above transformation types can be converted to any other types of the same nature,
or to a more generic type. Here are some additional examples:
\code
Rotation2Df r; r = Matrix2f(..); // assumes a pure rotation matrix
AngleAxisf aa; aa = Quaternionf(..);
AngleAxisf aa; aa = Matrix3f(..); // assumes a pure rotation matrix
Matrix2f m; m = Rotation2Df(..);
Matrix3f m; m = Quaternionf(..); Matrix3f m; m = Scaling(..);
Affine3f m; m = AngleAxis3f(..); Affine3f m; m = Scaling(..);
Affine3f m; m = Translation3f(..); Affine3f m; m = Matrix3f(..);
\endcode |
top\section TutorialGeoCommontransformationAPI Common API across transformation types
To some extent, Eigen's \ref Geometry_Module "geometry module" allows you to write
generic algorithms working on any kind of transformation representations:
Concatenation of two transformations | \code
gen1 * gen2;\endcode |
Apply the transformation to a vector | \code
vec2 = gen1 * vec1;\endcode |
Get the inverse of the transformation | \code
gen2 = gen1.inverse();\endcode |
Spherical interpolation \n (Rotation2D and Quaternion only) | \code
rot3 = rot1.slerp(alpha,rot2);\endcode |
top\section TutorialGeoTransform Affine transformations
Generic affine transformations are represented by the Transform class which internally
is a (Dim+1)^2 matrix. In Eigen we have chosen to not distinghish between points and
vectors such that all points are actually represented by displacement vectors from the
origin ( \f$ \mathbf{p} \equiv \mathbf{p}-0 \f$ ). With that in mind, real points and
vector distinguish when the transformation is applied.
Apply the transformation to a \b point | \code
VectorNf p1, p2;
p2 = t * p1;\endcode |
Apply the transformation to a \b vector | \code
VectorNf vec1, vec2;
vec2 = t.linear() * vec1;\endcode |
Apply a \em general transformation \n to a \b normal \b vector \n
| \code
VectorNf n1, n2;
MatrixNf normalMatrix = t.linear().inverse().transpose();
n2 = (normalMatrix * n1).normalized();\endcode |
(See subject 5.27 of this faq for the explanations) |
Apply a transformation with \em pure \em rotation \n to a \b normal \b vector
(no scaling, no shear) | \code
n2 = t.linear() * n1;\endcode |
OpenGL compatibility \b 3D | \code
glLoadMatrixf(t.data());\endcode |
OpenGL compatibility \b 2D | \code
Affine3f aux(Affine3f::Identity());
aux.linear().topLeftCorner<2,2>() = t.linear();
aux.translation().start<2>() = t.translation();
glLoadMatrixf(aux.data());\endcode |
\b Component \b accessors
full read-write access to the internal matrix | \code
t.matrix() = matN1xN1; // N1 means N+1
matN1xN1 = t.matrix();
\endcode |
coefficient accessors | \code
t(i,j) = scalar; <=> t.matrix()(i,j) = scalar;
scalar = t(i,j); <=> scalar = t.matrix()(i,j);
\endcode |
translation part | \code
t.translation() = vecN;
vecN = t.translation();
\endcode |
linear part | \code
t.linear() = matNxN;
matNxN = t.linear();
\endcode |
extract the rotation matrix | \code
matNxN = t.rotation();
\endcode |
\b Transformation \b creation \n
While transformation objects can be created and updated concatenating elementary transformations,
the Transform class also features a procedural API:
| procedural API | equivalent natural API |
Translation | \code
t.translate(Vector_(tx,ty,..));
t.pretranslate(Vector_(tx,ty,..));
\endcode | \code
t *= Translation_(tx,ty,..);
t = Translation_(tx,ty,..) * t;
\endcode |
\b Rotation \n In 2D and for the procedural API, any_rotation can also \n be an angle in radian | \code
t.rotate(any_rotation);
t.prerotate(any_rotation);
\endcode | \code
t *= any_rotation;
t = any_rotation * t;
\endcode |
Scaling | \code
t.scale(Vector_(sx,sy,..));
t.scale(s);
t.prescale(Vector_(sx,sy,..));
t.prescale(s);
\endcode | \code
t *= Scaling(sx,sy,..);
t *= Scaling(s);
t = Scaling(sx,sy,..) * t;
t = Scaling(s) * t;
\endcode |
Shear transformation \n ( \b 2D \b only ! ) | \code
t.shear(sx,sy);
t.preshear(sx,sy);
\endcode | |
Note that in both API, any many transformations can be concatenated in a single expression as shown in the two following equivalent examples:
\code
t.pretranslate(..).rotate(..).translate(..).scale(..);
\endcode |
\code
t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling(..);
\endcode |
top\section TutorialGeoEulerAngles Euler angles
Euler angles might be convenient to create rotation objects.
On the other hand, since there exist 24 different conventions, they are pretty confusing to use. This example shows how
to create a rotation matrix according to the 2-1-2 convention. | \code
Matrix3f m;
m = AngleAxisf(angle1, Vector3f::UnitZ())
* * AngleAxisf(angle2, Vector3f::UnitY())
* * AngleAxisf(angle3, Vector3f::UnitZ());
\endcode |
*/
}