[Math] how to rotate a vector in 3d space around arbitrary axis

3dquaternionsrotationstransformationvector-spaces

In my case I have two arbitrary vectors (suppose vector AB and CD ) and I am assuming that some rotation operation will happen to vector AB to get it the orientation of vector CD. so by using the knowledge the positions (cartesian coordinates) of both the vectors (already known) can I find the angle of rotation between them around some arbitrary axis of rotation in space?
Can I reproduce vector CD by using the knowledge of their positions and angle between them?

Basically I am not dealing with vectors but 3 dimensional objects with multiple cartesian coordinates in my system. so using the information of cartesian coordinates of both objects I want to do operations at first object to transform it to the second object. I hope the you are convinced with what I am trying to explain.

Best Answer

The angle between two unit vectors $u$ and $v$ is given as

$$ \theta = \arccos\left(u^\top v\right) $$

the shortest axis of rotation is the vector orthogonal to both vectors, and can be found using the cross product

$$ s = \frac{u \times v}{\lVert u \times v\rVert} $$

In most cases, $s$ should be normalized, even if $u$ and $v$ are already both unit vectors.

(note that switching the order of the cross product will give you the negative of the axis. The right-hand rule determines the order)

To create the rotation matrix that rotates $u$ into $v$, we can use the axis-angle formula for rotation matrices: (taken from wikipedia)

$$ R = \begin{bmatrix} \cos\theta + s_x^2 (1-\cos\theta) & s_x s_y (1-\cos\theta) -s_z \sin\theta & s_x s_z(1-\cos\theta + s_y \sin \theta \\ s_y s_x (1-\cos\theta)+s_z \sin\theta & \cos\theta + s_y^2 (1-\cos\theta) & s_y s_z (1-\cos\theta) - s_x\sin\theta \\ s_z s_x (1-\cos\theta) - s_y \sin\theta & s_zs_y(1-\cos\theta) + s_x \sin\theta & \cos\theta + s_z^2(1-\cos\theta) \end{bmatrix} $$

and now you can compute

$$ v = Ru $$

and

$$ u = R^\top v $$