// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Rectangle-Rectangle.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_ClosestSq ------------------------------------------------------------------------------------------------------------- //
// Input:  tgRT0, tgRT1: Rectangle primitives
// Output: _tyRT00, _tyRT01: Parametric parameters to generate point of minimal distance on rectangle #1
// Output: _tyRT10, _tyRT11: Parametric parameters to generate point of minimal distance on rectangle #2
// 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( TYPE *ptyRT00, TYPE *ptyRT01, TYPE *ptyRT10, TYPE *ptyRT11, CR_(RECTANGLE,DIM) tgRT0, CR_(RECTANGLE,DIM) tgRT1 )
{
    TYPE                                tyRT00 = TYPE(), tyRT10 = TYPE(), tyRT11 = TYPE();
    TYPE                                tyDistSq, tyTest, tyT0, tyT1, tyG1, tyRT01;

    // == Test Rectangle 0 Edges against Rectangle 1 ===========

    tyDistSq = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyRT10,&tyRT11, &tyRT00, tgRT1, tgRT0.Query_Point0(), tgRT0.Query_Edge0() );
    tyRT01 = TYPE(0.0);

    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT1, tgRT0.Query_Point2(), tgRT0.Query_Edge0() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = tyG1;
        tyRT01 = TYPE(1.0);
        tyRT10 = tyT0;
        tyRT11 = tyT1;
    };

    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT1, tgRT0.Query_Point0(), tgRT0.Query_Edge1() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = TYPE(0.0);
        tyRT01 = tyG1;
        tyRT10 = tyT0;
        tyRT11 = tyT1;
    };
    
    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT1, tgRT0.Query_Point1(), tgRT0.Query_Edge1() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = TYPE(1.0);
        tyRT01 = tyG1;
        tyRT10 = tyT0;
        tyRT11 = tyT1;
    };

    // == Test Rectangle 1 Edges against Rectangle 0 ===========

    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT0, tgRT1.Query_Point0(), tgRT1.Query_Edge0() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = tyT0;
        tyRT01 = tyT1;
        tyRT10 = tyG1;
        tyRT11 = TYPE(0.0);
    };

    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT0, tgRT1.Query_Point2(), tgRT1.Query_Edge0() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = tyT0;
        tyRT01 = tyT1;
        tyRT10 = tyG1;
        tyRT11 = TYPE(1.0);
    };

    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT0, tgRT1.Query_Point0(), tgRT1.Query_Edge1() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = tyT0;
        tyRT01 = tyT1;
        tyRT10 = TYPE(0.0);
        tyRT11 = tyG1;
    };

    tyTest = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT0,&tyT1, &tyG1, tgRT0, tgRT1.Query_Point1(), tgRT1.Query_Edge1() );
    if (tyTest < tyDistSq)
    {
        tyDistSq = tyTest;
        tyRT00 = tyT0;
        tyRT01 = tyT1;
        tyRT10 = TYPE(1.0);
        tyRT11 = tyG1;
    };

    // == Return Values ========================================

    *ptyRT00 = tyRT00;
    *ptyRT01 = tyRT01;
    *ptyRT10 = tyRT10;
    *ptyRT11 = tyRT11;

    return (tyDistSq);
};

template TgFLOAT32 F_ClosestSq( P_TgFLOAT32, P_TgFLOAT32, P_TgFLOAT32, P_TgFLOAT32, CR_TgF4RECTANGLE, CR_TgF4RECTANGLE );


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

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