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 JPH_SUPPRESS_WARNING_PUSH
123 void UpdatePointSetP(
uint32 inSet)
126 for (
int i = 0; i < mNumPoints; ++i)
127 if ((inSet & (1 << i)) != 0)
129 mP[num_points] = mP[i];
132 mNumPoints = num_points;
136 void UpdatePointSetPQ(
uint32 inSet)
139 for (
int i = 0; i < mNumPoints; ++i)
140 if ((inSet & (1 << i)) != 0)
142 mP[num_points] = mP[i];
143 mQ[num_points] = mQ[i];
146 mNumPoints = num_points;
150 void UpdatePointSetYPQ(
uint32 inSet)
153 for (
int i = 0; i < mNumPoints; ++i)
154 if ((inSet & (1 << i)) != 0)
156 mY[num_points] = mY[i];
157 mP[num_points] = mP[i];
158 mQ[num_points] = mQ[i];
161 mNumPoints = num_points;
164 JPH_SUPPRESS_WARNING_POP
167 void CalculatePointAAndB(
Vec3 &outPointA,
Vec3 &outPointB)
const
180 outPointA = u * mP[0] + v * mP[1];
181 outPointB = u * mQ[0] + v * mQ[1];
189 outPointA = u * mP[0] + v * mP[1] + w * mP[2];
190 outPointB = u * mQ[0] + v * mQ[1] + w * mQ[2];
196 memset(&outPointA, 0xcd,
sizeof(outPointA));
197 memset(&outPointB, 0xcd,
sizeof(outPointB));
213 template <
typename A,
typename B>
216 float tolerance_sq =
Square(inTolerance);
222 for (
int i = 0; i < 4; ++i)
227 float prev_v_len_sq = FLT_MAX;
236 Vec3 p = inA.GetSupport(ioV);
237 Vec3 q = inB.GetSupport(-ioV);
243 if (ioV.
Dot(w) < 0.0f)
247 Trace(
"Separating axis");
263 if (!GetClosest<true>(prev_v_len_sq, ioV, v_len_sq, set))
270 Trace(
"Full simplex");
277 if (v_len_sq <= tolerance_sq)
280 Trace(
"Distance zero");
287 if (v_len_sq <= FLT_EPSILON * GetMaxYLengthSq())
290 Trace(
"Machine precision reached");
302 if (prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq)
310 prev_v_len_sq = v_len_sq;
313 UpdatePointSetY(set);
333 template <
typename A,
typename B>
336 float tolerance_sq =
Square(inTolerance);
346 for (
int i = 0; i < 4; ++i)
358 float prev_v_len_sq = FLT_MAX;
367 Vec3 p = inA.GetSupport(ioV);
368 Vec3 q = inB.GetSupport(-ioV);
373 float dot = ioV.
Dot(w);
391 if (dot < 0.0f && dot * dot > v_len_sq * inMaxDistSq)
394 Trace(
"Distance bigger than max");
397 memset(&outPointA, 0xcd,
sizeof(outPointA));
398 memset(&outPointB, 0xcd,
sizeof(outPointB));
414 if (!GetClosest<true>(prev_v_len_sq, ioV, v_len_sq, set))
424 Trace(
"Full simplex");
432 UpdatePointSetYPQ(set);
435 if (v_len_sq <= tolerance_sq)
438 Trace(
"Distance zero");
447 Trace(
"Check v small compared to y: %g <= %g", (
double)v_len_sq, (
double)(FLT_EPSILON * GetMaxYLengthSq()));
449 if (v_len_sq <= FLT_EPSILON * GetMaxYLengthSq())
452 Trace(
"Machine precision reached");
465 Trace(
"Check v not changing enough: %g <= %g", (
double)(prev_v_len_sq - v_len_sq), (
double)(FLT_EPSILON * prev_v_len_sq));
468 if (prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq)
476 prev_v_len_sq = v_len_sq;
480 CalculatePointAAndB(outPointA, outPointB);
504 uint size =
sizeof(
Vec3) * mNumPoints;
505 memcpy(outY, mY, size);
506 memcpy(outP, mP, size);
507 memcpy(outQ, mQ, size);
508 outNumPoints = mNumPoints;
522 template <
typename A>
525 float tolerance_sq =
Square(inTolerance);
531 Vec3 x = inRayOrigin;
533 float v_len_sq = FLT_MAX;
534 bool allow_restart =
false;
543 Vec3 p = inA.GetSupport(v);
550 float v_dot_w = v.
Dot(w);
552 Trace(
"v . w = %g", (
double)v_dot_w);
557 float v_dot_r = v.
Dot(inRayDirection);
559 Trace(
"v . r = %g", (
double)v_dot_r);
565 float delta = v_dot_w / v_dot_r;
566 float old_lambda = lambda;
569 Trace(
"lambda = %g, delta = %g", (
double)lambda, (
double)delta);
573 if (old_lambda == lambda)
577 if (lambda >= ioLambda)
581 x = inRayOrigin + lambda * inRayDirection;
589 allow_restart =
true;
597 for (
int i = 0; i < mNumPoints; ++i)
602 if (!GetClosest<false>(v_len_sq, v, v_len_sq, set))
605 Trace(
"Failed to converge");
617 allow_restart =
false;
627 Trace(
"Full simplex");
637 UpdatePointSetP(set);
640 if (v_len_sq <= tolerance_sq)
664 template <
typename A,
typename B>
695 template <
typename A,
typename B>
696 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)
698 float tolerance_sq =
Square(inTolerance);
701 float sum_convex_radius = inConvexRadiusA + inConvexRadiusB;
712 float v_len_sq = FLT_MAX;
713 bool allow_restart =
false;
729 Vec3 q = inB.GetSupport(v);
730 Vec3 w = x - (q - p);
743 float v_dot_w = v.
Dot(w) - sum_convex_radius * v.
Length();
745 Trace(
"v . w = %g", (
double)v_dot_w);
750 float v_dot_r = v.
Dot(inDirection);
752 Trace(
"v . r = %g", (
double)v_dot_r);
758 float delta = v_dot_w / v_dot_r;
759 float old_lambda = lambda;
762 Trace(
"lambda = %g, delta = %g", (
double)lambda, (
double)delta);
766 if (old_lambda == lambda)
770 if (lambda >= ioLambda)
774 x = lambda * inDirection;
781 tolerance_sq =
Square(inTolerance + sum_convex_radius);
786 allow_restart =
true;
795 for (
int i = 0; i < mNumPoints; ++i)
796 mY[i] = x - (mQ[i] - mP[i]);
800 if (!GetClosest<false>(v_len_sq, v, v_len_sq, set))
803 Trace(
"Failed to converge");
815 allow_restart =
false;
826 Trace(
"Full simplex");
836 UpdatePointSetPQ(set);
839 if (v_len_sq <= tolerance_sq)
852 for (
int i = 0; i < mNumPoints; ++i)
853 mY[i] = x - (mQ[i] - mP[i]);
857 Vec3 convex_radius_a = inConvexRadiusA * normalized_v;
858 Vec3 convex_radius_b = inConvexRadiusB * normalized_v;
865 outPointB = mQ[0] + convex_radius_b;
866 outPointA = lambda > 0.0f? outPointB : mP[0] - convex_radius_a;
873 outPointB = bu * mQ[0] + bv * mQ[1] + convex_radius_b;
874 outPointA = lambda > 0.0f? outPointB : bu * mP[0] + bv * mP[1] - convex_radius_a;
883 outPointB = bu * mQ[0] + bv * mQ[1] + bw * mQ[2] + convex_radius_b;
884 outPointA = lambda > 0.0f? outPointB : bu * mP[0] + bv * mP[1] + bw * mP[2] - convex_radius_a;
891 outSeparatingAxis = sum_convex_radius > 0.0f? -v : -prev_v;
912 for (
int i = 0; i < mNumPoints; ++i)
915 RVec3 y_i = origin * mY[i];
917 for (
int j = i + 1; j < mNumPoints; ++j)
920 RVec3 y_j = origin * mY[j];
922 for (
int k = j + 1; k < mNumPoints; ++k)
925 RVec3 y_k = origin * mY[k];
926 RVec3 center = (y_i + y_j + y_k) /
Real(3);
927 RVec3 normal = (y_j - y_i).Cross(y_k - y_i);
928 if (normal.
Dot(center) <
Real(0))
937 mOffset +=
Vec3(mGeometry->mBounds.GetSize().GetX() + 2.0f, 0, 0);
#define JPH_GCC_SUPPRESS_WARNING(w)
Definition: Core.h:264
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
TraceFunction Trace
Definition: IssueReporting.cpp:18
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
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:58
static const Color sGreen
Definition: Color.h:54
static const Color sLightGrey
Definition: Color.h:63
static const Color sOrange
Definition: Color.h:60
static const Color sCyan
Definition: Color.h:59
static const Color sRed
Definition: Color.h:52
static const Color sYellow
Definition: Color.h:57
GeometryRef CreateTriangleGeometryForConvex(SupportFunction inGetSupport)
Definition: DebugRenderer.cpp:688
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:168
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:214
bool CastShape(Mat44Arg inStart, Vec3Arg inDirection, float inTolerance, const A &inA, const B &inB, float &ioLambda)
Definition: GJKClosestPoint.h:665
void GetClosestPointsSimplex(Vec3 *outY, Vec3 *outP, Vec3 *outQ, uint &outNumPoints) const
Definition: GJKClosestPoint.h:502
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:696
float GetClosestPoints(const A &inA, const B &inB, float inTolerance, float inMaxDistSq, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)
Definition: GJKClosestPoint.h:334
bool CastRay(Vec3Arg inRayOrigin, Vec3Arg inRayDirection, float inTolerance, const A &inA, float &ioLambda)
Definition: GJKClosestPoint.h:523
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:637
JPH_INLINE float Length() const
Length of vector.
Definition: Vec3.inl:669
JPH_INLINE Vec3 NormalizedOr(Vec3Arg inZeroValue) const
Normalize vector or return inZeroValue if the length of the vector is zero.
Definition: Vec3.inl:708
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:653
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