OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

cellCuts.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "cellCuts.H"
00028 #include "polyMesh.H"
00029 #include "Time.H"
00030 #include "ListOps.H"
00031 #include "cellLooper.H"
00032 #include "refineCell.H"
00033 #include "meshTools.H"
00034 #include "geomCellLooper.H"
00035 #include "OFstream.H"
00036 #include "plane.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 defineTypeNameAndDebug(Foam::cellCuts, 0);
00041 
00042 
00043 // * * * * * * * * * * * * * Private Static Functions  * * * * * * * * * * * //
00044 
00045 // Find val in first nElems elements of list.
00046 Foam::label Foam::cellCuts::findPartIndex
00047 (
00048     const labelList& elems,
00049     const label nElems,
00050     const label val
00051 )
00052 {
00053     for (label i = 0; i < nElems; i++)
00054     {
00055         if (elems[i] == val)
00056         {
00057             return i;
00058         }
00059     }
00060     return -1;
00061 }
00062 
00063 
00064 Foam::boolList Foam::cellCuts::expand
00065 (
00066     const label size,
00067     const labelList& labels
00068 )
00069 {
00070     boolList result(size, false);
00071 
00072     forAll(labels, labelI)
00073     {
00074         result[labels[labelI]] = true;
00075     }
00076     return result;
00077 }
00078 
00079 
00080 Foam::scalarField Foam::cellCuts::expand
00081 (
00082     const label size,
00083     const labelList& labels,
00084     const scalarField& weights
00085 )
00086 {
00087     scalarField result(size, -GREAT);
00088 
00089     forAll(labels, labelI)
00090     {
00091         result[labels[labelI]] = weights[labelI];
00092     }
00093     return result;
00094 }
00095 
00096 
00097 // Find first point in lst not in map.
00098 Foam::label Foam::cellCuts::firstUnique
00099 (
00100     const labelList& lst,
00101     const Map<label>& map
00102 )
00103 {
00104     forAll(lst, i)
00105     {
00106         if (!map.found(lst[i]))
00107         {
00108             return i;
00109         }
00110     }
00111     return -1;
00112 }
00113 
00114 
00115 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00116 
00117 // Write cell and raw cuts on any of the elements
00118 void Foam::cellCuts::writeUncutOBJ
00119 (
00120     const fileName& dir,
00121     const label cellI
00122 ) const
00123 {
00124     //- Cell edges
00125     OFstream cutsStream(dir / "cell_" + name(cellI) + ".obj");
00126 
00127     Pout<< "Writing cell for time " <<  mesh().time().timeName()
00128         << " to " << cutsStream.name() << nl;
00129 
00130     meshTools::writeOBJ
00131     (
00132         cutsStream,
00133         mesh().cells(),
00134         mesh().faces(),
00135         mesh().points(),
00136         labelList(1, cellI)
00137     );
00138 
00139     //- Loop cutting cell in two
00140     OFstream cutStream(dir / "cellCuts_" + name(cellI) + ".obj");
00141 
00142     Pout<< "Writing raw cuts on cell for time " <<  mesh().time().timeName()
00143         << " to " << cutStream.name() << nl;
00144 
00145     const labelList& cPoints = mesh().cellPoints()[cellI];
00146 
00147     forAll(cPoints, i)
00148     {
00149         label pointI = cPoints[i];
00150         if (pointIsCut_[pointI])
00151         {
00152             meshTools::writeOBJ(cutStream, mesh().points()[pointI]);
00153         }
00154     }
00155 
00156     const pointField& pts = mesh().points();
00157 
00158     const labelList& cEdges = mesh().cellEdges()[cellI];
00159 
00160     forAll(cEdges, i)
00161     {
00162         label edgeI = cEdges[i];
00163 
00164         if (edgeIsCut_[edgeI])
00165         {
00166             const edge& e = mesh().edges()[edgeI];
00167 
00168             const scalar w = edgeWeight_[edgeI];
00169 
00170             meshTools::writeOBJ(cutStream, w*pts[e[1]] + (1-w)*pts[e[0]]);
00171         }
00172     }
00173 }
00174 
00175 
00176 void Foam::cellCuts::writeOBJ
00177 (
00178     const fileName& dir,
00179     const label cellI,
00180     const pointField& loopPoints,
00181     const labelList& anchors
00182 ) const
00183 {
00184     //- Cell edges
00185     OFstream cutsStream(dir / "cell_" + name(cellI) + ".obj");
00186 
00187     Pout<< "Writing cell for time " <<  mesh().time().timeName()
00188         << " to " << cutsStream.name() << nl;
00189 
00190     meshTools::writeOBJ
00191     (
00192         cutsStream,
00193         mesh().cells(),
00194         mesh().faces(),
00195         mesh().points(),
00196         labelList(1, cellI)
00197     );
00198 
00199 
00200     //- Loop cutting cell in two
00201     OFstream loopStream(dir / "cellLoop_" + name(cellI) + ".obj");
00202 
00203     Pout<< "Writing loop for time " <<  mesh().time().timeName()
00204         << " to " << loopStream.name() << nl;
00205 
00206     label vertI = 0;
00207 
00208     writeOBJ(loopStream, loopPoints, vertI);
00209 
00210 
00211     //- Anchors for cell
00212     OFstream anchorStream(dir / "anchors_" + name(cellI) + ".obj");
00213 
00214     Pout<< "Writing anchors for time " <<  mesh().time().timeName()
00215         << " to " << anchorStream.name() << endl;
00216 
00217     forAll(anchors, i)
00218     {
00219         meshTools::writeOBJ(anchorStream, mesh().points()[anchors[i]]);
00220     }
00221 }
00222 
00223 
00224 // Find face on cell using the two edges.
00225 Foam::label Foam::cellCuts::edgeEdgeToFace
00226 (
00227     const label cellI,
00228     const label edgeA,
00229     const label edgeB
00230 ) const
00231 {
00232     const labelList& cFaces = mesh().cells()[cellI];
00233 
00234     forAll(cFaces, cFaceI)
00235     {
00236         label faceI = cFaces[cFaceI];
00237 
00238         const labelList& fEdges = mesh().faceEdges()[faceI];
00239 
00240         if
00241         (
00242             findIndex(fEdges, edgeA) != -1
00243          && findIndex(fEdges, edgeB) != -1
00244         )
00245         {
00246            return faceI;
00247         }
00248     }
00249 
00250     // Coming here means the loop is illegal since the two edges
00251     // are not shared by a face. We just mark loop as invalid and continue.
00252 
00253     WarningIn
00254     (
00255         "Foam::cellCuts::edgeEdgeToFace"
00256         "(const label cellI, const label edgeA,"
00257         "const label edgeB) const"
00258     )   << "cellCuts : Cannot find face on cell "
00259         << cellI << " that has both edges " << edgeA << ' ' << edgeB << endl
00260         << "faces : " << cFaces << endl
00261         << "edgeA : " << mesh().edges()[edgeA] << endl
00262         << "edgeB : " << mesh().edges()[edgeB] << endl
00263         << "Marking the loop across this cell as invalid" << endl;
00264 
00265     return -1;
00266 }
00267 
00268 
00269 // Find face on cell using an edge and a vertex.
00270 Foam::label Foam::cellCuts::edgeVertexToFace
00271 (
00272     const label cellI,
00273     const label edgeI,
00274     const label vertI
00275 ) const
00276 {
00277     const labelList& cFaces = mesh().cells()[cellI];
00278 
00279     forAll(cFaces, cFaceI)
00280     {
00281         label faceI = cFaces[cFaceI];
00282 
00283         const face& f = mesh().faces()[faceI];
00284 
00285         const labelList& fEdges = mesh().faceEdges()[faceI];
00286 
00287         if
00288         (
00289             findIndex(fEdges, edgeI) != -1
00290          && findIndex(f, vertI) != -1
00291         )
00292         {
00293            return faceI;
00294         }
00295     }
00296 
00297     WarningIn
00298     (
00299         "Foam::cellCuts::edgeVertexToFace"
00300         "(const label cellI, const label edgeI, "
00301         "const label vertI) const"
00302     )   << "cellCuts : Cannot find face on cell "
00303         << cellI << " that has both edge " << edgeI << " and vertex "
00304         << vertI << endl
00305         << "faces : " << cFaces << endl
00306         << "edge : " << mesh().edges()[edgeI] << endl
00307         << "Marking the loop across this cell as invalid" << endl;
00308 
00309     return -1;
00310 }
00311 
00312 
00313 // Find face using two vertices (guaranteed not to be along edge)
00314 Foam::label Foam::cellCuts::vertexVertexToFace
00315 (
00316     const label cellI,
00317     const label vertA,
00318     const label vertB
00319 ) const
00320 {
00321     const labelList& cFaces = mesh().cells()[cellI];
00322 
00323     forAll(cFaces, cFaceI)
00324     {
00325         label faceI = cFaces[cFaceI];
00326 
00327         const face& f = mesh().faces()[faceI];
00328 
00329         if
00330         (
00331             findIndex(f, vertA) != -1
00332          && findIndex(f, vertB) != -1
00333         )
00334         {
00335            return faceI;
00336         }
00337     }
00338 
00339     WarningIn("Foam::cellCuts::vertexVertexToFace")
00340         << "cellCuts : Cannot find face on cell "
00341         << cellI << " that has vertex " << vertA << " and vertex "
00342         << vertB << endl
00343         << "faces : " << cFaces << endl
00344         << "Marking the loop across this cell as invalid" << endl;
00345 
00346     return -1;
00347 }
00348 
00349 
00350 void Foam::cellCuts::calcFaceCuts() const
00351 {
00352     if (faceCutsPtr_)
00353     {
00354         FatalErrorIn("cellCuts::calcFaceCuts()")
00355             << "faceCuts already calculated" << abort(FatalError);
00356     }
00357 
00358     const faceList& faces = mesh().faces();
00359 
00360 
00361     faceCutsPtr_ = new labelListList(mesh().nFaces());
00362     labelListList& faceCuts = *faceCutsPtr_;
00363 
00364     for (label faceI = 0; faceI < mesh().nFaces(); faceI++)
00365     {
00366         const face& f = faces[faceI];
00367 
00368         // Big enough storage (possibly all points and all edges cut). Shrink
00369         // later on.
00370         labelList& cuts = faceCuts[faceI];
00371 
00372         cuts.setSize(2*f.size());
00373 
00374         label cutI = 0;
00375 
00376         // Do point-edge-point walk over face and collect all cuts.
00377         // Problem is that we want to start from one of the endpoints of a
00378         // string of connected cuts; we don't want to start somewhere in the
00379         // middle.
00380 
00381         // Pass1: find first point cut not preceeded by a cut.
00382         label startFp = -1;
00383 
00384         forAll(f, fp)
00385         {
00386             label v0 = f[fp];
00387 
00388             if (pointIsCut_[v0])
00389             {
00390                 label vMin1 = f[f.rcIndex(fp)];
00391 
00392                 if
00393                 (
00394                     !pointIsCut_[vMin1]
00395                  && !edgeIsCut_[findEdge(faceI, v0, vMin1)]
00396                 )
00397                 {
00398                     cuts[cutI++] = vertToEVert(v0);
00399                     startFp = f.fcIndex(fp);
00400                     break;
00401                 }
00402             }
00403         }
00404 
00405         // Pass2: first edge cut not preceeded by point cut
00406         if (startFp == -1)
00407         {
00408             forAll(f, fp)
00409             {
00410                 label fp1 = f.fcIndex(fp);
00411 
00412                 label v0 = f[fp];
00413                 label v1 = f[fp1];
00414 
00415                 label edgeI = findEdge(faceI, v0, v1);
00416 
00417                 if (edgeIsCut_[edgeI] && !pointIsCut_[v0])
00418                 {
00419                     cuts[cutI++] = edgeToEVert(edgeI);
00420                     startFp = fp1;
00421                     break;
00422                 }
00423             }
00424         }
00425 
00426         // Pass3: nothing found so far. Either face is not cut at all or all
00427         // vertices are cut. Start from 0.
00428         if (startFp == -1)
00429         {
00430             startFp = 0;
00431         }
00432 
00433         // Store all cuts starting from startFp;
00434         label fp = startFp;
00435 
00436         bool allVerticesCut = true;
00437 
00438         forAll(f, i)
00439         {
00440             label fp1 = f.fcIndex(fp);
00441 
00442             // Get the three items: current vertex, next vertex and edge
00443             // inbetween
00444             label v0 = f[fp];
00445             label v1 = f[fp1];
00446             label edgeI = findEdge(faceI, v0, v1);
00447 
00448             if (pointIsCut_[v0])
00449             {
00450                 cuts[cutI++] = vertToEVert(v0);
00451             }
00452             else
00453             {
00454                 // For check if all vertices have been cut (= illegal)
00455                 allVerticesCut = false;
00456             }
00457 
00458             if (edgeIsCut_[edgeI])
00459             {
00460                 cuts[cutI++] = edgeToEVert(edgeI);
00461             }
00462 
00463             fp = fp1;
00464         }
00465 
00466 
00467         if (allVerticesCut)
00468         {
00469             WarningIn("Foam::cellCuts::calcFaceCuts() const")
00470                 << "Face " << faceI << " vertices " << f
00471                 << " has all its vertices cut. Not cutting face." << endl;
00472 
00473             cutI = 0;
00474         }
00475 
00476         // Remove duplicate starting point
00477         if (cutI > 1 && cuts[cutI-1] == cuts[0])
00478         {
00479             cutI--;
00480         }
00481         cuts.setSize(cutI);
00482     }
00483 }
00484 
00485 
00486 // Find edge on face using two vertices
00487 Foam::label Foam::cellCuts::findEdge
00488 (
00489     const label faceI,
00490     const label v0,
00491     const label v1
00492 ) const
00493 {
00494     const edgeList& edges = mesh().edges();
00495 
00496     const labelList& fEdges = mesh().faceEdges()[faceI];
00497 
00498     forAll(fEdges, i)
00499     {
00500         const edge& e = edges[fEdges[i]];
00501 
00502         if
00503         (
00504             (e[0] == v0 && e[1] == v1)
00505          || (e[0] == v1 && e[1] == v0)
00506         )
00507         {
00508             return fEdges[i];
00509         }
00510     }
00511 
00512     return -1;
00513 }
00514 
00515 
00516 // Check if there is a face on the cell on which all cuts are.
00517 Foam::label Foam::cellCuts::loopFace
00518 (
00519     const label cellI,
00520     const labelList& loop
00521 ) const
00522 {
00523     const cell& cFaces = mesh().cells()[cellI];
00524 
00525     forAll(cFaces, cFaceI)
00526     {
00527         label faceI = cFaces[cFaceI];
00528 
00529         const labelList& fEdges = mesh().faceEdges()[faceI];
00530         const face& f = mesh().faces()[faceI];
00531 
00532         bool allOnFace = true;
00533 
00534         forAll(loop, i)
00535         {
00536             label cut = loop[i];
00537 
00538             if (isEdge(cut))
00539             {
00540                 if (findIndex(fEdges, getEdge(cut)) == -1)
00541                 {
00542                     // Edge not on face. Skip face.
00543                     allOnFace = false;
00544                     break;
00545                 }
00546             }
00547             else
00548             {
00549                 if (findIndex(f, getVertex(cut)) == -1)
00550                 {
00551                     // Vertex not on face. Skip face.
00552                     allOnFace = false;
00553                     break;
00554                 }
00555             }
00556         }
00557 
00558         if (allOnFace)
00559         {
00560             // Found face where all elements of loop are on the face.
00561             return faceI;
00562         }
00563     }
00564     return -1;
00565 }
00566 
00567 
00568 // From point go into connected face
00569 bool Foam::cellCuts::walkPoint
00570 (
00571     const label cellI,
00572     const label startCut,
00573 
00574     const label exclude0,
00575     const label exclude1,
00576 
00577     const label otherCut,
00578 
00579     label& nVisited,
00580     labelList& visited
00581 ) const
00582 {
00583     label vertI = getVertex(otherCut);
00584 
00585     const labelList& pFaces = mesh().pointFaces()[vertI];
00586 
00587     forAll(pFaces, pFaceI)
00588     {
00589         label otherFaceI = pFaces[pFaceI];
00590 
00591         if
00592         (
00593             otherFaceI != exclude0
00594          && otherFaceI != exclude1
00595          && meshTools::faceOnCell(mesh(), cellI, otherFaceI)
00596         )
00597         {
00598             label oldNVisited = nVisited;
00599 
00600             bool foundLoop =
00601                 walkCell
00602                 (
00603                     cellI,
00604                     startCut,
00605                     otherFaceI,
00606                     otherCut,
00607                     nVisited,
00608                     visited
00609                 );
00610 
00611             if (foundLoop)
00612             {
00613                 return true;
00614             }
00615 
00616             // No success. Restore state and continue
00617             nVisited = oldNVisited;
00618         }
00619     }
00620     return false;
00621 }
00622 
00623 
00624 // Cross cut (which is edge on faceI) onto next face
00625 bool Foam::cellCuts::crossEdge
00626 (
00627     const label cellI,
00628     const label startCut,
00629     const label faceI,
00630     const label otherCut,
00631 
00632     label& nVisited,
00633     labelList& visited
00634 ) const
00635 {
00636     // Cross edge to other face
00637     label edgeI = getEdge(otherCut);
00638 
00639     label otherFaceI = meshTools::otherFace(mesh(), cellI, faceI, edgeI);
00640 
00641     // Store old state
00642     label oldNVisited = nVisited;
00643 
00644     // Recurse to otherCut
00645     bool foundLoop =
00646         walkCell
00647         (
00648             cellI,
00649             startCut,
00650             otherFaceI,
00651             otherCut,
00652             nVisited,
00653             visited
00654         );
00655 
00656     if (foundLoop)
00657     {
00658         return true;
00659     }
00660     else
00661     {
00662         // No success. Restore state (i.e. backtrack)
00663         nVisited = oldNVisited;
00664 
00665         return false;
00666     }
00667 }
00668 
00669 
00670 bool Foam::cellCuts::addCut
00671 (
00672     const label cellI,
00673     const label cut,
00674     label& nVisited,
00675     labelList& visited
00676 ) const
00677 {
00678     // Check for duplicate cuts.
00679     if (findPartIndex(visited, nVisited, cut) != -1)
00680     {
00681         // Truncate (copy of) visited for ease of printing.
00682         labelList truncVisited(visited);
00683         truncVisited.setSize(nVisited);
00684 
00685         Pout<< "For cell " << cellI << " : trying to add duplicate cut " << cut;
00686         labelList cuts(1, cut);
00687         writeCuts(Pout, cuts, loopWeights(cuts));
00688 
00689         Pout<< " to path:";
00690         writeCuts(Pout, truncVisited, loopWeights(truncVisited));
00691         Pout<< endl;
00692 
00693         return false;
00694     }
00695     else
00696     {
00697         visited[nVisited++] = cut;
00698 
00699         return true;
00700     }
00701 }
00702 
00703 
00704 // Walk across faceI, storing cuts as you go. Returns last two cuts visisted.
00705 // Returns true if valid walk.
00706 bool Foam::cellCuts::walkFace
00707 (
00708     const label cellI,
00709     const label startCut,
00710     const label faceI,
00711     const label cut,
00712 
00713     label& lastCut,
00714     label& beforeLastCut,
00715     label& nVisited,
00716     labelList& visited
00717 ) const
00718 {
00719     const labelList& fCuts = faceCuts()[faceI];
00720 
00721     if (fCuts.size() < 2)
00722     {
00723         return false;
00724     }
00725 
00726     // Easy case : two cuts.
00727     if (fCuts.size() == 2)
00728     {
00729         if (fCuts[0] == cut)
00730         {
00731             if (!addCut(cellI, cut, nVisited, visited))
00732             {
00733                 return false;
00734             }
00735 
00736             beforeLastCut = cut;
00737             lastCut = fCuts[1];
00738 
00739             return true;
00740         }
00741         else
00742         {
00743             if (!addCut(cellI, cut, nVisited, visited))
00744             {
00745                 return false;
00746             }
00747 
00748             beforeLastCut = cut;
00749             lastCut = fCuts[0];
00750 
00751             return true;
00752         }
00753     }
00754 
00755     // Harder case: more than 2 cuts on face.
00756     // Should be start or end of string of cuts. Store all but last cut.
00757     if (fCuts[0] == cut)
00758     {
00759         // Walk forward
00760         for (label i = 0; i < fCuts.size()-1; i++)
00761         {
00762             if (!addCut(cellI, fCuts[i], nVisited, visited))
00763             {
00764                 return false;
00765             }
00766         }
00767         beforeLastCut = fCuts[fCuts.size()-2];
00768         lastCut = fCuts[fCuts.size()-1];
00769     }
00770     else if (fCuts[fCuts.size()-1] == cut)
00771     {
00772         for (label i = fCuts.size()-1; i >= 1; --i)
00773         {
00774             if (!addCut(cellI, fCuts[i], nVisited, visited))
00775             {
00776                 return false;
00777             }
00778         }
00779         beforeLastCut = fCuts[1];
00780         lastCut = fCuts[0];
00781     }
00782     else
00783     {
00784         WarningIn("Foam::cellCuts::walkFace")
00785             << "In middle of cut. cell:" << cellI << " face:" << faceI
00786             << " cuts:" << fCuts << " current cut:" << cut << endl;
00787 
00788         return false;
00789     }
00790 
00791     return true;
00792 }
00793 
00794 
00795 
00796 // Walk across cuts (cut edges or cut vertices) of cell. Stops when hit cut
00797 // already visited. Returns true when loop of 3 or more vertices found.
00798 bool Foam::cellCuts::walkCell
00799 (
00800     const label cellI,
00801     const label startCut,
00802     const label faceI,
00803     const label cut,
00804 
00805     label& nVisited,
00806     labelList& visited
00807 ) const
00808 {
00809     // Walk across face, storing cuts. Return last two cuts visited.
00810     label lastCut = -1;
00811     label beforeLastCut = -1;
00812 
00813 
00814     if (debug & 2)
00815     {
00816         Pout<< "For cell:" << cellI << " walked across face " << faceI
00817             << " from cut ";
00818         labelList cuts(1, cut);
00819         writeCuts(Pout, cuts, loopWeights(cuts));
00820         Pout<< endl;
00821     }
00822 
00823     bool validWalk = walkFace
00824     (
00825         cellI,
00826         startCut,
00827         faceI,
00828         cut,
00829 
00830         lastCut,
00831         beforeLastCut,
00832         nVisited,
00833         visited
00834     );
00835 
00836     if (!validWalk)
00837     {
00838         return false;
00839     }
00840 
00841     if (debug & 2)
00842     {
00843         Pout<< "    to last cut ";
00844         labelList cuts(1, lastCut);
00845         writeCuts(Pout, cuts, loopWeights(cuts));
00846         Pout<< endl;
00847     }
00848 
00849     // Check if starting point reached.
00850     if (lastCut == startCut)
00851     {
00852         if (nVisited >= 3)
00853         {
00854             if (debug & 2)
00855             {
00856                 // Truncate (copy of) visited for ease of printing.
00857                 labelList truncVisited(visited);
00858                 truncVisited.setSize(nVisited);
00859 
00860                 Pout<< "For cell " << cellI << " : found closed path:";
00861                 writeCuts(Pout, truncVisited, loopWeights(truncVisited));
00862                 Pout<< " closed by " << lastCut << endl;
00863             }
00864 
00865             return true;
00866         }
00867         else
00868         {
00869             // Loop but too short. Probably folded back on itself.
00870             return false;
00871         }
00872     }
00873 
00874 
00875     // Check last cut and one before that to determine type.
00876 
00877     // From beforeLastCut to lastCut:
00878     //  - from edge to edge
00879     //      (- always not along existing edge)
00880     //  - from edge to vertex
00881     //      - not along existing edge
00882     //      - along existing edge: not allowed?
00883     //  - from vertex to edge
00884     //      - not along existing edge
00885     //      - along existing edge. Not allowed. See above.
00886     //  - from vertex to vertex
00887     //      - not along existing edge
00888     //      - along existing edge
00889 
00890     if (isEdge(beforeLastCut))
00891     {
00892         if (isEdge(lastCut))
00893         {
00894             // beforeLastCut=edge, lastCut=edge.
00895 
00896             // Cross lastCut (=edge) into face not faceI
00897             return crossEdge
00898             (
00899                 cellI,
00900                 startCut,
00901                 faceI,
00902                 lastCut,
00903                 nVisited,
00904                 visited
00905             );
00906         }
00907         else
00908         {
00909             // beforeLastCut=edge, lastCut=vertex.
00910 
00911             // Go from lastCut to all connected faces (except faceI)
00912             return walkPoint
00913             (
00914                 cellI,
00915                 startCut,
00916                 faceI,          // exclude0: don't cross back on current face
00917                 -1,             // exclude1
00918                 lastCut,
00919                 nVisited,
00920                 visited
00921             );
00922         }
00923     }
00924     else
00925     {
00926         if (isEdge(lastCut))
00927         {
00928             // beforeLastCut=vertex, lastCut=edge.
00929             return crossEdge
00930             (
00931                 cellI,
00932                 startCut,
00933                 faceI,
00934                 lastCut,
00935                 nVisited,
00936                 visited
00937             );
00938         }
00939         else
00940         {
00941             // beforeLastCut=vertex, lastCut=vertex. Check if along existing
00942             // edge.
00943             label edgeI =
00944                 findEdge
00945                 (
00946                     faceI,
00947                     getVertex(beforeLastCut),
00948                     getVertex(lastCut)
00949                 );
00950 
00951             if (edgeI != -1)
00952             {
00953                 // Cut along existing edge. So is in fact on two faces.
00954                 // Get faces on both sides of the edge to make
00955                 // sure we dont fold back on to those.
00956 
00957                 label f0, f1;
00958                 meshTools::getEdgeFaces(mesh(), cellI, edgeI, f0, f1);
00959 
00960                 return walkPoint
00961                 (
00962                     cellI,
00963                     startCut,
00964                     f0,
00965                     f1,
00966                     lastCut,
00967                     nVisited,
00968                     visited
00969                 );
00970 
00971             }
00972             else
00973             {
00974                 // Cross cut across face.
00975                 return walkPoint
00976                 (
00977                     cellI,
00978                     startCut,
00979                     faceI,      // face to exclude
00980                     -1,         // face to exclude
00981                     lastCut,
00982                     nVisited,
00983                     visited
00984                 );
00985             }
00986         }
00987     }
00988 }
00989 
00990 
00991 // Determine for every cut cell the loop (= face) it is cut by. Done by starting
00992 // from a cut edge or cut vertex and walking across faces, from cut to cut,
00993 // until starting cut hit.
00994 // If multiple loops are possible across a cell circumference takes the first
00995 // one found.
00996 void Foam::cellCuts::calcCellLoops(const labelList& cutCells)
00997 {
00998     // Calculate cuts per face.
00999     const labelListList& allFaceCuts = faceCuts();
01000 
01001     // Per cell the number of faces with valid cuts. Is used as quick
01002     // rejection to see if cell can be cut.
01003     labelList nCutFaces(mesh().nCells(), 0);
01004 
01005     forAll(allFaceCuts, faceI)
01006     {
01007         const labelList& fCuts = allFaceCuts[faceI];
01008 
01009         if (fCuts.size() == mesh().faces()[faceI].size())
01010         {
01011             // Too many cuts on face. WalkCell would get very upset so disable.
01012             nCutFaces[mesh().faceOwner()[faceI]] = labelMin;
01013 
01014             if (mesh().isInternalFace(faceI))
01015             {
01016                 nCutFaces[mesh().faceNeighbour()[faceI]] = labelMin;
01017             }
01018         }
01019         else if (fCuts.size() >= 2)
01020         {
01021             // Could be valid cut. Update count for owner and neighbour.
01022             nCutFaces[mesh().faceOwner()[faceI]]++;
01023 
01024             if (mesh().isInternalFace(faceI))
01025             {
01026                 nCutFaces[mesh().faceNeighbour()[faceI]]++;
01027             }
01028         }
01029     }
01030 
01031 
01032     // Stack of visited cuts (nVisited used as stack pointer)
01033     // Size big enough.
01034     labelList visited(mesh().nPoints());
01035 
01036     forAll(cutCells, i)
01037     {
01038         label cellI = cutCells[i];
01039 
01040         bool validLoop = false;
01041 
01042         // Quick rejection: has enough faces that are cut?
01043         if (nCutFaces[cellI] >= 3)
01044         {
01045             const labelList& cFaces = mesh().cells()[cellI];
01046 
01047             if (debug & 2)
01048             {
01049                 Pout<< "cell:" << cellI << " cut faces:" << endl;
01050                 forAll(cFaces, i)
01051                 {
01052                     label faceI = cFaces[i];
01053                     const labelList& fCuts = allFaceCuts[faceI];
01054 
01055                     Pout<< "    face:" << faceI << " cuts:";
01056                     writeCuts(Pout, fCuts, loopWeights(fCuts));
01057                     Pout<< endl;
01058                 }
01059             }
01060 
01061             label nVisited = 0;
01062 
01063             // Determine the first cut face to start walking from.
01064             forAll(cFaces, cFaceI)
01065             {
01066                 label faceI = cFaces[cFaceI];
01067 
01068                 const labelList& fCuts = allFaceCuts[faceI];
01069 
01070                 // Take first or last cut of multiple on face.
01071                 // Note that in calcFaceCuts
01072                 // we have already made sure this is the start or end of a
01073                 // string of cuts.
01074                 if (fCuts.size() >= 2)
01075                 {
01076                     // Try walking from start of fCuts.
01077                     nVisited = 0;
01078 
01079                     if (debug & 2)
01080                     {
01081                         Pout<< "cell:" << cellI
01082                             << " start walk at face:" << faceI
01083                             << " cut:";
01084                         labelList cuts(1, fCuts[0]);
01085                         writeCuts(Pout, cuts, loopWeights(cuts));
01086                         Pout<< endl;
01087                     }
01088 
01089                     validLoop =
01090                         walkCell
01091                         (
01092                             cellI,
01093                             fCuts[0],
01094                             faceI,
01095                             fCuts[0],
01096 
01097                             nVisited,
01098                             visited
01099                         );
01100 
01101                     if (validLoop)
01102                     {
01103                         break;
01104                     }
01105 
01106                     // No need to try and walk from end of cuts since
01107                     // all paths already tried by walkCell.
01108                 }
01109             }
01110 
01111             if (validLoop)
01112             {
01113                 // Copy nVisited elements out of visited (since visited is
01114                 // never truncated for efficiency reasons)
01115 
01116                 labelList& loop = cellLoops_[cellI];
01117 
01118                 loop.setSize(nVisited);
01119 
01120                 for (label i = 0; i < nVisited; i++)
01121                 {
01122                     loop[i] = visited[i];
01123                 }
01124             }
01125             else
01126             {
01127                 // Invalid loop. Leave cellLoops_[cellI] zero size which
01128                 // flags this.
01129                 Pout<< "calcCellLoops(const labelList&) : did not find valid"
01130                     << " loop for cell " << cellI << endl;
01131                 // Dump cell and cuts on cell.
01132                 writeUncutOBJ(".", cellI);
01133 
01134                 cellLoops_[cellI].setSize(0);
01135             }
01136         }
01137         else
01138         {
01139             //Pout<< "calcCellLoops(const labelList&) : did not find valid"
01140             //    << " loop for cell " << cellI << " since not enough cut faces"
01141             //    << endl;
01142             cellLoops_[cellI].setSize(0);
01143         }
01144     }
01145 }
01146 
01147 
01148 // Walk unset edges of single cell from starting point and marks visited
01149 // edges and vertices with status.
01150 void Foam::cellCuts::walkEdges
01151 (
01152     const label cellI,
01153     const label pointI,
01154     const label status,
01155 
01156     Map<label>& edgeStatus,
01157     Map<label>& pointStatus
01158 ) const
01159 {
01160     if (pointStatus.insert(pointI, status))
01161     {
01162         // First visit to pointI
01163 
01164         const labelList& pEdges = mesh().pointEdges()[pointI];
01165 
01166         forAll(pEdges, pEdgeI)
01167         {
01168             label edgeI = pEdges[pEdgeI];
01169 
01170             if
01171             (
01172                 meshTools::edgeOnCell(mesh(), cellI, edgeI)
01173              && edgeStatus.insert(edgeI, status)
01174             )
01175             {
01176                 // First visit to edgeI so recurse.
01177 
01178                 label v2 = mesh().edges()[edgeI].otherVertex(pointI);
01179 
01180                 walkEdges(cellI, v2, status, edgeStatus, pointStatus);
01181             }
01182         }
01183     }
01184 }
01185 
01186 
01187 // Invert anchor point selection.
01188 Foam::labelList Foam::cellCuts::nonAnchorPoints
01189 (
01190     const labelList& cellPoints,
01191     const labelList& anchorPoints,
01192     const labelList& loop
01193 ) const
01194 {
01195     labelList newElems(cellPoints.size());
01196     label newElemI = 0;
01197 
01198     forAll(cellPoints, i)
01199     {
01200         label pointI = cellPoints[i];
01201 
01202         if
01203         (
01204             findIndex(anchorPoints, pointI) == -1
01205          && findIndex(loop, vertToEVert(pointI)) == -1
01206         )
01207         {
01208             newElems[newElemI++] = pointI;
01209         }
01210     }
01211 
01212     newElems.setSize(newElemI);
01213 
01214     return newElems;
01215 }
01216 
01217 
01218 //- Check anchor points on 'outside' of loop
01219 bool Foam::cellCuts::loopAnchorConsistent
01220 (
01221     const label cellI,
01222     const pointField& loopPts,
01223     const labelList& anchorPoints
01224 ) const
01225 {
01226     // Create identity face for ease of calculation of normal etc.
01227     face f(identity(loopPts.size()));
01228 
01229     vector normal = f.normal(loopPts);
01230     point ctr = f.centre(loopPts);
01231 
01232 
01233     // Get average position of anchor points.
01234     vector avg(vector::zero);
01235 
01236     forAll(anchorPoints, ptI)
01237     {
01238         avg += mesh().points()[anchorPoints[ptI]];
01239     }
01240     avg /= anchorPoints.size();
01241 
01242 
01243     if (((avg - ctr) & normal) > 0)
01244     {
01245         return true;
01246     }
01247     else
01248     {
01249         return false;
01250     }
01251 }
01252 
01253 
01254 // Determines set of anchor points given a loop. The loop should split the
01255 // cell into (one or) two sets of vertices. The set of vertices that is
01256 // on the 'normal' side of the loop is the anchor set.
01257 // Returns true if valid set, false otherwise.
01258 bool Foam::cellCuts::calcAnchors
01259 (
01260     const label cellI,
01261     const labelList& loop,
01262     const pointField& loopPts,
01263 
01264     labelList& anchorPoints
01265 ) const
01266 {
01267     const edgeList& edges = mesh().edges();
01268 
01269     const labelList& cPoints = mesh().cellPoints()[cellI];
01270     const labelList& cEdges = mesh().cellEdges()[cellI];
01271     const cell& cFaces = mesh().cells()[cellI];
01272 
01273     // Points on loop
01274 
01275     // Status of point:
01276     // 0 - on loop
01277     // 1 - point set 1
01278     // 2 - point set 2
01279     Map<label> pointStatus(2*cPoints.size());
01280     Map<label> edgeStatus(2*cEdges.size());
01281 
01282     // Mark loop vertices
01283     forAll(loop, i)
01284     {
01285         label cut = loop[i];
01286 
01287         if (isEdge(cut))
01288         {
01289             edgeStatus.insert(getEdge(cut), 0);
01290         }
01291         else
01292         {
01293             pointStatus.insert(getVertex(cut), 0);
01294         }
01295     }
01296     // Since edges between two cut vertices have not been marked, mark them
01297     // explicitly
01298     forAll(cEdges, i)
01299     {
01300         label edgeI = cEdges[i];
01301         const edge& e = edges[edgeI];
01302 
01303         if (pointStatus.found(e[0]) && pointStatus.found(e[1]))
01304         {
01305             edgeStatus.insert(edgeI, 0);
01306         }
01307     }
01308 
01309 
01310     // Find uncut starting vertex
01311     label uncutIndex = firstUnique(cPoints, pointStatus);
01312 
01313     if (uncutIndex == -1)
01314     {
01315         WarningIn("Foam::cellCuts::calcAnchors")
01316             << "Invalid loop " << loop << " for cell " << cellI << endl
01317             << "Can not find point on cell which is not cut by loop."
01318             << endl;
01319 
01320         writeOBJ(".", cellI, loopPts, labelList(0));
01321 
01322         return false;
01323     }
01324 
01325     // Walk unset vertices and edges and mark with 1 in pointStatus, edgeStatus
01326     walkEdges(cellI, cPoints[uncutIndex], 1, edgeStatus, pointStatus);
01327 
01328     // Find new uncut starting vertex
01329     uncutIndex = firstUnique(cPoints, pointStatus);
01330 
01331     if (uncutIndex == -1)
01332     {
01333         // All vertices either in loop or in anchor. So split is along single
01334         // face.
01335         WarningIn("Foam::cellCuts::calcAnchors")
01336             << "Invalid loop " << loop << " for cell " << cellI << endl
01337             << "All vertices of cell are either in loop or in anchor set"
01338             << endl;
01339 
01340         writeOBJ(".", cellI, loopPts, labelList(0));
01341 
01342         return false;
01343     }
01344 
01345     // Walk unset vertices and edges and mark with 2. These are the
01346     // pointset 2.
01347     walkEdges(cellI, cPoints[uncutIndex], 2, edgeStatus, pointStatus);
01348 
01349     // Collect both sets in lists.
01350     DynamicList<label> connectedPoints(cPoints.size());
01351     DynamicList<label> otherPoints(cPoints.size());
01352 
01353     forAllConstIter(Map<label>, pointStatus, iter)
01354     {
01355         if (iter() == 1)
01356         {
01357             connectedPoints.append(iter.key());
01358         }
01359         else if (iter() == 2)
01360         {
01361             otherPoints.append(iter.key());
01362         }
01363     }
01364     connectedPoints.shrink();
01365     otherPoints.shrink();
01366 
01367     // Check that all points have been used.
01368     uncutIndex = firstUnique(cPoints, pointStatus);
01369 
01370     if (uncutIndex != -1)
01371     {
01372         WarningIn("Foam::cellCuts::calcAnchors")
01373             << "Invalid loop " << loop << " for cell " << cellI
01374             << " since it splits the cell into more than two cells" << endl;
01375 
01376         writeOBJ(".", cellI, loopPts, connectedPoints);
01377 
01378         return false;
01379     }
01380 
01381 
01382     // Check that both parts (connectedPoints, otherPoints) have enough faces.
01383     labelHashSet connectedFaces(2*cFaces.size());
01384     labelHashSet otherFaces(2*cFaces.size());
01385 
01386     forAllConstIter(Map<label>, pointStatus, iter)
01387     {
01388         label pointI = iter.key();
01389 
01390         const labelList& pFaces = mesh().pointFaces()[pointI];
01391 
01392         if (iter() == 1)
01393         {
01394             forAll(pFaces, pFaceI)
01395             {
01396                 if (meshTools::faceOnCell(mesh(), cellI, pFaces[pFaceI]))
01397                 {
01398                     connectedFaces.insert(pFaces[pFaceI]);
01399                 }
01400             }
01401         }
01402         else if (iter() == 2)
01403         {
01404             forAll(pFaces, pFaceI)
01405             {
01406                 if (meshTools::faceOnCell(mesh(), cellI, pFaces[pFaceI]))
01407                 {
01408                     otherFaces.insert(pFaces[pFaceI]);
01409                 }
01410             }
01411         }
01412     }
01413 
01414     if (connectedFaces.size() < 3)
01415     {
01416         WarningIn("Foam::cellCuts::calcAnchors")
01417             << "Invalid loop " << loop << " for cell " << cellI
01418             << " since would have too few faces on one side." << nl
01419             << "All faces:" << cFaces << endl;
01420 
01421         writeOBJ(".", cellI, loopPts, connectedPoints);
01422 
01423         return false;
01424     }
01425 
01426     if (otherFaces.size() < 3)
01427     {
01428         WarningIn("Foam::cellCuts::calcAnchors")
01429             << "Invalid loop " << loop << " for cell " << cellI
01430             << " since would have too few faces on one side." << nl
01431             << "All faces:" << cFaces << endl;
01432 
01433         writeOBJ(".", cellI, loopPts, otherPoints);
01434 
01435         return false;
01436     }
01437 
01438 
01439     // Check that faces are split into two regions and not more.
01440     // When walking across the face the only transition of pointStatus is
01441     // from set1 to loop to set2 or back. Not allowed is from set1 to loop to
01442     // set1.
01443     {
01444         forAll(cFaces, i)
01445         {
01446             label faceI = cFaces[i];
01447 
01448             const face& f = mesh().faces()[faceI];
01449 
01450             bool hasSet1 = false;
01451             bool hasSet2 = false;
01452 
01453             label prevStat = pointStatus[f[0]];
01454 
01455             forAll(f, fp)
01456             {
01457                 label v0 = f[fp];
01458                 label pStat = pointStatus[v0];
01459 
01460                 if (pStat == prevStat)
01461                 {
01462                 }
01463                 else if (pStat == 0)
01464                 {
01465                     // Loop.
01466                 }
01467                 else if (pStat == 1)
01468                 {
01469                     if (hasSet1)
01470                     {
01471                         // Second occurence of set1.
01472                         WarningIn("Foam::cellCuts::calcAnchors")
01473                             << "Invalid loop " << loop << " for cell " << cellI
01474                             << " since face " << f << " would be split into"
01475                             << " more than two faces" << endl;
01476 
01477                         writeOBJ(".", cellI, loopPts, otherPoints);
01478 
01479                         return false;
01480                     }
01481 
01482                     hasSet1 = true;
01483                 }
01484                 else if (pStat == 2)
01485                 {
01486                     if (hasSet2)
01487                     {
01488                         // Second occurence of set1.
01489                         WarningIn("Foam::cellCuts::calcAnchors")
01490                             << "Invalid loop " << loop << " for cell " << cellI
01491                             << " since face " << f << " would be split into"
01492                             << " more than two faces" << endl;
01493 
01494                         writeOBJ(".", cellI, loopPts, otherPoints);
01495 
01496                         return false;
01497                     }
01498 
01499                     hasSet2 = true;
01500                 }
01501                 else
01502                 {
01503                     FatalErrorIn("Foam::cellCuts::calcAnchors")
01504                         << abort(FatalError);
01505                 }
01506 
01507                 prevStat = pStat;
01508 
01509 
01510                 label v1 = f.nextLabel(fp);
01511                 label edgeI = findEdge(faceI, v0, v1);
01512 
01513                 label eStat = edgeStatus[edgeI];
01514 
01515                 if (eStat == prevStat)
01516                 {
01517                 }
01518                 else if (eStat == 0)
01519                 {
01520                     // Loop.
01521                 }
01522                 else if (eStat == 1)
01523                 {
01524                     if (hasSet1)
01525                     {
01526                         // Second occurence of set1.
01527                         WarningIn("Foam::cellCuts::calcAnchors")
01528                             << "Invalid loop " << loop << " for cell " << cellI
01529                             << " since face " << f << " would be split into"
01530                             << " more than two faces" << endl;
01531 
01532                         writeOBJ(".", cellI, loopPts, otherPoints);
01533 
01534                         return false;
01535                     }
01536 
01537                     hasSet1 = true;
01538                 }
01539                 else if (eStat == 2)
01540                 {
01541                     if (hasSet2)
01542                     {
01543                         // Second occurence of set1.
01544                         WarningIn("Foam::cellCuts::calcAnchors")
01545                             << "Invalid loop " << loop << " for cell " << cellI
01546                             << " since face " << f << " would be split into"
01547                             << " more than two faces" << endl;
01548 
01549                         writeOBJ(".", cellI, loopPts, otherPoints);
01550 
01551                         return false;
01552                     }
01553 
01554                     hasSet2 = true;
01555                 }
01556                 prevStat = eStat;
01557             }
01558         }
01559     }
01560 
01561 
01562 
01563 
01564     // Check which one of point sets to use.
01565     bool loopOk = loopAnchorConsistent(cellI, loopPts, connectedPoints);
01566 
01567     //if (debug)
01568     {
01569         // Additional check: are non-anchor points on other side?
01570         bool otherLoopOk = loopAnchorConsistent(cellI, loopPts, otherPoints);
01571 
01572         if (loopOk == otherLoopOk)
01573         {
01574             // Both sets of points are supposedly on the same side as the
01575             // loop normal. Oops.
01576 
01577             WarningIn("Foam::cellCuts::calcAnchors")
01578                 << " For cell:" << cellI
01579                 << " achorpoints and nonanchorpoints are geometrically"
01580                 << " on same side!" << endl
01581                 << "cellPoints:" << cPoints << endl
01582                 << "loop:" << loop << endl
01583                 << "anchors:" << connectedPoints << endl
01584                 << "otherPoints:" << otherPoints << endl;
01585 
01586             writeOBJ(".", cellI, loopPts, connectedPoints);
01587         }
01588     }
01589 
01590     if (loopOk)
01591     {
01592         // connectedPoints on 'outside' of loop so these are anchor points
01593         anchorPoints.transfer(connectedPoints);
01594         connectedPoints.clear();
01595     }
01596     else
01597     {
01598         anchorPoints.transfer(otherPoints);
01599         otherPoints.clear();
01600     }
01601     return true;
01602 }
01603 
01604 
01605 Foam::pointField Foam::cellCuts::loopPoints
01606 (
01607     const labelList& loop,
01608     const scalarField& loopWeights
01609 ) const
01610 {
01611     pointField loopPts(loop.size());
01612 
01613     forAll(loop, fp)
01614     {
01615         loopPts[fp] = coord(loop[fp], loopWeights[fp]);
01616     }
01617     return loopPts;
01618 }
01619 
01620 
01621 // Returns weights of loop. Inverse of loopPoints.
01622 Foam::scalarField Foam::cellCuts::loopWeights(const labelList& loop) const
01623 {
01624     scalarField weights(loop.size());
01625 
01626     forAll(loop, fp)
01627     {
01628         label cut = loop[fp];
01629 
01630         if (isEdge(cut))
01631         {
01632             label edgeI = getEdge(cut);
01633 
01634             weights[fp] = edgeWeight_[edgeI];
01635         }
01636         else
01637         {
01638             weights[fp] = -GREAT;
01639         }
01640     }
01641     return weights;
01642 }
01643 
01644 
01645 // Check if cut edges in loop are compatible with ones in edgeIsCut_
01646 bool Foam::cellCuts::validEdgeLoop
01647 (
01648     const labelList& loop,
01649     const scalarField& loopWeights
01650 ) const
01651 {
01652     forAll(loop, fp)
01653     {
01654         label cut = loop[fp];
01655 
01656         if (isEdge(cut))
01657         {
01658             label edgeI = getEdge(cut);
01659 
01660             // Check: cut compatible only if can be snapped to existing one.
01661             if (edgeIsCut_[edgeI])
01662             {
01663                 scalar edgeLen = mesh().edges()[edgeI].mag(mesh().points());
01664 
01665                 if
01666                 (
01667                     mag(loopWeights[fp] - edgeWeight_[edgeI])
01668                   > geomCellLooper::snapTol()*edgeLen
01669                 )
01670                 {
01671                     // Positions differ too much->would create two cuts.
01672                     return false;
01673                 }
01674             }
01675         }
01676     }
01677     return true;
01678 }
01679 
01680 
01681 // Counts cuts on face. Includes cuts through vertices and through edges.
01682 // Assumes that if edge is cut both in edgeIsCut and in loop that the position
01683 // of the cut is the same.
01684 Foam::label Foam::cellCuts::countFaceCuts
01685 (
01686     const label faceI,
01687     const labelList& loop
01688 ) const
01689 {
01690     label nCuts = 0;
01691 
01692     // Count cut vertices
01693     const face& f = mesh().faces()[faceI];
01694 
01695     forAll(f, fp)
01696     {
01697         label vertI = f[fp];
01698 
01699         // Vertex already cut or mentioned in current loop.
01700         if
01701         (
01702             pointIsCut_[vertI]
01703          || (findIndex(loop, vertToEVert(vertI)) != -1)
01704         )
01705         {
01706             nCuts++;
01707         }
01708     }
01709 
01710     // Count cut edges.
01711     const labelList& fEdges = mesh().faceEdges()[faceI];
01712 
01713     forAll(fEdges, fEdgeI)
01714     {
01715         label edgeI = fEdges[fEdgeI];
01716 
01717         // Edge already cut or mentioned in current loop.
01718         if
01719         (
01720             edgeIsCut_[edgeI]
01721          || (findIndex(loop, edgeToEVert(edgeI)) != -1)
01722         )
01723         {
01724             nCuts++;
01725         }
01726     }
01727 
01728     return nCuts;
01729 }
01730 
01731 
01732 // Determine compatibility of loop with existing cut pattern. Does not use
01733 // cut-addressing (faceCuts_, cutCuts_)
01734 bool Foam::cellCuts::conservativeValidLoop
01735 (
01736     const label cellI,
01737     const labelList& loop
01738 ) const
01739 {
01740 
01741     if (loop.size() < 2)
01742     {
01743         return false;
01744     }
01745 
01746     forAll(loop, cutI)
01747     {
01748         if (isEdge(loop[cutI]))
01749         {
01750             label edgeI = getEdge(loop[cutI]);
01751 
01752             if (edgeIsCut_[edgeI])
01753             {
01754                 // edge compatibility already checked.
01755             }
01756             else
01757             {
01758                 // Quick rejection: vertices of edge itself cannot be cut.
01759                 const edge& e = mesh().edges()[edgeI];
01760 
01761                 if (pointIsCut_[e.start()] || pointIsCut_[e.end()])
01762                 {
01763                     return false;
01764                 }
01765 
01766 
01767                 // Check faces using this edge
01768                 const labelList& eFaces = mesh().edgeFaces()[edgeI];
01769 
01770                 forAll(eFaces, eFaceI)
01771                 {
01772                     label nCuts = countFaceCuts(eFaces[eFaceI], loop);
01773 
01774                     if (nCuts > 2)
01775                     {
01776                         return false;
01777                     }
01778                 }
01779             }
01780         }
01781         else
01782         {
01783             // Vertex cut
01784 
01785             label vertI = getVertex(loop[cutI]);
01786 
01787             if (!pointIsCut_[vertI])
01788             {
01789                 // New cut through vertex.
01790 
01791                 // Check edges using vertex.
01792                 const labelList& pEdges = mesh().pointEdges()[vertI];
01793 
01794                 forAll(pEdges, pEdgeI)
01795                 {
01796                     label edgeI = pEdges[pEdgeI];
01797 
01798                     if (edgeIsCut_[edgeI])
01799                     {
01800                         return false;
01801                     }
01802                 }
01803 
01804                 // Check faces using vertex.
01805                 const labelList& pFaces = mesh().pointFaces()[vertI];
01806 
01807                 forAll(pFaces, pFaceI)
01808                 {
01809                     label nCuts = countFaceCuts(pFaces[pFaceI], loop);
01810 
01811                     if (nCuts > 2)
01812                     {
01813                         return false;
01814                     }
01815                 }
01816             }
01817         }
01818     }
01819 
01820     // All ok.
01821     return true;
01822 }
01823 
01824 
01825 // Determine compatibility of loop with existing cut pattern. Does not use
01826 // derived cut-addressing (faceCuts), only pointIsCut, edgeIsCut.
01827 // Adds any cross-cuts found to newFaceSplitCut and sets cell points on
01828 // one side of the loop in anchorPoints.
01829 bool Foam::cellCuts::validLoop
01830 (
01831     const label cellI,
01832     const labelList& loop,
01833     const scalarField& loopWeights,
01834 
01835     Map<edge>& newFaceSplitCut,
01836     labelList& anchorPoints
01837 ) const
01838 {
01839     if (loop.size() < 2)
01840     {
01841         return false;
01842     }
01843 
01844     if (debug & 4)
01845     {
01846         // Allow as fallback the 'old' loop checking where only a single
01847         // cut per face is allowed.
01848         if (!conservativeValidLoop(cellI, loop))
01849         {
01850             return  false;
01851         }
01852     }
01853 
01854     forAll(loop, fp)
01855     {
01856         label cut = loop[fp];
01857         label nextCut = loop[(fp+1) % loop.size()];
01858 
01859         // Label (if any) of face cut (so cut not along existing edge)
01860         label meshFaceI = -1;
01861 
01862         if (isEdge(cut))
01863         {
01864             label edgeI = getEdge(cut);
01865 
01866             // Look one cut ahead to find if it is along existing edge.
01867 
01868             if (isEdge(nextCut))
01869             {
01870                 // From edge to edge -> cross cut
01871                 label nextEdgeI = getEdge(nextCut);
01872 
01873                 // Find face and mark as to be split.
01874                 meshFaceI = edgeEdgeToFace(cellI, edgeI, nextEdgeI);
01875 
01876                 if (meshFaceI == -1)
01877                 {
01878                     // Can't find face using both cut edges.
01879                     return false;
01880                 }
01881             }
01882             else
01883             {
01884                 // From edge to vertex -> cross cut only if no existing edge.
01885 
01886                 label nextVertI = getVertex(nextCut);
01887                 const edge& e = mesh().edges()[edgeI];
01888 
01889                 if (e.start() != nextVertI && e.end() != nextVertI)
01890                 {
01891                     // New edge. Find face and mark as to be split.
01892                     meshFaceI = edgeVertexToFace(cellI, edgeI, nextVertI);
01893 
01894                     if (meshFaceI == -1)
01895                     {
01896                         // Can't find face. Ilegal.
01897                         return false;
01898                     }
01899                 }
01900             }
01901         }
01902         else
01903         {
01904             // Cut is vertex.
01905             label vertI = getVertex(cut);
01906 
01907             if (isEdge(nextCut))
01908             {
01909                 // From vertex to edge -> cross cut only if no existing edge.
01910                 label nextEdgeI = getEdge(nextCut);
01911 
01912                 const edge& nextE = mesh().edges()[nextEdgeI];
01913 
01914                 if (nextE.start() != vertI && nextE.end() != vertI)
01915                 {
01916                     // Should be cross cut. Find face.
01917                     meshFaceI = edgeVertexToFace(cellI, nextEdgeI, vertI);
01918 
01919                     if (meshFaceI == -1)
01920                     {
01921                         return false;
01922                     }
01923                 }
01924             }
01925             else
01926             {
01927                 // From vertex to vertex -> cross cut only if no existing edge.
01928                 label nextVertI = getVertex(nextCut);
01929 
01930                 if (meshTools::findEdge(mesh(), vertI, nextVertI) == -1)
01931                 {
01932                     // New edge. Find face.
01933                     meshFaceI = vertexVertexToFace(cellI, vertI, nextVertI);
01934 
01935                     if (meshFaceI == -1)
01936                     {
01937                         return false;
01938                     }
01939                 }
01940             }
01941         }
01942 
01943         if (meshFaceI != -1)
01944         {
01945             // meshFaceI is cut across along cut-nextCut (so not along existing
01946             // edge). Check if this is compatible with existing pattern.
01947             edge cutEdge(cut, nextCut);
01948 
01949             Map<edge>::const_iterator iter = faceSplitCut_.find(meshFaceI);
01950 
01951             if (iter == faceSplitCut_.end())
01952             {
01953                 // Face not yet cut so insert.
01954                 newFaceSplitCut.insert(meshFaceI, cutEdge);
01955             }
01956             else
01957             {
01958                 // Face already cut. Ok if same edge.
01959                 if (iter() != cutEdge)
01960                 {
01961                     return false;
01962                 }
01963             }
01964         }
01965     }
01966 
01967     // Is there a face on which all cuts are?
01968     label faceContainingLoop = loopFace(cellI, loop);
01969 
01970     if (faceContainingLoop != -1)
01971     {
01972         WarningIn("Foam::cellCuts::validLoop")
01973             << "Found loop on cell " << cellI << " with all points"
01974             << " on face " << faceContainingLoop << endl;
01975 
01976         //writeOBJ(".", cellI, loopPoints(loop, loopWeights), labelList(0));
01977 
01978         return false;
01979     }
01980 
01981     // Calculate anchor points
01982     // Final success is determined by whether anchor points can be determined.
01983     return calcAnchors
01984     (
01985         cellI,
01986         loop,
01987         loopPoints(loop, loopWeights),
01988         anchorPoints
01989     );
01990 }
01991 
01992 
01993 // Update basic cut information (pointIsCut, edgeIsCut) from cellLoops.
01994 // Assumes cellLoops_ and edgeWeight_ already set and consistent.
01995 // Does not use any other information.
01996 void Foam::cellCuts::setFromCellLoops()
01997 {
01998     // 'Uncut' edges/vertices that are not used in loops.
01999     pointIsCut_ = false;
02000 
02001     edgeIsCut_ = false;
02002 
02003     faceSplitCut_.clear();
02004 
02005     forAll(cellLoops_, cellI)
02006     {
02007         const labelList& loop = cellLoops_[cellI];
02008 
02009         if (loop.size())
02010         {
02011             // Storage for cross-face cuts
02012             Map<edge> faceSplitCuts(loop.size());
02013             // Storage for points on one side of cell.
02014             labelList anchorPoints;
02015 
02016             if
02017             (
02018                !validLoop
02019                 (
02020                     cellI,
02021                     loop,
02022                     loopWeights(loop),
02023                     faceSplitCuts,
02024                     anchorPoints
02025                 )
02026             )
02027             {
02028                 //writeOBJ(".", cellI, loopPoints(cellI), anchorPoints);
02029 
02030                 //FatalErrorIn("cellCuts::setFromCellLoops()")
02031                 WarningIn("cellCuts::setFromCellLoops")
02032                     << "Illegal loop " << loop
02033                     << " when recreating cut-addressing"
02034                     << " from existing cellLoops for cell " << cellI
02035                     << endl;
02036                     //<< abort(FatalError);
02037 
02038                 cellLoops_[cellI].setSize(0);
02039                 cellAnchorPoints_[cellI].setSize(0);
02040             }
02041             else
02042             {
02043                 // Copy anchor points.
02044                 cellAnchorPoints_[cellI].transfer(anchorPoints);
02045 
02046                 // Copy faceSplitCuts into overall faceSplit info.
02047                 forAllConstIter(Map<edge>, faceSplitCuts, iter)
02048                 {
02049                     faceSplitCut_.insert(iter.key(), iter());
02050                 }
02051 
02052                 // Update edgeIsCut, pointIsCut information
02053                 forAll(loop, cutI)
02054                 {
02055                     label cut = loop[cutI];
02056 
02057                     if (isEdge(cut))
02058                     {
02059                         edgeIsCut_[getEdge(cut)] = true;
02060                     }
02061                     else
02062                     {
02063                         pointIsCut_[getVertex(cut)] = true;
02064                     }
02065                 }
02066             }
02067         }
02068     }
02069 
02070     // Reset edge weights
02071     forAll(edgeIsCut_, edgeI)
02072     {
02073         if (!edgeIsCut_[edgeI])
02074         {
02075             // Weight not used. Set to illegal value to make any use fall over.
02076             edgeWeight_[edgeI] = -GREAT;
02077         }
02078     }
02079 }
02080 
02081 
02082 // Upate basic cut information from single cellLoop. Returns true if loop
02083 // was valid.
02084 bool Foam::cellCuts::setFromCellLoop
02085 (
02086     const label cellI,
02087     const labelList& loop,
02088     const scalarField& loopWeights
02089 )
02090 {
02091     // Dump loop for debugging.
02092     if (debug)
02093     {
02094         OFstream str("last_cell.obj");
02095 
02096         str<< "# edges of cell " << cellI << nl;
02097 
02098         meshTools::writeOBJ
02099         (
02100             str,
02101             mesh().cells(),
02102             mesh().faces(),
02103             mesh().points(),
02104             labelList(1, cellI)
02105         );
02106 
02107 
02108         OFstream loopStr("last_loop.obj");
02109 
02110         loopStr<< "# looppoints for cell " << cellI << nl;
02111 
02112         pointField pointsOfLoop = loopPoints(loop, loopWeights);
02113 
02114         forAll(pointsOfLoop, i)
02115         {
02116             meshTools::writeOBJ(loopStr, pointsOfLoop[i]);
02117         }
02118 
02119         str << 'l';
02120 
02121         forAll(pointsOfLoop, i)
02122         {
02123             str << ' ' << i + 1;
02124         }
02125         str << ' ' << 1 << nl;
02126     }
02127 
02128     bool okLoop = false;
02129 
02130     if (validEdgeLoop(loop, loopWeights))
02131     {
02132         // Storage for cuts across face
02133         Map<edge> faceSplitCuts(loop.size());
02134         // Storage for points on one side of cell
02135         labelList anchorPoints;
02136 
02137         okLoop =
02138             validLoop(cellI, loop, loopWeights, faceSplitCuts, anchorPoints);
02139 
02140         if (okLoop)
02141         {
02142             // Valid loop. Copy cellLoops and anchorPoints
02143             cellLoops_[cellI] = loop;
02144             cellAnchorPoints_[cellI].transfer(anchorPoints);
02145 
02146             // Copy split cuts
02147             forAllConstIter(Map<edge>, faceSplitCuts, iter)
02148             {
02149                 faceSplitCut_.insert(iter.key(), iter());
02150             }
02151 
02152 
02153             // Update edgeIsCut, pointIsCut information
02154             forAll(loop, cutI)
02155             {
02156                 label cut = loop[cutI];
02157 
02158                 if (isEdge(cut))
02159                 {
02160                     label edgeI = getEdge(cut);
02161 
02162                     edgeIsCut_[edgeI] = true;
02163 
02164                     edgeWeight_[edgeI] = loopWeights[cutI];
02165                 }
02166                 else
02167                 {
02168                     label vertI = getVertex(cut);
02169 
02170                     pointIsCut_[vertI] = true;
02171                 }
02172             }
02173         }
02174     }
02175 
02176     return okLoop;
02177 }
02178 
02179 
02180 // Update basic cut information from cellLoops. Checks for consistency with
02181 // existing cut pattern.
02182 void Foam::cellCuts::setFromCellLoops
02183 (
02184     const labelList& cellLabels,
02185     const labelListList& cellLoops,
02186     const List<scalarField>& cellLoopWeights
02187 )
02188 {
02189     // 'Uncut' edges/vertices that are not used in loops.
02190     pointIsCut_ = false;
02191 
02192     edgeIsCut_ = false;
02193 
02194     forAll(cellLabels, cellLabelI)
02195     {
02196         label cellI = cellLabels[cellLabelI];
02197 
02198         const labelList& loop = cellLoops[cellLabelI];
02199 
02200         if (loop.size())
02201         {
02202             const scalarField& loopWeights = cellLoopWeights[cellLabelI];
02203 
02204             if (setFromCellLoop(cellI, loop, loopWeights))
02205             {
02206                 // Valid loop. Call above will have upated all already.
02207             }
02208             else
02209             {
02210                 // Clear cellLoops
02211                 cellLoops_[cellI].setSize(0);
02212             }
02213         }
02214     }
02215 }
02216 
02217 
02218 // Cut cells and update basic cut information from cellLoops. Checks each loop
02219 // for consistency with existing cut pattern.
02220 void Foam::cellCuts::setFromCellCutter
02221 (
02222     const cellLooper& cellCutter,
02223     const List<refineCell>& refCells
02224 )
02225 {
02226     // 'Uncut' edges/vertices that are not used in loops.
02227     pointIsCut_ = false;
02228 
02229     edgeIsCut_ = false;
02230 
02231     // storage for loop of cuts (cut vertices and/or cut edges)
02232     labelList cellLoop;
02233     scalarField cellLoopWeights;
02234 
02235     // For debugging purposes
02236     DynamicList<label> invalidCutCells(2);
02237     DynamicList<labelList> invalidCutLoops(2);
02238     DynamicList<scalarField> invalidCutLoopWeights(2);
02239 
02240 
02241     forAll(refCells, refCellI)
02242     {
02243         const refineCell& refCell = refCells[refCellI];
02244 
02245         label cellI = refCell.cellNo();
02246 
02247         const vector& refDir = refCell.direction();
02248 
02249 
02250         // Cut cell. Determines cellLoop and cellLoopWeights
02251         bool goodCut =
02252             cellCutter.cut
02253             (
02254                 refDir,
02255                 cellI,
02256 
02257                 pointIsCut_,
02258                 edgeIsCut_,
02259                 edgeWeight_,
02260 
02261                 cellLoop,
02262                 cellLoopWeights
02263             );
02264 
02265         // Check whether edge refinement is on a per face basis compatible with
02266         // current pattern.
02267         if (goodCut)
02268         {
02269             if (setFromCellLoop(cellI, cellLoop, cellLoopWeights))
02270             {
02271                 // Valid loop. Will have updated all info already.
02272             }
02273             else
02274             {
02275                 cellLoops_[cellI].setSize(0);
02276 
02277                 // Discarded by validLoop
02278                 if (debug)
02279                 {
02280                     invalidCutCells.append(cellI);
02281                     invalidCutLoops.append(cellLoop);
02282                     invalidCutLoopWeights.append(cellLoopWeights);
02283                 }
02284             }
02285         }
02286         else
02287         {
02288             // Clear cellLoops
02289             cellLoops_[cellI].setSize(0);
02290         }
02291     }
02292 
02293     if (debug && invalidCutCells.size())
02294     {
02295         invalidCutCells.shrink();
02296         invalidCutLoops.shrink();
02297         invalidCutLoopWeights.shrink();
02298 
02299         fileName cutsFile("invalidLoopCells.obj");
02300 
02301         Pout<< "cellCuts : writing inValidLoops cells to " << cutsFile << endl;
02302 
02303         OFstream cutsStream(cutsFile);
02304 
02305         meshTools::writeOBJ
02306         (
02307             cutsStream,
02308             mesh().cells(),
02309             mesh().faces(),
02310             mesh().points(),
02311             invalidCutCells
02312         );
02313 
02314         fileName loopsFile("invalidLoops.obj");
02315 
02316         Pout<< "cellCuts : writing inValidLoops loops to " << loopsFile << endl;
02317 
02318         OFstream loopsStream(loopsFile);
02319 
02320         label vertI = 0;
02321 
02322         forAll(invalidCutLoops, i)
02323         {
02324             writeOBJ
02325             (
02326                 loopsStream,
02327                 loopPoints(invalidCutLoops[i], invalidCutLoopWeights[i]),
02328                 vertI
02329             );
02330         }
02331     }
02332 }
02333 
02334 
02335 // Same as one before but cut plane prescribed (instead of just normal)
02336 void Foam::cellCuts::setFromCellCutter
02337 (
02338     const cellLooper& cellCutter,
02339     const labelList& cellLabels,
02340     const List<plane>& cellCutPlanes
02341 )
02342 {
02343     // 'Uncut' edges/vertices that are not used in loops.
02344     pointIsCut_ = false;
02345 
02346     edgeIsCut_ = false;
02347 
02348     // storage for loop of cuts (cut vertices and/or cut edges)
02349     labelList cellLoop;
02350     scalarField cellLoopWeights;
02351 
02352     // For debugging purposes
02353     DynamicList<label> invalidCutCells(2);
02354     DynamicList<labelList> invalidCutLoops(2);
02355     DynamicList<scalarField> invalidCutLoopWeights(2);
02356 
02357 
02358     forAll(cellLabels, i)
02359     {
02360         label cellI = cellLabels[i];
02361 
02362         // Cut cell. Determines cellLoop and cellLoopWeights
02363         bool goodCut =
02364             cellCutter.cut
02365             (
02366                 cellCutPlanes[i],
02367                 cellI,
02368 
02369                 pointIsCut_,
02370                 edgeIsCut_,
02371                 edgeWeight_,
02372 
02373                 cellLoop,
02374                 cellLoopWeights
02375             );
02376 
02377         // Check whether edge refinement is on a per face basis compatible with
02378         // current pattern.
02379         if (goodCut)
02380         {
02381             if (setFromCellLoop(cellI, cellLoop, cellLoopWeights))
02382             {
02383                 // Valid loop. Will have updated all info already.
02384             }
02385             else
02386             {
02387                 cellLoops_[cellI].setSize(0);
02388 
02389                 // Discarded by validLoop
02390                 if (debug)
02391                 {
02392                     invalidCutCells.append(cellI);
02393                     invalidCutLoops.append(cellLoop);
02394                     invalidCutLoopWeights.append(cellLoopWeights);
02395                 }
02396             }
02397         }
02398         else
02399         {
02400             // Clear cellLoops
02401             cellLoops_[cellI].setSize(0);
02402         }
02403     }
02404 
02405     if (debug && invalidCutCells.size())
02406     {
02407         invalidCutCells.shrink();
02408         invalidCutLoops.shrink();
02409         invalidCutLoopWeights.shrink();
02410 
02411         fileName cutsFile("invalidLoopCells.obj");
02412 
02413         Pout<< "cellCuts : writing inValidLoops cells to " << cutsFile << endl;
02414 
02415         OFstream cutsStream(cutsFile);
02416 
02417         meshTools::writeOBJ
02418         (
02419             cutsStream,
02420             mesh().cells(),
02421             mesh().faces(),
02422             mesh().points(),
02423             invalidCutCells
02424         );
02425 
02426         fileName loopsFile("invalidLoops.obj");
02427 
02428         Pout<< "cellCuts : writing inValidLoops loops to " << loopsFile << endl;
02429 
02430         OFstream loopsStream(loopsFile);
02431 
02432         label vertI = 0;
02433 
02434         forAll(invalidCutLoops, i)
02435         {
02436             writeOBJ
02437             (
02438                 loopsStream,
02439                 loopPoints(invalidCutLoops[i], invalidCutLoopWeights[i]),
02440                 vertI
02441             );
02442         }
02443     }
02444 }
02445 
02446 
02447 // Set orientation of loops
02448 void Foam::cellCuts::orientPlanesAndLoops()
02449 {
02450     // Determine anchorPoints if not yet done by validLoop.
02451     forAll(cellLoops_, cellI)
02452     {
02453         const labelList& loop = cellLoops_[cellI];
02454 
02455         if (loop.size() && cellAnchorPoints_[cellI].empty())
02456         {
02457             // Leave anchor points empty if illegal loop.
02458             calcAnchors
02459             (
02460                 cellI,
02461                 loop,
02462                 loopPoints(cellI),
02463                 cellAnchorPoints_[cellI]
02464             );
02465         }
02466     }
02467 
02468     if (debug & 2)
02469     {
02470         Pout<< "cellAnchorPoints:" << endl;
02471     }
02472     forAll(cellAnchorPoints_, cellI)
02473     {
02474         if (cellLoops_[cellI].size())
02475         {
02476             if (cellAnchorPoints_[cellI].empty())
02477             {
02478                 FatalErrorIn("orientPlanesAndLoops()")
02479                     << "No anchor points for cut cell " << cellI << endl
02480                     << "cellLoop:" << cellLoops_[cellI] << abort(FatalError);
02481             }
02482 
02483             if (debug & 2)
02484             {
02485                 Pout<< "    cell:" << cellI << " anchored at "
02486                     << cellAnchorPoints_[cellI] << endl;
02487             }
02488         }
02489     }
02490 
02491     // Calculate number of valid cellLoops
02492     nLoops_ = 0;
02493 
02494     forAll(cellLoops_, cellI)
02495     {
02496         if (cellLoops_[cellI].size())
02497         {
02498             nLoops_++;
02499         }
02500     }
02501 }
02502 
02503 
02504 // Do all: calculate addressing, calculate loops splitting cells
02505 void Foam::cellCuts::calcLoopsAndAddressing(const labelList& cutCells)
02506 {
02507     // Sanity check on weights
02508     forAll(edgeIsCut_, edgeI)
02509     {
02510         if (edgeIsCut_[edgeI])
02511         {
02512             scalar weight = edgeWeight_[edgeI];
02513 
02514             if (weight < 0 || weight > 1)
02515             {
02516                 FatalErrorIn
02517                 (
02518                     "cellCuts::calcLoopsAndAddressing(const labelList&)"
02519                 )   << "Weight out of range [0,1]. Edge " << edgeI
02520                     << " verts:" << mesh().edges()[edgeI]
02521                     << " weight:" << weight << abort(FatalError);
02522             }
02523         }
02524         else
02525         {
02526             // Weight not used. Set to illegal value to make any use fall over.
02527             edgeWeight_[edgeI] = -GREAT;
02528         }
02529     }
02530 
02531 
02532     // Calculate faces that split cells in two
02533     calcCellLoops(cutCells);
02534 
02535     if (debug & 2)
02536     {
02537         Pout<< "-- cellLoops --" << endl;
02538         forAll(cellLoops_, cellI)
02539         {
02540             const labelList& loop = cellLoops_[cellI];
02541 
02542             if (loop.size())
02543             {
02544                 Pout<< "cell:" << cellI << "  ";
02545                 writeCuts(Pout, loop, loopWeights(loop));
02546                 Pout<< endl;
02547             }
02548         }
02549     }
02550 
02551     // Redo basic cut information (pointIsCut, edgeIsCut, faceSplitCut)
02552     // using cellLoop only.
02553     setFromCellLoops();
02554 }
02555 
02556 
02557 void Foam::cellCuts::check() const
02558 {
02559     // Check weights for unsnapped values
02560     forAll(edgeIsCut_, edgeI)
02561     {
02562         if (edgeIsCut_[edgeI])
02563         {
02564             if
02565             (
02566                 edgeWeight_[edgeI] < geomCellLooper::snapTol()
02567              || edgeWeight_[edgeI] > (1 - geomCellLooper::snapTol())
02568             )
02569             {
02570                 // Should have been snapped.
02571                 //FatalErrorIn("cellCuts::check()")
02572                 WarningIn("cellCuts::check()")
02573                     << "edge:" << edgeI << " vertices:"
02574                     << mesh().edges()[edgeI]
02575                     << " weight:" << edgeWeight_[edgeI] << " should have been"
02576                     << " snapped to one of its endpoints"
02577                     //<< abort(FatalError);
02578                     << endl;
02579             }
02580         }
02581         else
02582         {
02583             if (edgeWeight_[edgeI] > - 1)
02584             {
02585                 FatalErrorIn("cellCuts::check()")
02586                     << "edge:" << edgeI << " vertices:"
02587                     << mesh().edges()[edgeI]
02588                     << " weight:" << edgeWeight_[edgeI] << " is not cut but"
02589                     << " its weight is not marked invalid"
02590                     << abort(FatalError);
02591             }
02592         }
02593     }
02594 
02595     // Check that all elements of cellloop are registered
02596     forAll(cellLoops_, cellI)
02597     {
02598         const labelList& loop = cellLoops_[cellI];
02599 
02600         forAll(loop, i)
02601         {
02602             label cut = loop[i];
02603 
02604             if
02605             (
02606                 (isEdge(cut) && !edgeIsCut_[getEdge(cut)])
02607              || (!isEdge(cut) && !pointIsCut_[getVertex(cut)])
02608             )
02609             {
02610                 labelList cuts(1, cut);
02611                 writeCuts(Pout, cuts, loopWeights(cuts));
02612 
02613                 FatalErrorIn("cellCuts::check()")
02614                     << "cell:" << cellI << " loop:"
02615                     << loop
02616                     << " cut:" << cut << " is not marked as cut"
02617                     << abort(FatalError);
02618             }
02619         }
02620     }
02621 
02622     // Check that no elements of cell loop are anchor point.
02623     forAll(cellLoops_, cellI)
02624     {
02625         const labelList& anchors = cellAnchorPoints_[cellI];
02626 
02627         const labelList& loop = cellLoops_[cellI];
02628 
02629         if (loop.size() && anchors.empty())
02630         {
02631             FatalErrorIn("cellCuts::check()")
02632                 << "cell:" << cellI << " loop:" << loop
02633                 << " has no anchor points"
02634                 << abort(FatalError);
02635         }
02636 
02637 
02638         forAll(loop, i)
02639         {
02640             label cut = loop[i];
02641 
02642             if
02643             (
02644                 !isEdge(cut)
02645              && findIndex(anchors, getVertex(cut)) != -1
02646             )
02647             {
02648                 FatalErrorIn("cellCuts::check()")
02649                     << "cell:" << cellI << " loop:" << loop
02650                     << " anchor points:" << anchors
02651                     << " anchor:" << getVertex(cut) << " is part of loop"
02652                     << abort(FatalError);
02653             }
02654         }
02655     }
02656 
02657 
02658     // Check that cut faces have a neighbour that is cut.
02659     forAllConstIter(Map<edge>, faceSplitCut_, iter)
02660     {
02661         label faceI = iter.key();
02662 
02663         if (mesh().isInternalFace(faceI))
02664         {
02665             label own = mesh().faceOwner()[faceI];
02666             label nei = mesh().faceNeighbour()[faceI];
02667 
02668             if (cellLoops_[own].empty() && cellLoops_[nei].empty())
02669             {
02670                 FatalErrorIn("cellCuts::check()")
02671                     << "Internal face:" << faceI << " cut by " << iter()
02672                     << " has owner:" << own
02673                     << " and neighbour:" << nei
02674                     << " that are both uncut"
02675                     << abort(FatalError);
02676             }
02677         }
02678         else
02679         {
02680             label own = mesh().faceOwner()[faceI];
02681 
02682             if (cellLoops_[own].empty())
02683             {
02684                 FatalErrorIn("cellCuts::check()")
02685                     << "Boundary face:" << faceI << " cut by " << iter()
02686                     << " has owner:" << own
02687                     << " that is uncut"
02688                     << abort(FatalError);
02689             }
02690         }
02691     }
02692 }
02693 
02694 
02695 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
02696 
02697 // Construct from cells to cut and pattern of cuts
02698 Foam::cellCuts::cellCuts
02699 (
02700     const polyMesh& mesh,
02701     const labelList& cutCells,
02702     const labelList& meshVerts,
02703     const labelList& meshEdges,
02704     const scalarField& meshEdgeWeights
02705 )
02706 :
02707     edgeVertex(mesh),
02708     pointIsCut_(expand(mesh.nPoints(), meshVerts)),
02709     edgeIsCut_(expand(mesh.nEdges(), meshEdges)),
02710     edgeWeight_(expand(mesh.nEdges(), meshEdges, meshEdgeWeights)),
02711     faceCutsPtr_(NULL),
02712     faceSplitCut_(cutCells.size()),
02713     cellLoops_(mesh.nCells()),
02714     nLoops_(-1),
02715     cellAnchorPoints_(mesh.nCells())
02716 {
02717     if (debug)
02718     {
02719         Pout<< "cellCuts : constructor from cut verts and edges" << endl;
02720     }
02721 
02722     calcLoopsAndAddressing(cutCells);
02723 
02724     // Calculate planes and flip cellLoops if nessecary
02725     orientPlanesAndLoops();
02726 
02727     if (debug)
02728     {
02729         check();
02730     }
02731 
02732     clearOut();
02733 
02734     if (debug)
02735     {
02736         Pout<< "cellCuts : leaving constructor from cut verts and edges"
02737             << endl;
02738     }
02739 }
02740 
02741 
02742 // Construct from pattern of cuts. Finds out itself which cells are cut.
02743 // (can go wrong if e.g. all neighbours of cell are refined)
02744 Foam::cellCuts::cellCuts
02745 (
02746     const polyMesh& mesh,
02747     const labelList& meshVerts,
02748     const labelList& meshEdges,
02749     const scalarField& meshEdgeWeights
02750 )
02751 :
02752     edgeVertex(mesh),
02753     pointIsCut_(expand(mesh.nPoints(), meshVerts)),
02754     edgeIsCut_(expand(mesh.nEdges(), meshEdges)),
02755     edgeWeight_(expand(mesh.nEdges(), meshEdges, meshEdgeWeights)),
02756     faceCutsPtr_(NULL),
02757     faceSplitCut_(mesh.nFaces()/10 + 1),
02758     cellLoops_(mesh.nCells()),
02759     nLoops_(-1),
02760     cellAnchorPoints_(mesh.nCells())
02761 {
02762     if (debug)
02763     {
02764         Pout<< "cellCuts : constructor from cellLoops" << endl;
02765     }
02766 
02767     calcLoopsAndAddressing(identity(mesh.nCells()));
02768 
02769     // Calculate planes and flip cellLoops if nessecary
02770     orientPlanesAndLoops();
02771 
02772     if (debug)
02773     {
02774         check();
02775     }
02776 
02777     clearOut();
02778 
02779     if (debug)
02780     {
02781         Pout<< "cellCuts : leaving constructor from cellLoops" << endl;
02782     }
02783 }
02784 
02785 
02786 // Construct from complete cellLoops. Assumes correct cut pattern.
02787 // Only constructs cut-cut addressing and cellAnchorPoints
02788 Foam::cellCuts::cellCuts
02789 (
02790     const polyMesh& mesh,
02791     const labelList& cellLabels,
02792     const labelListList& cellLoops,
02793     const List<scalarField>& cellEdgeWeights
02794 )
02795 :
02796     edgeVertex(mesh),
02797     pointIsCut_(mesh.nPoints(), false),
02798     edgeIsCut_(mesh.nEdges(), false),
02799     edgeWeight_(mesh.nEdges(), -GREAT),
02800     faceCutsPtr_(NULL),
02801     faceSplitCut_(cellLabels.size()),
02802     cellLoops_(mesh.nCells()),
02803     nLoops_(-1),
02804     cellAnchorPoints_(mesh.nCells())
02805 {
02806     if (debug)
02807     {
02808         Pout<< "cellCuts : constructor from cellLoops" << endl;
02809     }
02810 
02811     // Update pointIsCut, edgeIsCut, faceSplitCut from cell loops.
02812     // Makes sure cuts are consistent
02813     setFromCellLoops(cellLabels, cellLoops, cellEdgeWeights);
02814 
02815     // Calculate planes and flip cellLoops if nessecary
02816     orientPlanesAndLoops();
02817 
02818     if (debug)
02819     {
02820         check();
02821     }
02822 
02823     clearOut();
02824 
02825     if (debug)
02826     {
02827         Pout<< "cellCuts : leaving constructor from cellLoops" << endl;
02828     }
02829 }
02830 
02831 
02832 // Construct from list of cells to cut and cell cutter.
02833 Foam::cellCuts::cellCuts
02834 (
02835     const polyMesh& mesh,
02836     const cellLooper& cellCutter,
02837     const List<refineCell>& refCells
02838 )
02839 :
02840     edgeVertex(mesh),
02841     pointIsCut_(mesh.nPoints(), false),
02842     edgeIsCut_(mesh.nEdges(), false),
02843     edgeWeight_(mesh.nEdges(), -GREAT),
02844     faceCutsPtr_(NULL),
02845     faceSplitCut_(refCells.size()),
02846     cellLoops_(mesh.nCells()),
02847     nLoops_(-1),
02848     cellAnchorPoints_(mesh.nCells())
02849 {
02850     if (debug)
02851     {
02852         Pout<< "cellCuts : constructor from cellCutter" << endl;
02853     }
02854 
02855     // Update pointIsCut, edgeIsCut, faceSplitCut from cell loops.
02856     // Makes sure cuts are consistent
02857     setFromCellCutter(cellCutter, refCells);
02858 
02859     // Calculate planes and flip cellLoops if nessecary
02860     orientPlanesAndLoops();
02861 
02862     if (debug)
02863     {
02864         check();
02865     }
02866 
02867     clearOut();
02868 
02869     if (debug)
02870     {
02871         Pout<< "cellCuts : leaving constructor from cellCutter" << endl;
02872     }
02873 }
02874 
02875 
02876 // Construct from list of cells to cut, plane to cut with and cell cutter.
02877 Foam::cellCuts::cellCuts
02878 (
02879     const polyMesh& mesh,
02880     const cellLooper& cellCutter,
02881     const labelList& cellLabels,
02882     const List<plane>& cutPlanes
02883 )
02884 :
02885     edgeVertex(mesh),
02886     pointIsCut_(mesh.nPoints(), false),
02887     edgeIsCut_(mesh.nEdges(), false),
02888     edgeWeight_(mesh.nEdges(), -GREAT),
02889     faceCutsPtr_(NULL),
02890     faceSplitCut_(cellLabels.size()),
02891     cellLoops_(mesh.nCells()),
02892     nLoops_(-1),
02893     cellAnchorPoints_(mesh.nCells())
02894 {
02895     if (debug)
02896     {
02897         Pout<< "cellCuts : constructor from cellCutter with prescribed plane"
02898             << endl;
02899     }
02900 
02901     // Update pointIsCut, edgeIsCut, faceSplitCut from cell loops.
02902     // Makes sure cuts are consistent
02903     setFromCellCutter(cellCutter, cellLabels, cutPlanes);
02904 
02905     // Calculate planes and flip cellLoops if nessecary
02906     orientPlanesAndLoops();
02907 
02908     if (debug)
02909     {
02910         check();
02911     }
02912 
02913     clearOut();
02914 
02915     if (debug)
02916     {
02917         Pout<< "cellCuts : leaving constructor from cellCutter with prescribed"
02918             << " plane" << endl;
02919     }
02920 }
02921 
02922 
02923 // Construct from components
02924 Foam::cellCuts::cellCuts
02925 (
02926     const polyMesh& mesh,
02927     const boolList& pointIsCut,
02928     const boolList& edgeIsCut,
02929     const scalarField& edgeWeight,
02930     const Map<edge>& faceSplitCut,
02931     const labelListList& cellLoops,
02932     const label nLoops,
02933     const labelListList& cellAnchorPoints
02934 )
02935 :
02936     edgeVertex(mesh),
02937     pointIsCut_(pointIsCut),
02938     edgeIsCut_(edgeIsCut),
02939     edgeWeight_(edgeWeight),
02940     faceCutsPtr_(NULL),
02941     faceSplitCut_(faceSplitCut),
02942     cellLoops_(cellLoops),
02943     nLoops_(nLoops),
02944     cellAnchorPoints_(cellAnchorPoints)
02945 {
02946     if (debug)
02947     {
02948         Pout<< "cellCuts : constructor from components" << endl;
02949         Pout<< "cellCuts : leaving constructor from components" << endl;
02950     }
02951 }
02952 
02953 
02954 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
02955 
02956 Foam::cellCuts::~cellCuts()
02957 {
02958     clearOut();
02959 }
02960 
02961 
02962 void Foam::cellCuts::clearOut()
02963 {
02964     deleteDemandDrivenData(faceCutsPtr_);
02965 }
02966 
02967 
02968 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
02969 
02970 Foam::pointField Foam::cellCuts::loopPoints(const label cellI) const
02971 {
02972     const labelList& loop = cellLoops_[cellI];
02973 
02974     pointField loopPts(loop.size());
02975 
02976     forAll(loop, fp)
02977     {
02978         label cut = loop[fp];
02979 
02980         if (isEdge(cut))
02981         {
02982             label edgeI = getEdge(cut);
02983 
02984             loopPts[fp] = coord(cut, edgeWeight_[edgeI]);
02985         }
02986         else
02987         {
02988             loopPts[fp] = coord(cut, -GREAT);
02989         }
02990     }
02991     return loopPts;
02992 }
02993 
02994 
02995 // Flip loop for cell
02996 void Foam::cellCuts::flip(const label cellI)
02997 {
02998     labelList& loop = cellLoops_[cellI];
02999 
03000     reverse(loop);
03001 
03002     // Reverse anchor point set.
03003     cellAnchorPoints_[cellI] =
03004         nonAnchorPoints
03005         (
03006             mesh().cellPoints()[cellI],
03007             cellAnchorPoints_[cellI],
03008             loop
03009         );
03010 }
03011 
03012 
03013 // Flip loop only
03014 void Foam::cellCuts::flipLoopOnly(const label cellI)
03015 {
03016     labelList& loop = cellLoops_[cellI];
03017 
03018     reverse(loop);
03019 }
03020 
03021 
03022 void Foam::cellCuts::writeOBJ
03023 (
03024     Ostream& os,
03025     const pointField& loopPts,
03026     label& vertI
03027 ) const
03028 {
03029     label startVertI = vertI;
03030 
03031     forAll(loopPts, fp)
03032     {
03033         const point& pt = loopPts[fp];
03034 
03035         os << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
03036 
03037         vertI++;
03038     }
03039 
03040     os << 'f';
03041     forAll(loopPts, fp)
03042     {
03043         os  << ' ' << startVertI + fp + 1;
03044     }
03045     os<< endl;
03046 }
03047 
03048 
03049 void Foam::cellCuts::writeOBJ(Ostream& os) const
03050 {
03051     label vertI = 0;
03052 
03053     forAll(cellLoops_, cellI)
03054     {
03055         writeOBJ(os, loopPoints(cellI), vertI);
03056     }
03057 }
03058 
03059 
03060 void Foam::cellCuts::writeCellOBJ(const fileName& dir, const label cellI) const
03061 {
03062     const labelList& anchors = cellAnchorPoints_[cellI];
03063 
03064     writeOBJ(dir, cellI, loopPoints(cellI), anchors);
03065 }
03066 
03067 
03068 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd