Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Math API [Vector] [FXX_04].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(FS_SET) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_SET)( const TYPE fX, const TYPE fy, const TYPE fZ, const TYPE fW )
{
    V(TgVEC)                          vResult;

    vResult.m_aData[0] = fX;
    vResult.m_aData[1] = fy;
    vResult.m_aData[2] = fZ;
    vResult.m_aData[3] = fW;

    return (vResult);
}


/* ---- V(FS_SETP) ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_SETP)( const TYPE fX, const TYPE fy, const TYPE fZ )
{
    V(TgVEC)                          vResult;

    vResult.m_aData[0] = fX;
    vResult.m_aData[1] = fy;
    vResult.m_aData[2] = fZ;
    vResult.m_aData[3] = MKL(1.0);

    return (vResult);
}


/* ---- V(FS_SETV) ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_SETV)( const TYPE fX, const TYPE fy, const TYPE fZ )
{
    V(TgVEC)                          vResult;

    vResult.m_aData[0] = fX;
    vResult.m_aData[1] = fy;
    vResult.m_aData[2] = fZ;
    vResult.m_aData[3] = MKL(0.0);

    return (vResult);
}


/* ---- V(F_SETP) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_SETP)( V(CPCU_TgVEC) pv0 )
{
    V(TgVEC)                          vResult;

    vResult.m_aData[0] = pv0->m_aData[0];
    vResult.m_aData[1] = pv0->m_aData[1];
    vResult.m_aData[2] = pv0->m_aData[2];
    vResult.m_aData[3] = MKL(1.0);

    return (vResult);
}


/* ---- V(F_SETV) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_SETV)( V(CPCU_TgVEC) pv0 )
{
    V(TgVEC)                          vResult;

    vResult.m_aData[0] = pv0->m_aData[0];
    vResult.m_aData[1] = pv0->m_aData[1];
    vResult.m_aData[2] = pv0->m_aData[2];
    vResult.m_aData[3] = MKL(0.0);

    return (vResult);
}


/* ---- V(F_Set_Ortho) -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_Set_Ortho)( V(CPCU_TgVEC) pv0 )
{
    const TYPE                          fX = F(tgPM_ABS)( pv0->m_aData[0] );
    const TYPE                          fY = F(tgPM_ABS)( pv0->m_aData[1] );
    const TYPE                          fZ = F(tgPM_ABS)( pv0->m_aData[2] );

    if (fX < fY && fX < fZ)
    {
        return (V(FS_SETV)(MKL(0.0), fZ, -fY));
    }
    else if (fY < fZ)
    {
        return (V(FS_SETV)(fZ, MKL(0.0), -fX));
    }
    else
    {
        return (V(FS_SETV)(fY, -fX, MKL(0.0)));
    };
}


/* ---- V(F_Init_Basis_From_Vector) ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID V(F_Init_Basis_From_Vector)( V(PCU_TgVEC) pvB0, V(PCU_TgVEC) pvB1, V(CPCU_TgVEC) pvA )
{
    const TYPE                          fXX_ZZ = pvA->m_aData[0] * pvA->m_aData[0] + pvA->m_aData[2] * pvA->m_aData[2];
    const TYPE                          fXZ = F(tgPM_SQRT)( fXX_ZZ );

    TgERROR(TgTRUE == V(F_Is_Vector_Valid)(pvA) && TgTRUE == F(tgCM_NR1)(V(F_LSQ)(pvA)));

    if (F(tgCM_NR0)(fXZ))
    {
        *pvB0 = V(KTgV_UNIT_X);
        *pvB1 = V(KTgV_UNIT_Z);
    }
    else
    {
        const TYPE                          fXY = pvA->m_aData[0] * pvA->m_aData[1];
        const TYPE                          fYZ = pvA->m_aData[1] * pvA->m_aData[2];
        const TYPE                          fInvXZ = MKL(1.0) / fXZ;

        *pvB0 = V(FS_SETV)( -pvA->m_aData[2] * fInvXZ, MKL(0.0), pvA->m_aData[0] * fInvXZ );
        *pvB1 = V(FS_SETV)( -fXY, fXX_ZZ, -fYZ );
    };
}


/* ---- V(F_Is_Point_Valid) --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL V(F_Is_Point_Valid)( V(CPCU_TgVEC) pv0 )
{
    return (TgTRUE != F(tgPM_NAN)(pv0->m.x) && TgTRUE != F(tgPM_NAN)( pv0->m.y) && TgTRUE != F(tgPM_NAN)(pv0->m.z) && (MKL(1.0) == pv0->m.w) ? TgTRUE : TgFALSE);
}


/* ---- V(F_Is_Vector_Valid) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL V(F_Is_Vector_Valid)( V(CPCU_TgVEC) pv0 )
{
    return (TgTRUE != F(tgPM_NAN)(pv0->m.x) && TgTRUE != F(tgPM_NAN)( pv0->m.y) && TgTRUE != F(tgPM_NAN)(pv0->m.z) && (MKL(0.0) == pv0->m.w) ? TgTRUE : TgFALSE);
}


/* ---- V(F_DOT3) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TYPE V(F_DOT3)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    return (pv0->m_aData[0] * pv1->m_aData[0] + pv0->m_aData[1] * pv1->m_aData[1] + pv0->m_aData[2] * pv1->m_aData[2]);
}


/* ---- V(F_CX) --------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_CX)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    return (V(FS_SETV)(
        pv0->m_aData[1] * pv1->m_aData[2] - pv0->m_aData[2] * pv1->m_aData[1],
        pv0->m_aData[2] * pv1->m_aData[0] - pv0->m_aData[0] * pv1->m_aData[2],
        pv0->m_aData[0] * pv1->m_aData[1] - pv0->m_aData[1] * pv1->m_aData[0]
    ));
}


/* ---- V(F_UCX) -------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_UCX)( V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    V(TgVEC)                          vRet;
    TYPE                            fLength;

    vRet = V(F_CX)( pv0, pv1 );
    return (V(F_NORM_LEN)(&fLength, &vRet ));
}


/* ---- V(F_UCX_LEN) ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_UCX_LEN)( PCU_TYPE ptyLength, V(CPCU_TgVEC) pv0, V(CPCU_TgVEC) pv1 )
{
    V(TgVEC)                          vRet;

    vRet = V(F_CX)( pv0, pv1 );
    return (V(F_NORM_LEN)( ptyLength, &vRet ));
}


/* ---- V(F_NORM3_LEN) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_NORM3_LEN)( TYPE * __restrict const pfLength, V(CPCU_TgVEC) pvS0 )
{
    const TYPE                          fa0 = F(tgPM_ABS)( pvS0->m_aData[0] );
    const TYPE                          fa1 = F(tgPM_ABS)( pvS0->m_aData[1] );
    const TYPE                          fa2 = F(tgPM_ABS)( pvS0->m_aData[2] );
    const TYPE                          fM0 = F(tgCM_MAX)( fa0, fa1 );
    const TYPE                          fM1 = F(tgCM_MAX)( fa1, fa2 );
    const TYPE                          fMX = F(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 = F(tgPM_SQRT)( f0*f0 + f1*f1 + f2*f2 );
    const TYPE                          fInvLength = fLength > F(KTgEPS) ? MKL(1.0) / fLength : MKL(0.0);

    *pfLength = fLength*fMX;

    return (V(FS_SET)( f0*fInvLength, f1*fInvLength, f2*fInvLength, pvS0->m_aData[3] ) );
}


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

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


/* ---- V(F_PRX3) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL V(F_PRX3)( V(CPC_TgVEC) pV0, V(CPC_TgVEC) pV1 )
{
    V(TgVEC)                          vDiff;

    vDiff = V(F_SUB)( pV0, pV1 );
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.x ))
        return (TgFALSE);
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.y ))
        return (TgFALSE);
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.z ))
        return (TgFALSE);

    if (TgTRUE != F(tgCM_NR0)( vDiff.m.x*vDiff.m.x + vDiff.m.y*vDiff.m.y + vDiff.m.z*vDiff.m.z ))
        return (TgFALSE);

    return (TgTRUE);
}


/* ---- V(F_PRX4) ------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL V(F_PRX4)( V(CPCU_TgVEC) pV0, V(CPCU_TgVEC) pV1 )
{
    V(TgVEC)                          vDiff;

    vDiff = V(F_SUB)( pV0, pV1 );
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.x ))
        return (TgFALSE);
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.y ))
        return (TgFALSE);
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.z ))
        return (TgFALSE);
    if (TgTRUE != F(tgCM_NR0)( vDiff.m.w ))
        return (TgFALSE);

    if (TgTRUE != F(tgCM_NR0)( vDiff.m.x*vDiff.m.x + vDiff.m.y*vDiff.m.y + vDiff.m.z*vDiff.m.z + vDiff.m.w*vDiff.m.w ))
        return (TgFALSE);

    return (TgTRUE);
}


/* ---- V(FS_QT_Init_Axis_Angle) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_QT_Init_Axis_Angle)( const TYPE fX, const TYPE fY, const TYPE fZ, const TYPE fAngle )
{
    TYPE                                fSinA, fCosA;
    V(TgVEC)                            vRet;

#if TgCOMPILE_ASSERT
    vRet = V(FS_SETV)( fX, fY, fZ );
    TgERROR( TgTRUE == F(tgCM_NR1)( V(F_LEN)(&vRet) ));
    TgERROR( TgTRUE != F(tgPM_NAN)( fAngle ) );
#endif

    F(tgPM_SINCOS)( &fSinA, &fCosA, MKL(0.5)*fAngle );
    vRet.m.x = fX * fSinA;
    vRet.m.y = fY * fSinA;
    vRet.m.z = fZ * fSinA;
    vRet.m.w = fCosA;

    return (vRet);
}


/* ---- V(F_QT_Init_Axis_Angle) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_Init_Axis_Angle)( V(CPCU_TgVEC) pvAA )
{
    TgPARAM_CHECK( nullptr != pvAA );
    return (V( FS_QT_Init_Axis_Angle )( pvAA->m.x, pvAA->m.y, pvAA->m.z, pvAA->m.w ));
}


/* ---- V(FS_QT_Init_Euler) --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_QT_Init_Euler)( const TYPE fX, const TYPE fy, const TYPE fZ )
{
    TYPE                                fSinZ, fCosZ, fSinX, fCosX, fSinY, fCosY; /* [ROLL, PITCH, YAW] */
    V(TgVEC)                            vRet;

    F(tgPM_SINCOS)( &fSinX, &fCosX, MKL(0.5)*fX ); /*« Roll */
    F(tgPM_SINCOS)( &fSinY, &fCosY, MKL(0.5)*fy ); /*« Pitch */
    F(tgPM_SINCOS)( &fSinZ, &fCosZ, MKL(0.5)*fZ ); /*« Yaw */

    {
        const TYPE                          fTA = fSinX*fCosY;
        const TYPE                          fTB = fCosX*fSinY;
        const TYPE                          fTC = fCosX*fCosY;
        const TYPE                          fTD = fSinX*fSinY;

        vRet.m.x = fTA*fCosZ - fTB*fSinZ;
        vRet.m.y = fTB*fCosZ + fTA*fSinZ;
        vRet.m.z = fTC*fSinZ - fTD*fCosZ;
        vRet.m.w = fTC*fCosZ + fTD*fSinZ;
    }

    return (vRet);
}


/* ---- V(F_QT_Init_Euler) ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_Init_Euler)( V(CPCU_TgVEC) pvEul )
{
    return (V(FS_QT_Init_Euler)( pvEul->m.x, pvEul->m.y, pvEul->m.z ));
}


/* ---- V(FS_QT_Init_EulerX) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_QT_Init_EulerX)( const TYPE fX )
{
    TYPE                                fSinX, fCosA;
    V(TgVEC)                            vRet;

    F(tgPM_SINCOS)( &fSinX, &fCosA, MKL(0.5)*fX);
    vRet.m.x = fSinX;
    vRet.m.y = MKL(0.0);
    vRet.m.z = MKL(0.0);
    vRet.m.w = fCosA;

    return (vRet);
}


/* ---- V(FS_QT_Init_EulerY) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_QT_Init_EulerY)( const TYPE fY )
{
    TYPE                                fSinY, fCosA;
    V(TgVEC)                            vRet;

    F(tgPM_SINCOS)( &fSinY, &fCosA, MKL(0.5)*fY);
    vRet.m.x = MKL( 0.0 );
    vRet.m.y = fSinY;
    vRet.m.z = MKL( 0.0 );
    vRet.m.w = fCosA;

    return (vRet);
}


/* ---- V(FS_QT_Init_EulerZ) -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(FS_QT_Init_EulerZ)( const TYPE fZ )
{
    TYPE                                fSinZ, fCosA;
    V(TgVEC)                            vRet;

    F(tgPM_SINCOS)( &fSinZ, &fCosA, MKL(0.5)*fZ );
    vRet.m.x = MKL( 0.0 );
    vRet.m.y = MKL( 0.0 );
    vRet.m.z = fSinZ;
    vRet.m.w = fCosA;

    return (vRet);
}


/* ---- F(FS_Quat2Euler) ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID F(FS_Quat2Euler)( PCU_TYPE ptyX, PCU_TYPE ptyY, PCU_TYPE ptyZ, V(CPCU_TgVEC) pqR0 )
{
    const TYPE fSinY = MKL(2.0) * (pqR0->m_aData[1] * pqR0->m_aData[3] - pqR0->m_aData[0] * pqR0->m_aData[2]);

    if (MKL(1.0) + fSinY <= F(KTgEPS))
    {
        *ptyX = MKL(2.0) * F(tgPM_ATAN2)( pqR0->m_aData[2], pqR0->m_aData[3] );
        *ptyY = -F(KTgF_HALF_PI);
        *ptyZ = MKL(0.0);
    }
    else if (fSinY >= MKL(1.0) - F(KTgEPS))
    {
        *ptyX = MKL(2.0) * F(tgPM_ATAN2)( pqR0->m_aData[0], pqR0->m_aData[3] );
        *ptyY = F(KTgF_HALF_PI);
        *ptyZ = MKL(0.0);
    }
    else
    {
        const TYPE fTMPA = MKL(2.0)*(pqR0->m_aData[1] * pqR0->m_aData[2] + pqR0->m_aData[3] * pqR0->m_aData[0]);
        const TYPE fTMPB = MKL(2.0)*(pqR0->m_aData[0] * pqR0->m_aData[0] + pqR0->m_aData[1] * pqR0->m_aData[1]);
        const TYPE fTMPC = MKL(2.0)*(pqR0->m_aData[0] * pqR0->m_aData[1] + pqR0->m_aData[2] * pqR0->m_aData[3]);
        const TYPE fTMPD = MKL(2.0)*(pqR0->m_aData[1] * pqR0->m_aData[1] + pqR0->m_aData[2] * pqR0->m_aData[2]);

        *ptyX = F(tgPM_ATAN2)( fTMPA, MKL(1.0) - fTMPB );
        *ptyY = F(tgPM_ASIN)( fSinY );
        *ptyZ = F(tgPM_ATAN2)( fTMPC, MKL(1.0) - fTMPD );
    };
}


/* ---- V(F_Quat2Euler) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_Quat2Euler)( V(CPCU_TgVEC) pqR0 )
{
    V(TgVEC)                          vRet;

    F(FS_Quat2Euler)( &vRet.m.x, &vRet.m.y, &vRet.m.z, pqR0 );
    vRet.m.w = MKL(0.0);
    return (vRet);
}


/* ---- V(F_QT_INV) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_INV)( V(CPCU_TgVEC) pqR1 )
{
    V(TgVEC)                            vR0;

    vR0.m.x = -pqR1->m.x;
    vR0.m.y = -pqR1->m.y;
    vR0.m.z = -pqR1->m.z;
    vR0.m.w = pqR1->m.w;

    return (vR0);
}


/* ---- V(F_QT_MUL) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_MUL)( V(CPCU_TgVEC) pqR1, V(CPCU_TgVEC) pqR2 )
{
    V(TgVEC)                            vR0;

    vR0.m.x = pqR1->m.w * pqR2->m.x + pqR1->m.x * pqR2->m.w + pqR1->m.y * pqR2->m.z - pqR1->m.z * pqR2->m.y;
    vR0.m.y = pqR1->m.w * pqR2->m.y + pqR1->m.y * pqR2->m.w + pqR1->m.z * pqR2->m.x - pqR1->m.x * pqR2->m.z;
    vR0.m.z = pqR1->m.w * pqR2->m.z + pqR1->m.z * pqR2->m.w + pqR1->m.x * pqR2->m.y - pqR1->m.y * pqR2->m.x;
    vR0.m.w = pqR1->m.w * pqR2->m.w - pqR1->m.x * pqR2->m.x - pqR1->m.y * pqR2->m.y - pqR1->m.z * pqR2->m.z;
    return (vR0);
}


/* ---- V(F_QT_SLERP) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_SLERP)( const TYPE fdT, V(CPCU_TgVEC) pvR1, V(CPCU_TgVEC) pvR2 )
{
    TYPE                                fCosA;
    V(TgVEC)                           vX0, tvX1, tvX2;
    TYPE                                fF0, fF1;

    if (fdT <= MKL(0.0))
    {
        return (*pvR1);
    }
    else if (fdT >= MKL(1.0))
    {
        return (*pvR2);
    }

    fCosA = V(F_DOT)( pvR1, pvR2 );

    if (fCosA < MKL(0.0))
    {
        fCosA = -fCosA;
        vX0 = V(F_NEG)( pvR2 );
    }
    else
    {
        vX0 = *pvR2;
    };

    if ((MKL(1.0) - fCosA) > F(KTgEPS))
    {
        const TYPE                          fAngle = F(tgPM_ACOS)( fCosA );
        const TYPE                          fInvSinA = MKL(1.0) / F(tgPM_SIN)( fAngle );

        fF0 = F(tgPM_SIN)((MKL(1.0) - fdT) * fAngle) * fInvSinA;
        fF1 = F(tgPM_SIN)(fdT * fAngle) * fInvSinA;
    }
    else
    {
        fF0 = MKL(1.0) - fdT;
        fF1 = fdT;
    }

    tvX1 = V(F_MUL_VS)( pvR1, fF0 );
    tvX2 = V(F_MUL_VS)( &vX0, fF1 );

    return (V(F_ADD)(&tvX1, &tvX2));
}


/* ---- V(F_QT_VECTOR_TO_VECTOR) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_VECTOR_TO_VECTOR)( V(CPC_TgVEC) pvFrom, V(CPC_TgVEC) pvTo )
{
    /* If either the scale or the dot product are invalid this will cause a return of the zero quaternion. */

    const TYPE                          fFm_To = V(F_DOT)( pvFrom, pvTo ); /*« cos(θ) = A*B/|A||B| */
    const TYPE                          fScale = V(F_LEN)( pvFrom )*V(F_LEN)( pvTo );
    TYPE                                fCosA = F(tgPM_FSEL)( fScale - F(KTgEPS), fFm_To / fScale, MKL(1.0) );
    V(C_TgVEC)                          vAxis = V(F_UCX)( pvFrom, pvTo );
    const TYPE                          fSinHA = F(tgPM_SQRT)( (MKL(1.0) - fCosA)*MKL(0.5) );
    const TYPE                          fLimit = F(tgCM_CLP)( fCosA, MKL(1.0), MKL(-1.0) );
    V(TgVEC)                            qRet;

    /* Ensure that the result is within function range (floating point error) */
    /* Axis of rotation would be the vector perpendicular to both inputs, normalized if necessary */
    /* For an axis-angle, need to generate cos(θ/2) and sin(θ/2) */
    /*  Trig Identity: cos(2θ) = cos²(θ) - sin²(θ) = 2*cos²(θ) - 1 = 1 - 2*sin²(θ) */
    /*                 cos(θ/2) = √((1 + cos(θ))/2) */

    qRet.m.x = vAxis.m_aData[0] * fSinHA;
    qRet.m.y = vAxis.m_aData[1] * fSinHA;
    qRet.m.z = vAxis.m_aData[2] * fSinHA;
    qRet.m.w = F(tgPM_SQRT)( (MKL(1.0) + fLimit)*MKL(0.5) );

    return (qRet);
}


/* ---- V(F_QT_TX) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_TX)( V(CPCU_TgVEC) pV0, V(CPCU_TgVEC) pqR0 )
{
    M34(TgMAT)                          mR0;

    /* #PERF: Can definitely be made faster */
    M34(F_Init_Quat)( &mR0, pqR0 );
    return (M34(F_TX_V)(&mR0, pV0));
}


/* ---- V(F_QT_INV_TX) ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_QT_INV_TX)( V(CPCU_TgVEC) pV0, V(CPCU_TgVEC) pqR0 )
{
    V(TgVEC)                            vR0;
    M34(TgMAT)                          mR0;

    /* #PERF: Can definitely be made faster */
    vR0 = V(F_QT_INV)( pqR0 );
    M34(F_Init_Quat)( &mR0, &vR0 );
    return (M34(F_TX_V)( &mR0, pV0 ));
}


/* ---- V(F_Rotate_Vector_EulerX) --------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_Rotate_Vector_EulerX)( V(CPCU_TgVEC) pvV0, const TYPE fX )
{
    TYPE                                fSinX, fCosX;
    V(TgVEC)                           qRet;

    F(tgPM_SINCOS)( &fSinX, &fCosX, fX );
    qRet.m.x = pvV0->m.x;
    qRet.m.y = pvV0->m.y*fCosX - pvV0->m.z*fSinX;
    qRet.m.z = pvV0->m.y*fSinX + pvV0->m.z*fCosX;
    qRet.m.w = pvV0->m.w;

    return (qRet);
}


/* ---- V(F_Rotate_Vector_EulerY) --------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_Rotate_Vector_EulerY)( V(CPCU_TgVEC) pvV0, const TYPE fy )
{
    TYPE                                fSinY, fCosY;
    V(TgVEC)                           qRet;

    F(tgPM_SINCOS)( &fSinY, &fCosY, fy );
    qRet.m.x = pvV0->m.z*fSinY + pvV0->m.x*fCosY;
    qRet.m.y = pvV0->m.y;
    qRet.m.z = pvV0->m.z*fCosY - pvV0->m.x*fSinY;
    qRet.m.w = pvV0->m.w;

    return (qRet);
}


/* ---- V(F_Rotate_Vector_EulerZ) --------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE V(TgVEC) V(F_Rotate_Vector_EulerZ)( V(CPCU_TgVEC) pvV0, const TYPE fZ )
{
    TYPE                                fSinZ, fCosZ;
    V(TgVEC)                           qRet;

    F(tgPM_SINCOS)( &fSinZ, &fCosZ, fZ );
    qRet.m.x = pvV0->m.x*fCosZ - pvV0->m.y*fSinZ;
    qRet.m.y = pvV0->m.x*fSinZ + pvV0->m.y*fCosZ;
    qRet.m.z = pvV0->m.z;
    qRet.m.w = pvV0->m.w;

    return (qRet);
}