Advertisement

Matrix inverse

Started by March 07, 2001 02:33 PM
1 comment, last by Mr Cucumber 23 years, 10 months ago
Sorry to bother you with yet another question I am trying to find out how to calculate the matrix inverse and I found the D3D helper function that calculates the inverse:
HRESULT D3DMath_MatrixInvert( D3DMATRIX& q, D3DMATRIX& a )
{
    if( fabs(a._44 - 1.0f) > .001f)
        return E_INVALIDARG;
    if( fabs(a._14) > .001f || fabs(a._24) > .001f || fabs(a._34) > .001f )
        return E_INVALIDARG;

    FLOAT fDetInv = 1.0f / ( a._11 * ( a._22 * a._33 - a._23 * a._32 ) -
                             a._12 * ( a._21 * a._33 - a._23 * a._31 ) +
                             a._13 * ( a._21 * a._32 - a._22 * a._31 ) );

    q._11 =  fDetInv * ( a._22 * a._33 - a._23 * a._32 );
    q._12 = -fDetInv * ( a._12 * a._33 - a._13 * a._32 );
    q._13 =  fDetInv * ( a._12 * a._23 - a._13 * a._22 );
    q._14 = 0.0f;

    q._21 = -fDetInv * ( a._21 * a._33 - a._23 * a._31 );
    q._22 =  fDetInv * ( a._11 * a._33 - a._13 * a._31 );
    q._23 = -fDetInv * ( a._11 * a._23 - a._13 * a._21 );
    q._24 = 0.0f;

    q._31 =  fDetInv * ( a._21 * a._32 - a._22 * a._31 );
    q._32 = -fDetInv * ( a._11 * a._32 - a._12 * a._31 );
    q._33 =  fDetInv * ( a._11 * a._22 - a._12 * a._21 );
    q._34 = 0.0f;

    q._41 = -( a._41 * q._11 + a._42 * q._21 + a._43 * q._31 );
    q._42 = -( a._41 * q._12 + a._42 * q._22 + a._43 * q._32 );
    q._43 = -( a._41 * q._13 + a._42 * q._23 + a._43 * q._33 );
    q._44 = 1.0f;

    return S_OK;
} 
The thing I dont understand is the part where the q._41, q._42 and q._43 are calculated. I thought q was the matrix being calculated so how can it be in the calculation? Is it a zero matrix, an identity matrix or what is it?
If you look closely at the code you will notice that the fields q._41 to q._44 are calculated only from the values q._11 to q._34, which are calculated by the upper lines in the function. So the function just reuses previously calculated results. You do not need to fill in the matrix q.

The function just calculates the inverse of the 3x3 matrix a11 to a33 and uses that to calculate the missing row and column to make a 4x4 matrix. (check any book on linear algebra)


--- axelP
--- axelP
Advertisement
Thanks.
I should have noticed that.

This topic is closed to new replies.

Advertisement