Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Rectangle-Point.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Rectangle;Point;
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, 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:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution 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.
    The name of the author may not 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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Collision ========================================================================================================================================================= */

/* ---- V(tgCO_F_RT_ParamSq_VT) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  psRT0: Rectangle primitive                                                                                                                                     */
/* Input:  vS0: Point                                                                                                                                                     */
/* Output: fRT0, fRT1: 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.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_F_RT_ParamSq_VT)(TYPE *pfRT0, TYPE *pfRT1, V(CPC_TgRECTANGLE) psRT0, V(CPC_TgVEC) pvS0)
{
    V(C_TgVEC)                          vDS = V(F_SUB)(pvS0, &psRT0->m_vOrigin);
    const TYPE                          fDS_DS = V(F_LSQ)(&vDS);

    TgERROR( V(tgGM_RT_Is_Valid)(psRT0) && V(F_Is_Point_Valid)(pvS0) );

    if (fDS_DS <= F(KTgEPS))
    {
        /* Quick Out - the point is within tolerance of rectangle origin. */
        *pfRT0 = MKL(0.0);
        *pfRT1 = MKL(0.0);

        return (MKL(0.0));
    }
    else
    {
        const TYPE                          fE0_E0 = V(F_LSQ)(psRT0->m_avEdge + 0);
        const TYPE                          fE1_E1 = V(F_LSQ)(psRT0->m_avEdge + 1);

        if (fE0_E0 <= F(KTgEPS) || fE1_E1 <= F(KTgEPS))
        {
            /* Degenerate rectangle - One or both of the edges has a near-zero length */

            return (-F(KTgMAX));
        }
        else
        {
            const TYPE                          fDS_E0 = V(F_DOT)(&vDS, psRT0->m_avEdge + 0);
            const TYPE                          fDS_E1 = V(F_DOT)(&vDS, psRT0->m_avEdge + 1);
            const TYPE                          fRT0 = F(tgCM_CLP)(fDS_E0 / fE0_E0, MKL(0.0), MKL(1.0));
            const TYPE                          fRT1 = F(tgCM_CLP)(fDS_E1 / fE1_E1, MKL(0.0), MKL(1.0));

            V(C_TgVEC)                          vK0 = V(F_MUL_SV)(fRT0, psRT0->m_avEdge + 0);
            V(C_TgVEC)                          vK1 = V(F_MUL_SV)(fRT1, psRT0->m_avEdge + 1);
            V(C_TgVEC)                          vK2 = V(F_SUB)(pvS0, &psRT0->m_vOrigin);
            V(C_TgVEC)                          vK3 = V(F_SUB)(&vK2, &vK0);
            V(C_TgVEC)                          vK4 = V(F_SUB)(&vK3, &vK1);

            *pfRT0 = fRT0;
            *pfRT1 = fRT1;

            return (V(F_LSQ)(&vK4));
        };
    };
}