Beautiful maths simplification: quaternion from two vectors (2013)
lolengine.net
lolengine.net
You can play with the implementation at [1] and see the source (and maths explained!) at [2].
Quick summary:
1. Quaternion (cos(a), sin(a) u)--> rotation about the axis u by angle 2a (u is a unit vector in R^3)
2. (x,u) * (y,v) = (xy - u . v, xv + yu + u X v),
where . stands for the dot product, and X stands for the vector cross product.
If you look at the source, you can see that I was lazy and used trig / inverse trig. However, once you grasp how it works, it is not hard to improve this code according to the article.
The article is good once you know how quaternions work; my write-up aims to explain that. If the article scared you away, check [1] out - hope it helps!
The code is Java-like, so should be readable, and has plenty of comments.
This directly gives the final form of the article, up to a small optimization.
followed by normalization. Proof can be carried out completely in quaternion algebra.
An interesting degenerate case is when u=-v. In that case, another expression can be used: PERP(v)(vu+|vu|) {where PERP(v) means an arbitrary vector perpendicular to v}
followed by normalization. Both formulas need to be used in practice, depending on which one is closer to outputting a degenerate result.
Alas, I also needed the angle to compute the torque applied by an angular spring, so I slapped an arccos in there and called it a day.