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 "boundaryCutter.H"
00030 #include "polyMesh.H"
00031 #include "polyTopoChange.H"
00032 #include "polyAddCell.H"
00033 #include "polyAddFace.H"
00034 #include "polyAddPoint.H"
00035 #include "polyModifyFace.H"
00036 #include "polyModifyPoint.H"
00037 #include "mapPolyMesh.H"
00038 #include "meshTools.H"
00039
00040
00041
00042 namespace Foam
00043 {
00044
00045 defineTypeNameAndDebug(boundaryCutter, 0);
00046
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 void Foam::boundaryCutter::getFaceInfo
00056 (
00057 const label faceI,
00058 label& patchID,
00059 label& zoneID,
00060 label& zoneFlip
00061 ) const
00062 {
00063 patchID = -1;
00064
00065 if (!mesh_.isInternalFace(faceI))
00066 {
00067 patchID = mesh_.boundaryMesh().whichPatch(faceI);
00068 }
00069
00070 zoneID = mesh_.faceZones().whichZone(faceI);
00071
00072 zoneFlip = false;
00073
00074 if (zoneID >= 0)
00075 {
00076 const faceZone& fZone = mesh_.faceZones()[zoneID];
00077
00078 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00079 }
00080 }
00081
00082
00083
00084
00085 Foam::face Foam::boundaryCutter::addEdgeCutsToFace
00086 (
00087 const label faceI,
00088 const Map<labelList>& edgeToAddedPoints
00089 ) const
00090 {
00091 const edgeList& edges = mesh_.edges();
00092 const face& f = mesh_.faces()[faceI];
00093 const labelList& fEdges = mesh_.faceEdges()[faceI];
00094
00095
00096 DynamicList<label> newFace(2 * f.size());
00097
00098 forAll(f, fp)
00099 {
00100
00101 newFace.append(f[fp]);
00102
00103
00104 label v1 = f.nextLabel(fp);
00105
00106 label edgeI = meshTools::findEdge(edges, fEdges, f[fp], v1);
00107
00108 Map<labelList>::const_iterator fnd = edgeToAddedPoints.find(edgeI);
00109
00110 if (fnd != edgeToAddedPoints.end())
00111 {
00112
00113 const labelList& addedPoints = fnd();
00114
00115 if (edges[edgeI].start() == f[fp])
00116 {
00117
00118 forAll(addedPoints, i)
00119 {
00120 newFace.append(addedPoints[i]);
00121 }
00122 }
00123 else
00124 {
00125
00126 forAllReverse(addedPoints, i)
00127 {
00128 newFace.append(addedPoints[i]);
00129 }
00130 }
00131 }
00132 }
00133
00134 face returnFace;
00135 returnFace.transfer(newFace);
00136
00137 if (debug)
00138 {
00139 Pout<< "addEdgeCutsToFace:" << nl
00140 << " from : " << f << nl
00141 << " to : " << returnFace << endl;
00142 }
00143
00144 return returnFace;
00145 }
00146
00147
00148 void Foam::boundaryCutter::addFace
00149 (
00150 const label faceI,
00151 const face& newFace,
00152
00153 bool& modifiedFace,
00154 polyTopoChange& meshMod
00155 ) const
00156 {
00157
00158 label patchID, zoneID, zoneFlip;
00159 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00160 label own = mesh_.faceOwner()[faceI];
00161 label masterPoint = mesh_.faces()[faceI][0];
00162
00163 if (!modifiedFace)
00164 {
00165 meshMod.setAction
00166 (
00167 polyModifyFace
00168 (
00169 newFace,
00170 faceI,
00171 own,
00172 -1,
00173 false,
00174 patchID,
00175 false,
00176 zoneID,
00177 zoneFlip
00178 )
00179 );
00180
00181 modifiedFace = true;
00182 }
00183 else
00184 {
00185 meshMod.setAction
00186 (
00187 polyAddFace
00188 (
00189 newFace,
00190 own,
00191 -1,
00192 masterPoint,
00193 -1,
00194 -1,
00195 false,
00196 patchID,
00197 zoneID,
00198 zoneFlip
00199 )
00200 );
00201 }
00202 }
00203
00204
00205
00206
00207 bool Foam::boundaryCutter::splitFace
00208 (
00209 const label faceI,
00210 const Map<point>& pointToPos,
00211 const Map<labelList>& edgeToAddedPoints,
00212 polyTopoChange& meshMod
00213 ) const
00214 {
00215 const edgeList& edges = mesh_.edges();
00216 const face& f = mesh_.faces()[faceI];
00217 const labelList& fEdges = mesh_.faceEdges()[faceI];
00218
00219
00220 label nSplitEdges = 0;
00221 label nModPoints = 0;
00222 label nTotalSplits = 0;
00223
00224 forAll(f, fp)
00225 {
00226 if (pointToPos.found(f[fp]))
00227 {
00228 nModPoints++;
00229 nTotalSplits++;
00230 }
00231
00232
00233 label nextV = f.nextLabel(fp);
00234
00235 label edgeI = meshTools::findEdge(edges, fEdges, f[fp], nextV);
00236
00237 Map<labelList>::const_iterator fnd = edgeToAddedPoints.find(edgeI);
00238
00239 if (fnd != edgeToAddedPoints.end())
00240 {
00241 nSplitEdges++;
00242 nTotalSplits += fnd().size();
00243 }
00244 }
00245
00246 if (debug)
00247 {
00248 Pout<< "Face:" << faceI
00249 << " nModPoints:" << nModPoints
00250 << " nSplitEdges:" << nSplitEdges
00251 << " nTotalSplits:" << nTotalSplits << endl;
00252 }
00253
00254 if (nSplitEdges == 0 && nModPoints == 0)
00255 {
00256 FatalErrorIn("boundaryCutter::splitFace") << "Problem : face:" << faceI
00257 << " nSplitEdges:" << nSplitEdges
00258 << " nTotalSplits:" << nTotalSplits
00259 << abort(FatalError);
00260 return false;
00261 }
00262 else if (nSplitEdges + nModPoints == 1)
00263 {
00264
00265
00266 Warning << "Face " << faceI << " has only one edge cut " << endl;
00267 return false;
00268 }
00269 else
00270 {
00271
00272
00273
00274
00275
00276
00277 label patchID, zoneID, zoneFlip;
00278 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00279
00280
00281 face extendedFace(addEdgeCutsToFace(faceI, edgeToAddedPoints));
00282
00283
00284 label startFp = -1;
00285
00286 forAll(extendedFace, fp)
00287 {
00288 if (extendedFace[fp] >= mesh_.nPoints())
00289 {
00290 startFp = fp;
00291 break;
00292 }
00293 }
00294
00295 if (startFp == -1)
00296 {
00297
00298 forAll(extendedFace, fp)
00299 {
00300 if (pointToPos.found(extendedFace[fp]))
00301 {
00302 startFp = fp;
00303 break;
00304 }
00305 }
00306 }
00307
00308 if (startFp == -1)
00309 {
00310 FatalErrorIn("boundaryCutter::splitFace")
00311 << "Problem" << abort(FatalError);
00312 }
00313
00314
00315
00316 bool modifiedFace = false;
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 DynamicList<label> newFace(extendedFace.size());
00341
00342 label fp = startFp;
00343
00344 forAll(extendedFace, i)
00345 {
00346 label pointI = extendedFace[fp];
00347
00348 newFace.append(pointI);
00349
00350 if
00351 (
00352 newFace.size() > 2
00353 && (
00354 pointI >= mesh_.nPoints()
00355 || pointToPos.found(pointI)
00356 )
00357 )
00358 {
00359
00360 face tmpFace;
00361 tmpFace.transfer(newFace);
00362
00363
00364 addFace(faceI, tmpFace, modifiedFace, meshMod);
00365
00366
00367 newFace.append(extendedFace[startFp]);
00368 newFace.append(extendedFace[fp]);
00369 }
00370
00371 fp = (fp+1) % extendedFace.size();
00372 }
00373
00374
00375 if (newFace.size() > 2)
00376 {
00377
00378 face tmpFace;
00379 tmpFace.transfer(newFace);
00380
00381
00382 addFace(faceI, tmpFace, modifiedFace, meshMod);
00383 }
00384
00385
00386 return true;
00387 }
00388 }
00389
00390
00391
00392
00393
00394 Foam::boundaryCutter::boundaryCutter(const polyMesh& mesh)
00395 :
00396 mesh_(mesh),
00397 edgeAddedPoints_(),
00398 faceAddedPoint_()
00399 {}
00400
00401
00402
00403
00404 Foam::boundaryCutter::~boundaryCutter()
00405 {}
00406
00407
00408
00409
00410 void Foam::boundaryCutter::setRefinement
00411 (
00412 const Map<point>& pointToPos,
00413 const Map<List<point> >& edgeToCuts,
00414 const Map<labelPair>& faceToSplit,
00415 const Map<point>& faceToFeaturePoint,
00416 polyTopoChange& meshMod
00417 )
00418 {
00419
00420 edgeAddedPoints_.clear();
00421
00422 faceAddedPoint_.clear();
00423 faceAddedPoint_.resize(faceToFeaturePoint.size());
00424
00425
00426
00427
00428
00429
00430
00431 forAllConstIter(Map<point>, pointToPos, iter)
00432 {
00433 meshMod.setAction
00434 (
00435 polyModifyPoint
00436 (
00437 iter.key(),
00438 iter(),
00439 false,
00440 -1,
00441 true
00442 )
00443 );
00444 }
00445
00446
00447
00448
00449
00450
00451
00452 Map<labelList> edgeToAddedPoints(edgeToCuts.size());
00453
00454 forAllConstIter(Map<List<point> >, edgeToCuts, iter)
00455 {
00456 label edgeI = iter.key();
00457
00458 const edge& e = mesh_.edges()[edgeI];
00459
00460
00461 const List<point>& cuts = iter();
00462
00463 forAll(cuts, cutI)
00464 {
00465
00466 const point& featurePoint = cuts[cutI];
00467
00468 label addedPointI =
00469 meshMod.setAction
00470 (
00471 polyAddPoint
00472 (
00473 featurePoint,
00474 e.start(),
00475 -1,
00476 true
00477 )
00478 );
00479
00480 Map<labelList>::iterator fnd = edgeToAddedPoints.find(edgeI);
00481
00482 if (fnd != edgeToAddedPoints.end())
00483 {
00484 labelList& addedPoints = fnd();
00485
00486 label sz = addedPoints.size();
00487 addedPoints.setSize(sz+1);
00488 addedPoints[sz] = addedPointI;
00489 }
00490 else
00491 {
00492 edgeToAddedPoints.insert(edgeI, labelList(1, addedPointI));
00493 }
00494
00495 if (debug)
00496 {
00497 Pout<< "Added point " << addedPointI << " for edge " << edgeI
00498 << " with cuts:" << edgeToAddedPoints[edgeI] << endl;
00499 }
00500 }
00501 }
00502
00503
00504
00505
00506
00507
00508 forAllConstIter(Map<point>, faceToFeaturePoint, iter)
00509 {
00510 label faceI = iter.key();
00511
00512 const face& f = mesh_.faces()[faceI];
00513
00514 if (faceToSplit.found(faceI))
00515 {
00516 FatalErrorIn("boundaryCutter::setRefinement")
00517 << "Face " << faceI << " vertices " << f
00518 << " is both marked for face-centre decomposition and"
00519 << " diagonal splitting."
00520 << abort(FatalError);
00521 }
00522
00523 if (mesh_.isInternalFace(faceI))
00524 {
00525 FatalErrorIn("boundaryCutter::setRefinement")
00526 << "Face " << faceI << " vertices " << f
00527 << " is not an external face. Cannot split it"
00528 << abort(FatalError);
00529 }
00530
00531 label addedPointI =
00532 meshMod.setAction
00533 (
00534 polyAddPoint
00535 (
00536 iter(),
00537 f[0],
00538 -1,
00539 true
00540 )
00541 );
00542 faceAddedPoint_.insert(faceI, addedPointI);
00543
00544 if (debug)
00545 {
00546 Pout<< "Added point " << addedPointI << " for feature point "
00547 << iter() << " on face " << faceI << " with centre "
00548 << mesh_.faceCentres()[faceI] << endl;
00549 }
00550 }
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 boolList faceUptodate(mesh_.nFaces(), false);
00561
00562
00563
00564 forAllConstIter(Map<label>, faceAddedPoint_, iter)
00565 {
00566 label faceI = iter.key();
00567
00568
00569 face newFace(addEdgeCutsToFace(faceI, edgeToAddedPoints));
00570
00571 label addedPointI = iter();
00572
00573
00574 label patchID, zoneID, zoneFlip;
00575 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00576 label own = mesh_.faceOwner()[faceI];
00577 label masterPoint = mesh_.faces()[faceI][0];
00578
00579
00580
00581 face tri(3);
00582
00583 forAll(newFace, fp)
00584 {
00585 label nextV = newFace.nextLabel(fp);
00586
00587 tri[0] = newFace[fp];
00588 tri[1] = nextV;
00589 tri[2] = addedPointI;
00590
00591 if (fp == 0)
00592 {
00593
00594 meshMod.setAction
00595 (
00596 polyModifyFace
00597 (
00598 tri,
00599 faceI,
00600 own,
00601 -1,
00602 false,
00603 patchID,
00604 false,
00605 zoneID,
00606 zoneFlip
00607 )
00608 );
00609 }
00610 else
00611 {
00612
00613 meshMod.setAction
00614 (
00615 polyAddFace
00616 (
00617 tri,
00618 own,
00619 -1,
00620 masterPoint,
00621 -1,
00622 -1,
00623 false,
00624 patchID,
00625 zoneID,
00626 zoneFlip
00627 )
00628 );
00629 }
00630 }
00631
00632 faceUptodate[faceI] = true;
00633 }
00634
00635
00636
00637 forAllConstIter(Map<labelPair>, faceToSplit, iter)
00638 {
00639 label faceI = iter.key();
00640
00641 const face& f = mesh_.faces()[faceI];
00642
00643 if (faceAddedPoint_.found(faceI))
00644 {
00645 FatalErrorIn("boundaryCutter::setRefinement")
00646 << "Face " << faceI << " vertices " << f
00647 << " is both marked for face-centre decomposition and"
00648 << " diagonal splitting."
00649 << abort(FatalError);
00650 }
00651
00652
00653
00654 face newFace(addEdgeCutsToFace(faceI, edgeToAddedPoints));
00655
00656
00657 label patchID, zoneID, zoneFlip;
00658 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00659 label own = mesh_.faceOwner()[faceI];
00660 label masterPoint = mesh_.faces()[faceI][0];
00661
00662
00663 const labelPair& diag = iter();
00664
00665 label fp0 = findIndex(newFace, f[diag[0]]);
00666 label fp1 = findIndex(newFace, f[diag[1]]);
00667
00668 if (fp0 == -1 || fp1 == -1 || fp0 == fp1)
00669 {
00670 FatalErrorIn("boundaryCutter::setRefinement")
00671 << "Problem : Face " << faceI << " vertices " << f
00672 << " newFace:" << newFace << " diagonal:" << f[diag[0]]
00673 << ' ' << f[diag[1]]
00674 << abort(FatalError);
00675 }
00676
00677
00678
00679
00680 DynamicList<label> newVerts(newFace.size());
00681
00682
00683 label fp = fp0;
00684
00685 do
00686 {
00687 newVerts.append(newFace[fp]);
00688
00689 fp = (fp == newFace.size()-1 ? 0 : fp+1);
00690
00691 } while (fp != fp1);
00692
00693 newVerts.append(newFace[fp1]);
00694
00695
00696
00697 meshMod.setAction
00698 (
00699 polyModifyFace
00700 (
00701 face(newVerts.shrink()),
00702 faceI,
00703 own,
00704 -1,
00705 false,
00706 patchID,
00707 false,
00708 zoneID,
00709 zoneFlip
00710 )
00711 );
00712
00713
00714 newVerts.clear();
00715
00716
00717
00718 do
00719 {
00720 newVerts.append(newFace[fp]);
00721
00722 fp = (fp == newFace.size()-1 ? 0 : fp+1);
00723
00724 } while (fp != fp0);
00725
00726 newVerts.append(newFace[fp0]);
00727
00728
00729 meshMod.setAction
00730 (
00731 polyAddFace
00732 (
00733 face(newVerts.shrink()),
00734 own,
00735 -1,
00736 masterPoint,
00737 -1,
00738 -1,
00739 false,
00740 patchID,
00741 zoneID,
00742 zoneFlip
00743 )
00744 );
00745
00746 faceUptodate[faceI] = true;
00747 }
00748
00749
00750
00751
00752 forAllConstIter(Map<labelList>, edgeToAddedPoints, iter)
00753 {
00754 label edgeI = iter.key();
00755
00756 const labelList& eFaces = mesh_.edgeFaces()[edgeI];
00757
00758 forAll(eFaces, i)
00759 {
00760 label faceI = eFaces[i];
00761
00762 if (!faceUptodate[faceI] && !mesh_.isInternalFace(faceI))
00763 {
00764
00765 if (splitFace(faceI, pointToPos, edgeToAddedPoints, meshMod))
00766 {
00767
00768 faceUptodate[faceI] = true;
00769 }
00770 }
00771 }
00772 }
00773
00774
00775
00776
00777
00778 forAllConstIter(Map<labelList>, edgeToAddedPoints, iter)
00779 {
00780 label edgeI = iter.key();
00781
00782 const labelList& eFaces = mesh_.edgeFaces()[edgeI];
00783
00784 forAll(eFaces, i)
00785 {
00786 label faceI = eFaces[i];
00787
00788 if (!faceUptodate[faceI])
00789 {
00790
00791 face newFace(addEdgeCutsToFace(faceI, edgeToAddedPoints));
00792
00793 label own = mesh_.faceOwner()[faceI];
00794
00795 label nei = -1;
00796
00797 if (mesh_.isInternalFace(faceI))
00798 {
00799 nei = mesh_.faceNeighbour()[faceI];
00800 }
00801
00802 label patchID, zoneID, zoneFlip;
00803 getFaceInfo(faceI, patchID, zoneID, zoneFlip);
00804
00805 meshMod.setAction
00806 (
00807 polyModifyFace
00808 (
00809 newFace,
00810 faceI,
00811 own,
00812 nei,
00813 false,
00814 patchID,
00815 false,
00816 zoneID,
00817 zoneFlip
00818 )
00819 );
00820
00821 faceUptodate[faceI] = true;
00822 }
00823 }
00824 }
00825
00826
00827
00828 edgeAddedPoints_.resize(edgeToCuts.size());
00829
00830 forAllConstIter(Map<labelList>, edgeToAddedPoints, iter)
00831 {
00832 edgeAddedPoints_.insert(mesh_.edges()[iter.key()], iter());
00833 }
00834 }
00835
00836
00837 void Foam::boundaryCutter::updateMesh(const mapPolyMesh& morphMap)
00838 {
00839
00840
00841
00842
00843
00844
00845 {
00846
00847 Map<label> newAddedPoints(faceAddedPoint_.size());
00848
00849 forAllConstIter(Map<label>, faceAddedPoint_, iter)
00850 {
00851 label oldFaceI = iter.key();
00852
00853 label newFaceI = morphMap.reverseFaceMap()[oldFaceI];
00854
00855 label oldPointI = iter();
00856
00857 label newPointI = morphMap.reversePointMap()[oldPointI];
00858
00859 if (newFaceI >= 0 && newPointI >= 0)
00860 {
00861 newAddedPoints.insert(newFaceI, newPointI);
00862 }
00863 }
00864
00865
00866 faceAddedPoint_.transfer(newAddedPoints);
00867 }
00868
00869
00870
00871
00872
00873
00874
00875 {
00876
00877 HashTable<labelList, edge, Hash<edge> >
00878 newEdgeAddedPoints(edgeAddedPoints_.size());
00879
00880 for
00881 (
00882 HashTable<labelList, edge, Hash<edge> >::const_iterator iter =
00883 edgeAddedPoints_.begin();
00884 iter != edgeAddedPoints_.end();
00885 ++iter
00886 )
00887 {
00888 const edge& e = iter.key();
00889
00890 label newStart = morphMap.reversePointMap()[e.start()];
00891
00892 label newEnd = morphMap.reversePointMap()[e.end()];
00893
00894 if (newStart >= 0 && newEnd >= 0)
00895 {
00896 const labelList& addedPoints = iter();
00897
00898 labelList newAddedPoints(addedPoints.size());
00899 label newI = 0;
00900
00901 forAll(addedPoints, i)
00902 {
00903 label newAddedPointI =
00904 morphMap.reversePointMap()[addedPoints[i]];
00905
00906 if (newAddedPointI >= 0)
00907 {
00908 newAddedPoints[newI++] = newAddedPointI;
00909 }
00910 }
00911 if (newI > 0)
00912 {
00913 newAddedPoints.setSize(newI);
00914
00915 edge newE = edge(newStart, newEnd);
00916
00917 newEdgeAddedPoints.insert(newE, newAddedPoints);
00918 }
00919 }
00920 }
00921
00922
00923 edgeAddedPoints_.transfer(newEdgeAddedPoints);
00924 }
00925 }
00926
00927
00928