Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Geometry 3D - Triangle [Point].c_inc
    »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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Common ============================================================================================================================================================ */

/* ---- V(tgGM_PT_Is_Valid) --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL V(tgGM_PT_Is_Valid)( V(CPCU_TgPTRI) psPT1 )
{
    V(TgVEC)                      atvEdge[2], vNormal;

    /* Sanity Check: Vector form. */
    if (TgTRUE != V(F_Is_Point_Valid)(psPT1->m_avPoint + 0))
    {
        return (TgFALSE);
    };

    if (TgTRUE != V(F_Is_Point_Valid)(psPT1->m_avPoint + 1))
    {
        return (TgFALSE);
    };

    if (TgTRUE != V(F_Is_Point_Valid)(psPT1->m_avPoint + 2))
    {
        return (TgFALSE);
    };

    if (TgTRUE != V(F_Is_Vector_Valid)(&psPT1->m_vNormal) || TgTRUE != F(tgCM_NR1)(V(F_LSQ)(&psPT1->m_vNormal)))
    {
        return (TgFALSE);
    };

    atvEdge[0] = V(F_SUB)(psPT1->m_avPoint + 1, psPT1->m_avPoint + 0);
    atvEdge[1] = V(F_SUB)(psPT1->m_avPoint + 2, psPT1->m_avPoint + 0);

    /* Sanity Check: on the edge size. */

    if (TgTRUE == F(tgCM_NR0)(V(F_LSQ)(atvEdge + 0)) || TgTRUE == F(tgCM_NR0)(V(F_LSQ)(atvEdge + 1)))
    {
        return (TgFALSE);
    };

    /* Sanity check on the "aspect" ratio of the triangle - remember that |a x b| = |a|•|b|•|sin(Θ)|, and near zero sin(Θ) ≈ Θ */

    vNormal = V( F_CX )(atvEdge + 0, atvEdge + 1);

    if (V(F_LSQ)( &vNormal ) < F(KTgEPS) * V(F_LSQ)( atvEdge + 0 ) * V(F_LSQ)( atvEdge + 1 ))
    {
        return (TgFALSE);
    };

    return (TgTRUE);
}


/* ---- V(tgGM_PT_Support_Point) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID V(tgGM_PT_Support_Point)( V(P_TgVEC) pvRT, V(CPCU_TgPTRI) psPT1, V(CPCU_TgVEC) ptvDN )
{
    const TYPE                          fAx_N = V(F_DOT3)(ptvDN, &psPT1->m_vNormal);

    TgGEOM_ASSERT_PARAM(TgTRUE == V(tgGM_PT_Is_Valid)(psPT1) && TgTRUE == V(F_Is_Vector_Valid)(ptvDN));

    if (F(tgCM_NR0)(fAx_N - MKL(1.0)))
    {
        *pvRT = (V(FS_SETP)(
            F(KTgF_THIRD) * (psPT1->m_avPoint[0].m.x + psPT1->m_avPoint[1].m.x + psPT1->m_avPoint[2].m.x),
            F(KTgF_THIRD) * (psPT1->m_avPoint[0].m.y + psPT1->m_avPoint[1].m.y + psPT1->m_avPoint[2].m.y),
            F(KTgF_THIRD) * (psPT1->m_avPoint[0].m.z + psPT1->m_avPoint[1].m.z + psPT1->m_avPoint[2].m.z)
            ));
    }
    else
    {
        const TYPE                          fAx_P0 = V(F_DOT3)(ptvDN, psPT1->m_avPoint + 0);
        const TYPE                          fAx_P1 = V(F_DOT3)(ptvDN, psPT1->m_avPoint + 1);
        const TYPE                          fAx_P2 = V(F_DOT3)(ptvDN, psPT1->m_avPoint + 2);

        if (fAx_P0 > fAx_P1)
        {
            if (fAx_P0 > fAx_P2)
            {
                *pvRT = (psPT1->m_avPoint[0]);
            }
            else
            {
                *pvRT = (psPT1->m_avPoint[2]);
            };
        }
        else
        {
            if (fAx_P1 > fAx_P2)
            {
                *pvRT = (psPT1->m_avPoint[1]);
            }
            else
            {
                *pvRT = (psPT1->m_avPoint[2]);
            };
        };
    };
}