| 465 | |
| 466 | template<class T> |
| 467 | inline float trianglePointDistSq(const vec3<T> &t0, const vec3<T> &t1, const vec3<T> &t2, const vec3<T> &p) { |
| 468 | const vec3<T> edge0 = t1 - t0; |
| 469 | const vec3<T> edge1 = t2 - t0; |
| 470 | const vec3<T> v0 = t0 - p; |
| 471 | |
| 472 | float a = edge0 | edge0; |
| 473 | float b = edge0 | edge1; |
| 474 | float c = edge1 | edge1; |
| 475 | float d = edge0 | v0; |
| 476 | float e = edge1 | v0; |
| 477 | |
| 478 | float det = a * c - b * b; |
| 479 | float s = b * e - c * d; |
| 480 | float t = b * d - a * e; |
| 481 | |
| 482 | if (s + t < det) { |
| 483 | if (s < 0.f) { |
| 484 | if (t < 0.f) { |
| 485 | if (d < 0.f) { |
| 486 | s = std::clamp(-d / a, 0.f, 1.f); |
| 487 | t = 0.f; |
| 488 | } else { |
| 489 | s = 0.f; |
| 490 | t = std::clamp(-e / c, 0.f, 1.f); |
| 491 | } |
| 492 | } else { |
| 493 | s = 0.f; |
| 494 | t = std::clamp(-e / c, 0.f, 1.f); |
| 495 | } |
| 496 | } else if (t < 0.f) { |
| 497 | s = std::clamp(-d / a, 0.f, 1.f); |
| 498 | t = 0.f; |
| 499 | } else { |
| 500 | float invDet = 1.f / det; |
| 501 | s *= invDet; |
| 502 | t *= invDet; |
| 503 | } |
| 504 | } else { |
| 505 | if (s < 0.f) { |
| 506 | float tmp0 = b + d; |
| 507 | float tmp1 = c + e; |
| 508 | if (tmp1 > tmp0) { |
| 509 | float numer = tmp1 - tmp0; |
| 510 | float denom = a - 2 * b + c; |
| 511 | s = std::clamp(numer / denom, 0.f, 1.f); |
| 512 | t = 1 - s; |
| 513 | } else { |
| 514 | t = std::clamp(-e / c, 0.f, 1.f); |
| 515 | s = 0.f; |
| 516 | } |
| 517 | } else if (t < 0.f) { |
| 518 | if (a + d > b + e) { |
| 519 | float numer = c + e - b - d; |
| 520 | float denom = a - 2 * b + c; |
| 521 | s = std::clamp(numer / denom, 0.f, 1.f); |
| 522 | t = 1 - s; |
| 523 | } else { |
| 524 | s = std::clamp(-e / c, 0.f, 1.f); |
no test coverage detected