Home

Resume

Blog

Teikitu


// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//  »Project«   Teikitu Gaming System (TgS) (∂)
//  »File«      TgS Common - Math API [Matrix] [F] [F].i_inc
//  »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
//  »Version«   4.0
// ------------------------------------------------------------------------------------------------------------------------------ //
//  Copyright: © 2002-2010, Andrew Aye.  All Rights Reserved.
//  This software is free for non-commercial use. Redistribution and use in source and binary forms, with or without modification,
//  are permitted provided that the following conditions are met: 
//    Redistributions of source code must retain this copyright notice, this list of conditions and the following disclaimers. 
//    Redistributions in binary form must reproduce this copyright notice, this list of conditions and the following
//      disclaimers in the documentation and other materials provided with the distribution. 
//  Neither the names of the copyright owner nor the names of its contributors may be used to endorse or promote products derived
//  from this software without specific prior written permission. 
//  The intellectual property rights of the algorithms used reside with Andrew Aye.  You may not use this software, in whole or
//  in part, in support of any commercial product without the express written consent of the author.
//  There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// == Math ====================================================================================================================== //

// ---- IDENTITY MATRIX --------------------------------------------------------------------------------------------------------- //

TgINLINE TgVOID M(F_CLI)( M(PCU_TgMAT) pxM0 )
{
    pxM0->m.f11 = MKL(1.0); pxM0->m.f12 = MKL(0.0); pxM0->m.f13 = MKL(0.0);
    pxM0->m.f21 = MKL(0.0); pxM0->m.f22 = MKL(1.0); pxM0->m.f23 = MKL(0.0);
    pxM0->m.f31 = MKL(0.0); pxM0->m.f32 = MKL(0.0); pxM0->m.f33 = MKL(1.0);
}




// ---- MATRIX ARITHMETIC OPERATIONS -------------------------------------------------------------------------------------------- //

TgINLINE TgVOID M(F_CAT)( M(PCU_TgMAT) pxRet, M(CPCU_TgMAT) pxM0, M(CPCU_TgMAT) pxM1 )
{
    TgUINT32                            ui0;

    for (ui0 = 0; ui0 < 3; ++ui0)
    {
        pxRet->m_atyRowCol[ui0][0] =  pxM0->m_atyRowCol[ui0][0] * pxM1->m_atyRowCol[0][0]
                                    +  pxM0->m_atyRowCol[ui0][1] * pxM1->m_atyRowCol[1][0]
                                    +  pxM0->m_atyRowCol[ui0][2] * pxM1->m_atyRowCol[2][0];

        pxRet->m_atyRowCol[ui0][1] =  pxM0->m_atyRowCol[ui0][0] * pxM1->m_atyRowCol[0][1]
                                    +  pxM0->m_atyRowCol[ui0][1] * pxM1->m_atyRowCol[1][1]
                                    +  pxM0->m_atyRowCol[ui0][2] * pxM1->m_atyRowCol[2][1];

        pxRet->m_atyRowCol[ui0][2] =  pxM0->m_atyRowCol[ui0][0] * pxM1->m_atyRowCol[0][2]
                                    +  pxM0->m_atyRowCol[ui0][1] * pxM1->m_atyRowCol[1][2]
                                    +  pxM0->m_atyRowCol[ui0][2] * pxM1->m_atyRowCol[2][2];
    };
}




// ---- INVERSE FUNCTIONS ------------------------------------------------------------------------------------------------------- //

TgINLINE TgVOID M(F_INV_DET)( M(PCU_TgMAT) pxRet, const TYPE tyDet, M(CPCU_TgMAT) pxM1 )
{
    if (F(tgCM_NR0)( tyDet ))
    {
        TgERROR_MSGF( 0, TgT("%-16.16s(%-32.32s): Matrix is Singular - Zero Matrix returned.\n"), TgT("Math"), TgT("F_INV") );
        M(F_CLR)( pxRet );
    }
    else
    {
        const TYPE                          tyInvDet = MKL(1.0) / tyDet;

        pxRet->m.f11 =  tyInvDet*(pxM1->m.f22 * pxM1->m.f33 - pxM1->m.f23 * pxM1->m.f32);
        pxRet->m.f12 = -tyInvDet*(pxM1->m.f12 * pxM1->m.f33 - pxM1->m.f13 * pxM1->m.f32);
        pxRet->m.f13 =  tyInvDet*(pxM1->m.f12 * pxM1->m.f23 - pxM1->m.f13 * pxM1->m.f22);

        pxRet->m.f21 = -tyInvDet*(pxM1->m.f21 * pxM1->m.f33 - pxM1->m.f23 * pxM1->m.f31);
        pxRet->m.f22 =  tyInvDet*(pxM1->m.f11 * pxM1->m.f33 - pxM1->m.f13 * pxM1->m.f31);
        pxRet->m.f23 = -tyInvDet*(pxM1->m.f11 * pxM1->m.f23 - pxM1->m.f13 * pxM1->m.f21);

        pxRet->m.f31 =  tyInvDet*(pxM1->m.f21 * pxM1->m.f32 - pxM1->m.f22 * pxM1->m.f31);
        pxRet->m.f32 = -tyInvDet*(pxM1->m.f11 * pxM1->m.f32 - pxM1->m.f12 * pxM1->m.f31);
        pxRet->m.f33 =  tyInvDet*(pxM1->m.f11 * pxM1->m.f22 - pxM1->m.f12 * pxM1->m.f21);
    };
}


TgINLINE TgVOID M(F_INV)( M(PCU_TgMAT) pxRet, M(CPCU_TgMAT) pxM0 )
{
    M(F_INV_DET)( pxRet, M(F_DET)( pxM0 ), pxM0 );
}


TgINLINE TYPE M(F_DET)( M(CPCU_TgMAT) pxM0 )
{
    return (
        pxM0->m.f11*(pxM0->m.f22*pxM0->m.f33 - pxM0->m.f23*pxM0->m.f32) +
        pxM0->m.f12*(pxM0->m.f23*pxM0->m.f31 - pxM0->m.f21*pxM0->m.f33) +
        pxM0->m.f13*(pxM0->m.f21*pxM0->m.f32 - pxM0->m.f22*pxM0->m.f31)
    );
}




// ---- TRANSFORMATION ---------------------------------------------------------------------------------------------------------- //

TgINLINE V4(TgVEC) M(F_TX_V4)( M(CPCU_TgMAT) ptxM0, V4(CPCU_TgVEC) pvX0 )
{
    V4(TgVEC)                           tvRet;

    tvRet.m.x = V(F_DOT3_VV)( ptxM0->m_avRow + 0, (V3(CPCU_TgVEC))pvX0 );
    tvRet.m.y = V(F_DOT3_VV)( ptxM0->m_avRow + 1, (V3(CPCU_TgVEC))pvX0 );
    tvRet.m.z = V(F_DOT3_VV)( ptxM0->m_avRow + 2, (V3(CPCU_TgVEC))pvX0 );
    tvRet.m.w = pvX0->m.w;

    return (tvRet);
}




// ---- GET BASIS --------------------------------------------------------------------------------------------------------------- //

TgINLINE V4(TgVEC) M(F_GET_COL_0_V4)( M(CPCU_TgMAT) pxM0 )
{
    return (V4(F_SET_ELEM)( pxM0->m.f11, pxM0->m.f21, pxM0->m.f31, MKL(0.0) ));
}


TgINLINE V4(TgVEC) M(F_GET_COL_1_V4)( M(CPCU_TgMAT) pxM0 )
{
    return (V4(F_SET_ELEM)( pxM0->m.f12, pxM0->m.f22, pxM0->m.f32, MKL(0.0) ));
}


TgINLINE V4(TgVEC) M(F_GET_COL_2_V4)( M(CPCU_TgMAT) pxM0 )
{
    return (V4(F_SET_ELEM)( pxM0->m.f13, pxM0->m.f23, pxM0->m.f33, MKL(0.0) ));
}


TgINLINE V4(TgVEC) M(F_GET_COL_3_V4)( M(CPCU_TgMAT) pxM0 )
{
    return (V4(F_SET_ELEM)( MKL(0.0), MKL(0.0), MKL(0.0), MKL(1.0) ));
}




// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //
//  Scalar Function
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //

// ---- VALIDATION -------------------------------------------------------------------------------------------------------------- //

TgINLINE TgBOOL M(F_NaN)( M(CPCU_TgMAT) pxM0 )
{
    TgUINT32                            uiIndex;

    for (uiIndex = 0; uiIndex < 9; ++uiIndex)
    {
        if (F(tgCM_NaN)( pxM0->m_atyElement[uiIndex] ))
        {
            return (TgTRUE);
        };
    };

    return (TgFALSE);
}




// ---- INIT ROTATION [[0..2][0..2]] -------------------------------------------------------------------------------------------- //

TgINLINE TgVOID M(F_INIT_EUL_V3)( M(PCU_TgMAT) pxRet, V3(CPCU_TgVEC) pvEul )
{
    M(F_SET_EUL_ELEM)( pxRet, pvEul->m.x, pvEul->m.y, pvEul->m.z );
}


TgINLINE TgVOID M(F_INIT_EUL_V4)( M(PCU_TgMAT) pxRet, V4(CPCU_TgVEC) pvEul )
{
    M(F_SET_EUL_ELEM)( pxRet, pvEul->m.x, pvEul->m.y, pvEul->m.z );
}


TgINLINE TgVOID M(F_INIT_EUL_ELEM)( M(PCU_TgMAT) pxRet, C_TYPE  tyX, C_TYPE  tyY, C_TYPE  tyZ )
{
    M(F_SET_EUL_ELEM)( pxRet, tyX, tyY, tyZ );
}


TgINLINE TgVOID M(F_INIT_ELX)( M(PCU_TgMAT) pxRet, C_TYPE  tyX )
{
    M(F_SET_ELX)( pxRet, tyX );
}


TgINLINE TgVOID M(F_INIT_ELY)( M(PCU_TgMAT) pxRet, C_TYPE  tyY )
{
    M(F_SET_ELY)( pxRet, tyY );
}


TgINLINE TgVOID M(F_INIT_ELZ)( M(PCU_TgMAT) pxRet, C_TYPE tyZ )
{
    M(F_SET_ELZ)( pxRet, tyZ );
}


TgINLINE TgVOID M(F_INIT_ROT)( M(PCU_TgMAT) pxRet, V4(CPCU_TgVEC) pvEul )
{
    M(F_SET_ROT)( pxRet, pvEul );
}




// ---- GET BASIS --------------------------------------------------------------------------------------------------------------- //

TgINLINE V3(TgVEC) M(F_GET_COL_0_V3)( M(CPCU_TgMAT) pxM0 )
{
    return (V3(F_SET_ELEM)( pxM0->m.f11, pxM0->m.f21, pxM0->m.f31 ));
}


TgINLINE V3(TgVEC) M(F_GET_COL_1_V3)( M(CPCU_TgMAT) pxM0 )
{
    return (V3(F_SET_ELEM)( pxM0->m.f12, pxM0->m.f22, pxM0->m.f32 ));
}


TgINLINE V3(TgVEC) M(F_GET_COL_2_V3)( M(CPCU_TgMAT) pxM0 )
{
    return (V3(F_SET_ELEM)( pxM0->m.f13, pxM0->m.f23, pxM0->m.f33 ));
}


TgINLINE V3(TgVEC) M(F_GET_COL_3_V3)( M(CPCU_TgMAT) pxM0 )
{
    return (V3(F_SET_ELEM)( MKL(0.0), MKL(0.0), MKL(0.0) ));
}