Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Geometry 3D - Torus.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_TS_Is_Valid) --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL V(tgGM_TS_Is_Valid)( V(CPCU_TgTORUS) psTS1 )
{
    if (TgTRUE != V(F_Is_Point_Valid)(&psTS1->m_vOrigin))
    {
        return (TgFALSE);
    };

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

    if (F(tgPM_NAN)(psTS1->m_fRI) || psTS1->m_fRI <= MKL(0.0) || TgTRUE == F(tgPM_NAN)(psTS1->m_fRO) || psTS1->m_fRO <= MKL(0.0))
    {
        return (TgFALSE);
    };

    return (TgTRUE);
}


/* ---- V(tgGM_TS_Support_Point) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
V(TgVEC) V(tgGM_TS_Support_Point)( V(CPCU_TgTORUS) psTS1, V(CPCU_TgVEC) ptvDN )
{
    const TYPE                          fD_N = V(F_DOT)(ptvDN, &psTS1->m_vNormal);

    TgGEOM_ASSERT_PARAM(TgTRUE == V(tgGM_TS_Is_Valid)(psTS1) && TgTRUE == V(F_Is_Vector_Valid)(ptvDN) && TgTRUE == F(tgCM_NR1)(V(F_LSQ)(ptvDN)));

    if (F(tgCM_NR1)(F(tgPM_ABS)(fD_N)))
    {
        return (V(FS_SETP)(F(KTgMAX), F(KTgMAX), F(KTgMAX)));
    }
    else
    {
        TYPE                                fTest;

        V(C_TgVEC)                          vX0 = V(F_NMS_VSV)(&psTS1->m_vNormal, fD_N, ptvDN);
        V(C_TgVEC)                          tvX1 = V(F_NORM_LEN)(&fTest, &vX0);
        V(C_TgVEC)                          tvX2 = V(F_MAD_VSV)(ptvDN, psTS1->m_fRI, &psTS1->m_vOrigin);

        TgERROR(TgTRUE != F(tgCM_NR0)(fTest));

        return (V(F_MAD_VSV)(&tvX1, psTS1->m_fRO, &tvX2));
    };
}


/* ---- V(tgGM_TS_BA) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID V(tgGM_TS_BA)( V(PCU_TgBOXAA) psBA0, V(CPCU_TgTORUS) psTS1 )
{
    TYPE                                fTest;

    TgGEOM_ASSERT_PARAM(TgTRUE == V(tgGM_TS_Is_Valid)(psTS1));

    if (TgTRUE != F(tgCM_NR0)(F(tgPM_ABS)(psTS1->m_vNormal.m.x) - MKL(1.0)))
    {
        V(C_TgVEC)                          tvK0 = V(F_MUL_SV)(-psTS1->m_vNormal.m.x, &psTS1->m_vNormal);
        V(C_TgVEC)                          tvK1 = V(F_ADD)(&tvK0, &(V(KTgV_UNIT_X)));
        V(C_TgVEC)                          tvA = V(F_NORM_LEN)(&fTest, &tvK1);
        const TYPE                          fR_A = F(tgPM_ABS)(psTS1->m_fRO*tvA.m.x);

        TgERROR(TgTRUE != F(tgCM_NR0)(fTest));

        V(tgGM_BA_Set_MinX)( psBA0, psTS1->m_vOrigin.m.x - fR_A );
        V(tgGM_BA_Set_MaxX)( psBA0, psTS1->m_vOrigin.m.x + fR_A );
    }
    else
    {
        V(tgGM_BA_Set_MinX)( psBA0, psTS1->m_vOrigin.m.x );
        V(tgGM_BA_Set_MaxX)( psBA0, psTS1->m_vOrigin.m.x );
    };

    if (TgTRUE != F(tgCM_NR0)(F(tgPM_ABS)(psTS1->m_vNormal.m.y) - MKL(1.0)))
    {
        V(C_TgVEC)                          tvK0 = V(F_MUL_SV)(-psTS1->m_vNormal.m.y, &psTS1->m_vNormal);
        V(C_TgVEC)                          tvK1 = V(F_ADD)(&tvK0, &(V(KTgV_UNIT_Y)));
        V(C_TgVEC)                          tvA = V(F_NORM_LEN)(&fTest, &tvK1);
        const TYPE                          fR_A = F(tgPM_ABS)(psTS1->m_fRO*tvA.m.y);

        TgERROR(TgTRUE != F(tgCM_NR0)(fTest));

        V(tgGM_BA_Set_MinY)( psBA0, psTS1->m_vOrigin.m.y - fR_A );
        V(tgGM_BA_Set_MaxY)( psBA0, psTS1->m_vOrigin.m.y + fR_A );
    }
    else
    {
        V(tgGM_BA_Set_MinY)( psBA0, psTS1->m_vOrigin.m.y );
        V(tgGM_BA_Set_MaxY)( psBA0, psTS1->m_vOrigin.m.y );
    };

    if (TgTRUE != F(tgCM_NR0)(F(tgPM_ABS)(psTS1->m_vNormal.m.z) - MKL(1.0)))
    {
        V(C_TgVEC)                          tvK0 = V(F_MUL_SV)(-psTS1->m_vNormal.m.z, &psTS1->m_vNormal);
        V(C_TgVEC)                          tvK1 = V(F_ADD)(&tvK0, &(V(KTgV_UNIT_Z)));
        V(C_TgVEC)                          tvA = V(F_NORM_LEN)(&fTest, &tvK1);
        const TYPE                          fR_A = F(tgPM_ABS)(psTS1->m_fRO*tvA.m.z);

        TgERROR(TgTRUE != F(tgCM_NR0)(fTest));

        V(tgGM_BA_Set_MinZ)( psBA0, psTS1->m_vOrigin.m.z - fR_A );
        V(tgGM_BA_Set_MaxZ)( psBA0, psTS1->m_vOrigin.m.z + fR_A );
    }
    else
    {
        V(tgGM_BA_Set_MinZ)( psBA0, psTS1->m_vOrigin.m.z );
        V(tgGM_BA_Set_MaxZ)( psBA0, psTS1->m_vOrigin.m.z );
    };

    V(tgGM_BA_Expand)( psBA0, psTS1->m_fRI );
}


/* ---- V(tgGM_TS_Project) ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID V(tgGM_TS_Project)( PCU_TYPE ptyMin, PCU_TYPE ptyMax, V(CPCU_TgTORUS) psTS1, V(CPCU_TgVEC) ptvDN )
{
    const TYPE                          fDN_N = V(F_DOT)(ptvDN, &psTS1->m_vNormal);
    TYPE                                fLength;

    TgGEOM_ASSERT_PARAM(TgTRUE == V(tgGM_TS_Is_Valid)(psTS1) && TgTRUE == V(F_Is_Vector_Valid)(ptvDN) && TgTRUE == F(tgCM_NR1)(V(F_LSQ)(ptvDN)));

    if (F(tgCM_NR0)(F(tgPM_ABS)(fDN_N) - MKL(1.0)))
    {
        *ptyMin = F(KTgMAX);
        *ptyMax = -F(KTgMAX);
    }
    else
    {
        V(C_TgVEC)                          vX0 = V(F_NMS_SVV)(fDN_N, &psTS1->m_vNormal, ptvDN);
        V(C_TgVEC)                          tvX1 = V(F_NORM_LEN)(&fLength, &vX0);
        const TYPE                          fO_DN = V(F_DOT)(&psTS1->m_vOrigin, ptvDN);
        const TYPE                          fR_DN = F(tgPM_ABS)(psTS1->m_fRO*(V(F_DOT)(ptvDN, &tvX1)));

        TgERROR(TgTRUE != F(tgCM_NR0)(fLength));

        *ptyMin = fO_DN - fR_DN - psTS1->m_fRI;
        *ptyMax = fO_DN + fR_DN + psTS1->m_fRI;
    };
}


/* ---- V(tgGM_TS_Is_Contained) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL V(tgGM_TS_Is_Contained)( V(CPCU_TgTORUS) psTS1, V(CPCU_TgVEC) ptvS0 )
{
    V(TgVEC)                            tvDirN = V(F_SUB)(ptvS0, &psTS1->m_vOrigin);
    const TYPE                          fDN_N = V(F_DOT)(&tvDirN, &psTS1->m_vNormal);
    TYPE                                fLength;

    TgGEOM_ASSERT_PARAM(TgTRUE == V(F_Is_Point_Valid)(ptvS0));

    /* Check to see if its within the inner radius of the plane defining the torus. */
    if (F(tgPM_ABS)(fDN_N) > psTS1->m_fRI)
    {
        return (TgFALSE);
    }
    else
    {
        V(C_TgVEC)                          vX0 = V(F_NMS_SVV)(fDN_N, &psTS1->m_vNormal, &tvDirN);
        V(C_TgVEC)                          tvX1 = V(F_NORM_LEN)(&fLength, &vX0);

        /* Check if the point is not contained by infinite projections of the minimal and maximal circles of the torus. */
        if (fLength < psTS1->m_fRO - psTS1->m_fRI || fLength > psTS1->m_fRO + psTS1->m_fRI)
        {
            return (TgFALSE);
        }
        else
        {
            V(C_TgVEC)                         tvP0 = V(F_MAD_SVV)(psTS1->m_fRO, &tvX1, &psTS1->m_vOrigin);
            V(C_TgVEC)                         tvX2 = V(F_SUB)(ptvS0, &tvP0);

            if (V(F_LSQ)(&tvX2) > psTS1->m_fRI*psTS1->m_fRI)
            {
                return (TgFALSE);
            };

            return (TgTRUE);
        };
    };
}