19 if (denominator <
Square(FLT_EPSILON))
38 outV = -inA.
Dot(ab) / denominator;
65 float d01 = v0.
Dot(v1);
70 if (denominator < 1.0e-12f)
87 float a0 = inA.
Dot(v0);
88 float a1 = inA.
Dot(v1);
91 outU = 1.0f - outV - outW;
97 float d12 = v1.
Dot(v2);
100 if (denominator < 1.0e-12f)
117 float c1 = inC.
Dot(v1);
118 float c2 = inC.
Dot(v2);
121 outW = 1.0f - outU - outV;
149 return u * inA + v * inB;
156 template <
bool MustIncludeC = false>
183 if (n_len_sq < 1.0e-10f)
188 uint32 closest_set = 0b0100;
189 Vec3 closest_point = inC;
190 float best_dist_sq = inC.
LengthSq();
194 if constexpr (!MustIncludeC)
198 if (a_len_sq < best_dist_sq)
200 closest_set = 0b0001;
202 best_dist_sq = a_len_sq;
207 if (b_len_sq < best_dist_sq)
209 closest_set = 0b0010;
211 best_dist_sq = b_len_sq;
217 if (ac_len_sq >
Square(FLT_EPSILON))
219 float v =
Clamp(-a.
Dot(ac) / ac_len_sq, 0.0f, 1.0f);
222 if (dist_sq < best_dist_sq)
224 closest_set = 0b0101;
226 best_dist_sq = dist_sq;
233 if (bc_len_sq >
Square(FLT_EPSILON))
235 float v =
Clamp(-inB.Dot(bc) / bc_len_sq, 0.0f, 1.0f);
236 Vec3 q = inB + v * bc;
238 if (dist_sq < best_dist_sq)
240 closest_set = 0b0110;
242 best_dist_sq = dist_sq;
247 if constexpr (!MustIncludeC)
252 if (ab_len_sq >
Square(FLT_EPSILON))
254 float v =
Clamp(-inA.Dot(ab) / ab_len_sq, 0.0f, 1.0f);
255 Vec3 q = inA + v * ab;
257 if (dist_sq < best_dist_sq)
259 closest_set = 0b0011;
261 best_dist_sq = dist_sq;
266 outSet = closest_set;
267 return closest_point;
272 float d1 = ab.
Dot(ap);
273 float d2 = ac.
Dot(ap);
274 if (d1 <= 0.0f && d2 <= 0.0f)
276 outSet = swap_ac.
GetX()? 0b0100 : 0b0001;
282 float d3 = ab.
Dot(bp);
283 float d4 = ac.
Dot(bp);
284 if (d3 >= 0.0f && d4 <= d3)
291 if (d1 * d4 <= d3 * d2 && d1 >= 0.0f && d3 <= 0.0f)
293 float v = d1 / (d1 - d3);
294 outSet = swap_ac.
GetX()? 0b0110 : 0b0011;
300 float d5 = ab.
Dot(cp);
301 float d6 = ac.
Dot(cp);
302 if (d6 >= 0.0f && d5 <= d6)
304 outSet = swap_ac.
GetX()? 0b0001 : 0b0100;
309 if (d5 * d2 <= d1 * d6 && d2 >= 0.0f && d6 <= 0.0f)
311 float w = d2 / (d2 - d6);
317 float d4_d3 = d4 - d3;
318 float d5_d6 = d5 - d6;
319 if (d3 * d6 <= d5 * d4 && d4_d3 >= 0.0f && d5_d6 >= 0.0f)
321 float w = d4_d3 / (d4_d3 + d5_d6);
322 outSet = swap_ac.
GetX()? 0b0011 : 0b0110;
323 return inB + w * (c - inB);
333 return n * (a + inB + c).Dot(n) / (3.0f * n_len_sq);
343 Vec3 n = (inB - inA).CrossPrecise(inC - inA);
344 float signp = inA.
Dot(n);
345 float signd = (inD - inA).Dot(n);
350 return signp * signd > -FLT_EPSILON;
373 float signp0 = inA.
Dot(ab_cross_ac);
374 float signp1 = inA.
Dot(ac_cross_ad);
375 float signp2 = inA.
Dot(ad_cross_ab);
376 float signp3 = inB.
Dot(bd_cross_bc);
377 Vec4 signp(signp0, signp1, signp2, signp3);
380 float signd0 = ad.
Dot(ab_cross_ac);
381 float signd1 = ab.
Dot(ac_cross_ad);
382 float signd2 = ac.
Dot(ad_cross_ab);
383 float signd3 = -ab.
Dot(bd_cross_bc);
384 Vec4 signd(signd0, signd1, signd2, signd3);
409 template <
bool MustIncludeD = false>
416 uint32 closest_set = 0b1111;
418 float best_dist_sq = FLT_MAX;
424 if (origin_out_of_planes.
GetX())
426 if constexpr (MustIncludeD)
430 closest_set = 0b0001;
438 best_dist_sq = closest_point.
LengthSq();
442 if (origin_out_of_planes.
GetY())
447 if (dist_sq < best_dist_sq)
449 best_dist_sq = dist_sq;
451 closest_set = (set & 0b0001) + ((set & 0b0110) << 1);
456 if (origin_out_of_planes.
GetZ())
464 if (dist_sq < best_dist_sq)
466 best_dist_sq = dist_sq;
468 closest_set = (set & 0b0011) + ((set & 0b0100) << 1);
473 if (origin_out_of_planes.
GetW())
481 if (dist_sq < best_dist_sq)
484 closest_set = set << 1;
488 outSet = closest_set;
489 return closest_point;
#define JPH_NAMESPACE_END
Definition Core.h:434
std::uint32_t uint32
Definition Core.h:513
#define JPH_NAMESPACE_BEGIN
Definition Core.h:428
JPH_INLINE constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition Math.h:63
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition Math.h:70
JPH_INLINE float DifferenceOfProducts(float inA, float inB, float inC, float inD)
Definition Math.h:49
JPH_INLINE uint32 GetZ() const
Definition UVec4.h:104
JPH_INLINE uint32 GetY() const
Definition UVec4.h:103
static JPH_INLINE UVec4 sReplicate(uint32 inV)
Replicate int inV across all components.
Definition UVec4.inl:75
JPH_INLINE uint32 GetW() const
Definition UVec4.h:105
JPH_INLINE uint32 GetX() const
Get individual components.
Definition UVec4.h:102
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:958
JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const
Dot product, returns the dot product in X, Y, Z and W components.
Definition Vec3.inl:926
JPH_INLINE Vec3 CrossPrecise(Vec3Arg inV2) const
Cross product (more precise version when FMA is available)
Definition Vec3.inl:889
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:987
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:125
static JPH_INLINE Vec3 sSelect(Vec3Arg inNotSet, Vec3Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition Vec3.inl:376
static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Less than or equal (component wise)
Definition Vec4.inl:281
static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2)
Less than (component wise)
Definition Vec4.inl:258
static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Greater than or equal (component wise)
Definition Vec4.inl:327
JPH_INLINE int GetSignBits() const
Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3.
Definition Vec4.inl:1190
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition Vec4.inl:97
Helper utils to find the closest point to a line segment, triangle or tetrahedron.
Definition ClosestPoint.h:11
bool GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, float &outU, float &outV)
Definition ClosestPoint.h:15
Vec3 GetClosestPointOnTriangle(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, uint32 &outSet)
Definition ClosestPoint.h:157
UVec4 OriginOutsideOfTetrahedronPlanes(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
Definition ClosestPoint.h:359
Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
Definition ClosestPoint.h:410
bool OriginOutsideOfPlane(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
Check if the origin is outside the plane of triangle (inA, inB, inC). inD specifies the front side of...
Definition ClosestPoint.h:337
Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
Definition ClosestPoint.h:129