// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Sphere-Ray.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_RAY_INL_)
#define _TGS_COLLISION_SPHERE_RAY_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) - Points of penetration will be generated on this primitive.
// tgRY0        Ray (Input)
// tgPacket     Container of points resulting from a contact generator.  Generation parameters provided as well. (Input/Ouput)
// tyPM         Current normalized time of first contact. (Input/Output)
// bPenetrate   If the swept primitives are in penetration, if true the function will return points of penetration. (Input)
// tgDT         A structure holding the swept primitive displacement for the entire duration of the test period. (Input)

// tvSP0        The point of closest proximity on the sphere. (Output)
// tvRY0        The point of closest proximity on the ray. (Output)
// tgPacket     Container of points resulting from a contact generator.  Generation parameters provided as well. (Input/Ouput)
// tyPM         Current normalized time of first contact. (Input/Output)
// tyRY0        Parametric parameter to generate point of interest #1 based on the ray. (Output)
// tyRY1        Parametric parameter to generate point of interest #2 based on the ray. (Output)
// tgCL         Container of points resulting from the clip operation. (Output)

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




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

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

template <typename TYPE, int DIM> TgFORCEINLINE
TYPE F_DistSq( CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    const TYPE                          tyDist = F_Dist( tgRY0, 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_(RAY,DIM) tgRY0 )
{
    const TYPE                          tyDist = F_Dist( tgRY0, 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) ptvRY0, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    const TYPE                          tyTest = F_Closest( ptvRY0, tgRY0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_SUB( *ptvRY0, 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) ptvRY0, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    const TYPE                          tyTest = F_Closest( ptvRY0, tgRY0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_SUB( *ptvRY0, 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 *ptyRY0, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    const TYPE                          tyTest = F_Closest( ptyRY0, tgRY0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_SUB( tgRY0.Query_Origin(), tgSP0.Query_Origin() );
    C_(VECTOR,DIM)                      tvK1 = MATH::F_ADD( tvK0, MATH::F_MUL( *ptyRY0, tgRY0.Query_DirN() ) );

    *ptvSP0 = MATH::F_ADD( tgSP0.Query_Origin(), MATH::F_MUL( tgSP0.Query_Radius() / tyDist, tvK1 ) );
    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 *ptyRY0, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    const TYPE                          tyTest = F_Closest( ptyRY0, tgRY0, tgSP0.Query_Origin() );
    const TYPE                          tyDist = P::FSEL( tyTest - tgSP0.Query_Radius(), tyTest, TYPE(-1.0) );
    C_(VECTOR,DIM)                      tvK0 = MATH::F_SUB( tgRY0.Query_Origin(), tgSP0.Query_Origin() );
    C_(VECTOR,DIM)                      tvK1 = MATH::F_ADD( tvK0, MATH::F_MUL( *ptyRY0, tgRY0.Query_DirN() ) );

    *ptvSP0 = MATH::F_ADD( tgSP0.Query_Origin(), MATH::F_MUL( tgSP0.Query_Radius() / tyDist, tvK1 ) );
    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_(RAY,DIM) tgRY0 )
{
    return (F_DistSq(tgRY0, tgSP0.Query_Origin()) <= tgSP0.Query_RadiusSq());
};


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

template <typename TYPE, int DIM> TgFORCEINLINE
TgRESULT F_Contact_Intersect( PC_(CONTACT_PACKET,DIM) ptgPacket, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    return (TTgINT_SPLN<TYPE,DIM,1,0>::DO( ptgPacket, tgSP0, tgRY0.Query_Origin(),tgRY0.Query_DirN() ));
};


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

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

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

    T_(VECTOR,DIM)                      tvRY0;

    const TYPE                          tyDistSq = F_ClosestSq( &tvRY0, tgRY0,tgSP0.Query_Origin() );

    if (tyDistSq > tgSP0.Query_RadiusSq())
    {
        return (TgE_NOINTERSECT);
    };

    return (F_Internal_Penetrate( ptgPacket, tgRY0.Query_DirN(), tgSP0,tvRY0,tyDistSq ));
};


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

template <typename TYPE, int DIM> TgFORCEINLINE
TgRESULT F_Contact_Sweep( PC_(CONTACT_PACKET,DIM) ptgPacket, TYPE *ptyPM, CR_(RAY,DIM) tgRY0, CR_(SPHERE,DIM) tgSP0, CR_(DELTA,DIM) tgDT )
{
    return (TTgSWP_SPLN<TYPE,DIM,1,0>::DO( ptgPacket, ptyPM, tgRY0.Query_Origin(),tgRY0.Query_DirN(), tgSP0,tgDT ));
};


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

template <typename TYPE, int DIM> TgFORCEINLINE
TgRESULT F_Clip( TYPE *ptyRY0, TYPE *ptyRY1, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    return (TTgCLP_SPLN<TYPE,DIM,1,0>::DO( ptyRY0,ptyRY1, tgSP0, tgRY0.Query_Origin(),tgRY0.Query_DirN() ));
};


template <typename TYPE, int DIM> TgFORCEINLINE
TgRESULT F_Clip( PC_(CLIP_LIST,DIM) ptgCL, CR_(SPHERE,DIM) tgSP0, CR_(RAY,DIM) tgRY0 )
{
    return (TTgCLP_SPLN<TYPE,DIM,1,0>::DO( ptgCL, tgSP0, tgRY0.Query_Origin(),tgRY0.Query_DirN() ));
};


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

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