Home

Resume

Blog

Teikitu


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

/* ---- FI(tgGM_Volume_MS) ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE FI(tgGM_Volume_MS)(V(CPCU_TgMESH_SIMPLE) psMS1)
{
    TYPE                                fVolume = MKL(0.0);
    TgMESH_ITER                         itMS;
    TgUINT32                            uiIndex;

    FI(tgGM_Iterator_Init_MS)(&itMS, psMS1);

    for (uiIndex = 0; uiIndex < itMS.m_nuiFace; ++uiIndex)
    {
        V(CPCU_TgVEC)                       ptvS0 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(CPCU_TgVEC)                       ptvS1 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(CPCU_TgVEC)                       ptvS2 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(C_TgVEC)                          tvE0 = V(F_SUB)(ptvS1, ptvS0);
        V(C_TgVEC)                          tvE2 = V(F_SUB)(ptvS2, ptvS0);
        const TYPE                          fNx = tvE0.m.y*tvE2.m.z - tvE0.m.z*tvE2.m.y;
        const TYPE                          fNy = tvE0.m.z*tvE2.m.x - tvE0.m.x*tvE2.m.z;
        const TYPE                          fNz = tvE0.m.x*tvE2.m.y - tvE0.m.y*tvE2.m.x;
        const TYPE                          fTriD = fNx*ptvS0->m.x + fNy*ptvS0->m.y + fNz*ptvS0->m.z;

        TYPE                                fDA, fDB, fAA, fBB, fC1, fCA, fCB, fP1, fPA, fPB;
        TgSINT32                            i0, i1, i2;

        if (fNx > fNy && fNx > fNz)
        {
            i2 = 0;
        }
        else
        {
            i2 = (fNy > fNz) ? 1 : 2;
        };

        i0 = (i2 + 1) % 3;
        i1 = (i2 + 2) % 3;

        fDA = ptvS1->m.x - ptvS0->m.x;
        fDB = ptvS1->m.y - ptvS0->m.y;
        fAA = ptvS0->m.x * ptvS0->m.x;
        fBB = ptvS0->m.y * ptvS0->m.y;

        fC1 = ptvS1->m.x + ptvS0->m.x;
        fCA = ptvS1->m.x * fC1 + fAA;
        fCB = ptvS1->m.y * (ptvS1->m.y + ptvS0->m.y) + fBB;

        fP1 = fDB * fC1;
        fPA = fDB * fCA;
        fPB = fDA * fCB;

        fDA = ptvS2->m.x - ptvS1->m.x;
        fDB = ptvS2->m.y - ptvS1->m.y;
        fAA = ptvS1->m.x * ptvS1->m.x;
        fBB = ptvS1->m.y * ptvS1->m.y;

        fC1 = ptvS2->m.x + ptvS1->m.x;
        fCA = ptvS2->m.x * fC1 + fAA;
        fCB = ptvS2->m.y * (ptvS2->m.y + ptvS1->m.y) + fBB;

        fP1 += fDB * fC1;
        fPA += fDB * fCA;
        fPB += fDA * fCB;

        fDA = ptvS0->m.x - ptvS2->m.x;
        fDB = ptvS0->m.y - ptvS2->m.y;
        fAA = ptvS2->m.x * ptvS2->m.x;
        fBB = ptvS2->m.y * ptvS2->m.y;

        fC1 = ptvS0->m.x + ptvS2->m.x;
        fCA = ptvS0->m.x * fC1 + fAA;
        fCB = ptvS0->m.y * (ptvS0->m.y + ptvS2->m.y) + fBB;

        fP1 += fDB * fC1;
        fPA += fDB * fCA;
        fPB += fDA * fCB;

        fP1 /= MKL(2.0);
        fPA /= MKL(6.0);
        fPB /= MKL(-6.0);

        if (i0 == 0) /* 0,1,2 */
        {
            fVolume += fNx * fPA / fNz;
        }
        else if (i1 == 0) /* 2,0,1 */
        {
            fVolume += fNz * fPB / fNy;
        }
        else /* 1,2,0 */
        {
            fVolume += (fNy / -(fNx * fNx)) * (fNy * fPA + fNz * fPB - fTriD * fP1);
        };
    };

    return (fVolume);
}


/* ---- FI(tgGM_Area_MS) ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE FI(tgGM_Area_MS)(V(CPCU_TgMESH_SIMPLE) psMS1)
{
    TYPE                                fArea = MKL(0.0);
    TgMESH_ITER                         itMS;
    TgUINT32                            uiIndex;

    FI(tgGM_Iterator_Init_MS)(&itMS, psMS1);

    for (uiIndex = 0; uiIndex < itMS.m_nuiFace; ++uiIndex)
    {
        V(CPCU_TgVEC)                       ptvS0 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(CPCU_TgVEC)                       ptvS1 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(CPCU_TgVEC)                       ptvS2 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(C_TgVEC)                          tvE0 = V(F_SUB)(ptvS1, ptvS0);
        V(C_TgVEC)                          tvE2 = V(F_SUB)(ptvS2, ptvS0);
        V(C_TgVEC)                          tvNM = V(F_CX)(&tvE0, &tvE2);

        fArea += MKL(0.5)*V(F_LEN)(&tvNM);
    };

    return (fArea);
}


/* ---- FI(tgGM_Is_Contained_MS) ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL FI(tgGM_Is_Contained_MS)(V(CPCU_TgMESH_SIMPLE) psMS1, V(CPCU_TgVEC) ptvS3)
{
    TgMESH_ITER                         itMS;
    TgUINT32                            uiIndex;

    FI(tgGM_Iterator_Init_MS)(&itMS, psMS1);

    for (uiIndex = 0; uiIndex < itMS.m_nuiFace; ++uiIndex)
    {
        V(CPCU_TgVEC)                       ptvS0 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(CPCU_TgVEC)                       ptvS1 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(CPCU_TgVEC)                       ptvS2 = psMS1->m_pvVert_List + FI(tgGM_Iterator_Incr_MS)(&itMS, psMS1);
        V(C_TgVEC)                          tvE0 = V(F_SUB)(ptvS1, ptvS0);
        V(C_TgVEC)                          tvE2 = V(F_SUB)(ptvS2, ptvS0);
        V(C_TgVEC)                          tvNM = V(F_CX)(&tvE0, &tvE2);
        V(C_TgVEC)                          vX0 = V(F_SUB)(ptvS3, ptvS0);

        if (MKL(0.0) >= V(F_DOT3)(&tvNM, &vX0))
        {
            return (TgFALSE);
        };
    };

    return (TgTRUE);
}


/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions - Mesh BA */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- FI(tgGM_MA_Update_Face_BA) -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID FI(tgGM_MA_Update_Face_BA)(V(PCU_TgMESH_BA) psMA0)
{
    TgMESH_ITER                         itMS;
    TgUINT32                            uiFace;

    FI(tgGM_Iterator_Init_MS)(&itMS, &psMA0->m_sMS);

    psMA0->m_vCG[0] = V(KTgV_ZERO);
    psMA0->m_vCG[1] = V(KTgV_ZERO);

    if (ETgSWEEP_ROTATIONAL == psMA0->m_enSweep)
    {
        for (uiFace = 0; uiFace < itMS.m_nuiFace; ++uiFace)
        {
            C_TgUINT32                          uiI0 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI1 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI2 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);

            V(CPCU_TgVEC)                       ptvX0 = psMA0->m_sMS.m_pvVert_List + uiI0;
            V(CPCU_TgVEC)                       ptvX1 = psMA0->m_sMS.m_pvVert_List + uiI1;
            V(CPCU_TgVEC)                       ptvX2 = psMA0->m_sMS.m_pvVert_List + uiI2;

            V(C_TgVEC)                          tvX3 = V(F_ADD)(ptvX0, psMA0->m_pvVert_DT + uiI0);
            V(C_TgVEC)                          tvX4 = V(F_ADD)(ptvX1, psMA0->m_pvVert_DT + uiI1);
            V(C_TgVEC)                          tvX5 = V(F_ADD)(ptvX2, psMA0->m_pvVert_DT + uiI2);

            V(tgGM_BA_Init_PT)( psMA0->m_psBA + uiFace, ptvX0 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX1 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX2 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, &tvX3 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, &tvX4 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, &tvX5 );
        };
    }
    else if (ETgSWEEP_LINEAR == psMA0->m_enSweep)
    {
        for (uiFace = 0; uiFace < itMS.m_nuiFace; ++uiFace)
        {
            C_TgUINT32                          uiI0 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI1 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI2 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);

            V(CPCU_TgVEC)                       ptvX0 = psMA0->m_sMS.m_pvVert_List + uiI0;
            V(CPCU_TgVEC)                       ptvX1 = psMA0->m_sMS.m_pvVert_List + uiI1;
            V(CPCU_TgVEC)                       ptvX2 = psMA0->m_sMS.m_pvVert_List + uiI2;

            V(tgGM_BA_Init_PT)( psMA0->m_psBA + uiFace, ptvX0 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX1 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX2 );
            V(tgGM_BA_Sweep)( psMA0->m_psBA + uiFace, &psMA0->m_sDT.m_vDT );
        };
    }
    else if (ETgSWEEP_STATIC == psMA0->m_enSweep)
    {
        for (uiFace = 0; uiFace < itMS.m_nuiFace; ++uiFace)
        {
            C_TgUINT32                          uiI0 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI1 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI2 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);

            V(CPCU_TgVEC)                       ptvX0 = psMA0->m_sMS.m_pvVert_List + uiI0;
            V(CPCU_TgVEC)                       ptvX1 = psMA0->m_sMS.m_pvVert_List + uiI1;
            V(CPCU_TgVEC)                       ptvX2 = psMA0->m_sMS.m_pvVert_List + uiI2;

            V(tgGM_BA_Init_PT)( psMA0->m_psBA + uiFace, ptvX0 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX1 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX2 );
        };
    };
}


/* ---- FI(tgGM_MA_Update_Face_CG_BA) ----------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID FI(tgGM_MA_Update_Face_CG_BA)(V(PCU_TgMESH_BA) psMA0)
{
    TgMESH_ITER                         itMS;
    TgUINT32                            uiFace;

    FI(tgGM_Iterator_Init_MS)(&itMS, &psMA0->m_sMS);

    psMA0->m_vCG[0] = V(KTgV_ZERO);
    psMA0->m_vCG[1] = V(KTgV_ZERO);

    if (ETgSWEEP_ROTATIONAL == psMA0->m_enSweep)
    {
        for (uiFace = 0; uiFace < itMS.m_nuiFace; ++uiFace)
        {
            C_TgUINT32                          uiI0 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI1 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI2 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);

            V(CPCU_TgVEC)                       ptvX0 = psMA0->m_sMS.m_pvVert_List + uiI0;
            V(CPCU_TgVEC)                       ptvX1 = psMA0->m_sMS.m_pvVert_List + uiI1;
            V(CPCU_TgVEC)                       ptvX2 = psMA0->m_sMS.m_pvVert_List + uiI2;

            V(C_TgVEC)                          tvX3 = V(F_ADD)(ptvX0, psMA0->m_pvVert_DT + uiI0);
            V(C_TgVEC)                          tvX4 = V(F_ADD)(ptvX1, psMA0->m_pvVert_DT + uiI1);
            V(C_TgVEC)                          tvX5 = V(F_ADD)(ptvX2, psMA0->m_pvVert_DT + uiI2);

            V(C_TgVEC)                          tvX6 = V(F_ADD)(ptvX1, ptvX2);
            V(C_TgVEC)                          tvX7 = V(F_ADD)(&tvX4, &tvX5);
            V(C_TgVEC)                          tvX8 = V(F_ADD)(ptvX0, &tvX6);
            V(C_TgVEC)                          tvX9 = V(F_ADD)(&tvX3, &tvX7);

            psMA0->m_vCG[0] = V(F_ADD)(psMA0->m_vCG + 0, &tvX8);
            psMA0->m_vCG[1] = V(F_ADD)(psMA0->m_vCG + 1, &tvX9);

            V(tgGM_BA_Init_PT)( psMA0->m_psBA + uiFace, ptvX0 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX1 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX2 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, &tvX3 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, &tvX4 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, &tvX5 );
        };

        psMA0->m_vCG[0] = V(F_DIV_VS)(psMA0->m_vCG + 0, (TYPE)(itMS.m_nuiFace * MKL(3.0)));
        psMA0->m_vCG[1] = V(F_DIV_VS)(psMA0->m_vCG + 1, (TYPE)(itMS.m_nuiFace * MKL(3.0)));
    }
    else if (ETgSWEEP_LINEAR == psMA0->m_enSweep)
    {
        for (uiFace = 0; uiFace < itMS.m_nuiFace; ++uiFace)
        {
            C_TgUINT32                          uiI0 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI1 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI2 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);

            V(CPCU_TgVEC)                       ptvX0 = psMA0->m_sMS.m_pvVert_List + uiI0;
            V(CPCU_TgVEC)                       ptvX1 = psMA0->m_sMS.m_pvVert_List + uiI1;
            V(CPCU_TgVEC)                       ptvX2 = psMA0->m_sMS.m_pvVert_List + uiI2;
            V(C_TgVEC)                          tvX3 = V(F_ADD)(ptvX1, ptvX2);
            V(C_TgVEC)                          tvX4 = V(F_ADD)(ptvX0, &tvX3);

            psMA0->m_vCG[0] = V(F_ADD)(psMA0->m_vCG + 0, &tvX4);

            V(tgGM_BA_Init_PT)( psMA0->m_psBA + uiFace, ptvX0 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX1 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX2 );
            V(tgGM_BA_Sweep)( psMA0->m_psBA + uiFace, &psMA0->m_sDT.m_vDT );
        };

        psMA0->m_vCG[0] = V(F_DIV_VS)(psMA0->m_vCG + 0, (TYPE)(itMS.m_nuiFace * MKL(3.0)));
        psMA0->m_vCG[1] = V(F_ADD)(psMA0->m_vCG + 0, &psMA0->m_sDT.m_vDT);
    }
    else if (ETgSWEEP_STATIC == psMA0->m_enSweep)
    {
        for (uiFace = 0; uiFace < itMS.m_nuiFace; ++uiFace)
        {
            C_TgUINT32                          uiI0 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI1 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);
            C_TgUINT32                          uiI2 = FI(tgGM_Iterator_Incr_MS)(&itMS, &psMA0->m_sMS);

            V(CPCU_TgVEC)                       ptvX0 = psMA0->m_sMS.m_pvVert_List + uiI0;
            V(CPCU_TgVEC)                       ptvX1 = psMA0->m_sMS.m_pvVert_List + uiI1;
            V(CPCU_TgVEC)                       ptvX2 = psMA0->m_sMS.m_pvVert_List + uiI2;
            V(C_TgVEC)                          tvX3 = V(F_ADD)(ptvX1, ptvX2);
            V(C_TgVEC)                          tvX4 = V(F_ADD)(ptvX0, &tvX3);

            psMA0->m_vCG[0] = V(F_ADD)(psMA0->m_vCG + 0, &tvX4);

            V(tgGM_BA_Init_PT)( psMA0->m_psBA + uiFace, ptvX0 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX1 );
            V(tgGM_BA_Union_PT)( psMA0->m_psBA + uiFace, ptvX2 );
        }

        psMA0->m_vCG[0] = V(F_DIV_VS)(psMA0->m_vCG + 0, (TYPE)(itMS.m_nuiFace * MKL(3.0)));
        psMA0->m_vCG[1] = V(KTgV_ZERO);
    }
    else
    {
        TgERROR( TgFALSE );
    };
}