/*
Copyright (C) 2006, 2008 Sony Computer Entertainment Inc.
All rights reserved.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
//#include "PfxContactBoxBox.h"
#include <math.h>
#include "../PlatformDefinitions.h"
#include "boxBoxDistance.h"
static inline float sqr( float a )
{
return (a * a);
}
enum BoxSepAxisType
{
A_AXIS, B_AXIS, CROSS_AXIS
};
//-------------------------------------------------------------------------------------------------
// voronoiTol: bevels Voronoi planes slightly which helps when features are parallel.
//-------------------------------------------------------------------------------------------------
static const float voronoiTol = -1.0e-5f;
//-------------------------------------------------------------------------------------------------
// separating axis tests: gaps along each axis are computed, and the axis with the maximum
// gap is stored. cross product axes are normalized.
//-------------------------------------------------------------------------------------------------
#define AaxisTest( dim, letter, first ) \
{ \
if ( first ) \
{ \
maxGap = gap = gapsA.get##letter(); \
if ( gap > distanceThreshold ) return gap; \
axisType = A_AXIS; \
faceDimA = dim; \
axisA = identity.getCol##dim(); \
} \
else \
{ \
gap = gapsA.get##letter(); \
if ( gap > distanceThreshold ) return gap; \
else if ( gap > maxGap ) \
{ \
maxGap = gap; \
axisType = A_AXIS; \
faceDimA = dim; \
axisA = identity.getCol##dim(); \
} \
} \
}
#define BaxisTest( dim, letter ) \
{ \
gap = gapsB.get##letter(); \
if ( gap > distanceThreshold ) return gap; \
else if ( gap > maxGap ) \
{ \
maxGap = gap; \
axisType = B_AXIS; \
faceDimB = dim; \
axisB = identity.getCol##dim(); \
} \
}
#define CrossAxisTest( dima, dimb, letterb ) \
{ \
const float lsqr_tolerance = 1.0e-30f; \
float lsqr; \
\
lsqr = lsqrs.getCol##dima().get##letterb(); \
\
if ( lsqr > lsqr_tolerance ) \
{ \
float l_recip = 1.0f / sqrtf( lsqr ); \
gap = float(gapsAxB.getCol##dima().get##letterb()) * l_recip; \
\
if ( gap > distanceThreshold ) \
{ \
return gap; \
} \
\
if ( gap > maxGap ) \
{ \
maxGap = gap; \
axisType = CROSS_AXIS; \
edgeDimA = dima; \
edgeDimB = dimb; \
axisA = cross(identity.getCol##dima(),matrixAB.getCol##dimb()) * l_recip; \
} \
} \
}
//-------------------------------------------------------------------------------------------------
// tests whether a vertex of box B and a face of box A are the closest features
//-------------------------------------------------------------------------------------------------
inline
float
VertexBFaceATest(
bool & inVoronoi,
float & t0,
float & t1,
const vmVector3 & hA,
PE_REF(vmVector3) faceOffsetAB,
PE_REF(vmVector3) faceOffsetBA,
const vmMatrix3 & matrixAB,
const vmMatrix3 & matrixBA,
PE_REF(vmVector3) signsB,
PE_REF(vmVector3) scalesB )
{
// compute a corner of box B in A's coordinate system
vmVector3 corner =
vmVector3( faceOffsetAB + matrixAB.getCol0() * scalesB.getX() + matrixAB.getCol1() * scalesB.getY() );
// compute the parameters of the point on A, closest to this corner
t0 = corner[0];
t1 = corner[1];
if ( t0 > hA[0] )
t0 = hA[0];
else if ( t0 < -hA[0] )
t0 = -hA[0];
if ( t1 > hA[1] )
t1 =