// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//
//  Project:   Talina Gaming System (TgS) (∂)
//  File:      TgS Collision - Parallelogram-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:  tgPE0: Parallelogram primitive
// Input:  tgRT0: Rectangle primitive
// Output: tyPE0, tyPE1: Parametric parameters to generate point of minimal distance on the parallelogram
// Output: tyRT0, tyRT1: Parametric parameters to generate point of minimal distance on the rectangle
// 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 *ptyPE0, TYPE *ptyPE1, TYPE *ptyRT0, TYPE *ptyRT1, CR_(PARALLELOGRAM,DIM) tgPE0, CR_(RECTANGLE,DIM) tgRT0 )
{
    TgASSERT( tgPE0.Is_Valid() )

    TYPE                                tyT_E0, tyT_E1, tyT_G1, tyT_RetVal;
    TYPE                                tyPE00, tyPE01, tyRT10, tyRT11;
    TYPE                                tyRetVal = LIMITS<TYPE>::MAX;

    // Compare the edges of the parallelogram against the parallelogram.

    tyRetVal = TTgCSQ_PELN<TYPE,DIM,1,1>::DO( &tyPE00,&tyPE01, &tyRT10, tgPE0, tgRT0.Query_Origin(),tgRT0.Query_Edge0() );
    tyRT11 = TYPE(0.0);

    tyT_RetVal = TTgCSQ_PELN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgPE0, tgRT0.Query_Point2(),tgRT0.Query_Edge0() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = tyT_E0;
        tyPE01 = tyT_E1;
        tyRT10 = tyT_G1;
        tyRT11 = TYPE(1.0);
    }

    tyT_RetVal = TTgCSQ_PELN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgPE0, tgRT0.Query_Origin(),tgRT0.Query_Edge1() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = tyT_E0;
        tyPE01 = tyT_E1;
        tyRT10 = TYPE(0.0);
        tyRT11 = tyT_G1;
    }

    tyT_RetVal = TTgCSQ_PELN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgPE0, tgRT0.Query_Point1(),tgRT0.Query_Edge1() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = tyT_E0;
        tyPE01 = tyT_E1;
        tyRT10 = TYPE(1.0);
        tyRT11 = tyT_G1;
    }

    // Compare the edges of the parallelogram to the parallelogram.

    tyT_RetVal = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgRT0, tgPE0.Query_Origin(),tgPE0.Query_Edge0() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = tyT_G1;
        tyPE01 = TYPE(0.0);
        tyRT10 = tyT_E0;
        tyRT11 = tyT_E1;
    };

    tyT_RetVal = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgRT0, tgPE0.Query_Point2(),tgPE0.Query_Edge0() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = tyT_G1;
        tyPE01 = TYPE(1.0);
        tyRT10 = tyT_E0;
        tyRT11 = tyT_E1;
    };

    tyT_RetVal = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgRT0, tgPE0.Query_Origin(),tgPE0.Query_Edge1() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = TYPE(0.0);
        tyPE01 = tyT_G1;
        tyRT10 = tyT_E0;
        tyRT11 = tyT_E1;
    };

    tyT_RetVal = TTgCSQ_RTLN<TYPE,DIM,1,1>::DO( &tyT_E0,&tyT_E1, &tyT_G1, tgRT0, tgPE0.Query_Point1(),tgPE0.Query_Edge1() );
    if (tyT_RetVal < tyRetVal)
    {
        tyRetVal = tyT_RetVal;
        tyPE00 = TYPE(1.0);
        tyPE01 = tyT_G1;
        tyRT10 = tyT_E0;
        tyRT11 = tyT_E1;
    };

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

    *ptyPE0 = tyPE00;
    *ptyPE1 = tyPE01;
    *ptyRT0 = tyRT10;
    *ptyRT1 = tyRT11;

    return (tyRetVal);
};

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


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

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