# Rotating one 3-vector to another

I have written an algorithm for solving the following problem: Given two 3-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$.

-

This is the right general approach, but the corner case $\|a\times b\| \approx 0$ must be handled.

If $\theta < \epsilon,$ $R=I$.

If $\pi-\theta < \epsilon$, you can choose for $\mathbf{x}$ any vector orthogonal to $\mathbf{a}$, for instance $\mathbf{x} = \frac{\mathbf{a} \times e_i}{\|\mathbf{a}\times e_i\|}$, where $i$ is the index of the component of $\mathbf{a}$ with least magnitude.

-
I was aware of the first case (very small angle), but you have shown the angle approaching 180° is also important to handle. Thanks. –  Libor Feb 2 '13 at 21:53
Libor, user7530: Can anyone explain the origin of rotation matrix formula? I've no idea about rotation matrix and skew matrix. I'd also like to know what happens at 0 and 180 to handle the cases separately? I also have no idea about orthogonal vector. I can understand till finding the angle between vectors and the axis of rotation. –  cegprakash Jun 9 at 23:17
@cegprakash Have you read the article on Wikipedia about the Rodrigues Rotation Formula? What you ask is a rather broad topic, you may want to try asking specific, new questions. –  user7530 Jun 9 at 23:40

I'm not clear on why you have a factor of $A^2$ in your expression for $R$. In particular, wikipedia lists the matrix form for the Rodrigues formula as

$$R = I \cos \theta + A \sin \theta + (1-\cos \theta) x x^T$$

-
Note that for $k$ a unit vector, $A^2 = kk^T - I$. –  user7530 Feb 2 '13 at 21:40

I have a simpler method comes from Erigen's "Mechanics of Continua". R is rotational matrix that rotate vector "a" align with vector "b" Matlab Code:

%%%%%% Rotate vector a align with vector b%%%%%%%%%% syms ax ay az bx by bz k real

a=[ax ay az]'

au=a./sqrt(ax^2+ay^2+az^2)

b=[bx by bz]'

bu=b./sqrt(bx^2+by^2+bz^2)

R=[bu(1)*au(1) bu(1)*au(2) bu(1)*au(3);

bu(2)*au(1) bu(2)*au(2) bu(2)*au(3)

bu(3)*au(1) bu(3)*au(2) bu(3)*au(3)]


% You can verify it by type

c=R*a

cu=c./sqrt(c(1)^2+c(2)^2+c(3)^2)

simple(bu-cu)

%the result is zero means c(a after rotation) and b are aligned with each other.

simple(sqrt(c(1)^2+c(2)^2+c(3)^2)-sqrt(c(1)^2+c(2)^2+c(3)^2))

%the result is zero means c(a after rotation) and a are of the same length

%%%%%%%End%%%%%%%%%%%%

-

Not being familiar with Matlab syntax, I was wondering if Leyu Wang (or anyone) could translate his answer into more common syntax so that I know what the operators are? I need to implement this in an old version of TCL, which does not have powerful math functions, so I probably can't use matrix operations, but rather have to do it all with trig.

(Obviously, this is not an answer but rather a question, but since my account is new, it will not let me comment on his answer -- sorry!)

-
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. –  vonbrand 1 min ago