Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Math API [Vector] [F].i_inc
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, 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:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution 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.
    The name of the author may not 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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Common ============================================================================================================================================================ */

/* ---- V(F_ADD) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_ADD)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    #define EQN(A) (pv0->m_aData[A] + pv1->m_aData[A])
    VEC_ASSIGN_EQN;
    #undef EQN
}

/* ---- V(F_MAD) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_MAD)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1, V(CPCU_TgVEC) pv2 )
{
    #define EQN(A) pv0->m_aData[A]*pv1->m_aData[A] + pv2->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_NMS) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NMS)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1, V(CPCU_TgVEC) pv2 )
{
    #define EQN(A) -(pv0->m_aData[A]*pv1->m_aData[A] - pv2->m_aData[A])
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_DOT) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE V(F_DOT)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    return (
          pv0->m_aData[0] * pv1->m_aData[0]
    #if (DIM > 1)
        + pv0->m_aData[1] * pv1->m_aData[1]
    #endif
    #if (DIM > 2)
        + pv0->m_aData[2] * pv1->m_aData[2]
    #endif
    #if (DIM > 3)
        + pv0->m_aData[3] * pv1->m_aData[3]
    #endif
    #if (DIM > 4)
        + pv0->m_aData[4] * pv1->m_aData[4]
    #endif
    #if (DIM > 5)
        + pv0->m_aData[5] * pv1->m_aData[5]
    #endif
    #if (DIM > 6)
        + pv0->m_aData[6] * pv1->m_aData[6]
    #endif
    #if (DIM > 7)
        + pv0->m_aData[7] * pv1->m_aData[7]
    #endif
    #if (DIM > 8)
        + pv0->m_aData[8] * pv1->m_aData[8]
    #endif
    #if (DIM > 9)
        + pv0->m_aData[9] * pv1->m_aData[9]
    #endif
    #if (DIM > 10)
        + pv0->m_aData[10] * pv1->m_aData[10]
    #endif
    #if (DIM > 11)
        + pv0->m_aData[11] * pv1->m_aData[11]
    #endif
    #if (DIM > 12)
        + pv0->m_aData[12] * pv1->m_aData[12]
    #endif
    #if (DIM > 13)
        + pv0->m_aData[13] * pv1->m_aData[13]
    #endif
    #if (DIM > 14)
        + pv0->m_aData[14] * pv1->m_aData[14]
    #endif
    #if (DIM > 15)
        + pv0->m_aData[15] * pv1->m_aData[15]
    #endif
    #if (DIM > 16)
        #error DIM Exceeds expectations
    #endif
        );
}


/* ---- V(F_MUL) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_MUL)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    #define EQN(A) pv0->m_aData[A] * pv1->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_DIV) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_DIV)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    #define EQN(A) pv0->m_aData[A] / pv1->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_SUB) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_SUB)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    #define EQN(A) pv0->m_aData[A] - pv1->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_LSQ) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE V(F_LSQ)( V(CPCU_TgVEC) pv0 )
{
    return (V(F_DOT)( pv0, pv0) );
}


/* ---- V(F_LEN) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE V(F_LEN)( V(CPCU_TgVEC) pv0 )
{
    return (T(tgPM_SQRT)( V(F_LSQ)(pv0)) );
}


/* ---- V(F_NEG) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NEG)( V(CPCU_TgVEC) pv0 )
{
    #define EQN(A) pv0->m_aData[A] * MKL(-1.0)
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_SQRT) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_SQRT)( V(CPCU_TgVEC) pv0 )
{
    #define EQN(A) T(tgPM_SQRT)( pv0->m_aData[A] )
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_RSQRT) ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_RSQRT)( V(CPCU_TgVEC) pv0 )
{
    #define EQN(A) MKL(1.0) / T(tgPM_SQRT)( pv0->m_aData[A] )
    VEC_ASSIGN_EQN;
    #undef EQN
}


#if (DIM==3)
/* ---- V(F_NORM_LEN) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NORM_LEN)( TYPE *pfLength, V(CPCU_TgVEC) pvS0 )
{
    const TYPE                          fa0 = T(tgPM_ABS)( pvS0->m_aData[0] );
    const TYPE                          fa1 = T(tgPM_ABS)( pvS0->m_aData[1] );
    const TYPE                          fa2 = T(tgPM_ABS)( pvS0->m_aData[2] );
    const TYPE                          fM0 = T(tgCM_MAX)( fa0, fa1 );
    const TYPE                          fM1 = T(tgCM_MAX)( fa1, fa2 );
    const TYPE                          fMX = T(tgCM_MAX)( fM0, fM1 );
    const TYPE                          f0 = pvS0->m_aData[0] / fMX;
    const TYPE                          f1 = pvS0->m_aData[1] / fMX;
    const TYPE                          f2 = pvS0->m_aData[2] / fMX;
    const TYPE                          fLength = T(tgPM_SQRT)( f0*f0 + f1*f1 + f2*f2 );
    const TYPE                          fInvLength = fLength > T(KTgEPS) ? MKL(1.0) / fLength : MKL(0.0);
    V(TgVEC)                            vRet;

    *pfLength = fLength*fMX;

    vRet.m.x = f0*fInvLength;
    vRet.m.y = f1*fInvLength;
    vRet.m.z = f2*fInvLength;

    return (vRet);
}
#elif (DIM==4)
/* ---- V(F_NORM_LEN) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NORM_LEN)( TYPE *pfLength, V(CPCU_TgVEC) pvS0 )
{
    const TYPE                          fa0 = T(tgPM_ABS)( pvS0->m_aData[0] );
    const TYPE                          fa1 = T(tgPM_ABS)( pvS0->m_aData[1] );
    const TYPE                          fa2 = T(tgPM_ABS)( pvS0->m_aData[2] );
    const TYPE                          fa3 = T(tgPM_ABS)( pvS0->m_aData[3] );
    const TYPE                          fM0 = T(tgCM_MAX)( fa0, fa1 );
    const TYPE                          fM1 = T(tgCM_MAX)( fa2, fa3 );
    const TYPE                          fMX = T(tgCM_MAX)( fM0, fM1 );
    const TYPE                          f0 = pvS0->m_aData[0] / fMX;
    const TYPE                          f1 = pvS0->m_aData[1] / fMX;
    const TYPE                          f2 = pvS0->m_aData[2] / fMX;
    const TYPE                          f3 = pvS0->m_aData[3] / fMX;
    const TYPE                          fLength = T(tgPM_SQRT)( f0*f0 + f1*f1 + f2*f2 + f3*f3 );
    const TYPE                          fInvLength = fLength > T(KTgEPS) ? MKL(1.0) / fLength : MKL(0.0);

    *pfLength = fLength*fMX;

    return (V(FS_SET)( f0*fInvLength, f1*fInvLength, f2*fInvLength, f3*fInvLength) );
}
#else
/* ---- V(F_NORM_LEN) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NORM_LEN)( TYPE *pfLength, V(CPCU_TgVEC) pv0 )
{
    *pfLength = V(F_LEN)( pv0 );
    return (V(F_DIV_VS)( pv0, *pfLength ) );
}
#endif


/* ---- V(F_NORM) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NORM)( V(CPCU_TgVEC) pv0 )
{
    TYPE                            fLength;

    return (V(F_NORM_LEN)( &fLength, pv0 ) );
}


/* ---- V(F_FLR) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_FLR)( V(CPCU_TgVEC) pv0 )
{
    #define EQN(A) T(tgPM_FLOOR)( pv0->m_aData[A] )
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_CEL) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_CEL)( V(CPCU_TgVEC) pv0 )
{
    #define EQN(A) T(tgPM_CEIL)( pv0->m_aData[A] )
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_CLP) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_CLP)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pvMin, V(CPCU_TgVEC) pvMax )
{
    #define EQN(A) T(tgCM_CLP)( pv0->m_aData[A], pvMin->m_aData[A], pvMax->m_aData[A] )
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_SAT) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_SAT)( V(CPCU_TgVEC) pv0 )
{
    #define EQN(A) T(tgCM_CLP)( pv0->m_aData[A], MKL(0.0), MKL(1.0) )
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_NaN) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL V(F_NaN)( V(CPCU_TgVEC) pv0 )
{
    TgUINT32                            uiIndex = 0;

    for (uiIndex = 0; uiIndex < DIM; ++uiIndex)
    {
        if (T(tgPM_NAN)(pv0->m_aData[uiIndex]))
        {
            return (TgTRUE);
        };
    };

    return (TgFALSE);
}


/* ---- V(F_BND) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL V(F_BND)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pvBound )
{
    TgUINT32                            uiIndex = 0;

    for (uiIndex = 0; uiIndex < DIM; ++uiIndex)
    {
        if ((-pvBound->m_aData[uiIndex] > pv0->m_aData[uiIndex]) || (pv0->m_aData[uiIndex] > pvBound->m_aData[uiIndex]))
        {
            return (TgFALSE);
        };
    };

    return (TgTRUE);
}


/* ---- V(F_MAD_SVV) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_MAD_SVV)( const TYPE fScalar, V(CPCU_TgVEC) pv1, V(CPCU_TgVEC) pv2 )
{
    #define EQN(A) fScalar*pv1->m_aData[A] + pv2->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_MAD_VSV) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_MAD_VSV)( V(CPCU_TgVEC) pv0, const TYPE fScalar, V(CPCU_TgVEC) pv2 )
{
    #define EQN(A) pv0->m_aData[A]*fScalar + pv2->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_NMS_SVV) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NMS_SVV)( const TYPE fScalar, V(CPCU_TgVEC) pv1, V(CPCU_TgVEC) pv2 )
{
    #define EQN(A) -(fScalar*pv1->m_aData[A] - pv2->m_aData[A])
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_NMS_VSV) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NMS_VSV)( V(CPCU_TgVEC) pv0, const TYPE fScalar, V(CPCU_TgVEC) pv2 )
{
    #define EQN(A) -(pv0->m_aData[A]*fScalar - pv2->m_aData[A])
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_MUL_SV) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_MUL_SV)( const TYPE fScalar, V(CPCU_TgVEC) pv1 )
{
    #define EQN(A) fScalar * pv1->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_MUL_VS) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_MUL_VS)( V(CPCU_TgVEC) pv0, const TYPE fScalar )
{
    #define EQN(A) pv0->m_aData[A] * fScalar
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_DIV_SV) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_DIV_SV)( const TYPE fScalar, V(CPCU_TgVEC) pv1 )
{
    #define EQN(A) fScalar / pv1->m_aData[A]
    VEC_ASSIGN_EQN;
    #undef EQN
}


/* ---- V(F_DIV_VS) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_DIV_VS)( V(CPCU_TgVEC) pv0, const TYPE fScalar )
{
    const TYPE fInvScalar = MKL(1.0) / fScalar;
    #define EQN(A) pv0->m_aData[A] * fInvScalar
    VEC_ASSIGN_EQN;
    #undef EQN
}