Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Matrix Inverse


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 ZMaster   Members   -  Reputation: 240

Like
Likes
Like

Posted 12 September 2003 - 11:24 PM

Hi, I''m looking for a good algo to calculate the inverse of a 4x4 matrix. I didn''t find anything good with google. I mean, I can have a lot of information about calculating the inverse of a matrix on paper, but that''s mostly impossible to implement as C code So how do you guys calculate matrix inverses in your engines/games/projects? Any code or information would be useful. Thanks, Flo

Sponsor:

#2 T2k   Members   -  Reputation: 220

Like
Likes
Like

Posted 12 September 2003 - 11:32 PM

code snipeds:


//#define _float double

#ifndef _float
#define _float float
#endif

class MATRIX{// Last row is 0, 0, 0, 1, else change class functions( remove // and /* ), frustum changes last row

union{
_float m[4*4]; // m[x][y]= [((x-1)*4) + (y-1)]

struct { // mxy !!!(in definition/here reversed)=>

_float m11, m12, m13, m14, // m11 m21 m31 m41

m21, m22, m23, m24, // m12 m22 m32 m42

m31, m32, m33, m34, // m13 m23 m33 m43

m41, m42, m43, m44; // m14 m24 m34 m44

};
};
...



_float MATRIX::GetM3Determinant(const MATRIX3 &matrix){
return ( matrix.x.x * (matrix.y.y * matrix.z.z - matrix.y.z * matrix.z.y) -
matrix.y.x * (matrix.x.y * matrix.z.z - matrix.x.z * matrix.z.y) +
matrix.z.x * (matrix.x.y * matrix.y.z - matrix.x.z * matrix.y.y) );
}

MATRIX::MATRIX3 MATRIX::GetSubMatrix(long x, long y){
MATRIX3 tmp;

long xoffset= 0;
for(long i=0; i < 4; i++)
if(i != x){
long yoffset= 0;
for(long j= 0; j < 4; j++)
if(j != y){
*( ((_float *) &tmp) + xoffset*3 + yoffset)= m[i*4 + j];
yoffset++;
}
xoffset++;
}
return tmp;
}

_float MATRIX::GetDeterminant(){
_float result= 0;
long i= 1;

MATRIX3 tmp;
for(long n= 0; n < 4; n++, i*=-1){
tmp= GetSubMatrix(0, n);
result+= m[n] * GetM3Determinant(tmp) * i;
}
return result;
}


MATRIX MATRIX::GetCorrectInverse(){
MATRIX inverse;
MATRIX3 tmp;

_float m4determinant= GetDeterminant();

if(fabs(m4determinant) < bias)
inverse= MATRIX(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);// Matrix has no inverse

else{
for( long i=0; i < 4; i++)
for(long j=0; j < 4; j++){
long sign= 1-((i+j)%2)*2;
tmp= GetSubMatrix(i, j);
inverse.m[i + j*4]= GetM3Determinant(tmp) * sign / m4determinant;
}
}
return inverse;
}



i hope you can read the important part, but i think i will edit my post in few minutes...

edit:

class VECTOR3D{
public:
_float x, y, z;
...


struct MATRIX3{
VECTOR3D x,y,z;
};

uhm i dont think it will compile, i have not posted overloaded operators etc...


edit2:

_float MATRIX::GetDeterminant(){//of a 4x4 Matrix!

_float result= 0;
long i= 1;

MATRIX3 tmp;
for(long n= 0; n < 4; n++, i*=-1){
tmp= GetSubMatrix(0, n);
result+= m[n] * GetM3Determinant(tmp) * i;
}
return result;
}

edit3: added source tags (this board eats them...


T2k

[edited by - T2k on September 13, 2003 6:36:55 AM]

[edited by - T2k on September 13, 2003 6:40:18 AM]

[edited by - T2k on September 13, 2003 6:43:13 AM]

#3 ZMaster   Members   -  Reputation: 240

Like
Likes
Like

Posted 12 September 2003 - 11:58 PM

That''s a bit of code
I think I''ll have to read this many times to understand.

So do you obtain submatrices until you get a 2x2 matrix and calculate the determinants like this, or what?

|a b|
|c d| det = ab - cd

I''ll try to implement this in my matrix class then



#4 T2k   Members   -  Reputation: 220

Like
Likes
Like

Posted 13 September 2003 - 12:01 AM

quote:
Original post by ZMaster
|a b|
|c d| det = ab - cd



det= ad - cb


T2k

#5 MV   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 September 2003 - 04:44 AM

Please, help me remember what inversed matrix are used for ? Inverse kynematic maybe ? and what else ?

#6 UnIcron   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 September 2003 - 05:18 AM

Just an observation. While it is important to have a good general method for finding an inverse, always keep in mind that there are several special cases that can greatly simplify things.

For example, if a transformation matrix is built only from rotations, then the inverse of the matrix is guaranteed to be the transpose.

You can also often get an inverse matrix by inverting parameters. For example, a matrix given by
glTranslatef(10.0,10.0,10.0) is guaranteed to be the inverse of
glTranslatef(-10.0, -10.0, -10.0).


[edited by - UnIcron on September 15, 2003 12:20:12 PM]

#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 15 September 2003 - 11:21 AM

MV, in OpenGL is the most common use of the inverse if you want to set the camera. Set it as an object but invert before sendinf it to OpenGL.

#8 rodzilla   Members   -  Reputation: 133

Like
Likes
Like

Posted 16 September 2003 - 03:30 AM

In that case, isn''t it easier to store the camera''s position and orientation as a vector and a quaternion ?


SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.
The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)

#9 ZMaster   Members   -  Reputation: 240

Like
Likes
Like

Posted 16 September 2003 - 05:31 AM

Well, thanks. I got a good routine now. I''m building submatrices (first 3x3, then 2x2) and just calculate the inverse of those (like T2k suggested).

Thank for the tips UnIcron. Should I implement some different routines for performance reason? I''ll think about that - maybe I should test it

Basicly: The matrix multiplied by it''s inverse equals the identity matrix.
I don''t have a practical example in mind now, but you must sometimes use a matrix inverse when converting coordinates to different spaces. I''m coding some math classes for my engine, and I wanted to implement an Invert routine.

@rodzilla: That''s easier, sure, but it always depends on what camera you want to set up. I finished my 3rd person camera class some minutes ago. I use matrices in it very frequently. There is a function Perspective(), that calculates a perspective projection matrix and uses it in OpenGL (just like gluPerspective) for example. Moreover I have a matrix that takes the camera transforms (rotation, translation). I''s multiplied with the current modelview matrix every frame, so I could easily write a function, that automatically tracks an object (like glLookAt, but more automatic) just by making some changes to the camera transform matrix.


#10 circuit   Members   -  Reputation: 134

Like
Likes
Like

Posted 16 September 2003 - 07:06 AM

float Determinant4f(const float m[16])
{
return
m[12]*m[9]*m[6]*m[3]-
m[8]*m[13]*m[6]*m[3]-
m[12]*m[5]*m[10]*m[3]+
m[4]*m[13]*m[10]*m[3]+
m[8]*m[5]*m[14]*m[3]-
m[4]*m[9]*m[14]*m[3]-
m[12]*m[9]*m[2]*m[7]+
m[8]*m[13]*m[2]*m[7]+
m[12]*m[1]*m[10]*m[7]-
m[0]*m[13]*m[10]*m[7]-
m[8]*m[1]*m[14]*m[7]+
m[0]*m[9]*m[14]*m[7]+
m[12]*m[5]*m[2]*m[11]-
m[4]*m[13]*m[2]*m[11]-
m[12]*m[1]*m[6]*m[11]+
m[0]*m[13]*m[6]*m[11]+
m[4]*m[1]*m[14]*m[11]-
m[0]*m[5]*m[14]*m[11]-
m[8]*m[5]*m[2]*m[15]+
m[4]*m[9]*m[2]*m[15]+
m[8]*m[1]*m[6]*m[15]-
m[0]*m[9]*m[6]*m[15]-
m[4]*m[1]*m[10]*m[15]+
m[0]*m[5]*m[10]*m[15];
}

BOOL GenerateInverseMatrix4f(float i[16], const float m[16])
{
float x=Determinant4f(m);
if (x==0) return FALSE;

i[0]= (-m[13]*m[10]*m[7] +m[9]*m[14]*m[7] +m[13]*m[6]*m[11]
-m[5]*m[14]*m[11] -m[9]*m[6]*m[15] +m[5]*m[10]*m[15])/x;
i[4]= ( m[12]*m[10]*m[7] -m[8]*m[14]*m[7] -m[12]*m[6]*m[11]
+m[4]*m[14]*m[11] +m[8]*m[6]*m[15] -m[4]*m[10]*m[15])/x;
i[8]= (-m[12]*m[9]* m[7] +m[8]*m[13]*m[7] +m[12]*m[5]*m[11]
-m[4]*m[13]*m[11] -m[8]*m[5]*m[15] +m[4]*m[9]* m[15])/x;
i[12]=( m[12]*m[9]* m[6] -m[8]*m[13]*m[6] -m[12]*m[5]*m[10]
+m[4]*m[13]*m[10] +m[8]*m[5]*m[14] -m[4]*m[9]* m[14])/x;
i[1]= ( m[13]*m[10]*m[3] -m[9]*m[14]*m[3] -m[13]*m[2]*m[11]
+m[1]*m[14]*m[11] +m[9]*m[2]*m[15] -m[1]*m[10]*m[15])/x;
i[5]= (-m[12]*m[10]*m[3] +m[8]*m[14]*m[3] +m[12]*m[2]*m[11]
-m[0]*m[14]*m[11] -m[8]*m[2]*m[15] +m[0]*m[10]*m[15])/x;
i[9]= ( m[12]*m[9]* m[3] -m[8]*m[13]*m[3] -m[12]*m[1]*m[11]
+m[0]*m[13]*m[11] +m[8]*m[1]*m[15] -m[0]*m[9]* m[15])/x;
i[13]=(-m[12]*m[9]* m[2] +m[8]*m[13]*m[2] +m[12]*m[1]*m[10]
-m[0]*m[13]*m[10] -m[8]*m[1]*m[14] +m[0]*m[9]* m[14])/x;
i[2]= (-m[13]*m[6]* m[3] +m[5]*m[14]*m[3] +m[13]*m[2]*m[7]
-m[1]*m[14]*m[7] -m[5]*m[2]*m[15] +m[1]*m[6]* m[15])/x;
i[6]= ( m[12]*m[6]* m[3] -m[4]*m[14]*m[3] -m[12]*m[2]*m[7]
+m[0]*m[14]*m[7] +m[4]*m[2]*m[15] -m[0]*m[6]* m[15])/x;
i[10]=(-m[12]*m[5]* m[3] +m[4]*m[13]*m[3] +m[12]*m[1]*m[7]
-m[0]*m[13]*m[7] -m[4]*m[1]*m[15] +m[0]*m[5]* m[15])/x;
i[14]=( m[12]*m[5]* m[2] -m[4]*m[13]*m[2] -m[12]*m[1]*m[6]
+m[0]*m[13]*m[6] +m[4]*m[1]*m[14] -m[0]*m[5]* m[14])/x;
i[3]= ( m[9]* m[6]* m[3] -m[5]*m[10]*m[3] -m[9]* m[2]*m[7]
+m[1]*m[10]*m[7] +m[5]*m[2]*m[11] -m[1]*m[6]* m[11])/x;
i[7]= (-m[8]* m[6]* m[3] +m[4]*m[10]*m[3] +m[8]* m[2]*m[7]
-m[0]*m[10]*m[7] -m[4]*m[2]*m[11] +m[0]*m[6]* m[11])/x;
i[11]=( m[8]* m[5]* m[3] -m[4]*m[9]* m[3] -m[8]* m[1]*m[7]
+m[0]*m[9]* m[7] +m[4]*m[1]*m[11] -m[0]*m[5]* m[11])/x;
i[15]=(-m[8]* m[5]* m[2] +m[4]*m[9]* m[2] +m[8]* m[1]*m[6]
-m[0]*m[9]* m[6] -m[4]*m[1]*m[10] +m[0]*m[5]* m[10])/x;

return TRUE;
}

[edited by - circuit on September 16, 2003 2:08:36 PM]

#11 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 16 September 2003 - 02:11 PM

rodzilla, I think that matrices are better for the camera because they are easier to set/read than quaternions for the average programmer.
http://www.sjbaker.org/steve/omniv/matrices_can_be_your_friends.html

#12 nfz   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 September 2003 - 05:05 AM

Here is another technique that I use for finding the inverse of a 4x4 matrix.
Note: the matrices used are the OpenGL Column major format

Step 1. Transpose the 3x3 rotation portion of the 4x4 matrix to get the inverse rotation

MatrixInvRotate(mtxin, mtxout);

the MatrixInvRotate code is:

void MatrixInvRotate(glMatrix mtxin, glMatrix mtxout)
{
for(int i=0 ; i<3; i++) {
for(int j=0; j<3; j++) {
mtxout[j][i] = mtxin[i][j];
}
}
}

Step 2. negate the translation vector portion of the 4x4 matrix and then rotate it using the newly constructed 3x3 rotation matrix

msVec3 vTmp, vTmp2;

vTmp[0] = -mtxin[3][0];
vTmp[1] = -mtxin[3][1];
vTmp[2] = -mtxin[3][2];

VectorRotate(vTmp, mtxout, vTmp2);


The VectorRotate code is straight forward:

void VectorRotate (const msVec3 vin, const glMatrix mtx, msVec3 vout)
{
vout[0] = vin[0]*mtx[0][0] + vin[1]*mtx[1][0] + vin[2]*mtx[2][0];
vout[1] = vin[0]*mtx[0][1] + vin[1]*mtx[1][1] + vin[2]*mtx[2][1];
vout[2] = vin[0]*mtx[0][2] + vin[1]*mtx[1][2] + vin[2]*mtx[2][2];
}

Step 3. put the new translation vector into the new 4x4 matrix

mtxout[3][0] = vTmp2[0];
mtxout[3][1] = vTmp2[1];
mtxout[3][2] = vTmp2[2];

Step 4. do some house cleaning so that the matrix can be used with OpenGL

mtxout[0][3] = mtxout[1][3] = mtxout[2][3] = 0.0f;
mtxout[3][3] = 1.0f;


You now have the inverse of the original 4x4 matrix.

I use inverse matrices for cameras, bone animation and skinning using bone weights, and object picking.



#13 MV   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 September 2003 - 01:13 AM

quote:

I use inverse matrices for cameras, bone animation and skinning using bone weights, and object picking.



Could you make a bit more details ?
For bone animation : is it for inverse kynematics ?
For skinning using bone weights : are you using vertex buffers ?



#14 nfz   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 September 2003 - 01:50 AM

For bone animation the inverse matrix representing bone position and rotation is required for doing both forward and inverse kynematics. The inverse matrix of the bone position and rotation is also required for skinning because I store the vertices in object space rather than bone space since each vertex can be attached to more than one bone. Doing it this way makes for slightly simpler code that runs faster especially as a vertex program on a GPU(ie vertex shader).

For the skinning I support two rendering paths using the OpenGL API. If the card supports ARB vertex program then all the vertex attribute data (position vector, bone weights etc) is offloaded to the GPU and vertex blending is executed on the GPU and its very fast. If the graphics card has a fixed function pipeline then the vertex blending is done on the CPU so I don''t use vertex buffers.

#15 MV   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 September 2003 - 03:02 AM

quote:
Original post by nfz
For bone animation the inverse matrix representing bone position and rotation is required for doing both forward and inverse kynematics.


What is forward kynematic ?

quote:
Original post by nfz
The inverse matrix of the bone position and rotation is also required for skinning because I store the vertices in object space rather than bone space since each vertex can be attached to more than one bone.


Yeah, that''s a problem for me... Did you deeply study the subject, and is it the only solution you found ? Cause using this method, you should make vertex per vertex matrix computation that should be very heavy in thems of CPU work. You should at least make 2 matrix multiplication for EACH vertex, in my case this should kill my frame rate


quote:
Doing it this way makes for slightly simpler code that runs faster especially as a vertex program on a GPU(ie vertex shader).

i''m afraid my english is a bit weak, so I didnt all understand :D, but I think this means the same I wrote before ???

#16 nfz   Members   -  Reputation: 122

Like
Likes
Like

Posted 18 September 2003 - 11:01 AM

Forward kinematics (FK) is the propagation of motion from the parent joint down the chain to all of its children. An example is if the parent rotates then all the children rotate with the parent. FK is the simple part of kinematics to solve and is used in most applications and games that support bone animation.

Inverse kinematics (IK) is the reverse, ie if the child joint moves it effects its parent and on up the chain. IK in real time is much harder to solve and code and is more computationaly expensive so you don''t see it used in games much.

There are a few different ways of performing vertex blending and no I wouldn''t say I have studied it in depth so the way I am doing it now may not be the most efficient but it works and gives decent frame rates on middle of the road pc hardware. My target frame rate is 40fps with 10 animated objects(machines and creatures) in the forground at a screen resolution of 1024x768 24bpp using a GF2 and 1Ghz Athlon. Each model is low poly having about 800 triangles and the scenery has about 1800 visible triangles so in total there are no more than 10000 triangles being pumped through. On my dev machine at work (Radeon 8500 128 meg and AthlonXP 1.63 Ghz) I get about 130fps (CPU vertex blending) with each model having 4 animation streams being blended at the same time at a resolution of 1280x1024 32 bpp. Using a vertex shader and a vertex array on the GPU, the FPS jumps to about 270. Todays hardware allows these techniques to be used so now I am learning to use them. A year and a half ago I wouldn''t have considered it.

The bone matrix calculations are done at the beginning of each frame but before vertex processing. But, yes, vertex blending is expensive in CPU time and its a balance you have to play between good animation and frame rates.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS