Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Parallelogram-Point.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Parallelogram;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_PE_ParamSq_VT) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  psPE0: Parallelogram primitive                                                                                                                                 */
/* Input:  pvS0: Point                                                                                                                                                    */
/* Output: _tyPE0, _tyPE1: Parametric parameters to generate point of minimal distance on the parallelogram                                                               */
/* Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_F_PE_ParamSq_VT)(TYPE *pfPE0, TYPE *pfPE1, V(CPC_TgPARALLELOGRAM) psPE0, V(CPC_TgVEC) pvS0)
{
    V(C_TgVEC)                          vDS = V(F_SUB)(pvS0, &psPE0->m_vOrigin);
    const TYPE                          fDS_DS = V(F_LSQ)(&vDS);
    const TYPE                          fE1_EN0 = V(F_DOT)(psPE0->m_avEdgeNormal + 0, psPE0->m_avEdge + 1);
    const TYPE                          fE0_EN1 = V(F_DOT)(psPE0->m_avEdgeNormal + 1, psPE0->m_avEdge + 0);

    TgPARAM_CHECK( V(tgGM_PE_Is_Valid)(psPE0) && V(F_Is_Point_Valid)(pvS0) );

    if (TgEXPECT_FALSE( fDS_DS <= F(KTgEPS) ))
    {
        /* Quick Out - the point is within tolerance of rectangle origin. */
        *pfPE0 = MKL(0.0);
        *pfPE1 = MKL(0.0);
        return (MKL(0.0));
    };

    if (TgEXPECT_FALSE( fE1_EN0 <= F(KTgEPS) || fE0_EN1 <= F(KTgEPS) ))
    {
        /* Degenerate parallelogram - One or both of the edges has a near-zero length */
        return (-F(KTgMAX));
    }
    else
    {
        const TYPE                          f00 = V(F_DOT)(&vDS, psPE0->m_avEdgeNormal + 0);
        const TYPE                          f01 = V(F_DOT)(&vDS, psPE0->m_avEdgeNormal + 1);
        const TYPE                          f02 = fE1_EN0 - f00;
        const TYPE                          f03 = fE0_EN1 - f01;

        const TYPE                          fDS_N = V(F_DOT)(&vDS, &psPE0->m_vNormal);

        TYPE                                fPE0 = F(KTgMAX), fPE1 = F(KTgMAX), fDistSq = F(KTgMAX);
        TYPE                                fT0, fTest;

        if (f00 >= MKL(0.0) && f01 >= MKL(0.0) && f02 >= MKL(0.0) && f03 >= MKL(0.0))
        {
            *pfPE0 = f01 / fE0_EN1;
            *pfPE1 = f00 / fE1_EN0;

            return (fDS_N*fDS_N);
        };

        if (f00 < F(KTgEPS))
        {
            fTest = V(tgCO_F_LR11_ParamSq_VT)(&fT0, &psPE0->m_vOrigin, psPE0->m_avEdge + 0, pvS0);

            if (fTest < fDistSq)
            {
                fDistSq = fTest;
                fPE0 = fT0;
                fPE1 = MKL(0.0);
            };
        };

        if (f01 < F(KTgEPS))
        {
            fTest = V(tgCO_F_LR11_ParamSq_VT)(&fT0, &psPE0->m_vOrigin, psPE0->m_avEdge + 1, pvS0);

            if (fTest < fDistSq)
            {
                fDistSq = fTest;
                fPE0 = MKL(0.0);
                fPE1 = fT0;
            };
        };

        if (f02 < F(KTgEPS))
        {
            V(C_TgVEC)                          vK0 = V(tgGM_PE_Query_Point_2)(psPE0);

            fTest = V(tgCO_F_LR11_ParamSq_VT)(&fT0, &vK0, psPE0->m_avEdge + 0, pvS0);

            if (fTest < fDistSq)
            {
                fDistSq = fTest;
                fPE0 = fT0;
                fPE1 = MKL(1.0);
            };
        };

        if (f03 < F(KTgEPS))
        {
            V(C_TgVEC)                          vK0 = V(tgGM_PE_Query_Point_1)(psPE0);

            fTest = V(tgCO_F_LR11_ParamSq_VT)(&fT0, &vK0, psPE0->m_avEdge + 1, pvS0);

            if (fTest < fDistSq)
            {
                fDistSq = fTest;
                fPE0 = MKL(1.0);
                fPE1 = fT0;
            };
        };

        *pfPE0 = fPE0;
        *pfPE1 = fPE1;

        return (fDistSq);
    };
}