35 template <
bool LastPo
intPartOfClosestFeature>
36 bool GetClosest(
float inPrevVLenSq,
Vec3 &outV,
float &outVLenSq,
uint32 &outSet)
const
39 for (
int i = 0; i < mNumPoints; ++i)
40 Trace(
"y[%d] = [%s], |y[%d]| = %g", i,
ConvertToString(mY[i]).c_str(), i, (
double)mY[i].Length());
79 if (v_len_sq < inPrevVLenSq)
90 Trace(
"New closer point is further away, failed to converge");
96 float GetMaxYLengthSq()
const
99 for (
int i = 1; i < mNumPoints; ++i)
100 y_len_sq = max(y_len_sq, mY[i].LengthSq());
105 void UpdatePointSetY(
uint32 inSet)
108 for (
int i = 0; i < mNumPoints; ++i)
109 if ((inSet & (1 << i)) != 0)
111 mY[num_points] = mY[i];
114 mNumPoints = num_points;
118 void UpdatePointSetP(
uint32 inSet)
121 for (
int i = 0; i < mNumPoints; ++i)
122 if ((inSet & (1 << i)) != 0)
124 mP[num_points] = mP[i];
127 mNumPoints = num_points;
131 void UpdatePointSetPQ(
uint32 inSet)
134 for (
int i = 0; i < mNumPoints; ++i)
135 if ((inSet & (1 << i)) != 0)
137 mP[num_points] = mP[i];
138 mQ[num_points] = mQ[i];
141 mNumPoints = num_points;
145 void UpdatePointSetYPQ(
uint32 inSet)
148 for (
int i = 0; i < mNumPoints; ++i)
149 if ((inSet & (1 << i)) != 0)
151 mY[num_points] = mY[i];
152 mP[num_points] = mP[i];
153 mQ[num_points] = mQ[i];
156 mNumPoints = num_points;
160 void CalculatePointAAndB(
Vec3 &outPointA,
Vec3 &outPointB)
const
173 outPointA = u * mP[0] + v * mP[1];
174 outPointB = u * mQ[0] + v * mQ[1];
182 outPointA = u * mP[0] + v * mP[1] + w * mP[2];
183 outPointB = u * mQ[0] + v * mQ[1] + w * mQ[2];
189 memset(&outPointA, 0xcd,
sizeof(outPointA));
190 memset(&outPointB, 0xcd,
sizeof(outPointB));
206 template <
typename A,
typename B>
209 float tolerance_sq =
Square(inTolerance);
215 for (
int i = 0; i < 4; ++i)
220 float prev_v_len_sq = FLT_MAX;
229 Vec3 p = inA.GetSupport(ioV);
230 Vec3 q = inB.GetSupport(-ioV);
236 if (ioV.
Dot(w) < 0.0f)
240 Trace(
"Separating axis");
256 if (!GetClosest<true>(prev_v_len_sq, ioV, v_len_sq, set))
263 Trace(
"Full simplex");
270 if (v_len_sq <= tolerance_sq)
273 Trace(
"Distance zero");
280 if (v_len_sq <= FLT_EPSILON * GetMaxYLengthSq())
283 Trace(
"Machine precision reached");
295 if (prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq)
303 prev_v_len_sq = v_len_sq;
306 UpdatePointSetY(set);
326 template <
typename A,
typename B>
329 float tolerance_sq =
Square(inTolerance);
339 for (
int i = 0; i < 4; ++i)
351 float prev_v_len_sq = FLT_MAX;
360 Vec3 p = inA.GetSupport(ioV);
361 Vec3 q = inB.GetSupport(-ioV);
366 float dot = ioV.
Dot(w);
384 if (dot < 0.0f && dot * dot > v_len_sq * inMaxDistSq)
387 Trace(
"Distance bigger than max");
390 memset(&outPointA, 0xcd,
sizeof(outPointA));
391 memset(&outPointB, 0xcd,
sizeof(outPointB));
407 if (!GetClosest<true>(prev_v_len_sq, ioV, v_len_sq, set))
417 Trace(
"Full simplex");
425 UpdatePointSetYPQ(set);
428 if (v_len_sq <= tolerance_sq)
431 Trace(
"Distance zero");
440 Trace(
"Check v small compared to y: %g <= %g", (
double)v_len_sq, (
double)(FLT_EPSILON * GetMaxYLengthSq()));
442 if (v_len_sq <= FLT_EPSILON * GetMaxYLengthSq())
445 Trace(
"Machine precision reached");
458 Trace(
"Check v not changing enough: %g <= %g", (
double)(prev_v_len_sq - v_len_sq), (
double)(FLT_EPSILON * prev_v_len_sq));
461 if (prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq)
469 prev_v_len_sq = v_len_sq;
473 CalculatePointAAndB(outPointA, outPointB);
497 uint size =
sizeof(
Vec3) * mNumPoints;
498 memcpy(outY, mY, size);
499 memcpy(outP, mP, size);
500 memcpy(outQ, mQ, size);
501 outNumPoints = mNumPoints;
515 template <
typename A>
518 float tolerance_sq =
Square(inTolerance);
524 Vec3 x = inRayOrigin;
526 float v_len_sq = FLT_MAX;
527 bool allow_restart =
false;
536 Vec3 p = inA.GetSupport(v);
543 float v_dot_w = v.
Dot(w);
545 Trace(
"v . w = %g", (
double)v_dot_w);
550 float v_dot_r = v.
Dot(inRayDirection);
552 Trace(
"v . r = %g", (
double)v_dot_r);
558 float delta = v_dot_w / v_dot_r;
559 float old_lambda = lambda;
562 Trace(
"lambda = %g, delta = %g", (
double)lambda, (
double)delta);
566 if (old_lambda == lambda)
570 if (lambda >= ioLambda)
574 x = inRayOrigin + lambda * inRayDirection;
582 allow_restart =
true;
590 for (
int i = 0; i < mNumPoints; ++i)
595 if (!GetClosest<false>(v_len_sq, v, v_len_sq, set))
598 Trace(
"Failed to converge");
610 allow_restart =
false;
620 Trace(
"Full simplex");
630 UpdatePointSetP(set);
633 if (v_len_sq <= tolerance_sq)
657 template <
typename A,
typename B>
688 template <
typename A,
typename B>
689 bool CastShape(
Mat44Arg inStart,
Vec3Arg inDirection,
float inTolerance,
const A &inA,
const B &inB,
float inConvexRadiusA,
float inConvexRadiusB,
float &ioLambda,
Vec3 &outPointA,
Vec3 &outPointB,
Vec3 &outSeparatingAxis)
691 float tolerance_sq =
Square(inTolerance);
694 float sum_convex_radius = inConvexRadiusA + inConvexRadiusB;
705 float v_len_sq = FLT_MAX;
706 bool allow_restart =
false;
722 Vec3 q = inB.GetSupport(v);
723 Vec3 w = x - (q - p);
736 float v_dot_w = v.
Dot(w) - sum_convex_radius * v.
Length();
738 Trace(
"v . w = %g", (
double)v_dot_w);
743 float v_dot_r = v.
Dot(inDirection);
745 Trace(
"v . r = %g", (
double)v_dot_r);
751 float delta = v_dot_w / v_dot_r;
752 float old_lambda = lambda;
755 Trace(
"lambda = %g, delta = %g", (
double)lambda, (
double)delta);
759 if (old_lambda == lambda)
763 if (lambda >= ioLambda)
767 x = lambda * inDirection;
774 tolerance_sq =
Square(inTolerance + sum_convex_radius);
779 allow_restart =
true;
788 for (
int i = 0; i < mNumPoints; ++i)
789 mY[i] = x - (mQ[i] - mP[i]);
793 if (!GetClosest<false>(v_len_sq, v, v_len_sq, set))
796 Trace(
"Failed to converge");
808 allow_restart =
false;
819 Trace(
"Full simplex");
829 UpdatePointSetPQ(set);
832 if (v_len_sq <= tolerance_sq)
845 for (
int i = 0; i < mNumPoints; ++i)
846 mY[i] = x - (mQ[i] - mP[i]);
850 Vec3 convex_radius_a = inConvexRadiusA * normalized_v;
851 Vec3 convex_radius_b = inConvexRadiusB * normalized_v;
858 outPointB = mQ[0] + convex_radius_b;
859 outPointA = lambda > 0.0f? outPointB : mP[0] - convex_radius_a;
866 outPointB = bu * mQ[0] + bv * mQ[1] + convex_radius_b;
867 outPointA = lambda > 0.0f? outPointB : bu * mP[0] + bv * mP[1] - convex_radius_a;
876 outPointB = bu * mQ[0] + bv * mQ[1] + bw * mQ[2] + convex_radius_b;
877 outPointA = lambda > 0.0f? outPointB : bu * mP[0] + bv * mP[1] + bw * mP[2] - convex_radius_a;
884 outSeparatingAxis = sum_convex_radius > 0.0f? -v : -prev_v;
905 for (
int i = 0; i < mNumPoints; ++i)
908 RVec3 y_i = origin * mY[i];
910 for (
int j = i + 1; j < mNumPoints; ++j)
913 RVec3 y_j = origin * mY[j];
915 for (
int k = j + 1; k < mNumPoints; ++k)
918 RVec3 y_k = origin * mY[k];
919 RVec3 center = (y_i + y_j + y_k) /
Real(3);
920 RVec3 normal = (y_j - y_i).Cross(y_k - y_i);
921 if (normal.
Dot(center) <
Real(0))
930 mOffset +=
Vec3(mGeometry->mBounds.GetSize().GetX() + 2.0f, 0, 0);
unsigned int uint
Definition Core.h:485
#define JPH_NAMESPACE_END
Definition Core.h:418
std::uint32_t uint32
Definition Core.h:488
#define JPH_NAMESPACE_BEGIN
Definition Core.h:412
TraceFunction Trace
Definition IssueReporting.cpp:14
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition Math.h:55
float Real
Definition Real.h:27
static const Color sPurple
Definition Color.h:61
static const Color sGreen
Definition Color.h:57
static const Color sLightGrey
Definition Color.h:66
static const Color sOrange
Definition Color.h:63
static const Color sCyan
Definition Color.h:62
static const Color sRed
Definition Color.h:55
static const Color sYellow
Definition Color.h:60
GeometryRef CreateTriangleGeometryForConvex(SupportFunction inGetSupport)
Definition DebugRenderer.cpp:698
void DrawMarker(RVec3Arg inPosition, ColorArg inColor, float inSize)
Draw a marker on a position.
Definition DebugRenderer.cpp:172
void DrawCoordinateSystem(RMat44Arg inTransform, float inSize=1.0f)
Draw coordinate system (3 arrows, x = red, y = green, z = blue)
Definition DebugRenderer.cpp:206
virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode=ECullMode::CullBackFace, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)=0
static DebugRenderer * sInstance
Singleton instance.
Definition DebugRenderer.h:179
virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor)=0
Draw line.
void DrawArrow(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor, float inSize)
Draw an arrow.
Definition DebugRenderer.cpp:184
virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::Off)=0
Draw a single back face culled triangle.
Definition GJKClosestPoint.h:22
bool Intersects(const A &inA, const B &inB, float inTolerance, Vec3 &ioV)
Definition GJKClosestPoint.h:207
bool CastShape(Mat44Arg inStart, Vec3Arg inDirection, float inTolerance, const A &inA, const B &inB, float &ioLambda)
Definition GJKClosestPoint.h:658
void GetClosestPointsSimplex(Vec3 *outY, Vec3 *outP, Vec3 *outQ, uint &outNumPoints) const
Definition GJKClosestPoint.h:495
bool CastShape(Mat44Arg inStart, Vec3Arg inDirection, float inTolerance, const A &inA, const B &inB, float inConvexRadiusA, float inConvexRadiusB, float &ioLambda, Vec3 &outPointA, Vec3 &outPointB, Vec3 &outSeparatingAxis)
Definition GJKClosestPoint.h:689
float GetClosestPoints(const A &inA, const B &inB, float inTolerance, float inMaxDistSq, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)
Definition GJKClosestPoint.h:327
bool CastRay(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inTolerance, const A &inA, float &ioLambda)
Definition GJKClosestPoint.h:516
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
static JPH_INLINE Mat44 sTranslation(Vec3Arg inV)
Get matrix that translates.
Definition Mat44.inl:144
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:650
JPH_INLINE float Length() const
Length of vector.
Definition Vec3.inl:682
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition Vec3.inl:721
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:666
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:103
bool GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, float &outU, float &outV)
Definition ClosestPoint.h:18
Vec3 GetClosestPointOnTriangle(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, uint32 &outSet)
Definition ClosestPoint.h:160
Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
Definition ClosestPoint.h:413
Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
Definition ClosestPoint.h:132
Structure that performs a Minkowski difference A - B.
Definition ConvexSupport.h:67
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition ConvexSupport.h:75