File Exchange

image thumbnail

Efficient Vector Rotation Matrix Routine

version 1.1 (33.5 KB) by

Efficient Method to Determine a Matrix Needed to Rotate One Vector to Another

1 Download


View License

Suppose you have a unit vector described by f = [ai bj ck] and you would like to rotate f such that its result is t = [di ej fk]. This routine will find R(f,t) such that R(f,t)*f’ = t’. Where R(f,t) is known as the 3 x 3 transformation matrix needed to rotate f into t.
This MATLAB routine was based on a published article titled “Efficiently Building a Matrix to Rotate One Vector to Another” written by Tomas Moller and John Hughes in 1999.

This method features no square roots or trigonometric function calls and is reported to be faster than any other vector rotation matrix method tested by Moller and Hughes. In fact, the Goldman method (fastest method tested) was 50% slower than this routine in conversion speed tests.

Working Example:

Unit Vector f:
>> f = rand(1,3);
>> f = f./norm(f);

Unit Vector t:
>> t = rand(1,3);
>> t = t./norm(t);

The transformation matrix R(f,t) is found:
>> R = vecRotMat(f,t);

Now, test that R*f’= t’
>> R*f’

Comments and Ratings (3)

Darin Koblick

Darin Koblick (view profile)

Alexander, you are correct. I went ahead and changed the vector angle check to be closer to 1 instead of 0.99. Also, I found that I needed to add the case where vectors where close to parallel (i.e. abs(c) > 1-1e-13) and two or more components had the same values. Thank you for pointing out the problem to me.


Thank you for submission. But if cosine of angle between vectors is >.99 (by module), calculation is incorrect.
A pair f=[1 0 0], t=[0.9904 0 -0.1380] can be used as an example.

Jan Simon

Jan Simon (view profile)

There is some potential for accelerations:
1. Avoid calculatin ~idx 35 times, but use a temporary variable instead.
2. Matlab's CROSS and DOT implementations are very slow, so better calculate them inlined.



Added robustness to parallel vector check. Added extra check to account for vectors that are parallel and have two or more identical values. Rotation error was found to be on the order of 1e-15.

MATLAB Release
MATLAB 7.14 (R2012a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today