22 if (denominator <
Square(FLT_EPSILON))
41 outV = -inA.
Dot(ab) / denominator;
68 float d01 = v0.
Dot(v1);
72 float denominator = d00 * d11 - d01 * d01;
73 if (denominator < 1.0e-12f)
90 float a0 = inA.
Dot(v0);
91 float a1 = inA.
Dot(v1);
92 outV = (d01 * a1 - d11 * a0) / denominator;
93 outW = (d01 * a0 - d00 * a1) / denominator;
94 outU = 1.0f - outV - outW;
100 float d12 = v1.
Dot(v2);
102 float denominator = d11 * d22 - d12 * d12;
103 if (denominator < 1.0e-12f)
120 float c1 = inC.
Dot(v1);
121 float c2 = inC.
Dot(v2);
122 outU = (d22 * c1 - d12 * c2) / denominator;
123 outV = (d11 * c2 - d12 * c1) / denominator;
124 outW = 1.0f - outU - outV;
152 return u * inA + v * inB;
159 template <
bool MustIncludeC = false>
186 if (n_len_sq < 1.0e-10f)
191 uint32 closest_set = 0b0100;
192 Vec3 closest_point = inC;
193 float best_dist_sq = inC.
LengthSq();
197 if constexpr (!MustIncludeC)
201 if (a_len_sq < best_dist_sq)
203 closest_set = 0b0001;
205 best_dist_sq = a_len_sq;
210 if (b_len_sq < best_dist_sq)
212 closest_set = 0b0010;
214 best_dist_sq = b_len_sq;
220 if (ac_len_sq >
Square(FLT_EPSILON))
222 float v =
Clamp(-a.
Dot(ac) / ac_len_sq, 0.0f, 1.0f);
225 if (dist_sq < best_dist_sq)
227 closest_set = 0b0101;
229 best_dist_sq = dist_sq;
236 if (bc_len_sq >
Square(FLT_EPSILON))
238 float v =
Clamp(-inB.Dot(bc) / bc_len_sq, 0.0f, 1.0f);
239 Vec3 q = inB + v * bc;
241 if (dist_sq < best_dist_sq)
243 closest_set = 0b0110;
245 best_dist_sq = dist_sq;
250 if constexpr (!MustIncludeC)
255 if (ab_len_sq >
Square(FLT_EPSILON))
257 float v =
Clamp(-inA.Dot(ab) / ab_len_sq, 0.0f, 1.0f);
258 Vec3 q = inA + v * ab;
260 if (dist_sq < best_dist_sq)
262 closest_set = 0b0011;
264 best_dist_sq = dist_sq;
269 outSet = closest_set;
270 return closest_point;
275 float d1 = ab.
Dot(ap);
276 float d2 = ac.
Dot(ap);
277 if (d1 <= 0.0f && d2 <= 0.0f)
279 outSet = swap_ac.
GetX()? 0b0100 : 0b0001;
285 float d3 = ab.
Dot(bp);
286 float d4 = ac.
Dot(bp);
287 if (d3 >= 0.0f && d4 <= d3)
294 if (d1 * d4 <= d3 * d2 && d1 >= 0.0f && d3 <= 0.0f)
296 float v = d1 / (d1 - d3);
297 outSet = swap_ac.
GetX()? 0b0110 : 0b0011;
303 float d5 = ab.
Dot(cp);
304 float d6 = ac.
Dot(cp);
305 if (d6 >= 0.0f && d5 <= d6)
307 outSet = swap_ac.
GetX()? 0b0001 : 0b0100;
312 if (d5 * d2 <= d1 * d6 && d2 >= 0.0f && d6 <= 0.0f)
314 float w = d2 / (d2 - d6);
320 float d4_d3 = d4 - d3;
321 float d5_d6 = d5 - d6;
322 if (d3 * d6 <= d5 * d4 && d4_d3 >= 0.0f && d5_d6 >= 0.0f)
324 float w = d4_d3 / (d4_d3 + d5_d6);
325 outSet = swap_ac.
GetX()? 0b0011 : 0b0110;
326 return inB + w * (c - inB);
336 return n * (a + inB + c).Dot(n) / (3.0f * n_len_sq);
346 Vec3 n = (inB - inA).Cross(inC - inA);
347 float signp = inA.
Dot(n);
348 float signd = (inD - inA).Dot(n);
353 return signp * signd > -FLT_EPSILON;
376 float signp0 = inA.
Dot(ab_cross_ac);
377 float signp1 = inA.
Dot(ac_cross_ad);
378 float signp2 = inA.
Dot(ad_cross_ab);
379 float signp3 = inB.
Dot(bd_cross_bc);
380 Vec4 signp(signp0, signp1, signp2, signp3);
383 float signd0 = ad.
Dot(ab_cross_ac);
384 float signd1 = ab.
Dot(ac_cross_ad);
385 float signd2 = ac.
Dot(ad_cross_ab);
386 float signd3 = -ab.
Dot(bd_cross_bc);
387 Vec4 signd(signd0, signd1, signd2, signd3);
412 template <
bool MustIncludeD = false>
419 uint32 closest_set = 0b1111;
421 float best_dist_sq = FLT_MAX;
427 if (origin_out_of_planes.
GetX())
429 if constexpr (MustIncludeD)
433 closest_set = 0b0001;
439 closest_point = GetClosestPointOnTriangle<false>(inA, inB, inC, closest_set);
441 best_dist_sq = closest_point.
LengthSq();
445 if (origin_out_of_planes.
GetY())
448 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inC, inD, set);
450 if (dist_sq < best_dist_sq)
452 best_dist_sq = dist_sq;
454 closest_set = (set & 0b0001) + ((set & 0b0110) << 1);
459 if (origin_out_of_planes.
GetZ())
465 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inB, inD, set);
467 if (dist_sq < best_dist_sq)
469 best_dist_sq = dist_sq;
471 closest_set = (set & 0b0011) + ((set & 0b0100) << 1);
476 if (origin_out_of_planes.
GetW())
482 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inB, inC, inD, set);
484 if (dist_sq < best_dist_sq)
487 closest_set = set << 1;
491 outSet = closest_set;
492 return closest_point;
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
JPH_INLINE constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition: Math.h:52
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:56
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:645
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:590
JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const
Dot product, returns the dot product in X, Y, Z and W components.
Definition: Vec3.inl:629
static JPH_INLINE Vec3 sSelect(Vec3Arg inV1, Vec3Arg inV2, UVec4Arg inControl)
Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of in...
Definition: Vec3.inl:269
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:661
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Less than or equal (component wise)
Definition: Vec4.inl:194
static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2)
Less than (component wise)
Definition: Vec4.inl:180
static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2)
Greater than or equal (component wise)
Definition: Vec4.inl:222
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:749
static JPH_INLINE Vec4 sReplicate(float inV)
Replicate inV across all components.
Definition: Vec4.inl:74
Helper utils to find the closest point to a line segment, triangle or tetrahedron.
Definition: ClosestPoint.h:14
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
UVec4 OriginOutsideOfTetrahedronPlanes(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
Definition: ClosestPoint.h:362
Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
Definition: ClosestPoint.h:413
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:340
Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
Definition: ClosestPoint.h:132