// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Circle-Point.cpp
//  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".
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //




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

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

// ---- F_DistSq ---------------------------------------------------------------------------------------------------------------- //
// Input:  tvCIS0: Origin of the circle (and the point on the plane).
// Input:  tvCIN0: Normal to the plane containing the circle.
// Input:  tyRadius: Radius of the circle
// Input:  tvS0: Point, not necessarily in the plane.
// Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.
// ------------------------------------------------------------------------------------------------------------------------------ //

template<typename TYPE, int DIM>
TYPE F_DistSq( M_(VECTOR,DIM) tvCIS0, M_(VECTOR,DIM) tvCIN0, const TYPE                          tyRadius, M_(VECTOR,DIM) tvS1 )
{
    TgASSERT( MATH::F_Is_Point_Valid( tvCIS0 ) && MATH::F_Is_Vector_Valid( tvCIN0 ) && MATH::F_Is_Point_Valid( tvS1 ) )
    TgASSERT( Is_Valid( tyRadius ) && tyRadius > TYPE(0.0) );

    C_(VECTOR,DIM)                      tvDS = MATH::F_SUB( tvS1, tvCIS0 );
    const TYPE                          tyDS_DS = MATH::F_LSQ( tvDS );

    if (tyDS_DS <= LIMITS<TYPE>::EPSILON)
    {
        // Quick Out - the point is within tolerance of circle origin.

        return (tyRadius*tyRadius);
    };

    const TYPE                          tyDS_N = MATH::F_DOT(tvDS,tvCIN0);
    TYPE                                tyLenPDS;
    C_(VECTOR,DIM)                      tvPDS = MATH::F_NORM( &tyLenPDS, MATH::F_SUB( tvDS, MATH::F_MUL( tyDS_N, tvCIN0 ) ) );
    
    if (tyLenPDS <= LIMITS<TYPE>::EPSILON)
    {
        // Quick Out - the point is directly above the origin

        return (tyRadius*tyRadius + tyDS_N*tyDS_N);
    };

    return (MATH::F_LSQ( MATH::F_SUB( tvDS, MATH::F_MUL( tyRadius, tvPDS ) ) ));
};

template TgFLOAT32 F_DistSq( M_TgF4VECTOR, M_TgF4VECTOR, C_TgFLOAT32, M_TgF4VECTOR );


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

// ---- F_ClosestSq ------------------------------------------------------------------------------------------------------------- //
// Input:  tvCIS0: Origin of the circle (and the point on the plane).
// Input:  tvCIN0: Normal to the plane containing the circle.
// Input:  tyRadius: Radius of the circle
// Input:  tvS0: Point, not necessarily in the plane.
// Output: tvCI0: Point of closest proximity on the circle.
// Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.
// ------------------------------------------------------------------------------------------------------------------------------ //

template<typename TYPE, int DIM>
TYPE F_ClosestSq( PC_(VECTOR,DIM) ptvCI0, M_(VECTOR,DIM) tvCIS0, M_(VECTOR,DIM) tvCIN0, const TYPE                          tyRadius, M_(VECTOR,DIM) tvS1 )
{
    TgASSERT( MATH::F_Is_Point_Valid( tvCIS0 ) && MATH::F_Is_Vector_Valid( tvCIN0 ) && MATH::F_Is_Point_Valid( tvS1 ) )
    TgASSERT( Is_Valid( tyRadius ) && tyRadius > TYPE(0.0) );

    C_(VECTOR,DIM)                      tvDS = MATH::F_SUB( tvS1, tvCIS0 );
    const TYPE                          tyDS_DS = MATH::F_LSQ( tvDS );

    if (tyDS_DS <= LIMITS<TYPE>::EPSILON)
    {
        // Quick Out - the point is within tolerance of circle origin.

        C_(VECTOR,DIM)                      tvDirN = MATH::F_NORM( MATH::F_Set_Ortho( tvCIN0 ) );

        *ptvCI0 = MATH::F_ADD( tvCIS0, MATH::F_MUL( tyRadius, tvDirN ) );

        return (tyRadius*tyRadius);
    };

    const TYPE                          tyDS_N = MATH::F_DOT(tvDS,tvCIN0);
    TYPE                                tyLenPDS;
    C_(VECTOR,DIM)                      tvPDS = MATH::F_NORM( &tyLenPDS, MATH::F_SUB( tvDS, MATH::F_MUL( tyDS_N, tvCIN0 ) ) );
    
    if (tyLenPDS <= LIMITS<TYPE>::EPSILON)
    {
        // The point is directly above the origin. Thus, every point on the circle is equidistant - make an arbitrary choice.
    
        C_(VECTOR,DIM)                      tvDirN = MATH::F_NORM( MATH::F_Set_Ortho( tvCIN0 ) );

        *ptvCI0 = MATH::F_ADD( tvCIS0, MATH::F_MUL( tyRadius, tvDirN ) );

        return (tyRadius*tyRadius + tyDS_N*tyDS_N);
    };

    *ptvCI0 = MATH::F_ADD( tvCIS0, MATH::F_MUL( tyRadius, tvPDS ) );
    
    return (MATH::F_LSQ( MATH::F_SUB( tvS1, *ptvCI0 ) ));
};

template TgFLOAT32 F_ClosestSq( PC_TgF4VECTOR, M_TgF4VECTOR, M_TgF4VECTOR, C_TgFLOAT32, M_TgF4VECTOR );


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

}; // END COL //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}; // END TGS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////