GeometryTools.cc 50.4 KB
 Praetorius, Simon committed Aug 02, 2012 1 2 3 4 ``````#include "GeometryTools.h" #define EPSILON FLT_EPSILON `````` 5 ``````namespace meshconv2 { `````` Praetorius, Simon committed Aug 02, 2012 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 `````` // ############### // ## General ## // ############### //----------------------------------------< solve_determinant4 > double solve_determinant4(double x11, double x12, double x13, double x14, double x21, double x22, double x23, double x24, double x31, double x32, double x33, double x34, double x41, double x42, double x43, double x44){ return x11*(x22*x33*x44 + x23*x34*x42 + x24*x32*x43 - x24*x33*x42 - x23*x32*x44 - x22*x34*x43) -x12*(x21*x33*x44 + x23*x34*x41 + x24*x31*x43 - x24*x33*x41 - x23*x31*x44 - x21*x34*x43) +x13*(x21*x32*x44 + x22*x34*x41 + x24*x31*x42 - x24*x32*x41 - x22*x31*x44 - x21*x34*x42) -x14*(x21*x32*x43 + x22*x33*x41 + x23*x31*x42 - x23*x32*x41 - x22*x31*x43 - x21*x33*x42); } `````` Praetorius, Simon committed Aug 07, 2012 22 23 24 25 26 27 28 ``````double solve_determinant3(double x11, double x12, double x13, double x21, double x22, double x23, double x31, double x32, double x33){ return (x11*x22*x33 + x12*x23*x31 + x13*x21*x32) -(x13*x22*x31 + x12*x21*x33 + x11*x23*x32); } `````` Praetorius, Simon committed Aug 02, 2012 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 ``````// ################# // ## 2D Worlds ## // ################# //----------------------------------------< edge_length_2d > double edge_length_2d(double lin0[], double lin1[]){ long i; double p[2]; for(i=0; i<2; ++i) p[i] = lin0[i] - lin1[i]; return sqrt(p[0] * p[0] + p[1] * p[1]); } //----------------------------------------< boundingbox_triangle_2d > void boundingbox_triangle_2d(double tri0[], double tri1[], double tri2[], double min_corner[], double max_corner[]){ min_corner[0] = std::min(std::min(tri0[0],tri1[0]),tri2[0]); min_corner[1] = std::min(std::min(tri0[1],tri1[1]),tri2[1]); max_corner[0] = std::max(std::max(tri0[0],tri1[0]),tri2[0]); max_corner[1] = std::max(std::max(tri0[1],tri1[1]),tri2[1]); } //----------------------------------------< centroid_of_box_2d > void centroid_of_box_2d(double min_corner[], double max_corner[], double c[]){ c[0] = 0.5*(max_corner[0]+min_corner[0]); c[1] = 0.5*(max_corner[1]+min_corner[1]); } //----------------------------------------< centroid_of_triangle_2d > void centroid_of_triangle_2d(double tri0[], double tri1[], double tri2[], double c[]){ c[0] = (tri0[0]+tri1[0],tri2[0])/3.0; c[1] = (tri0[1]+tri1[1],tri2[1])/3.0; } //----------------------------------------< point_in_triangle_2d > bool point_in_triangle_2d(double p[], double a[], double b[], double c[]){ long i; double v0[2], v1[2], v2[2]; double dot00, dot01, dot02, dot11, dot12, invDenom, u, v; //compute direction vectors for(i=0; i<2; i++) v0[i] = c[i] - a[i]; for(i=0; i<2; i++) v1[i] = b[i] - a[i]; for(i=0; i<2; i++) v2[i] = p[i] - a[i]; //compute dot products dot00 = v0[0]*v0[0] + v0[1]*v0[1]; //dot(v0, v0) dot01 = v0[0]*v1[0] + v0[1]*v1[1]; //dot(v0, v1) dot02 = v0[0]*v2[0] + v0[1]*v2[1]; //dot(v0, v2) dot11 = v1[0]*v1[0] + v1[1]*v1[1]; //dot(v1, v1) dot12 = v1[0]*v2[0] + v1[1]*v2[1]; //dot(v1, v2) //compute barycentric coordinates invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01); u = (dot11 * dot02 - dot01 * dot12) * invDenom; v = (dot00 * dot12 - dot01 * dot02) * invDenom; //check if point is in triangle return (u >= 0.0) && (v >= 0.0) && (u + v <= 1.0); } //----------------------------------------< point_in_triangle_generous_2d > bool point_in_triangle_generous_2d(double p[], double a[], double b[], double c[]){ long i; double v0[2], v1[2], v2[2]; double dot00, dot01, dot02, dot11, dot12, invDenom, u, v; //compute direction vectors for(i=0; i<2; i++) v0[i] = c[i] - a[i]; for(i=0; i<2; i++) v1[i] = b[i] - a[i]; for(i=0; i<2; i++) v2[i] = p[i] - a[i]; //compute dot products dot00 = v0[0]*v0[0] + v0[1]*v0[1]; //dot(v0, v0) dot01 = v0[0]*v1[0] + v0[1]*v1[1]; //dot(v0, v1) dot02 = v0[0]*v2[0] + v0[1]*v2[1]; //dot(v0, v2) dot11 = v1[0]*v1[0] + v1[1]*v1[1]; //dot(v1, v1) dot12 = v1[0]*v2[0] + v1[1]*v2[1]; //dot(v1, v2) //compute barycentric coordinates invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01); u = (dot11 * dot02 - dot01 * dot12) * invDenom; v = (dot00 * dot12 - dot01 * dot02) * invDenom; //check if point is in triangle return (u >= -EPSILON) && (v >= -EPSILON) && (u + v - 1.0 <= EPSILON); } //----------------------------------------< point_in_box_2d > bool point_in_box_2d(double p[], double min_corner[], double max_corner[]){ return (p[0] >= min_corner[0]) && (p[0] <= max_corner[0]) && (p[1] >= min_corner[1]) && (p[1] <= max_corner[1]); } //----------------------------------------< point_in_box_generous_2d > bool point_in_box_generous_2d(double p[], double min_corner[], double max_corner[]){ return (p[0]-min_corner[0] >= -EPSILON) && (p[0]-max_corner[0] <= EPSILON) && (p[1]-min_corner[1] >= -EPSILON) && (p[1]-max_corner[1] <= EPSILON); } //----------------------------------------< intersection_line_line_2d > //tests whether the line segment defined by end points 'lin0' and 'lin1' intersects the connection //between lamp and new_dof. If the line segment is intersected in one of its vertices the index of that //vertex (0 or 1) is returned in hit_vertex otherwise hit_vertex is set to -1. bool intersection_line_line_2d(double lin0[], double lin1[], double lamp[], double new_dof[], long &hit_vertex){ long i; double x=-1.0, y=-1.0, tmp; double p3[2], q3[2]; hit_vertex = -1; for(i=0; i<2; ++i){ p3[i] = lin1[i] - lin0[i]; q3[i] = new_dof[i] - lamp[i]; } //partial coincidence of small element and center-connection is a special case, it does not count as an //intersection tmp = p3[0]*q3[1] - p3[1]*q3[0]; if(abs(tmp) > EPSILON){ //no partial coincidence; test for intersection if(abs(q3[0]) < EPSILON){ if(abs(p3[0]) > EPSILON){ x = (lamp[0] - lin0[0]) / p3[0]; y = (lamp[1] - lin0[1] - p3[1]*x) / -q3[1]; } } else if(abs(q3[1]) < EPSILON){ if(abs(p3[1]) > EPSILON){ x = (lamp[1] - lin0[1]) / p3[1]; y = (lamp[0] - lin0[0] - p3[0]*x) / -q3[0]; } } else{ if(abs(tmp) > EPSILON){ x = ((lamp[0]-lin0[0])*q3[1] - (lamp[1]-lin0[1])*q3[0]) / tmp; y = (lamp[0] - lin0[0] - p3[0]*x) / -q3[0]; } } if(x>-EPSILON && x-1-EPSILON && y-1<-EPSILON){ if(x-EPSILON) hit_vertex = 1; return true; } } return false; } //----------------------------------------< intersection_line_triangle_2d > bool intersection_line_triangle_2d(double lin0[], double lin1[], double tri0[], double tri1[], double tri2[]){ if(point_in_triangle_generous_2d(lin0, tri0, tri1, tri2)){ return true; } if(point_in_triangle_generous_2d(lin1, tri0, tri1, tri2)){ return true; } long hit_vertex; if(intersection_line_line_2d(tri0, tri1, lin0, lin1, hit_vertex)){ return true; } if(intersection_line_line_2d(tri1, tri2, lin0, lin1, hit_vertex)){ return true; } if(intersection_line_line_2d(tri2, tri0, lin0, lin1, hit_vertex)){ return true; } return false; } //----------------------------------------< intersection_line_box_2d > bool intersection_line_box_2d(double lin0[], double lin1[], double min_corner[], double max_corner[]){ double p0[2]; double p1[2]; p0[0] = min_corner[0]; p0[1] = max_corner[1]; p1[0] = max_corner[0]; p1[1] = min_corner[1]; long hit_vertex; if (intersection_line_line_2d(lin0,lin1,min_corner,p0,hit_vertex)) return true; if (intersection_line_line_2d(lin0,lin1,min_corner,p1,hit_vertex)) return true; if (intersection_line_line_2d(lin0,lin1,max_corner,p0,hit_vertex)) return true; if (intersection_line_line_2d(lin0,lin1,max_corner,p1,hit_vertex)) return true; return false; } //----------------------------------------< intersection_box_box_2d > bool intersection_box_box_2d(double min_corner0[], double max_corner0[], double min_corner1[], double max_corner1[]){ double p[2]; if (edge_length_2d(min_corner0,max_corner0) < edge_length_2d(min_corner1,max_corner1)) { // box0 is smaller than box1 if (point_in_box_2d(min_corner0, min_corner1, max_corner1)) return true; if (point_in_box_2d(max_corner0, min_corner1, max_corner1)) return true; p[0] = min_corner0[0]; p[1] = max_corner0[1]; if (point_in_box_2d(p, min_corner1, max_corner1)) return true; p[0] = max_corner0[0]; p[1] = min_corner0[1]; if (point_in_box_2d(p, min_corner1, max_corner1)) return true; } else { // box1 is smaller than box0 if (point_in_box_2d(min_corner1, min_corner0, max_corner0)) return true; if (point_in_box_2d(max_corner1, min_corner0, max_corner0)) return true; p[0] = min_corner1[0]; p[1] = max_corner1[1]; if (point_in_box_2d(p, min_corner0, max_corner0)) return true; p[0] = max_corner1[0]; p[1] = min_corner1[1]; if (point_in_box_2d(p, min_corner0, max_corner0)) return true; } `````` Thomas Witkowski committed Aug 16, 2012 262 263 `````` return false; `````` Praetorius, Simon committed Aug 02, 2012 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 ``````} //----------------------------------------< intersection_triangle_box_2d > bool intersection_triangle_box_2d(double tri0[], double tri1[], double tri2[], double min_corner[], double max_corner[]){ // vertices of triangle covered by box if (point_in_box_generous_2d(tri0,min_corner,max_corner)) return true; if (point_in_box_generous_2d(tri1,min_corner,max_corner)) return true; if (point_in_box_generous_2d(tri2,min_corner,max_corner)) return true; // bounding box of triangle does not intersect the box double p0[2]; double p1[2]; boundingbox_triangle_2d(tri0,tri1,tri2,p0,p1); if (!intersection_box_box_2d(min_corner,max_corner,p0,p1)) return false; // vertices of box covered by triangle if (point_in_triangle_generous_2d(min_corner, tri0, tri1, tri2)) return true; if (point_in_triangle_generous_2d(max_corner, tri0, tri1, tri2)) return true; p0[0] = min_corner[0]; p0[1] = max_corner[1]; if (point_in_triangle_generous_2d(p0, tri0, tri1, tri2)) return true; p1[0] = max_corner[0]; p1[1] = min_corner[1]; if (point_in_triangle_generous_2d(p1, tri0, tri1, tri2)) return true; // edges of triangle intersect box if (intersection_line_box_2d(tri0,tri1,min_corner,max_corner)) return true; if (intersection_line_box_2d(tri1,tri2,min_corner,max_corner)) return true; if (intersection_line_box_2d(tri2,tri0,min_corner,max_corner)) return true; return false; } //----------------------------------------< distance_point_line_2d > //calculates the distance between a given point and the line segment between 'lin0' and 'lin1'. double distance_point_line_2d(double point[], double lin0[], double lin1[]){ double gx, gy, fx, fy, dx, dy, t; //the (complete) line through 'lin0' and 'lin1' is given by 'l = lin0 + t*g' with g as follows: gx = lin1[0] - lin0[0]; gy = lin1[1] - lin0[1]; //the normal-vector from 'point' to l is 'v = lin0 + t*g - point' for some t which is determined // by 'v*g = 0' t = (-gx * (lin0[0]-point[0]) - gy * (lin0[1]-point[1])) / (gx*gx + gy*gy); //if the orthogonal projection of 'point' onto l is not on the given line segment then one of the //vertices 'lin0' or 'lin1' is the nearest point to 'point' if(t<0){ //'lin0' is nearest fx = lin0[0]; fy = lin0[1]; } else if(t>1){ //'lin1' is nearest fx = lin1[0]; fy = lin1[1]; } else{ //orthogonal projection is nearest fx = lin0[0] + t * gx; fy = lin0[1] + t * gy; } //calculate distance dx = point[0] - fx; dy = point[1] - fy; return sqrt(dx*dx + dy*dy); } //----------------------------------------< distance_point_line_with_intersection_2d > //calculates the distance between a given point and the line-segment between 'lin0' and 'lin1'. //This version also returns the intersection-point of the perpendicular with the given line-segment. double distance_point_line_with_intersection_2d(double point[], double lin0[], double lin1[], double intersection[]){ double gx, gy, dx, dy, t; //the (complete) line through 'lin0' and 'lin1' is given by 'l = lin0 + t*g' with g as follows: gx = lin1[0] - lin0[0]; gy = lin1[1] - lin0[1]; //the normal-vector from 'point' to l is 'v = lin0 + t*g - point' for some t which is determined // by 'v*g = 0' t = (-gx * (lin0[0]-point[0]) - gy * (lin0[1]-point[1])) / (gx*gx + gy*gy); //if the orthogonal projection of 'point' onto l is not on the given line segment then one of the //vertices 'lin0' or 'lin1' is the nearest point to 'point' if(t<0){ //'lin0' is nearest intersection[0] = lin0[0]; intersection[1] = lin0[1]; } else if(t>1){ //'lin1' is nearest intersection[0] = lin1[0]; intersection[1] = lin1[1]; } else{ //orthogonal projection is nearest intersection[0] = lin0[0] + t * gx; intersection[1] = lin0[1] + t * gy; } //calculate distance dx = point[0] - intersection[0]; dy = point[1] - intersection[1]; return sqrt(dx*dx + dy*dy); } //----------------------------------------< distance_point_box_2d > double distance_point_box_2d(double point[], double min_corner[], double max_corner[]){ if (point_in_box_2d(point,min_corner,max_corner)) return 0.0; double p[2]; double dist = edge_length_2d(point, min_corner); dist = std::min(dist, edge_length_2d(point, min_corner)); p[0] = min_corner[0]; p[1] = max_corner[1]; dist = std::min(dist, edge_length_2d(point, p)); p[0] = max_corner[0]; p[1] = min_corner[1]; dist = std::min(dist, edge_length_2d(point, p)); return dist; } //----------------------------------------< distance_point_triangle_2d > double distance_point_triangle_2d(double point[], double tri0[], double tri1[], double tri2[]){ if (point_in_triangle_2d(point,tri0,tri1,tri2)) return 0.0; double dist = edge_length_2d(point, tri0); dist = std::min(dist, edge_length_2d(point, tri1)); dist = std::min(dist, edge_length_2d(point, tri2)); return dist; } bool point_in_polygon(double point[], const std::vector > &vertices) { bool inside = false; `````` Praetorius, Simon committed Mar 05, 2013 415 `````` size_t i, j; `````` Praetorius, Simon committed Aug 02, 2012 416 417 418 419 420 421 422 423 424 425 `````` for (i = 0, j = vertices.size()-1; i < vertices.size(); j = i++) { if ((((vertices[i][1] <= point[1]) && (point[1] < vertices[j][1])) || ((vertices[j][1] <= point[1]) && (point[1] < vertices[i][1]))) && (point[0] < (vertices[j][0] - vertices[i][0]) * (point[1] - vertices[i][1]) / (vertices[j][1] - vertices[i][1]) + vertices[i][0])) inside = !inside; } return inside; } `````` Praetorius, Simon committed Aug 04, 2012 426 427 428 429 430 ``````//----------------------------------------< triangle_area_2d > double triangle_area_2d(double tri0[], double tri1[], double tri2[]){ return 0.5 * abs((tri1[0] - tri0[0]) * (tri2[1] - tri0[1]) - (tri2[0] - tri0[0]) * (tri1[1] - tri0[1])); } `````` Praetorius, Simon committed Aug 02, 2012 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 ``````// ################# // ## 3D Worlds ## // ################# //----------------------------------------< points_identical_3d > bool points_identical_3d(double p1[], double p2[]){ long i; for(i=0; i<3; ++i){ if(abs(p1[i] - p2[i]) > EPSILON) break; } if(i == 3){ return true; }else{ return false; } } //----------------------------------------< normal_vector_3d > void normal_vector_3d(double tri0[], double tri1[], double tri2[], double normal[]){ long i; double p[3], q[3]; for(i=0; i<3; ++i) p[i] = tri0[i] - tri1[i]; for(i=0; i<3; ++i) q[i] = tri0[i] - tri2[i]; normal[0] = p[1] * q[2] - p[2] * q[1]; normal[1] = p[2] * q[0] - p[0] * q[2]; normal[2] = p[0] * q[1] - p[1] * q[0]; } //----------------------------------------< triangle_area_3d > double triangle_area_3d(double tri0[], double tri1[], double tri2[]){ long i; double p[3], q[3], n[3]; for(i=0; i<3; ++i) p[i] = tri0[i] - tri1[i]; for(i=0; i<3; ++i) q[i] = tri0[i] - tri2[i]; n[0] = p[1] * q[2] - p[2] * q[1]; n[1] = p[2] * q[0] - p[0] * q[2]; n[2] = p[0] * q[1] - p[1] * q[0]; return sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]) / 2; } //----------------------------------------< edge_length_3d > double edge_length_3d(double lin0[], double lin1[]){ long i; double p[3]; for(i=0; i<3; ++i) p[i] = lin0[i] - lin1[i]; return sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]); } //----------------------------------------< triangle_max_edge_length_3d > double triangle_max_edge_length_3d(double tri0[], double tri1[], double tri2[]){ long i; double p[3], max_el, a; for(i=0; i<3; ++i) p[i] = tri0[i] - tri1[i]; a = p[0] * p[0] + p[1] * p[1] + p[2] * p[2]; max_el = a; for(i=0; i<3; ++i) p[i] = tri1[i] - tri2[i]; a = p[0] * p[0] + p[1] * p[1] + p[2] * p[2]; if(a > max_el) max_el = a; for(i=0; i<3; ++i) p[i] = tri2[i] - tri0[i]; a = p[0] * p[0] + p[1] * p[1] + p[2] * p[2]; if(a > max_el) max_el = a; return sqrt(max_el); } //----------------------------------------< unit_normal_vector_3d > void unit_normal_vector_3d(double tri0[], double tri1[], double tri2[], double normal[]){ long i; double b; double p[3], q[3]; for(i=0; i<3; ++i) p[i] = tri0[i] - tri1[i]; for(i=0; i<3; ++i) q[i] = tri0[i] - tri2[i]; normal[0] = p[1] * q[2] - p[2] * q[1]; normal[1] = p[2] * q[0] - p[0] * q[2]; normal[2] = p[0] * q[1] - p[1] * q[0]; b = sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]); for(i=0; i<3; ++i) normal[i] /= b; } //----------------------------------------< degenerate_triangle_3d > bool degenerate_triangle_3d(double tri0[], double tri1[], double tri2[]){ long i; double p[3], q[3], n[3]; for(i=0; i<3; ++i) p[i] = tri0[i] - tri1[i]; for(i=0; i<3; ++i) q[i] = tri0[i] - tri2[i]; n[0] = p[1] * q[2] - p[2] * q[1]; n[1] = p[2] * q[0] - p[0] * q[2]; n[2] = p[0] * q[1] - p[1] * q[0]; if(abs(n[0]) void boundingbox_triangle_3d(double tri0[], double tri1[], double tri2[], double min_corner[], double max_corner[]){ min_corner[0] = std::min(std::min(tri0[0],tri1[0]),tri2[0]); min_corner[1] = std::min(std::min(tri0[1],tri1[1]),tri2[1]); min_corner[2] = std::min(std::min(tri0[2],tri1[2]),tri2[2]); max_corner[0] = std::max(std::max(tri0[0],tri1[0]),tri2[0]); max_corner[1] = std::max(std::max(tri0[1],tri1[1]),tri2[1]); max_corner[2] = std::max(std::max(tri0[2],tri1[2]),tri2[2]); } //----------------------------------------< centroid_of_box_3d > void centroid_of_box_3d(double min_corner[], double max_corner[], double c[]){ c[0] = 0.5*(max_corner[0]+min_corner[0]); c[1] = 0.5*(max_corner[1]+min_corner[1]); c[2] = 0.5*(max_corner[2]+min_corner[2]); } //----------------------------------------< centroid_of_triangle_3d > void centroid_of_triangle_3d(double tri0[], double tri1[], double tri2[], double c[]){ c[0] = (tri0[0]+tri1[0],tri2[0])/3.0; c[1] = (tri0[1]+tri1[1],tri2[1])/3.0; c[2] = (tri0[2]+tri1[2],tri2[2])/3.0; } //----------------------------------------< point_in_box_3d > bool point_in_box_3d(double p[], double min_corner[], double max_corner[]){ return (p[0] >= min_corner[0]) && (p[0] <= max_corner[0]) && (p[1] >= min_corner[1]) && (p[1] <= max_corner[1]) && (p[2] >= min_corner[2]) && (p[2] <= max_corner[2]); } //----------------------------------------< point_in_box_generous_3d > bool point_in_box_generous_3d(double p[], double min_corner[], double max_corner[]){ return (p[0]-min_corner[0] >= -EPSILON) && (p[0]-max_corner[0] <= EPSILON) && (p[1]-min_corner[1] >= -EPSILON) && (p[1]-max_corner[1] <= EPSILON) && (p[2]-min_corner[2] >= -EPSILON) && (p[2]-max_corner[2] <= EPSILON); } //----------------------------------------< point_in_tetrahedron_3d > bool point_in_tetrahedron_3d(double p[], double a[], double b[], double c[], double d[]){ double d0, d1, d2, d3, d4; d0 = solve_determinant4(a[0], a[1], a[2], 1.0, b[0], b[1], b[2], 1.0, c[0], c[1], c[2], 1.0, d[0], d[1], d[2], 1.0); d1 = solve_determinant4(p[0], p[1], p[2], 1.0, b[0], b[1], b[2], 1.0, c[0], c[1], c[2], 1.0, d[0], d[1], d[2], 1.0); d2 = solve_determinant4(a[0], a[1], a[2], 1.0, p[0], p[1], p[2], 1.0, c[0], c[1], c[2], 1.0, d[0], d[1], d[2], 1.0); d3 = solve_determinant4(a[0], a[1], a[2], 1.0, b[0], b[1], b[2], 1.0, p[0], p[1], p[2], 1.0, d[0], d[1], d[2], 1.0); d4 = solve_determinant4(a[0], a[1], a[2], 1.0, b[0], b[1], b[2], 1.0, c[0], c[1], c[2], 1.0, p[0], p[1], p[2], 1.0); //if(d0>=0.0){ // return (d1>=0.0 && d2>=0.0 && d3>=0.0 && d4>=0.0); //}else{ // return (d1<=0.0 && d2<=0.0 && d3<=0.0 && d4<=0.0); //} //the following code is highly experimental and should not be trusted! if(d0>=0.0){ return (d1>=-EPSILON && d2>=-EPSILON && d3>=-EPSILON && d4>=-EPSILON); }else{ return (d1<=EPSILON && d2<=EPSILON && d3<=EPSILON && d4<=EPSILON); } } //----------------------------------------< intersection_line_line_3d > bool intersection_line_line_3d(double p1[], double p2[], double p3[], double p4[]){ double p13[3], p43[3], p21[3]; double d1343, d4321, d1321, d4343, d2121; double numer, denom; double mua, mub; p13[0] = p1[0] - p3[0]; p13[1] = p1[1] - p3[1]; p13[2] = p1[2] - p3[2]; p43[0] = p4[0] - p3[0]; p43[1] = p4[1] - p3[1]; p43[2] = p4[2] - p3[2]; p21[0] = p2[0] - p1[0]; p21[1] = p2[1] - p1[1]; p21[2] = p2[2] - p1[2]; //test for collinearity if( abs(p21[1]*p43[2] - p21[2]*p43[1]) < EPSILON && abs(p21[2]*p43[0] - p21[0]*p43[2]) < EPSILON && abs(p21[0]*p43[1] - p21[1]*p43[0]) < EPSILON ){ //lines are collinear; now test for an intersection double x11, x12, x21, x22, y11, y12, y21, y22, z11, z12, z21, z22; if(p1[0]<=p2[0]){ x11 = p1[0]; x12 = p2[0]; } else{ x11 = p2[0]; x12 = p1[0]; } if(p1[1]<=p2[1]){ y11 = p1[1]; y12 = p2[1]; } else{ y11 = p2[1]; y12 = p1[1]; } if(p1[2]<=p2[2]){ z11 = p1[2]; z12 = p2[2]; } else{ z11 = p2[2]; z12 = p1[2]; } if(p3[0]<=p4[0]){ x21 = p3[0]; x22 = p4[0]; } else{ x21 = p4[0]; x22 = p3[0]; } if(p3[1]<=p4[1]){ y21 = p3[1]; y22 = p4[1]; } else{ y21 = p4[1]; y22 = p3[1]; } if(p3[2]<=p4[2]){ z21 = p3[2]; z22 = p4[2]; } else{ z21 = p4[2]; z22 = p3[2]; } if(x11<=x22 && x12>=x21 && y11<=y22 && y12>=y21 && z11<=z22 && z12>=z21){ if(abs(p21[0]) > EPSILON){ mua = -p13[0]/p21[0]; if(abs(p1[1] + p21[1]*mua - p3[1]) < EPSILON && abs(p1[2] + p21[2]*mua - p3[2]) < EPSILON){ return true; } }else if(abs(p21[1]) > EPSILON){ mua = -p13[1]/p21[1]; if(abs(p1[0] + p21[0]*mua - p3[0]) < EPSILON && abs(p1[2] + p21[2]*mua - p3[2]) < EPSILON){ return true; } }else{ mua = -p13[2]/p21[2]; if(abs(p1[0] + p21[0]*mua - p3[0]) < EPSILON && abs(p1[1] + p21[1]*mua - p3[1]) < EPSILON){ return true; } } } return false; } d1343 = p13[0] * p43[0] + p13[1] * p43[1] + p13[2] * p43[2]; d4321 = p43[0] * p21[0] + p43[1] * p21[1] + p43[2] * p21[2]; d1321 = p13[0] * p21[0] + p13[1] * p21[1] + p13[2] * p21[2]; d4343 = p43[0] * p43[0] + p43[1] * p43[1] + p43[2] * p43[2]; d2121 = p21[0] * p21[0] + p21[1] * p21[1] + p21[2] * p21[2]; denom = d2121 * d4343 - d4321 * d4321; if(abs(denom) < EPSILON){ return false; } numer = d1343 * d4321 - d1321 * d4343; mua = numer / denom; mub = (d1343 + d4321 * mua) / d4343; if(mua<-EPSILON || mua-1>EPSILON || mub<-EPSILON || mub-1>EPSILON){ return false; } if(abs(p1[0] + mua * p21[0] - p3[0] - mub * p43[0]) < EPSILON && abs(p1[1] + mua * p21[1] - p3[1] - mub * p43[1]) < EPSILON && abs(p1[2] + mua * p21[2] - p3[2] - mub * p43[2]) < EPSILON){ return true; }else{ return false; } } //----------------------------------------< intersection_line_line_with_intersection_3d > bool intersection_line_line_with_intersection_3d(double p1[], double p2[], double p3[], double p4[], double intersection[]){ double p13[3], p43[3], p21[3]; double d1343, d4321, d1321, d4343, d2121; double numer, denom; double mua, mub; int ci = -1; p13[0] = p1[0] - p3[0]; p13[1] = p1[1] - p3[1]; p13[2] = p1[2] - p3[2]; p43[0] = p4[0] - p3[0]; p43[1] = p4[1] - p3[1]; p43[2] = p4[2] - p3[2]; p21[0] = p2[0] - p1[0]; p21[1] = p2[1] - p1[1]; p21[2] = p2[2] - p1[2]; //test for collinearity if( abs(p21[1]*p43[2] - p21[2]*p43[1]) < EPSILON && abs(p21[2]*p43[0] - p21[0]*p43[2]) < EPSILON && abs(p21[0]*p43[1] - p21[1]*p43[0]) < EPSILON ){ //lines are collinear; now test for an intersection double x11, x12, x21, x22, y11, y12, y21, y22, z11, z12, z21, z22; if(p1[0]<=p2[0]){ x11 = p1[0]; x12 = p2[0]; } else{ x11 = p2[0]; x12 = p1[0]; } if(p1[1]<=p2[1]){ y11 = p1[1]; y12 = p2[1]; } else{ y11 = p2[1]; y12 = p1[1]; } if(p1[2]<=p2[2]){ z11 = p1[2]; z12 = p2[2]; } else{ z11 = p2[2]; z12 = p1[2]; } if(p3[0]<=p4[0]){ x21 = p3[0]; x22 = p4[0]; } else{ x21 = p4[0]; x22 = p3[0]; } if(p3[1]<=p4[1]){ y21 = p3[1]; y22 = p4[1]; } else{ y21 = p4[1]; y22 = p3[1]; } if(p3[2]<=p4[2]){ z21 = p3[2]; z22 = p4[2]; } else{ z21 = p4[2]; z22 = p3[2]; } if(x11<=x22 && x12>=x21 && y11<=y22 && y12>=y21 && z11<=z22 && z12>=z21){ if(abs(p21[0]) > EPSILON){ mua = -p13[0] / p21[0]; if(abs(p1[1] + p21[1]*mua - p3[1]) < EPSILON && abs(p1[2] + p21[2]*mua - p3[2]) < EPSILON) ci = 0; }else if(abs(p21[1]) > EPSILON){ mua = -p13[1] / p21[1]; if(abs(p1[0] + p21[0]*mua - p3[0]) < EPSILON && abs(p1[2] + p21[2]*mua - p3[2]) < EPSILON) ci = 1; }else{ mua = -p13[2] / p21[2]; if(abs(p1[0] + p21[0]*mua - p3[0]) < EPSILON && abs(p1[1] + p21[1]*mua - p3[1]) < EPSILON) ci = 2; } if(ci != -1){ //determine best intersection-point //priority is p3(*) > p4 > p3 > p2 (one of these three points must be part of both line segments //(*): p3 is only priorized over p4 if it is not a vertex of the first line segment if(mua > EPSILON && mua-1< -EPSILON){ intersection[0] = p3[0]; intersection[1] = p3[1]; intersection[2] = p3[2]; }else{ mub = (p4[ci]-p1[ci]) / p21[ci]; if(mub > -EPSILON && mub-1 < EPSILON){ intersection[0] = p4[0]; intersection[1] = p4[1]; intersection[2] = p4[2]; }else if(mua > -EPSILON && mua-1 < EPSILON){ intersection[0] = p3[0]; intersection[1] = p3[1]; intersection[2] = p3[2]; }else{ intersection[0] = p2[0]; intersection[1] = p2[1]; intersection[2] = p2[2]; } } return true; } } return false; } d1343 = p13[0] * p43[0] + p13[1] * p43[1] + p13[2] * p43[2]; d4321 = p43[0] * p21[0] + p43[1] * p21[1] + p43[2] * p21[2]; d1321 = p13[0] * p21[0] + p13[1] * p21[1] + p13[2] * p21[2]; d4343 = p43[0] * p43[0] + p43[1] * p43[1] + p43[2] * p43[2]; d2121 = p21[0] * p21[0] + p21[1] * p21[1] + p21[2] * p21[2]; denom = d2121 * d4343 - d4321 * d4321; if(abs(denom) < EPSILON){ return false; } numer = d1343 * d4321 - d1321 * d4343; mua = numer / denom; mub = (d1343 + d4321 * mua) / d4343; if(mua<-EPSILON || mua-1>EPSILON || mub<-EPSILON || mub-1>EPSILON){ return false; } //determine potential intersection-point intersection[0] = p1[0] + mua * p21[0]; intersection[1] = p1[1] + mua * p21[1]; intersection[2] = p1[2] + mua * p21[2]; //check if this really is an intersection if(abs(intersection[0] - p3[0] - mub * p43[0]) < EPSILON && abs(intersection[1] - p3[1] - mub * p43[1]) < EPSILON && abs(intersection[2] - p3[2] - mub * p43[2]) < EPSILON){ return true; }else{ return false; } } //----------------------------------------< intersection_line_triangle_3d_chirkov > //ATTENTION: function does not work for lines almost in the plane of the triangle! //calculates the intersection of a line segment and a triangle if it exists. The intersection-point is not //calculated, the last argument "sol" remains unchanged. // input: a line segment (lin0, lin1), and a triangle (tri0, tri1, tri2) // return: 0 disjoint // 1 intersection inside triangle // 2,3,4 intersection in edge of triangle (edge_index is 0, 1, 2 respectively) //this algorithm is based on an implementation by Nick Chirkov (journal of graphics tools 10(3):13-18, 2005). int intersection_line_triangle_3d(double tri0[], double tri1[], double tri2[], double lin0[], double lin1[], double sol[]){ long i, type; double org[3]; double end[3]; double dir[3]; double x, y, z, d; //cube-collision-detection to determine necessity of real intersection check //note: this does not seem to improve performance unfortunately. //double p3[3],q3[3],p4[3],q4[3]; //for(i=0; i<3; ++i){ // if(tri0[i] <= tri1[i]){ // if(tri0[i] <= tri2[i]){ // p3[i] = tri0[i]; // q3[i] = ((tri1[i]>=tri2[i])? tri1[i] : tri2[i]); // } // else{ // p3[i] = tri2[i]; // q3[i] = tri1[i]; // } // } // else{ // if(tri1[i] <= tri2[i]){ // p3[i] = tri1[i]; // q3[i] = ((tri0[i]>=tri2[i])? tri2[i] : tri0[i]); // } // else{ // p3[i] = tri2[i]; // q3[i] = tri0[i]; // } // } // if(lin0[i] <= lin1[i]){ // p4[i] = lin0[i]; // q4[i] = lin1[i]; // } // else{ // p4[i] = lin1[i]; // q4[i] = lin0[i]; // } //} //if(p3[0]>q4[0] || q3[0]q4[1] || q3[1]q4[2] || q3[2]len) type = 1; else if(fabs(y)>len) type = 2; else type = 3; for(i=0;i<=2;i++){ org[i] = lin0[i]; //todo: org can be replaced by lin0 throughout the whole function end[i] = lin1[i]; //todo: replace end by lin1 dir[i] = end[i] - org[i]; } //determine intersection double signSrc = x*org[0] + y*org[1] + z*org[2] - d; double signDst = x*end[0] + y*end[1] + z*end[2] - d; if(signSrc*signDst > 0.0){ return 0; } double di = signSrc - signDst; if(type == 1){ double basey = org[1] - tri0[1]; double basez = org[2] - tri0[2]; double adelx = signSrc*(ay * dir[2] - az * dir[1]); if((adelx + di * (ay*basez-az*basey)) * (signSrc*(dir[1]*bz-dir[2]*by) + di * (basey*bz-basez*by)) > 0.0){ double e2y = tri1[1] - tri2[1]; double e2z = tri1[2] - tri2[2]; basey = org[1] - tri1[1]; basez = org[2] - tri1[2]; if((adelx + di * (ay*basez-az*basey)) * (signSrc*(dir[1]*e2z-dir[2]*e2y) + di * (basey*e2z-basez*e2y)) > 0.0){ goto test_edges; } } } else if(type == 2){ double basex = org[0] - tri0[0]; double basez = org[2] - tri0[2]; double adely = signSrc*(az * dir[0] - ax * dir[2]); if((adely + di * (az*basex-ax*basez)) * (signSrc*(dir[2]*bx-dir[0]*bz) + di * (basez*bx-basex*bz)) > 0.0){ double e2x = tri1[0] - tri2[0]; double e2z = tri1[2] - tri2[2]; basex = org[0] - tri1[0]; basez = org[2] - tri1[2]; if((adely + di * (az*basex-ax*basez)) * (signSrc*(dir[2]*e2x-dir[0]*e2z) + di * (basez*e2x-basex*e2z)) > 0.0){ goto test_edges; } } } else{ double basex = org[0] - tri0[0]; double basey = org[1] - tri0[1]; double adelz = signSrc*(ax * dir[1] - ay * dir[0]); if((adelz + di * (ax*basey-ay*basex)) * (signSrc*(dir[0]*by-dir[1]*bx) + di * (basex*by-basey*bx)) > 0.0){ double e2x = tri1[0] - tri2[0]; double e2y = tri1[1] - tri2[1]; basex = org[0] - tri1[0]; basey = org[1] - tri1[1]; if((adelz + di * (ax*basey-ay*basex)) * (signSrc*(dir[0]*e2y-dir[1]*e2x) + di * (basex*e2y-basey*e2x)) > 0.0){ goto test_edges; ``````