**0**

# Rotation matrix ortho-normalization

###
#21
Members - Reputation: **1147**

Posted 25 October 2004 - 06:18 PM

Can you implement camera like in my The Galaxy?

Quaternion it's just more elegant way to store orthogonal transform. I use quaternions in places where if i woulda have used matrix i woulda need to reorthonormalize.

###
#23
Members - Reputation: **1147**

Posted 25 October 2004 - 08:56 PM

Quote:

Original post by MichaelT

(Dmytry)"...computed in mind..."

My math teacher could calc arbitrary divisions down to the 5th decimal in his mind with little effort ;) That said, not all (actually few) have this talent, so don't take that for granted.

In this case it's easy. It scales by it's length squared;

0.3

^{2}+0.5

^{2}+0.5

^{2}+0.5

^{2}= 0.09+0.75 = 0.84

Tangent of half of angle is

sqrt(0.75)/0.3 = sqrt(3/4)/0.3 = sqrt(3)/2/0.3 = sqrt(3)/0.6

###
#25
Members - Reputation: **150**

Posted 26 October 2004 - 12:32 AM

create a nonunit quaternion from rotation matrix

martix must contain only rotation (not scale or shear)

the result quaternion length is numerical stable

*/

inline void from_matrix( const matrix33& mtx )

{

typedef float mtx_elm[3][3];

const mtx_elm& m = mtx.m;

float tr = m[0][0] + m[1][1] + m[2][2]; // trace of martix

if (tr > 0.0f){ // if trace positive than "w" is biggest component

set( m[1][2] - m[2][1], m[2][0] - m[0][2], m[0][1] - m[1][0], tr + 1.0f );

}else // Some of vector components is bigger

if( (m[0][0] > m[1][1] ) && ( m[0][0] > m[2][2]) ) {

set( 1.0f + m[0][0] - m[1][1] - m[2][2], m[1][0] + m[0][1],

m[2][0] + m[0][2], m[1][2] - m[2][1] );

}else

if ( m[1][1] > m[2][2] ){

set( m[1][0] + m[0][1], 1.0f + m[1][1] - m[0][0] - m[2][2],

m[2][1] + m[1][2], m[2][0] - m[0][2] );

}else{

set( m[2][0] + m[0][2], m[2][1] + m[1][2],

1.0f + m[2][2] - m[0][0] - m[1][1], m[0][1] - m[1][0] );

}

}

/**

set the rotation to matrix

*/

inline void to_matrix( matrix33& m )const {

float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;

float s = 2.0f/norm(); // 4 mul 3 add 1 div

x2 = x * s; y2 = y * s; z2 = z * s;

xx = x * x2; xy = x * y2; xz = x * z2;

yy = y * y2; yz = y * z2; zz = z * z2;

wx = w * x2; wy = w * y2; wz = w * z2;

m.m[0][0] = 1.0f - (yy + zz);

m.m[1][0] = xy - wz;

m.m[2][0] = xz + wy;

m.m[0][1] = xy + wz;

m.m[1][1] = 1.0f - (xx + zz);

m.m[2][1] = yz - wx;

m.m[0][2] = xz - wy;

m.m[1][2] = yz + wx;

m.m[2][2] = 1.0f - (xx + yy);

}

http://www.gamedev.ru/articles/graph/20040603.zip

###
#26
Members - Reputation: **1147**

Posted 26 October 2004 - 01:22 AM

Quote:

Original post by MichaelTQuote:

Original post by Eelco

good luck using stawman tactics for everything.

explain: "stawman tactics"

i think he meant strawman arguments...

BTW, minorlogic: Privet, videl tvoi formuli kasatelno quaternionov na euclideanspace.com, kruto i vnushaet! (Hello, man! Readed your quaternion derivations on euclideanspace.com, pretty impressive!)

###
#27
Anonymous Poster_Anonymous Poster_*
Guests - Reputation:

Posted 26 October 2004 - 02:31 AM

“I'm trying to figure out how to orthonormalize a rotation matrix so that the basis vectors would be unit length and perpendicular. This is to combat floating point precision errors that accumulate in the matrix.”

For which Dmytry gave the correct answer.

I as understand this is the Game dev forum were people comes for answer to specific questions. You basically told Gaimplay that the answer to his question required a rewrite of the entire project for not reason at all. Has his done what you suggested in the end his still have the same problem his started with, would not he?

I just reconfirmed the first answer was the correct one for the question.

What you did was to create and answer in search of a new problem. Part of been smart is not just to accumulate knowledge you also need to understand then questions.

Has it occur to you hi may be working on a very serious project where engineering re-factoring is just not an option.

About the straw man argument: like I said as usual you missing the point again and changing the subject.

Dmytry I think I am or was in you side this question was not about quaternions was about matrices. Make no mistake, do not think you are the only one understanding the algebra of rotation many more people do to.

###
#28
Members - Reputation: **301**

Posted 26 October 2004 - 02:58 AM

Quote:

Original post by Anonymous Poster

Eelco as usual you misunderstood the question: This was the original question

“I'm trying to figure out how to orthonormalize a rotation matrix so that the basis vectors would be unit length and perpendicular. This is to combat floating point precision errors that accumulate in the matrix.”

For which Dmytry gave the correct answer.

I as understand this is the Game dev forum were people comes for answer to specific questions. You basically told Gaimplay that the answer to his question required a rewrite of the entire project for not reason at all. Has his done what you suggested in the end his still have the same problem his started with, would not he?

I just reconfirmed the first answer was the correct one for the question.

What you did was to create and answer in search of a new problem. Part of been smart is not just to accumulate knowledge you also need to understand then questions.

Has it occur to you hi may be working on a very serious project where engineering re-factoring is just not an option.

i wasnt the one to bring up quaternions, i started out with giving him a possible solution to his stated problem. i merely confirmed what charles and dymtry suggested: quaternions might serve you better, so you might want to consider them. to which i added: probably, if you dont care for the advantages matrices have to offer over quats, which is likely in a gameengine.

Quote:

About the straw man argument: like I said as usual you missing the point again and changing the subject.

believe whatever you wish. youll have to start making arguments though if youre interested in persuading others.

Quote:

Dmytry I think I am or was in you side this question was not about quaternions was about matrices. Make no mistake, do not think you are the only one understanding the algebra of rotation many more people do to.

sure many people understand this, and my understanding probably isnt nearly as deep as that of a lot of others. i doubt those people include you though. i might add to that that dmytry and charlesB are the most knowledgeable people i know on these boards on the subject of math, and they seem to hold the same stance, which i will reiterate again for clarity: quaternions are the best method to store pure rotation (+ uniform scaling optionally).

were not forcing him to rewrite his engine using quats, we answered his question, dmytry corrected me and gave an even better method, and we gave some suggestions beyond the scope of his question: which he is free to act on or completely ignore.

###
#29
Members - Reputation: **191**

Posted 26 October 2004 - 03:20 AM

This way the same columns are used. Is it a good idea to switch the order for which columns are processed for the orthonormalization during the simulation?

first with the order dimitry gave and next orthonormalisation

for example start with Column3=Normalized(CrossProduct(Column1,Column2)) and do other 2 columns

or is this just making things more complicated?

###
#30
Members - Reputation: **1147**

Posted 26 October 2004 - 03:26 AM

After question is answered, it's OK to discuss related topics that can be useful for OP.

2:It's not needed to rewrite everything, because quaternions and rotation-only matrices represent same thing. They both is concatenated by multiplication, both should have functions like "load to openGL", most methods of both is the same. With good design, it's not a problem to replace one by other. Also, it's probably better if OP will rewrite it now than later, because probably the only thing he need to rewrite now is camera class.

3:I don't think no one else there understands rotations better than i. Yes, i sometimes have altitude like "f34r my math skillz", but it's usually when i'm first to reply with some simple calculations done in mind.

4: you gave no mathematical proofs, only some example quaternions that "should do bad things". In fact they don't, and it's pretty easy to mentally figure what 'em do if you know quaternions.

In summary: If you need to store and process variable rotation, quaternions is better than matrices.

f34r skillz part: today and at yesterday evening i worked with matrices for perspective transforms used to do that clickster (matrices that was used to make that hexagons, not camera matrices)

edit: and i must say, i clicked "reply" before i saw Eelco's reply.

[Edited by - Dmytry on October 26, 2004 9:26:21 AM]

###
#31
Members - Reputation: **1147**

Posted 26 October 2004 - 03:38 AM

Quote:

Original post by Airo

I've got a question about Dimitry's matrix orthonormalizing method.

This way the same columns are used. Is it a good idea to switch the order for which columns are processed for the orthonormalization during the simulation?

first with the order dimitry gave and next orthonormalisation

for example start with Column3=Normalized(CrossProduct(Column1,Column2)) and do other 2 columns

or is this just making things more complicated?

It shouldn't matter -

Column1=Normalized(CrossProduct(Column2,Column3));

Column2=Normalized(CrossProduct(Column3,Column1));

needs to be normalized anyway.

Maybe my version is marginally more mathematically stable, but i can't say for sure.

It may be useful to swich order depending to what axis more needs to be keept mostly untouched.

It's may be better in some cases to keep specific row.

(it will work with rows instead of columsn as well). For instance, if matrix contains global->local transform for camera, iin opengl style, it's better to keep 3rd row.

###
#33
Members - Reputation: **1147**

Posted 26 October 2004 - 03:51 AM

Quote:

Original post by Eelco

interesting picture.

what is it? a vonoroi representation of the vertices of a subdivided isocahedron?

Yes,kinda like that.

Some more things

I'm using some kind of projection matrices to get coordinates on icosohedron faces - i just multiply coordinate with 3x3 matrix and then divide result by result's z, and i'm getting point projected onto icosohedron face.

###
#35
Anonymous Poster_Anonymous Poster_*
Guests - Reputation:

Posted 26 October 2004 - 04:18 AM

Quote:

“1:Eelco gave keyword for search, therefore, he answered original question. I just failed to remember that it's named after Gramm-Schmidt, so i posted a solution.”

I am glad he is learning that method I was the want who introduced to him after hi gave another erroneous answer to similar question in another tread. But I think he still needs to understand the meaning of it and not applying it literally.

And not he did not answer the question he just say quaternion are better than matrices.

And that is not truth. Maybe for him they are.

Quote:

“2:It's not needed to rewrite everything, because quaternions and rotation-only matrices represent same thing. They both is concatenated by multiplication, both should have functions like "load to openGL", most methods of both is the same. With good design, it's not a problem to replace one by other. Also, it's probably better if OP will rewrite it now than later, because probably the only thing he need to rewrite now is camera class.”

You are totally wrong man quaternion do not concatenate by multiplication, I just proved to you, just do it yourself, before making such statements, and yes replacing the transformation in a major re-factoring job that may bring a whole new bag of bugs. It may not even be possible, as I read the person is using directx.

Quote:

“3:I don't think no one else understands rotations better than i. Yes, i sometimes have altitude like "f34r my math skillz", but it's usually when i'm first to reply with some simple calculations done in mind.”

Wow that is a strong statement, the only think I can say is that it speaks for itself.

Oh maybe you should do more things on paper and less work in your mind, you could get better result, and less misleading would result of such impulsive reactions.

Quote:

“In summary: If you need to store and process variable rotation, quaternions is better than matrices”

perhaps you should rephrase it like this:

If you need to store and process variable rotation, in my opinion quaternions is better than matrices”

As for Eelco: I think there is not point to continue this argument: Eelco I think you should use quaternion in your projects and do not let anyone else to convince you otherwise.

Bye.

###
#36
Members - Reputation: **301**

Posted 26 October 2004 - 04:28 AM

Quote:

Original post by Anonymous Poster

You two are hopeless.

Quote:

“1:Eelco gave keyword for search, therefore, he answered original question. I just failed to remember that it's named after Gramm-Schmidt, so i posted a solution.”

I am glad he is learning that method I was the want who introduced to him after hi gave another erroneous answer to similar question in another tread. But I think he still needs to understand the meaning of it and not applying it literally.

And not he did not answer the question he just say quaternion are better than matrices.

And that is not truth. Maybe for him they are.

ah that explains a lot. i thought i recognized that debating style, or rather lack of it, from somewhere.

Quote:

Quote:

“2:It's not needed to rewrite everything, because quaternions and rotation-only matrices represent same thing. They both is concatenated by multiplication, both should have functions like "load to openGL", most methods of both is the same. With good design, it's not a problem to replace one by other. Also, it's probably better if OP will rewrite it now than later, because probably the only thing he need to rewrite now is camera class.”

You are totally wrong man quaternion do not concatenate by multiplication, I just proved to you, just do it yourself, before making such statements, and yes replacing the transformation in a major re-factoring job that may bring a whole new bag of bugs. It may not even be possible, as I read the person is using directx.

why are directx and quaternions mutually exclusive? i dont know directx, so it might be for all i know, but it somehow seems very unlikely to me.

Quote:

Quote:

“3:I don't think no one else understands rotations better than i. Yes, i sometimes have altitude like "f34r my math skillz", but it's usually when i'm first to reply with some simple calculations done in mind.”

Wow that is a strong statement, the only think I can say is that it speaks for itself.

Oh maybe you should do more things on paper and less work in your mind, you could get better result, and less misleading would result of such impulsive reactions.

Quote:

“In summary: If you need to store and process variable rotation, quaternions is better than matrices”

perhaps you should rephrase it like this:

If you need to store and process variable rotation, in my opinion quaternions is better than matrices”

As for Eelco: I think there is not point to continue this argument: Eelco I think you should use quaternion in your projects and do not let anyone else to convince you otherwise.

Bye.

on the contrary. maybe this debate is pointless, and i fear it is, but if you would present some arguments as to why quaternions are not superior to matrices for the stated purposes, i would gladly consider them. unfortunatly, your ratio bewteen logical arguments and flames seems to be lowering by the post...

###
#37
Members - Reputation: **863**

Posted 26 October 2004 - 04:33 AM

Quote:

Original post by Anonymous Poster

Rotation of a vector by a quaternion while it is possible it is about8 time more expensivethan rotation by the matrix equivalent.

False, it's of the same order of magnitude. I benchmark math functions all day long for my math lib. I remember a thread where I compared Q*V against M<-Q then M*V. The balance point was around 6 vertices. So you can easilly infer from these data, that Q*V is roughly 100%-50% slower than M*V. This depends on wether you use the FPU, 3DNow or SSE. Thus if you have less than 6 vertices. Quat*Vert is fine. Else with more verts the Quat to Matrix transfo, will be marginal.

Quote:

If you do not belief me then you look and two mayor graphics apis OpenGL and direct3d they use matrices to form matrix stack, not quaternion.

You missed the point. It's first principally because matrices are not isomorph to quaternions. Matrices enable projections, non uniform scaling etc ... It's only for this reason : generality of the matrices that any RENDERER uses them in their API. It's also because the RENDERER obviously prefers to deal with matrices, the fastest way to transform many vertices, as you said it's the fastest general way.

**BUT :**

Obviously accumulation of rotations between frames concerns ANIMATION which is obviously of the user responsibility and the IN TOPIC for this thread. Quaternions are more accurate, because made of 4 floats. Implicitely a rotation and a uniform scaling. Exactly what's required, not less not more. That's for this precise reason that you could intuitively understand why it's far less sensible to numeric deviations than general matrices. Now if you want a true math proof Dmytry or I can surely deliver that. Still I have other cats to beat. But you could also possibly Google by yourself. That'd be fair. I also remember my first year in "Mathematiques Superieures", the teacher warned us very clearly that matrices are highly unstable numerically. So you could also ask to your local math teacher if you know one.

OpenGL and D3D do not cope with such accumulations. Well you could use the matrix stack this way but be prepared to see your scene shrink and post a noob question like : "Why does my scene disappear ?"

###
#39
Members - Reputation: **1147**

Posted 26 October 2004 - 04:56 AM

Quote:

Original post by Anonymous Poster

You two are hopeless.

Quote:

“1:Eelco gave keyword for search, therefore, he answered original question. I just failed to remember that it's named after Gramm-Schmidt, so i posted a solution.”

I am glad he is learning that method I was the want who introduced to him after hi gave another erroneous answer to similar question in another tread.

Do you realize that it's not a valid argument?

Quote:

But I think he still needs to understand the meaning of it and not applying it literally.

And not he did not answer the question he just say quaternion are better than matrices.

And that is not truth. Maybe for him they are.

Quote:

“2:It's not needed to rewrite everything, because quaternions and rotation-only matrices represent same thing. They both is concatenated by multiplication, both should have functions like "load to openGL", most methods of both is the same. With good design, it's not a problem to replace one by other. Also, it's probably better if OP will rewrite it now than later, because probably the only thing he need to rewrite now is camera class.”

You are totally wrong man quaternion do not concatenate by multiplication, I just proved to you, just do it yourself, before making such statements,

Where you proved? Proved what?

Read the <href="http://www.flipcode.com/documents/matrfaq.html">FAQ

or gamedev version

and mathworld, look at end of page.

Quaternion rotations

**is**concatenated by quaternion multiplication.

Quote:

and yes replacing the transformation in a major re-factoring job that may bring a whole new bag of bugs. It may not even be possible, as I read the person is using directx.

First time hear that quaternions is inpossible in directx.... interesting.

Quote:

Quote:

“3:I don't think no one else understands rotations better than i. Yes, i sometimes have altitude like "f34r my math skillz", but it's usually when i'm first to reply with some simple calculations done in mind.”

Wow that is a strong statement, the only think I can say is that it speaks for itself.

Oh maybe you should do more things on paper and less work in your mind, you could get better result, and less misleading would result of such impulsive reactions.

Maybe i have incorrectly computed angles and axises of your example quaternions?

Please catch at least one formal mistake i made in calculations before suggesting me what to do in paper.

Quote:

Quote:

“In summary: If you need to store and process variable rotation, quaternions is better than matrices”

perhaps you should rephrase it like this:

If you need to store and process variable rotation, in my opinion quaternions is better than matrices”

As for Eelco: I think there is not point to continue this argument: Eelco I think you should use quaternion in your projects and do not let anyone else to convince you otherwise.

Bye.

Bye.

[edit: quote tags fixed]

[Edited by - Dmytry on October 26, 2004 1:56:12 PM]

###
#40
Members - Reputation: **1147**

Posted 26 October 2004 - 05:15 AM

Quote:

Original post by minorlogic

Just a note.

In algorithm that i posted before, not used SQRT routine.

When you do cross and normalization , you use SQRT or invSQRT.

Ya gde-to pro eto chital dage, vozmozhno tvoi tekst. Eto ti pridumal pervii? (moy quaternion-to-matrix prosto delaet mashtabiruuschuu matricu esli quaternion ne edinichnoi dlini). Mozhno novuu temu zapostit' , a-to tut opyat' anonimi vodu mutyat (delo konchitsya tem chto voobche zapretyat anonimov nafig). Eto osnovanno na idee chto esli podelit matricu na dlinu v kvadrate, poluchim pravilnuu matricu?

Yes, i readed about it somewhere. Maybe it was your text. It's interesting. It uses idea that if we scale resulting matrix by 1/squared_quaternion_length, we get matrix without scaling?

2everyone logged in: what you think about this quaternion from matrix and quaternion to matrix formules? Is it's known?