Linear Algebra – Rotating One 3D Vector to Another

3dlinear algebrarotations

I have written an algorithm for solving the following problem: Given two 3d-vectors, say: $a,b$, find rotation of $a$ so that its orientation matches $b$.

However, I am not sure if the following algorithm works in all cases:

1) Find axis and angle using cross product and dot product:

$$\mathbf{x}=\frac{a\times b}{||a\times b||}\\ \theta=\cos^{-1}(\frac{a\cdot b}{||a||\cdot ||b||})$$

3) Find rotation matrix using exponential map:

$$\mathbf{R}=e^{\mathbf{A}\theta} =\mathbf{I}+\sin(\theta)\cdot \mathbf{A}+\left(1-\cos(\theta)\right) \cdot \mathbf{A}^{2}$$

where $\mathbf{A}$ is a skew-symmetric matrix corresponding to $\mathbf{x}$:

$$\mathbf{A}=[\mathbf{x}]_{\times}=\begin{bmatrix}0 & -\mathbf{x}_{3} & \mathbf{x}_{2} \\ \mathbf{x}_{3} & 0 & -\mathbf{x}_{1} \\ -\mathbf{x}_{2} & \mathbf{x}_{1} & 0\end{bmatrix}$$

Notes:

The axis is computed using cross product as this gives vector perpendicular to both $a$ and $b$. Only direction of the axis is important, hence it is divided by its magnitude. However, I am not sure if $\mathbf{x}$ will always have the proper direction (the result can be $-\mathbf{x}$ instead of $\mathbf{x}$?).

The rotation matrix is computed using Rodrigues' rotation formula.

Finally, the vector $\mathbf{R}a$ should have same direction as $b$.

I have tested this numerically and it seems working, but I would like to be sure the formulas work for any two $a,b$.

Best Answer

I know this is a long-dead and well-answered question, but I woke this morning thinking "All of the answers involve an "if" statement, i.e., all are discontinuous functions of the inputs. Is there a continuous answer, i.e., a continuous function $$ R: S^2 \times S^2 \to SO(3) $$ that takes a pair of vectors $u, v$ to a rotation matrix $R(u, v)$ with the property that $R(u, v) u = v$?"

The paper that Tomas Moller and I wrote back in 1999, for instance, uses "the coordinate vector corresponding to the smallest entry of $w$" for some vector $w$, which doesn't vary continuously as a function of $w$. And I wondered, "Did we really do as well as possible, or might there have been a continuous solution?"

The answer is "no." But the proof uses a bit of topology.

Fix the vector $u$ (set it to be $e_1$, for instance), and look at the map $$ K : S^2 \to SO(3) : v \mapsto R(u, v). $$ Then compose this with the map $$ H : SO(3) \to S^2 : M \mapsto Mu. $$ The composite map $$ H\circ K: S^2 \to S^2 $$ is $v \mapsto R(u, v)u = v$, i.e., the identity map on $S^2$. But that means that $$ (H\circ K)_{*} : H_2(S^2) \to H_2(S^2), $$ the induced map on second homology, must be the identify from $\mathbb Z $ to $\mathbb Z$. But since $$ (H\circ K)_{*} = H_{*} \circ K_{*}$$ this map must factor through $H_2(SO(3)) = 0$, which is impossible.

Thus: There's no continuous solution to the "rotate one vector to another" problem, a fact that I should have mentioned back in our original paper. Sigh. Hindshight is 20-20.