// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Rectangle-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_RECTANGLE_LINEAR_INL_)
#define _TGS_COLLISION_RECTANGLE_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.

// tgRT0        Parallelogram (Input)
// tvS0         Point (Input)
// tvD0         Direction (Input)

// tvRT0        The point of closest proximity on the parallelogram. (Output)
// tvLN0        The point of closest proximity on the line. (Output)
// tgCL         Container of points resulting from the clip operation. (Output)

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




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

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

template <typename TYPE, int DIM, bool bC0, bool bC1> TgFORCEINLINE
TYPE TTgCSQ_RTLN<TYPE,DIM,bC0,bC1>::DO(
    PC_(VECTOR,DIM) ptvRT0, PC_(VECTOR,DIM) ptvLN0, CR_(RECTANGLE,DIM) tgRT0, M_(VECTOR,DIM) tvS0, M_(VECTOR,DIM) tvD0
) {
    TYPE                                tyT0,tyT1, tyT2, tyDistSq;

    tyDistSq = DO( &tyT0,&tyT1, &tyT2, tgRT0, tvS0,tvD0 );
    tyDistSq = P::FSEL( tyDistSq, tyDistSq, -LIMITS<TYPE>::MAX );

    C_(VECTOR,DIM)                      tvK0 = MATH::F_MUL( tyT0, tgRT0.Query_Edge0() );
    C_(VECTOR,DIM)                      tvK1 = MATH::F_MUL( tyT1, tgRT0.Query_Edge1() );

    *ptvRT0 = MATH::F_ADD( MATH::F_ADD( tgRT0.Query_Origin(), tvK0 ), tvK1 );
    *ptvLN0 = MATH::F_ADD( tvS0, MATH::F_MUL( tyT2, tvD0 ) );

    return (tyDistSq);
};


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

template <typename TYPE, int DIM, bool bC0, bool bC1> TgFORCEINLINE
TgRESULT TTgCLP_RTLN<TYPE,DIM,bC0,bC1>::DO( PC_(CLIP_LIST,DIM) ptgCL, CR_(RECTANGLE,DIM) tgRT0, 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, tgRT0, 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  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////