Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Collision - F - Rectangle-Triangle.c_inc
    »Keywords«  Collision;Distance;Closest;Intersect;Penetrate;Sweep;Rectangle;Triangle;
    »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 ========================================================================================================================================================= */

/* ---- V(tgCO_F_RT_ParamSq_ST) ----------------------------------------------------------------------------------------------------------------------------------------- */
/* Input:  psRT0: Rectangle primitive                                                                                                                                     */
/* Input:  psST0: Space Triangle primitive                                                                                                                                */
/* Output: _fRT0, _fRT1: Parametric parameters to generate point of minimal distance on the rectangle                                                                     */
/* Output: _fST0, _fST1: Parametric parameters to generate point of minimal distance on the triangle                                                                      */
/* Return: Minimal distance between the two primitives or negative type max if they intersect or are invalid.                                                             */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TYPE V(tgCO_F_RT_ParamSq_ST)( TYPE *pfRT0, TYPE *pfRT1, TYPE *pfST0, TYPE *pfST1, V(CPC_TgRECTANGLE) psRT0, V(CPC_TgSTRI) psST0 )
{
    TYPE                                fDistSq, fRT0, fRT1, fET0, fET1, fTest, fT0, fT1, fG1;
    V(TgVEC)                            vT0;

    /* == Test Rectangle 0 Edges against Triangle 1 == */

    fDistSq = V(tgCO_FI_ST_ParamSq_LR11)(&fET0, &fET1, &fRT0, psST0, &psRT0->m_vOrigin, psRT0->m_avEdge + 0);
    fRT1 = MKL(0.0);

    vT0 = V(tgGM_RT_Query_Point_2)(psRT0);
    fTest = V(tgCO_FI_ST_ParamSq_LR11)(&fT0, &fT1, &fG1, psST0, &vT0, psRT0->m_avEdge + 0);
    if (fTest < fDistSq)
    {
        fDistSq = fTest;
        fRT0 = fG1;
        fRT1 = MKL(1.0);
        fET0 = fT0;
        fET1 = fT1;
    };

    fTest = V(tgCO_FI_ST_ParamSq_LR11)(&fT0, &fT1, &fG1, psST0, &psRT0->m_vOrigin, psRT0->m_avEdge + 1);
    if (fTest < fDistSq)
    {
        fDistSq = fTest;
        fRT0 = MKL(0.0);
        fRT1 = fG1;
        fET0 = fT0;
        fET1 = fT1;
    };

    vT0 = V(tgGM_RT_Query_Point_1)(psRT0);
    fTest = V(tgCO_FI_ST_ParamSq_LR11)(&fT0, &fT1, &fG1, psST0, &vT0, psRT0->m_avEdge + 1);
    if (fTest < fDistSq)
    {
        fDistSq = fTest;
        fRT0 = MKL(1.0);
        fRT1 = fG1;
        fET0 = fT0;
        fET1 = fT1;
    };

    /* == Test Triangle 1 Edges against Rectangle 0 == */

    fTest = V(tgCO_FI_RT_ParamSq_LR11)( &fT0, &fT1, &fG1, psRT0, psST0->m_sCT.m_sET.m_sPT.m_avPoint + 0, psST0->m_sCT.m_sET.m_avEdge + 0);
    if (fTest < fDistSq)
    {
        fDistSq = fTest;
        fRT0 = fT0;
        fRT1 = fT1;
        fET0 = fG1;
        fET1 = MKL(0.0);
    };

    fTest = V(tgCO_FI_RT_ParamSq_LR11)( &fT0, &fT1, &fG1, psRT0, psST0->m_sCT.m_sET.m_sPT.m_avPoint + 1, psST0->m_sCT.m_sET.m_avEdge + 1);
    if (fTest < fDistSq)
    {
        fDistSq = fTest;
        fRT0 = fT0;
        fRT1 = fT1;
        fET0 = MKL(1.0) - fG1;
        fET1 = fG1;
    };

    fTest = V(tgCO_FI_RT_ParamSq_LR11)( &fT0, &fT1, &fG1, psRT0, psST0->m_sCT.m_sET.m_sPT.m_avPoint + 2, psST0->m_sCT.m_sET.m_avEdge + 2);
    if (fTest < fDistSq)
    {
        fDistSq = fTest;
        fRT0 = fT0;
        fRT1 = fT1;
        fET0 = MKL(0.0);
        fET1 = MKL(1.0) - fG1;
    };

    /* == Return Values == */

    *pfRT0 = fRT0;
    *pfRT1 = fRT1;
    *pfST0 = fET0;
    *pfST1 = fET1;

    return (fDistSq);
}