# Fast inversion of homogenous transformation matric

3 replies to this topic

### #1anubis

Senior Member

• Members
• 2225 posts

Posted 08 September 2004 - 05:05 PM

matrix inversion in general is a slow and tedious process. however in computer graphics we mostly deal with special kinds of matrices that allow for much faster inversion. one such type of matrix is the standard 4 by 4 transformation matrix that contains a rotation and a translation. conveniantly we can invert the rotation and the translation seperatly. the 3x3 rotation matrix is easily inverted by taking the dot product between each pair of row vectors, since it is orthogonal (the vector dot itself is 1 and and 0 for all the others). taking the dot product of the row vectors is equivalent to multiplying the matrix with it's transpose. so the inversion of the rotation part is simply it's transpose

void Matrix4x4::FastInvert()
{
swap(m[0][1], m[1][0]);
swap(m[0][2], m[2][0]);
swap(m[1][2], m[2][1]);


the translation part is then equally easy inverted (by taking the negative translation and multiplying it with the inverted rotation matrix, which is the transpose we just calculated).

  float m30 = -(m[0][0] * m[3][0] + m[1][0] * m[3][1] + m[2][0] * m[3][2]);
float m31 = -(m[0][1] * m[3][0] + m[1][1] * m[3][1] + m[2][1] * m[3][2]);
m[3][2] = -(m[0][2] * m[3][0] + m[1][2] * m[3][1] + m[2][2] * m[3][2]);
m[3][1] = m31;
m[3][0] = m30;
}


it should be noted that this only works if the matrix is unscaled because the row vectors of the rotation matrix are required to have unit length. the calculation should be extendable though if the matrix is scaled equally in all directions.
If Prolog is the answer, what is the question ?

### #2anubis

Senior Member

• Members
• 2225 posts

Posted 08 September 2004 - 05:33 PM

i eddited the smileys out because they only showed up as "smiley.gif" when i accessed the code from the front page
If Prolog is the answer, what is the question ?

### #3z80

Valued Member

• Members
• 104 posts

Posted 08 September 2004 - 05:44 PM

anubis said:

  m[3][0] = -(m[0][0] * m[3][0] + m[1][0] * m[3][1] + m[2][0] * m[3][2]);
m[3][1] = -(m[0][1] * m[3][0] + m[1][1] * m[3][1] + m[2][1] * m[3][2]);
m[3][2] = -(m[0][2] * m[3][0] + m[1][2] * m[3][1] + m[2][2] * m[3][2]);
}


Hmm... dont you need some temp variables for m[3][0] and m[3][1] so you dont use their new updated values when m[3][1] and m[3][2] is computed?

### #4anubis

Senior Member

• Members
• 2225 posts

Posted 08 September 2004 - 06:06 PM

right...
i converted the code from my own, which just returns a new matrix so that's how the erorr sneaked in... i will edit it right away. thanks anyway
If Prolog is the answer, what is the question ?

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users