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

TgVOID V(tgGM_BA_PC)( V(PCU_TgBOXAA) ptgBA0, V(CPCU_TgPARTICLE) ptgPC1, const TYPE tyT )
{
    V(C_TgVEC)                          tvX0 = *(V(tgGM_Query_Position_PC)( ptgPC1 ));
    V(C_TgVEC)                          tvX1 = V(tgGM_Query_Position_Time_PC)( ptgPC1, tyT );

    TgGEOM_ASSERT_PARAM(V(tgGM_Is_Valid_PC)( ptgPC1 ) && !F(tgCM_NaN)( tyT ) && tyT >= MKL(0.0));

    V(tgGM_Init_PT_BA)( ptgBA0, &tvX0 );
    V(tgGM_Union_PT_BA)( ptgBA0, &tvX1 );

    // Check for points of inflexion during the time interval

    if (!F(tgCM_NR0)( ptgPC1->m_tvAccel.m.x ) && ptgPC1->m_tvVel.m.x * ptgPC1->m_tvAccel.m.x < MKL(0.0))
    {
        const TYPE                          tyPoI = -ptgPC1->m_tvVel.m.x / ptgPC1->m_tvAccel.m.x;

        if (tyPoI > MKL(0.0) && tyPoI < tyT)
        {
            const TYPE tyTMP = ptgPC1->m_tvPos.m.x + tyPoI*ptgPC1->m_tvVel.m.x + MKL(0.5)*tyPoI*tyPoI*ptgPC1->m_tvAccel.m.x;

            V(tgGM_Set_MinX_BA)( ptgBA0, F(tgPM_FSEL)(tyTMP - V(tgGM_Query_MinX_BA)( ptgBA0 ), V(tgGM_Query_MinX_BA)( ptgBA0 ), tyTMP) );
            V(tgGM_Set_MaxX_BA)( ptgBA0, F(tgPM_FSEL)(V(tgGM_Query_MaxX_BA)( ptgBA0 ) - tyTMP, V(tgGM_Query_MaxX_BA)( ptgBA0 ), tyTMP) );
        };
    };

    if (!F(tgCM_NR0)( ptgPC1->m_tvAccel.m.y ) && ptgPC1->m_tvVel.m.y * ptgPC1->m_tvAccel.m.y < MKL(0.0))
    {
        const TYPE                          tyPoI = -ptgPC1->m_tvVel.m.y / ptgPC1->m_tvAccel.m.y;

        if (tyPoI > MKL(0.0) && tyPoI < tyT)
        {
            const TYPE tyTMP = ptgPC1->m_tvPos.m.y + tyPoI*ptgPC1->m_tvVel.m.y + MKL(0.5)*tyPoI*tyPoI*ptgPC1->m_tvAccel.m.y;

            V(tgGM_Set_MinY_BA)( ptgBA0, F(tgPM_FSEL)(tyTMP - V(tgGM_Query_MinY_BA)( ptgBA0 ), V(tgGM_Query_MinY_BA)( ptgBA0 ), tyTMP) );
            V(tgGM_Set_MaxY_BA)( ptgBA0, F(tgPM_FSEL)(V(tgGM_Query_MaxY_BA)( ptgBA0 ) - tyTMP, V(tgGM_Query_MaxY_BA)( ptgBA0 ), tyTMP) );
        };
    };

    if (!F(tgCM_NR0)( ptgPC1->m_tvAccel.m.z ) && ptgPC1->m_tvVel.m.z * ptgPC1->m_tvAccel.m.z < MKL(0.0))
    {
        const TYPE                          tyPoI = -ptgPC1->m_tvVel.m.z / ptgPC1->m_tvAccel.m.z;

        if (tyPoI > MKL(0.0) && tyPoI < tyT)
        {
            const TYPE tyTMP = ptgPC1->m_tvPos.m.z + tyPoI*ptgPC1->m_tvVel.m.z + MKL(0.5)*tyPoI*tyPoI*ptgPC1->m_tvAccel.m.z;

            V(tgGM_Set_MinZ_BA)( ptgBA0, F(tgPM_FSEL)(tyTMP - V(tgGM_Query_MinZ_BA)( ptgBA0 ), V(tgGM_Query_MinZ_BA)( ptgBA0 ), tyTMP) );
            V(tgGM_Set_MaxZ_BA)( ptgBA0, F(tgPM_FSEL)(V(tgGM_Query_MaxZ_BA)( ptgBA0 ) - tyTMP, V(tgGM_Query_MaxZ_BA)( ptgBA0 ), tyTMP) );
        };
    };
}


TgVOID V(tgGM_Project_PC)( PCU_TYPE ptyMin, PCU_TYPE ptyMax, V(CPCU_TgPARTICLE) ptgPC1, V(CPCU_TgVEC) ptvDN )
{
    const TYPE                          tyS_AX = V(F_DOT_VV)( &ptgPC1->m_tvPos, ptvDN );
    const TYPE                          tyV_AX = V(F_DOT_VV)( &ptgPC1->m_tvVel, ptvDN );
    const TYPE                          tyA_AX = V(F_DOT_VV)( &ptgPC1->m_tvAccel, ptvDN );
    TYPE                                tyTMP;

    TgGEOM_ASSERT_PARAM(V(tgGM_Is_Valid_PC)( ptgPC1 ) && V(F_Is_Vector_Valid)( ptvDN ));

    *ptyMin = *ptyMax = tyS_AX;

    tyTMP = tyS_AX + tyV_AX + MKL(0.5) * tyA_AX;
    if (tyTMP < *ptyMin)
    {
        *ptyMin = tyTMP;
    }
    else if (tyTMP > *ptyMax)
    {
        *ptyMax = tyTMP;
    };

    // Check for points of inflexion during the time interval

    if (!F(tgCM_NR0)( tyA_AX ) && tyV_AX * tyA_AX < MKL(0.0))
    {
        const TYPE                          tyPoI = -tyV_AX / tyA_AX;

        if (tyPoI > MKL(0.0) && tyPoI < MKL(1.0))
        {
            tyTMP = tyS_AX + tyPoI * tyV_AX + MKL(0.5) * tyPoI * tyPoI * tyA_AX;

            if (tyTMP < *ptyMin)
            {
                *ptyMin = tyTMP;
            }
            else if (tyTMP > *ptyMax)
            {
                *ptyMax = tyTMP;
            };
        };
    };
}