// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Box-Linear.inl
//  Author:    Andrew Aye (EMail: andrew.aye@gmail.com, Web: http://www.andrewaye.com) 
//  Version:   3.11
//
// ------------------------------------------------------------------------------------------------------------------------------ //
//
//  Copyright: © 2002-2008, 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".
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#if !defined(_TGS_COLLISION_BOX_LINEAR_INL_)
#define _TGS_COLLISION_BOX_LINEAR_INL_
#pragma once

// ============================================================================================================================== //

// Functions used for internal use - primitive based functions should be the primary system calls.

//  A linear is a generic term used to describe the set of 1D primitives.  To avoid code duplication these functions are normally
// created through templates where two boolean template parameters are used to indicate if the free variable is closed on a
// particular side of the number line.

// tgBX0        Box (Input)
// i0           An index to indicate which face of the box is to perform the clipping operation. (Input)
// tvS0         Point (Input)
// tvD0         Direction (Input)

// tvBX0        The point of closest proximity on the box. (Output)
// tvT0         The point of closest proximity on the linear. (Output)
// tgCL         Container of points resulting from the clip operation.
// tyT0         Parametric parameter to generate point of interest based on the linear. (Output)
// tyT1         Parametric parameter to generate point of interest based on the linear. (Output)

// ============================================================================================================================== //




namespace TGS { // START TGS ///////////////////////////////////////////////////////////////////////////////////////////////////////
namespace COL { // START COL ///////////////////////////////////////////////////////////////////////////////////////////////////////

// ============================================================================================================================== //

template <typename TYPE, int DIM, bool bC0, bool bC1> TgFORCEINLINE
TYPE TTgCSQ_BXLN<TYPE,DIM,bC0,bC1>::DO(
    PC_(VECTOR,DIM) ptvBX0, PC_(VECTOR,DIM) ptvT0, CR_(BOX,DIM) tgBX0, M_(VECTOR,DIM) tvS0, M_(VECTOR,DIM) tvD0
) {
    TYPE                                tyB0,tyB1,tyB2, tyT1;

    TYPE tyDistSq = DO( &tyB0,&tyB1,&tyB2, &tyT1, tgBX0, tvS0,tvD0 );
    *ptvBX0 = tgBX0.Calc_Point( tyB0,tyB1,tyB2 );
    *ptvT0 = MATH::F_ADD( tvS0, MATH::F_MUL( tyT1, tvD0 ) );
    return (P::FSEL( tyDistSq, tyDistSq, -LIMITS<TYPE>::MAX ));
};


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

template <typename TYPE, int DIM, bool bC0, bool bC1> TgFORCEINLINE
TgRESULT TTgCLP_BXLN<TYPE,DIM,bC0,bC1>::DO( PC_(CLIP_LIST,DIM) ptgCL, CR_(BOX,DIM) tgBX0, M_(VECTOR,DIM) tvS0, M_(VECTOR,DIM) tvD0 )
{
    if (ptgCL->m_niMax < 2)
    {
        return (TgE_FAIL);
    };

    TYPE                                tyT0, tyT1;

    C_TgRESULT tgResult = DO( &tyT0,&tyT1, tgBX0, tvS0,tvD0 );
    
    ptgCL->m_ptvPoint[0] = MATH::F_ADD( tvS0, MATH::F_MUL( tyT0, tvD0 ) );
    ptgCL->m_ptvPoint[1] = MATH::F_ADD( tvS0, MATH::F_MUL( tyT1, tvD0 ) );

    if (TgFAILED( tgResult ) || (bC0 && tyT0 < TYPE(0.0) && tyT1 <= TYPE(0.0)) || (bC1 && tyT0 >= TYPE(1.0) && tyT1 > TYPE(1.0)))
    {
        ptgCL->m_niPoint = 0;
        return (TgE_NOINTERSECT);
    }

    ptgCL->m_niPoint = 2;
    return (TgS_OK);
};


template <typename TYPE, int DIM, bool bC0, bool bC1> TgFORCEINLINE
TgRESULT TTgCLP_BXLN<TYPE,DIM,bC0,bC1>::DO( TYPE *ptyT0, TYPE *ptyT1, CR_(BOX,DIM) tgBX0, C_TgINT i0, M_(VECTOR,DIM) tvS0, M_(VECTOR,DIM) tvD0 )
{
    T_(RECTANGLE,DIM)                   tgFace;

    switch (i0) {
        case 0:
            tgFace.Set(
                tgBX0.Calc_Point( tgBX0.Query_Extent0(), tgBX0.Query_Extent1(), tgBX0.Query_Extent2() ),
                MATH::F_MUL( -tgBX0.Query_Extent1(), tgBX0.Query_AxisUnit1() ),
                MATH::F_MUL( -tgBX0.Query_Extent2(), tgBX0.Query_AxisUnit2() ),
                tgBX0.Query_AxisUnit0()
            ); break;
        case 1:
            tgFace.Set(
                tgBX0.Calc_Point( tgBX0.Query_Extent0(), tgBX0.Query_Extent1(), tgBX0.Query_Extent2() ),
                MATH::F_MUL( -tgBX0.Query_Extent0(), tgBX0.Query_AxisUnit0() ),
                MATH::F_MUL( -tgBX0.Query_Extent2(), tgBX0.Query_AxisUnit2() ),
                tgBX0.Query_AxisUnit1()
            ); break;
        case 2:
            tgFace.Set(
                tgBX0.Calc_Point( tgBX0.Query_Extent0(), tgBX0.Query_Extent1(), tgBX0.Query_Extent2() ),
                MATH::F_MUL( -tgBX0.Query_Extent0(), tgBX0.Query_AxisUnit0() ),
                MATH::F_MUL( -tgBX0.Query_Extent1(), tgBX0.Query_AxisUnit1() ),
                tgBX0.Query_AxisUnit2()
            ); break;
        default:
            TgASSERT( TgFALSE );
            return (TgFALSE);
    };

    return (TTgCLP_RTLN<TYPE,DIM,bC0,bC1>::DO( ptyT0, ptyT1, tgFace, tvS0, tvD0 ));
};


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

template <typename TYPE, int DIM, bool bC0, bool bC1> TgFORCEINLINE
TgRESULT TTgCLP_BXLN<TYPE,DIM,bC0,bC1>::DO( PC_(CLIP_LIST,DIM) ptgCL, CR_(BOX,DIM) tgBX0, C_TgINT i0, M_(VECTOR,DIM) tvS0, M_(VECTOR,DIM) tvD0 )
{
    if (ptgCL->m_niMax < 2)
    {
        return (TgE_FAIL);
    };

    TYPE                                tyT0, tyT1;

    C_TgRESULT tgResult = DO( &tyT0,&tyT1, tgBX0,i0, tvS0,tvD0 );
    
    ptgCL->m_ptvPoint[0] = MATH::F_ADD( tvS0, MATH::F_MUL( tyT0, tvD0 ) );
    ptgCL->m_ptvPoint[1] = MATH::F_ADD( tvS0, MATH::F_MUL( tyT1, tvD0 ) );

    if (TgFAILED( tgResult ) || (bC0 && tyT0 < TYPE(0.0) && tyT1 <= TYPE(0.0)) || (bC1 && tyT0 >= TYPE(1.0) && tyT1 > TYPE(1.0)))
    {
        ptgCL->m_niPoint = 0;
        return (TgE_NOINTERSECT);
    }

    ptgCL->m_niPoint = 2;
    return (TgS_OK);
};


// ============================================================================================================================== //

}; // END COL //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}; // END TGS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //  END  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////