// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Disk-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:  tgDK0: Disk - The set of points on the plane that are at or less than the radius value.
// 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( CR_(DISK,DIM) tgDK0, M_(VECTOR,DIM) tvS0 )
{
    TgASSERT( tgDK0.Is_Valid() && MATH::F_Is_Point_Valid( tvS0 ) );

    C_(VECTOR,DIM)                      tvDS = MATH::F_SUB( tvS0, tgDK0.Query_Origin() );
    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 (tyDS_DS);
    };

    const TYPE                          tyDS_N = MATH::F_DOT(tvDS,tgDK0.Query_Normal());
    C_(VECTOR,DIM)                      tvPDS = MATH::F_SUB( tvDS, MATH::F_MUL( tyDS_N, tgDK0.Query_Normal() ) );
    const TYPE                          tyPDS_PDS = MATH::F_LSQ( tvPDS );
    
    if (tyPDS_PDS <= tgDK0.Query_Radius()*tgDK0.Query_Radius())
    {
        // Quick Out - the point is directly above the disk

        return (tyDS_N*tyDS_N);
    };

    return (MATH::F_LSQ( MATH::F_SUB( tvDS, MATH::F_MUL( tgDK0.Query_Radius() / P::SQRT( tyPDS_PDS ), tvPDS ) ) ));
}

template TgFLOAT32 F_DistSq( CR_TgF4DISK, M_TgF4VECTOR );


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

// ---- F_ClosestSq ------------------------------------------------------------------------------------------------------------- //
// Input:  tgDK0: Disk - The set of points on the plane that are at or less than the radius value.
// Input:  tvS0: Point, not necessarily in the plane.
// Output: tvDK0: 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) ptvDK0, CR_(DISK,DIM) tgDK0, M_(VECTOR,DIM) tvS0 )
{
    TgASSERT( tgDK0.Is_Valid() && MATH::F_Is_Point_Valid( tvS0 ) );

    C_(VECTOR,DIM)                      tvDS = MATH::F_SUB( tvS0, tgDK0.Query_Origin() );
    const TYPE                          tyDS_DS = MATH::F_LSQ( tvDS );

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

        *ptvDK0 = tgDK0.Query_Origin();
        return (tyDS_DS);
    };

    const TYPE                          tyDS_N = MATH::F_DOT( tvDS, tgDK0.Query_Normal() );
    C_(VECTOR,DIM)                      tvPDS = MATH::F_SUB( tvDS, MATH::F_MUL( tyDS_N, tgDK0.Query_Normal() ) );
    const TYPE                          tyPDS_PDS = MATH::F_LSQ( tvPDS );
    
    if (tyPDS_PDS <= tgDK0.Query_Radius()*tgDK0.Query_Radius())
    {
        // Quick Out - the point is directly above the disk
   
        *ptvDK0 = MATH::F_ADD( tgDK0.Query_Origin(), tvPDS );
        return (tyDS_N*tyDS_N);
    };

    *ptvDK0 = MATH::F_ADD( tgDK0.Query_Origin(), MATH::F_MUL( tgDK0.Query_Radius() / P::SQRT( tyPDS_PDS ), tvPDS ) );
    
    return (MATH::F_LSQ( MATH::F_SUB( tvS0, *ptvDK0 ) ));
}

template TgFLOAT32 F_ClosestSq( PC_TgF4VECTOR, CR_TgF4DISK, M_TgF4VECTOR );


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

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