Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Clip [Template].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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Collision ========================================================================================================================================================= */

/* ---- VCO_F(Clip) ----------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT VCO_F(Clip)( V(PC_STg2_CO_Clip_List) psCL, CPC_PRIM psPM0 )
{
    TgSINT32                            iIdx = 0, niCount = 0;
    TgBOOL                              bFirst = TgFALSE;
    TYPE                                fG0, fG1;
    V(PC_TgVEC)                         avData = psCL->m_avPoint;
    V(P_TgVEC)                          avPoint;

    TgALLOCA( V(TgVEC), (TgUINT32)psCL->m_niPoint + 2u, avPoint );

    if (!avPoint || psCL->m_niMax <= 2)
    {
        /* Integrity check on the data stream.  There must exist at least two points in the clip list. */
        return (KTgE_FAIL);
    };

    /* First iterate to find the first point contained inside of the tube. */
    for (; iIdx < psCL->m_niPoint; ++iIdx)
    {
        /* Check to see if the point is contained within the primitive */

        if (!VGM(Is_Contained)( psPM0, avData + iIdx ))
        {
            continue;
        };

        if (iIdx != 0)
        {
            /* F_Clip the line segment from iPrevIdx to iIdx to the primitive, adding the clipped point to the list */
            if (TgSUCCEEDED( VCO_F(Clip_Points)( &fG0, &fG1, psPM0, &avData[iIdx], &avData[iIdx - 1] ) ))
            {
                V(C_TgVEC)                          vK0 = V(F_MUL_SV)( fG1, &(avData[iIdx - 1] ));
                V(C_TgVEC)                          vK1 = V(F_MUL_SV)( MKL(1.0) - fG1, &(avData[iIdx]) );

                TgERROR( (TgTRUE != F(tgPM_NAN)(fG1)) && fG1 > MKL(0.0) && fG1 < MKL(1.0) );

                avPoint[niCount++] = V(F_ADD)( &vK1, &vK0 );
            }
            else
            {
                TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): tgCO_F_Internal_Clip did not find intersection.\n" ), TgT("Collision"), TgT("F_Internal_Clip") );
            };
        }
        else
        {
            bFirst = TgTRUE;
        };

        /* Add the first point contained by the primitive */

        avPoint[niCount++] = avData[iIdx];

        break;
    };

    /*  Either all points have been found to be outside of the primitive, which will cause processing to logically terminate or the first point contained within the */
    /* primitive has been isolated. Further processing is done by sequentially adding contained points or clipping the segment between a known contained point and an */
    /* uncontained point. */
    for (; iIdx < psCL->m_niPoint; ++iIdx)
    {
        TgSINT32                            iNIdx = (iIdx + 1) % psCL->m_niPoint;

        /* Check to see if the point is contained inside the primitive */

        if (!VGM(Is_Contained)( psPM0, avData + iNIdx ))
        {
            /* F_Clip the line segment from iIdx to iNIdx to the primitive, adding the clipped point to the list */
            if (TgSUCCEEDED( VCO_F(Clip_Points)( &fG0, &fG1, psPM0, avData + iIdx, avData + iNIdx ) ))
            {
                TgERROR( (TgTRUE != F(tgPM_NAN)( fG1 )) && fG1 > MKL(0.0) && fG1 < MKL(1.0) );
                if (niCount >= psCL->m_niMax)
                {
                    TgFREEA( avPoint );
                    return (KTgE_FAIL);
                }
                else
                {
                    V(C_TgVEC)                          vK1 = V(F_MUL_SV)( MKL(1.0) - fG1, avData + iIdx );
                    V(C_TgVEC)                          vK2 = V(F_MUL_SV)( fG1, avData + iNIdx );

                    avPoint[niCount++] = V(F_ADD)(&vK1, &vK2);
                };
            }
            else
            {
                TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32): tgCO_F_Internal_Clip did not find intersection.\n" ), TgT("Collision"), TgT("F_Internal_Clip") );
            };

            /* Continue through the list until a new valid point is found */
            for (++iIdx; iIdx < psCL->m_niPoint; ++iIdx)
            {
                iNIdx = (iIdx + 1) % psCL->m_niPoint;

                /* Check to see if the point is contained within the primitive */

                if (!VGM(Is_Contained) (psPM0, avData + iNIdx ))
                {
                    continue;
                }

                /* F_Clip the line segment from iIdx to iNIdx to the primitive, adding the clipped point to the list */
                if (TgSUCCEEDED( VCO_F(Clip_Points)( &fG0, &fG1, psPM0, avData + iNIdx, avData + iIdx ) ))
                {
                    TgERROR( (TgTRUE != F(tgPM_NAN)( fG1 )) && fG1 > MKL(0.0) && fG1 < MKL(1.0) );
                    if (niCount >= psCL->m_niMax)
                    {
                        TgFREEA( avPoint );
                        return (KTgE_FAIL);
                    }
                    else
                    {
                        V(C_TgVEC)                          vK4 = V(F_MUL_SV)( MKL(1.0) - fG1, avData + iNIdx );
                        V(C_TgVEC)                          vK5 = V(F_MUL_SV)( fG1, avData + iIdx );

                        avPoint[niCount++] = V(F_ADD)( &vK4, &vK5 );
                    }
                }
                else
                {
                    TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32): tgCO_F_Internal_Clip did not find intersection.\n" ), TgT("Collision"), TgT("F_Internal_Clip") );
                };

                break;
            };

            if (iIdx == psCL->m_niPoint - 1)
            {
                if (bFirst)
                {
                    /* F_Clip the line segment from iIdx to iNIdx to the primitive, adding the clipped point to the list */
                    if (TgSUCCEEDED( VCO_F(Clip_Points)( &fG0, &fG1, psPM0, avData, avData + iIdx ) ))
                    {
                        TgERROR( (TgTRUE != F(tgPM_NAN)( fG1 )) && fG1 > MKL(0.0) && fG1 < MKL(1.0) );
                        if (niCount >= psCL->m_niMax)
                        {
                            TgFREEA( avPoint );
                            return (KTgE_FAIL);
                        }
                        else
                        {
                            V(C_TgVEC)                          vK6 = V(F_MUL_SV)( MKL(1.0) - fG1, avData );
                            V(C_TgVEC)                          vK7 = V(F_MUL_SV)( fG1, avData + iIdx );

                            avPoint[niCount++] = V(F_ADD)( &vK6, &vK7 );
                        }
                    }
                    else
                    {
                        TgERROR_MSGF( 0, TgT( "%-16.16s(%-32.32s): Algorithm - Tube F_Clip did not find intersection.\n" ), TgT("Collision"), TgT("F_Internal_Clip") );
                    };
                };

                break;
            };
        };

        if (niCount >= psCL->m_niMax)
        {
            TgFREEA( avPoint );
            return (KTgE_FAIL);
        };

        avPoint[niCount++] = avData[iNIdx];
    };

    TgERROR( niCount < psCL->m_niMax );

    for (iIdx = 0; iIdx < niCount; ++iIdx)
    {
        psCL->m_avPoint[iIdx] = avPoint[iIdx];
    };

    psCL->m_niPoint = niCount;
    TgFREEA( avPoint );

    return (KTgS_OK);
}