What are advantages of quaternion over $3\times3$ rotator matrix for representing arbitrary rotation?
There are two typical operation one performs with rotations.
- Transform a point: $p_a = R_{a,b} \cdot p_b$.
- Concatenating two rotations: $R_{a,c} = R_{a,b} \cdot R_{b,c}$
In a nutshell, operation 1. is cheaper for rotation matrices, while operation 2. is cheaper for unit quaternions. For instance rotation matrix concatenation takes 27 multiplications but only 16 for quaternions.
However, this is not the full story. If we only were to save a bit of computation, why bother learning about quaternions and their less intuitive formulae?
Let us assume we represent our rotations as 3x3 matrices $R$. In order for $R$ to be a rotation matrix, it has to be orthogonal, i.e. $R\cdot R^\top=I$ (and it should hold that $det(R)=1$). However, if we perform a concatenation operation using numerical arithmetic, the resulting 3x3 matrix is not necessarily orthogonal due to numerical precision, e.g. rounding errors. Such an error might be small, but if we were to perform a large number of such operations, the error will accumulate. One approach would be to detect if a matrix is non-orthogonal, $R\cdot R^\top\neq I$, and then re-orthogonalize the matrix. However, such an operation is usually expensive/complex since it envolves an SVD-decomposition or similar algorithm.
If you represent a rotation as quaternion, there is a simpler condition: The quaternion has to be of unit length. Thus we can simply check if $|q_r^2 + q_x^2 + q_y^2 + q_z^2 - 1|> \epsilon$ and in case re-normalize the 4-vector $q$. Note that there are even ways to perform this re-normalization without computing the square root.
In computer science terms: Unit quaternion have a simpler type invariant than rotation matrices; violations are slightly cheaper to detect and the invariant is cheaper to enforce.
Furthermore, it is worth mentioning that going from a unit quaternion to a 3d rotation matrix is relatively cheap and straight-forward. (Going the other way is more tricky to get right for all corner cases.) Hence, it can be a good strategy to represent 3d rotations as quaternions by default. In case we need to transform a large number of points by a specific rotation, we can convert the corresponding quaternion into a rotation matrix and save some computation.
There are a number of other advantages of quaternions:
- Less storage: 4 numbers versus 9.
- Interpolation between two rotations can be cheaper to compute for quaternions (e.g. using SLERP) than for matrices where we need to extract the rotation vector (angle x axis) representation.
Note that there is common pitfall when using quaternions: There is a one-to-two relation between rotation (matrices) in 3d and unit quaternion. For one rotation matrix $R$, there exist two unit quaternions $(q_r, \pm \mathbf{v})$, where $q_r$ is the real part and $\mathbf{v}$ the imaginary/vector part of the quaternion. Hence, it is a bit more difficult to check whether two quaternions represent the same rotation; simply checking the components for equality is not sufficient.