File size: 30,916 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 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2021-2026 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 "intersectionPatchToPatch.H"
#include "triIntersect.H"
#include "units.H"
#include "vtkWritePolyData.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace patchToPatches
{
defineTypeNameAndDebug(intersection, 0);
addToRunTimeSelectionTable(patchToPatch, intersection, bool);
int intersection::debugSrcFacei =
debug::debugSwitch((intersection::typeName + "SrcFace").c_str(), -1);
int intersection::debugTgtFacei =
debug::debugSwitch((intersection::typeName + "TgtFace").c_str(), -1);
}
}
// * * * * * * * * * * * Private Static Member Functions * * * * * * * * * * //
template<class Type>
Foam::FixedList<Type, 3>
Foam::patchToPatches::intersection::triPointValues
(
const triFace& t,
const UList<Type>& values
)
{
FixedList<Type, 3> result;
forAll(t, i)
{
result[i] = values[t[i]];
}
return result;
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::treeBoundBox Foam::patchToPatches::intersection::srcBoxStatic
(
const face& srcFace,
const pointField& srcPoints,
const vectorField& srcPointNormals
)
{
static DynamicList<point> ps;
ps.clear();
const scalar l = sqrt(mag(srcFace.area(srcPoints)));
forAll(srcFace, srcFacePointi)
{
const label srcPointi = srcFace[srcFacePointi];
const point& p = srcPoints[srcPointi];
const vector& n = srcPointNormals[srcPointi];
ps.append(p - l/2*n);
ps.append(p + l/2*n);
}
return treeBoundBox(ps);
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::treeBoundBox Foam::patchToPatches::intersection::srcBox
(
const face& srcFace,
const pointField& srcPoints,
const vectorField& srcPointNormals
) const
{
return srcBoxStatic(srcFace, srcPoints, srcPointNormals);
}
bool Foam::patchToPatches::intersection::intersectFaces
(
const primitiveOldTimePatch& srcPatch,
const vectorField& srcPointNormals,
const vectorField& srcPointNormals0,
const primitiveOldTimePatch& tgtPatch,
const label srcFacei,
const label tgtFacei
)
{
// Quick rejection based on bound box
const treeBoundBox srcFaceBox =
srcBox
(
srcPatch.localFaces()[srcFacei],
srcPatch.localPoints(),
srcPointNormals
);
const treeBoundBox tgtFaceBox(tgtPatch.points(), tgtPatch[tgtFacei]);
if (!srcFaceBox.overlaps(tgtFaceBox)) return false;
// Construct face triangulations on demand
if (srcTriPoints_[srcFacei].empty())
{
triEngine_.triangulate
(
UIndirectList<point>
(
srcPatch.localPoints(),
srcPatch.localFaces()[srcFacei]
)
);
srcTriPoints_[srcFacei] =
triEngine_.triPoints(srcPatch.localFaces()[srcFacei]);
srcTriFaceEdges_[srcFacei] = triEngine_.triEdges();
}
if (tgtTriPoints_[tgtFacei].empty())
{
triEngine_.triangulate
(
UIndirectList<point>
(
tgtPatch.localPoints(),
tgtPatch.localFaces()[tgtFacei]
)
);
tgtTriPoints_[tgtFacei] =
triEngine_.triPoints(tgtPatch.localFaces()[tgtFacei]);
tgtTriFaceEdges_[tgtFacei] = triEngine_.triEdges();
}
// Construct and initialise workspace
bool srcCouples = false;
couple srcCouple;
srcFaceEdgePart_.resize(srcPatch[srcFacei].size());
forAll(srcFaceEdgePart_, srcFaceEdgei)
{
const edge e =
srcPatch.localFaces()[srcFacei].faceEdge(srcFaceEdgei);
const vector eC = e.centre(srcPatch.localPoints());
srcFaceEdgePart_[srcFaceEdgei] = part(Zero, eC);
}
bool tgtCouples = false;
couple tgtCouple;
tgtFaceEdgePart_.resize(tgtPatch[tgtFacei].size());
forAll(tgtFaceEdgePart_, tgtFaceEdgei)
{
const edge e =
tgtPatch.localFaces()[tgtFacei].faceEdge(tgtFaceEdgei);
const vector eC = e.centre(tgtPatch.localPoints());
tgtFaceEdgePart_[tgtFaceEdgei] = part(Zero, eC);
}
part errorPart(Zero, srcPatch.faceCentres()[srcFacei]);
// Cache the face area magnitudes
const scalar srcMagA = mag(srcPatch.faceAreas()[srcFacei]);
const scalar tgtMagA = mag(tgtPatch.faceAreas()[tgtFacei]);
// Determine whether or not to debug this tri intersection
const bool debugTriIntersect =
(debugSrcFacei != -1 || debugTgtFacei != -1)
&& (debugSrcFacei == -1 || debugSrcFacei == srcFacei)
&& (debugTgtFacei == -1 || debugTgtFacei == tgtFacei);
// Loop the face triangles and compute the intersections
bool anyCouples = false;
forAll(srcTriPoints_[srcFacei], srcFaceTrii)
{
const triFace& srcT = srcTriPoints_[srcFacei][srcFaceTrii];
const FixedList<point, 3> srcPs =
triPointValues(srcT, srcPatch.localPoints());
const FixedList<vector, 3> srcNs =
triPointValues(srcT, srcPointNormals);
forAll(tgtTriPoints_[tgtFacei], tgtFaceTrii)
{
const triFace tgtT =
reverse()
? tgtTriPoints_[tgtFacei][tgtFaceTrii].reverseFace()
: tgtTriPoints_[tgtFacei][tgtFaceTrii];
const FixedList<point, 3> tgtPs =
triPointValues(tgtT, tgtPatch.localPoints());
// Do tri-intersection
ictSrcPoints_.clear();
ictSrcPointNormals_.clear();
ictTgtPoints_.clear();
ictPointLocations_.clear();
triIntersect::intersectTris
(
srcPs,
srcNs,
{false, false, false},
{-1, -1, -1},
tgtPs,
{false, false, false},
{-1, -1, -1},
ictSrcPoints_,
ictSrcPointNormals_,
ictTgtPoints_,
ictPointLocations_,
debugTriIntersect,
debugTriIntersect
? word
(
typeName
+ "_srcFace=" + Foam::name(srcFacei)
+ "_tgtFace=" + Foam::name(tgtFacei)
+ "_intersection=" + Foam::name
(srcFaceTrii*tgtTriPoints_[tgtFacei].size() + tgtFaceTrii)
)
: word::null
);
// If there is no intersection then continue
if (ictPointLocations_.empty())
{
continue;
}
// Mark that there has been an intersection
anyCouples = true;
// Compute the intersection geometry
const part ictSrcPart(ictSrcPoints_);
const part ictTgtPart(ictTgtPoints_);
// If the intersection is below tolerance then continue
if
(
mag(ictSrcPart.area) < small*srcMagA
|| mag(ictTgtPart.area) < small*tgtMagA
)
{
continue;
}
// Mark that the source and target faces intersect
srcCouples = tgtCouples = true;
// Store the intersection geometry
srcCouple += ictSrcPart;
srcCouple.nbr += ictTgtPart;
if (reverse())
{
tgtCouple += ictTgtPart;
tgtCouple.nbr += ictSrcPart;
}
else
{
tgtCouple -= ictTgtPart;
tgtCouple.nbr -= ictSrcPart;
}
// Store the intersection polygons for debugging
const label debugSrcPoint0 = debugPoints_.size();
const label debugTgtPoint0 =
debugPoints_.size() + ictSrcPoints_.size();
if (debug > 1)
{
debugPoints_.append(ictSrcPoints_);
debugPoints_.append(ictTgtPoints_);
debugFaces_.append
(
identityMap(debugSrcPoint0, ictSrcPoints_.size())
);
debugFaceSrcFaces_.append(srcFacei);
debugFaceTgtFaces_.append(tgtFacei);
debugFaceSides_.append(1);
debugFaces_.append
(
identityMap(debugTgtPoint0, ictTgtPoints_.size())
);
debugFaceSrcFaces_.append(srcFacei);
debugFaceTgtFaces_.append(tgtFacei);
debugFaceSides_.append(-1);
}
// Store edge and error areas
forAll(ictPointLocations_, i0)
{
const label i1 = ictPointLocations_.fcIndex(i0);
// Get the locations on each end of this edge of the
// intersection polygon
const triIntersect::location l0 = ictPointLocations_[i0];
const triIntersect::location l1 = ictPointLocations_[i1];
// Get the geometry for the projection of this edge
const part ictEdgePart
(
FixedList<point, 4>
({
ictSrcPoints_[i0],
ictSrcPoints_[i1],
ictTgtPoints_[i1],
ictTgtPoints_[i0]
})
);
// Store the "side" of the intersection that this edge
// corresponds to
label ictEdgeSide = -labelMax;
// If this edge corresponds to an edge of the source
// triangle
if
(
l0.isSrcNotTgtPoint()
|| l1.isSrcNotTgtPoint()
|| (
l0.isIntersection()
&& l1.isIntersection()
&& l0.srcEdgei() == l1.srcEdgei()
)
)
{
const label srcEi =
l0.isSrcPoint() ? l0.srcPointi()
: l1.isSrcPoint() ? (l1.srcPointi() + 2) % 3
: l0.srcEdgei();
const label srcFaceEdgei =
srcTriFaceEdges_[srcFacei][srcFaceTrii][srcEi];
if (srcFaceEdgei < srcPatch[srcFacei].size())
{
srcFaceEdgePart_[srcFaceEdgei] += ictEdgePart;
ictEdgeSide = 1;
}
else
{
errorPart += ictEdgePart;
ictEdgeSide = 0;
}
}
// If this edge corresponds to an edge of the target
// triangle
else if
(
l0.isTgtNotSrcPoint()
|| l1.isTgtNotSrcPoint()
|| (
l0.isIntersection()
&& l1.isIntersection()
&& l0.tgtEdgei() == l1.tgtEdgei()
)
)
{
const label tgtEi =
l0.isTgtPoint() ? (l0.tgtPointi() + 2) % 3
: l1.isTgtPoint() ? l1.tgtPointi()
: l0.tgtEdgei();
const label tgtFaceEdgei =
tgtTriFaceEdges_[tgtFacei][tgtFaceTrii]
[reverse() ? 2 - tgtEi : tgtEi];
if (tgtFaceEdgei < tgtPatch[tgtFacei].size())
{
tgtFaceEdgePart_[tgtFaceEdgei] +=
reverse() ? ictEdgePart : -ictEdgePart;
ictEdgeSide = -1;
}
else
{
errorPart += ictEdgePart;
ictEdgeSide = 0;
}
}
// No other location combinations should be possible for an
// intersection without any shared points
else
{
FatalErrorInFunction
<< "The intersection topology " << ictPointLocations_
<< " between triangle #" << srcFaceTrii
<< " of source face #" << srcFacei
<< " and triangle #" << tgtFaceTrii
<< " of target face #" << tgtFacei
<< " was not recognised. This is a bug."
<< exit(FatalError);
}
// Store the projected edge quadrilateral for debugging
if (debug > 1)
{
debugFaces_.append
(
labelList
({
debugSrcPoint0 + i0,
debugSrcPoint0 + i1,
debugTgtPoint0 + i1,
debugTgtPoint0 + i0
})
);
debugFaceSrcFaces_.append(srcFacei);
debugFaceTgtFaces_.append(tgtFacei);
debugFaceSides_.append(ictEdgeSide);
}
}
}
}
// If the source face couples the target, then store the intersection
if (srcCouples)
{
srcLocalTgtFaces_[srcFacei].append(tgtFacei);
srcCouples_[srcFacei].append(srcCouple);
}
// If any intersection has occurred then store the edge and error parts
if (anyCouples)
{
forAll(srcFaceEdgeParts_[srcFacei], srcFaceEdgei)
{
srcFaceEdgeParts_[srcFacei][srcFaceEdgei] +=
srcFaceEdgePart_[srcFaceEdgei];
}
srcErrorParts_[srcFacei] +=
reverse() ? sum(tgtFaceEdgePart_) : -sum(tgtFaceEdgePart_);
srcErrorParts_[srcFacei] += errorPart;
}
// If the target face couples the source, then store in the intersection
if (tgtCouples)
{
tgtLocalSrcFaces_[tgtFacei].append(srcFacei);
tgtCouples_[tgtFacei].append(tgtCouple);
}
return anyCouples;
}
void Foam::patchToPatches::intersection::initialise
(
const primitiveOldTimePatch& srcPatch,
const vectorField& srcPointNormals,
const vectorField& srcPointNormals0,
const primitiveOldTimePatch& tgtPatch
)
{
patchToPatch::initialise
(
srcPatch,
srcPointNormals,
srcPointNormals0,
tgtPatch
);
srcCouples_.resize(srcPatch.size());
forAll(srcLocalTgtFaces_, i)
{
srcCouples_[i].clear();
}
srcEdgeParts_.resize(srcPatch.nEdges());
forAll(srcEdgeParts_, srcEdgei)
{
const edge& e = srcPatch.edges()[srcEdgei];
const point c = e.centre(srcPatch.localPoints());
srcEdgeParts_[srcEdgei] = part(Zero, c);
}
srcErrorParts_.resize(srcPatch.size());
forAll(srcErrorParts_, srcFacei)
{
srcErrorParts_[srcFacei] =
part(Zero, srcPatch.faceCentres()[srcFacei]);
}
tgtCouples_.resize(tgtPatch.size());
forAll(tgtLocalSrcFaces_, i)
{
tgtCouples_[i].clear();
}
srcTriPoints_ = List<triFaceList>(srcPatch.size());
srcTriFaceEdges_ = List<List<FixedList<label, 3>>>(srcPatch.size());
tgtTriPoints_ = List<triFaceList>(tgtPatch.size());
tgtTriFaceEdges_ = List<List<FixedList<label, 3>>>(tgtPatch.size());
srcFaceEdgeParts_.resize(srcPatch.size());
forAll(srcFaceEdgeParts_, srcFacei)
{
srcFaceEdgeParts_[srcFacei].resize(srcPatch[srcFacei].size());
forAll(srcFaceEdgeParts_[srcFacei], srcFaceEdgei)
{
const label srcEdgei =
srcPatch.faceEdges()[srcFacei][srcFaceEdgei];
srcFaceEdgeParts_[srcFacei][srcFaceEdgei] = srcEdgeParts_[srcEdgei];
}
}
if (debug > 1)
{
debugPoints_.clear();
debugFaces_.clear();
debugFaceSrcFaces_.clear();
debugFaceTgtFaces_.clear();
debugFaceSides_.clear();
}
}
Foam::labelList Foam::patchToPatches::intersection::finaliseLocal
(
const primitiveOldTimePatch& srcPatch,
const vectorField& srcPointNormals,
const vectorField& srcPointNormals0,
const primitiveOldTimePatch& tgtPatch
)
{
const labelList newToOldLocalTgtFace =
patchToPatch::finaliseLocal
(
srcPatch,
srcPointNormals,
srcPointNormals0,
tgtPatch
);
tgtCouples_ = List<DynamicList<couple>>(tgtCouples_, newToOldLocalTgtFace);
return newToOldLocalTgtFace;
}
void Foam::patchToPatches::intersection::rDistributeTgt
(
const primitiveOldTimePatch& tgtPatch
)
{
patchToPatch::rDistributeTgt(tgtPatch);
patchToPatchTools::rDistributeListList
(
tgtPatch.size(),
tgtMapPtr_(),
tgtCouples_
);
}
Foam::label Foam::patchToPatches::intersection::finalise
(
const primitiveOldTimePatch& srcPatch,
const vectorField& srcPointNormals,
const vectorField& srcPointNormals0,
const primitiveOldTimePatch& tgtPatch,
const transformer& tgtToSrc
)
{
const label nCouples =
patchToPatch::finalise
(
srcPatch,
srcPointNormals,
srcPointNormals0,
tgtPatch,
tgtToSrc
);
// Convert face-edge-parts to edge-parts
labelList srcEdgeNParts(srcEdgeParts_.size(), 0);
forAll(srcEdgeParts_, srcEdgei)
{
const edge& e = srcPatch.edges()[srcEdgei];
srcEdgeParts_[srcEdgei] = part();
forAll(srcPatch.edgeFaces()[srcEdgei], i)
{
const label srcFacei = srcPatch.edgeFaces()[srcEdgei][i];
const label srcFaceEdgei =
findIndex(srcPatch.faceEdges()[srcFacei], srcEdgei);
const edge fe =
srcPatch.localFaces()[srcFacei].faceEdge(srcFaceEdgei);
if (edge::compare(e, fe) > 0)
{
srcEdgeParts_[srcEdgei] +=
srcFaceEdgeParts_[srcFacei][srcFaceEdgei];
}
else
{
srcEdgeParts_[srcEdgei] -=
srcFaceEdgeParts_[srcFacei][srcFaceEdgei];
}
srcEdgeNParts[srcEdgei] ++;
}
}
forAll(srcEdgeParts_, srcEdgei)
{
srcEdgeParts_[srcEdgei].area /= srcEdgeNParts[srcEdgei];
}
// Add the difference between the face-edge-part and the edge-part into the
// face-error-parts
forAll(srcEdgeParts_, srcEdgei)
{
const edge& e = srcPatch.edges()[srcEdgei];
forAll(srcPatch.edgeFaces()[srcEdgei], i)
{
const label srcFacei = srcPatch.edgeFaces()[srcEdgei][i];
const label srcFaceEdgei =
findIndex(srcPatch.faceEdges()[srcFacei], srcEdgei);
const edge fe =
srcPatch.localFaces()[srcFacei].faceEdge(srcFaceEdgei);
if (edge::compare(e, fe) > 0)
{
srcErrorParts_[srcFacei] -= srcEdgeParts_[srcEdgei];
}
else
{
srcErrorParts_[srcFacei] += srcEdgeParts_[srcEdgei];
}
srcErrorParts_[srcFacei] +=
srcFaceEdgeParts_[srcFacei][srcFaceEdgei];
}
}
// Transform the target couples back to the target side
if (!isNull(tgtToSrc))
{
forAll(tgtCouples_, tgtFacei)
{
forAll(tgtCouples_[tgtFacei], i)
{
couple& c = tgtCouples_[tgtFacei][i];
c.area = tgtToSrc.invTransform(c.area);
c.centre = tgtToSrc.invTransformPosition(c.centre);
c.nbr.area = tgtToSrc.invTransform(c.nbr.area);
c.nbr.centre = tgtToSrc.invTransformPosition(c.nbr.centre);
}
}
}
// Calculate coverage and total areas on both sides
auto coverage = []
(
const primitivePatch& patch,
const List<DynamicList<couple>>& couples,
scalar& area,
scalar& coupleArea,
List<scalar>& coverage
)
{
area = 0;
coupleArea = 0;
coverage.resize(patch.size());
forAll(patch, facei)
{
const scalar magA = mag(patch.faceAreas()[facei]);
vector aCouple = Zero;
forAll(couples[facei], i)
{
aCouple += couples[facei][i].area;
}
const scalar magACouple = mag(aCouple);
area += magA;
coupleArea += magACouple;
coverage[facei] = magACouple/magA;
}
reduce(area, sumOp());
reduce(coupleArea, sumOp());
};
scalar srcArea = 0, srcCoupleArea = 0;
scalar tgtArea = 0, tgtCoupleArea = 0;
coverage(srcPatch, srcCouples_, srcArea, srcCoupleArea, srcCoverage_);
coverage(tgtPatch, tgtCouples_, tgtArea, tgtCoupleArea, tgtCoverage_);
// Clear the triangulation workspace
srcTriPoints_.clear();
srcTriFaceEdges_.clear();
tgtTriPoints_.clear();
tgtTriFaceEdges_.clear();
// Clear face-edge-parts
srcFaceEdgePart_.clear();
tgtFaceEdgePart_.clear();
srcFaceEdgeParts_.clear();
// Checking and reporting
if (nCouples != 0)
{
scalarField srcOpenness(srcPatch.size());
scalarField srcError(srcPatch.size());
scalarField srcDepth(srcPatch.size());
scalarField srcAngleDeg(srcPatch.size());
forAll(srcPatch, srcFacei)
{
const vector& a = srcPatch.faceAreas()[srcFacei];
const scalar magA = mag(a);
const point& c = srcPatch.faceCentres()[srcFacei];
couple Cpl(part(Zero, c), part(Zero, c));
forAll(srcCouples_[srcFacei], srcTgtFacei)
{
const couple& cpl = srcCouples_[srcFacei][srcTgtFacei];
Cpl += cpl;
Cpl.nbr += cpl.nbr;
}
vector projectionA = Zero;
scalar projectionV = 0;
forAll(srcCouples_[srcFacei], srcTgtFacei)
{
const couple& cpl = srcCouples_[srcFacei][srcTgtFacei];
projectionA += cpl.nbr.area;
projectionV +=
- (cpl.area/3 & (cpl.centre - Cpl.centre))
+ (cpl.nbr.area/3 & (cpl.nbr.centre - Cpl.centre));
}
forAll(srcPatch.faceEdges()[srcFacei], srcFaceEdgei)
{
const label srcEdgei =
srcPatch.faceEdges()[srcFacei][srcFaceEdgei];
const edge& e = srcPatch.edges()[srcEdgei];
const edge fe =
srcPatch.localFaces()[srcFacei].faceEdge(srcFaceEdgei);
const scalar sign = edge::compare(e, fe);
projectionA += sign*srcEdgeParts_[srcEdgei].area;
projectionV +=
sign*srcEdgeParts_[srcEdgei].area/3
& (srcEdgeParts_[srcEdgei].centre - Cpl.centre);
}
projectionA += srcErrorParts_[srcFacei].area;
projectionV +=
srcErrorParts_[srcFacei].area/3
& (srcErrorParts_[srcFacei].centre - Cpl.centre);
const vector aHat = normalised(a);
const vector aOppHat = normalised(a - Cpl.area + Cpl.nbr.area);
srcAngleDeg[srcFacei] =
radToDeg(acos(min(max(aHat & aOppHat, -1), +1)));
srcOpenness[srcFacei] = mag(projectionA - Cpl.area)/magA;
srcError[srcFacei] = mag(srcErrorParts_[srcFacei].area)/magA;
srcDepth[srcFacei] = mag(projectionV)/pow3(sqrt(magA));
}
reduce(tgtArea, sumOp());
reduce(tgtCoupleArea, sumOp());
Info<< indent << "Source min/average/max coverage = "
<< gMin(srcCoverage_) << '/' << srcCoupleArea/srcArea << '/'
<< gMax(srcCoverage_) << endl
<< indent << "Target min/average/max coverage = "
<< gMin(tgtCoverage_) << '/' << tgtCoupleArea/tgtArea << '/'
<< gMax(tgtCoverage_) << endl
<< indent << "Source average openness/error/depth/angle = "
<< gAverage(srcOpenness) << '/' << gAverage(srcError) << '/'
<< gAverage(srcDepth) << '/' << gAverage(srcAngleDeg) << endl
<< indent << "Source max openness/error/depth/angle = "
<< gMax(srcOpenness) << '/' << gMax(srcError) << '/'
<< gMax(srcDepth) << '/' << gMax(srcAngleDeg) << endl;
if (debug)
{
word name = patchToPatch::typeName + '_' + typeName;
if (Pstream::parRun())
{
name += "_proc" + Foam::name(Pstream::myProcNo());
}
Info<< indent << "Writing source patch to "
<< name + "_srcPatch.vtk" << endl;
vtkWritePolyData::write
(
name + "_srcPatch" + ".vtk",
name + "_srcPatch",
false,
srcPatch.localPoints(),
labelList(),
labelListList(),
srcPatch.localFaces(),
"coverage", false, scalarField(srcCoverage_),
"openness", false, srcOpenness,
"error", false, srcError,
"depth", false, srcDepth,
"angle", false, srcAngleDeg,
"normals", true, srcPointNormals
);
Info<< indent << "Writing target patch to "
<< name + "_tgtPatch.vtk" << endl;
vtkWritePolyData::write
(
name + "_tgtPatch" + ".vtk",
name + "_tgtPatch",
false,
tgtPatch.localPoints(),
labelList(),
labelListList(),
tgtPatch.localFaces(),
"coverage", false, scalarField(tgtCoverage_)
);
if (debug > 1)
{
Info<< indent << "Writing intersected faces to "
<< name + ".vtk" << endl;
vtkWritePolyData::write
(
name + ".vtk",
name,
false,
debugPoints_,
labelList(),
labelListList(),
debugFaces_,
"srcFace", false, Field<label>(debugFaceSrcFaces_),
"tgtFace", false, Field<label>(debugFaceTgtFaces_),
"side", false, Field<label>(debugFaceSides_)
);
debugPoints_.clear();
debugFaces_.clear();
debugFaceSrcFaces_.clear();
debugFaceTgtFaces_.clear();
debugFaceSides_.clear();
}
}
}
return nCouples;
}
Foam::tmpNrc<Foam::List<Foam::DynamicList<Foam::scalar>>>
Foam::patchToPatches::intersection::srcWeights() const
{
List<DynamicList<scalar>>* resultPtr
(
new List<DynamicList<scalar>>(srcCouples_.size())
);
List<DynamicList<scalar>>& result = *resultPtr;
forAll(srcCouples_, srcFacei)
{
result[srcFacei].resize(srcCouples_[srcFacei].size());
scalar aSum = 0;
forAll(srcCouples_[srcFacei], i)
{
const scalar a = mag(srcCouples_[srcFacei][i].area);
result[srcFacei][i] = a;
aSum += a;
}
forAll(srcCouples_[srcFacei], i)
{
result[srcFacei][i] *=
min(max(srcCoverage_[srcFacei], small), scalar(1))/aSum;
}
}
return tmpNrc<List<DynamicList<scalar>>>(resultPtr);
}
Foam::tmpNrc<Foam::List<Foam::DynamicList<Foam::scalar>>>
Foam::patchToPatches::intersection::tgtWeights() const
{
List<DynamicList<scalar>>* resultPtr
(
new List<DynamicList<scalar>>(tgtCouples_.size())
);
List<DynamicList<scalar>>& result = *resultPtr;
forAll(tgtCouples_, tgtFacei)
{
result[tgtFacei].resize(tgtCouples_[tgtFacei].size());
scalar aSum = 0;
forAll(tgtCouples_[tgtFacei], i)
{
const scalar a = mag(tgtCouples_[tgtFacei][i].area);
result[tgtFacei][i] = a;
aSum += a;
}
forAll(tgtCouples_[tgtFacei], i)
{
result[tgtFacei][i] *=
min(max(tgtCoverage_[tgtFacei], small), scalar(1))/aSum;
}
}
return tmpNrc<List<DynamicList<scalar>>>(resultPtr);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::patchToPatches::intersection::intersection(const bool reverse)
:
patchToPatch(reverse),
srcCouples_(),
srcCoverage_(),
srcEdgeParts_(),
srcErrorParts_(),
tgtCouples_(),
tgtCoverage_(),
triEngine_(),
srcTriPoints_(),
srcTriFaceEdges_(),
tgtTriPoints_(),
tgtTriFaceEdges_(),
ictSrcPoints_(),
ictSrcPointNormals_(),
ictTgtPoints_(),
ictPointLocations_(),
srcFaceEdgePart_(),
tgtFaceEdgePart_(),
srcFaceEdgeParts_(),
debugPoints_(),
debugFaces_(),
debugFaceSrcFaces_(),
debugFaceTgtFaces_(),
debugFaceSides_()
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::patchToPatches::intersection::~intersection()
{}
// ************************************************************************* //
|