Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Geometry 3D - Cylinder.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_CY_Support_Point) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
V(TgVEC) V(tgGM_CY_Support_Point)( V(CPCU_TgTUBE) psCY0, V(CPCU_TgVEC) ptvDirN )
{
    const TYPE                          fUAX_N = V(F_DOT)(ptvDirN, &psCY0->m.m.vU_HAX);
    V(TgVEC)                            vResult;
    TYPE                                fTest;

    TgERROR(TgTRUE == F(tgCM_NR1)(V(F_LSQ)(ptvDirN)) && TgTRUE == V(F_Is_Vector_Valid)(ptvDirN));

    if (fUAX_N > F(KTgEPS))
    {
        vResult = V(F_ADD)(&psCY0->m.m.vOrigin, &psCY0->m_vHAX);
    }
    else if (fUAX_N < -F(KTgEPS))
    {
        vResult = V(F_SUB)(&psCY0->m.m.vOrigin, &psCY0->m_vHAX);
    }
    else
    {
        vResult = psCY0->m.m.vOrigin;
    }

    {
        V(C_TgVEC)                          vX0 = V(F_MUL_SV)(fUAX_N, &psCY0->m.m.vU_HAX);
        V(C_TgVEC)                          tvX1 = V(F_SUB)(ptvDirN, &vX0);
        V(C_TgVEC)                          tvRadial = V(F_NORM_LEN)(&fTest, &tvX1);

        if (fTest > F(KTgEPS))
        {
            vResult = V(F_MAD_SVV)(psCY0->m_fRadius, &tvRadial, &vResult);
        };
    }

    return (vResult);
}


/* ---- V(tgGM_CY_Is_Cap_Contained) ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL V(tgGM_CY_Is_Cap_Contained)( V(PCU_TgELLIPSE) psEL0, V(CPCU_TgTUBE) psCY0, const TYPE fDS_C0A, V(CPCU_TgTUBE) psCY1, const TYPE fDS_C1A )
{
 /* Containment Test - the projection of the cap of cylinder 1 onto the cap of cylinder 0 */

    V(C_TgVEC)                          vX0 = V(F_MUL_SV)(psCY0->m.m.vU_HAX.m.x, &psCY0->m.m.vU_HAX);
    V(C_TgVEC)                          tvX1 = V(F_MUL_SV)(psCY0->m.m.vU_HAX.m.y, &psCY0->m.m.vU_HAX);
    V(C_TgVEC)                          tvX2 = V(F_MUL_SV)(psCY0->m.m.vU_HAX.m.z, &psCY0->m.m.vU_HAX);
    V(C_TgVEC)                          tvXF0 = V(F_SUB)(&(V(KTgV_UNIT_X)), &vX0);
    V(C_TgVEC)                          tvXF1 = V(F_SUB)(&(V(KTgV_UNIT_Y)), &tvX1);
    V(C_TgVEC)                          tvXF2 = V(F_SUB)(&(V(KTgV_UNIT_Z)), &tvX2);

    /* Transform the centre of the caps of closest proximity */

    V(C_TgVEC)                          tvB0 = V(F_MUL_SV)(F(tgPM_FSEL)(fDS_C0A, MKL(1.0), MKL(-1.0)), &psCY0->m_vHAX);
    V(C_TgVEC)                          tvB1 = V(F_MUL_SV)(F(tgPM_FSEL)(fDS_C1A, MKL(-1.0), MKL(1.0)), &psCY1->m_vHAX);
    V(C_TgVEC)                          tvW0 = V(F_ADD)(&psCY0->m.m.vOrigin, &tvB0);
    V(C_TgVEC)                          tvW1 = V(F_ADD)(&psCY1->m.m.vOrigin, &tvB1);
    const TYPE                          fA0 = V(F_DOT)(&tvXF0, &tvW0);
    const TYPE                          fA1 = V(F_DOT)(&tvXF1, &tvW0);
    const TYPE                          fA2 = V(F_DOT)(&tvXF2, &tvW0);
    const TYPE                          fB0 = V(F_DOT)(&tvXF0, &tvW1);
    const TYPE                          fB1 = V(F_DOT)(&tvXF1, &tvW1);
    const TYPE                          fB2 = V(F_DOT)(&tvXF2, &tvW1);
    V(C_TgVEC)                          tvW2 = V(FS_SETP)(fA0, fA1, fA2);
    V(C_TgVEC)                          tvW3 = V(FS_SETP)(fB0, fB1, fB2);
    const TYPE                          fU0 = V(F_DOT)(&tvXF0, &psCY1->m.m.vU_HAX);
    const TYPE                          fU1 = V(F_DOT)(&tvXF1, &psCY1->m.m.vU_HAX);
    const TYPE                          fU2 = V(F_DOT)(&tvXF2, &psCY1->m.m.vU_HAX);

    V(TgVEC)                            tvA0, tvA1;
    V(TgVEC)                            tvAxis;
    TYPE                                fCapRadiusRatio;

    /* Ellipse Axes */

    tvAxis = V(FS_SETV)(fU0, fU1, fU2);

    if (V(F_LSQ)(&tvAxis) > F(KTgEPS))
    {
        tvA0 = V(F_UCX)(&tvAxis, &psCY0->m.m.vU_HAX);
        tvAxis = V(F_CX)(&psCY1->m.m.vU_HAX, &tvA0);
        tvA1 = V(FS_SETV)(V(F_DOT)(&tvXF0, &tvAxis), V(F_DOT)(&tvXF1, &tvAxis), V(F_DOT)(&tvXF2, &tvAxis));
    }
    else
    {
        tvA0 = psCY0->m.m.vU_Basis0;
        tvA1 = psCY0->m.m.vU_Basis1;
    };

    tvA1 = V(F_NORM_LEN)(&fCapRadiusRatio, &tvA1);

    {
        const TYPE                          fMajRad = fCapRadiusRatio*psCY1->m_fRadius;
        const TYPE                          fA00 = V(F_DOT)(&tvA0, &psCY0->m.m.vU_Basis0);
        const TYPE                          fA01 = V(F_DOT)(&tvA0, &psCY0->m.m.vU_Basis1);
        const TYPE                          fA10 = V(F_DOT)(&tvA1, &psCY0->m.m.vU_Basis0);
        const TYPE                          fA11 = V(F_DOT)(&tvA1, &psCY0->m.m.vU_Basis1);
        const TYPE                          fR00 = fA00 * psCY1->m_fRadius;
        const TYPE                          fR01 = fA01 * psCY1->m_fRadius;
        const TYPE                          fR10 = fA10 * fMajRad;
        const TYPE                          fR11 = fA11 * fMajRad;
        V(C_TgVEC)                          tvW32 = V(F_SUB)(&tvW3, &tvW2);
        const TYPE                          fWC0 = V(F_DOT)(&tvW32, &psCY0->m.m.vU_Basis0);
        const TYPE                          fWC1 = V(F_DOT)(&tvW32, &psCY0->m.m.vU_Basis1);

        /* Initialize the resulting ellipse formed by the projection */

        V(tgGM_EL_Set_Origin)( psEL0, &tvW3 );
        V(tgGM_EL_Set_Minor_Unit)( psEL0, &tvA0 );
        V(tgGM_EL_Set_Normal)( psEL0, &psCY0->m.m.vU_HAX );
        V(tgGM_EL_Set_Major_Unit)( psEL0, &tvA1 );
        V(tgGM_EL_Set_Minor_Radius)( psEL0, psCY1->m_fRadius );
        V(tgGM_EL_Set_Major_Radius)( psEL0, fMajRad );

        if (
            (fWC0 + fR00)*(fWC0 + fR00) + (fWC1 + fR01)*(fWC1 + fR01) > psCY0->m_fRadiusSq ||
            (fWC0 - fR00)*(fWC0 - fR00) + (fWC1 - fR01)*(fWC1 - fR01) > psCY0->m_fRadiusSq ||
            (fWC0 + fR10)*(fWC0 + fR10) + (fWC1 + fR11)*(fWC1 + fR11) > psCY0->m_fRadiusSq ||
            (fWC0 - fR10)*(fWC0 - fR10) + (fWC1 - fR11)*(fWC1 - fR11) > psCY0->m_fRadiusSq
        )
        {
            return (TgFALSE);
        };
    };

    return (TgTRUE);
}