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

meshDualiser.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 Class
00026     meshDualiser
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "meshDualiser.H"
00031 #include "meshTools.H"
00032 #include "polyMesh.H"
00033 #include "polyTopoChange.H"
00034 #include "mapPolyMesh.H"
00035 #include "edgeFaceCirculator.H"
00036 #include "mergePoints.H"
00037 #include "OFstream.H"
00038 
00039 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00040 
00041 defineTypeNameAndDebug(Foam::meshDualiser, 0);
00042 
00043 
00044 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00045 
00046 void Foam::meshDualiser::checkPolyTopoChange(const polyTopoChange& meshMod)
00047 {
00048     // Assume no removed points
00049     pointField points(meshMod.points().size());
00050     forAll(meshMod.points(), i)
00051     {
00052         points[i] = meshMod.points()[i];
00053     }
00054 
00055     labelList oldToNew;
00056     pointField newPoints;
00057     bool hasMerged = mergePoints
00058     (
00059         points,
00060         1E-6,
00061         false,
00062         oldToNew,
00063         newPoints
00064     );
00065 
00066     if (hasMerged)
00067     {
00068         labelListList newToOld(invertOneToMany(newPoints.size(), oldToNew));
00069 
00070         forAll(newToOld, newI)
00071         {
00072             if (newToOld[newI].size() != 1)
00073             {
00074                 FatalErrorIn
00075                 (
00076                     "meshDualiser::checkPolyTopoChange(const polyTopoChange&)"
00077                 )   << "duplicate verts:" << newToOld[newI]
00078                     << " coords:"
00079                     << UIndirectList<point>(points, newToOld[newI])()
00080                     << abort(FatalError);
00081             }
00082         }
00083     }
00084 }
00085 
00086 
00087 // Dump state so far.
00088 void Foam::meshDualiser::dumpPolyTopoChange
00089 (
00090     const polyTopoChange& meshMod,
00091     const fileName& prefix
00092 )
00093 {
00094     OFstream str1(prefix + "Faces.obj");
00095     OFstream str2(prefix + "Edges.obj");
00096 
00097     Info<< "Dumping current polyTopoChange. Faces to " << str1.name()
00098         << " , points and edges to " << str2.name() << endl;
00099 
00100     const DynamicList<point>& points = meshMod.points();
00101 
00102     forAll(points, pointI)
00103     {
00104         meshTools::writeOBJ(str1, points[pointI]);
00105         meshTools::writeOBJ(str2, points[pointI]);
00106     }
00107 
00108     const DynamicList<face>& faces = meshMod.faces();
00109 
00110     forAll(faces, faceI)
00111     {
00112         const face& f = faces[faceI];
00113 
00114         str1<< 'f';
00115         forAll(f, fp)
00116         {
00117             str1<< ' ' << f[fp]+1;
00118         }
00119         str1<< nl;
00120 
00121         str2<< 'l';
00122         forAll(f, fp)
00123         {
00124             str2<< ' ' << f[fp]+1;
00125         }
00126         str2<< ' ' << f[0]+1 << nl;
00127     }
00128 }
00129 
00130 
00131 //- Given cell and point on mesh finds the corresponding dualCell. Most
00132 //  points become only one cell but the feature points might be split.
00133 Foam::label Foam::meshDualiser::findDualCell
00134 (
00135     const label cellI,
00136     const label pointI
00137 ) const
00138 {
00139     const labelList& dualCells = pointToDualCells_[pointI];
00140 
00141     if (dualCells.size() == 1)
00142     {
00143         return dualCells[0];
00144     }
00145     else
00146     {
00147         label index = findIndex(mesh_.pointCells()[pointI], cellI);
00148 
00149         return dualCells[index];
00150     }
00151 }
00152 
00153 
00154 // Helper function to generate dualpoints on all boundary edges emanating
00155 // from (boundary & feature) point
00156 void Foam::meshDualiser::generateDualBoundaryEdges
00157 (
00158     const PackedBoolList& isBoundaryEdge,
00159     const label pointI,
00160     polyTopoChange& meshMod
00161 )
00162 {
00163     const labelList& pEdges = mesh_.pointEdges()[pointI];
00164 
00165     forAll(pEdges, pEdgeI)
00166     {
00167         label edgeI = pEdges[pEdgeI];
00168 
00169         if (edgeToDualPoint_[edgeI] == -1 && isBoundaryEdge.get(edgeI) == 1)
00170         {
00171             const edge& e = mesh_.edges()[edgeI];
00172 
00173             edgeToDualPoint_[edgeI] = meshMod.addPoint
00174             (
00175                 e.centre(mesh_.points()),
00176                 pointI, // masterPoint
00177                 -1,     // zoneID
00178                 true    // inCell
00179             );
00180         }
00181     }
00182 }
00183 
00184 
00185 // Return true if point on face has same dual cells on both owner and neighbour
00186 // sides.
00187 bool Foam::meshDualiser::sameDualCell
00188 (
00189     const label faceI,
00190     const label pointI
00191 ) const
00192 {
00193     if (!mesh_.isInternalFace(faceI))
00194     {
00195         FatalErrorIn("sameDualCell(const label, const label)")
00196             << "face:" << faceI << " is not internal face."
00197             << abort(FatalError);
00198     }
00199 
00200     label own = mesh_.faceOwner()[faceI];
00201     label nei = mesh_.faceNeighbour()[faceI];
00202 
00203     return findDualCell(own, pointI) == findDualCell(nei, pointI);
00204 }
00205 
00206 
00207 Foam::label Foam::meshDualiser::addInternalFace
00208 (
00209     const label masterPointI,
00210     const label masterEdgeI,
00211     const label masterFaceI,
00212 
00213     const bool edgeOrder,
00214     const label dualCell0,
00215     const label dualCell1,
00216     const DynamicList<label>& verts,
00217     polyTopoChange& meshMod
00218 ) const
00219 {
00220     face newFace(verts);
00221 
00222     if (edgeOrder != (dualCell0 < dualCell1))
00223     {
00224         reverse(newFace);
00225     }
00226 
00227     if (debug)
00228     {
00229         pointField facePoints(meshMod.points(), newFace);
00230 
00231         labelList oldToNew;
00232         pointField newPoints;
00233         bool hasMerged = mergePoints
00234         (
00235             facePoints,
00236             1E-6,
00237             false,
00238             oldToNew,
00239             newPoints
00240         );
00241 
00242         if (hasMerged)
00243         {
00244             FatalErrorIn("addInternalFace(..)")
00245                 << "verts:" << verts << " newFace:" << newFace
00246                 << " face points:" << facePoints
00247                 << abort(FatalError);
00248         }
00249     }
00250 
00251 
00252     label zoneID = -1;
00253     bool zoneFlip = false;
00254     if (masterFaceI != -1)
00255     {
00256         zoneID = mesh_.faceZones().whichZone(masterFaceI);
00257 
00258         if (zoneID != -1)
00259         {
00260             const faceZone& fZone = mesh_.faceZones()[zoneID];
00261 
00262             zoneFlip = fZone.flipMap()[fZone.whichFace(masterFaceI)];
00263         }
00264     }
00265 
00266     label dualFaceI;
00267 
00268     if (dualCell0 < dualCell1)
00269     {
00270         dualFaceI = meshMod.addFace
00271         (
00272             newFace,
00273             dualCell0,      // own
00274             dualCell1,      // nei
00275             masterPointI,   // masterPointID
00276             masterEdgeI,    // masterEdgeID
00277             masterFaceI,    // masterFaceID
00278             false,          // flipFaceFlux
00279             -1,             // patchID
00280             zoneID,         // zoneID
00281             zoneFlip        // zoneFlip
00282         );
00283 
00284         //pointField dualPoints(meshMod.points());
00285         //vector n(newFace.normal(dualPoints));
00286         //n /= mag(n);
00287         //Pout<< "Generated internal dualFace:" << dualFaceI
00288         //    << " verts:" << newFace
00289         //    << " points:" << UIndirectList<point>(meshMod.points(), newFace)()
00290         //    << " n:" << n
00291         //    << " between dualowner:" << dualCell0
00292         //    << " dualneigbour:" << dualCell1
00293         //    << endl;
00294     }
00295     else
00296     {
00297         dualFaceI = meshMod.addFace
00298         (
00299             newFace,
00300             dualCell1,      // own
00301             dualCell0,      // nei
00302             masterPointI,   // masterPointID
00303             masterEdgeI,    // masterEdgeID
00304             masterFaceI,    // masterFaceID
00305             false,          // flipFaceFlux
00306             -1,             // patchID
00307             zoneID,         // zoneID
00308             zoneFlip        // zoneFlip
00309         );
00310 
00311         //pointField dualPoints(meshMod.points());
00312         //vector n(newFace.normal(dualPoints));
00313         //n /= mag(n);
00314         //Pout<< "Generated internal dualFace:" << dualFaceI
00315         //    << " verts:" << newFace
00316         //    << " points:" << UIndirectList<point>(meshMod.points(), newFace)()
00317         //    << " n:" << n
00318         //    << " between dualowner:" << dualCell1
00319         //    << " dualneigbour:" << dualCell0
00320         //    << endl;
00321     }
00322     return dualFaceI;
00323 }
00324 
00325 
00326 Foam::label Foam::meshDualiser::addBoundaryFace
00327 (
00328     const label masterPointI,
00329     const label masterEdgeI,
00330     const label masterFaceI,
00331 
00332     const label dualCellI,
00333     const label patchI,
00334     const DynamicList<label>& verts,
00335     polyTopoChange& meshMod
00336 ) const
00337 {
00338     face newFace(verts);
00339 
00340     label zoneID = -1;
00341     bool zoneFlip = false;
00342     if (masterFaceI != -1)
00343     {
00344         zoneID = mesh_.faceZones().whichZone(masterFaceI);
00345 
00346         if (zoneID != -1)
00347         {
00348             const faceZone& fZone = mesh_.faceZones()[zoneID];
00349 
00350             zoneFlip = fZone.flipMap()[fZone.whichFace(masterFaceI)];
00351         }
00352     }
00353 
00354     label dualFaceI = meshMod.addFace
00355     (
00356         newFace,
00357         dualCellI,      // own
00358         -1,             // nei
00359         masterPointI,   // masterPointID
00360         masterEdgeI,    // masterEdgeID
00361         masterFaceI,    // masterFaceID
00362         false,          // flipFaceFlux
00363         patchI,         // patchID
00364         zoneID,         // zoneID
00365         zoneFlip        // zoneFlip
00366     );
00367 
00368     //pointField dualPoints(meshMod.points());
00369     //vector n(newFace.normal(dualPoints));
00370     //n /= mag(n);
00371     //Pout<< "Generated boundary dualFace:" << dualFaceI
00372     //    << " verts:" << newFace
00373     //    << " points:" << UIndirectList<point>(meshMod.points(), newFace)()
00374     //    << " n:" << n
00375     //    << " on dualowner:" << dualCellI
00376     //    << endl;
00377     return dualFaceI;
00378 }
00379 
00380 
00381 // Walks around edgeI.
00382 // splitFace=true : creates multiple faces
00383 // splitFace=false: creates single face if same dual cells on both sides,
00384 //                  multiple faces otherwise.
00385 void Foam::meshDualiser::createFacesAroundEdge
00386 (
00387     const bool splitFace,
00388     const PackedBoolList& isBoundaryEdge,
00389     const label edgeI,
00390     const label startFaceI,
00391     polyTopoChange& meshMod,
00392     boolList& doneEFaces
00393 ) const
00394 {
00395     const edge& e = mesh_.edges()[edgeI];
00396     const labelList& eFaces = mesh_.edgeFaces()[edgeI];
00397 
00398     label fp = edgeFaceCirculator::getMinIndex
00399     (
00400         mesh_.faces()[startFaceI],
00401         e[0],
00402         e[1]
00403     );
00404 
00405     edgeFaceCirculator ie
00406     (
00407         mesh_,
00408         startFaceI, // face
00409         true,       // ownerSide
00410         fp,         // fp
00411         isBoundaryEdge.get(edgeI) == 1  // isBoundaryEdge
00412     );
00413     ie.setCanonical();
00414 
00415     bool edgeOrder = ie.sameOrder(e[0], e[1]);
00416     label startFaceLabel = ie.faceLabel();
00417 
00418     //Pout<< "At edge:" << edgeI << " verts:" << e
00419     //    << " points:" << mesh_.points()[e[0]] << mesh_.points()[e[1]]
00420     //    << " started walking at face:" << ie.faceLabel()
00421     //    << " verts:" << mesh_.faces()[ie.faceLabel()]
00422     //    << " edgeOrder:" << edgeOrder
00423     //    << " in direction of cell:" << ie.cellLabel()
00424     //    << endl;
00425 
00426     // Walk and collect face.
00427     DynamicList<label> verts(100);
00428 
00429     if (edgeToDualPoint_[edgeI] != -1)
00430     {
00431         verts.append(edgeToDualPoint_[edgeI]);
00432     }
00433     if (faceToDualPoint_[ie.faceLabel()] != -1)
00434     {
00435         doneEFaces[findIndex(eFaces, ie.faceLabel())] = true;
00436         verts.append(faceToDualPoint_[ie.faceLabel()]);
00437     }
00438     if (cellToDualPoint_[ie.cellLabel()] != -1)
00439     {
00440         verts.append(cellToDualPoint_[ie.cellLabel()]);
00441     }
00442 
00443     label currentDualCell0 = findDualCell(ie.cellLabel(), e[0]);
00444     label currentDualCell1 = findDualCell(ie.cellLabel(), e[1]);
00445 
00446     ++ie;
00447 
00448     while (true)
00449     {
00450         label faceI = ie.faceLabel();
00451 
00452         // Mark face as visited.
00453         doneEFaces[findIndex(eFaces, faceI)] = true;
00454 
00455         if (faceToDualPoint_[faceI] != -1)
00456         {
00457             verts.append(faceToDualPoint_[faceI]);
00458         }
00459 
00460         label cellI = ie.cellLabel();
00461 
00462         if (cellI == -1)
00463         {
00464             // At ending boundary face. We've stored the face point above
00465             // so this is the whole face.
00466             break;
00467         }
00468 
00469 
00470         label dualCell0 = findDualCell(cellI, e[0]);
00471         label dualCell1 = findDualCell(cellI, e[1]);
00472 
00473         // Generate face. (always if splitFace=true; only if needed to
00474         // separate cells otherwise)
00475         if
00476         (
00477             splitFace
00478          || (
00479                 dualCell0 != currentDualCell0
00480              || dualCell1 != currentDualCell1
00481             )
00482         )
00483         {
00484             // Close current face.
00485             addInternalFace
00486             (
00487                 -1,         // masterPointI
00488                 edgeI,      // masterEdgeI
00489                 -1,         // masterFaceI
00490                 edgeOrder,
00491                 currentDualCell0,
00492                 currentDualCell1,
00493                 verts.shrink(),
00494                 meshMod
00495             );
00496 
00497             // Restart
00498             currentDualCell0 = dualCell0;
00499             currentDualCell1 = dualCell1;
00500 
00501             verts.clear();
00502             if (edgeToDualPoint_[edgeI] != -1)
00503             {
00504                 verts.append(edgeToDualPoint_[edgeI]);
00505             }
00506             if (faceToDualPoint_[faceI] != -1)
00507             {
00508                 verts.append(faceToDualPoint_[faceI]);
00509             }
00510         }
00511 
00512         if (cellToDualPoint_[cellI] != -1)
00513         {
00514             verts.append(cellToDualPoint_[cellI]);
00515         }
00516 
00517         ++ie;
00518 
00519         if (ie == ie.end())
00520         {
00521             // Back at start face (for internal edge only). See if this needs
00522             // adding.
00523             if (isBoundaryEdge.get(edgeI) == 0)
00524             {
00525                 label startDual = faceToDualPoint_[startFaceLabel];
00526 
00527                 if (startDual != -1 && findIndex(verts, startDual) == -1)
00528                 {
00529                     verts.append(startDual);
00530                 }
00531             }
00532             break;
00533         }
00534     }
00535 
00536     verts.shrink();
00537     addInternalFace
00538     (
00539         -1,         // masterPointI
00540         edgeI,      // masterEdgeI
00541         -1,         // masterFaceI
00542         edgeOrder,
00543         currentDualCell0,
00544         currentDualCell1,
00545         verts,
00546         meshMod
00547     );
00548 }
00549 
00550 
00551 // Walks around circumference of faceI. Creates single face. Gets given
00552 // starting (feature) edge to start from. Returns ending edge. (all edges
00553 // in form of index in faceEdges)
00554 void Foam::meshDualiser::createFaceFromInternalFace
00555 (
00556     const label faceI,
00557     label& fp,
00558     polyTopoChange& meshMod
00559 ) const
00560 {
00561     const face& f = mesh_.faces()[faceI];
00562     const labelList& fEdges = mesh_.faceEdges()[faceI];
00563     label own = mesh_.faceOwner()[faceI];
00564     label nei = mesh_.faceNeighbour()[faceI];
00565 
00566     //Pout<< "createFaceFromInternalFace : At face:" << faceI
00567     //    << " verts:" << f
00568     //    << " points:" << UIndirectList<point>(mesh_.points(), f)()
00569     //    << " started walking at edge:" << fEdges[fp]
00570     //    << " verts:" << mesh_.edges()[fEdges[fp]]
00571     //    << endl;
00572 
00573 
00574     // Walk and collect face.
00575     DynamicList<label> verts(100);
00576 
00577     verts.append(faceToDualPoint_[faceI]);
00578     verts.append(edgeToDualPoint_[fEdges[fp]]);
00579 
00580     // Step to vertex after edge mid
00581     fp = f.fcIndex(fp);
00582 
00583     // Get cells on either side of face at that point
00584     label currentDualCell0 = findDualCell(own, f[fp]);
00585     label currentDualCell1 = findDualCell(nei, f[fp]);
00586 
00587     forAll(f, i)
00588     {
00589         // Check vertex
00590         if (pointToDualPoint_[f[fp]] != -1)
00591         {
00592             verts.append(pointToDualPoint_[f[fp]]);
00593         }
00594 
00595         // Edge between fp and fp+1
00596         label edgeI = fEdges[fp];
00597 
00598         if (edgeToDualPoint_[edgeI] != -1)
00599         {
00600             verts.append(edgeToDualPoint_[edgeI]);
00601         }
00602 
00603         // Next vertex on edge
00604         label nextFp = f.fcIndex(fp);
00605 
00606         // Get dual cells on nextFp to check whether face needs closing.
00607         label dualCell0 = findDualCell(own, f[nextFp]);
00608         label dualCell1 = findDualCell(nei, f[nextFp]);
00609 
00610         if (dualCell0 != currentDualCell0 || dualCell1 != currentDualCell1)
00611         {
00612             // Check: make sure that there is a midpoint on the edge.
00613             if (edgeToDualPoint_[edgeI] == -1)
00614             {
00615                 FatalErrorIn("createFacesFromInternalFace(..)")
00616                     << "face:" << faceI << " verts:" << f
00617                     << " points:" << UIndirectList<point>(mesh_.points(), f)()
00618                     << " no feature edge between " << f[fp]
00619                     << " and " << f[nextFp] << " although have different"
00620                     << " dual cells." << endl
00621                     << "point " << f[fp] << " has dual cells "
00622                     << currentDualCell0 << " and " << currentDualCell1
00623                     << " ; point "<< f[nextFp] << " has dual cells "
00624                     << dualCell0 << " and " << dualCell1
00625                     << abort(FatalError);
00626             }
00627 
00628 
00629             // Close current face.
00630             verts.shrink();
00631             addInternalFace
00632             (
00633                 -1,         // masterPointI
00634                 -1,         // masterEdgeI
00635                 faceI,      // masterFaceI
00636                 true,       // edgeOrder,
00637                 currentDualCell0,
00638                 currentDualCell1,
00639                 verts,
00640                 meshMod
00641             );
00642             break;
00643         }
00644 
00645         fp = nextFp;
00646     }
00647 }
00648 
00649 
00650 // Given a point on a face converts the faces around the point.
00651 // (pointFaces()). Gets starting face and marks off visited faces in donePFaces.
00652 void Foam::meshDualiser::createFacesAroundBoundaryPoint
00653 (
00654     const label patchI,
00655     const label patchPointI,
00656     const label startFaceI,
00657     polyTopoChange& meshMod,
00658     boolList& donePFaces            // pFaces visited
00659 ) const
00660 {
00661     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00662     const polyPatch& pp = patches[patchI];
00663     const labelList& pFaces = pp.pointFaces()[patchPointI];
00664     const labelList& own = mesh_.faceOwner();
00665 
00666     label pointI = pp.meshPoints()[patchPointI];
00667 
00668     if (pointToDualPoint_[pointI] == -1)
00669     {
00670         // Not a feature point. Loop over all connected
00671         // pointFaces.
00672 
00673         // Starting face
00674         label faceI = startFaceI;
00675 
00676         DynamicList<label> verts(4);
00677 
00678         while (true)
00679         {
00680             label index = findIndex(pFaces, faceI-pp.start());
00681 
00682             // Has face been visited already?
00683             if (donePFaces[index])
00684             {
00685                 break;
00686             }
00687             donePFaces[index] = true;
00688 
00689             // Insert face centre
00690             verts.append(faceToDualPoint_[faceI]);
00691 
00692             label dualCellI = findDualCell(own[faceI], pointI);
00693 
00694             // Get the edge before the patchPointI
00695             const face& f = mesh_.faces()[faceI];
00696             label fp = findIndex(f, pointI);
00697             label prevFp = f.rcIndex(fp);
00698             label edgeI = mesh_.faceEdges()[faceI][prevFp];
00699 
00700             if (edgeToDualPoint_[edgeI] != -1)
00701             {
00702                 verts.append(edgeToDualPoint_[edgeI]);
00703             }
00704 
00705             // Get next boundary face (whilst staying on edge).
00706             edgeFaceCirculator circ
00707             (
00708                 mesh_,
00709                 faceI,
00710                 true,   // ownerSide
00711                 prevFp, // index of edge in face
00712                 true    // isBoundaryEdge
00713             );
00714 
00715             do
00716             {
00717                 ++circ;
00718             }
00719             while (mesh_.isInternalFace(circ.faceLabel()));
00720 
00721             // Step to next face
00722             faceI = circ.faceLabel();
00723 
00724             if (faceI < pp.start() || faceI >= pp.start()+pp.size())
00725             {
00726                 FatalErrorIn
00727                 (
00728                     "meshDualiser::createFacesAroundBoundaryPoint(..)"
00729                 )   << "Walked from face on patch:" << patchI
00730                     << " to face:" << faceI
00731                     << " fc:" << mesh_.faceCentres()[faceI]
00732                     << " on patch:" << patches.whichPatch(faceI)
00733                     << abort(FatalError);
00734             }
00735 
00736             // Check if different cell.
00737             if (dualCellI != findDualCell(own[faceI], pointI))
00738             {
00739                 FatalErrorIn
00740                 (
00741                     "meshDualiser::createFacesAroundBoundaryPoint(..)"
00742                 )   << "Different dual cells but no feature edge"
00743                     << " inbetween point:" << pointI
00744                     << " coord:" << mesh_.points()[pointI]
00745                     << abort(FatalError);
00746             }
00747         }
00748 
00749         verts.shrink();
00750 
00751         label dualCellI = findDualCell(own[faceI], pointI);
00752 
00753         //Bit dodgy: create dualface from the last face (instead of from
00754         // the central point). This will also use the original faceZone to
00755         // put the new face (which might span multiple original faces) in.
00756 
00757         addBoundaryFace
00758         (
00759             //pointI,     // masterPointI
00760             -1,         // masterPointI
00761             -1,         // masterEdgeI
00762             faceI,      // masterFaceI
00763             dualCellI,
00764             patchI,
00765             verts,
00766             meshMod
00767         );
00768     }
00769     else
00770     {
00771         label faceI = startFaceI;
00772 
00773         // Storage for face
00774         DynamicList<label> verts(mesh_.faces()[faceI].size());
00775 
00776         // Starting point.
00777         verts.append(pointToDualPoint_[pointI]);
00778 
00779         // Find edge between pointI and next point on face.
00780         const labelList& fEdges = mesh_.faceEdges()[faceI];
00781         label nextEdgeI = fEdges[findIndex(mesh_.faces()[faceI], pointI)];
00782         if (edgeToDualPoint_[nextEdgeI] != -1)
00783         {
00784             verts.append(edgeToDualPoint_[nextEdgeI]);
00785         }
00786 
00787         do
00788         {
00789             label index = findIndex(pFaces, faceI-pp.start());
00790 
00791             // Has face been visited already?
00792             if (donePFaces[index])
00793             {
00794                 break;
00795             }
00796             donePFaces[index] = true;
00797 
00798             // Face centre
00799             verts.append(faceToDualPoint_[faceI]);
00800 
00801             // Find edge before pointI on faceI
00802             const labelList& fEdges = mesh_.faceEdges()[faceI];
00803             const face& f = mesh_.faces()[faceI];
00804             label prevFp = f.rcIndex(findIndex(f, pointI));
00805             label edgeI = fEdges[prevFp];
00806 
00807             if (edgeToDualPoint_[edgeI] != -1)
00808             {
00809                 // Feature edge. Close any face so far. Note: uses face to
00810                 // create dualFace from. Could use pointI instead.
00811                 verts.append(edgeToDualPoint_[edgeI]);
00812                 addBoundaryFace
00813                 (
00814                     -1,     // masterPointI
00815                     -1,     // masterEdgeI
00816                     faceI,  // masterFaceI
00817                     findDualCell(own[faceI], pointI),
00818                     patchI,
00819                     verts.shrink(),
00820                     meshMod
00821                 );
00822                 verts.clear();
00823 
00824                 verts.append(pointToDualPoint_[pointI]);
00825                 verts.append(edgeToDualPoint_[edgeI]);
00826             }
00827 
00828             // Cross edgeI to next boundary face
00829             edgeFaceCirculator circ
00830             (
00831                 mesh_,
00832                 faceI,
00833                 true,   // ownerSide
00834                 prevFp, // index of edge in face
00835                 true    // isBoundaryEdge
00836             );
00837 
00838             do
00839             {
00840                 ++circ;
00841             }
00842             while (mesh_.isInternalFace(circ.faceLabel()));
00843 
00844             // Step to next face. Quit if not on same patch.
00845             faceI = circ.faceLabel();
00846         }
00847         while
00848         (
00849             faceI != startFaceI
00850          && faceI >= pp.start()
00851          && faceI < pp.start()+pp.size()
00852         );
00853 
00854         if (verts.size() > 2)
00855         {
00856             // Note: face created from face, not from pointI
00857             addBoundaryFace
00858             (
00859                 -1,             // masterPointI
00860                 -1,             // masterEdgeI
00861                 startFaceI,     // masterFaceI
00862                 findDualCell(own[faceI], pointI),
00863                 patchI,
00864                 verts.shrink(),
00865                 meshMod
00866             );
00867         }
00868     }
00869 }
00870 
00871 
00872 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00873 
00874 Foam::meshDualiser::meshDualiser(const polyMesh& mesh)
00875 :
00876     mesh_(mesh),
00877     pointToDualCells_(mesh_.nPoints()),
00878     pointToDualPoint_(mesh_.nPoints(), -1),
00879     cellToDualPoint_(mesh_.nCells()),
00880     faceToDualPoint_(mesh_.nFaces(), -1),
00881     edgeToDualPoint_(mesh_.nEdges(), -1)
00882 {}
00883 
00884 
00885 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00886 
00887 
00888 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00889 
00890 void Foam::meshDualiser::setRefinement
00891 (
00892     const bool splitFace,
00893     const labelList& featureFaces,
00894     const labelList& featureEdges,
00895     const labelList& singleCellFeaturePoints,
00896     const labelList& multiCellFeaturePoints,
00897     polyTopoChange& meshMod
00898 )
00899 {
00900     const labelList& own = mesh_.faceOwner();
00901     const labelList& nei = mesh_.faceNeighbour();
00902     const vectorField& cellCentres = mesh_.cellCentres();
00903 
00904     // Mark boundary edges and points.
00905     // (Note: in 1.4.2 we can use the built-in mesh point ordering
00906     //  facility instead)
00907     PackedBoolList isBoundaryEdge(mesh_.nEdges());
00908     for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00909     {
00910         const labelList& fEdges = mesh_.faceEdges()[faceI];
00911 
00912         forAll(fEdges, i)
00913         {
00914             isBoundaryEdge.set(fEdges[i], 1);
00915         }
00916     }
00917 
00918 
00919     if (splitFace)
00920     {
00921         // This is a special mode where whenever we are walking around an edge
00922         // every area through a cell becomes a separate dualface. So two
00923         // dual cells will probably have more than one dualface between them!
00924         // This mode implies that
00925         // - all faces have to be feature faces since there has to be a
00926         //   dualpoint at the face centre.
00927         // - all edges have to be feature edges ,,
00928         boolList featureFaceSet(mesh_.nFaces(), false);
00929         forAll(featureFaces, i)
00930         {
00931             featureFaceSet[featureFaces[i]] = true;
00932         }
00933         label faceI = findIndex(featureFaceSet, false);
00934 
00935         if (faceI != -1)
00936         {
00937             FatalErrorIn
00938             (
00939                 "meshDualiser::setRefinement"
00940                 "(const labelList&, const labelList&, const labelList&"
00941                 ", const labelList, polyTopoChange&)"
00942             )   << "In split-face-mode (splitFace=true) but not all faces"
00943                 << " marked as feature faces." << endl
00944                 << "First conflicting face:" << faceI
00945                 << " centre:" << mesh_.faceCentres()[faceI]
00946                 << abort(FatalError);
00947         }
00948 
00949         boolList featureEdgeSet(mesh_.nEdges(), false);
00950         forAll(featureEdges, i)
00951         {
00952             featureEdgeSet[featureEdges[i]] = true;
00953         }
00954         label edgeI = findIndex(featureEdgeSet, false);
00955 
00956         if (edgeI != -1)
00957         {
00958             const edge& e = mesh_.edges()[edgeI];
00959             FatalErrorIn
00960             (
00961                 "meshDualiser::setRefinement"
00962                 "(const labelList&, const labelList&, const labelList&"
00963                 ", const labelList, polyTopoChange&)"
00964             )   << "In split-face-mode (splitFace=true) but not all edges"
00965                 << " marked as feature edges." << endl
00966                 << "First conflicting edge:" << edgeI
00967                 << " verts:" << e
00968                 << " coords:" << mesh_.points()[e[0]] << mesh_.points()[e[1]]
00969                 << abort(FatalError);
00970         }
00971     }
00972     else
00973     {
00974         // Check that all boundary faces are feature faces.
00975 
00976         boolList featureFaceSet(mesh_.nFaces(), false);
00977         forAll(featureFaces, i)
00978         {
00979             featureFaceSet[featureFaces[i]] = true;
00980         }
00981         for
00982         (
00983             label faceI = mesh_.nInternalFaces();
00984             faceI < mesh_.nFaces();
00985             faceI++
00986         )
00987         {
00988             if (!featureFaceSet[faceI])
00989             {
00990                 FatalErrorIn
00991                 (
00992                     "meshDualiser::setRefinement"
00993                     "(const labelList&, const labelList&, const labelList&"
00994                     ", const labelList, polyTopoChange&)"
00995                 )   << "Not all boundary faces marked as feature faces."
00996                     << endl
00997                     << "First conflicting face:" << faceI
00998                     << " centre:" << mesh_.faceCentres()[faceI]
00999                     << abort(FatalError);
01000             }
01001         }
01002     }
01003 
01004 
01005 
01006 
01007     // Start creating cells, points, and faces (in that order)
01008 
01009 
01010     // 1. Mark which cells to create
01011     // Mostly every point becomes one cell but sometimes (for feature points)
01012     // all cells surrounding a feature point become cells. Also a non-manifold
01013     // point can create two cells! So a dual cell is uniquely defined by a
01014     // mesh point + cell (as in pointCells index)
01015 
01016     // 2. Mark which face centres to create
01017 
01018     // 3. Internal faces can now consist of
01019     //      - only cell centres of walk around edge
01020     //      - cell centres + face centres of walk around edge
01021     //      - same but now other side is not a single cell
01022 
01023     // 4. Boundary faces (or internal faces between cell zones!) now consist of
01024     //      - walk around boundary point.
01025 
01026 
01027 
01028     autoPtr<OFstream> dualCcStr;
01029     if (debug)
01030     {
01031         dualCcStr.reset(new OFstream("dualCc.obj"));
01032         Pout<< "Dumping centres of dual cells to " << dualCcStr().name()
01033             << endl;
01034     }
01035 
01036 
01037     // Dual cells (from points)
01038     // ~~~~~~~~~~~~~~~~~~~~~~~~
01039 
01040     // pointToDualCells_[pointI]
01041     // - single entry : all cells surrounding point all become the same
01042     //                  cell.
01043     // - multiple entries: in order of pointCells.
01044 
01045 
01046     // feature points that become single cell
01047     forAll(singleCellFeaturePoints, i)
01048     {
01049         label pointI = singleCellFeaturePoints[i];
01050 
01051         pointToDualPoint_[pointI] = meshMod.addPoint
01052         (
01053             mesh_.points()[pointI],
01054             pointI,                                 // masterPoint
01055             mesh_.pointZones().whichZone(pointI),   // zoneID
01056             true                                    // inCell
01057         );
01058 
01059         // Generate single cell
01060         pointToDualCells_[pointI].setSize(1);
01061         pointToDualCells_[pointI][0] = meshMod.addCell
01062         (
01063             pointI, //masterPointID,
01064             -1,     //masterEdgeID,
01065             -1,     //masterFaceID,
01066             -1,     //masterCellID,
01067             -1      //zoneID
01068         );
01069         if (dualCcStr.valid())
01070         {
01071             meshTools::writeOBJ(dualCcStr(), mesh_.points()[pointI]);
01072         }
01073     }
01074 
01075     // feature points that become multiple cells
01076     forAll(multiCellFeaturePoints, i)
01077     {
01078         label pointI = multiCellFeaturePoints[i];
01079 
01080         if (pointToDualCells_[pointI].size() > 0)
01081         {
01082             FatalErrorIn
01083             (
01084                 "meshDualiser::setRefinement"
01085                 "(const labelList&, const labelList&, const labelList&"
01086                 ", const labelList, polyTopoChange&)"
01087             )   << "Point " << pointI << " at:" << mesh_.points()[pointI]
01088                 << " is both in singleCellFeaturePoints"
01089                 << " and multiCellFeaturePoints."
01090                 << abort(FatalError);
01091         }
01092 
01093         pointToDualPoint_[pointI] = meshMod.addPoint
01094         (
01095             mesh_.points()[pointI],
01096             pointI,                                 // masterPoint
01097             mesh_.pointZones().whichZone(pointI),   // zoneID
01098             true                                    // inCell
01099         );
01100 
01101         // Create dualcell for every cell connected to dual point
01102 
01103         const labelList& pCells = mesh_.pointCells()[pointI];
01104 
01105         pointToDualCells_[pointI].setSize(pCells.size());
01106 
01107         forAll(pCells, pCellI)
01108         {
01109             pointToDualCells_[pointI][pCellI] = meshMod.addCell
01110             (
01111                 pointI,                                     //masterPointID
01112                 -1,                                         //masterEdgeID
01113                 -1,                                         //masterFaceID
01114                 -1,                                         //masterCellID
01115                 mesh_.cellZones().whichZone(pCells[pCellI]) //zoneID
01116             );
01117             if (dualCcStr.valid())
01118             {
01119                 meshTools::writeOBJ
01120                 (
01121                     dualCcStr(),
01122                     0.5*(mesh_.points()[pointI]+cellCentres[pCells[pCellI]])
01123                 );
01124             }
01125         }
01126     }
01127     // Normal points
01128     forAll(mesh_.points(), pointI)
01129     {
01130         if (pointToDualCells_[pointI].empty())
01131         {
01132             pointToDualCells_[pointI].setSize(1);
01133             pointToDualCells_[pointI][0] = meshMod.addCell
01134             (
01135                 pointI, //masterPointID,
01136                 -1,     //masterEdgeID,
01137                 -1,     //masterFaceID,
01138                 -1,     //masterCellID,
01139                 -1      //zoneID
01140             );
01141 
01142             if (dualCcStr.valid())
01143             {
01144                 meshTools::writeOBJ(dualCcStr(), mesh_.points()[pointI]);
01145             }
01146         }
01147     }
01148 
01149 
01150     // Dual points (from cell centres, feature faces, feature edges)
01151     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01152 
01153     forAll(cellToDualPoint_, cellI)
01154     {
01155         cellToDualPoint_[cellI] = meshMod.addPoint
01156         (
01157             cellCentres[cellI],
01158             mesh_.faces()[mesh_.cells()[cellI][0]][0],     // masterPoint
01159             -1,     // zoneID
01160             true    // inCell
01161         );
01162     }
01163 
01164     // From face to dual point
01165 
01166     forAll(featureFaces, i)
01167     {
01168         label faceI = featureFaces[i];
01169 
01170         faceToDualPoint_[faceI] = meshMod.addPoint
01171         (
01172             mesh_.faceCentres()[faceI],
01173             mesh_.faces()[faceI][0],     // masterPoint
01174             -1,     // zoneID
01175             true    // inCell
01176         );
01177     }
01178     // Detect whether different dual cells on either side of a face. This
01179     // would neccesitate having a dual face built from the face and thus a
01180     // dual point at the face centre.
01181     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
01182     {
01183         if (faceToDualPoint_[faceI] == -1)
01184         {
01185             const face& f = mesh_.faces()[faceI];
01186 
01187             forAll(f, fp)
01188             {
01189                 label ownDualCell = findDualCell(own[faceI], f[fp]);
01190                 label neiDualCell = findDualCell(nei[faceI], f[fp]);
01191 
01192                 if (ownDualCell != neiDualCell)
01193                 {
01194                     faceToDualPoint_[faceI] = meshMod.addPoint
01195                     (
01196                         mesh_.faceCentres()[faceI],
01197                         f[fp],  // masterPoint
01198                         -1,     // zoneID
01199                         true    // inCell
01200                     );
01201 
01202                     break;
01203                 }
01204             }
01205         }
01206     }
01207 
01208     // From edge to dual point
01209 
01210     forAll(featureEdges, i)
01211     {
01212         label edgeI = featureEdges[i];
01213 
01214         const edge& e = mesh_.edges()[edgeI];
01215 
01216         edgeToDualPoint_[edgeI] = meshMod.addPoint
01217         (
01218             e.centre(mesh_.points()),
01219             e[0],   // masterPoint
01220             -1,     // zoneID
01221             true    // inCell
01222         );
01223     }
01224 
01225     // Detect whether different dual cells on either side of an edge. This
01226     // would neccesitate having a dual face built perpendicular to the edge
01227     // and thus a dual point at the mid of the edge.
01228     // Note: not really true - the face can be built without the edge centre!
01229     const labelListList& edgeCells = mesh_.edgeCells();
01230 
01231     forAll(edgeCells, edgeI)
01232     {
01233        if (edgeToDualPoint_[edgeI] == -1)
01234        {
01235             const edge& e = mesh_.edges()[edgeI];
01236 
01237             // We need a point on the edge if not all cells on both sides
01238             // are the same.
01239 
01240             const labelList& eCells = mesh_.edgeCells()[edgeI];
01241 
01242             label dualE0 = findDualCell(eCells[0], e[0]);
01243             label dualE1 = findDualCell(eCells[0], e[1]);
01244 
01245             for (label i = 1; i < eCells.size(); i++)
01246             {
01247                 label newDualE0 = findDualCell(eCells[i], e[0]);
01248 
01249                 if (dualE0 != newDualE0)
01250                 {
01251                     edgeToDualPoint_[edgeI] = meshMod.addPoint
01252                     (
01253                         e.centre(mesh_.points()),
01254                         e[0],                               // masterPoint
01255                         mesh_.pointZones().whichZone(e[0]), // zoneID
01256                         true                                // inCell
01257                     );
01258 
01259                     break;
01260                 }
01261 
01262                 label newDualE1 = findDualCell(eCells[i], e[1]);
01263 
01264                 if (dualE1 != newDualE1)
01265                 {
01266                     edgeToDualPoint_[edgeI] = meshMod.addPoint
01267                     (
01268                         e.centre(mesh_.points()),
01269                         e[1],   // masterPoint
01270                         mesh_.pointZones().whichZone(e[1]), // zoneID
01271                         true    // inCell
01272                     );
01273 
01274                     break;
01275                 }
01276             }
01277         }
01278     }
01279 
01280     // Make sure all boundary edges emanating from feature points are
01281     // feature edges as well.
01282     forAll(singleCellFeaturePoints, i)
01283     {
01284         generateDualBoundaryEdges
01285         (
01286             isBoundaryEdge,
01287             singleCellFeaturePoints[i],
01288             meshMod
01289         );
01290     }
01291     forAll(multiCellFeaturePoints, i)
01292     {
01293         generateDualBoundaryEdges
01294         (
01295             isBoundaryEdge,
01296             multiCellFeaturePoints[i],
01297             meshMod
01298         );
01299     }
01300 
01301 
01302     // Check for duplicate points
01303     if (debug)
01304     {
01305         dumpPolyTopoChange(meshMod, "generatedPoints_");
01306         checkPolyTopoChange(meshMod);
01307     }
01308 
01309 
01310     // Now we have all points and cells
01311     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01312     //  - pointToDualCells_ : per point a single dualCell or multiple dualCells
01313     //  - pointToDualPoint_ : per point -1 or the dual point at the coordinate
01314     //  - edgeToDualPoint_  : per edge -1 or the edge centre
01315     //  - faceToDualPoint_  : per face -1 or the face centre
01316     //  - cellToDualPoint_  : per cell the cell centre
01317     // Now we have to walk all edges and construct faces. Either single face
01318     // per edge or multiple (-if nonmanifold edge -if different dualcells)
01319 
01320     const edgeList& edges = mesh_.edges();
01321 
01322     forAll(edges, edgeI)
01323     {
01324         const labelList& eFaces = mesh_.edgeFaces()[edgeI];
01325 
01326         boolList doneEFaces(eFaces.size(), false);
01327 
01328         forAll(eFaces, i)
01329         {
01330             if (!doneEFaces[i])
01331             {
01332                 // We found a face that hasn't yet been visited. This might
01333                 // happen for non-manifold edges where a single edge can
01334                 // become multiple faces.
01335 
01336                 label startFaceI = eFaces[i];
01337 
01338                 //Pout<< "Walking edge:" << edgeI
01339                 //    << " points:" << mesh_.points()[e[0]]
01340                 //    << mesh_.points()[e[1]]
01341                 //    << " startFace:" << startFaceI
01342                 //    << " at:" << mesh_.faceCentres()[startFaceI]
01343                 //    << endl;
01344 
01345                 createFacesAroundEdge
01346                 (
01347                     splitFace,
01348                     isBoundaryEdge,
01349                     edgeI,
01350                     startFaceI,
01351                     meshMod,
01352                     doneEFaces
01353                 );
01354             }
01355         }
01356     }
01357 
01358     if (debug)
01359     {
01360         dumpPolyTopoChange(meshMod, "generatedFacesFromEdges_");
01361     }
01362 
01363     // Create faces from feature faces. These can be internal or external faces.
01364     // - feature face : centre needs to be included.
01365     //      - single cells on either side: triangulate
01366     //      - multiple cells: create single face between unique cell pair. Only
01367     //                        create face where cells differ on either side.
01368     // - non-feature face : inbetween cell zones.
01369     forAll(faceToDualPoint_, faceI)
01370     {
01371         if (faceToDualPoint_[faceI] != -1 && mesh_.isInternalFace(faceI))
01372         {
01373             const face& f = mesh_.faces()[faceI];
01374             const labelList& fEdges = mesh_.faceEdges()[faceI];
01375 
01376             // Starting edge
01377             label fp = 0;
01378 
01379             do
01380             {
01381                 // Find edge that is in dual mesh and where the
01382                 // next point (fp+1) has different dual cells on either side.
01383                 bool foundStart = false;
01384 
01385                 do
01386                 {
01387                     if
01388                     (
01389                         edgeToDualPoint_[fEdges[fp]] != -1
01390                     && !sameDualCell(faceI, f.nextLabel(fp))
01391                     )
01392                     {
01393                         foundStart = true;
01394                         break;
01395                     }
01396                     fp = f.fcIndex(fp);
01397                 }
01398                 while (fp != 0);
01399 
01400                 if (!foundStart)
01401                 {
01402                     break;
01403                 }
01404 
01405                 // Walk from edge fp and generate a face.
01406                 createFaceFromInternalFace
01407                 (
01408                     faceI,
01409                     fp,
01410                     meshMod
01411                 );
01412             }
01413             while(fp != 0);
01414         }
01415     }
01416 
01417     if (debug)
01418     {
01419         dumpPolyTopoChange(meshMod, "generatedFacesFromFeatFaces_");
01420     }
01421 
01422 
01423     // Create boundary faces. Every boundary point has one or more dualcells.
01424     // These need to be closed.
01425     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
01426 
01427     forAll(patches, patchI)
01428     {
01429         const polyPatch& pp = patches[patchI];
01430 
01431         const labelListList& pointFaces = pp.pointFaces();
01432 
01433         forAll(pointFaces, patchPointI)
01434         {
01435             const labelList& pFaces = pointFaces[patchPointI];
01436 
01437             boolList donePFaces(pFaces.size(), false);
01438 
01439             forAll(pFaces, i)
01440             {
01441                 if (!donePFaces[i])
01442                 {
01443                     // Starting face
01444                     label startFaceI = pp.start()+pFaces[i];
01445 
01446                     //Pout<< "Walking around point:" << pointI
01447                     //    << " coord:" << mesh_.points()[pointI]
01448                     //    << " on patch:" << patchI
01449                     //    << " startFace:" << startFaceI
01450                     //    << " at:" << mesh_.faceCentres()[startFaceI]
01451                     //    << endl;
01452 
01453                     createFacesAroundBoundaryPoint
01454                     (
01455                         patchI,
01456                         patchPointI,
01457                         startFaceI,
01458                         meshMod,
01459                         donePFaces            // pFaces visited
01460                     );
01461                 }
01462             }
01463         }
01464     }
01465 
01466     if (debug)
01467     {
01468         dumpPolyTopoChange(meshMod, "generatedFacesFromBndFaces_");
01469     }
01470 }
01471 
01472 
01473 
01474 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
01475 
01476 
01477 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
01478 
01479 
01480 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
01481 
01482 
01483 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd