File size: 33,681 Bytes
5a600cf | 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 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "surfaceIntersection.H"
#include "triSurfaceSearch.H"
#include "labelPairLookup.H"
#include "OFstream.H"
#include "HashSet.H"
#include "triSurface.H"
#include "pointIndexHit.H"
#include "mergePoints.H"
#include "plane.H"
#include "edgeIntersections.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(surfaceIntersection, 0);
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// Checks if there exists a special topological situation that causes
// edge and the face it hit not to be recognised.
//
// For now if the face shares a point with the edge
bool Foam::surfaceIntersection::excludeEdgeHit
(
const triSurface& surf,
const label edgeI,
const label facei,
const scalar
)
{
const triSurface::FaceType& f = surf.localFaces()[facei];
const edge& e = surf.edges()[edgeI];
forAll(f, fp)
{
if (f[0] == e.start() || f[0] == e.end())
{
return true;
}
}
// {
// // Get edge vector
// vector eVec = e.vec(surf.localPoints());
// eVec /= mag(eVec) + vSmall;
//
// const labelList& eLabels = surf.faceEdges()[facei];
//
// // Get edge vector of 0th edge of face
// vector e0Vec = surf.edges()[eLabels[0]].vec(surf.localPoints());
// e0Vec /= mag(e0Vec) + vSmall;
//
// vector n = e0Vec ^ eVec;
//
// if (mag(n) < small)
// {
// // e0 is aligned with e. Choose next edge of face.
// vector e1Vec = surf.edges()[eLabels[1]].vec(surf.localPoints());
// e1Vec /= mag(e1Vec) + vSmall;
//
// n = e1Vec ^ eVec;
//
// if (mag(n) < small)
// {
// // Problematic triangle. Two edges aligned with edgeI. Give
// // up.
// return true;
// }
// }
//
// // Check if same as faceNormal
// if (mag(n & surf.faceNormals()[facei]) > 1-tol)
// {
//
// Pout<< "edge:" << e << " face:" << facei
// << " e0Vec:" << e0Vec << " n:" << n
// << " normalComponent:" << (n & surf.faceNormals()[facei])
// << " tol:" << tol << endl;
//
// return true;
// }
// else
// {
// return false;
// }
// }
return false;
}
//// Find intersection of plane with edges of hitFacei. Returns
//// - edgeI
//// - intersection point
//Foam::pointIndexHit Foam::surfaceIntersection::faceEdgeIntersection
//(
// const triSurface& surf,
// const label hitFacei,
//
// const vector& n,
// const point& eStart,
// const point& eEnd
//)
//{
// pointIndexHit pInter;
//
// const pointField& points = surf.points();
//
// const triSurface::FaceType& f = surf.localFaces()[hitFacei];
//
// // Plane for intersect test.
// plane pl(eStart, n);
//
// forAll(f, fp)
// {
// label fp1 = f.fcIndex(fp);
//
// const point& start = points[f[fp]];
// const point& end = points[f[fp1]];
//
// vector eVec(end - start);
//
// scalar s = pl.normalIntersect(start, eVec);
//
// if (s < 0 || s > 1)
// {
// pInter.setPoint(start + s*eVec);
//
// // Check if is correct one: orientation walking
// // eStart - eEnd - hitPoint should be opposite n
// vector n2(triPointRef(start, end, pInter.hitPoint()).normal());
//
// Pout<< "plane normal:" << n
// << " start:" << start << " end:" << end
// << " hit at:" << pInter.hitPoint()
// << " resulting normal:" << n2 << endl;
//
// if ((n2 & n) < 0)
// {
// pInter.setHit();
//
// // Find corresponding edge between f[fp] f[fp1]
// label edgeI =
// meshTools::findEdge
// (
// surf.edges(),
// surf.faceEdges()[hitFacei],
// f[fp],
// f[fp1]
// );
//
// pInter.setIndex(edgeI);
//
// return pInter;
// }
// }
// }
//
// FatalErrorInFunction
// << "Did not find intersection of plane " << pl
// << " with edges of face " << hitFacei << " verts:" << f
// << abort(FatalError);
//
// return pInter;
//}
void Foam::surfaceIntersection::storeIntersection
(
const bool isFirstSurf,
const labelList& facesA,
const label faceB,
DynamicList<edge>& allCutEdges,
DynamicList<point>& allCutPoints
)
{
forAll(facesA, facesAI)
{
label faceA = facesA[facesAI];
// Combine two faces. Always make sure the face from the first surface
// is element 0.
labelPair twoFaces;
if (isFirstSurf)
{
twoFaces[0] = faceA;
twoFaces[1] = faceB;
}
else
{
twoFaces[0] = faceB;
twoFaces[1] = faceA;
}
labelPairLookup::const_iterator iter = facePairToVertex_.find(twoFaces);
if (iter == facePairToVertex_.end())
{
// New intersection. Store face-face intersection.
facePairToVertex_.insert(twoFaces, allCutPoints.size()-1);
}
else
{
// Second occurrence of surf1-surf2 intersection.
// Or rather the face on surf1 intersects a face on
// surface2 twice -> we found edge.
// Check whether perhaps degenerate
const point& prevHit = allCutPoints[*iter];
const point& thisHit = allCutPoints.last();
if (mag(prevHit - thisHit) < small)
{
WarningInFunction
<< "Encountered degenerate edge between face "
<< twoFaces[0] << " on first surface"
<< " and face " << twoFaces[1] << " on second surface"
<< endl
<< "Point on first surface:" << prevHit << endl
<< "Point on second surface:" << thisHit << endl
<< endl;
}
else
{
allCutEdges.append(edge(*iter, allCutPoints.size()-1));
// Remember face on surf
facePairToEdge_.insert(twoFaces, allCutEdges.size()-1);
}
}
}
}
// Classify cut of edge of surface1 with surface2:
// 1- point of edge hits point on surface2
// 2- edge pierces point on surface2
// 3- point of edge hits edge on surface2
// 4- edge pierces edge on surface2
// 5- point of edge hits face on surface2
// 6- edge pierces face on surface2
//
// Note that handling of 2 and 4 should be the same but with surface1 and
// surface2 reversed.
void Foam::surfaceIntersection::classifyHit
(
const triSurface& surf1,
const scalarField& surf1PointTol,
const triSurface& surf2,
const bool isFirstSurf,
const label edgeI,
const scalar tolDim,
const pointIndexHit& pHit,
DynamicList<edge>& allCutEdges,
DynamicList<point>& allCutPoints,
List<DynamicList<label>>& surfEdgeCuts
)
{
const edge& e = surf1.edges()[edgeI];
const labelList& facesA = surf1.edgeFaces()[edgeI];
// Label of face on surface2 edgeI intersected
label surf2Facei = pHit.index();
// Classify point on surface2
const triSurface::FaceType& f2 = surf2.localFaces()[surf2Facei];
const pointField& surf2Pts = surf2.localPoints();
label nearType, nearLabel;
f2.nearestPointClassify(pHit.hitPoint(), surf2Pts, nearType, nearLabel);
// Classify points on edge of surface1
label edgeEnd =
classify
(
surf1PointTol[e.start()],
surf1PointTol[e.end()],
pHit.hitPoint(),
e,
surf1.localPoints()
);
if (nearType == triPointRef::POINT)
{
if (edgeEnd >= 0)
{
// 1. Point hits point. Do nothing.
if (debug & 2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e
<< " surf2: vertex " << f2[nearLabel]
<< " coord:" << surf2Pts[f2[nearLabel]] << endl;
}
}
else
{
// 2. Edge hits point. Cut edge with new point.
if (debug & 2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " somewhere on edge " << e
<< " surf2: vertex " << f2[nearLabel]
<< " coord:" << surf2Pts[f2[nearLabel]] << endl;
}
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
const labelList& facesB = surf2.pointFaces()[f2[nearLabel]];
forAll(facesB, faceBI)
{
storeIntersection
(
isFirstSurf,
facesA,
facesB[faceBI],
allCutEdges,
allCutPoints
);
}
}
}
else if (nearType == triPointRef::EDGE)
{
if (edgeEnd >= 0)
{
// 3. Point hits edge. Do nothing on this side. Reverse
// is handled by 2 (edge hits point)
label edge2I = getEdge(surf2, surf2Facei, nearLabel);
const edge& e2 = surf2.edges()[edge2I];
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e
<< " surf2: edge " << e2
<< " coords:" << surf2Pts[e2.start()]
<< surf2Pts[e2.end()] << endl;
}
}
else
{
// 4. Edge hits edge.
// Cut edge with new point (creates duplicates when
// doing the surf2 with surf1 intersection but these
// are merged later on)
label edge2I = getEdge(surf2, surf2Facei, nearLabel);
const edge& e2 = surf2.edges()[edge2I];
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " somewhere on edge " << e
<< " surf2: edge " << e2
<< " coords:" << surf2Pts[e2.start()]
<< surf2Pts[e2.end()] << endl;
}
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
// edge hits all faces on surf2 connected to the edge
if (isFirstSurf)
{
// edge-edge intersection is symmetric, store only
// once.
// edge hits all faces on surf2 connected to the
// edge
const labelList& facesB = surf2.edgeFaces()[edge2I];
forAll(facesB, faceBI)
{
storeIntersection
(
isFirstSurf,
facesA,
facesB[faceBI],
allCutEdges,
allCutPoints
);
}
}
}
}
else
{
if (edgeEnd >= 0)
{
// 5. Point hits face. Do what? Introduce
// point & triangulation in face?
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e
<< " surf2: face " << surf2Facei
<< endl;
}
//
// Look exactly at what side (of surf2) edge is. Leave out ones on
// inside of surf2 (i.e. on opposite side of normal)
//
// Vertex on/near surf2
label nearVert = -1;
if (edgeEnd == 0)
{
nearVert = e.start();
}
else
{
nearVert = e.end();
}
const point& nearPt = surf1.localPoints()[nearVert];
// Vertex away from surf2
label otherVert = e.otherVertex(nearVert);
const point& otherPt = surf1.localPoints()[otherVert];
if (debug)
{
Pout
<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e << " coord:"
<< surf1.localPoints()[nearVert]
<< " surf2: face " << surf2Facei << endl;
}
vector eVec = otherPt - nearPt;
if ((surf2.faceNormals()[surf2Facei] & eVec) > 0)
{
// otherVert on outside of surf2
// Shift hitPoint a bit along edge.
// point hitPt = nearPt + 0.1*eVec;
point hitPt = nearPt;
if (debug&2)
{
Pout<< "Shifted " << pHit.hitPoint()
<< " to " << hitPt
<< " along edge:" << e
<< " coords:" << surf1.localPoints()[e.start()]
<< surf1.localPoints()[e.end()] << endl;
}
// Reclassify as normal edge-face pierce (see below)
allCutPoints.append(hitPt);
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
// edge hits single face only
storeIntersection
(
isFirstSurf,
facesA,
surf2Facei,
allCutEdges,
allCutPoints
);
}
else
{
if (debug&2)
{
Pout<< "Discarding " << pHit.hitPoint()
<< " since edge " << e << " on inside of surf2."
<< " surf2 normal:" << surf2.faceNormals()[surf2Facei]
<< endl;
}
}
}
else
{
// 6. Edge pierces face. 'Normal' situation.
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " somewhere on edge " << e
<< " surf2: face " << surf2Facei
<< endl;
}
// edgeI intersects surf2. Store point.
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
// edge hits single face only
storeIntersection
(
isFirstSurf,
facesA,
surf2Facei,
allCutEdges,
allCutPoints
);
}
}
if (debug&2)
{
Pout<< endl;
}
}
// Cut all edges of surf1 with surf2. Sets
// - cutPoints : coordinates of cutPoints
// - cutEdges : newly created edges between cutPoints
// - facePairToVertex : hash from face1I and face2I to cutPoint
// - facePairToEdge : hash from face1I and face2I to cutEdge
// - surfEdgeCuts : gives for each edge the cutPoints
// (in order from start to end)
//
void Foam::surfaceIntersection::doCutEdges
(
const triSurface& surf1,
const triSurfaceSearch& querySurf2,
const bool isFirstSurf,
const bool isSelfIntersection,
DynamicList<edge>& allCutEdges,
DynamicList<point>& allCutPoints,
List<DynamicList<label>>& surfEdgeCuts
)
{
scalar oldTol = intersection::setPlanarTol(1e-3);
const pointField& surf1Pts = surf1.localPoints();
// Calculate local (to point) tolerance based on min edge length.
scalarField surf1PointTol(surf1Pts.size());
forAll(surf1PointTol, pointi)
{
surf1PointTol[pointi] =
intersection::planarTol()
* minEdgeLen(surf1, pointi);
}
const triSurface& surf2 = querySurf2.surface();
forAll(surf1.edges(), edgeI)
{
const edge& e = surf1.edges()[edgeI];
point pStart = surf1Pts[e.start()];
const point& pEnd = surf1Pts[e.end()];
const point tolVec = intersection::planarTol()*(pEnd-pStart);
const scalar tolDim = mag(tolVec);
bool doTrack = false;
do
{
pointIndexHit pHit = querySurf2.tree().findLine(pStart, pEnd);
if (pHit.hit())
{
if (isSelfIntersection)
{
// Skip all intersections which are hit at endpoints of
// edge.
// Problem is that if faces are almost coincident the
// intersection point will be calculated quite incorrectly
// The error might easily be larger than 1% of the edge
// length.
// So what we do here is to exclude hit faces if our edge
// is in their plane and they share a point with the edge.
// Label of face on surface2 edgeI intersected
label hitFacei = pHit.index();
if
(
!excludeEdgeHit
(
surf1,
edgeI,
hitFacei,
0.1 // 1-cos of angle between normals
)
)
{
// Classify point on surface1
label edgeEnd = classify
(
surf1PointTol[e.start()],
surf1PointTol[e.end()],
pHit.hitPoint(),
e,
surf1Pts
);
if (edgeEnd < 0)
{
if (debug)
{
Pout<< "edge:" << edgeI << " vertices:" << e
<< " start:" << surf1Pts[e.start()]
<< " end:" << surf1Pts[e.end()]
<< " hit:" << pHit.hitPoint()
<< " tolDim:" << tolDim
<< " planarTol:"
<< intersection::planarTol()
<< endl;
}
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
}
}
}
else
{
classifyHit
(
surf1,
surf1PointTol,
surf2,
isFirstSurf,
edgeI,
tolDim,
pHit,
allCutEdges,
allCutPoints,
surfEdgeCuts
);
}
if (mag(pHit.hitPoint() - pEnd) < tolDim)
{
doTrack = false;
}
else
{
pStart = pHit.hitPoint() + tolVec;
doTrack = true;
}
}
else
{
doTrack = false;
}
}
while (doTrack);
}
intersection::setPlanarTol(oldTol);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Null constructor
Foam::surfaceIntersection::surfaceIntersection()
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(0),
facePairToEdge_(0),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{}
// Construct from two surfaces
Foam::surfaceIntersection::surfaceIntersection
(
const triSurfaceSearch& query1,
const triSurfaceSearch& query2
)
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(2*max(query1.surface().size(), query2.surface().size())),
facePairToEdge_(2*max(query1.surface().size(), query2.surface().size())),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{
const triSurface& surf1 = query1.surface();
const triSurface& surf2 = query2.surface();
//
// Cut all edges of surf1 with surf2.
//
if (debug)
{
Pout<< "Cutting surf1 edges" << endl;
}
DynamicList<edge> allCutEdges(surf1.nEdges()/20);
DynamicList<point> allCutPoints(surf1.nPoints()/20);
// From edge to cut index on surface1
List<DynamicList<label>> edgeCuts1(query1.surface().nEdges());
doCutEdges
(
surf1,
query2,
true, // is first surface; construct labelPair in correct
// order
false, // not self intersection
allCutEdges,
allCutPoints,
edgeCuts1
);
// Transfer to straight labelListList
transfer(edgeCuts1, surf1EdgeCuts_);
//
// Cut all edges of surf2 with surf1.
//
if (debug)
{
Pout<< "Cutting surf2 edges" << endl;
}
// From edge to cut index
List<DynamicList<label>> edgeCuts2(query2.surface().nEdges());
doCutEdges
(
surf2,
query1,
false, // is second surface
false, // not self intersection
allCutEdges,
allCutPoints,
edgeCuts2
);
// Transfer to straight label(List)List
transfer(edgeCuts2, surf2EdgeCuts_);
cutEdges_.transfer(allCutEdges);
cutPoints_.transfer(allCutPoints);
if (debug)
{
Pout<< "surfaceIntersection : Intersection generated:"
<< endl
<< " points:" << cutPoints_.size() << endl
<< " edges :" << cutEdges_.size() << endl;
Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
<< endl;
OFstream intStream("intEdges.obj");
writeOBJ(cutPoints_, cutEdges_, intStream);
// Dump all cut edges to files
Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
OFstream edge1Stream("surf1EdgeCuts.obj");
writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
Pout<< "Dumping cut edges of surface2 to surf2EdgeCuts.obj" << endl;
OFstream edge2Stream("surf2EdgeCuts.obj");
writeIntersectedEdges(surf2, surf2EdgeCuts_, edge2Stream);
}
}
// Construct from full intersection Poutrmation
Foam::surfaceIntersection::surfaceIntersection
(
const triSurface& surf1,
const edgeIntersections& intersections1,
const triSurface& surf2,
const edgeIntersections& intersections2
)
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(2*max(surf1.size(), surf2.size())),
facePairToEdge_(2*max(surf1.size(), surf2.size())),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{
// All intersection Pout (so for both surfaces)
DynamicList<edge> allCutEdges((surf1.nEdges() + surf2.nEdges())/20);
DynamicList<point> allCutPoints((surf1.nPoints() + surf2.nPoints())/20);
// Cut all edges of surf1 with surf2
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (debug)
{
Pout<< "Storing surf1 intersections" << endl;
}
{
// From edge to cut index on surface1
List<DynamicList<label>> edgeCuts1(surf1.nEdges());
forAll(intersections1, edgeI)
{
const List<pointIndexHit>& intersections = intersections1[edgeI];
forAll(intersections, i)
{
const pointIndexHit& pHit = intersections[i];
// edgeI intersects surf2. Store point.
allCutPoints.append(pHit.hitPoint());
edgeCuts1[edgeI].append(allCutPoints.size()-1);
storeIntersection
(
true, // is first surface
surf1.edgeFaces()[edgeI],
pHit.index(), // surf2Facei
allCutEdges,
allCutPoints
);
}
}
// Transfer to straight labelListList
transfer(edgeCuts1, surf1EdgeCuts_);
}
// Cut all edges of surf2 with surf1
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (debug)
{
Pout<< "Storing surf2 intersections" << endl;
}
{
// From edge to cut index on surface2
List<DynamicList<label>> edgeCuts2(surf2.nEdges());
forAll(intersections2, edgeI)
{
const List<pointIndexHit>& intersections = intersections2[edgeI];
forAll(intersections, i)
{
const pointIndexHit& pHit = intersections[i];
// edgeI intersects surf1. Store point.
allCutPoints.append(pHit.hitPoint());
edgeCuts2[edgeI].append(allCutPoints.size()-1);
storeIntersection
(
false, // is second surface
surf2.edgeFaces()[edgeI],
pHit.index(), // surf2Facei
allCutEdges,
allCutPoints
);
}
}
// Transfer to surf2EdgeCuts_ (straight labelListList)
transfer(edgeCuts2, surf2EdgeCuts_);
}
// Transfer to straight label(List)List
cutEdges_.transfer(allCutEdges);
cutPoints_.transfer(allCutPoints);
if (debug)
{
Pout<< "surfaceIntersection : Intersection generated:"
<< endl
<< " points:" << cutPoints_.size() << endl
<< " edges :" << cutEdges_.size() << endl;
Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
<< endl;
OFstream intStream("intEdges.obj");
writeOBJ(cutPoints_, cutEdges_, intStream);
// Dump all cut edges to files
Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
OFstream edge1Stream("surf1EdgeCuts.obj");
writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
Pout<< "Dumping cut edges of surface2 to surf2EdgeCuts.obj" << endl;
OFstream edge2Stream("surf2EdgeCuts.obj");
writeIntersectedEdges(surf2, surf2EdgeCuts_, edge2Stream);
}
// Debugging stuff
{
// Check all facePairToVertex is used.
labelHashSet usedPoints;
forAllConstIter(labelPairLookup, facePairToEdge_, iter)
{
label edgeI = iter();
const edge& e = cutEdges_[edgeI];
usedPoints.insert(e[0]);
usedPoints.insert(e[1]);
}
forAllConstIter(labelPairLookup, facePairToVertex_, iter)
{
label pointi = iter();
if (!usedPoints.found(pointi))
{
WarningInFunction
<< "Problem: cut point:" << pointi
<< " coord:" << cutPoints_[pointi]
<< " not used by any edge" << endl;
}
}
}
}
// Construct from single surface. Used to test for self-intersection.
Foam::surfaceIntersection::surfaceIntersection
(
const triSurfaceSearch& query1
)
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(2*query1.surface().size()),
facePairToEdge_(2*query1.surface().size()),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{
const triSurface& surf1 = query1.surface();
//
// Cut all edges of surf1 with surf1 itself.
//
if (debug)
{
Pout<< "Cutting surf1 edges" << endl;
}
DynamicList<edge> allCutEdges;
DynamicList<point> allCutPoints;
// From edge to cut index on surface1
List<DynamicList<label>> edgeCuts1(query1.surface().nEdges());
doCutEdges
(
surf1,
query1,
true, // is first surface; construct labelPair in correct
// order
true, // self intersection
allCutEdges,
allCutPoints,
edgeCuts1
);
// Transfer to straight label(List)List
transfer(edgeCuts1, surf1EdgeCuts_);
cutEdges_.transfer(allCutEdges);
cutPoints_.transfer(allCutPoints);
// Shortcut.
if (cutPoints_.empty() && cutEdges_.empty())
{
if (debug)
{
Pout<< "Empty intersection" << endl;
}
return;
}
//
// Remove duplicate points (from edge-point or edge-edge cutting)
//
// Get typical dimension.
scalar minEdgeLen = great;
forAll(surf1.edges(), edgeI)
{
minEdgeLen = min
(
minEdgeLen,
surf1.edges()[edgeI].mag(surf1.localPoints())
);
}
// Merge points
labelList pointMap;
pointField newPoints;
bool hasMerged = mergePoints
(
cutPoints_,
minEdgeLen*intersection::planarTol(),
false,
pointMap,
newPoints
);
if (hasMerged)
{
if (debug)
{
Pout<< "Merged:" << hasMerged
<< " mergeDist:" << minEdgeLen*intersection::planarTol()
<< " cutPoints:" << cutPoints_.size()
<< " newPoints:" << newPoints.size()
<< endl;
}
// Copy points
cutPoints_.transfer(newPoints);
// Renumber vertices referenced by edges
forAll(cutEdges_, edgeI)
{
edge& e = cutEdges_[edgeI];
e.start() = pointMap[e.start()];
e.end() = pointMap[e.end()];
if (e.mag(cutPoints_) < minEdgeLen*intersection::planarTol())
{
if (debug)
{
Pout<< "Degenerate cut:" << edgeI << " vertices:" << e
<< " coords:" << cutPoints_[e.start()] << ' '
<< cutPoints_[e.end()] << endl;
}
}
}
// Renumber vertices referenced by edgeCut lists. Remove duplicates.
forAll(surf1EdgeCuts_, edgeI)
{
// Get indices of cutPoints this edge is cut by
labelList& cutVerts = surf1EdgeCuts_[edgeI];
removeDuplicates(pointMap, cutVerts);
}
}
if (debug)
{
Pout<< "surfaceIntersection : Intersection generated and compressed:"
<< endl
<< " points:" << cutPoints_.size() << endl
<< " edges :" << cutEdges_.size() << endl;
Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
<< endl;
OFstream intStream("intEdges.obj");
writeOBJ(cutPoints_, cutEdges_, intStream);
}
if (debug)
{
// Dump all cut edges to files
Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
OFstream edge1Stream("surf1EdgeCuts.obj");
writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::pointField& Foam::surfaceIntersection::cutPoints() const
{
return cutPoints_;
}
const Foam::edgeList& Foam::surfaceIntersection::cutEdges() const
{
return cutEdges_;
}
const Foam::labelPairLookup& Foam::surfaceIntersection::facePairToVertex() const
{
return facePairToVertex_;
}
const Foam::labelPairLookup& Foam::surfaceIntersection::facePairToEdge() const
{
return facePairToEdge_;
}
const Foam::labelListList& Foam::surfaceIntersection::edgeCuts
(
const bool isFirstSurf
) const
{
if (isFirstSurf)
{
return surf1EdgeCuts_;
}
else
{
return surf2EdgeCuts_;
}
}
const Foam::labelListList& Foam::surfaceIntersection::surf1EdgeCuts() const
{
return surf1EdgeCuts_;
}
const Foam::labelListList& Foam::surfaceIntersection::surf2EdgeCuts() const
{
return surf2EdgeCuts_;
}
// ************************************************************************* //
|