21 if (denominator <
Square(FLT_EPSILON))
39 outV = -inA.
Dot(ab) / denominator;
58 float d00 = v0.
Dot(v0);
59 float d11 = v1.
Dot(v1);
60 float d22 = v2.
Dot(v2);
64 float d01 = v0.
Dot(v1);
66 float denominator = d00 * d11 - d01 * d01;
67 if (abs(denominator) < 1.0e-12f)
83 float a0 = inA.
Dot(v0);
84 float a1 = inA.
Dot(v1);
85 outV = (d01 * a1 - d11 * a0) / denominator;
86 outW = (d01 * a0 - d00 * a1) / denominator;
87 outU = 1.0f - outV - outW;
93 float d12 = v1.
Dot(v2);
95 float denominator = d11 * d22 - d12 * d12;
96 if (abs(denominator) < 1.0e-12f)
112 float c1 = inC.
Dot(v1);
113 float c2 = inC.
Dot(v2);
114 outU = (d22 * c1 - d12 * c2) / denominator;
115 outV = (d11 * c2 - d12 * c1) / denominator;
116 outW = 1.0f - outU - outV;
143 return u * inA + v * inB;
150 template <
bool MustIncludeC = false>
177 if (n_len_sq < 1.0e-11f)
182 uint32 closest_set = 0b0100;
183 Vec3 closest_point = inC;
184 float best_dist_sq = inC.
LengthSq();
188 if constexpr (!MustIncludeC)
192 if (a_len_sq < best_dist_sq)
194 closest_set = 0b0001;
196 best_dist_sq = a_len_sq;
201 if (b_len_sq < best_dist_sq)
203 closest_set = 0b0010;
205 best_dist_sq = b_len_sq;
211 if (ac_len_sq >
Square(FLT_EPSILON))
213 float v =
Clamp(-a.
Dot(ac) / ac_len_sq, 0.0f, 1.0f);
216 if (dist_sq < best_dist_sq)
218 closest_set = 0b0101;
220 best_dist_sq = dist_sq;
227 if (bc_len_sq >
Square(FLT_EPSILON))
229 float v =
Clamp(-inB.Dot(bc) / bc_len_sq, 0.0f, 1.0f);
230 Vec3 q = inB + v * bc;
232 if (dist_sq < best_dist_sq)
234 closest_set = 0b0110;
236 best_dist_sq = dist_sq;
241 if constexpr (!MustIncludeC)
246 if (ab_len_sq >
Square(FLT_EPSILON))
248 float v =
Clamp(-inA.Dot(ab) / ab_len_sq, 0.0f, 1.0f);
249 Vec3 q = inA + v * ab;
251 if (dist_sq < best_dist_sq)
253 closest_set = 0b0011;
255 best_dist_sq = dist_sq;
260 outSet = closest_set;
261 return closest_point;
266 float d1 = ab.
Dot(ap);
267 float d2 = ac.
Dot(ap);
268 if (d1 <= 0.0f && d2 <= 0.0f)
270 outSet = swap_ac.
GetX()? 0b0100 : 0b0001;
276 float d3 = ab.
Dot(bp);
277 float d4 = ac.
Dot(bp);
278 if (d3 >= 0.0f && d4 <= d3)
285 if (d1 * d4 <= d3 * d2 && d1 >= 0.0f && d3 <= 0.0f)
287 float v = d1 / (d1 - d3);
288 outSet = swap_ac.
GetX()? 0b0110 : 0b0011;
294 float d5 = ab.
Dot(cp);
295 float d6 = ac.
Dot(cp);
296 if (d6 >= 0.0f && d5 <= d6)
298 outSet = swap_ac.
GetX()? 0b0001 : 0b0100;
303 if (d5 * d2 <= d1 * d6 && d2 >= 0.0f && d6 <= 0.0f)
305 float w = d2 / (d2 - d6);
311 float d4_d3 = d4 - d3;
312 float d5_d6 = d5 - d6;
313 if (d3 * d6 <= d5 * d4 && d4_d3 >= 0.0f && d5_d6 >= 0.0f)
315 float w = d4_d3 / (d4_d3 + d5_d6);
316 outSet = swap_ac.
GetX()? 0b0011 : 0b0110;
317 return inB + w * (c - inB);
327 return n * (a + inB + c).Dot(n) / (3.0f * n_len_sq);
337 Vec3 n = (inB - inA).Cross(inC - inA);
338 float signp = inA.
Dot(n);
339 float signd = (inD - inA).Dot(n);
344 return signp * signd > -FLT_EPSILON;
367 float signp0 = inA.
Dot(ab_cross_ac);
368 float signp1 = inA.
Dot(ac_cross_ad);
369 float signp2 = inA.
Dot(ad_cross_ab);
370 float signp3 = inB.
Dot(bd_cross_bc);
371 Vec4 signp(signp0, signp1, signp2, signp3);
374 float signd0 = ad.
Dot(ab_cross_ac);
375 float signd1 = ab.
Dot(ac_cross_ad);
376 float signd2 = ac.
Dot(ad_cross_ab);
377 float signd3 = -ab.
Dot(bd_cross_bc);
378 Vec4 signd(signd0, signd1, signd2, signd3);
403 template <
bool MustIncludeD = false>
410 uint32 closest_set = 0b1111;
412 float best_dist_sq = FLT_MAX;
418 if (origin_out_of_planes.
GetX())
420 if constexpr (MustIncludeD)
424 closest_set = 0b0001;
430 closest_point = GetClosestPointOnTriangle<false>(inA, inB, inC, closest_set);
432 best_dist_sq = closest_point.
LengthSq();
436 if (origin_out_of_planes.
GetY())
439 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inC, inD, set);
441 if (dist_sq < best_dist_sq)
443 best_dist_sq = dist_sq;
445 closest_set = (set & 0b0001) + ((set & 0b0110) << 1);
450 if (origin_out_of_planes.
GetZ())
456 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inB, inD, set);
458 if (dist_sq < best_dist_sq)
460 best_dist_sq = dist_sq;
462 closest_set = (set & 0b0011) + ((set & 0b0100) << 1);
467 if (origin_out_of_planes.
GetW())
473 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inB, inC, inD, set);
475 if (dist_sq < best_dist_sq)
478 closest_set = set << 1;
482 outSet = closest_set;
483 return closest_point;
#define JPH_NAMESPACE_END
Definition Core.h:354
std::uint32_t uint32
Definition Core.h:429
#define JPH_NAMESPACE_BEGIN
Definition Core.h:348
constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition Math.h:45
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:637
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:582
JPH_INLINE Vec4 DotV4(Vec3Arg inV2) const
Dot product, returns the dot product in X, Y, Z and W components.
Definition Vec3.inl:621
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:653
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:741
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
Vec3 GetClosestPointOnTriangle(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, uint32 &outSet)
Definition ClosestPoint.h:151
void GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, float &outU, float &outV)
Definition ClosestPoint.h:17
UVec4 OriginOutsideOfTetrahedronPlanes(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
Definition ClosestPoint.h:353
Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
Definition ClosestPoint.h:404
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:331
Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
Definition ClosestPoint.h:123