Compute the rotation matrix given two vectors using Rodrigues' formula
In the previous post, we have shown how angular velocities and rotation matrices are linked through the exponential map, or to be specific, the Rodrigues’ rotation formula. In this post I would like to give as an useful application of Rodrigues’ formula, to compute the rotation matrix between two vectors.
Let $a,b\in\mathbb{R}^3$ ($a\neq b$) be two unit vectors expressed in an arbitary coordinate frame. Our goal is to compute the rotation matrix from $a$ to $b$.
We denote by $u$ and $\theta$ be the axis and the angle of rotation, repectively. Since $a$ and $b$ are unitary, by definition the cross product gives the product between $u$ and $\theta$:
$$
a\times b = u\sin\theta,
$$
and the dot product gives the cosinus:
$$
a\cdot b = \cos\theta.
$$
According to the Rodrigues’ formula, the rotation matrix from $a$ to $b$ has a closed form:
$$
R = I + \sin(\theta){u\times} + \left(1-\cos(\theta)\right)(u\times)^2,
$$
which is equal to:
Finally, we can write the following Python code to do the job:
1 | def cross_3d(v): |