/usr/include/simgear/math/SGIntersect.hxx is in libsimgear-dev 1:2018.1.1+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 | // Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef SGIntersect_HXX
#define SGIntersect_HXX
#include <algorithm>
template<typename T>
inline bool
intersects(const SGSphere<T>& s1, const SGSphere<T>& s2)
{
if (s1.empty())
return false;
if (s2.empty())
return false;
T dist = s1.getRadius() + s2.getRadius();
return distSqr(s1.getCenter(), s2.getCenter()) <= dist*dist;
}
template<typename T1, typename T2>
inline bool
intersects(const SGBox<T1>& box, const SGSphere<T2>& sphere)
{
if (sphere.empty())
return false;
if (box.empty())
return false;
SGVec3<T1> closest = box.getClosestPoint(sphere.getCenter());
return distSqr(closest, SGVec3<T1>(sphere.getCenter())) <= sphere.getRadius2();
}
// make it symmetric
template<typename T1, typename T2>
inline bool
intersects(const SGSphere<T1>& sphere, const SGBox<T2>& box)
{ return intersects(box, sphere); }
template<typename T1, typename T2>
inline bool
intersects(const SGVec3<T1>& v, const SGBox<T2>& box)
{
if (v[0] < box.getMin()[0])
return false;
if (box.getMax()[0] < v[0])
return false;
if (v[1] < box.getMin()[1])
return false;
if (box.getMax()[1] < v[1])
return false;
if (v[2] < box.getMin()[2])
return false;
if (box.getMax()[2] < v[2])
return false;
return true;
}
template<typename T1, typename T2>
inline bool
intersects(const SGBox<T1>& box, const SGVec3<T2>& v)
{ return intersects(v, box); }
template<typename T>
inline bool
intersects(const SGRay<T>& ray, const SGPlane<T>& plane)
{
// We compute the intersection point
// x = origin + \alpha*direction
// from the ray origin and non nomalized direction.
// For 0 <= \alpha the ray intersects the infinite plane.
// The intersection point x can also be written
// x = n*dist + y
// where n is the planes normal, dist is the distance of the plane from
// the origin in normal direction and y is ana aproriate vector
// perpendicular to n.
// Equate the x values and take the scalar product with the plane normal n.
// dot(n, origin) + \alpha*dot(n, direction) = dist
// We can now compute alpha from the above equation.
// \alpha = (dist - dot(n, origin))/dot(n, direction)
// The negative numerator for the \alpha expression
T num = plane.getPositiveDist();
num -= dot(plane.getNormal(), ray.getOrigin());
// If the numerator is zero, we have the rays origin included in the plane
if (fabs(num) <= SGLimits<T>::min())
return true;
// The denominator for the \alpha expression
T den = dot(plane.getNormal(), ray.getDirection());
// If we get here, we already know that the rays origin is not included
// in the plane. Thus if we have a zero denominator we have
// a ray paralell to the plane. That is no intersection.
if (fabs(den) <= SGLimits<T>::min())
return false;
// We would now compute \alpha = num/den and compare with 0 and 1.
// But to avoid that expensive division, check equation multiplied by
// the denominator.
T alphaDen = copysign(1, den)*num;
if (alphaDen < 0)
return false;
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(const SGPlane<T>& plane, const SGRay<T>& ray)
{ return intersects(ray, plane); }
template<typename T>
inline bool
intersects(SGVec3<T>& dst, const SGRay<T>& ray, const SGPlane<T>& plane)
{
// We compute the intersection point
// x = origin + \alpha*direction
// from the ray origin and non nomalized direction.
// For 0 <= \alpha the ray intersects the infinite plane.
// The intersection point x can also be written
// x = n*dist + y
// where n is the planes normal, dist is the distance of the plane from
// the origin in normal direction and y is ana aproriate vector
// perpendicular to n.
// Equate the x values and take the scalar product with the plane normal n.
// dot(n, origin) + \alpha*dot(n, direction) = dist
// We can now compute alpha from the above equation.
// \alpha = (dist - dot(n, origin))/dot(n, direction)
// The negative numerator for the \alpha expression
T num = plane.getPositiveDist();
num -= dot(plane.getNormal(), ray.getOrigin());
// If the numerator is zero, we have the rays origin included in the plane
if (fabs(num) <= SGLimits<T>::min()) {
dst = ray.getOrigin();
return true;
}
// The denominator for the \alpha expression
T den = dot(plane.getNormal(), ray.getDirection());
// If we get here, we already know that the rays origin is not included
// in the plane. Thus if we have a zero denominator we have
// a ray paralell to the plane. That is no intersection.
if (fabs(den) <= SGLimits<T>::min())
return false;
// We would now compute \alpha = num/den and compare with 0 and 1.
// But to avoid that expensive division, check equation multiplied by
// the denominator.
T alpha = num/den;
if (alpha < 0)
return false;
dst = ray.getOrigin() + alpha*ray.getDirection();
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(SGVec3<T>& dst, const SGPlane<T>& plane, const SGRay<T>& ray)
{ return intersects(dst, ray, plane); }
template<typename T>
inline bool
intersects(const SGLineSegment<T>& lineSegment, const SGPlane<T>& plane)
{
// We compute the intersection point
// x = origin + \alpha*direction
// from the line segments origin and non nomalized direction.
// For 0 <= \alpha <= 1 the line segment intersects the infinite plane.
// The intersection point x can also be written
// x = n*dist + y
// where n is the planes normal, dist is the distance of the plane from
// the origin in normal direction and y is ana aproriate vector
// perpendicular to n.
// Equate the x values and take the scalar product with the plane normal n.
// dot(n, origin) + \alpha*dot(n, direction) = dist
// We can now compute alpha from the above equation.
// \alpha = (dist - dot(n, origin))/dot(n, direction)
// The negative numerator for the \alpha expression
T num = plane.getPositiveDist();
num -= dot(plane.getNormal(), lineSegment.getOrigin());
// If the numerator is zero, we have the lines origin included in the plane
if (fabs(num) <= SGLimits<T>::min())
return true;
// The denominator for the \alpha expression
T den = dot(plane.getNormal(), lineSegment.getDirection());
// If we get here, we already know that the lines origin is not included
// in the plane. Thus if we have a zero denominator we have
// a line paralell to the plane. That is no intersection.
if (fabs(den) <= SGLimits<T>::min())
return false;
// We would now compute \alpha = num/den and compare with 0 and 1.
// But to avoid that expensive division, compare equations
// multiplied by |den|. Note that copysign is usually a compiler intrinsic
// that expands in assembler code that not even stalls the cpus pipes.
T alphaDen = copysign(1, den)*num;
if (alphaDen < 0)
return false;
if (den < alphaDen)
return false;
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(const SGPlane<T>& plane, const SGLineSegment<T>& lineSegment)
{ return intersects(lineSegment, plane); }
template<typename T>
inline bool
intersects(SGVec3<T>& dst, const SGLineSegment<T>& lineSegment, const SGPlane<T>& plane)
{
// We compute the intersection point
// x = origin + \alpha*direction
// from the line segments origin and non nomalized direction.
// For 0 <= \alpha <= 1 the line segment intersects the infinite plane.
// The intersection point x can also be written
// x = n*dist + y
// where n is the planes normal, dist is the distance of the plane from
// the origin in normal direction and y is an aproriate vector
// perpendicular to n.
// Equate the x values and take the scalar product with the plane normal n.
// dot(n, origin) + \alpha*dot(n, direction) = dist
// We can now compute alpha from the above equation.
// \alpha = (dist - dot(n, origin))/dot(n, direction)
// The negative numerator for the \alpha expression
T num = plane.getPositiveDist();
num -= dot(plane.getNormal(), lineSegment.getStart());
// If the numerator is zero, we have the lines origin included in the plane
if (fabs(num) <= SGLimits<T>::min()) {
dst = lineSegment.getStart();
return true;
}
// The denominator for the \alpha expression
T den = dot(plane.getNormal(), lineSegment.getDirection());
// If we get here, we already know that the lines origin is not included
// in the plane. Thus if we have a zero denominator we have
// a line paralell to the plane. That is: no intersection.
if (fabs(den) <= SGLimits<T>::min())
return false;
// We would now compute \alpha = num/den and compare with 0 and 1.
// But to avoid that expensive division, check equation multiplied by
// the denominator. FIXME: shall we do so? or compute like that?
T alpha = num/den;
if (alpha < 0)
return false;
if (1 < alpha)
return false;
dst = lineSegment.getStart() + alpha*lineSegment.getDirection();
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(SGVec3<T>& dst, const SGPlane<T>& plane, const SGLineSegment<T>& lineSegment)
{ return intersects(dst, lineSegment, plane); }
// Distance of a line segment to a point
template<typename T>
inline T
distSqr(const SGLineSegment<T>& lineSeg, const SGVec3<T>& p)
{
SGVec3<T> ps = p - lineSeg.getStart();
T psdotdir = dot(ps, lineSeg.getDirection());
if (psdotdir <= 0)
return dot(ps, ps);
SGVec3<T> pe = p - lineSeg.getEnd();
if (0 <= dot(pe, lineSeg.getDirection()))
return dot(pe, pe);
return dot(ps, ps) - psdotdir*psdotdir/dot(lineSeg.getDirection(), lineSeg.getDirection());
}
// make it symmetric
template<typename T>
inline T
distSqr(const SGVec3<T>& p, const SGLineSegment<T>& lineSeg)
{ return distSqr(lineSeg, p); }
// with sqrt
template<typename T>
inline T
dist(const SGVec3<T>& p, const SGLineSegment<T>& lineSeg)
{ return sqrt(distSqr(lineSeg, p)); }
template<typename T>
inline T
dist(const SGLineSegment<T>& lineSeg, const SGVec3<T>& p)
{ return sqrt(distSqr(lineSeg, p)); }
template<typename T>
inline bool
intersects(const SGRay<T>& ray, const SGSphere<T>& sphere)
{
// See Tomas Akeniene - Moeller/Eric Haines: Real Time Rendering,
// second edition, page 571
SGVec3<T> l = sphere.getCenter() - ray.getOrigin();
T s = dot(l, ray.getDirection());
T l2 = dot(l, l);
T r2 = sphere.getRadius2();
if (s < 0 && l2 > r2)
return false;
T d2 = dot(ray.getDirection(), ray.getDirection());
// The original test would read
// T m2 = l2 - s*s/d2;
// if (m2 > r2)
// return false;
// but to avoid the expensive division, we multiply by d2
T m2 = d2*l2 - s*s;
if (m2 > d2*r2)
return false;
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(const SGSphere<T>& sphere, const SGRay<T>& ray)
{ return intersects(ray, sphere); }
template<typename T>
inline bool
intersects(const SGLineSegment<T>& lineSegment, const SGSphere<T>& sphere)
{
// See Tomas Akeniene - Moeller/Eric Haines: Real Time Rendering,
// second edition, page 571
SGVec3<T> l = sphere.getCenter() - lineSegment.getStart();
T ld = length(lineSegment.getDirection());
T s = dot(l, lineSegment.getDirection())/ld;
T l2 = dot(l, l);
T r2 = sphere.getRadius2();
if (s < 0 && l2 > r2)
return false;
T m2 = l2 - s*s;
if (m2 > r2)
return false;
T q = sqrt(r2 - m2);
T t = s - q;
if (ld < t)
return false;
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(const SGSphere<T>& sphere, const SGLineSegment<T>& lineSegment)
{ return intersects(lineSegment, sphere); }
template<typename T>
inline bool
// FIXME do not use that default argument later. Just for development now
intersects(SGVec3<T>& x, const SGTriangle<T>& tri, const SGRay<T>& ray, T eps = 0)
{
// See Tomas Akeniene - Moeller/Eric Haines: Real Time Rendering
// Method based on the observation that we are looking for a
// point x that can be expressed in terms of the triangle points
// x = v_0 + u*(v_1 - v_0) + v*(v_2 - v_0)
// with 0 <= u, v and u + v <= 1.
// OTOH it could be expressed in terms of the ray
// x = o + t*d
// Now we can compute u, v and t.
SGVec3<T> p = cross(ray.getDirection(), tri.getEdge(1));
T denom = dot(p, tri.getEdge(0));
T signDenom = copysign(1, denom);
SGVec3<T> s = ray.getOrigin() - tri.getBaseVertex();
SGVec3<T> q = cross(s, tri.getEdge(0));
// Now t would read
// t = 1/denom*dot(q, tri.getEdge(1));
// To avoid an expensive division we multiply by |denom|
T tDenom = signDenom*dot(q, tri.getEdge(1));
if (tDenom < 0)
return false;
// For line segment we would test against
// if (1 < t)
// return false;
// with the original t. The multiplied test would read
// if (absDenom < tDenom)
// return false;
T absDenom = fabs(denom);
T absDenomEps = absDenom*eps;
// T u = 1/denom*dot(p, s);
T u = signDenom*dot(p, s);
if (u < -absDenomEps)
return false;
// T v = 1/denom*dot(q, d);
// if (v < -eps)
// return false;
T v = signDenom*dot(q, ray.getDirection());
if (v < -absDenomEps)
return false;
if (u + v > absDenom + absDenomEps)
return false;
// return if paralell ??? FIXME what if paralell and in plane?
// may be we are ok below than anyway??
if (absDenom <= SGLimits<T>::min())
return false;
x = ray.getOrigin();
// if we have survived here it could only happen with denom == 0
// that the point is already in plane. Then return the origin ...
if (SGLimitsd::min() < absDenom)
x += (tDenom/absDenom)*ray.getDirection();
return true;
}
template<typename T>
inline bool
intersects(const SGTriangle<T>& tri, const SGRay<T>& ray, T eps = 0)
{
// FIXME: for now just wrap the other method. When that has prooven
// well optimized, implement that special case
SGVec3<T> dummy;
return intersects(dummy, tri, ray, eps);
}
template<typename T>
inline bool
// FIXME do not use that default argument later. Just for development now
intersects(SGVec3<T>& x, const SGTriangle<T>& tri, const SGLineSegment<T>& lineSegment, T eps = 0)
{
// See Tomas Akeniene - Moeller/Eric Haines: Real Time Rendering
// Method based on the observation that we are looking for a
// point x that can be expressed in terms of the triangle points
// x = v_0 + u*(v_1 - v_0) + v*(v_2 - v_0)
// with 0 <= u, v and u + v <= 1.
// OTOH it could be expressed in terms of the lineSegment
// x = o + t*d
// Now we can compute u, v and t.
SGVec3<T> p = cross(lineSegment.getDirection(), tri.getEdge(1));
T denom = dot(p, tri.getEdge(0));
T signDenom = copysign(1, denom);
SGVec3<T> s = lineSegment.getStart() - tri.getBaseVertex();
SGVec3<T> q = cross(s, tri.getEdge(0));
// Now t would read
// t = 1/denom*dot(q, tri.getEdge(1));
// To avoid an expensive division we multiply by |denom|
T tDenom = signDenom*dot(q, tri.getEdge(1));
if (tDenom < 0)
return false;
// For line segment we would test against
// if (1 < t)
// return false;
// with the original t. The multiplied test reads
T absDenom = fabs(denom);
if (absDenom < tDenom)
return false;
// take the CPU accuracy in account
T absDenomEps = absDenom*eps;
// T u = 1/denom*dot(p, s);
T u = signDenom*dot(p, s);
if (u < -absDenomEps)
return false;
// T v = 1/denom*dot(q, d);
// if (v < -eps)
// return false;
T v = signDenom*dot(q, lineSegment.getDirection());
if (v < -absDenomEps)
return false;
if (u + v > absDenom + absDenomEps)
return false;
// return if paralell ??? FIXME what if paralell and in plane?
// may be we are ok below than anyway??
if (absDenom <= SGLimits<T>::min())
return false;
x = lineSegment.getStart();
// if we have survived here it could only happen with denom == 0
// that the point is already in plane. Then return the origin ...
if (SGLimitsd::min() < absDenom)
x += (tDenom/absDenom)*lineSegment.getDirection();
return true;
}
template<typename T>
inline bool
intersects(const SGTriangle<T>& tri, const SGLineSegment<T>& lineSegment, T eps = 0)
{
// FIXME: for now just wrap the other method. When that has prooven
// well optimized, implement that special case
SGVec3<T> dummy;
return intersects(dummy, tri, lineSegment, eps);
}
template<typename T>
inline SGVec3<T>
closestPoint(const SGTriangle<T>& tri, const SGVec3<T>& p)
{
// This method minimizes the distance function Q(u, v) = || p - x ||
// where x is a point in the trialgle given by the vertices v_i
// x = v_0 + u*(v_1 - v_0) + v*(v_2 - v_0)
// The theoretical analysis is somehow too long for a comment.
// May be it is sufficient to see that this code passes all the tests.
SGVec3<T> off = tri.getBaseVertex() - p;
T a = dot(tri.getEdge(0), tri.getEdge(0));
T b = dot(tri.getEdge(0), tri.getEdge(1));
T c = dot(tri.getEdge(1), tri.getEdge(1));
T d = dot(tri.getEdge(0), off);
T e = dot(tri.getEdge(1), off);
T det = a*c - b*b;
T u = b*e - c*d;
T v = b*d - a*e;
/*
// Regions
// \2|
// \|
// |\
// 3 |0\ 1
//----------
// 4 | 5 \ 6
*/
if (u + v <= det) {
if (u < 0) {
if (v < 0) {
// region 4
if (d < 0) {
if (a <= -d) {
// u = 1;
// v = 0;
return tri.getBaseVertex() + tri.getEdge(0);
} else {
u = -d/a;
// v = 0;
return tri.getBaseVertex() + u*tri.getEdge(0);
}
} else {
if (0 < e) {
// u = 0;
// v = 0;
return tri.getBaseVertex();
} else if (c <= -e) {
// u = 0;
// v = 1;
return tri.getBaseVertex() + tri.getEdge(1);
} else {
// u = 0;
v = -e/c;
return tri.getBaseVertex() + v*tri.getEdge(1);
}
}
} else {
// region 3
if (0 <= e) {
// u = 0;
// v = 0;
return tri.getBaseVertex();
} else if (c <= -e) {
// u = 0;
// v = 1;
return tri.getBaseVertex() + tri.getEdge(1);
} else {
// u = 0;
v = -e/c;
return tri.getBaseVertex() + v*tri.getEdge(1);
}
}
} else if (v < 0) {
// region 5
if (0 <= d) {
// u = 0;
// v = 0;
return tri.getBaseVertex();
} else if (a <= -d) {
// u = 1;
// v = 0;
return tri.getBaseVertex() + tri.getEdge(0);
} else {
u = -d/a;
// v = 0;
return tri.getBaseVertex() + u*tri.getEdge(0);
}
} else {
// region 0
if (det <= SGLimits<T>::min()) {
u = 0;
v = 0;
return tri.getBaseVertex();
} else {
T invDet = 1/det;
u *= invDet;
v *= invDet;
return tri.getBaseVertex() + u*tri.getEdge(0) + v*tri.getEdge(1);
}
}
} else {
if (u < 0) {
// region 2
T tmp0 = b + d;
T tmp1 = c + e;
if (tmp0 < tmp1) {
T numer = tmp1 - tmp0;
T denom = a - 2*b + c;
if (denom <= numer) {
// u = 1;
// v = 0;
return tri.getBaseVertex() + tri.getEdge(0);
} else {
u = numer/denom;
v = 1 - u;
return tri.getBaseVertex() + u*tri.getEdge(0) + v*tri.getEdge(1);
}
} else {
if (tmp1 <= 0) {
// u = 0;
// v = 1;
return tri.getBaseVertex() + tri.getEdge(1);
} else if (0 <= e) {
// u = 0;
// v = 0;
return tri.getBaseVertex();
} else {
// u = 0;
v = -e/c;
return tri.getBaseVertex() + v*tri.getEdge(1);
}
}
} else if (v < 0) {
// region 6
T tmp0 = b + e;
T tmp1 = a + d;
if (tmp0 < tmp1) {
T numer = tmp1 - tmp0;
T denom = a - 2*b + c;
if (denom <= numer) {
// u = 0;
// v = 1;
return tri.getBaseVertex() + tri.getEdge(1);
} else {
v = numer/denom;
u = 1 - v;
return tri.getBaseVertex() + u*tri.getEdge(0) + v*tri.getEdge(1);
}
} else {
if (tmp1 < 0) {
// u = 1;
// v = 0;
return tri.getBaseVertex() + tri.getEdge(0);
} else if (0 <= d) {
// u = 0;
// v = 0;
return tri.getBaseVertex();
} else {
u = -d/a;
// v = 0;
return tri.getBaseVertex() + u*tri.getEdge(0);
}
}
} else {
// region 1
T numer = c + e - b - d;
if (numer <= 0) {
// u = 0;
// v = 1;
return tri.getVertex(2);
} else {
T denom = a - 2*b + c;
if (denom <= numer) {
// u = 1;
// v = 0;
return tri.getBaseVertex() + tri.getEdge(0);
} else {
u = numer/denom;
v = 1 - u;
return tri.getBaseVertex() + u*tri.getEdge(0) + v*tri.getEdge(1);
}
}
}
}
}
template<typename T>
inline SGVec3<T>
closestPoint(const SGVec3<T>& p, const SGTriangle<T>& tri)
{ return closestPoint(tri, p); }
template<typename T, typename T2>
inline bool
intersects(const SGTriangle<T>& tri, const SGSphere<T2>& sphere)
{
// This method minimizes the distance function Q(u, v) = || p - x ||
// where x is a point in the trialgle given by the vertices v_i
// x = v_0 + u*(v_1 - v_0) + v*(v_2 - v_0)
// The theoretical analysis is somehow too long for a comment.
// May be it is sufficient to see that this code passes all the tests.
SGVec3<T> off = tri.getBaseVertex() - SGVec3<T>(sphere.getCenter());
T baseDist2 = dot(off, off);
T a = dot(tri.getEdge(0), tri.getEdge(0));
T b = dot(tri.getEdge(0), tri.getEdge(1));
T c = dot(tri.getEdge(1), tri.getEdge(1));
T d = dot(tri.getEdge(0), off);
T e = dot(tri.getEdge(1), off);
T det = a*c - b*b;
T u = b*e - c*d;
T v = b*d - a*e;
/*
// Regions
// \2|
// \|
// |\
// 3 |0\ 1
//----------
// 4 | 5 \ 6
*/
if (u + v <= det) {
if (u < 0) {
if (v < 0) {
// region 4
if (d < 0) {
if (a <= -d) {
// u = 1;
// v = 0;
T sqrDist = a + 2*d + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
u = -d/a;
// v = 0;
T sqrDist = d*u + baseDist2;
return sqrDist <= sphere.getRadius2();
}
} else {
if (0 < e) {
// u = 0;
// v = 0;
return baseDist2 <= sphere.getRadius2();
} else if (c <= -e) {
// u = 0;
// v = 1;
T sqrDist = c + 2*e + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
// u = 0;
v = -e/c;
T sqrDist = e*v + baseDist2;
return sqrDist <= sphere.getRadius2();
}
}
} else {
// region 3
if (0 <= e) {
// u = 0;
// v = 0;
return baseDist2 <= sphere.getRadius2();
} else if (c <= -e) {
// u = 0;
// v = 1;
T sqrDist = c + 2*e + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
// u = 0;
v = -e/c;
T sqrDist = e*v + baseDist2;
return sqrDist <= sphere.getRadius2();
}
}
} else if (v < 0) {
// region 5
if (0 <= d) {
// u = 0;
// v = 0;
return baseDist2 <= sphere.getRadius2();
} else if (a <= -d) {
// u = 1;
// v = 0;
T sqrDist = a + 2*d + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
u = -d/a;
// v = 0;
T sqrDist = d*u + baseDist2;
return sqrDist <= sphere.getRadius2();
}
} else {
// region 0
if (det <= SGLimits<T>::min()) {
// sqrDist = baseDist2;
u = 0;
v = 0;
return baseDist2 <= sphere.getRadius2();
} else {
T invDet = 1/det;
u *= invDet;
v *= invDet;
T sqrDist = u*(a*u + b*v + 2*d) + v*(b*u + c*v + 2*e) + baseDist2;
return sqrDist <= sphere.getRadius2();
}
}
} else {
if (u < 0) {
// region 2
T tmp0 = b + d;
T tmp1 = c + e;
if (tmp0 < tmp1) {
T numer = tmp1 - tmp0;
T denom = a - 2*b + c;
if (denom <= numer) {
// u = 1;
// v = 0;
T sqrDist = a + 2*d + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
u = numer/denom;
v = 1 - u;
T sqrDist = u*(a*u + b*v + 2*d) + v*(b*u + c*v + 2*e) + baseDist2;
return sqrDist <= sphere.getRadius2();
}
} else {
if (tmp1 <= 0) {
// u = 0;
// v = 1;
T sqrDist = c + 2*e + baseDist2;
return sqrDist <= sphere.getRadius2();
} else if (0 <= e) {
// u = 0;
// v = 0;
return baseDist2 <= sphere.getRadius2();
} else {
// u = 0;
v = -e/c;
T sqrDist = e*v + baseDist2;
return sqrDist <= sphere.getRadius2();
}
}
} else if (v < 0) {
// region 6
T tmp0 = b + e;
T tmp1 = a + d;
if (tmp0 < tmp1) {
T numer = tmp1 - tmp0;
T denom = a - 2*b + c;
if (denom <= numer) {
// u = 0;
// v = 1;
T sqrDist = c + 2*e + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
v = numer/denom;
u = 1 - v;
T sqrDist = u*(a*u + b*v + 2*d) + v*(b*u + c*v + 2*e)+baseDist2;
return sqrDist <= sphere.getRadius2();
}
} else {
if (tmp1 < 0) {
// u = 1;
// v = 0;
T sqrDist = a + 2*d + baseDist2;
return sqrDist <= sphere.getRadius2();
} else if (0 <= d) {
// sqrDist = baseDist2;
// u = 0;
// v = 0;
return baseDist2 <= sphere.getRadius2();
} else {
u = -d/a;
// v = 0;
T sqrDist = d*u + baseDist2;
return sqrDist <= sphere.getRadius2();
}
}
} else {
// region 1
T numer = c + e - b - d;
if (numer <= 0) {
// u = 0;
// v = 1;
T sqrDist = c + 2*e + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
T denom = a - 2*b + c;
if (denom <= numer) {
// u = 1;
// v = 0;
T sqrDist = a + 2*d + baseDist2;
return sqrDist <= sphere.getRadius2();
} else {
u = numer/denom;
v = 1 - u;
T sqrDist = u*(a*u + b*v + 2*d) + v*(b*u + c*v + 2*e) + baseDist2;
return sqrDist <= sphere.getRadius2();
}
}
}
}
}
template<typename T1, typename T2>
inline bool
intersects(const SGSphere<T1>& sphere, const SGTriangle<T2>& tri)
{ return intersects(tri, sphere); }
template<typename T>
inline bool
intersects(const SGVec3<T>& v, const SGSphere<T>& sphere)
{
if (sphere.empty())
return false;
return distSqr(v, sphere.getCenter()) <= sphere.getRadius2();
}
template<typename T>
inline bool
intersects(const SGSphere<T>& sphere, const SGVec3<T>& v)
{ return intersects(v, sphere); }
template<typename T>
inline bool
intersects(const SGBox<T>& box, const SGLineSegment<T>& lineSegment)
{
// See Tomas Akeniene - Moeller/Eric Haines: Real Time Rendering
SGVec3<T> c = lineSegment.getCenter() - box.getCenter();
SGVec3<T> w = T(0.5)*lineSegment.getDirection();
SGVec3<T> v(fabs(w.x()), fabs(w.y()), fabs(w.z()));
SGVec3<T> h = T(0.5)*box.getSize();
if (fabs(c[0]) > v[0] + h[0])
return false;
if (fabs(c[1]) > v[1] + h[1])
return false;
if (fabs(c[2]) > v[2] + h[2])
return false;
if (fabs(c[1]*w[2] - c[2]*w[1]) > h[1]*v[2] + h[2]*v[1])
return false;
if (fabs(c[0]*w[2] - c[2]*w[0]) > h[0]*v[2] + h[2]*v[0])
return false;
if (fabs(c[0]*w[1] - c[1]*w[0]) > h[0]*v[1] + h[1]*v[0])
return false;
return true;
}
template<typename T>
inline bool
intersects(const SGLineSegment<T>& lineSegment, const SGBox<T>& box)
{ return intersects(box, lineSegment); }
template<typename T>
inline bool
intersects(const SGBox<T>& box, const SGRay<T>& ray)
{
// See Tomas Akeniene - Moeller/Eric Haines: Real Time Rendering
for (unsigned i = 0; i < 3; ++i) {
T cMin = box.getMin()[i];
T cMax = box.getMax()[i];
T cOrigin = ray.getOrigin()[i];
T cDir = ray.getDirection()[i];
if (fabs(cDir) <= SGLimits<T>::min()) {
if (cOrigin < cMin)
return false;
if (cMax < cOrigin)
return false;
}
T nearr = - SGLimits<T>::max();
T farr = SGLimits<T>::max();
T T1 = (cMin - cOrigin) / cDir;
T T2 = (cMax - cOrigin) / cDir;
if (T1 > T2) std::swap (T1, T2);/* since T1 intersection with near plane */
if (T1 > nearr) nearr = T1; /* want largest Tnear */
if (T2 < farr) farr = T2; /* want smallest Tfarr */
if (nearr > farr) // farr box is missed
return false;
if (farr < 0) // box is behind ray
return false;
}
return true;
}
// make it symmetric
template<typename T>
inline bool
intersects(const SGRay<T>& ray, const SGBox<T>& box)
{ return intersects(box, ray); }
template<typename T1, typename T2>
inline bool
intersects(const SGBox<T1>& box1, const SGBox<T2>& box2)
{
if (box2.getMax()[0] < box1.getMin()[0])
return false;
if (box1.getMax()[0] < box2.getMin()[0])
return false;
if (box2.getMax()[1] < box1.getMin()[1])
return false;
if (box1.getMax()[1] < box2.getMin()[1])
return false;
if (box2.getMax()[2] < box1.getMin()[2])
return false;
if (box1.getMax()[2] < box2.getMin()[2])
return false;
return true;
}
#endif
|