Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Triangle-Point.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Triangle;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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

/*                                                                                                                                                                        */
/* Triangle Definition: T0(α,β) = P0 + α•E0 + β•E1 | α ε [ 0, 1], β ε [ 0, 1], (α + β) ε [0, 1]                                                                           */
/*                                                                                                                                                                        */
/* Derivation:                                                                                                                                                            */
/*                                                                                                                                                                        */
/*                                                                                                                                                                        */
/*         .    .              .    .                                                                                                                                     */
/*          . G .               . G .   NOTE: Keep in mind that in regions C, E and G, an obtuse angle can lead                                                           */
/*           .  .                .  .            to having significant projections along the other edge.                                                                  */
/*            . .                 . .      E0                                                                                                                             */
/*             ..                  ..     .                                                                                                                               */
/*              .2                  .  F .                                                                                                                                */
/*              |\                  |\  .                                                                                                                                 */
/*              | \                 | \.                                                                                                                                  */
/*          B   |  \   F         B  | /.  E                                                                                                                               */
/*              | A \               |/  .                                                                                                                                 */
/*         .....|____\..... E0      .O   .                                                                                                                                */
/*             O.     .1           ..     .                                                                                                                               */
/*              .      .          . .  D   .                                                                                                                              */
/*          C   .   D   .   E    .  .                                                                                                                                     */
/*              .        .      . C .                                                                                                                                     */
/*              .                   .                                                                                                                                     */
/*             E1                  E1                                                                                                                                     */
/*                                                                                                                                                                        */
/*     These regions define the closest feature of the triangle when contained in their area.  The point is projected onto the                                            */
/*    triangle plane, and the region that contains the point is determined.  It is then a simple matter to calculate the resulting                                        */
/*    distance to the triangle for the point.                                                                                                                             */
/*                                                                                                                                                                        */

/* == Collision ========================================================================================================================================================= */

/* ---- V(tgCO_F_ET_ParamSq_VT) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  psET0: Edge triangle primitive                                                                                                                                 */
/* Input:  vS0: Point                                                                                                                                                     */
/* Output: _fET0, _fET1: Parametric parameters to generate point of minimal distance on the triangle                                                                      */
/* Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_F_ET_ParamSq_VT)(TYPE *pfET0, TYPE *pfET1, V(CPC_TgETRI) psET0, V(CPC_TgVEC) pvS0)
{
    V(TgVEC)                            vX0 = V(F_SUB)(pvS0, psET0->m_sPT.m_avPoint + 0);
    const TYPE                          fX0_X0 = V(F_LSQ)(&vX0);

    TgERROR( V(tgGM_ET_Is_Valid)(psET0) && V(F_Is_Point_Valid)(pvS0) );

    if (fX0_X0 <= F(KTgEPS))
    {
        /* Quick Out - the point is within tolerance of triangle origin. */

        *pfET0 = MKL(0.0);
        *pfET1 = MKL(0.0);

        return (fX0_X0);
    }
    else
    {
        const TYPE                          fE0_E0 = V(F_LSQ)(psET0->m_avEdge + 0);
        const TYPE                          fE2_E2 = V(F_LSQ)(psET0->m_avEdge + 2);
        const TYPE                          fE0_E2 = -V(F_DOT)(psET0->m_avEdge + 0, psET0->m_avEdge + 2);
        const TYPE                          fDet = F(tgPM_ABS)(fE0_E0*fE2_E2 - fE0_E2*fE0_E2);

        if (fE0_E0 <= F(KTgEPS) || fE2_E2 <= F(KTgEPS))
        {   /* Degenerate triangle - One or both of the edges has a near-zero length */
            return (-F(KTgMAX));
        };

        if (fDet <= F(KTgEPS))
        {   /* Degenerate triangle - Edges are co-linear (zero-area triangle) */
            return (-F(KTgMAX));
        }
        else
        {
            const TYPE                          fX0_E0 = V(F_DOT)(psET0->m_avEdge + 0, &vX0);
            const TYPE                          fX0_E2 = -V(F_DOT)(psET0->m_avEdge + 2, &vX0);
            const TYPE                          fPM0 = fE2_E2*fX0_E0 - fE0_E2*fX0_E2;
            const TYPE                          fPM1 = fE0_E0*fX0_E2 - fE0_E2*fX0_E0;

            TYPE                                fET0, fET1;

            /* Categorize the point according to how its contained by the edges, and the sum of the edges. */

            if (fPM0 + fPM1 <= fDet)
            {
                if (fPM1 >= MKL(0.0))
                {
                    const TYPE                          fK0 = F(tgCM_CLP)(fX0_E2 / fE2_E2, MKL(0.0), MKL(1.0));

                    fET0 = F(tgPM_FSEL)(fPM0, fPM0 / fDet, MKL(0.0));
                    fET1 = F(tgPM_FSEL)(fPM0, fPM1 / fDet, fK0);
                }
                else
                {
                    const TYPE                          fK0 = F(tgCM_CLP)(fX0_E2 / fE2_E2, MKL(0.0), MKL(1.0));

                    fET0 = F(tgCM_CLP)(fX0_E0 / fE0_E0, MKL(0.0), MKL(1.0));
                    fET1 = (fPM0 >= MKL(0.0) || fX0_E0 > MKL(0.0)) ? MKL(0.0) : fK0;
                };
            }
            else
            {
                V(C_TgVEC)                          vK0 = V(F_SUB)(pvS0, psET0->m_sPT.m_avPoint + 1);
                V(C_TgVEC)                          vK1 = V(F_SUB)(psET0->m_sPT.m_avPoint + 2, pvS0);
                const TYPE                          fE1_X1 = V(F_DOT)(psET0->m_avEdge + 1, &vK0);
                const TYPE                          fE1_X2 = V(F_DOT)(psET0->m_avEdge + 1, &vK1);
                const TYPE                          fE1_E1 = V(F_LSQ)(psET0->m_avEdge + 1);

                if (fPM1 >= MKL(0.0))
                {
                    const TYPE                          fK0 = F(tgCM_CLP)(fX0_E2 / fE2_E2, MKL(0.0), MKL(1.0));
                    const TYPE                          fK1 = F(tgCM_CLP)(fE1_X1 / fE1_E1, MKL(0.0), MKL(1.0));

                    fET0 = F(tgCM_CLP)(fE1_X2 / fE1_E1, MKL(0.0), MKL(1.0));
                    fET1 = (fPM0 < MKL(0.0) && fE1_X2 <= MKL(0.0)) ? fK0 : fK1;
                }
                else
                {
                    const TYPE                          fK0 = F(tgCM_CLP)(fE1_X2 / fE1_E1, MKL(0.0), MKL(1.0));
                    const TYPE                          fK1 = F(tgCM_CLP)(fX0_E0 / fE0_E0, MKL(0.0), MKL(1.0));

                    fET1 = F(tgCM_CLP)(fE1_X1 / fE1_E1, MKL(0.0), MKL(1.0));
                    fET0 = fE1_X1 > MKL(0.0) ? fK0 : fK1;
                };
            };

            *pfET0 = fET0;
            *pfET1 = fET1;

            {
                V(C_TgVEC)                          vK0 = V(F_MUL_SV)(fET0, psET0->m_avEdge + 0);
                V(C_TgVEC)                          vK1 = V(F_MUL_SV)(fET1, psET0->m_avEdge + 2);
                V(C_TgVEC)                          vK2 = V(F_ADD)(psET0->m_sPT.m_avPoint, &vK0);
                V(C_TgVEC)                          vK3 = V(F_SUB)(&vK2, &vK1);
                V(C_TgVEC)                          vK4 = V(F_SUB)(pvS0, &vK3);

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