// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Sphere-Triangle.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_SPHERE_ETRI_INL_)
#define _TGS_COLLISION_SPHERE_ETRI_INL_
#pragma once

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

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

// tgSP0        Sphere (Input)
// tgET0        Edge Triangle (Input)
// tgCT0        Collision Triangle (Input)
// tgPacket     Container of points resulting from a contact generator.  Generation parameters provided as well. (Input/Ouput)

// tvSP0        The point of closest proximity on the sphere. (Output)
// tvET0        The point of closest proximity on the triangle. (Output)
// tyET0        Bary-Centric value for edge 0-1 describing point of closest proximity. (Output)
// tyET1        Bary-Centric value for edge 0-2 describing point of closest proximity. (Output)
// tgPacket     Container of points resulting from a contact generator.  Generation parameters provided as well. (Input/Ouput)

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




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

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

template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_DistSq( CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0 )
{
    const TYPE                          tyDist = F_Dist( tgET0, tgSP0.Query_Origin() ) - tgSP0.Query_Radius();

    return (P::FSEL( tyDist, tyDist*tyDist, -LIMITS<TYPE>::MAX ));
};


template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_Dist( CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0 )
{
    const TYPE                          tyDist = F_Dist( tgET0, tgSP0.Query_Origin() ) - tgSP0.Query_Radius();

    return (P::FSEL( tyDist, tyDist, -LIMITS<TYPE>::MAX ));
};


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

template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_ClosestSq( PC_(VECTOR,DIM) ptvSP0, PC_(VECTOR,DIM) ptvET0, CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0 )
{
    const TYPE                          tyTest = F_Closest( ptvET0, tgET0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_SUB( *ptvET0, tgSP0.Query_Origin() );

    *ptvSP0 = MATH::F_ADD( tgSP0.Query_Origin(), MATH::F_MUL( tgSP0.Query_Radius() / tyDist, tvK0 ) );
    return (P::FSEL( tyDist, (tyDist - tgSP0.Query_Radius())*(tyDist - tgSP0.Query_Radius()), -LIMITS<TYPE>::MAX ));
};


template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_Closest( PC_(VECTOR,DIM) ptvSP0, PC_(VECTOR,DIM) ptvET0, CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0 )
{
    const TYPE                          tyTest = F_Closest( ptvET0, tgET0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_SUB( *ptvET0, tgSP0.Query_Origin() );

    *ptvSP0 = MATH::F_ADD( tgSP0.Query_Origin(), MATH::F_MUL( tgSP0.Query_Radius() / tyDist, tvK0 ) );
    return (P::FSEL( tyDist, tyDist - tgSP0.Query_Radius(), -LIMITS<TYPE>::MAX ));
};


template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_ClosestSq( PC_(VECTOR,DIM) ptvSP0, TYPE *ptyET0, TYPE *ptyET1, CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0 )
{
    const TYPE                          tyTest = F_Closest( ptyET0,ptyET1, tgET0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_MUL( *ptyET0, tgET0.Query_Edge0() );
    C_(VECTOR,DIM)                      tvK1 = MATH::F_MUL( *ptyET1, tgET0.Query_Edge2() );
    C_(VECTOR,DIM)                      tvK2 = MATH::F_ADD( tgET0.Query_Origin(), tvK0 );
    C_(VECTOR,DIM)                      tvK3 = MATH::F_ADD( tgSP0.Query_Origin(), tvK1 );
    C_(VECTOR,DIM)                      tvK4 = MATH::F_SUB( tvK2, tvK3 );

    *ptvSP0 = MATH::F_ADD( tgSP0.Query_Origin(), MATH::F_MUL( tgSP0.Query_Radius() / tyDist, tvK4 ) );
    return (P::FSEL( tyDist, (tyDist - tgSP0.Query_Radius())*(tyDist - tgSP0.Query_Radius()), -LIMITS<TYPE>::MAX ));
};


template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_Closest(PC_(VECTOR,DIM) ptvSP0, TYPE *ptyET0, TYPE *ptyET1, CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0)
{
    const TYPE                          tyTest = F_Closest( ptyET0,ptyET1, tgET0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_MUL( *ptyET0, tgET0.Query_Edge0() );
    C_(VECTOR,DIM)                      tvK1 = MATH::F_MUL( *ptyET1, tgET0.Query_Edge2() );
    C_(VECTOR,DIM)                      tvK2 = MATH::F_ADD( tgET0.Query_Origin(), tvK0 );
    C_(VECTOR,DIM)                      tvK3 = MATH::F_ADD( tgSP0.Query_Origin(), tvK1 );
    C_(VECTOR,DIM)                      tvK4 = MATH::F_SUB( tvK2, tvK3 );

    *ptvSP0 = MATH::F_ADD( tgSP0.Query_Origin(), MATH::F_MUL( tgSP0.Query_Radius() / tyDist, tvK4 ) );
    return (P::FSEL( tyDist, tyDist - tgSP0.Query_Radius(), -LIMITS<TYPE>::MAX ));
};


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

template <typename TYPE, int DIM> TgFORCEINLINE
TgBOOL F_Contact_Test( CR_(SPHERE,DIM) tgSP0, CR_(ETRI,DIM) tgET0 )
{
    const TYPE                          tyDist = F_Dist( tgET0, tgSP0.Query_Origin() ) - tgSP0.Query_Radius();
    return (tyDist < TYPE(0.0));
};


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

template <typename TYPE, int DIM> TgFORCEINLINE
TgRESULT F_Contact_Penetrate( PC_(CONTACT_PACKET,DIM) ptgPacket, CR_(CTRI,DIM) tgCT0, CR_(SPHERE,DIM) tgSP0 )
{
    TgASSERT((TgSIZE)ptgPacket->m_iStride >= sizeof( P_(CONTACT,DIM) ))
    TgASSERT(tgSP0.Is_Valid() && tgCT0.Is_Valid())

    if (0 == ptgPacket->m_niMaxContact || ptgPacket->m_niContact >= ptgPacket->m_niMaxContact || NULL == ptgPacket->m_ptgContact)
    {
        return (TgE_FAIL);
    };

    C_(VECTOR,DIM)                      tvDS = MATH::F_SUB( tgSP0.Query_Origin(), tgCT0.Query_Origin() );
    const TYPE                          tyDS_N = MATH::F_DOT( tvDS, tgCT0.Query_Normal() );

    //  Ignore penetration if the centroid (origin) of the sphere is below the plane.  No penetration if its more than radius
    // off of the plane.
    if (tyDS_N <= TYPE(0.0) || tyDS_N > tgSP0.Query_Radius())
    {
        return (TgE_NOINTERSECT);
    };

    return (F_Internal_Penetrate( ptgPacket, tgCT0, tgSP0 ));
};


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

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