19 if (denominator <
Square(FLT_EPSILON))
38 outV = -inA.
Dot(ab) / denominator;
65 float d01 = v0.
Dot(v1);
71 if (denominator <= 1.0e-6f * d00 * d11)
88 float a0 = inA.
Dot(v0);
89 float a1 = inA.
Dot(v1);
92 outU = 1.0f - outV - outW;
98 float d12 = v1.
Dot(v2);
101 if (denominator <= 1.0e-6f * d11 * d22)
118 float c1 = inC.
Dot(v1);
119 float c2 = inC.
Dot(v2);
122 outW = 1.0f - outU - outV;
150 return u * inA + v * inB;
157 template <
bool MustIncludeC = false>
184 if (n_len_sq < 1.0e-10f)
189 uint32 closest_set = 0b0100;
190 Vec3 closest_point = inC;
191 float best_dist_sq = inC.
LengthSq();
195 if constexpr (!MustIncludeC)
199 if (a_len_sq < best_dist_sq)
201 closest_set = 0b0001;
203 best_dist_sq = a_len_sq;
208 if (b_len_sq < best_dist_sq)
210 closest_set = 0b0010;
212 best_dist_sq = b_len_sq;
218 if (ac_len_sq >
Square(FLT_EPSILON))
220 float v =
Clamp(-a.
Dot(ac) / ac_len_sq, 0.0f, 1.0f);
223 if (dist_sq < best_dist_sq)
225 closest_set = 0b0101;
227 best_dist_sq = dist_sq;
234 if (bc_len_sq >
Square(FLT_EPSILON))
236 float v =
Clamp(-inB.Dot(bc) / bc_len_sq, 0.0f, 1.0f);
237 Vec3 q = inB + v * bc;
239 if (dist_sq < best_dist_sq)
241 closest_set = 0b0110;
243 best_dist_sq = dist_sq;
248 if constexpr (!MustIncludeC)
253 if (ab_len_sq >
Square(FLT_EPSILON))
255 float v =
Clamp(-inA.Dot(ab) / ab_len_sq, 0.0f, 1.0f);
256 Vec3 q = inA + v * ab;
258 if (dist_sq < best_dist_sq)
260 closest_set = 0b0011;
262 best_dist_sq = dist_sq;
267 outSet = closest_set;
268 return closest_point;
273 float d1 = ab.
Dot(ap);
274 float d2 = ac.
Dot(ap);
275 if (d1 <= 0.0f && d2 <= 0.0f)
277 outSet = swap_ac.
GetX()? 0b0100 : 0b0001;
283 float d3 = ab.
Dot(bp);
284 float d4 = ac.
Dot(bp);
285 if (d3 >= 0.0f && d4 <= d3)
292 if (d1 * d4 <= d3 * d2 && d1 >= 0.0f && d3 <= 0.0f)
294 float v = d1 / (d1 - d3);
295 outSet = swap_ac.
GetX()? 0b0110 : 0b0011;
301 float d5 = ab.
Dot(cp);
302 float d6 = ac.
Dot(cp);
303 if (d6 >= 0.0f && d5 <= d6)
305 outSet = swap_ac.
GetX()? 0b0001 : 0b0100;
310 if (d5 * d2 <= d1 * d6 && d2 >= 0.0f && d6 <= 0.0f)
312 float w = d2 / (d2 - d6);
318 float d4_d3 = d4 - d3;
319 float d5_d6 = d5 - d6;
320 if (d3 * d6 <= d5 * d4 && d4_d3 >= 0.0f && d5_d6 >= 0.0f)
322 float w = d4_d3 / (d4_d3 + d5_d6);
323 outSet = swap_ac.
GetX()? 0b0011 : 0b0110;
324 return inB + w * (c - inB);
334 return n * (a + inB + c).Dot(n) / (3.0f * n_len_sq);
344 Vec3 n = (inB - inA).CrossPrecise(inC - inA);
345 float signp = inA.
Dot(n);
346 float signd = (inD - inA).Dot(n);
351 return signp * signd > -FLT_EPSILON;
374 float signp0 = inA.
Dot(ab_cross_ac);
375 float signp1 = inA.
Dot(ac_cross_ad);
376 float signp2 = inA.
Dot(ad_cross_ab);
377 float signp3 = inB.
Dot(bd_cross_bc);
378 Vec4 signp(signp0, signp1, signp2, signp3);
381 float signd0 = ad.
Dot(ab_cross_ac);
382 float signd1 = ab.
Dot(ac_cross_ad);
383 float signd2 = ac.
Dot(ad_cross_ab);
384 float signd3 = -ab.
Dot(bd_cross_bc);
385 Vec4 signd(signd0, signd1, signd2, signd3);
410 template <
bool MustIncludeD = false>
417 uint32 closest_set = 0b1111;
419 float best_dist_sq = FLT_MAX;
425 if (origin_out_of_planes.
GetX())
427 if constexpr (MustIncludeD)
431 closest_set = 0b0001;
439 best_dist_sq = closest_point.
LengthSq();
443 if (origin_out_of_planes.
GetY())
448 if (dist_sq < best_dist_sq)
450 best_dist_sq = dist_sq;
452 closest_set = (set & 0b0001) + ((set & 0b0110) << 1);
457 if (origin_out_of_planes.
GetZ())
465 if (dist_sq < best_dist_sq)
467 best_dist_sq = dist_sq;
469 closest_set = (set & 0b0011) + ((set & 0b0100) << 1);
474 if (origin_out_of_planes.
GetW())
482 if (dist_sq < best_dist_sq)
485 closest_set = set << 1;
489 outSet = closest_set;
490 return closest_point;
#define JPH_NAMESPACE_END
Definition Core.h:479
std::uint32_t uint32
Definition Core.h:559
#define JPH_NAMESPACE_BEGIN
Definition Core.h:473
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:931
JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const
Dot product, returns the dot product in X, Y, Z and W components.
Definition Vec3.inl:941
JPH_INLINE Vec3 CrossPrecise(Vec3Arg inV2) const
Cross product (more precise version when FMA is available)
Definition Vec3.inl:893
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:946
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:1130
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:158
UVec4 OriginOutsideOfTetrahedronPlanes(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
Definition ClosestPoint.h:360
Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
Definition ClosestPoint.h:411
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:338
Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
Definition ClosestPoint.h:130