4

I'm working on an OpenGL project in Java, and it has come to point where I'd like to create the transformation matrices in my own code, so i can use them to do world-to-screen point transformations, and vice versa. I've created a Matrix class with support for transformations, and that is all working quite nicely. However, I'm having trouble actually figuring out how to create an inverse transform.

So my question is this:

  • Given an arbitrary affine (4x4) transformation matrix, how do you create the inverse transformation matrix? Are some matrices uninvertible? What are the limitations and caveats of inverting a transformation matrix?

From my research, I've heard various methods of doing so, with the simplest being to transpose then negate the matrix. However, this doesn't seem to be actually working. I've heard that this method doesn't work on some matrices, and even that some matrices are uninvertible.

I'm looking for more than just a "plug in this equation" answer, because I'd actually like to understand what's going on when I invert a matrix. This also excludes "just use this library" answers. I might move to a matrix library in the future, but for now I'd like to create it myself.

Edit: Before anyone asks, this is NOT homework. This is a personal project.

Edit: Apparently there's a whole list of strategies for calculating inverse matrices here: http://en.wikipedia.org/wiki/Invertible_matrix

jpfx1342
  • 868
  • 8
  • 14
  • 2
    Are you trying to break it back down into scale, rotate, translate components? I don't think there's a mathematically perfect way to do this, but there are techniques for pulling out the components that "make sense" – nielsbot Oct 03 '12 at 19:30
  • 1
    No, as I understand it, the inverse of a transformation matrix is the matrix that "reverses" it's operations. So if I had a matrix that was "translate(1, 2, 3), rotate(45deg)", applying it to a point would apply those operations, while applying it's inverse would "undo" or "reverse" those operations. – jpfx1342 Oct 03 '12 at 20:30

1 Answers1

2

Here is some code that I used in my Computer Graphics course, basically I used the Gauss Jordan elimination for calculating the inverse of a matrix. For a matrix to be invertible its determinant value must be not equal to zero. I have not handled that case in my code though, I am not going to do it all for you.

Matrix4* Matrix4::FindInverse(Matrix4 &a){

int n = R;
int i = 0;
int j = 0;
float pivot = 0;
Matrix4* invA = NULL;
//TODO: Check whether the matrix is invertible.Else Return
invA = new Matrix4();
invA->SetMatrix4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);


for(i = 0; i < n; i++){
    pivot = a.v[i][i];

    if(pivot != 1.0 and pivot != 0){
        for(int t = i; t < n; t++){
            a.v[i][t] = a.v[i][t]/pivot;
            invA->v[i][t] = invA->v[i][t]/pivot;
        }
    }

    //Update to the new pivot which must be 1.0
    pivot = a.v[i][i];

    for(j = 0; j < n; j++){
        if( j==i ){
            continue;

        }
        else{
            float l = a.v[j][i]/pivot;
            for(int m = 0; m < n; m++){
                a.v[j][m] = a.v[j][m] - l * a.v[i][m];
                invA->v[j][m] = invA->v[j][m] - (l * invA->v[i][m]);
            }
        }
    }
}
return invA;

}

Apurv
  • 4,458
  • 2
  • 21
  • 31
  • That's alright, like I said, I'd like this to be a learning experience. – jpfx1342 Oct 03 '12 at 20:26
  • That's quite useful. I can't believe I never found this. I dug around on Wikipedia and Google for hours and never found it. I've also heard mention that transformation matrices are rarely singular, so this should work nicely. I'll still include a determinant check for robustness, but I appreciate this. – jpfx1342 Oct 03 '12 at 20:42