# Get a rotation to align a vector, n with another vector, a and be able to rotate around a

Let us say I have a rigid body, B. Let us say I have a normal vector n. And let us say I have another direction vector a. I need to rotate my rigid body such that n aligns with a. For the purposes of this question, let us assume that the normal vector n and B share the same frame of reference. I understand this is an under-constrained problem, since the rigid body can be free to rotate around the axis a after being aligned. Let us call this angle of rotation as $$\theta$$ Is there a way to get this rotation matrix parameterized by $$\theta$$, such that I can plug in different values of $$\theta$$ and I can obtain a rigid body pose?

Thanks.

Assuming you are working in 3-dimensions, this is exactly what the cross-product does. To find the vector of rotation that rotates $$\mathbf{n} \in \mathbb{R}^{3}$$ into $$\mathbf{a} \in \mathbb{R}^{3}$$, we have:

$$\mathbf{e} = \mathbf{n} \times \mathbf{a} = (||\mathbf{n}||\ ||\mathbf{a}|| \sin(\theta)) \cdot \mathbf{u}$$

where $$||\cdot||$$ represents the 2-norm, $$\theta$$ represents the angle of rotation between $$\mathbf{a}$$ and $$\mathbf{n}$$, and $$\mathbf{u} \in \mathbb{R}^{3}$$ represents the axis of rotation. Now, to rotate $$\mathbf{n}$$ onto $$\mathbf{a}$$. Via the Rodrigues' rotation formula, we have:

$$\mathbf{n}_{\mathbf{a}} = \cos(\theta) \mathbf{n} + \sin(\theta)(\mathbf{u} \times \mathbf{n}) + (1-\cos(\theta))(\mathbf{u} \cdot \mathbf{n})\mathbf{u}$$

Where $$\theta$$ and $$\mathbf{u}$$ are the same values as described originally. We solve for them as:

$$\mathbf{u} = \frac{\mathbf{e}}{||\mathbf{e}||} \\ \theta = \arccos(\frac{\mathbf{n} \cdot \mathbf{a}}{||\mathbf{n}|| \ ||\mathbf{a}||})$$

It should be noted that we use $$\arccos$$ instead of $$\arcsin$$ as this will return an angle from $$0$$ to $$\pi$$, which is a better representation of the angle between two vectors. You might notice that $$\mathbf{e} = \mathbf{0}$$ when $$\theta = \pi$$. This is the case as the cross product is undetermined in these configurations (we have an infinite number of vectors that we could rotate about to achieve our goal). As you mentioned, this is one of the issues with the problem you have posed, but luckily it only occurs in one specific case and hard-coded solutions can be made to handle this fringe case.

Now, to obtain a rotation matrix that will perform the same rotation for the rest of the vectors associated with body B, we have:

$$\mathbf{R} = \exp(\mathbf{K}\theta) = \mathbf{I} + \sin(\theta)\mathbf{K} + (1-\cos(\theta))\mathbf{K}^{2}$$

where $$\mathbf{K} = [\mathbf{u}]_{\times}$$ is the cross-product matrix:

$$\mathbf{K} \triangleq \left[ \begin{array}{ccc} 0 & -u_{3} & u_{2} \\ u_{3} & 0 & -u_{1} \\ -u_{2} & u_{1} & 0 \end{array} \right]$$

Again, you will need to handle the case where $$\mathbf{n}$$ and $$\mathbf{a}$$ are parallel and opposite direction as an independent case, solving for $$\mathbf{u}$$ and $$\theta$$ still. Once this is handled, this method should work generally for any $$\mathbf{n}$$ and $$\mathbf{a}$$.