29#ifndef GU_SWEEP_BOX_TRIANGLE_SAT_H
30#define GU_SWEEP_BOX_TRIANGLE_SAT_H
32#include "geometry/PxTriangle.h"
34#include "GuSweepSharedTests.h"
35#include "GuInternal.h"
53 const float d0 = -BoxExt - TriMax; \
54 const float d1 = BoxExt - TriMin; \
55 const bool bIntersect = (d0<=0.0f && d1>=0.0f); \
56 bValidMTD &= bIntersect;
59 static PX_FORCE_INLINE RetType testAxis(
const PxTriangle& tri,
const PxVec3& extents,
const PxVec3& dir,
const PxVec3& axis, MTDType& bValidMTD,
float& tfirst,
float& tlast)
61 const float d0t = tri.verts[0].dot(axis);
62 const float d1t = tri.verts[1].dot(axis);
63 const float d2t = tri.verts[2].dot(axis);
65 float TriMin =
PxMin(d0t, d1t);
66 float TriMax =
PxMax(d0t, d1t);
67 TriMin =
PxMin(TriMin, d2t);
68 TriMax =
PxMax(TriMax, d2t);
72 const float BoxExt =
PxAbs(axis.x)*extents.x +
PxAbs(axis.y)*extents.y +
PxAbs(axis.z)*extents.z;
75 const float v = dir.dot(axis);
76 if(
PxAbs(v) < 1.0E-6f)
78 const float oneOverV = -1.0f / v;
83 const float t0_ = d0 * oneOverV;
84 const float t1_ = d1 * oneOverV;
85 float t0 =
PxMin(t0_, t1_);
86 float t1 =
PxMax(t0_, t1_);
88 if(t0 > tlast)
return false;
89 if(t1 < tfirst)
return false;
92 tlast =
PxMin(t1, tlast);
95 tfirst =
PxMax(t0, tfirst);
100 template<const
int XYZ>
101 static PX_FORCE_INLINE RetType testAxisXYZ(
const PxTriangle& tri,
const PxVec3& extents,
const PxVec3& dir,
float oneOverDir, MTDType& bValidMTD,
float& tfirst,
float& tlast)
103 const float d0t = tri.verts[0][XYZ];
104 const float d1t = tri.verts[1][XYZ];
105 const float d2t = tri.verts[2][XYZ];
107 float TriMin =
PxMin(d0t, d1t);
108 float TriMax =
PxMax(d0t, d1t);
109 TriMin =
PxMin(TriMin, d2t);
110 TriMax =
PxMax(TriMax, d2t);
114 const float BoxExt = extents[XYZ];
117 const float v = dir[XYZ];
118 if(
PxAbs(v) < 1.0E-6f)
121 const float oneOverV = -oneOverDir;
126 const float t0_ = d0 * oneOverV;
127 const float t1_ = d1 * oneOverV;
128 float t0 =
PxMin(t0_, t1_);
129 float t1 =
PxMax(t0_, t1_);
131 if(t0 > tlast)
return false;
132 if(t1 < tfirst)
return false;
135 tlast =
PxMin(t1, tlast);
138 tfirst =
PxMax(t0, tfirst);
143 PX_FORCE_INLINE int testSeparationAxes(
const PxTriangle& tri,
const PxVec3& extents,
144 const PxVec3& normal,
const PxVec3& dir,
const PxVec3& oneOverDir,
float tmax,
float& tcoll)
146 bool bValidMTD =
true;
147 float tfirst = -FLT_MAX;
148 float tlast = FLT_MAX;
151 if(!testAxis(tri, extents, dir, normal, bValidMTD, tfirst, tlast))
155 if(!testAxisXYZ<0>(tri, extents, dir, oneOverDir.x, bValidMTD, tfirst, tlast))
157 if(!testAxisXYZ<1>(tri, extents, dir, oneOverDir.y, bValidMTD, tfirst, tlast))
159 if(!testAxisXYZ<2>(tri, extents, dir, oneOverDir.z, bValidMTD, tfirst, tlast))
163 for(PxU32 i=0; i<3; i++)
167 const PxVec3 TriEdge = tri.verts[ip1] - tri.verts[i];
170 const PxVec3 Sep = cross100(TriEdge);
171 if((Sep.dot(Sep))>=1.0E-6f && !testAxis(tri, extents, dir, Sep, bValidMTD, tfirst, tlast))
175 const PxVec3 Sep = cross010(TriEdge);
176 if((Sep.dot(Sep))>=1.0E-6f && !testAxis(tri, extents, dir, Sep, bValidMTD, tfirst, tlast))
180 const PxVec3 Sep = cross001(TriEdge);
181 if((Sep.dot(Sep))>=1.0E-6f && !testAxis(tri, extents, dir, Sep, bValidMTD, tfirst, tlast))
186 if(tfirst > tmax || tlast < 0.0f)
208 if(doBackfaceCulling && (triNormal.
dot(dir)) >= 0.0f)
212 return testSeparationAxes(tri, extents, triNormal, dir, oneOverDir, tmax, toi);
Triangle class.
Definition PxTriangle.h:47
PX_FORCE_INLINE void denormalizedNormal(PxVec3 &_normal) const
Compute the unnormalized normal of the triangle.
Definition PxTriangle.h:111
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3 &v) const
returns the scalar product of this and other.
Definition PxVec3.h:276
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
int triBoxSweepTestBoxSpace(const PxTriangle &tri, const PxVec3 &extents, const PxVec3 &dir, const PxVec3 &oneOverDir, float tmax, float &toi, bool doBackfaceCulling)
Definition GuSweepBoxTriangle_SAT.cpp:35
PX_FORCE_INLINE int triBoxSweepTestBoxSpace_inlined(const PxTriangle &tri, const PxVec3 &extents, const PxVec3 &dir, const PxVec3 &oneOverDir, float tmax, float &toi, PxU32 doBackfaceCulling)
Inlined version of triBoxSweepTestBoxSpace. See that other function for comments.
Definition GuSweepBoxTriangle_SAT.h:201
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition PxMath.h:72
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
The return value is the lesser of the two specified values.
Definition PxMath.h:88