Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Circle-Point.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Circle;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_FI_CI_DistSq_VT) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  vCIS0: Origin of the circle (and the point on the plane).                                                                                                      */
/* Input:  vCIN0: Normal to the plane containing the circle.                                                                                                              */
/* Input:  fRadius: Radius of the circle                                                                                                                                  */
/* Input:  vS0: Point, not necessarily in the plane.                                                                                                                      */
/* Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_FI_CI_DistSq_VT)( V(CPC_TgVEC) pvCI_S0, V(CPC_TgVEC) pvCI_N0, const TYPE fRadius, V(CPC_TgVEC) pvS0 )
{
    V(C_TgVEC)                          vDS = V(F_SUB)(pvS0, pvCI_S0);
    const TYPE                          fDS_DS = V(F_LSQ)(&vDS);

    TgERROR( V(F_Is_Point_Valid)(pvCI_S0) && V(F_Is_Vector_Valid)(pvCI_N0) && V(F_Is_Point_Valid)(pvS0) );
    TgERROR( !F(tgPM_NAN)(fRadius) && fRadius > MKL(0.0) );

    if (fDS_DS <= F(KTgEPS))
    {   /* Quick Out - the point is within tolerance of circle origin. */
        return (fRadius*fRadius);
    }
    else
    {
        const TYPE                          fDS_N = V(F_DOT)(&vDS, pvCI_N0);
        TYPE                                fLenPDS;
        V(C_TgVEC)                          vK0 = V(F_MUL_SV)(fDS_N, pvCI_N0);
        V(C_TgVEC)                          vK1 = V(F_SUB)(&vDS, &vK0);
        V(C_TgVEC)                          vPDS = V(F_NORM_LEN)(&fLenPDS, &vK1);

        if (fLenPDS <= F(KTgEPS))
        {   /* Quick Out - the point is directly above the origin */
            return (fRadius*fRadius + fDS_N*fDS_N);
        }
        else
        {
            V(C_TgVEC)                          vK2 = V(F_MUL_SV)(fRadius, &vPDS);
            V(C_TgVEC)                          vK3 = V(F_SUB)(&vDS, &vK2);

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


/* ---- V(tgCO_FI_CI_ClosestSq_VT) -------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  vCIS0: Origin of the circle (and the point on the plane).                                                                                                      */
/* Input:  vCIN0: Normal to the plane containing the circle.                                                                                                              */
/* Input:  fRadius: Radius of the circle                                                                                                                                  */
/* Input:  vS0: Point, not necessarily in the plane.                                                                                                                      */
/* Output: vCI0: Point of closest proximity on the circle.                                                                                                                */
/* Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_FI_CI_ClosestSq_VT)( V(PC_TgVEC) pvCI0, V(CPC_TgVEC) pvCI_S0, V(CPC_TgVEC) pvCI_N0, const TYPE fRadius, V(CPC_TgVEC) pvS0 )
{
    V(C_TgVEC)                          vDS = V(F_SUB)(pvS0, pvCI_S0);
    const TYPE                          fDS_DS = V(F_LSQ)(&vDS);

    TgERROR( V(F_Is_Point_Valid)(pvCI_S0) && V(F_Is_Vector_Valid)(pvCI_N0) && V(F_Is_Point_Valid)(pvS0) );
    TgERROR( !F(tgPM_NAN)(fRadius) && fRadius > MKL(0.0) );

    if (fDS_DS <= F(KTgEPS))
    {   /* Quick Out - the point is within tolerance of circle origin. */
        V(C_TgVEC)                          vK0 = V(F_Set_Ortho)(pvCI_N0);
        V(C_TgVEC)                          vDirN = V(F_NORM)(&vK0);
        V(C_TgVEC)                          vK1 = V(F_MUL_SV)(fRadius, &vDirN);

        *pvCI0 = V(F_ADD)(pvCI_S0, &vK1);

        return (fRadius*fRadius);
    }
    else
    {
        const TYPE                          fDS_N = V(F_DOT)(&vDS, pvCI_N0);
        TYPE                                fLenPDS;
        V(C_TgVEC)                          vK0 = V(F_MUL_SV)(fDS_N, pvCI_N0);
        V(C_TgVEC)                          vK1 = V(F_SUB)(&vDS, &vK0);
        V(C_TgVEC)                          vPDS = V(F_NORM_LEN)(&fLenPDS, &vK1);

        if (fLenPDS <= F(KTgEPS))
        {
            /* The point is directly above the origin. Thus, every point on the circle is equidistant - make an arbitrary choice. */
            V(C_TgVEC)                          vK2 = V(F_Set_Ortho)(pvCI_N0);
            V(C_TgVEC)                          vDirN = V(F_NORM)(&vK2);
            V(C_TgVEC)                          vK3 = V(F_MUL_SV)(fRadius, &vDirN);

            *pvCI0 = V(F_ADD)(pvCI_S0, &vK3);

            return (fRadius*fRadius + fDS_N*fDS_N);
        }
        else
        {
            V(C_TgVEC)                          vK2 = V(F_MUL_SV)(fRadius, &vPDS);
            V(C_TgVEC)                          vK3 = V(F_ADD)(pvCI_S0, &vK2);
            V(C_TgVEC)                          vK4 = V(F_SUB)(pvS0, &vK3);

            *pvCI0 = vK3;

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