Home

Resume

Blog

Teikitu


// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//  »Project«   Teikitu Gaming System (TgS) (∂)
//  »File«      TgS Collision - F - Box-Box.c_inc
//  »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
//  »Version«   4.0
//  »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Box;
// ------------------------------------------------------------------------------------------------------------------------------ //
//  Copyright: © 2002-2010, 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: 
//    Redistributions of source code must retain this copyright notice, this list of conditions and the following disclaimers. 
//    Redistributions 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. 
//  Neither the names of the copyright owner nor the names of its contributors may 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 ================================================================================================================= //

// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //
//  File Local Functions
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //

static TgBOOL                               V(tgCO_F_Penetrate_Axis_Seperation_BX_BX)(
                                                V(PCU_STg2_CO_Axis_Result),V(CPCU_TgBOX),V(CPCU_TgBOX) );
static TgRESULT                             V(tgCO_F_Penetrate_Box_Axis_BX_BX)(
                                                V(PCU_STg2_CO_Packet),V(PCU_STg2_CO_Axis_Result),V(CPCU_TgBOX),V(CPCU_TgBOX) );




// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //
//  Public Functions
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //

// ---- tgCO_F_Penetrate_BX_BX -------------------------------------------------------------------------------------------------- //
// Input:  tgPacket: The current series of contact points for this query-series, and contact generation parameters.
// Input:  sBX0: Box primitive
// Input:  sBX1: Box - contact points are generated on this primitive
// Output: tgPacket: Points of penetration between the two primitives are added to it
// Return: Result Code
// ------------------------------------------------------------------------------------------------------------------------------ //
TgRESULT V(tgCO_F_Penetrate_BX_BX)( V(PCU_STg2_CO_Packet) psPacket, V(CPCU_TgBOX) psBX0, V(CPCU_TgBOX) psBX1 )
{
    C_TgSINT32                          niContact = psPacket->m_niContact;
    V(P_STg2_CO_Contact)                psContact;
    V(STg2_CO_Axis_Result)              sAxS;

    TgASSERT_PARAM((TgSIZE)psPacket->m_iStride >= sizeof( V(P_STg2_CO_Contact) ));
    TgASSERT_PARAM(V(tgGM_Is_Valid_BX)( psBX0 ) && V(tgGM_Is_Valid_BX)( psBX1 ));

    if (0 == psPacket->m_niMaxContact || psPacket->m_niContact >= psPacket->m_niMaxContact || NULL == psPacket->m.psContact)
    {
        return (TgE_FAIL);
    };

    // Find the minimal axis of separation, or return if the primitives are not in contact.

    if (!V(tgCO_F_Penetrate_Axis_Seperation_BX_BX)( &sAxS, psBX0,psBX1 ))
    {
        return (ETgE_NO_INTERSECT);
    };

    TgASSERT( F(tgCM_NR1)( V(F_LSQ)( &sAxS.m_vNormal ) ) && sAxS.m_fDepth >= MKL(0.0) );

    // == Contact Generation ==================================================================================================== //

    psContact = (V(P_STg2_CO_Contact))(psPacket->m.piContact + psPacket->m_niContact*psPacket->m_iStride);

    if (sAxS.m_iAxis >= 12)
    {
    }
    else
    {
        if (sAxS.m_iAxis >= 6)
        {
            return (V(tgCO_F_Penetrate_Box_Axis_BX_BX)( psPacket, &sAxS, psBX0, psBX1 ));
        }
        else
        {
            return (V(tgCO_F_Penetrate_Box_Axis_BX_BX)( psPacket, &sAxS, psBX0, psBX1 ));
        };
    };

    return (niContact == psPacket->m_niContact ? ETgE_NO_INTERSECT : TgS_OK);
}




// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //
//  File Local Functions
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. //

// ---- tgCO_F_Penetrate_Axis_Seperation_BX_BX ---------------------------------------------------------------------------------- //
// Input:  sBX0, sBX1: Box primitives
// Output: sAxS: Structure holds the resulting axis separation information necessary to create a contact set.
// Return: False if a separating axis exists, true otherwise
// ------------------------------------------------------------------------------------------------------------------------------ //
TgBOOL V(tgCO_F_Penetrate_Axis_Seperation_BX_BX)( V(PCU_STg2_CO_Axis_Result) psAxS, V(CPCU_TgBOX) psBX0, V(CPCU_TgBOX) psBX1 )
{
    TYPE                                fMinBox0, fMaxBox0, fMinBox1, fMaxBox1;
    TgBOOL                              bUseAxis[3] = { TgTRUE, TgTRUE, TgTRUE };
    TgSINT32                            iIndex;
    TgUINT32                            iBox0, iBox1;

    // -- Axis: Box Face/Plane Normals -------------------------

    for (iIndex = 0; iIndex < 3; ++iIndex)
    {
        // Determine the extents of the primitives along the chosen axis.

        V(C_TgVEC)                          vAxisUnit0 = psBX0->m.m.avAxis[iIndex];
        const TYPE                          fB0_AU = V(F_DOT_VV)( &psBX0->m.m.vOrigin, &vAxisUnit0 );

        fMinBox0 = fB0_AU - psBX0->m_vExtent.m_aData[iIndex];
        fMaxBox0 = fB0_AU + psBX0->m_vExtent.m_aData[iIndex];
        V(tgGM_Project_BX)( &fMinBox1, &fMaxBox1, psBX1, &vAxisUnit0 );

        if (fMaxBox1 < fMinBox0 || fMinBox1 > fMaxBox0) //« Separation Test.
        {
            return (TgFALSE);
        };

        {   // Selection of the best (minimal depth) axis.
            const TYPE                          fMinDepth = fMaxBox1 - fMinBox0;
            const TYPE                          fMaxDepth = fMaxBox0 - fMinBox1;
            C_TgBOOL                            bNegAxis = fMinDepth > fMaxDepth;

            if ((bNegAxis ? fMaxDepth : fMinDepth) < psAxS->m_fDepth)
            {
                psAxS->m_vNormal = bNegAxis ? vAxisUnit0 : V(F_NEG)( &vAxisUnit0 );
                psAxS->m_fDepth = bNegAxis ? fMaxDepth : fMinDepth;
                psAxS->m_iAxis = bNegAxis ? iIndex + 3 : iIndex;
            };
        };

        //  Check this unit axis of box 0 against the three axis of box 1.  If there is a match then we mark this axis to
        // be ignored during the cross product phase.

        if (
            F(tgCM_NR1)( V(F_DOT_VV)( psBX1->m.m.avAxis + 0, &vAxisUnit0 ) ) ||
            F(tgCM_NR1)( V(F_DOT_VV)( psBX1->m.m.avAxis + 1, &vAxisUnit0 ) ) ||
            F(tgCM_NR1)( V(F_DOT_VV)( psBX1->m.m.avAxis + 2, &vAxisUnit0 ) )
        ) {
            bUseAxis[iIndex] = TgFALSE;
        };

        // Determine the extents of the primitives along the chosen axis.

        {
            V(C_TgVEC)                          vAxisUnit1 = psBX1->m.m.avAxis[iIndex];
            const TYPE                          fB1_AU = V(F_DOT_VV)( &psBX1->m.m.vOrigin, &vAxisUnit1 );

            V(tgGM_Project_BX)( &fMinBox0, &fMaxBox0, psBX0, &vAxisUnit1 );
            fMinBox1 = fB1_AU - psBX1->m_vExtent.m_aData[iIndex];
            fMaxBox1 = fB1_AU + psBX1->m_vExtent.m_aData[iIndex];

            if (fMaxBox1 < fMinBox0 || fMinBox1 > fMaxBox0) //« Separation Test.
            {
                return (TgFALSE);
            }
            else
            {   // Selection of the best (minimal depth) axis.
                const TYPE                          fMinDepth = fMaxBox1 - fMinBox0;
                const TYPE                          fMaxDepth = fMaxBox0 - fMinBox1;
                C_TgBOOL                            bNegAxis = fMinDepth > fMaxDepth;

                if ((bNegAxis ? fMaxDepth : fMinDepth) < psAxS->m_fDepth)
                {
                    psAxS->m_vNormal = bNegAxis ? vAxisUnit1 : V(F_NEG)( &vAxisUnit1 );
                    psAxS->m_fDepth = bNegAxis ? fMaxDepth : fMinDepth;
                    psAxS->m_iAxis = bNegAxis ? iIndex + 9 : iIndex + 6;
                };
            };
        };
    };

    // -- Axis: Axis-Box Cross Product -------------------------

    for (iBox0 = 0; iBox0 < 3; ++iBox0)
    {
        if (!bUseAxis[iBox0])
        {
            continue;
        };

        for (iBox1 = 0; iBox1 < 3; ++iBox1)
        {
            // Axis is created by taking the cross product of the triangle edge and a box axis.
            TYPE                                fAxis;
            V(C_TgVEC)                          vAxis = V(F_UCX_LEN)( &fAxis, psBX1->m.m.avAxis + iBox1, psBX0->m.m.avAxis + iBox0 );

            if (F(tgCM_NR0)( fAxis )) //« Sanity/Parallel check for the resulting vector.
            {
                continue;
            };

            V(tgGM_Project_BX)( &fMinBox0, &fMaxBox0, psBX0, &vAxis );
            V(tgGM_Project_BX)( &fMinBox1, &fMaxBox1, psBX1, &vAxis );

            if (fMaxBox1 < fMinBox0 || fMinBox1 > fMaxBox0) //« Separation Test.
            {
                return (TgFALSE);
            }
            else
            {   // Selection of the best (minimal depth) axis.
                const TYPE                          fMinDepth = fMaxBox1 - fMinBox0;
                const TYPE                          fMaxDepth = fMaxBox0 - fMinBox1;
                C_TgBOOL                            bNegAxis = fMinDepth > fMaxDepth;

                if ((bNegAxis ? fMaxDepth : fMinDepth) < psAxS->m_fDepth)
                {
                    psAxS->m_vNormal = bNegAxis ? vAxis : V(F_NEG)( &vAxis );
                    psAxS->m_fDepth = bNegAxis ? fMaxDepth : fMinDepth;
                    psAxS->m_iAxis = iBox0*3 + iBox1 + (bNegAxis ? 21 : 12);
                };
            };
        };
    };

    return (TgTRUE);
}


// ---- tgCO_F_Penetrate_Box_Axis_BX_BX ----------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
TgRESULT V(tgCO_F_Penetrate_Box_Axis_BX_BX)(
    V(PCU_STg2_CO_Packet) psPacket, V(PCU_STg2_CO_Axis_Result) psAxS, V(CPCU_TgBOX) psBX0, V(CPCU_TgBOX) psBX1 )
{
    V(STg2_CO_Axis_Project)             sConfig;

    V(tgCO_F_Axis_ProjInfo_BX)( &sConfig, &psAxS->m_vNormal, psBX0 );

    switch (sConfig.m_iMinID) {
    case 1: { //« Vertex-Face Contact

        return (TgS_OK);
    };
    case 2: { //« Edge-Face Contact

        return (TgS_OK);
    };
    case 4: { //« Face-Face Contact

        return (TgS_OK);
    };
    default:
        TgS_NO_DEFAULT(return (TgE_FAIL));
    };
}