Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Geometry 3D - Circle.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_CI_Is_Valid) --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL V(tgGM_CI_Is_Valid)( V(CPCU_TgCIRCLE) psCI1 )
{
    if (TgTRUE != V(F_Is_Point_Valid)(&psCI1->m.m.vOrigin))
    {
        return (TgFALSE);
    };

    if (TgTRUE != V(F_Is_Vector_Valid)(&psCI1->m.m.vNormal) || TgTRUE != F(tgCM_NR1)(V(F_LSQ)(&psCI1->m.m.vNormal)))
    {
        return (TgFALSE);
    };

    if (F(tgPM_NAN)(psCI1->m_fRadius) || !(psCI1->m_fRadius > MKL(0.0)))
    {
        return (TgFALSE);
    };

    return (TgTRUE);
}


/* ---- V(tgGM_CI_Support_Point) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
V(TgVEC) V(tgGM_CI_Support_Point)( V(CPCU_TgCIRCLE) psCI1, V(CPCU_TgVEC) ptvDN )
{
    const TYPE                          fD_N = V(F_DOT)(ptvDN, &psCI1->m.m.vNormal);

    TgERROR(TgTRUE == V(F_Is_Vector_Valid)(ptvDN) && TgTRUE == F(tgCM_NR0)(V(F_LSQ)(ptvDN) - MKL(1.0)));

    if (F(tgCM_NR0)(F(tgPM_ABS)(fD_N) - MKL(1.0)))
    {
        return (V(FS_SETP)(F(KTgMAX), F(KTgMAX), F(KTgMAX)));
    }
    else
    {
        TYPE                                fTest;

        V(C_TgVEC)                          vX0 = V(F_MUL_SV)(fD_N, &psCI1->m.m.vNormal);
        V(C_TgVEC)                          tvX1 = V(F_SUB)(ptvDN, &vX0);
        V(C_TgVEC)                          tvX2 = V(F_NORM_LEN)(&fTest, &tvX1);
        V(C_TgVEC)                          tvX3 = V(F_MUL_VS)(&tvX2, psCI1->m_fRadius);

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

        return (V(F_ADD)(&psCI1->m.m.vOrigin, &tvX3));
    };
}


/* ---- V(tgGM_CI_BA) --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID V(tgGM_CI_BA)( V(PCU_TgBOXAA) psBA0, V(CPCU_TgCIRCLE) psCI1 )
{
    TYPE                                fTest;

    TgGEOM_ASSERT_PARAM(TgTRUE == V(tgGM_CI_Is_Valid)(psCI1));

    if (TgTRUE != F(tgCM_NR0)(F(tgPM_ABS)(psCI1->m.m.vNormal.m.x) - MKL(1.0)))
    {
        V(C_TgVEC)                          tvK0 = V(F_MUL_SV)(-psCI1->m.m.vNormal.m.x, &psCI1->m.m.vNormal);
        V(C_TgVEC)                          tvK1 = V(F_ADD)(&tvK0, &(V(KTgV_UNIT_X)));
        V(C_TgVEC)                          tvK2 = V(F_NORM_LEN)(&fTest, &tvK1);

        const TYPE                          fR_A = F(tgPM_ABS)(psCI1->m_fRadius*tvK2.m.x);

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

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

    if (TgTRUE != F(tgCM_NR0)(F(tgPM_ABS)(psCI1->m.m.vNormal.m.y) - MKL(1.0)))
    {
        V(C_TgVEC)                          tvK0 = V(F_MUL_SV)(-psCI1->m.m.vNormal.m.y, &psCI1->m.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)(psCI1->m_fRadius*tvA.m.y);

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

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

    if (TgTRUE != F(tgCM_NR0)(F(tgPM_ABS)(psCI1->m.m.vNormal.m.z) - MKL(1.0)))
    {
        V(C_TgVEC)                          tvK0 = V(F_MUL_SV)(-psCI1->m.m.vNormal.m.z, &psCI1->m.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)(psCI1->m_fRadius*tvA.m.z);

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

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


/* ---- V(tgGM_CI_Project) ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID V(tgGM_CI_Project)( PCU_TYPE ptyMin, PCU_TYPE ptyMax, V(CPCU_TgCIRCLE) psCI1, V(CPCU_TgVEC) ptvDN )
{
    const TYPE                          fAx_N = V(F_DOT)(ptvDN, &psCI1->m.m.vNormal);

    TgGEOM_ASSERT_PARAM(0 != ptyMin && 0 != ptyMax && TgTRUE == V(tgGM_CI_Is_Valid)(psCI1) && TgTRUE == V(F_Is_Vector_Valid)(ptvDN));

    if (F(tgCM_NR0)(F(tgPM_ABS)(fAx_N) - MKL(1.0)))
    {
        *ptyMin = F(KTgMAX);
        *ptyMax = -F(KTgMAX);
    }
    else
    {
        TYPE                                fTest;

        V(C_TgVEC)                          vX0 = V(F_MUL_SV)(fAx_N, &psCI1->m.m.vNormal);
        V(C_TgVEC)                          tvX1 = V(F_SUB)(ptvDN, &vX0);
        V(C_TgVEC)                          tvX2 = V(F_NORM_LEN)(&fTest, &tvX1);
        const TYPE                          fOR_DN = V(F_DOT)(&psCI1->m.m.vOrigin, ptvDN);
        const TYPE                          fX2_DN = V(F_DOT)(ptvDN, &tvX2);
        const TYPE                          fRS_AX = F(tgPM_ABS)(psCI1->m_fRadius * fX2_DN);

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

        *ptyMin = fOR_DN - fRS_AX;
        *ptyMax = fOR_DN + fRS_AX;
    };
}