Euler angles describe an orientation by chopping it into three rotations about the coordinate axes, and their problems are born of that chopping. In this lesson we take the opposite route: describing any rotation as a single turn about a well-chosen axis. This is neither an approximation nor a special case: a theorem guarantees that every 3D rotation is exactly that. The resulting representation—an axis and an angle—is the closest to how we think about a rotation ("the ship does a barrel roll about its longitudinal axis", "the satellite orbits about this tilted axis"), and Rodrigues' formula lets us apply it directly to a vector with the tools of Module 3: a projection and a cross product. It is also the natural bridge to the quaternions of the next lesson.
The code assumes that normalize, norm, dot_product, cross_product and project (Module 3); affine_matrix, transform_point, about_pivot and the matrices rotation_matrix_x/y/z (Module 2); matrix_to_euler (previous lesson), and the data ship_orientation, ship = (8, 3, 6) and lamp = (0, 12, 0) are already defined.
Euler's rotation theorem
Euler's rotation theorem. Every rotation of three-dimensional space that leaves one point fixed is a turn through some angle about an axis that passes through that point.
Put another way: however many rotations we chain together—a yaw, then a pitch, then a roll, then twenty more—the final result can always be reached with a single turn. There is a line of points that, after the whole dance, end up where they started.
We had already come across this fact in "Eigenvalues and Eigenvectors": a 3D rotation matrix always has the eigenvalue \(\lambda = 1\), and its eigenvector is the axis of rotation, because \(R,\mathbf{k} = \mathbf{k}\) means precisely that the rotation leaves the direction \(\mathbf{k}\) untouched. Now we can justify why that eigenvalue always exists. A rotation satisfies \(R,R^T = I\) and \(\det R = 1\), so:
\[ \det(R - I) = \det(R - R,R^T) = \det!\big(R,(I - R^T)\big) = \det R \cdot \det(I - R^T) = \det(I - R) \]
(in the last step, \(\det R = 1\) and a matrix has the same determinant as its transpose). But \(I - R = -(R - I)\), and changing the sign of a 3×3 matrix multiplies its determinant by \((-1)^3 = -1\). Therefore \(\det(R - I) = -\det(R - I)\), which forces \(\det(R - I) = 0\): the number 1 is a root of the characteristic polynomial. The argument depends on the dimension being odd; in 2D it does not work, and that is why a rotation in the plane leaves no direction fixed.
Axis-angle representation and the rotation vector
The theorem suggests a way of describing a rotation with two pieces of data:
- an axis: a unit vector \(\mathbf{k}\) that gives the direction of the line about which we rotate;
- an angle \(\theta\): how far we rotate, with the direction given by the right-hand rule (thumb along \(\mathbf{k}\), the fingers mark the positive rotation).
A unit vector is two degrees of freedom and the angle is the third: the three a rotation must have.
The basic matrices of Module 2 are special cases: \(R_x(\theta)\) is the pair \(\big((1,0,0),\ \theta\big)\), and the ship's orientation, \(N = R_y(53.13°)\), is the pair \(\big((0,1,0),\ 53.13°\big)\).
The representation is not unique, although its duplications are far more innocent than Euler's:
| Equivalent pairs | Reason |
|---|---|
| \((\mathbf{k}, \theta)\) and \((-\mathbf{k}, -\theta)\) | Reversing the axis reverses the positive direction |
| \((\mathbf{k}, \theta)\) and \((\mathbf{k}, \theta + 360°)\) | One extra full turn does not change the result |
| \((\mathbf{k}, 180°)\) and \((-\mathbf{k}, 180°)\) | A half turn is the same in both directions |
| \((\text{any } \mathbf{k},\ 0°)\) | With no rotation there is no defined axis |
The usual practice is to normalize to an angle between 0° and 180°, choosing the direction of the axis that makes it positive.
Rotation vector. Since \(\mathbf{k}\) is a unit vector, the two pieces of data can be packed into a single vector of three components, \(\boldsymbol{\theta} = \theta,\mathbf{k}\): its direction is the axis and its length is the angle (in radians). It is the most compact form possible, and it is the form in which the angular velocity of a spinning body is expressed, as we will see in "Physics Simulation". It does have a catch, though: although it looks like an ordinary vector, rotation vectors cannot be added to compose rotations (unless they share an axis).
Deriving Rodrigues' formula
We want to rotate a vector \(\mathbf{v}\) through an angle \(\theta\) about a unit axis \(\mathbf{k}\) that passes through the origin. The idea is to split \(\mathbf{v}\) into two parts that behave very differently under the rotation, as we did with the ship's velocity in "Dot Product and Cross Product":
\[ \mathbf{v} = \mathbf{v}\parallel + \mathbf{v}\perp \qquad \mathbf{v}\parallel = (\mathbf{v} \cdot \mathbf{k}),\mathbf{k} \qquad \mathbf{v}\perp = \mathbf{v} - \mathbf{v}_\parallel \]
Step 1: the parallel part does not move. \(\mathbf{v}_\parallel\) lies on the axis, and the points of the axis are precisely the ones the rotation leaves still.
Step 2: the perpendicular part rotates in a plane. \(\mathbf{v}\perp\) lives in the plane perpendicular to the axis, and there the rotation is a plain old 2D rotation. To write it down we need two perpendicular axes within that plane. The first is \(\mathbf{v}\perp\) itself. The second is supplied by the cross product:
\[ \mathbf{w} = \mathbf{k} \times \mathbf{v} \]
This vector is perpendicular to \(\mathbf{k}\) (it lies in the plane of rotation) and perpendicular to \(\mathbf{v}\perp\). It is also the same length as \(\mathbf{v}\perp\): since \(\mathbf{k} \times \mathbf{v}\parallel = \mathbf{0}\) (they are parallel), \(\mathbf{w} = \mathbf{k} \times \mathbf{v}\perp\), the product of a unit vector and another one perpendicular to it, whose magnitude is \(1 \cdot \lVert \mathbf{v}\perp \rVert \cdot \sin 90°\). In short, \(\mathbf{w}\) is \(\mathbf{v}\perp\) rotated a quarter turn about \(\mathbf{k}\) in the positive direction.
With those two axes, rotating \(\mathbf{v}_\perp\) through an angle \(\theta\) is the same as rotating \((1, 0)\) in the plane to obtain \((\cos\theta, \sin\theta)\): so much cosine along the starting axis, so much sine along the axis that is a quarter turn ahead.
\[ \mathbf{v}\perp' = \cos\theta\ \mathbf{v}\perp + \sin\theta\ (\mathbf{k} \times \mathbf{v}) \]
Step 3: add them up. The rotated vector is the part that does not move plus the part that rotates:
\[ \mathbf{v}' = \mathbf{v}\parallel + \cos\theta\ \mathbf{v}\perp + \sin\theta\ (\mathbf{k} \times \mathbf{v}) \]
Substituting \(\mathbf{v}_\perp = \mathbf{v} - (\mathbf{v} \cdot \mathbf{k}),\mathbf{k}\) and collecting terms gives the form in which Rodrigues' formula is usually quoted:
\[ \boxed{\ \mathbf{v}' = \cos\theta\ \mathbf{v} + \sin\theta\ (\mathbf{k} \times \mathbf{v}) + (1 - \cos\theta),(\mathbf{k} \cdot \mathbf{v}),\mathbf{k}\ } \]
Three terms, each with its own role: what remains of the original vector, the sideways push of the rotation and the correction that puts the part parallel to the axis back in its place. Two quick checks: with \(\theta = 0\) we get \(\mathbf{v}' = \mathbf{v}\), and if \(\mathbf{v}\) lies on the axis (\(\mathbf{v} = a,\mathbf{k}\)), the cross product vanishes and we are left with \(\cos\theta,\mathbf{v} + (1 - \cos\theta),\mathbf{v} = \mathbf{v}\).
Example: the ship orbits about a tilted axis
Until now the ship only knew how to orbit about the lighthouse's vertical with \(R_y\). The designers of "Stellar Lighthouse" want a tilted orbit: the axis passes through the base of the lighthouse (the origin) and has direction
\[ \mathbf{k} = (0,\ 0.8,\ 0.6) \]
(a unit vector: \(0.64 + 0.36 = 1\); it is tilted about 36.87° from the vertical toward \(+Z\)). Where is the ship, which starts at \(\mathbf{v} = (8, 3, 6)\), after a quarter of an orbit (\(\theta = 90°\))?
- Component along the axis: \(\mathbf{k} \cdot \mathbf{v} = 0 + 2.4 + 3.6 = 6\). Parallel part: \(\mathbf{v}_\parallel = 6,\mathbf{k} = (0,\ 4.8,\ 3.6)\).
- Perpendicular part: \(\mathbf{v}_\perp = (8, 3, 6) - (0, 4.8, 3.6) = (8,\ -1.8,\ 2.4)\).
- Cross product:
\[ \mathbf{k} \times \mathbf{v} = \begin{pmatrix} 0.8 \cdot 6 - 0.6 \cdot 3 \ 0.6 \cdot 8 - 0 \cdot 6 \ 0 \cdot 3 - 0.8 \cdot 8 \end{pmatrix} = \begin{pmatrix} 3 \ 4.8 \ -6.4 \end{pmatrix} \]
- With \(\cos 90° = 0\) and \(\sin 90° = 1\): \(\mathbf{v}' = \mathbf{v}_\parallel + \mathbf{k} \times \mathbf{v} = (0, 4.8, 3.6) + (3, 4.8, -6.4) = (3,\ 9.6,\ -2.8)\).
Checks: \(\lVert \mathbf{v}_\perp \rVert^2 = 64 + 3.24 + 5.76 = 73\) and \(\lVert \mathbf{k} \times \mathbf{v} \rVert^2 = 9 + 23.04 + 40.96 = 73\) (they are the same length, as they had to be: \(\sqrt{73} \approx 8.54\) m is the radius of the orbit). And the distance to the lighthouse is preserved: \(9 + 92.16 + 7.84 = 109 = 64 + 9 + 36\).
import numpy as np
def rotate_axis_angle(v, axis, angle):
"""Rotate the vector v by `angle` radians about `axis` (which passes through the origin)."""
v = np.asarray(v, dtype=float)
k = normalize(axis)
parallel = project(v, k) # does not move
perpendicular = v - parallel # rotates in the plane perpendicular to the axis
return (parallel + np.cos(angle) * perpendicular
+ np.sin(angle) * cross_product(k, v))
orbit_axis = np.array([0.0, 0.8, 0.6])
for degrees in (0.0, 90.0, 180.0, 270.0, 360.0):
print(degrees, np.round(rotate_axis_angle(ship, orbit_axis, np.radians(degrees)), 4) + 0.0)The function follows the derivation literally: project obtains the parallel part, the subtraction gives the perpendicular one and cross_product supplies the axis "a quarter turn ahead". We normalize the axis inside the function so that the caller does not have to worry about it. Along the orbit the ship climbs to a height of 9.6 m and drops to the level of the lighthouse's base: it is a tilted circle.
The skew-symmetric matrix of the cross product
Rodrigues' formula rotates a vector. To obtain a rotation matrix (one that can be composed with the others in the engine) each term has to be written as "matrix times \(\mathbf{v}\)". The only one that resists is the cross product, but it too is a linear transformation of \(\mathbf{v}\) (remember: it is distributive and scalars factor out), so it has a matrix. Expanding \(\mathbf{k} \times \mathbf{v}\) with \(\mathbf{k} = (k_x, k_y, k_z)\):
\[ \mathbf{k} \times \mathbf{v} = \begin{pmatrix} k_y v_z - k_z v_y \ k_z v_x - k_x v_z \ k_x v_y - k_y v_x \end{pmatrix} = \underbrace{\begin{pmatrix} 0 & -k_z & k_y \ k_z & 0 & -k_x \ -k_y & k_x & 0 \end{pmatrix}}{K\ =\ [\mathbf{k}]\times} \begin{pmatrix} v_x \ v_y \ v_z \end{pmatrix} \]
The matrix \(K\), also written \([\mathbf{k}]_\times\), is skew-symmetric: \(K^T = -K\). Its diagonal is zero and each entry is the negative of its mirror image. Multiplying by \(K\) is "taking the cross product with \(\mathbf{k}\) on the left".
And \(K^2\)? Applying \(K\) twice is \(\mathbf{k} \times (\mathbf{k} \times \mathbf{v})\). We already know that \(\mathbf{k} \times \mathbf{v}\) is \(\mathbf{v}_\perp\) rotated a quarter turn; repeating the operation rotates it another quarter, and a half turn within the plane is a change of sign:
\[ K^2,\mathbf{v} = -\mathbf{v}_\perp \]
That is, \(K^2\) extracts the part perpendicular to the axis, with its sign changed. (In matrix form, \(K^2 = \mathbf{k}\mathbf{k}^T - I\): the negative of the matrix \(I - \mathbf{k}\mathbf{k}^T\) that projects onto the plane perpendicular to \(\mathbf{k}\) in "Distances and Geometric Projections".)
Matrix form of Rodrigues' formula
We start from the version in parts, \(\mathbf{v}' = \mathbf{v}\parallel + \cos\theta,\mathbf{v}\perp + \sin\theta,(\mathbf{k} \times \mathbf{v})\), and write \(\mathbf{v}\parallel = \mathbf{v} - \mathbf{v}\perp\):
\[ \mathbf{v}' = \mathbf{v} + \sin\theta,(\mathbf{k} \times \mathbf{v}) - (1 - \cos\theta),\mathbf{v}_\perp = \mathbf{v} + \sin\theta,K\mathbf{v} + (1 - \cos\theta),K^2\mathbf{v} \]
Factoring out \(\mathbf{v}\) leaves the matrix of the rotation:
\[ \boxed{\ R(\mathbf{k}, \theta) = I + \sin\theta\ K + (1 - \cos\theta)\ K^2\ } \]
Expanded entry by entry, with \(c = \cos\theta\), \(s = \sin\theta\), \(t = 1 - \cos\theta\) and \(\mathbf{k} = (x, y, z)\):
\[ R = \begin{pmatrix} c + t,x^2 & t,xy - s,z & t,xz + s,y \\ t,xy + s,z & c + t,y^2 & t,yz - s,x \\ t,xz - s,y & t,yz + s,x & c + t,z^2 \end{pmatrix} \]
Notice its structure: a symmetric part (the terms with \(c\) and \(t\), which come from \(I\) and from \(K^2\)) and a skew-symmetric part (the terms with \(s\), which come from \(K\)). That separation is the key to making the return journey a little further down.
Example. For the orbit axis, \(\mathbf{k} = (0, 0.8, 0.6)\), and \(\theta = 90°\) (\(s = 1\), \(t = 1\)):
\[ K = \begin{pmatrix} 0 & -0.6 & 0.8 \ 0.6 & 0 & 0 \ -0.8 & 0 & 0 \end{pmatrix} \quad K^2 = \begin{pmatrix} -1 & 0 & 0 \ 0 & -0.36 & 0.48 \ 0 & 0.48 & -0.64 \end{pmatrix} \quad R = I + K + K^2 = \begin{pmatrix} 0 & -0.6 & 0.8 \ 0.6 & 0.64 & 0.48 \ -0.8 & 0.48 & 0.36 \end{pmatrix} \]
And \(R,(8, 3, 6)^T = (-1.8 + 4.8,\ 4.8 + 1.92 + 2.88,\ -6.4 + 1.44 + 2.16) = (3,\ 9.6,\ -2.8)\), the same point as before.
def skew_symmetric_matrix(k):
"""Matrix K such that K @ v = cross_product(k, v)."""
x, y, z = k
return np.array([[0.0, -z, y],
[z, 0.0, -x],
[-y, x, 0.0]])
def axis_angle_matrix(axis, angle):
"""3x3 rotation matrix of `angle` radians about `axis` (Rodrigues)."""
k_mat = skew_symmetric_matrix(normalize(axis))
return np.eye(3) + np.sin(angle) * k_mat + (1.0 - np.cos(angle)) * (k_mat @ k_mat)
quarter_orbit = axis_angle_matrix(orbit_axis, np.radians(90.0))
print(np.round(quarter_orbit, 4) + 0.0)
print(np.round(quarter_orbit @ ship, 4))
print(np.allclose(quarter_orbit.T @ quarter_orbit, np.eye(3)),
round(float(np.linalg.det(quarter_orbit)), 6))Like euler_to_matrix, the function returns a 3×3 matrix; when we need it in 4×4 format we will extend it with affine_matrix(r, [0, 0, 0]). When should each version be used? To rotate one vector, rotate_axis_angle is more direct; to rotate many points with the same rotation or to compose it with other transformations, you build the matrix once and reuse it.
Checking against the coordinate axes
If the formula is correct, using \(X\), \(Y\) or \(Z\) as the axis must give back the basic matrices. Let's try the Y axis, \(\mathbf{k} = (0, 1, 0)\):
\[ K = \begin{pmatrix} 0 & 0 & 1 \ 0 & 0 & 0 \ -1 & 0 & 0 \end{pmatrix} \qquad K^2 = \begin{pmatrix} -1 & 0 & 0 \ 0 & 0 & 0 \ 0 & 0 & -1 \end{pmatrix} \]
\[ I + \sin\theta,K + (1 - \cos\theta),K^2 = \begin{pmatrix} 1 - (1 - \cos\theta) & 0 & \sin\theta \ 0 & 1 & 0 \ -\sin\theta & 0 & 1 - (1 - \cos\theta) \end{pmatrix} = \begin{pmatrix} \cos\theta & 0 & \sin\theta \ 0 & 1 & 0 \ -\sin\theta & 0 & \cos\theta \end{pmatrix} = R_y(\theta) \]
Even the "backwards" sign of \(R_y\) shows up, without our having to think about it. The other two axes are checked in the same way; with code:
angle = np.radians(37.0)
print(np.allclose(axis_angle_matrix([1.0, 0.0, 0.0], angle), rotation_matrix_x(angle)[:3, :3]),
np.allclose(axis_angle_matrix([0.0, 1.0, 0.0], angle), rotation_matrix_y(angle)[:3, :3]),
np.allclose(axis_angle_matrix([0.0, 0.0, 1.0], angle), rotation_matrix_z(angle)[:3, :3]))The three matrices of Module 2 were, then, three special cases of a single formula.
From a matrix to axis-angle
Given any rotation matrix, what is its axis and what is its angle? We could compute the eigenvector for \(\lambda = 1\), but the structure of Rodrigues' formula offers a shortcut.
The angle, from the trace. The trace of a matrix is the sum of its diagonal. In \(R = I + \sin\theta,K + (1 - \cos\theta),K^2\): the trace of \(I\) is 3, that of \(K\) is 0 and that of \(K^2 = \mathbf{k}\mathbf{k}^T - I\) is \(1 - 3 = -2\). Therefore:
\[ \text{trace}(R) = 3 - 2,(1 - \cos\theta) = 1 + 2\cos\theta \qquad\Longrightarrow\qquad \theta = \arccos!\left(\frac{\text{trace}(R) - 1}{2}\right) \]
arccos returns an angle between 0° and 180°: exactly the normalization we wanted.
The axis, from the skew-symmetric part. Subtracting from \(R\) its transpose cancels the symmetric part and doubles the skew-symmetric one:
\[ R - R^T = 2\sin\theta\ K \qquad\Longrightarrow\qquad \mathbf{k} = \frac{1}{2\sin\theta}\begin{pmatrix} r_{21} - r_{12} \ r_{02} - r_{20} \ r_{10} - r_{01} \end{pmatrix} \]
(indices from 0; just read off in \(K\) where each component of \(\mathbf{k}\) sits).
Example. For the quarter-orbit matrix: trace \(= 0 + 0.64 + 0.36 = 1\), so \(\cos\theta = 0\) and \(\theta = 90°\). Axis: \(\frac{1}{2},(0.48 - 0.48,\ 0.8 + 0.8,\ 0.6 + 0.6) = (0,\ 0.8,\ 0.6)\). We recover the data we started with.
Special cases. The formula for the axis divides by \(\sin\theta\), which vanishes in two situations:
| Case | What happens | What to do |
|---|---|---|
| \(\theta = 0°\) | \(R = I\): there is no rotation and any axis will do | Return an arbitrary axis (for example, Y) and angle 0 |
| \(\theta = 180°\) | \(R\) is symmetric: \(R - R^T = 0\), even though the axis is perfectly well defined | Use the symmetric part: \(R = I + 2K^2 = 2,\mathbf{k}\mathbf{k}^T - I\), so \(\mathbf{k}\mathbf{k}^T = (R + I)/2\) |
In the half-turn case, each column of \(\mathbf{k}\mathbf{k}^T\) is \(\mathbf{k}\) multiplied by one of its components; it is enough to take the column corresponding to the largest component (the largest entry on the diagonal) and normalize it. The sign of the axis makes no difference, because \((\mathbf{k}, 180°)\) and \((-\mathbf{k}, 180°)\) are the same rotation.
def matrix_to_axis_angle(r, tolerance=1e-6):
"""Return (unit axis, angle in [0, pi]) for a 3x3 or 4x4 rotation matrix."""
r = np.asarray(r, dtype=float)[:3, :3]
cosine = np.clip((np.trace(r) - 1.0) / 2.0, -1.0, 1.0)
angle = float(np.arccos(cosine))
if angle < tolerance: # no rotation: any axis will do
return np.array([0.0, 1.0, 0.0]), 0.0
if np.pi - angle < tolerance: # half turn: R = 2 k k^T - I
b = (r + np.eye(3)) / 2.0 # b = k k^T
i = int(np.argmax(np.diag(b))) # most reliable column
return normalize(b[:, i]), angle
axis = np.array([r[2, 1] - r[1, 2], r[0, 2] - r[2, 0], r[1, 0] - r[0, 1]])
return axis / (2.0 * np.sin(angle)), angle
for name, m in (("orbit", quarter_orbit),
("ship", ship_orientation),
("half turn", axis_angle_matrix(orbit_axis, np.pi))):
axis, angle = matrix_to_axis_angle(m)
print(name, np.round(axis, 4) + 0.0, round(np.degrees(angle), 2))The second line unmasks \(N\) for the third time in the course, this time without having to recognize any pattern by eye: Y axis, 53.13°. This function works for any rotation matrix, wherever it comes from.
Rotation about an axis that does not pass through the origin: the barrel roll
Everything so far assumes an axis that passes through the origin. But the ship's signature maneuver, the barrel roll—one full turn of roll about its own longitudinal axis—has an axis that passes through the center of the ship, \((8, 3, 6)\), with direction \(n_3 = (0.8, 0, 0.6)\). The solution is the pivot recipe from "Composition of Transformations": take the pivot to the origin, rotate and undo the translation.
\[ M = T(\mathbf{p})\ R(\mathbf{k}, \theta)\ T(-\mathbf{p}) \qquad\text{or, without matrices:}\qquad \mathbf{q}' = \mathbf{p} + \text{rotate}\big(\mathbf{q} - \mathbf{p}\big) \]
Any point on the axis will do as the pivot \(\mathbf{p}\).
By hand. The tip of the right wing is at \((10.4, 3, 2.8)\), that is, \(4,n_1 = (2.4, 0, -3.2)\) from the center. We rotate that vector 30° about \(n_3\). Since the wing is perpendicular to the longitudinal axis, there is no parallel part (\(n_3 \cdot 4n_1 = 0\)), and the cross product is \(n_3 \times 4,n_1 = 4,n_2 = (0, 4, 0)\), by the cyclic rule of the ship basis (\(n_1 \to n_2 \to n_3 \to n_1\)). Then:
\[ \cos 30° \cdot (2.4,\ 0,\ -3.2) + \sin 30° \cdot (0,\ 4,\ 0) = (2.078,\ 2,\ -2.771) \]
and the wing tip ends up at \((8, 3, 6) + (2.078, 2, -2.771) = (10.078,\ 5,\ 3.229)\): it has risen 2 m. A positive roll raises the right wing, as we established in the previous lesson.
n3 = ship_orientation[:, 2] # longitudinal axis
right_wing = np.array([10.4, 3.0, 2.8])
nose = np.array([9.6, 3.0, 7.2])
for degrees in (30.0, 90.0, 180.0):
rotation = affine_matrix(axis_angle_matrix(n3, np.radians(degrees)), [0.0, 0.0, 0.0])
barrel_roll = about_pivot(rotation, ship)
print(degrees, np.round(transform_point(barrel_roll, right_wing), 4) + 0.0,
np.round(transform_point(barrel_roll, nose), 4))At 90° the wing points up (it is directly above the center, 4 m higher); at 180° it occupies the place where the tip of the left wing used to be, \((5.6, 3, 9.2)\): the ship is flying upside down. The nose, which lies on the axis, does not move at any point.
And the ship's orientation during the barrel roll? It is the rotation of the barrel roll applied after \(N\). Here the rule from the previous lesson about world axes and the object's own axes reappears: rotating about the axis \(n_3\) (expressed in the world) means multiplying on the left; rotating about the local Z axis means multiplying on the right. The two must agree:
thirty = np.radians(30.0)
left_multiplied = axis_angle_matrix(n3, thirty) @ ship_orientation
right_multiplied = ship_orientation @ rotation_matrix_z(thirty)[:3, :3]
print(np.allclose(left_multiplied, right_multiplied))
print(np.round(np.degrees(matrix_to_euler(left_multiplied)), 2) + 0.0)In Euler angles, the barrel roll is simply the roll growing from 0° to 360°, with the yaw and the pitch untouched.
Limitations
The axis-angle representation is intuitive, compact and free of gimbal lock. Why not use it for everything?
Composing is not direct. Given two rotations \((\mathbf{k}_1, \theta_1)\) and \((\mathbf{k}_2, \theta_2)\), Euler's theorem guarantees that their composition is another rotation \((\mathbf{k}_3, \theta_3)\), but there is no simple rule for obtaining it: neither the axes nor the angles add up. You have to convert both to matrices, multiply and extract the result.
composite = axis_angle_matrix([0.0, 1.0, 0.0], np.radians(90.0)) @ axis_angle_matrix([1.0, 0.0, 0.0], np.radians(90.0))
axis, angle = matrix_to_axis_angle(composite)
print(np.round(axis, 4), round(np.degrees(angle), 2))A 90° pitch followed by a 90° yaw is equivalent to a rotation of 120° about the diagonal \((1, 1, -1)/\sqrt{3}\). Nothing in the starting data (two coordinate axes, two right angles) gave any hint of it.
| Aspect | Axis-angle |
|---|---|
| Memory | 4 numbers (3 as a rotation vector) |
| Readability | Very good for a single rotation |
| Applying it to a vector | Direct (Rodrigues), with sines and cosines on every call |
| Composing two rotations | Indirect: you have to go through matrices |
| Delicate cases | \(\theta = 0\) (undefined axis) and \(\theta = 180°\) (special extraction) |
| Interpolating | Easy between rotations with the same axis (just interpolate the angle); not obvious in general |
The ideal would be a representation with the same geometric content—an axis and an angle—but with a multiplication rule of its own, simple and free of trigonometric functions, for composing rotations directly. It exists, and it is the subject of the next lesson.
Common Mistakes and Tips
- Forgetting to normalize the axis. With a non-unit axis Rodrigues' formula does not rotate: it distorts. The
mini3dfunctions normalize internally; if you write the formula by hand, check that \(\lVert \mathbf{k} \rVert = 1\). - Reversing the order of the cross product. It is \(\mathbf{k} \times \mathbf{v}\), not \(\mathbf{v} \times \mathbf{k}\). With the order swapped the rotation goes the opposite way.
- Adding rotation vectors to compose rotations. It is only valid if they share an axis.
- Applying the general extraction near 0° or 180°. You divide by a sine that is almost zero and the axis comes out with a large error. Handle those cases separately, as
matrix_to_axis_angledoes. - Forgetting the pivot.
rotate_axis_angleandaxis_angle_matrixrotate about an axis that passes through the origin. For an axis that passes through another point, subtract the point, rotate and add it back (or useabout_pivot). - Expecting angles greater than 180° from the extraction. The trace cannot tell 200° from 160°; the function returns 160° with the axis reversed, which is the same rotation.
- Tip. Check any rotation with the two cheap invariants: the component along the axis (\(\mathbf{k} \cdot \mathbf{v}\)) and the norm must not change.
Exercises
Exercise 1. The ship, at \((8, 3, 6)\), completes half an orbit (180°) about the axis \(\mathbf{k} = (0, 0.8, 0.6)\) that passes through the base of the lighthouse. a) Simplify Rodrigues' formula for \(\theta = 180°\). b) Compute the final position by hand. c) Check that the component along the axis and the distance to the origin have not changed.
Exercise 2. In Exercise 1 of the previous lesson, the command (yaw 90°, pitch 0°, roll 90°) gave the matrix
\[ R = \begin{pmatrix} 0 & 0 & 1 \ 1 & 0 & 0 \ 0 & 1 & 0 \end{pmatrix} \]
a) Obtain its axis and its angle by hand. b) Check that the axis is an eigenvector with eigenvalue 1. c) Interpret the result geometrically: what does this rotation do to the X, Y and Z axes?
Exercise 3. A maintenance drone must rotate about a tilted axis with direction \((0, 0.8, 0.6)\) that passes through the lighthouse's lamp, \((0, 12, 0)\). a) If the drone is where the ship is, at \((8, 3, 6)\), where will it be after rotating 90°? Work it out by hand. b) Check it with about_pivot. c) How far is it from the lamp before and after?
Solutions
Solution 1.
a) With \(\cos 180° = -1\) and \(\sin 180° = 0\): \(\mathbf{v}' = -\mathbf{v} + 2,(\mathbf{k} \cdot \mathbf{v}),\mathbf{k} = 2,\mathbf{v}_\parallel - \mathbf{v}\). A half turn about an axis keeps the parallel part and reverses the perpendicular one.
b) \(\mathbf{v}_\parallel = (0, 4.8, 3.6)\), computed in the example. \(\mathbf{v}' = (0, 9.6, 7.2) - (8, 3, 6) = (-8,\ 6.6,\ 1.2)\).
c) \(\mathbf{k} \cdot \mathbf{v}' = 0.8 \cdot 6.6 + 0.6 \cdot 1.2 = 5.28 + 0.72 = 6\) ✓. \(\lVert \mathbf{v}' \rVert^2 = 64 + 43.56 + 1.44 = 109\) ✓. It matches the 180° row of the orbit table.
Solution 2.
a) Trace \(= 0\), so \(\cos\theta = (0 - 1)/2 = -0.5\) and \(\theta = 120°\). Skew-symmetric part: \((r_{21} - r_{12},\ r_{02} - r_{20},\ r_{10} - r_{01}) = (1 - 0,\ 1 - 0,\ 1 - 0) = (1, 1, 1)\). Dividing by \(2\sin 120° = \sqrt{3}\): \(\mathbf{k} = (1, 1, 1)/\sqrt{3} \approx (0.577, 0.577, 0.577)\).
b) \(R,(1, 1, 1)^T = (1, 1, 1)\): each row of \(R\) has a single 1, so the product adds one component from each. ✓
c) The columns of \(R\) are the images of the axes: X goes to \((0, 1, 0)\) (Y), Y goes to \((0, 0, 1)\) (Z) and Z goes to \((1, 0, 0)\) (X). The rotation cyclically permutes the three axes, \(X \to Y \to Z \to X\), and that is precisely a third of a turn about the diagonal of the cube, which is equidistant from all three. Three applications in a row put everything back in place: \(3 \times 120° = 360°\).
permutation = np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
axis, angle = matrix_to_axis_angle(permutation)
print(np.round(axis, 4), round(np.degrees(angle), 2))
print(np.allclose(axis_angle_matrix([1.0, 1.0, 1.0], np.radians(120.0)), permutation))Solution 3.
a) We work with the vector from the pivot: \(\mathbf{d} = (8, 3, 6) - (0, 12, 0) = (8, -9, 6)\).
- \(\mathbf{k} \cdot \mathbf{d} = -7.2 + 3.6 = -3.6\); \(\mathbf{d}_\parallel = -3.6,\mathbf{k} = (0,\ -2.88,\ -2.16)\).
- \(\mathbf{k} \times \mathbf{d} = (0.8 \cdot 6 - 0.6 \cdot (-9),\ 0.6 \cdot 8 - 0,\ 0 - 0.8 \cdot 8) = (10.2,\ 4.8,\ -6.4)\).
- With 90°: \(\mathbf{d}' = \mathbf{d}_\parallel + \mathbf{k} \times \mathbf{d} = (10.2,\ 1.92,\ -8.56)\).
- Undoing the translation: \((0, 12, 0) + (10.2, 1.92, -8.56) = (10.2,\ 13.92,\ -8.56)\).
b) and c)
rotation = affine_matrix(axis_angle_matrix([0.0, 0.8, 0.6], np.radians(90.0)), [0.0, 0.0, 0.0])
destination = transform_point(about_pivot(rotation, lamp), ship)
print(np.round(destination, 4))
print(round(norm(ship - lamp), 4), round(norm(destination - lamp), 4))The distance to the lamp is \(\sqrt{64 + 81 + 36} = \sqrt{181} \approx 13.45\) m before and after: the pivot lies on the axis and rotations preserve distances to the points of the axis. Notice that the result is very different from the quarter orbit of the example, \((3, 9.6, -2.8)\), even though the direction of the axis and the angle are the same: an axis of rotation is a line, not just a direction.
Conclusion
Every 3D rotation is a turn about an axis—Euler's theorem, which is nothing more than the existence of the eigenvalue 1—and can therefore be described with a unit axis and an angle, or with the rotation vector that packs them together. Rodrigues' formula applies that rotation by splitting the vector into a part parallel to the axis, which does not move, and a perpendicular part, which rotates in its plane with the help of the cross product. Written with the skew-symmetric matrix \(K\), it gives \(R = I + \sin\theta,K + (1 - \cos\theta),K^2\), which contains \(R_x\), \(R_y\) and \(R_z\) as special cases; and read backwards (the trace for the angle, the skew-symmetric part for the axis) it lets us extract the axis and the angle of any rotation matrix. With a pivot, it also works for axes that do not pass through the origin, such as that of the ship's barrel roll. Its weak point is composition: there is no direct way of combining two axis-angle pairs. In the next lesson, "Quaternions", we will store the same axis and the same angle in four numbers with a multiplication rule of their own, and composing rotations will once again be as easy as multiplying.
