Code covered by the BSD License

### Highlights from Efficient Vector Rotation Matrix Routine

Be the first to rate this file! 1 Download (last 30 days) File Size: 33.5 KB File ID: #37597 Version: 1.1

# Efficient Vector Rotation Matrix Routine

### Darin Koblick (view profile)

23 Jul 2012 (Updated )

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

File Information
Description

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’

Acknowledgements

This file inspired Rot Vec Around Arb Axis(Unit Vec2 Rotate,Rotation Axis Unit Vec,Theta).

Required Products MATLAB
MATLAB release MATLAB 7.14 (R2012a)
23 Apr 2014 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.

Comment only
22 Apr 2014 Alexander

### Alexander (view profile)

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.

Comment only
10 Jan 2013 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.

Comment only