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) < FLT_EPSILON)
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) < FLT_EPSILON)
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();
187 if constexpr (!MustIncludeC)
191 if (a_len_sq < best_dist_sq)
193 closest_set = 0b0001;
195 best_dist_sq = a_len_sq;
200 if (b_len_sq < best_dist_sq)
202 closest_set = 0b0010;
204 best_dist_sq = b_len_sq;
209 if (ab_len_sq >
Square(FLT_EPSILON))
211 float v =
Clamp(-a.
Dot(ab) / ab_len_sq, 0.0f, 1.0f);
214 if (dist_sq < best_dist_sq)
216 closest_set = swap_ac.
GetX()? 0b0110 : 0b0011;
218 best_dist_sq = dist_sq;
225 if (ac_len_sq >
Square(FLT_EPSILON))
227 float v =
Clamp(-a.
Dot(ac) / ac_len_sq, 0.0f, 1.0f);
230 if (dist_sq < best_dist_sq)
232 closest_set = 0b0101;
234 best_dist_sq = dist_sq;
241 if (bc_len_sq >
Square(FLT_EPSILON))
243 float v =
Clamp(-inB.Dot(bc) / bc_len_sq, 0.0f, 1.0f);
244 Vec3 q = inB + v * bc;
246 if (dist_sq < best_dist_sq)
248 closest_set = swap_ac.
GetX()? 0b0011 : 0b0110;
250 best_dist_sq = dist_sq;
254 outSet = closest_set;
255 return closest_point;
260 float d1 = ab.
Dot(ap);
261 float d2 = ac.
Dot(ap);
262 if (d1 <= 0.0f && d2 <= 0.0f)
264 outSet = swap_ac.
GetX()? 0b0100 : 0b0001;
270 float d3 = ab.
Dot(bp);
271 float d4 = ac.
Dot(bp);
272 if (d3 >= 0.0f && d4 <= d3)
279 if (d1 * d4 <= d3 * d2 && d1 >= 0.0f && d3 <= 0.0f)
281 float v = d1 / (d1 - d3);
282 outSet = swap_ac.
GetX()? 0b0110 : 0b0011;
288 float d5 = ab.
Dot(cp);
289 float d6 = ac.
Dot(cp);
290 if (d6 >= 0.0f && d5 <= d6)
292 outSet = swap_ac.
GetX()? 0b0001 : 0b0100;
297 if (d5 * d2 <= d1 * d6 && d2 >= 0.0f && d6 <= 0.0f)
299 float w = d2 / (d2 - d6);
305 float d4_d3 = d4 - d3;
306 float d5_d6 = d5 - d6;
307 if (d3 * d6 <= d5 * d4 && d4_d3 >= 0.0f && d5_d6 >= 0.0f)
309 float w = d4_d3 / (d4_d3 + d5_d6);
310 outSet = swap_ac.
GetX()? 0b0011 : 0b0110;
311 return inB + w * (c - inB);
321 return n * (a + inB + c).Dot(n) / (3.0f * n_len_sq);
331 Vec3 n = (inB - inA).Cross(inC - inA);
332 float signp = inA.
Dot(n);
333 float signd = (inD - inA).Dot(n);
338 return signp * signd > -FLT_EPSILON;
361 float signp0 = inA.
Dot(ab_cross_ac);
362 float signp1 = inA.
Dot(ac_cross_ad);
363 float signp2 = inA.
Dot(ad_cross_ab);
364 float signp3 = inB.
Dot(bd_cross_bc);
365 Vec4 signp(signp0, signp1, signp2, signp3);
368 float signd0 = ad.
Dot(ab_cross_ac);
369 float signd1 = ab.
Dot(ac_cross_ad);
370 float signd2 = ac.
Dot(ad_cross_ab);
371 float signd3 = -ab.
Dot(bd_cross_bc);
372 Vec4 signd(signd0, signd1, signd2, signd3);
397 template <
bool MustIncludeD = false>
404 uint32 closest_set = 0b1111;
406 float best_dist_sq = FLT_MAX;
412 if (origin_out_of_planes.
GetX())
414 if constexpr (MustIncludeD)
418 closest_set = 0b0001;
424 closest_point = GetClosestPointOnTriangle<false>(inA, inB, inC, closest_set);
426 best_dist_sq = closest_point.
LengthSq();
430 if (origin_out_of_planes.
GetY())
433 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inC, inD, set);
435 if (dist_sq < best_dist_sq)
437 best_dist_sq = dist_sq;
439 closest_set = (set & 0b0001) + ((set & 0b0110) << 1);
444 if (origin_out_of_planes.
GetZ())
450 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inB, inD, set);
452 if (dist_sq < best_dist_sq)
454 best_dist_sq = dist_sq;
456 closest_set = (set & 0b0011) + ((set & 0b0100) << 1);
461 if (origin_out_of_planes.
GetW())
467 Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inB, inC, inD, set);
469 if (dist_sq < best_dist_sq)
472 closest_set = set << 1;
476 outSet = closest_set;
477 return closest_point;
uint32_t uint32
Definition Core.h:312
#define JPH_NAMESPACE_END
Definition Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition Core.h:234
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:103
JPH_INLINE uint32 GetY() const
Definition UVec4.h:102
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:104
JPH_INLINE uint32 GetX() const
Get individual components.
Definition UVec4.h:101
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:746
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:347
Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
Definition ClosestPoint.h:398
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:325
Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
Definition ClosestPoint.h:123