View Full Version : Matrix to transform vector MA to MB same length

09-24-2008, 05:53 PM
i am working on a 3D project. some where in my program i need to draw a plan that is perpendicular to a vector MB. for doing this i need to know the (x,y,z) of each voxel in this plan (a portion of the plan or window).
for this i need to make a marix that transform a well known vector MA (for exemple parallel to X axis) to a known vector MB of the same length. why ? because I will calculate the (x,y,z) of the plan perpendicular to X axis (witch is easy to calculate) and then multiplay by the matrix to obtain the (x,y,z) in the new plan (perpendicular to MB)
So i need a rotation matrix (because of the same origin M of the two vectors). I think i can do only two rotations (on Y and Z axis) to obtain the new vector MB.
In the literature I have only found rotation about x, y or z axis or even rotation about a vector v. but i can t find a matrix to have a vector from an other.
I don t know if i can consider the multiplication of two matrix (one alpha rotation about y and beta rotation about z) and try to find alpha and beta.
excuse me but i am not good at maths
think you

09-24-2008, 09:59 PM
To find a matrix that transform a vector MA to a vector MB you can try this:

1) If MA and MB are not unit length, normalize it.

2) Calculate the dot product (http://en.wikipedia.org/wiki/Dot_product) of MA and MB, this will give the cosinus of the angle between MA and MB. To get the angle, take the arccos of the result.

3) Calculate the cross product (http://en.wikipedia.org/wiki/Cross_product) of MA and MB, this will give a vector for the axe of rotation.

4) Now we have an angle and a vector. Then you can use the Rodrigues formula (http://mathworld.wolfram.com/RodriguesRotationFormula.html) to calculate a rotation matrix to rotate MA to MB.

hope that help

09-25-2008, 03:32 PM
perfect think you

09-25-2008, 03:42 PM
Take care of the cross product because you will have to normalize it. And there are two special case when the cross product is zero :

1) MA = MB in this case the rotation matrix is identity.

2) MA = -MB, in this case you will have to choose a vector that lie
on the plane (MA_x,MA_y,MA_z).

09-26-2008, 04:13 PM
ok think you

09-27-2008, 06:03 AM
Excuse me, but I still have one small problem.
the dot product will give me cos(teta) so the angle my be a or -a !!
and the cross product give me |sin(teta)| (absolute value). so how can I know if it is a or -a ??
think you

09-27-2008, 09:56 AM
The simple thing to do is to try both.

if MA * Rodrigues(a,vect) = MB then the angle is a
if MA * Rodrigues(-a,vect) = MB then the angle is -a

If the dot product is 0, the possible angles
are PI/2 and -PI/2.

The compare function between two vectors is something like this to avoid floating point precision problem.

bool compare(vect1,vect2)
EPSILON = 0.001;
if( (fabs(vect1.x - vect2.x) < EPSILON) &amp;&amp; (repeat for y and z))

09-27-2008, 11:18 AM
I think there is a clever way to determine the sign of angle a.

Take the determinant (http://en.wikipedia.org/wiki/Determinant) of the two vector

| 1 1 1 |
| MB_X MB_Y MB_Z |
| MA_X MA_Y MA_Z |

if the determinant of this matrix is positive,
the position of MB is clockwise relative of MA and the angle a
is positive.

if the determinant is negative, the position of MB is counter-clockwise relative of MA and the angle a
is negative.

Michael Steinberg
09-27-2008, 07:14 PM
The (anticommutative) crossproduct and the windedness of the rotation around that crossproduct (which you use as the axis fpr rotation) will incorporate that information. ( a x b = - b x a )
So you have the same Angle if you swap two vectors, but in the same step you are negating the rotation axis and thus the orientation of the rotation.