Rotation from one vector to another, Möller

Started by
3 comments, last by __fold 20 years, 5 months ago
I found this http://www.ce.chalmers.se/staff/tomasm/code/fromtorot.txt. It's Möllers implementation for calculating the rotating matrix between two vectors as descriebed in some papers and in the real-time rendering book. The thing is in the beginning it takes care of a special case, when the two vectors are parallel. I don't understand what the code does. I would just check the crossproduct to see if it's parallel. Then return identity matrix if the vectors are pointing at the same direction, otherwise -1 * identity matrix. Any ideas? [edited by - __fold on December 16, 2003 9:18:20 AM]
Advertisement
A dot product is more efficient than a cross product (there are less operations to do).

Thus, it''s better to do the dot product of both vectors (a and b).

here''s the thing:

When the vectors are perpendicular, the dot product is 0.

When they''re parallel, it''s either 1 or -1, depending on whether they''re pointing in the same direction (1) or opposite directions (-1).

Following examples:

[0, 0, 1] . [0, 1, 0] (perpendicular vectors)

0*0+1*0+0*1 = 0

[0,0, 1] . [0, 0, 1] (parallel -same direction)

0*0+0*0+1*1 = 1

[0, 0, 1] . [0, 0, -1] (parallel - opposite directions)

0*0+0*0+1*-1 = -1

Anything "between" parallel and perpendicular will have a value such that 0 < abs(dotproduct) < 1.

Now, the epsilon in the if statement is there to account for floating-point errors. It would suck if you did a comparison againt 1 and your normalized vectors, because of precision, returned 0.998 or something like that, so the epsilon is there to account for that.

Hope that helps,


Josh
Ah, ok, now I understand. But i think I test this, I'm not sure at all that the solution he presents is faster, especially when he uses coefficients. He's solution is almost accurate, that's a good thing though.

Thanks for the reply!

[edited by - __fold on December 16, 2003 4:49:46 PM]
... assuming your vectors are normalized.

Your problem is basically a quaternion division. There is an equivalance between points and quaternions. Google about quaternions and point rotations to understand it better. Also check some mouse-trackball simulation code.
"Coding math tricks in asm is more fun than Java"
Charles B: All the vectors are normalized and I have already a fully working Quaternion class.

I want to create the rotation matrix cause I need to rotate, transate and rotate alot of points in software. So I want to calculate a matrix M = R*T*R and then calculate Vi_new = M * Vi for 1 < i < 300000 (it''s used for face/face CD).

The code for caclulating the from-to-matrix is actually using the quaternion q^ = (1/sqrt(2(1+s.t)(s x t), sqrt(2(1+s.t)/2) (where s and t are vectors) which simplifies to the matrix in the last part of the code.

This topic is closed to new replies.

Advertisement