# Finding rotation axis and angle to align two 3D vector bases

I have asked this question before and, while the accepted answer solved my problem back then, I am still interested in finding the rotation axis and angle. Let me rephrase the problem here:

I would like to find the rotation axis and angle needed to align two 3D vetor bases, $(\vec a, \vec b, \vec c)$ towards $(\vec d, \vec e, \vec f)$ (meaning that, after the rotation is applied, $(\vec a, \vec b, \vec c)$ equals $(\vec d, \vec e, \vec f)$). These bases are orthonormal (i.e. normalized vectors, forming 90-degree angles between any of them). The bases also have the same handedness (i.e. no mirroring is necessary to align one towards the other).

Now, due to these bases having the same handedness, there must exist a single rotation that aligns one towards the other (right?).

I would like to know the solution out of curiosity. There's no concern for stability/precision. I've tried solving the original question's accepted answer symbolically, then extracting the axis and angle from the matrix -- did not go so well. Maybe finding the eigenvector would help?

• If you want a linear transformation that maps the first three vectors to the second one, then you don't need to find an axis of rotation and a rotation. Finding an axis of rotation is not generally stable as the transformation approaches the identity matrix where there are 3 eigenvectors with eigenvalue 1. You may want to rethink your approach. – DisintegratingByParts Jan 29 '15 at 21:42
• I might use another approach for my purposes, but still, I am very curious! Even if it means checking for corner cases (just like it's needed for the formula I give at the start of the linked question), I would like to see the general solution to find the axis-angle rotation. – HLorenzi Feb 2 '15 at 2:14
• Given that there are issues of numerical stability in play, I would ask: what would you do with that axis and angle? – Muphrid Feb 2 '15 at 2:25
• The original idea is to rotate 3D models for placing them at the correct orientation on a Mario Galaxy-esque gravity engine, and to interpolate these orientations between frames (e.g., slow-down effects). I have done it already flawlessly after the linked question, and I can do the interpolation using a strategy other than this. Now I just want to know the solution out of curiosity. Is it possible, even if it means checking for corner cases? And what is the algebraic/algorithmic solution? – HLorenzi Feb 3 '15 at 20:45

The rotation matrix, $R$, can be found relatively easy, such that,

$$R\,\vec{a} = \vec{d}; \quad R\,\vec{b} = \vec{e}; \quad R\,\vec{c} = \vec{f}.$$

$$R = \vec{d}\vec{a} + \vec{e}\vec{b} + \vec{f}\vec{c}.$$

When using this notation the multiplication with a vector should be written as a dot product. From this it can also be proven that this expression for $R$ is correct. For example with mapping $\vec{a}$ to $\vec{d}$, the first dyad will return $\vec{d}\vec{a}\cdot\vec{a}$ and since $\vec{a}$ is normalized that is equal to $\vec{d}$. The remaining dot products will yield zero, because the other vectors on the right side of each dyad is orthogonal to $\vec{a}$.

Each dyad can also be written as a 3$\times$3 matrix using the vector direct product. The total 3$\times$3 rotation matrix can then be constructed by adding the three 3$\times$3 matrix from the vector direct products.

The axis of rotation is equal to the eigenvector of the eigenvalue equal to one. For finding this eigenvector you can take a look at the answer to this question. In order to find the axis of rotation you have to calculate the following matrix,

$$R-R^T = \begin{bmatrix} 0 & \alpha & \beta \\ -\alpha & 0 & \gamma \\ -\beta & -\gamma & 0 \end{bmatrix},$$

such that the normalized axis of rotation, $\vec{r}$, is equal to,

$$\vec{r} = \frac{ \begin{bmatrix} -\gamma & \beta & -\alpha \end{bmatrix}^T }{\sqrt{\alpha^2 + \beta^2 + \gamma^2}}.$$

The angle of rotation, $\theta$, can be found according to,

$$\text{Tr}(R) = 1 + 2\cos\theta,$$

where $\text{Tr}(R)$ is the trace of $R$, the sum of its diagonal elements. Due to the cosine you do have to look at the sign of the angle and direction of $\vec{r}$.