| 1 |
s) |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
JPH_NAMESPACE_BEGIN |
| 8 |
|
| 9 |
|
| 10 |
JPH_PRECISE_MATH_ON |
| 11 |
|
| 12 |
|
| 13 |
namespace ClosestPoint |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
inline bool GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, float &outU, float &outV) |
| 19 |
{ |
| 20 |
Vec3 ab = inB - inA; |
| 21 |
float denominator = ab.LengthSq(); |
| 22 |
if (denominator < Square(FLT_EPSILON)) |
| 23 |
{ |
| 24 |
|
| 25 |
if (inA.LengthSq() < inB.LengthSq()) |
| 26 |
{ |
| 27 |
|
| 28 |
outU = 1.0f; |
| 29 |
outV = 0.0f; |
| 30 |
} |
| 31 |
else |
| 32 |
{ |
| 33 |
|
| 34 |
outU = 0.0f; |
| 35 |
outV = 1.0f; |
| 36 |
} |
| 37 |
return false; |
| 38 |
} |
| 39 |
else |
| 40 |
{ |
| 41 |
outV = -inA.Dot(ab) / denominator; |
| 42 |
outU = 1.0f - outV; |
| 43 |
} |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
inline bool GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, float &outU, float &outV, float &outW) |
| 51 |
{ |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Vec3 v0 = inB - inA; |
| 58 |
Vec3 v1 = inC - inA; |
| 59 |
Vec3 v2 = inC - inB; |
| 60 |
|
| 61 |
|
| 62 |
float d00 = v0.LengthSq(); |
| 63 |
float d11 = v1.LengthSq(); |
| 64 |
float d22 = v2.LengthSq(); |
| 65 |
if (d00 <= d22) |
| 66 |
{ |
| 67 |
|
| 68 |
float d01 = v0.Dot(v1); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
float denominator = d00 * d11 - d01 * d01; |
| 73 |
if (denominator < 1.0e-12f) |
| 74 |
{ |
| 75 |
|
| 76 |
if (d00 > d11) |
| 77 |
{ |
| 78 |
GetBaryCentricCoordinates(inA, inB, outU, outV); |
| 79 |
outW = 0.0f; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
GetBaryCentricCoordinates(inA, inC, outU, outW); |
| 84 |
outV = 0.0f; |
| 85 |
} |
| 86 |
return false; |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 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; |
| 95 |
} |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
|
| 100 |
float d12 = v1.Dot(v2); |
| 101 |
|
| 102 |
float denominator = d11 * d22 - d12 * d12; |
| 103 |
if (denominator < 1.0e-12f) |
| 104 |
{ |
| 105 |
|
| 106 |
if (d11 > d22) |
| 107 |
{ |
| 108 |
GetBaryCentricCoordinates(inA, inC, outU, outW); |
| 109 |
outV = 0.0f; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
GetBaryCentricCoordinates(inB, inC, outV, outW); |
| 114 |
outU = 0.0f; |
| 115 |
} |
| 116 |
return false; |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 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; |
| 125 |
} |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
inline Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet) |
| 133 |
{ |
| 134 |
float u, v; |
| 135 |
GetBaryCentricCoordinates(inA, inB, u, v); |
| 136 |
if (v <= 0.0f) |
| 137 |
{ |
| 138 |
|
| 139 |
outSet = 0b0001; |
| 140 |
return inA; |
| 141 |
} |
| 142 |
else if (u <= 0.0f) |
| 143 |
{ |
| 144 |
|
| 145 |
outSet = 0b0010; |
| 146 |
return inB; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
|
| 151 |
outSet = 0b0011; |
| 152 |
return u * inA + v * inB; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
template <bool MustIncludeC = false> |
| 160 |
inline Vec3 GetClosestPointOnTriangle(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, uint32 &outSet) |
| 161 |
{ |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
UVec4 swap_ac; |
| 171 |
{ |
| 172 |
Vec3 ac = inC - inA; |
| 173 |
Vec3 bc = inC - inB; |
| 174 |
swap_ac = Vec4::sLess(bc.DotV4(bc), ac.DotV4(ac)); |
| 175 |
} |
| 176 |
Vec3 a = Vec3::sSelect(inA, inC, swap_ac); |
| 177 |
Vec3 c = Vec3::sSelect(inC, inA, swap_ac); |
| 178 |
|
| 179 |
|
| 180 |
Vec3 ab = inB - a; |
| 181 |
Vec3 ac = c - a; |
| 182 |
Vec3 n = ab.Cross(ac); |
| 183 |
float n_len_sq = n.LengthSq(); |
| 184 |
|
| 185 |
|
| 186 |
if (n_len_sq < 1.0e-10f) |
| 187 |
{ |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
uint32 closest_set = 0b0100; |
| 192 |
Vec3 closest_point = inC; |
| 193 |
float best_dist_sq = inC.LengthSq(); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if constexpr (!MustIncludeC) |
| 198 |
{ |
| 199 |
|
| 200 |
float a_len_sq = inA.LengthSq(); |
| 201 |
if (a_len_sq < best_dist_sq) |
| 202 |
{ |
| 203 |
closest_set = 0b0001; |
| 204 |
closest_point = inA; |
| 205 |
best_dist_sq = a_len_sq; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
float b_len_sq = inB.LengthSq(); |
| 210 |
if (b_len_sq < best_dist_sq) |
| 211 |
{ |
| 212 |
closest_set = 0b0010; |
| 213 |
closest_point = inB; |
| 214 |
best_dist_sq = b_len_sq; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
float ac_len_sq = ac.LengthSq(); |
| 220 |
if (ac_len_sq > Square(FLT_EPSILON)) |
| 221 |
{ |
| 222 |
float v = Clamp(-a.Dot(ac) / ac_len_sq, 0.0f, 1.0f); |
| 223 |
Vec3 q = a + v * ac; |
| 224 |
float dist_sq = q.LengthSq(); |
| 225 |
if (dist_sq < best_dist_sq) |
| 226 |
{ |
| 227 |
closest_set = 0b0101; |
| 228 |
closest_point = q; |
| 229 |
best_dist_sq = dist_sq; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
Vec3 bc = inC - inB; |
| 235 |
float bc_len_sq = bc.LengthSq(); |
| 236 |
if (bc_len_sq > Square(FLT_EPSILON)) |
| 237 |
{ |
| 238 |
float v = Clamp(-inB.Dot(bc) / bc_len_sq, 0.0f, 1.0f); |
| 239 |
Vec3 q = inB + v * bc; |
| 240 |
float dist_sq = q.LengthSq(); |
| 241 |
if (dist_sq < best_dist_sq) |
| 242 |
{ |
| 243 |
closest_set = 0b0110; |
| 244 |
closest_point = q; |
| 245 |
best_dist_sq = dist_sq; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if constexpr (!MustIncludeC) |
| 251 |
{ |
| 252 |
|
| 253 |
ab = inB - inA; |
| 254 |
float ab_len_sq = ab.LengthSq(); |
| 255 |
if (ab_len_sq > Square(FLT_EPSILON)) |
| 256 |
{ |
| 257 |
float v = Clamp(-inA.Dot(ab) / ab_len_sq, 0.0f, 1.0f); |
| 258 |
Vec3 q = inA + v * ab; |
| 259 |
float dist_sq = q.LengthSq(); |
| 260 |
if (dist_sq < best_dist_sq) |
| 261 |
{ |
| 262 |
closest_set = 0b0011; |
| 263 |
closest_point = q; |
| 264 |
best_dist_sq = dist_sq; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
outSet = closest_set; |
| 270 |
return closest_point; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
Vec3 ap = -a; |
| 275 |
float d1 = ab.Dot(ap); |
| 276 |
float d2 = ac.Dot(ap); |
| 277 |
if (d1 <= 0.0f && d2 <= 0.0f) |
| 278 |
{ |
| 279 |
outSet = swap_ac.GetX()? 0b0100 : 0b0001; |
| 280 |
return a; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
Vec3 bp = -inB; |
| 285 |
float d3 = ab.Dot(bp); |
| 286 |
float d4 = ac.Dot(bp); |
| 287 |
if (d3 >= 0.0f && d4 <= d3) |
| 288 |
{ |
| 289 |
outSet = 0b0010; |
| 290 |
return inB; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
if (d1 * d4 <= d3 * d2 && d1 >= 0.0f && d3 <= 0.0f) |
| 295 |
{ |
| 296 |
float v = d1 / (d1 - d3); |
| 297 |
outSet = swap_ac.GetX()? 0b0110 : 0b0011; |
| 298 |
return a + v * ab; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
Vec3 cp = -c; |
| 303 |
float d5 = ab.Dot(cp); |
| 304 |
float d6 = ac.Dot(cp); |
| 305 |
if (d6 >= 0.0f && d5 <= d6) |
| 306 |
{ |
| 307 |
outSet = swap_ac.GetX()? 0b0001 : 0b0100; |
| 308 |
return c; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
if (d5 * d2 <= d1 * d6 && d2 >= 0.0f && d6 <= 0.0f) |
| 313 |
{ |
| 314 |
float w = d2 / (d2 - d6); |
| 315 |
outSet = 0b0101; |
| 316 |
return a + w * ac; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 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) |
| 323 |
{ |
| 324 |
float w = d4_d3 / (d4_d3 + d5_d6); |
| 325 |
outSet = swap_ac.GetX()? 0b0011 : 0b0110; |
| 326 |
return inB + w * (c - inB); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
outSet = 0b0111; |
| 336 |
return n * (a + inB + c).Dot(n) / (3.0f * n_len_sq); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
inline bool OriginOutsideOfPlane(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD) |
| 341 |
{ |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
Vec3 n = (inB - inA).Cross(inC - inA); |
| 347 |
float signp = inA.Dot(n); |
| 348 |
float signd = (inD - inA).Dot(n); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
return signp * signd > -FLT_EPSILON; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
inline UVec4 OriginOutsideOfTetrahedronPlanes(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD) |
| 363 |
{ |
| 364 |
Vec3 ab = inB - inA; |
| 365 |
Vec3 ac = inC - inA; |
| 366 |
Vec3 ad = inD - inA; |
| 367 |
Vec3 bd = inD - inB; |
| 368 |
Vec3 bc = inC - inB; |
| 369 |
|
| 370 |
Vec3 ab_cross_ac = ab.Cross(ac); |
| 371 |
Vec3 ac_cross_ad = ac.Cross(ad); |
| 372 |
Vec3 ad_cross_ab = ad.Cross(ab); |
| 373 |
Vec3 bd_cross_bc = bd.Cross(bc); |
| 374 |
|
| 375 |
|
| 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); |
| 381 |
|
| 382 |
|
| 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); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
int sign_bits = signd.GetSignBits(); |
| 393 |
switch (sign_bits) |
| 394 |
{ |
| 395 |
case 0: |
| 396 |
|
| 397 |
return Vec4::sGreaterOrEqual(signp, Vec4::sReplicate(-FLT_EPSILON)); |
| 398 |
|
| 399 |
case 0xf: |
| 400 |
|
| 401 |
return Vec4::sLessOrEqual(signp, Vec4::sReplicate(FLT_EPSILON)); |
| 402 |
|
| 403 |
default: |
| 404 |
|
| 405 |
return UVec4::sReplicate(0xffffffff); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
template <bool MustIncludeD = false> |
| 413 |
inline Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet) |
| 414 |
{ |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
uint32 closest_set = 0b1111; |
| 420 |
Vec3 closest_point = Vec3::sZero(); |
| 421 |
float best_dist_sq = FLT_MAX; |
| 422 |
|
| 423 |
|
| 424 |
UVec4 origin_out_of_planes = OriginOutsideOfTetrahedronPlanes(inA, inB, inC, inD); |
| 425 |
|
| 426 |
|
| 427 |
if (origin_out_of_planes.GetX()) |
| 428 |
{ |
| 429 |
if constexpr (MustIncludeD) |
| 430 |
{ |
| 431 |
|
| 432 |
|
| 433 |
closest_set = 0b0001; |
| 434 |
closest_point = inA; |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
|
| 439 |
closest_point = GetClosestPointOnTriangle<false>(inA, inB, inC, closest_set); |
| 440 |
} |
| 441 |
best_dist_sq = closest_point.LengthSq(); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
if (origin_out_of_planes.GetY()) |
| 446 |
{ |
| 447 |
uint32 set; |
| 448 |
Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inC, inD, set); |
| 449 |
float dist_sq = q.LengthSq(); |
| 450 |
if (dist_sq < best_dist_sq) |
| 451 |
{ |
| 452 |
best_dist_sq = dist_sq; |
| 453 |
closest_point = q; |
| 454 |
closest_set = (set & 0b0001) + ((set & 0b0110) << 1); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
if (origin_out_of_planes.GetZ()) |
| 460 |
{ |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
uint32 set; |
| 465 |
Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inA, inB, inD, set); |
| 466 |
float dist_sq = q.LengthSq(); |
| 467 |
if (dist_sq < best_dist_sq) |
| 468 |
{ |
| 469 |
best_dist_sq = dist_sq; |
| 470 |
closest_point = q; |
| 471 |
closest_set = (set & 0b0011) + ((set & 0b0100) << 1); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
if (origin_out_of_planes.GetW()) |
| 477 |
{ |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
uint32 set; |
| 482 |
Vec3 q = GetClosestPointOnTriangle<MustIncludeD>(inB, inC, inD, set); |
| 483 |
float dist_sq = q.LengthSq(); |
| 484 |
if (dist_sq < best_dist_sq) |
| 485 |
{ |
| 486 |
closest_point = q; |
| 487 |
closest_set = set << 1; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
outSet = closest_set; |
| 492 |
return closest_point; |
| 493 |
} |
| 494 |
}; |
| 495 |
|
| 496 |
JPH_PRECISE_MATH_OFF |
| 497 |
|
| 498 |
JPH_NAMESPACE_END |