Rotation formalisms in three dimensions

In geometry, various formalisms exist to express a rotation in three dimensions as a mathematical transformation. In physics, this concept is applied to classical mechanics where rotational (or angular) kinematics is the science of quantitative description of a purely rotational motion. The orientation of an object at a given instant is described with the same tools, as it is defined as an imaginary rotation from a reference placement in space, rather than an actually observed rotation from a previous placement in space.
According to Euler's rotation theorem the rotation of a rigid body (or three-dimensional coordinate system with the fixed origin) is described by a single rotation about some axis. Such a rotation may be uniquely described by a minimum of three real parameters. However, for various reasons, there are several ways to represent it. Many of these representations use more than the necessary minimum of three parameters, although each of them still has only three degrees of freedom.
An example where rotation representation is used is in computer vision, where an automated observer needs to track a target. Consider a rigid body, with three orthogonal unit vectors fixed to its body (representing the three axes of the object's local coordinate system). The basic problem is to specify the orientation of these three unit vectors, and hence the rigid body, with respect to the observer's coordinate system, regarded as a reference placement in space.
Rotations and motions.
Rotation formalisms are focused on proper (orientation-preserving) motions of the Euclidean space with one fixed point, that a "rotation" refers to. Although physical motions with a fixed point are an important case (such as ones described in the center-of-mass frame, or motions of a joint), this approach creates a knowledge about all motions. Any proper motion of the Euclidean space decomposes to a rotation around the origin and a translation. Whichever the order of their composition will be, the "pure" rotation component wouldn't change, uniquely determined by the complete motion.
One can also understand "pure" rotations as linear maps in a vector space equipped with Euclidean structure, not as maps of points of a corresponding affine space. In other words, a rotation formalism captures only the rotational part of a motion, that contains three degrees of freedom, and ignores the translational part, that contains another three.
When representing a rotation as numbers in a computer, some people prefer the quaternion representation or the axis+angle representation, because they avoid the gimbal lock that can occur with Euler rotations.
Formalism alternatives.
Rotation matrix.
The above-mentioned triad of unit vectors is also called a basis. Specifying the coordinates ("components") of vectors of this basis in its current (rotated) position, in terms of the reference (non-rotated) coordinate axes, will completely describe the rotation. The three unit vectors, formula_1, formula_2 and formula_3, that form the rotated basis each consist of 3 coordinates, yielding a total of 9 parameters.
These parameters can be written as the elements of a matrix , called a rotation matrix. Typically, the coordinates of each of these vectors are arranged along a column of the matrix (however, beware that an alternative definition of rotation matrix exists and is widely used, where the vectors coordinates defined above are arranged by rows)
The elements of the rotation matrix are not all independent—as Euler's rotation theorem dictates, the rotation matrix has only three degrees of freedom.
The rotation matrix has the following properties:
The angle which appears in the eigenvalue expression corresponds to the angle of the Euler axis and angle representation. The eigenvector corresponding to the eigenvalue of 1 is the accompanying Euler axis, since the axis is the only (nonzero) vector which remains unchanged by left-multiplying (rotating) it with the rotation matrix.
The above properties are equivalent to:
which is another way of stating that formula_7 form a 3D orthonormal basis. These statements comprise a total of 6 conditions (the cross product contains 3), leaving the rotation matrix with just 3 degrees of freedom, as required.
Two successive rotations represented by matrices and are easily combined as elements of a group,
(Note the order, since the vector being rotated is multiplied from the right).
The ease by which vectors can be rotated using a rotation matrix, as well as the ease of combining successive rotations, make the rotation matrix a useful and popular way to represent rotations, even though it is less concise than other representations.
Euler axis and angle (rotation vector).
From Euler's rotation theorem we know that any rotation can be expressed as a single rotation about some axis. The axis is the unit vector (unique except for sign) which remains unchanged by the rotation. The magnitude of the angle is also unique, with its sign being determined by the sign of the rotation axis.
The axis can be represented as a three-dimensional unit vector
and the angle by a scalar .
Since the axis is normalized, it has only two degrees of freedom. The angle adds the third degree of freedom to this rotation representation.
One may wish to express rotation as a rotation vector, or Euler vector, an un-normalized three-dimensional vector the direction of which specifies the axis, and the length of which is ,
The rotation vector is useful in some contexts, as it represents a three-dimensional rotation with only three scalar values (its components), representing the three degrees of freedom. This is also true for representations based on sequences of three Euler angles (see below).
If the rotation angle is zero, the axis is not uniquely defined. Combining two successive rotations, each represented by an Euler axis and angle, is not straightforward, and in fact does not satisfy the law of vector addition, which shows that finite rotations are not really vectors at all. It is best to employ the rotation matrix or quaternion notation, calculate the product, and then convert back to Euler axis and angle.
Euler rotations.
The idea behind Euler rotations is to split the complete rotation of the coordinate system into three simpler constitutive rotations, called precession, nutation, and intrinsic rotation, being each one of them an increment on one of the Euler angles. Notice that the outer matrix will represent a rotation around one of the axes of the reference frame, and the inner matrix represents a rotation around one of the moving frame axes. The middle matrix represents a rotation around an intermediate axis called line of nodes.
However, the definition of Euler angles is not unique and in the literature many different conventions are used. These conventions depend on the axes about which the rotations are carried out, and their sequence (since rotations are not commutative).
The convention being used is usually indicated by specifying the axes about which the consecutive rotations (before being composed) take place, referring to them by index or letter . The engineering and robotics communities typically use 3-1-3 Euler angles. Notice that after composing the independent rotations, they do not rotate about their axis anymore. The most external matrix rotates the other two, leaving the second rotation matrix over the line of nodes, and the third one in a frame comoving with the body. There are possible combinations of three basic rotations but only of them can be used for representing arbitrary 3D rotations as Euler angles. These 12 combinations avoid consecutive rotations around the same axis (such as XXY) which would reduce the degrees of freedom that can be represented.
Therefore, Euler angles are never expressed in terms of the external frame, or in terms of the co-moving rotated body frame, but in a mixture. Other conventions (e.g., rotation matrix or quaternions) are used to avoid this problem.
In aviation orientation of the aircraft is usually expressed as intrinsic Tait-Bryan angles following the convention, which are called heading, elevation, and bank (or synonymously, yaw, pitch, and roll).
Quaternions.
Quaternions, which form a four-dimensional vector space, have proven very useful in representing rotations due to several advantages over the other representations mentioned in this article.
A quaternion representation of rotation is written as a versor (normalized quaternion)
The above definition stores quaternion as an array following the convention used in (Wertz 1980) and (Markley 2003). An alternative definition used for example in (Coutsias 1999) and (Schmidt 2001) defines the "scalar" term as the first quaternion element, with the other elements shifted down one position.
In terms of the Euler axis 
and angle this versor's components are expressed as follows: 
Inspection shows that the quaternion parametrization obeys the following constraint:
The last term (in our definition) is often called the scalar term, which has its origin in quaternions when understood as the mathematical extension of the complex numbers, written as 
and where are the hypercomplex numbers satisfying
Quaternion multiplication, that is used to specify a composite rotation, is performed in the same manner as multiplication of complex numbers, except that the order of elements must be taken into account, since multiplication is not commutative. In matrix notation we can write quaternion multiplication as
Combining two consecutive quaternion rotations is therefore just as simple as using the rotation matrix. Just as two successive rotation matrices, followed by , are combined as
we can represent this with quaternion parameters in a similarly concise way:
Quaternions are a very popular parametrization due to the following properties:
Like rotation matrices, quaternions must sometimes be renormalized due to rounding errors, to make sure that they correspond to valid rotations. The computational cost of renormalizing a quaternion, however, is much less than for normalizing a matrix.
Quaternions also capture the spinorial character of rotations in three dimensions. For a three-dimensional object connected to its (fixed) surroundings by slack strings or bands, the strings or bands can be untangled after "two" complete turns about some fixed axis from an initial untangled state. Algebraically, the quaternion describing such a rotation changes from a scalar +1 (initially), through (scalar + pseudovector) values to scalar −1 (at one full turn), through (scalar + pseudovector) values back to scalar +1 (at two full turns). This cycle repeats every 2 turns. After turns (integer ), without any intermediate untangling attempts, the strings/bands can be partially untangled back to the turns state with each application of the same procedure used in untangling from 2 turns to 0 turns. Applying the same procedure times will take a -tangled object back to the untangled or 0 turn state. The untangling process also removes any rotation-generated twisting about the strings/bands themselves. Simple 3D mechanical models can be used to demonstrate these facts.
Rodrigues vector.
The Rodrigues vector (sometimes called the Gibbs vector, with coordinates called Rodrigues parameters) can be expressed in terms of the axis and angle of the rotation as follows,
This representation is a higher-dimensional analog of the gnomonic projection, mapping unit quaternions from a 3-sphere onto the 3-dimensional pure-vector hyperplane.
It has a discontinuity at 180° ( radians): as any rotation vector tends to an angle of radians, its tangent tends to infinity.
A rotation followed by a rotation in the Rodrigues representation has the simple rotation composition form
Today, the most straightforward way to prove this formula is in the (faithful) doublet representation, where , etc.
The combinatoric features of the Pauli matrix derivation just mentioned are also identical to the equivalent quaternion derivation below. Construct a quaternion associated with a spatial rotation R as,
Then the composition of the rotation RB with RA is the rotation RC=RBRA, with rotation axis and angle defined by the product of the quaternions,
that is
Expand this quaternion product to 
Divide both sides of this equation by the identity resulting from the previous one,
and evaluate
 {1 - 
\tan\frac{\beta}{2}\tan\frac{\alpha}{2} \mathbf{B}\cdot \mathbf{A}}.
</math>
This is Rodrigues' formula for the axis of a composite rotation defined in terms of the axes of the two component rotations. He derived this formula in 1840 (see page 408). The three rotation axes A, B, and C form a spherical triangle and the dihedral angles between the planes formed by the sides of this triangle are defined by the rotation angles.
Modified Rodrigues parameters (MRPs) can be expressed in terms of Euler axis and angle by
The modified Rodrigues vector is a stereographic projection mapping unit quaternions from a 3-sphere onto the 3-dimensional pure-vector hyperplane.
Cayley–Klein parameters.
See definition at Wolfram Mathworld.
Vector transformation law.
Active rotations of a 3D vector p in Euclidean space around an axis n over an angle η can be easily written in terms of dot and cross products as follows:
wherein
is the longitudinal component of p along n, given by the dot product,
is the transverse component of p with respect to n, and
is the cross product, of p with n.
The above formula shows that the longitudinal component of p remains unchanged, whereas the transverse portion of p is rotated in the plane perpendicular to n. This plane is spanned by the transverse portion of p itself and a direction perpendicular to both p and n. The rotation is directly identifiable in the equation as a 2D rotation over an angle η.
Passive rotations can be described by the same formula, but with an inverse sign of either η or n.
Conversion formulae between formalisms.
Rotation matrix ↔ Euler angles.
The Euler angles can be extracted from the rotation matrix formula_32 by inspecting the rotation matrix in analytical form.
Rotation matrix → Euler angles ( extrinsic).
Using the -convention, the 3-1-3 extrinsic Euler angles , and (around the -axis, -axis and again the formula_33-axis) can be obtained as follows:
Note that is equivalent to where it also takes into account the quadrant that the point is in; see atan2.
When implementing the conversion, one has to take into account several situations:
Euler angles ( intrinsic) → rotation matrix.
The rotation matrix is generated from the 3-2-1 intrinsic Euler angles by multiplying the three matrices generated by rotations about the axes. 
The axes of the rotation depend on the specific convention being used. For the -convention the rotations are about the -, - and -axes with angles , and , the individual matrices are as follows:
This yields
Note: This is valid for a right-hand system, which is the convention used in almost all engineering and physics disciplines.
The interpretation of these right-handed rotation matrices is that they express coordinate transformations (passive) as opposed to point transformations (active). Because expresses a rotation from the local frame to the global frame (i.e., encodes the axes of frame w.r.t frame ), the elementary rotation matrices are composed as above. Because the inverse rotation is just the rotation transposed, if we wanted the global-to-local rotation from frame to frame , we would write formula_38.
Rotation matrix ↔ Euler axis/angle.
If the Euler angle is not a multiple of , the Euler axis and angle can be computed from the elements of the rotation matrix as follows:
Alternatively, the following method can be used:
Eigendecomposition of the rotation matrix yields the eigenvalues 1 and . The Euler axis is the eigenvector corresponding to the eigenvalue of 1, and can be computed from the remaining eigenvalues.
The Euler axis can be also found using singular value decomposition since it is the normalized vector spanning the null-space of the matrix .
To convert the other way the rotation matrix corresponding to an Euler axis and angle can be computed according to Rodrigues' rotation formula (with appropriate modification) as follows:
with the identity matrix, and
is the cross-product matrix.
This expands to:
Rotation matrix ↔ quaternion.
When computing a quaternion from the rotation matrix there is a sign ambiguity, since and represent the same rotation.
One way of computing the quaternion
from the rotation matrix is as follows:
There are three other mathematically equivalent ways to compute . Numerical inaccuracy can be reduced by avoiding situations in which the denominator is close to zero. One of the other three methods looks as follows:
The rotation matrix corresponding to the quaternion can be computed as follows:
where
which gives
or equivalently
Euler angles ↔ quaternion.
Euler angles ( extrinsic) → quaternion.
We will consider the -convention 3-1-3 extrinsic Euler angles for the following algorithm. The terms of the algorithm depend on the convention used.
We can compute the quaternion
from the Euler angles as follows:
Euler angles ( intrinsic) → quaternion.
A quaternion equivalent to yaw (), pitch () and roll () angles. or intrinsic Tait–Bryan angles following the convention, can be computed by
Quaternion → Euler angles ( extrinsic).
Given the rotation quaternion
the -convention 3-1-3 extrinsic Euler Angles can be computed by
Quaternion → Euler angles ( intrinsic).
Given the rotation quaternion
yaw, pitch and roll angles, or intrinsic Tait–Bryan angles following the convention, can be computed by
Euler axis–angle ↔ quaternion.
Given the Euler axis and angle , the quaternion
can be computed by
Given the rotation quaternion , define
Then the Euler axis and angle can be computed by
Conversion formulae for derivatives.
Rotation matrix ↔ angular velocities.
The angular velocity vector
can be extracted from the time derivative of the rotation matrix by the following relation:
The derivation is adapted from Ioffe as follows:
For any vector , consider and differentiate it:
The derivative of a vector is the linear velocity of its tip. Since is a rotation matrix, by definition the length of is always equal to the length of , and hence it does not change with time. Thus, when rotates, its tip moves along a circle, and the linear velocity of its tip is tangential to the circle; i.e., always perpendicular to . In this specific case, the relationship between the linear velocity vector and the angular velocity vector is 
(see circular motion and cross product).
By the transitivity of the abovementioned equations, 
which implies
Quaternion ↔ angular velocities.
The angular velocity vector
can be obtained from the derivative of the quaternion as follows:
where formula_69 is the conjugate (inverse) of formula_70.
Conversely, the derivative of the quaternion is
Rotors in a geometric algebra.
The formalism of geometric algebra (GA) provides an extension and interpretation of the quaternion method. Central to GA is the geometric product of vectors, an extension of the traditional inner and cross products, given by
where the symbol denotes the exterior product or wedge product. This product of vectors , and produces two terms: a scalar part from the inner product and a bivector part from the wedge product. This bivector describes the plane perpendicular to what the cross product of the vectors would return.
Bivectors in GA have some unusual properties compared to vectors. Under the geometric product, bivectors have a negative square: the bivector describes the -plane. Its square is . Because the unit basis vectors are orthogonal to each other, the geometric product reduces to the antisymmetric outer product – and can be swapped freely at the cost of a factor of −1. The square reduces to since the basis vectors themselves square to +1.
This result holds generally for all bivectors, and as a result the bivector plays a role similar to the imaginary unit. Geometric algebra uses bivectors in its analogue to the quaternion, the "rotor", given by
where is a unit bivector that describes the plane of rotation. Because squares to −1, the power series expansion of generates the trigonometric functions. The rotation formula that maps a vector to a rotated vector is then
where
is the "reverse" of formula_76 (reversing the order of the vectors in formula_77 is equivalent to changing its sign).
Example. A rotation about the axis
can be accomplished by converting to its dual bivector,
where is the unit volume element, the only trivector (pseudoscalar) in three-dimensional space. The result is
In three-dimensional space, however, it is often simpler to leave the expression for , using the fact that commutes with all objects in 3D and also squares to −1. A rotation of the vector in this plane by an angle is then
Recognizing that
and that is the reflection of about the plane perpendicular to gives a geometric interpretation to the rotation operation: the rotation preserves the components that are parallel to and changes only those that are perpendicular. The terms are then computed:
The result of the rotation is then
A simple check on this result is the angle . Such a rotation should map to . Indeed, the rotation reduces to
exactly as expected. This rotation formula is valid not only for vectors but for any multivector. In addition, when Euler angles are used, the complexity of the operation is much reduced. Compounded rotations come from multiplying the rotors, so the total rotor from Euler angles is
but
These rotors come back out of the exponentials like so:
where refers to rotation in the original coordinates. Similarly for the rotation,
Noting that and commute (rotations in the same plane must commute), and the total rotor becomes
Thus, the compounded rotations of Euler angles become a series of equivalent rotations in the original fixed frame.
While rotors in geometric algebra work almost identically to quaternions in three dimensions, the power of this formalism is its generality: this method is appropriate and valid in spaces with any number of dimensions. In 3D, rotations have three degrees of freedom, a degree for each linearly independent plane (bivector) the rotation can take place in. It has been known that pairs of quaternions can be used to generate rotations in 4D, yielding six degrees of freedom, and the geometric algebra approach verifies this result: in 4D, there are six linearly independent bivectors that can be used as the generators of rotations.
Angle-Angle-Angle.
Rotations can be modeled as an axis and an angle; as illustrated with a gyroscope which has an axis through the rotor, and the amount of spin around that axis demonstrated by the rotation of the rotor; this rotation can be expressed as formula_91 where axis is a unit vector specifying the direction of the rotor axis. From the origin, in any direction, is the same rotation axis, with the scale of the angle equivalent to the distance from the origin. From any other point in space, similarly the same direction vector applied relative to the orientation represented by the starting point rather than the origin applies the same change around the same axes that the unit vector specifies. The formula_92 scaling each point gives a unique coordinate in Angle-Angle-Angle notation. The difference between two coordinates immediately yields the single axis of rotation and angle between the two orientations.
The natural log of a quaternion represents curving space by 3 angles around 3 axles of rotation, and is expressed in arc-length; similar to Euler angles, but order independent. There is a Lie product formula definition of the addition of rotations, which is that they are 
sum of infinitesimal steps of each rotation applied in series; this would imply that rotations are the result of all rotations
in the same instant are applied, rather than a series of rotations applied subsequently.
The axes of rotation are aligned to the standard cartesian formula_93 axes. These rotations may be simply added and subtracted, especially when the frames being rotated are fixed to each other as in IK chains. Differences between two objects that are in the same reference frame are found by simply subtracting their orientations. Rotations that are applied from external sources, or are from sources relative to the current rotation still require multiplications, application of the Rodriguez Formula is provided.
The rotation from each axle coordinate represent rotating the plane perpendicular to the specified axis simultaneously with all other axles. Although the measures can be considered in angles, the representation is actually the arc-length of the curve; an angle implies a rotation around a point, where a curvature is a delta applied to the current point in an inertial direction.
Just an observational note: log quaternions have rings, or octaves of rotations; that is for rotations greater than 4formula_94 have related curves. Curvatures of things that approach this boundary appear to chaotically jump orbits.
For 'human readable' angles the 1-norm can be used to rescale the angles to look more 'appropriate'; much like Celsius might be considered more correct than Fahrenheit.
Other related values are immediately derivable:
The total angle of rotation...
The axis of rotation...
Basis Matrix Computation.
This was built from rotating the vectors (1,0,0), (0,1,0), (0,0,1), and reducing constants.
Given an input formula_100
Which are used to compute the resulting matrix...
Alternate Basis Calculation.
Alternatively this can be used
given: formula_103
convert to angle-axis formula_104, and formula_105 = formula_106
Compute some partial expressions:formula_107
Compute the resulting matrix: formula_108
Expanded : formula_109
Vector Rotation.
Rotate the vector formula_110 around the rotation vector formula_111.
The angle of rotation will be formula_112.
Calculate the cosine of the angle times the vector to rotate, plus sine of the angle times the axis of rotation, plus one minus cosine of the angle times the dot product of the vector and rotation axis times the axis of rotation.
Some notes: the dot product includes the cosine of the angle between the vector being rotated and the axis of rotation times the length of V; the cross product includes the sine of the angle between the vector being rotated and the axis of rotation.
Rotate a Rotation Vector.
Using Rodrigues Composite Rotation Formula
For a given rotation vector formula_114, and another rotation vector formula_115 to rotate the frame around.
From the initial rotation vectors, extract the angles and axes:
Normalized axis of rotation for the current frame:
Normalized axis of rotation to rotate the frame around:
The result angle angle of the rotation is
The result, un-normalized axis of rotation:
The Rodrigues Rotation Formula would lead that the sin of above resulting angle can be used to normalize the vector, however this fails for large ranges; so normalize the result axis as any other vector.
And the final frame rotation coordinate:
Spin rotation around a fixed axis.
A rotation vector formula_126 represents three axes; these may be used as a shorthand to rotate the rotation around using the above 'Rotate a Rotation Vector'. These expressions are best represented as code fragments.
Setup some constants used in other expressions.
 const nx = Q.x / Q.length;
 const ny = Q.y / Q.length;
 const nz = Q.z / Q.length;
 const angle = Q.length;
 const s = Math.sin( angle ); 
 const c1 = Math.cos( angle );
 const c = 1-c1;
using the above values...
 // compute the 'x' for the frame this rotation vector represents
 const cnx = c * nx
 xAxis = { x : cnx*nx + c1
 , y : cnx*ny + s*nz
or
 // compute the 'y' for the frame this rotation vector represents
 const cny = c * ny; 
 yAxis = { x : (cny*nx) - s*nz
 , y : (cny*ny) + c1
or
 // compute the 'z' for the frame this rotation vector represents
 const cnz = c * nz;
 zAxis = { x : ( cnz*nx ) + s*ny
 , y : ( cnz*ny ) - s*nx
Conversion from Basis Matrix.
Compute the determinant of the matrix... 
Convert to the angle of rotation...
compute the normal factor...
the resulting angle-angle-angle:
Conversion from Normal Vector(Y).
Representation of a normal as a rotation, this assumes that the vector formula_136 is 'up'. If some other axle is considered primary, the coordinates can be simply swapped.
This assume a normalized input vector in the direction of the normal
The angle is simply the sum of the x/z coordinate(or y,x if Z is 'up', or y,z if X is 'up')...
if angle is 0, the job is done, result with formula_139
Some temporary values; these values are just partials referenced later...
Use the projected normal on the Y axis as the angle to rotate...
Align Normal using Basis.
The default tangent and bi-tangent of rotations which only have their normal set, results in tangents and bi-tangents that are irregular. Alternatively build a basis matrix, and convert from basis using the above mentioned method. 
Compute the normal of the above, and the matrix to convert...
And then use the basis to log quaternion conversion...
Align Normal Directly.
Or This is the direct computation to result with a log quaternion; compute the above result vector and then...
This is the angle
These partial products are used below...
Compute the normalized rotation vector (axle of rotation)...
and finally compute the resulting log quaternion.
Conversion from axis-angle.
This assumes the input axis formula_151 is normalized. If there is 0 rotation, result with formula_139