36 template <
bool LastPo
intPartOfClosestFeature>
37 bool GetClosest(
float inPrevVLenSq,
Vec3 &outV,
float &outVLenSq,
uint32 &outSet)
const
40 for (
int i = 0; i < mNumPoints; ++i)
41 Trace(
"y[%d] = [%s], |y[%d]| = %g", i,
ConvertToString(mY[i]).c_str(), i, (
double)mY[i].Length());
62 v = ClosestPoint::GetClosestPointOnTriangle<LastPointPartOfClosestFeature>(mY[0], mY[1], mY[2], set);
67 v = ClosestPoint::GetClosestPointOnTetrahedron<LastPointPartOfClosestFeature>(mY[0], mY[1], mY[2], mY[3], set);
80 if (v_len_sq < inPrevVLenSq)
91 Trace(
"New closer point is further away, failed to converge");
97 float GetMaxYLengthSq()
const
100 for (
int i = 1; i < mNumPoints; ++i)
101 y_len_sq = max(y_len_sq, mY[i].LengthSq());
106 void UpdatePointSetY(
uint32 inSet)
109 for (
int i = 0; i < mNumPoints; ++i)
110 if ((inSet & (1 << i)) != 0)
112 mY[num_points] = mY[i];
115 mNumPoints = num_points;
119 void UpdatePointSetP(
uint32 inSet)
122 for (
int i = 0; i < mNumPoints; ++i)
123 if ((inSet & (1 << i)) != 0)
125 mP[num_points] = mP[i];
128 mNumPoints = num_points;
132 void UpdatePointSetPQ(
uint32 inSet)
135 for (
int i = 0; i < mNumPoints; ++i)
136 if ((inSet & (1 << i)) != 0)
138 mP[num_points] = mP[i];
139 mQ[num_points] = mQ[i];
142 mNumPoints = num_points;
146 void UpdatePointSetYPQ(
uint32 inSet)
149 for (
int i = 0; i < mNumPoints; ++i)
150 if ((inSet & (1 << i)) != 0)
152 mY[num_points] = mY[i];
153 mP[num_points] = mP[i];
154 mQ[num_points] = mQ[i];
157 mNumPoints = num_points;
161 void CalculatePointAAndB(
Vec3 &outPointA,
Vec3 &outPointB)
const
174 outPointA = u * mP[0] + v * mP[1];
175 outPointB = u * mQ[0] + v * mQ[1];
183 outPointA = u * mP[0] + v * mP[1] + w * mP[2];
184 outPointB = u * mQ[0] + v * mQ[1] + w * mQ[2];
190 memset(&outPointA, 0xcd,
sizeof(outPointA));
191 memset(&outPointB, 0xcd,
sizeof(outPointB));
207 template <
typename A,
typename B>
210 float tolerance_sq =
Square(inTolerance);
216 for (
int i = 0; i < 4; ++i)
221 float prev_v_len_sq = FLT_MAX;
230 Vec3 p = inA.GetSupport(ioV);
231 Vec3 q = inB.GetSupport(-ioV);
237 if (ioV.
Dot(w) < 0.0f)
241 Trace(
"Separating axis");
257 if (!GetClosest<true>(prev_v_len_sq, ioV, v_len_sq, set))
264 Trace(
"Full simplex");
271 if (v_len_sq <= tolerance_sq)
274 Trace(
"Distance zero");
281 if (v_len_sq <= FLT_EPSILON * GetMaxYLengthSq())
284 Trace(
"Machine precision reached");
296 if (prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq)
304 prev_v_len_sq = v_len_sq;
307 UpdatePointSetY(set);
327 template <
typename A,
typename B>
330 float tolerance_sq =
Square(inTolerance);
340 for (
int i = 0; i < 4; ++i)
352 float prev_v_len_sq = FLT_MAX;
361 Vec3 p = inA.GetSupport(ioV);
362 Vec3 q = inB.GetSupport(-ioV);
367 float dot = ioV.
Dot(w);
385 if (dot < 0.0f && dot * dot > v_len_sq * inMaxDistSq)
388 Trace(
"Distance bigger than max");
391 memset(&outPointA, 0xcd,
sizeof(outPointA));
392 memset(&outPointB, 0xcd,
sizeof(outPointB));
408 if (!GetClosest<true>(prev_v_len_sq, ioV, v_len_sq, set))
418 Trace(
"Full simplex");
426 UpdatePointSetYPQ(set);
429 if (v_len_sq <= tolerance_sq)
432 Trace(
"Distance zero");
441 Trace(
"Check v small compared to y: %g <= %g", (
double)v_len_sq, (
double)(FLT_EPSILON * GetMaxYLengthSq()));
443 if (v_len_sq <= FLT_EPSILON * GetMaxYLengthSq())
446 Trace(
"Machine precision reached");
459 Trace(
"Check v not changing enough: %g <= %g", (
double)(prev_v_len_sq - v_len_sq), (
double)(FLT_EPSILON * prev_v_len_sq));
462 if (prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq)
470 prev_v_len_sq = v_len_sq;
474 CalculatePointAAndB(outPointA, outPointB);
498 uint size =
sizeof(
Vec3) * mNumPoints;
499 memcpy(outY, mY, size);
500 memcpy(outP, mP, size);
501 memcpy(outQ, mQ, size);
502 outNumPoints = mNumPoints;
516 template <
typename A>
519 float tolerance_sq =
Square(inTolerance);
525 Vec3 x = inRayOrigin;
527 float v_len_sq = FLT_MAX;
528 bool allow_restart =
false;
537 Vec3 p = inA.GetSupport(v);
544 float v_dot_w = v.
Dot(w);
546 Trace(
"v . w = %g", (
double)v_dot_w);
551 float v_dot_r = v.
Dot(inRayDirection);
553 Trace(
"v . r = %g", (
double)v_dot_r);
559 float delta = v_dot_w / v_dot_r;
560 float old_lambda = lambda;
563 Trace(
"lambda = %g, delta = %g", (
double)lambda, (
double)delta);
567 if (old_lambda == lambda)
571 if (lambda >= ioLambda)
575 x = inRayOrigin + lambda * inRayDirection;
583 allow_restart =
true;
591 for (
int i = 0; i < mNumPoints; ++i)
596 if (!GetClosest<false>(v_len_sq, v, v_len_sq, set))
599 Trace(
"Failed to converge");
611 allow_restart =
false;
621 Trace(
"Full simplex");
631 UpdatePointSetP(set);
634 if (v_len_sq <= tolerance_sq)
658 template <
typename A,
typename B>
689 template <
typename A,
typename B>
690 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)
692 float tolerance_sq =
Square(inTolerance);
695 float sum_convex_radius = inConvexRadiusA + inConvexRadiusB;
706 float v_len_sq = FLT_MAX;
707 bool allow_restart =
false;
723 Vec3 q = inB.GetSupport(v);
724 Vec3 w = x - (q - p);
737 float v_dot_w = v.
Dot(w) - sum_convex_radius * v.
Length();
739 Trace(
"v . w = %g", (
double)v_dot_w);
744 float v_dot_r = v.
Dot(inDirection);
746 Trace(
"v . r = %g", (
double)v_dot_r);
752 float delta = v_dot_w / v_dot_r;
753 float old_lambda = lambda;
756 Trace(
"lambda = %g, delta = %g", (
double)lambda, (
double)delta);
760 if (old_lambda == lambda)
764 if (lambda >= ioLambda)
768 x = lambda * inDirection;
775 tolerance_sq =
Square(inTolerance + sum_convex_radius);
780 allow_restart =
true;
789 for (
int i = 0; i < mNumPoints; ++i)
790 mY[i] = x - (mQ[i] - mP[i]);
794 if (!GetClosest<false>(v_len_sq, v, v_len_sq, set))
797 Trace(
"Failed to converge");
809 allow_restart =
false;
820 Trace(
"Full simplex");
830 UpdatePointSetPQ(set);
833 if (v_len_sq <= tolerance_sq)
846 for (
int i = 0; i < mNumPoints; ++i)
847 mY[i] = x - (mQ[i] - mP[i]);
851 Vec3 convex_radius_a = inConvexRadiusA * normalized_v;
852 Vec3 convex_radius_b = inConvexRadiusB * normalized_v;
859 outPointB = mQ[0] + convex_radius_b;
860 outPointA = lambda > 0.0f? outPointB : mP[0] - convex_radius_a;
867 outPointB = bu * mQ[0] + bv * mQ[1] + convex_radius_b;
868 outPointA = lambda > 0.0f? outPointB : bu * mP[0] + bv * mP[1] - convex_radius_a;
877 outPointB = bu * mQ[0] + bv * mQ[1] + bw * mQ[2] + convex_radius_b;
878 outPointA = lambda > 0.0f? outPointB : bu * mP[0] + bv * mP[1] + bw * mP[2] - convex_radius_a;
885 outSeparatingAxis = sum_convex_radius > 0.0f? -v : -prev_v;
906 for (
int i = 0; i < mNumPoints; ++i)
909 RVec3 y_i = origin * mY[i];
911 for (
int j = i + 1; j < mNumPoints; ++j)
914 RVec3 y_j = origin * mY[j];
916 for (
int k = j + 1; k < mNumPoints; ++k)
919 RVec3 y_k = origin * mY[k];
920 RVec3 center = (y_i + y_j + y_k) /
Real(3);
921 RVec3 normal = (y_j - y_i).Cross(y_k - y_i);
922 if (normal.
Dot(center) <
Real(0))
931 mOffset +=
Vec3(mGeometry->mBounds.GetSize().GetX() + 2.0f, 0, 0);
unsigned int uint
Definition Core.h:453
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:456
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
TraceFunction Trace
Definition IssueReporting.cpp:18
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition Math.h:52
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:23
bool Intersects(const A &inA, const B &inB, float inTolerance, Vec3 &ioV)
Definition GJKClosestPoint.h:208
bool CastShape(Mat44Arg inStart, Vec3Arg inDirection, float inTolerance, const A &inA, const B &inB, float &ioLambda)
Definition GJKClosestPoint.h:659
void GetClosestPointsSimplex(Vec3 *outY, Vec3 *outP, Vec3 *outQ, uint &outNumPoints) const
Definition GJKClosestPoint.h:496
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:690
float GetClosestPoints(const A &inA, const B &inB, float inTolerance, float inMaxDistSq, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)
Definition GJKClosestPoint.h:328
bool CastRay(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inTolerance, const A &inA, float &ioLambda)
Definition GJKClosestPoint.h:517
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:649
JPH_INLINE float Length() const
Length of vector.
Definition Vec3.inl:681
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition Vec3.inl:720
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:665
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:107
bool GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, float &outU, float &outV)
Definition ClosestPoint.h:18
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