// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//  »Project«   Talina Gaming System (TgS) (∂)
//  »File«      TgS Common - Geometry 3D - Box.inl
//  »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".
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //

TgBOOL V(tgGM_Is_Valid_BX)( V(CPCU_TgBOX) ptgBX1 )
{
    if (!V(F_Is_Point_Valid)( &ptgBX1->m.m.tvOrigin ))
    {
        return (TgFALSE);
    };

    if ( /* Axes must be valid vectors and the lengths should be valid positive values. */
        !V(F_Is_Vector_Valid)( ptgBX1->m.m.atvAxis + 0 ) || !(ptgBX1->m_tvExtent.m.x < MKL(0.0)) ||
        !V(F_Is_Vector_Valid)( ptgBX1->m.m.atvAxis + 1 ) || !(ptgBX1->m_tvExtent.m.y < MKL(0.0)) ||
        !V(F_Is_Vector_Valid)( ptgBX1->m.m.atvAxis + 2 ) || !(ptgBX1->m_tvExtent.m.z < MKL(0.0))
    ) {
        return (TgFALSE);
    };

    if (   !F(tgCM_NR1)( V(F_LSQ)( ptgBX1->m.m.atvAxis + 0 ) )
        || !F(tgCM_NR1)( V(F_LSQ)( ptgBX1->m.m.atvAxis + 1 ) )
        || !F(tgCM_NR1)( V(F_LSQ)( ptgBX1->m.m.atvAxis + 2 ) ) )
    {
        return (TgFALSE);
    };

    if ( /* All axes should be normal with respect to each other */
        !F(tgCM_NR0)( V(F_DOT_VV)( ptgBX1->m.m.atvAxis + 0, ptgBX1->m.m.atvAxis + 1 ) ) ||
        !F(tgCM_NR0)( V(F_DOT_VV)( ptgBX1->m.m.atvAxis + 0, ptgBX1->m.m.atvAxis + 2 ) ) ||
        !F(tgCM_NR0)( V(F_DOT_VV)( ptgBX1->m.m.atvAxis + 1, ptgBX1->m.m.atvAxis + 2 ) )
    ) {
        return (TgFALSE);
    };

    return (TgTRUE);
}


TgVOID V(tgGM_BA_BX)( V(PCU_TgBOXAA) ptgBA0, V(CPCU_TgBOX) ptgBX1 )
{
    V(C_TgVEC)                          tvE0 = V(F_MUL_SV)( ptgBX1->m_tvExtent.m.x, ptgBX1->m.m.atvAxis + 0 );
    V(C_TgVEC)                          tvE1 = V(F_MUL_SV)( ptgBX1->m_tvExtent.m.y, ptgBX1->m.m.atvAxis + 1 );
    V(C_TgVEC)                          tvE2 = V(F_MUL_SV)( ptgBX1->m_tvExtent.m.z, ptgBX1->m.m.atvAxis + 2 );
    V(C_TgVEC)                          tvE3 = V(F_SUB_VV)( &tvE1, &tvE0 );
    V(C_TgVEC)                          tvE4 = V(F_SUB_VV)( &tvE2, &tvE0 );
    V(C_TgVEC)                          tvE5 = V(F_SUB_VV)( &tvE2, &tvE1 );

    const TYPE                          tyA0 = F(tgPM_FSEL)( tvE4.m.x, tvE0.m.x, tvE2.m.x );
    const TYPE                          tyA1 = F(tgPM_FSEL)( tvE4.m.y, tvE0.m.y, tvE2.m.y );
    const TYPE                          tyA2 = F(tgPM_FSEL)( tvE4.m.z, tvE0.m.z, tvE2.m.z );
    const TYPE                          tyA3 = F(tgPM_FSEL)( tvE5.m.x, tvE1.m.x, tvE2.m.x );
    const TYPE                          tyA4 = F(tgPM_FSEL)( tvE5.m.y, tvE1.m.y, tvE2.m.y );
    const TYPE                          tyA5 = F(tgPM_FSEL)( tvE5.m.z, tvE1.m.z, tvE2.m.z );

    const TYPE                          tyA6 = F(tgPM_FSEL)(tvE5.m.x, tvE2.m.x, tvE1.m.x);
    const TYPE                          tyA7 = F(tgPM_FSEL)(tvE5.m.y, tvE2.m.y, tvE1.m.y);
    const TYPE                          tyA8 = F(tgPM_FSEL)(tvE5.m.z, tvE2.m.z, tvE1.m.z);
    const TYPE                          tyA9 = F(tgPM_FSEL)(tvE4.m.x, tvE2.m.x, tvE0.m.x);
    const TYPE                          tyAA = F(tgPM_FSEL)(tvE4.m.y, tvE2.m.y, tvE0.m.y);
    const TYPE                          tyAB = F(tgPM_FSEL)(tvE4.m.z, tvE2.m.z, tvE0.m.z);

    const TYPE                          tyX0 = F(tgPM_FSEL)(tvE3.m.x, tyA0, tyA3 );
    const TYPE                          tyY0 = F(tgPM_FSEL)(tvE3.m.y, tyA1, tyA4 );
    const TYPE                          tyZ0 = F(tgPM_FSEL)(tvE3.m.z, tyA2, tyA5 );
    const TYPE                          tyX1 = F(tgPM_FSEL)(tvE3.m.x, tyA6, tyA9 );
    const TYPE                          tyY1 = F(tgPM_FSEL)(tvE3.m.y, tyA7, tyAA );
    const TYPE                          tyZ1 = F(tgPM_FSEL)(tvE3.m.z, tyA8, tyAB );

    V(C_TgVEC)                          tvMin = V(F_SETV_ELEM)( tyX0,tyY0,tyZ0 );
    V(C_TgVEC)                          tvON = V(F_ADD_VV)( &ptgBX1->m.m.tvOrigin, &tvMin );
    V(C_TgVEC)                          tvMax = V(F_SETV_ELEM)( tyX1,tyY1,tyZ1 );
    V(C_TgVEC)                          tvOX = V(F_ADD_VV)( &ptgBX1->m.m.tvOrigin, &tvMax );

    TgGEOM_ASSERT_PARAM(V(tgGM_Is_Valid_BX)( ptgBX1 ));

    V(tgGM_Init_PT_BA)( ptgBA0, &tvON );
    V(tgGM_Union_PT_BA)( ptgBA0, &tvOX );
}