// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Plane-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_PLANE_LINEAR_INL_)
#define _TGS_COLLISION_PLANE_LINEAR_INL_
#pragma once

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

// F_Dist[Sq], F_Closest[Sq] - Return the minimal distance [squared] between the primitives or negative type max if intersecting.

//  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.

// tgPN0        Plane (Input)
// tvS0         Point (Input)
// tvD0         Direction (Input)

// tyT0         Parametric parameter to generate point of interest based on the line. (Output)
// tyT1         Parametric parameter to generate point of interest based 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
TgRESULT TTgCLP_PNLN<TYPE,DIM,bC0,bC1>::DO( TYPE *ptyT0, TYPE *ptyT1, CR_(PLANE,DIM) tgPN0, M_(VECTOR,DIM) tvS0, M_(VECTOR,DIM) tvD0 )
{
    TgCOMPILER_ASSERT( !(!bC0 && bC1) )

    const TYPE                          tyC0 = F_Dist( tgPN0, tvS0 );
    const TYPE                          tyC1 = F_Dist( tgPN0, MATH::F_ADD( tvS0, tvD0 ) );

    if (bC0 && bC1)
    {
        *ptyT0 = P::FSEL( tyC0, TYPE(0.0), tyC0 / (tyC1 - tyC0) );
        *ptyT1 = P::FSEL( tyC1, TYPE(1.0), tyC1 / (tyC1 - tyC0) );
    }
    else if (bC0 && !bC1)
    {
        *ptyT0 = P::FSEL( tyC0, TYPE(0.0), tyC0 / (tyC1 - tyC0) );
        *ptyT1 = P::FSEL( tyC1, P::FSEL( tyC0 - tyC1, tyC1 / (tyC1 - tyC0), LIMITS<TYPE>::MAX ), tyC1 / (tyC1 - tyC0) );
    }
    else
    {
        *ptyT0 = P::FSEL( tyC0, P::FSEL( tyC0 - tyC1,-LIMITS<TYPE>::MAX, tyC0 / (tyC1 - tyC0) ), tyC0 / (tyC1 - tyC0) );
        *ptyT1 = P::FSEL( tyC1, P::FSEL( tyC0 - tyC1, tyC1 / (tyC1 - tyC0), LIMITS<TYPE>::MAX ), tyC1 / (tyC1 - tyC0) );
    }

    return (tyC0 >= TYPE(0.0) || tyC1 >= TYPE(0.0) ? TgS_OK : TgE_FAIL);
};


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