Problem: find the minimum magnitude angle rotation from $\hat{a}$ and $\hat{b}$, where both are unit vectors.

The unit quaternion $Q$ that rotates (unit bivectors) $a$ to $b$ can be expressed as:

and solving for $Q$ gives:

This is well-covered material and if you’ve read my previous few posts then it is copy-n-paste math, beat-a-dead-horse material. The bullet points are:

  • The relative unit quaternion ($R$) between the unit bivectors is : $R=\mathbf{ba}^{-1}=…=\mathbf{a} \cdot \mathbf{b} + \mathbf{a} \times \mathbf{b}$. This maps the plane that spans $\mathbf{a},\mathbf{b}$ and 0 to the complex plane that spans ${1,R}$ such that $\mathbf{a}$ is mapped to 1.
  • All that is left is taking the square root (half-angle) of $R$, which is a complex function so the remainder of the derivation is simply over complex numbers.
  • When $b=-a$ we have $R=-1$ and $Q=\sqrt{-1}$. The math explodes since there are an infinite number of solutions instead of one unique (all unit bivectors). Numerically the math explodes when approaching.

Ignoring the degenerate case this translates into:

vec4 q_from_normals(vec3 a, vec3 b)
  float k = 1.0+dot(a,b);         // 1+d
  float s = inversesqrt(k+k);     // 1/sqrt(2+2d)
  return vec4(s*cross(a,b), k*s); // (1+d)/sqrt(2+2d) + (a x b)/sqrt(2+2d)

Performing some renaming of variables:

and applying $Q$ to the standard orthonormal basis yields a rotation matrix:

The above is reduced in what I consider the most obvious way, but the diagonals have that horrible one minus. Replacing the one with $Q \cdot Q$ and reducing yields the much nicer:

Since we can rewrite $r$ as:

shows that the above is identical to the non-degenerate case presented by Moller and Hughes1.

Some alternate constructions for the same material:

  • Inigo Quilez walks to the same matrix solution as an example of the dangers of composing functions instead of carrying through derivations.2
  • Sam Hocevar builds the quaternion solution from vector calculus and trigonometry.3

Toy C code can be found: HERE

References and Footnotes

  1. “Efficiently Building a Matrix to Rotate One Vector to Another”, Tomas Moller, John F. Hughes, 1999, (PDF

  2. “avoiding trigonometry”, Inigo Quilez, 2013, (PAGE

  3. “Beautiful maths simplification: quaternion from two vectors”, Sam Hocevar, 2013 (PAGE