00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "cellClassification.H"
00030 #include "triSurfaceSearch.H"
00031 #include "indexedOctree.H"
00032 #include "treeDataFace.H"
00033 #include "meshSearch.H"
00034 #include "cellInfo.H"
00035 #include "polyMesh.H"
00036 #include "MeshWave.H"
00037 #include "ListOps.H"
00038 #include "meshTools.H"
00039
00040
00041
00042 namespace Foam
00043 {
00044 defineTypeNameAndDebug(cellClassification, 0);
00045 }
00046
00047
00048
00049
00050 Foam::label Foam::cellClassification::count
00051 (
00052 const labelList& elems,
00053 const label elem
00054 )
00055 {
00056 label cnt = 0;
00057
00058 forAll(elems, elemI)
00059 {
00060 if (elems[elemI] == elem)
00061 {
00062 cnt++;
00063 }
00064 }
00065 return cnt;
00066
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 Foam::boolList Foam::cellClassification::markFaces
00076 (
00077 const triSurfaceSearch& search
00078 ) const
00079 {
00080 cpuTime timer;
00081
00082 boolList cutFace(mesh_.nFaces(), false);
00083
00084 label nCutFaces = 0;
00085
00086
00087
00088
00089 forAll(mesh_.edges(), edgeI)
00090 {
00091 if (debug && (edgeI % 10000 == 0))
00092 {
00093 Pout<< "Intersecting mesh edge " << edgeI << " with surface"
00094 << endl;
00095 }
00096
00097 const edge& e = mesh_.edges()[edgeI];
00098
00099 const point& p0 = mesh_.points()[e.start()];
00100 const point& p1 = mesh_.points()[e.end()];
00101
00102 pointIndexHit pHit(search.tree().findLineAny(p0, p1));
00103
00104 if (pHit.hit())
00105 {
00106 const labelList& myFaces = mesh_.edgeFaces()[edgeI];
00107
00108 forAll(myFaces, myFaceI)
00109 {
00110 label faceI = myFaces[myFaceI];
00111
00112 if (!cutFace[faceI])
00113 {
00114 cutFace[faceI] = true;
00115
00116 nCutFaces++;
00117 }
00118 }
00119 }
00120 }
00121
00122 if (debug)
00123 {
00124 Pout<< "Intersected edges of mesh with surface in = "
00125 << timer.cpuTimeIncrement() << " s\n" << endl << endl;
00126 }
00127
00128
00129
00130
00131
00132 labelList allFaces(mesh_.nFaces() - nCutFaces);
00133
00134 label allFaceI = 0;
00135
00136 forAll(cutFace, faceI)
00137 {
00138 if (!cutFace[faceI])
00139 {
00140 allFaces[allFaceI++] = faceI;
00141 }
00142 }
00143
00144 if (debug)
00145 {
00146 Pout<< "Testing " << allFaceI << " faces for piercing by surface"
00147 << endl;
00148 }
00149
00150 treeBoundBox allBb(mesh_.points());
00151
00152
00153 scalar tol = 1E-6 * allBb.avgDim();
00154
00155 point& bbMin = allBb.min();
00156 bbMin.x() -= tol;
00157 bbMin.y() -= tol;
00158 bbMin.z() -= tol;
00159
00160 point& bbMax = allBb.max();
00161 bbMax.x() += 2*tol;
00162 bbMax.y() += 2*tol;
00163 bbMax.z() += 2*tol;
00164
00165 indexedOctree<treeDataFace> faceTree
00166 (
00167 treeDataFace(false, mesh_, allFaces),
00168 allBb,
00169 8,
00170 10,
00171 3.0
00172 );
00173
00174 const triSurface& surf = search.surface();
00175 const edgeList& edges = surf.edges();
00176 const pointField& localPoints = surf.localPoints();
00177
00178 label nAddFaces = 0;
00179
00180 forAll(edges, edgeI)
00181 {
00182 if (debug && (edgeI % 10000 == 0))
00183 {
00184 Pout<< "Intersecting surface edge " << edgeI
00185 << " with mesh faces" << endl;
00186 }
00187 const edge& e = edges[edgeI];
00188
00189 const point& start = localPoints[e.start()];
00190 const point& end = localPoints[e.end()];
00191
00192 vector edgeNormal(end - start);
00193 const scalar edgeMag = mag(edgeNormal);
00194 const vector smallVec = 1E-9*edgeNormal;
00195
00196 edgeNormal /= edgeMag+VSMALL;
00197
00198
00199 point pt = start;
00200
00201 while (true)
00202 {
00203 pointIndexHit pHit(faceTree.findLine(pt, end));
00204
00205 if (!pHit.hit())
00206 {
00207 break;
00208 }
00209 else
00210 {
00211 label faceI = faceTree.shapes().faceLabels()[pHit.index()];
00212
00213 if (!cutFace[faceI])
00214 {
00215 cutFace[faceI] = true;
00216
00217 nAddFaces++;
00218 }
00219
00220
00221 pt = pHit.hitPoint() + smallVec;
00222
00223 if (((pt-start) & edgeNormal) >= edgeMag)
00224 {
00225 break;
00226 }
00227 }
00228 }
00229 }
00230
00231 if (debug)
00232 {
00233 Pout<< "Detected an additional " << nAddFaces << " faces cut"
00234 << endl;
00235
00236 Pout<< "Intersected edges of surface with mesh faces in = "
00237 << timer.cpuTimeIncrement() << " s\n" << endl << endl;
00238 }
00239
00240 return cutFace;
00241 }
00242
00243
00244
00245
00246
00247 void Foam::cellClassification::markCells
00248 (
00249 const meshSearch& queryMesh,
00250 const boolList& piercedFace,
00251 const pointField& outsidePts
00252 )
00253 {
00254
00255
00256
00257
00258 List<cellInfo> cellInfoList(mesh_.nCells());
00259
00260
00261 forAll(piercedFace, faceI)
00262 {
00263 if (piercedFace[faceI])
00264 {
00265 cellInfoList[mesh_.faceOwner()[faceI]] =
00266 cellInfo(cellClassification::CUT);
00267
00268 if (mesh_.isInternalFace(faceI))
00269 {
00270 cellInfoList[mesh_.faceNeighbour()[faceI]] =
00271 cellInfo(cellClassification::CUT);
00272 }
00273 }
00274 }
00275
00276
00277
00278
00279
00280
00281 labelHashSet outsideFacesMap(outsidePts.size() * 6 * 2);
00282
00283 forAll(outsidePts, outsidePtI)
00284 {
00285
00286 label cellI = queryMesh.findCell(outsidePts[outsidePtI], -1, false);
00287
00288 if (cellI == -1)
00289 {
00290 FatalErrorIn
00291 (
00292 "List<cellClassification::cType> markCells"
00293 "(const meshSearch&, const boolList&, const pointField&)"
00294 ) << "outsidePoint " << outsidePts[outsidePtI]
00295 << " is not inside any cell"
00296 << nl << "It might be on a face or outside the geometry"
00297 << exit(FatalError);
00298 }
00299
00300 cellInfoList[cellI] = cellInfo(cellClassification::OUTSIDE);
00301
00302
00303 const labelList& myFaces = mesh_.cells()[cellI];
00304 forAll(myFaces, myFaceI)
00305 {
00306 outsideFacesMap.insert(myFaces[myFaceI]);
00307 }
00308 }
00309
00310
00311
00312
00313
00314
00315 labelList changedFaces(outsideFacesMap.toc());
00316
00317 List<cellInfo> changedFacesInfo
00318 (
00319 changedFaces.size(),
00320 cellInfo(cellClassification::OUTSIDE)
00321 );
00322
00323 MeshWave<cellInfo> cellInfoCalc
00324 (
00325 mesh_,
00326 changedFaces,
00327 changedFacesInfo,
00328 cellInfoList,
00329 mesh_.globalData().nTotalCells()
00330 );
00331
00332
00333 const List<cellInfo>& allInfo = cellInfoCalc.allCellInfo();
00334
00335 forAll(allInfo, cellI)
00336 {
00337 label t = allInfo[cellI].type();
00338
00339 if (t == cellClassification::NOTSET)
00340 {
00341 t = cellClassification::INSIDE;
00342 }
00343 operator[](cellI) = t;
00344 }
00345 }
00346
00347
00348 void Foam::cellClassification::classifyPoints
00349 (
00350 const label meshType,
00351 const labelList& cellType,
00352 List<pointStatus>& pointSide
00353 ) const
00354 {
00355 pointSide.setSize(mesh_.nPoints());
00356
00357 forAll(mesh_.pointCells(), pointI)
00358 {
00359 const labelList& pCells = mesh_.pointCells()[pointI];
00360
00361 pointSide[pointI] = UNSET;
00362
00363 forAll(pCells, i)
00364 {
00365 label type = cellType[pCells[i]];
00366
00367 if (type == meshType)
00368 {
00369 if (pointSide[pointI] == UNSET)
00370 {
00371 pointSide[pointI] = MESH;
00372 }
00373 else if (pointSide[pointI] == NONMESH)
00374 {
00375 pointSide[pointI] = MIXED;
00376
00377 break;
00378 }
00379 }
00380 else
00381 {
00382 if (pointSide[pointI] == UNSET)
00383 {
00384 pointSide[pointI] = NONMESH;
00385 }
00386 else if (pointSide[pointI] == MESH)
00387 {
00388 pointSide[pointI] = MIXED;
00389
00390 break;
00391 }
00392 }
00393 }
00394 }
00395 }
00396
00397
00398 bool Foam::cellClassification::usesMixedPointsOnly
00399 (
00400 const List<pointStatus>& pointSide,
00401 const label cellI
00402 ) const
00403 {
00404 const faceList& faces = mesh_.faces();
00405
00406 const cell& cFaces = mesh_.cells()[cellI];
00407
00408 forAll(cFaces, cFaceI)
00409 {
00410 const face& f = faces[cFaces[cFaceI]];
00411
00412 forAll(f, fp)
00413 {
00414 if (pointSide[f[fp]] != MIXED)
00415 {
00416 return false;
00417 }
00418 }
00419 }
00420
00421
00422 return true;
00423 }
00424
00425
00426 void Foam::cellClassification::getMeshOutside
00427 (
00428 const label meshType,
00429 faceList& outsideFaces,
00430 labelList& outsideOwner
00431 ) const
00432 {
00433 const labelList& own = mesh_.faceOwner();
00434 const labelList& nbr = mesh_.faceNeighbour();
00435 const faceList& faces = mesh_.faces();
00436
00437 outsideFaces.setSize(mesh_.nFaces());
00438 outsideOwner.setSize(mesh_.nFaces());
00439 label outsideI = 0;
00440
00441
00442
00443 for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00444 {
00445 label ownType = operator[](own[faceI]);
00446 label nbrType = operator[](nbr[faceI]);
00447
00448 if (ownType == meshType && nbrType != meshType)
00449 {
00450 outsideFaces[outsideI] = faces[faceI];
00451 outsideOwner[outsideI] = own[faceI];
00452 outsideI++;
00453 }
00454 else if (ownType != meshType && nbrType == meshType)
00455 {
00456 outsideFaces[outsideI] = faces[faceI];
00457 outsideOwner[outsideI] = nbr[faceI];
00458 outsideI++;
00459 }
00460 }
00461
00462
00463
00464 for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00465 {
00466 if (operator[](own[faceI]) == meshType)
00467 {
00468 outsideFaces[outsideI] = faces[faceI];
00469 outsideOwner[outsideI] = own[faceI];
00470 outsideI++;
00471 }
00472 }
00473 outsideFaces.setSize(outsideI);
00474 outsideOwner.setSize(outsideI);
00475 }
00476
00477
00478
00479
00480
00481 Foam::cellClassification::cellClassification
00482 (
00483 const polyMesh& mesh,
00484 const meshSearch& meshQuery,
00485 const triSurfaceSearch& surfQuery,
00486 const pointField& outsidePoints
00487 )
00488 :
00489 labelList(mesh.nCells(), cellClassification::NOTSET),
00490 mesh_(mesh)
00491 {
00492 markCells
00493 (
00494 meshQuery,
00495 markFaces(surfQuery),
00496 outsidePoints
00497 );
00498 }
00499
00500
00501
00502 Foam::cellClassification::cellClassification
00503 (
00504 const polyMesh& mesh,
00505 const labelList& cellType
00506 )
00507 :
00508 labelList(cellType),
00509 mesh_(mesh)
00510 {
00511 if (mesh_.nCells() != size())
00512 {
00513 FatalErrorIn
00514 (
00515 "cellClassification::cellClassification"
00516 "(const polyMesh&, const labelList&)"
00517 ) << "Number of elements of cellType argument is not equal to the"
00518 << " number of cells" << abort(FatalError);
00519 }
00520 }
00521
00522
00523
00524 Foam::cellClassification::cellClassification(const cellClassification& cType)
00525 :
00526 labelList(cType),
00527 mesh_(cType.mesh())
00528 {}
00529
00530
00531
00532
00533
00534
00535
00536 Foam::label Foam::cellClassification::trimCutCells
00537 (
00538 const label nLayers,
00539 const label meshType,
00540 const label fillType
00541 )
00542 {
00543
00544 labelList newCellType(*this);
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 newCellType = *this;
00562
00563
00564
00565 for (label iter = 0; iter < nLayers; iter++)
00566 {
00567
00568
00569
00570 List<pointStatus> pointSide(mesh_.nPoints());
00571 classifyPoints(meshType, newCellType, pointSide);
00572
00573
00574 forAll(pointSide, pointI)
00575 {
00576 if (pointSide[pointI] == MIXED)
00577 {
00578
00579 const labelList& pCells = mesh_.pointCells()[pointI];
00580
00581 forAll(pCells, i)
00582 {
00583 label type = newCellType[pCells[i]];
00584
00585 if (type == cellClassification::CUT)
00586 {
00587
00588
00589 newCellType[pCells[i]] = meshType;
00590 }
00591 }
00592 }
00593 }
00594 }
00595
00596
00597
00598
00599
00600
00601 label nChanged = 0;
00602
00603 forAll(newCellType, cellI)
00604 {
00605 if (operator[](cellI) == cellClassification::CUT)
00606 {
00607 if (newCellType[cellI] != meshType)
00608 {
00609
00610
00611 operator[](cellI) = fillType;
00612 nChanged++;
00613 }
00614 }
00615 }
00616
00617 return nChanged;
00618 }
00619
00620
00621
00622 Foam::label Foam::cellClassification::growSurface
00623 (
00624 const label meshType,
00625 const label fillType
00626 )
00627 {
00628 boolList hasMeshType(mesh_.nPoints(), false);
00629
00630
00631
00632 forAll(mesh_.pointCells(), pointI)
00633 {
00634 const labelList& myCells = mesh_.pointCells()[pointI];
00635
00636
00637 forAll(myCells, myCellI)
00638 {
00639 label type = operator[](myCells[myCellI]);
00640
00641 if (type == meshType)
00642 {
00643 hasMeshType[pointI] = true;
00644
00645 break;
00646 }
00647 }
00648 }
00649
00650
00651
00652 label nChanged = 0;
00653
00654 forAll(hasMeshType, pointI)
00655 {
00656 if (hasMeshType[pointI])
00657 {
00658 const labelList& myCells = mesh_.pointCells()[pointI];
00659
00660 forAll(myCells, myCellI)
00661 {
00662 if (operator[](myCells[myCellI]) != meshType)
00663 {
00664 operator[](myCells[myCellI]) = fillType;
00665
00666 nChanged++;
00667 }
00668 }
00669 }
00670 }
00671 return nChanged;
00672 }
00673
00674
00675
00676
00677
00678
00679 Foam::label Foam::cellClassification::fillHangingCells
00680 (
00681 const label meshType,
00682 const label fillType,
00683 const label maxIter
00684 )
00685 {
00686 label nTotChanged = 0;
00687
00688 for(label iter = 0; iter < maxIter; iter++)
00689 {
00690 label nChanged = 0;
00691
00692
00693 List<pointStatus> pointSide(mesh_.nPoints());
00694 classifyPoints(meshType, *this, pointSide);
00695
00696
00697
00698
00699 forAll(pointSide, pointI)
00700 {
00701 if (pointSide[pointI] == MIXED)
00702 {
00703 const labelList& pCells = mesh_.pointCells()[pointI];
00704
00705 forAll(pCells, i)
00706 {
00707 label cellI = pCells[i];
00708
00709 if (operator[](cellI) == meshType)
00710 {
00711 if (usesMixedPointsOnly(pointSide, cellI))
00712 {
00713 operator[](cellI) = fillType;
00714
00715 nChanged++;
00716 }
00717 }
00718 }
00719 }
00720 }
00721 nTotChanged += nChanged;
00722
00723 Pout<< "removeHangingCells : changed " << nChanged
00724 << " hanging cells" << endl;
00725
00726 if (nChanged == 0)
00727 {
00728 break;
00729 }
00730 }
00731
00732 return nTotChanged;
00733 }
00734
00735
00736 Foam::label Foam::cellClassification::fillRegionEdges
00737 (
00738 const label meshType,
00739 const label fillType,
00740 const label maxIter
00741 )
00742 {
00743 label nTotChanged = 0;
00744
00745 for(label iter = 0; iter < maxIter; iter++)
00746 {
00747
00748
00749 faceList outsideFaces;
00750 labelList outsideOwner;
00751
00752 getMeshOutside(meshType, outsideFaces, outsideOwner);
00753
00754
00755 primitiveFacePatch fp(outsideFaces, mesh_.points());
00756
00757 const labelListList& edgeFaces = fp.edgeFaces();
00758
00759 label nChanged = 0;
00760
00761
00762
00763 forAll(edgeFaces, edgeI)
00764 {
00765 const labelList& eFaces = edgeFaces[edgeI];
00766
00767 if (eFaces.size() > 2)
00768 {
00769
00770
00771
00772 forAll(eFaces, i)
00773 {
00774 label patchFaceI = eFaces[i];
00775
00776 label ownerCell = outsideOwner[patchFaceI];
00777
00778 if (operator[](ownerCell) == meshType)
00779 {
00780 operator[](ownerCell) = fillType;
00781
00782 nChanged++;
00783
00784 break;
00785 }
00786 }
00787 }
00788 }
00789
00790 nTotChanged += nChanged;
00791
00792 Pout<< "fillRegionEdges : changed " << nChanged
00793 << " cells using multiply connected edges" << endl;
00794
00795 if (nChanged == 0)
00796 {
00797 break;
00798 }
00799 }
00800
00801 return nTotChanged;
00802 }
00803
00804
00805 Foam::label Foam::cellClassification::fillRegionPoints
00806 (
00807 const label meshType,
00808 const label fillType,
00809 const label maxIter
00810 )
00811 {
00812 label nTotChanged = 0;
00813
00814 for(label iter = 0; iter < maxIter; iter++)
00815 {
00816
00817
00818 faceList outsideFaces;
00819 labelList outsideOwner;
00820
00821 getMeshOutside(meshType, outsideFaces, outsideOwner);
00822
00823
00824 primitiveFacePatch fp(outsideFaces, mesh_.points());
00825
00826 labelHashSet nonManifoldPoints;
00827
00828
00829 fp.checkPointManifold(false, &nonManifoldPoints);
00830
00831 const Map<label>& meshPointMap = fp.meshPointMap();
00832
00833 label nChanged = 0;
00834
00835 for
00836 (
00837 labelHashSet::const_iterator iter = nonManifoldPoints.begin();
00838 iter != nonManifoldPoints.end();
00839 ++iter
00840 )
00841 {
00842
00843 label patchPointI = meshPointMap[iter.key()];
00844
00845 const labelList& pFaces = fp.pointFaces()[patchPointI];
00846
00847
00848
00849
00850 forAll(pFaces, i)
00851 {
00852 label patchFaceI = pFaces[i];
00853
00854 label ownerCell = outsideOwner[patchFaceI];
00855
00856 if (operator[](ownerCell) == meshType)
00857 {
00858 operator[](ownerCell) = fillType;
00859
00860 nChanged++;
00861
00862 break;
00863 }
00864 }
00865 }
00866
00867 nTotChanged += nChanged;
00868
00869 Pout<< "fillRegionPoints : changed " << nChanged
00870 << " cells using multiply connected points" << endl;
00871
00872 if (nChanged == 0)
00873 {
00874 break;
00875 }
00876 }
00877
00878 return nTotChanged;
00879 }
00880
00881
00882 void Foam::cellClassification::writeStats(Ostream& os) const
00883 {
00884 os << "Cells:" << size() << endl
00885 << " notset : " << count(*this, NOTSET) << endl
00886 << " cut : " << count(*this, CUT) << endl
00887 << " inside : " << count(*this, INSIDE) << endl
00888 << " outside : " << count(*this, OUTSIDE) << endl;
00889 }
00890
00891
00892
00893
00894 void Foam::cellClassification::operator=(const Foam::cellClassification& rhs)
00895 {
00896 labelList::operator=(rhs);
00897 }
00898
00899
00900