File size: 22,803 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 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-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 "cellClassification.H"
#include "triSurfaceSearch.H"
#include "meshSearch.H"
#include "treeDataFace.H"
#include "cellInfo.H"
#include "polyMesh.H"
#include "FaceCellWave.H"
#include "ListOps.H"
#include "meshTools.H"
#include "cpuTime.H"
#include "triSurface.H"
#include "globalMeshData.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(cellClassification, 0);
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::label Foam::cellClassification::count
(
const labelList& elems,
const label elem
)
{
label cnt = 0;
forAll(elems, elemI)
{
if (elems[elemI] == elem)
{
cnt++;
}
}
return cnt;
}
// Mark all faces that are cut by the surface. Two pass:
// Pass1: mark all mesh edges that intersect surface (quick since triangle
// pierce test).
// Pass2: Check for all point neighbours of these faces whether any of their
// faces are pierced.
Foam::boolList Foam::cellClassification::markFaces
(
const triSurfaceSearch& search
) const
{
cpuTime timer;
boolList cutFace(mesh_.nFaces(), false);
label nCutFaces = 0;
// Intersect mesh edges with surface (is fast) and mark all faces that
// use edge.
forAll(mesh_.edges(), edgeI)
{
if (debug && (edgeI % 10000 == 0))
{
Pout<< "Intersecting mesh edge " << edgeI << " with surface"
<< endl;
}
const edge& e = mesh_.edges()[edgeI];
const point& p0 = mesh_.points()[e.start()];
const point& p1 = mesh_.points()[e.end()];
pointIndexHit pHit(search.tree().findLineAny(p0, p1));
if (pHit.hit())
{
const labelList& myFaces = mesh_.edgeFaces()[edgeI];
forAll(myFaces, myFacei)
{
label facei = myFaces[myFacei];
if (!cutFace[facei])
{
cutFace[facei] = true;
nCutFaces++;
}
}
}
}
if (debug)
{
Pout<< "Intersected edges of mesh with surface in = "
<< timer.cpuTimeIncrement() << " s\n" << endl << endl;
}
//
// Construct octree on faces that have not yet been marked as cut
//
labelList allFaces(mesh_.nFaces() - nCutFaces);
label allFacei = 0;
forAll(cutFace, facei)
{
if (!cutFace[facei])
{
allFaces[allFacei++] = facei;
}
}
if (debug)
{
Pout<< "Testing " << allFacei << " faces for piercing by surface"
<< endl;
}
treeBoundBox allBb(mesh_.points());
// Extend domain slightly (also makes it 3D if was 2D)
scalar tol = 1e-6 * allBb.avgDim();
point& bbMin = allBb.min();
bbMin.x() -= tol;
bbMin.y() -= tol;
bbMin.z() -= tol;
point& bbMax = allBb.max();
bbMax.x() += 2*tol;
bbMax.y() += 2*tol;
bbMax.z() += 2*tol;
indexedOctree<treeDataFace> faceTree
(
treeDataFace(false, mesh_, allFaces),
allBb, // overall search domain
8, // maxLevel
10, // leafsize
3.0 // duplicity
);
const triSurface& surf = search.surface();
const edgeList& edges = surf.edges();
const pointField& localPoints = surf.localPoints();
label nAddFaces = 0;
forAll(edges, edgeI)
{
if (debug && (edgeI % 10000 == 0))
{
Pout<< "Intersecting surface edge " << edgeI
<< " with mesh faces" << endl;
}
const edge& e = edges[edgeI];
const point& start = localPoints[e.start()];
const point& end = localPoints[e.end()];
vector edgeNormal(end - start);
const scalar edgeMag = mag(edgeNormal);
const vector smallVec = 1e-9*edgeNormal;
edgeNormal /= edgeMag+vSmall;
// Current start of pierce test
point pt = start;
while (true)
{
pointIndexHit pHit(faceTree.findLine(pt, end));
if (!pHit.hit())
{
break;
}
else
{
label facei = faceTree.shapes().faceLabels()[pHit.index()];
if (!cutFace[facei])
{
cutFace[facei] = true;
nAddFaces++;
}
// Restart from previous endpoint
pt = pHit.hitPoint() + smallVec;
if (((pt-start) & edgeNormal) >= edgeMag)
{
break;
}
}
}
}
if (debug)
{
Pout<< "Detected an additional " << nAddFaces << " faces cut"
<< endl;
Pout<< "Intersected edges of surface with mesh faces in = "
<< timer.cpuTimeIncrement() << " s\n" << endl << endl;
}
return cutFace;
}
// Determine faces cut by surface and use to divide cells into types. See
// cellInfo. All cells reachable from outsidePts are considered to be of type
// 'outside'
void Foam::cellClassification::markCells
(
const boolList& piercedFace,
const List<point>& outsidePts
)
{
// Use meshwave to partition mesh, starting from outsidePt
// Construct null; sets type to NOTSET
List<cellInfo> cellInfoList(mesh_.nCells());
// Mark cut cells first
forAll(piercedFace, facei)
{
if (piercedFace[facei])
{
cellInfoList[mesh_.faceOwner()[facei]] =
cellInfo(cellClassification::CUT);
if (mesh_.isInternalFace(facei))
{
cellInfoList[mesh_.faceNeighbour()[facei]] =
cellInfo(cellClassification::CUT);
}
}
}
//
// Mark cells containing outside points as being outside
//
// Coarse guess number of faces
labelHashSet outsideFacesMap(outsidePts.size() * 6 * 2);
forAll(outsidePts, outsidePtI)
{
// Use linear search for points.
label celli = meshSearch::findCellNoTree(mesh_, outsidePts[outsidePtI]);
if (returnReduce(celli, maxOp()) == -1)
{
FatalErrorInFunction
<< "outsidePoint " << outsidePts[outsidePtI]
<< " is not inside any cell"
<< nl << "It might be on a face or outside the geometry"
<< exit(FatalError);
}
if (celli >= 0)
{
cellInfoList[celli] = cellInfo(cellClassification::OUTSIDE);
// Mark faces of celli
const labelList& myFaces = mesh_.cells()[celli];
forAll(myFaces, myFacei)
{
outsideFacesMap.insert(myFaces[myFacei]);
}
}
}
//
// Mark faces to start wave from
//
labelList changedFaces(outsideFacesMap.toc());
List<cellInfo> changedFacesInfo
(
changedFaces.size(),
cellInfo(cellClassification::OUTSIDE)
);
List<cellInfo> faceInfoList(mesh().nFaces());
FaceCellWave<cellInfo> cellInfoCalc
(
mesh_,
changedFaces, // Labels of changed faces
changedFacesInfo, // Information on changed faces
faceInfoList,
cellInfoList, // Information on all cells
mesh_.globalData().nTotalCells() + 1 // max iterations
);
forAll(cellInfoList, celli)
{
label t = cellInfoList[celli].type();
if (t == cellClassification::NOTSET)
{
t = cellClassification::INSIDE;
}
operator[](celli) = t;
}
}
void Foam::cellClassification::classifyPoints
(
const label meshType,
const labelList& cellType,
List<pointStatus>& pointSide
) const
{
pointSide.setSize(mesh_.nPoints());
forAll(mesh_.pointCells(), pointi)
{
const labelList& pCells = mesh_.pointCells()[pointi];
pointSide[pointi] = UNSET;
forAll(pCells, i)
{
label type = cellType[pCells[i]];
if (type == meshType)
{
if (pointSide[pointi] == UNSET)
{
pointSide[pointi] = MESH;
}
else if (pointSide[pointi] == NONMESH)
{
pointSide[pointi] = MIXED;
break;
}
}
else
{
if (pointSide[pointi] == UNSET)
{
pointSide[pointi] = NONMESH;
}
else if (pointSide[pointi] == MESH)
{
pointSide[pointi] = MIXED;
break;
}
}
}
}
}
bool Foam::cellClassification::usesMixedPointsOnly
(
const List<pointStatus>& pointSide,
const label celli
) const
{
const faceList& faces = mesh_.faces();
const cell& cFaces = mesh_.cells()[celli];
forAll(cFaces, cFacei)
{
const face& f = faces[cFaces[cFacei]];
forAll(f, fp)
{
if (pointSide[f[fp]] != MIXED)
{
return false;
}
}
}
// All points are mixed.
return true;
}
void Foam::cellClassification::getMeshOutside
(
const label meshType,
faceList& outsideFaces,
labelList& outsideOwner
) const
{
const labelList& own = mesh_.faceOwner();
const labelList& nbr = mesh_.faceNeighbour();
const faceList& faces = mesh_.faces();
outsideFaces.setSize(mesh_.nFaces());
outsideOwner.setSize(mesh_.nFaces());
label outsideI = 0;
// Get faces on interface between meshType and non-meshType
for (label facei = 0; facei < mesh_.nInternalFaces(); facei++)
{
label ownType = operator[](own[facei]);
label nbrType = operator[](nbr[facei]);
if (ownType == meshType && nbrType != meshType)
{
outsideFaces[outsideI] = faces[facei];
outsideOwner[outsideI] = own[facei]; // meshType cell
outsideI++;
}
else if (ownType != meshType && nbrType == meshType)
{
outsideFaces[outsideI] = faces[facei];
outsideOwner[outsideI] = nbr[facei]; // meshType cell
outsideI++;
}
}
// Get faces on outside of real mesh with cells of meshType.
for (label facei = mesh_.nInternalFaces(); facei < mesh_.nFaces(); facei++)
{
if (operator[](own[facei]) == meshType)
{
outsideFaces[outsideI] = faces[facei];
outsideOwner[outsideI] = own[facei]; // meshType cell
outsideI++;
}
}
outsideFaces.setSize(outsideI);
outsideOwner.setSize(outsideI);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct from mesh and surface and point(s) on outside
Foam::cellClassification::cellClassification
(
const polyMesh& mesh,
const triSurfaceSearch& surfQuery,
const List<point>& outsidePoints
)
:
labelList(mesh.nCells(), cellClassification::NOTSET),
mesh_(mesh)
{
markCells(markFaces(surfQuery), outsidePoints);
}
// Construct from mesh and meshType.
Foam::cellClassification::cellClassification
(
const polyMesh& mesh,
const labelList& cellType
)
:
labelList(cellType),
mesh_(mesh)
{
if (mesh_.nCells() != size())
{
FatalErrorInFunction
<< "Number of elements of cellType argument is not equal to the"
<< " number of cells" << abort(FatalError);
}
}
// Copy constructor
Foam::cellClassification::cellClassification(const cellClassification& cType)
:
labelList(cType),
mesh_(cType.mesh())
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// Makes cutCells further than nLayers away from meshType to
// fillType. Returns number of cells changed.
Foam::label Foam::cellClassification::trimCutCells
(
const label nLayers,
const label meshType,
const label fillType
)
{
// Temporary cell type for growing.
labelList newCellType(*this);
// // Split types into outside and rest
// forAll(*this, celli)
// {
// label type = operator[](celli);
//
// if (type == meshType)
// {
// newCellType[celli] = type;
// }
// else
// {
// newCellType[celli] = fillType;
// }
// }
newCellType = *this;
// Do point-cell-point walk on newCellType out from meshType cells
for (label iter = 0; iter < nLayers; iter++)
{
// Get status of points: visible from meshType (pointSide == MESH)
// or fillType/cutCells cells (pointSide == NONMESH) or
// both (pointSide == MIXED)
List<pointStatus> pointSide(mesh_.nPoints());
classifyPoints(meshType, newCellType, pointSide);
// Grow layer of outside cells
forAll(pointSide, pointi)
{
if (pointSide[pointi] == MIXED)
{
// Make cut
const labelList& pCells = mesh_.pointCells()[pointi];
forAll(pCells, i)
{
label type = newCellType[pCells[i]];
if (type == cellClassification::CUT)
{
// Found cut cell sharing point with
// mesh type cell. Grow.
newCellType[pCells[i]] = meshType;
}
}
}
}
}
// Merge newCellType into *this:
// - leave meshType cells intact
// - leave nonMesh cells intact
// - make cutcells fillType if they were not marked in newCellType
label nChanged = 0;
forAll(newCellType, celli)
{
if (operator[](celli) == cellClassification::CUT)
{
if (newCellType[celli] != meshType)
{
// Cell was cutCell but further than nLayers away from
// meshType. Convert to fillType.
operator[](celli) = fillType;
nChanged++;
}
}
}
return nChanged;
}
// Grow surface by pointNeighbours of type meshType
Foam::label Foam::cellClassification::growSurface
(
const label meshType,
const label fillType
)
{
boolList hasMeshType(mesh_.nPoints(), false);
// Mark points used by meshType cells
forAll(mesh_.pointCells(), pointi)
{
const labelList& myCells = mesh_.pointCells()[pointi];
// Check if one of cells has meshType
forAll(myCells, myCelli)
{
label type = operator[](myCells[myCelli]);
if (type == meshType)
{
hasMeshType[pointi] = true;
break;
}
}
}
// Change neighbours of marked points
label nChanged = 0;
forAll(hasMeshType, pointi)
{
if (hasMeshType[pointi])
{
const labelList& myCells = mesh_.pointCells()[pointi];
forAll(myCells, myCelli)
{
if (operator[](myCells[myCelli]) != meshType)
{
operator[](myCells[myCelli]) = fillType;
nChanged++;
}
}
}
}
return nChanged;
}
// Check all points used by cells of meshType for use of at least one point
// which is not on the outside. If all points are on the outside of the mesh
// this would probably result in a flattened cell when projecting the cell
// onto the surface.
Foam::label Foam::cellClassification::fillHangingCells
(
const label meshType,
const label fillType,
const label maxIter
)
{
label nTotChanged = 0;
for (label iter = 0; iter < maxIter; iter++)
{
label nChanged = 0;
// Get status of points: visible from meshType or non-meshType cells
List<pointStatus> pointSide(mesh_.nPoints());
classifyPoints(meshType, *this, pointSide);
// Check all cells using mixed point type for whether they use mixed
// points only. Note: could probably speed this up by counting number
// of mixed verts per face and mixed faces per cell or something?
forAll(pointSide, pointi)
{
if (pointSide[pointi] == MIXED)
{
const labelList& pCells = mesh_.pointCells()[pointi];
forAll(pCells, i)
{
label celli = pCells[i];
if (operator[](celli) == meshType)
{
if (usesMixedPointsOnly(pointSide, celli))
{
operator[](celli) = fillType;
nChanged++;
}
}
}
}
}
nTotChanged += nChanged;
Pout<< "removeHangingCells : changed " << nChanged
<< " hanging cells" << endl;
if (nChanged == 0)
{
break;
}
}
return nTotChanged;
}
Foam::label Foam::cellClassification::fillRegionEdges
(
const label meshType,
const label fillType,
const label maxIter
)
{
label nTotChanged = 0;
for (label iter = 0; iter < maxIter; iter++)
{
// Get interface between meshType cells and non-meshType cells as a list
// of faces and for each face the cell which is the meshType.
faceList outsideFaces;
labelList outsideOwner;
getMeshOutside(meshType, outsideFaces, outsideOwner);
// Build primitivePatch out of it and check it for problems.
primitiveFacePatch fp(outsideFaces, mesh_.points());
const labelListList& edgeFaces = fp.edgeFaces();
label nChanged = 0;
// Check all edgeFaces for non-manifoldness
forAll(edgeFaces, edgeI)
{
const labelList& eFaces = edgeFaces[edgeI];
if (eFaces.size() > 2)
{
// patch connected through pinched edge. Remove first face using
// edge (and not yet changed)
forAll(eFaces, i)
{
label patchFacei = eFaces[i];
label ownerCell = outsideOwner[patchFacei];
if (operator[](ownerCell) == meshType)
{
operator[](ownerCell) = fillType;
nChanged++;
break;
}
}
}
}
nTotChanged += nChanged;
Pout<< "fillRegionEdges : changed " << nChanged
<< " cells using multiply connected edges" << endl;
if (nChanged == 0)
{
break;
}
}
return nTotChanged;
}
Foam::label Foam::cellClassification::fillRegionPoints
(
const label meshType,
const label fillType,
const label maxIter
)
{
label nTotChanged = 0;
for (label iter = 0; iter < maxIter; iter++)
{
// Get interface between meshType cells and non-meshType cells as a list
// of faces and for each face the cell which is the meshType.
faceList outsideFaces;
labelList outsideOwner;
getMeshOutside(meshType, outsideFaces, outsideOwner);
// Build primitivePatch out of it and check it for problems.
primitiveFacePatch fp(outsideFaces, mesh_.points());
labelHashSet nonManifoldPoints;
// Check for non-manifold points.
fp.checkPointManifold(false, &nonManifoldPoints);
const Map<label>& meshPointMap = fp.meshPointMap();
label nChanged = 0;
forAllConstIter(labelHashSet, nonManifoldPoints, iter)
{
// Find a face on fp using point and remove it.
const label patchPointi = meshPointMap[iter.key()];
const labelList& pFaces = fp.pointFaces()[patchPointi];
// Remove any face using conflicting point. Does first face which
// has not yet been done. Could be more intelligent and decide which
// one would be best to remove.
forAll(pFaces, i)
{
const label patchFacei = pFaces[i];
const label ownerCell = outsideOwner[patchFacei];
if (operator[](ownerCell) == meshType)
{
operator[](ownerCell) = fillType;
nChanged++;
break;
}
}
}
nTotChanged += nChanged;
Pout<< "fillRegionPoints : changed " << nChanged
<< " cells using multiply connected points" << endl;
if (nChanged == 0)
{
break;
}
}
return nTotChanged;
}
void Foam::cellClassification::writeStats(Ostream& os) const
{
os << "Cells:" << size() << endl
<< " notset : " << count(*this, NOTSET) << endl
<< " cut : " << count(*this, CUT) << endl
<< " inside : " << count(*this, INSIDE) << endl
<< " outside : " << count(*this, OUTSIDE) << endl;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
void Foam::cellClassification::operator=(const Foam::cellClassification& rhs)
{
labelList::operator=(rhs);
}
// ************************************************************************* //
|