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

edgeCollapser.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 "edgeCollapser.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChange.H"
00030 #include "ListOps.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 Foam::label Foam::edgeCollapser::findIndex
00035 (
00036     const labelList& elems,
00037     const label start,
00038     const label size,
00039     const label val
00040 )
00041 {
00042     for (label i = start; i < size; i++)
00043     {
00044         if (elems[i] == val)
00045         {
00046             return i;
00047         }
00048     }
00049     return -1;
00050 }
00051 
00052 
00053 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00054 
00055 // Changes region of connected set of points
00056 Foam::label Foam::edgeCollapser::changePointRegion
00057 (
00058     const label pointI,
00059     const label oldRegion,
00060     const label newRegion
00061 )
00062 {
00063     label nChanged = 0;
00064 
00065     if (pointRegion_[pointI] == oldRegion)
00066     {
00067         pointRegion_[pointI] = newRegion;
00068         nChanged++;
00069 
00070         // Step to neighbouring point across edges with same region number
00071 
00072         const labelList& pEdges = mesh_.pointEdges()[pointI];
00073 
00074         forAll(pEdges, i)
00075         {
00076             label otherPointI = mesh_.edges()[pEdges[i]].otherVertex(pointI);
00077 
00078             nChanged += changePointRegion(otherPointI, oldRegion, newRegion);
00079         }
00080     }
00081     return nChanged;
00082 }
00083 
00084 
00085 bool Foam::edgeCollapser::pointRemoved(const label pointI) const
00086 {
00087     label region = pointRegion_[pointI];
00088 
00089     if (region == -1 || pointRegionMaster_[region] == pointI)
00090     {
00091         return false;
00092     }
00093     else
00094     {
00095         return true;
00096     }
00097 }
00098 
00099 
00100 void Foam::edgeCollapser::filterFace(const label faceI, face& f) const
00101 {
00102     label newFp = 0;
00103 
00104     forAll(f, fp)
00105     {
00106         label pointI = f[fp];
00107 
00108         label region = pointRegion_[pointI];
00109 
00110         if (region == -1)
00111         {
00112             f[newFp++] = pointI;
00113         }
00114         else
00115         {
00116             label master = pointRegionMaster_[region];
00117 
00118             if (findIndex(f, 0, newFp, master) == -1)
00119             {
00120                 f[newFp++] = master;
00121             }
00122         }
00123     }
00124 
00125     // Check for pinched face. Tries to correct
00126     // - consecutive duplicate vertex. Removes duplicate vertex.
00127     // - duplicate vertex with one other vertex in between (spike).
00128     // Both of these should not really occur! and should be checked before
00129     // collapsing edges.
00130 
00131     const label size = newFp;
00132 
00133     newFp = 2;
00134 
00135     for (label fp = 2; fp < size; fp++)
00136     {
00137         label fp1 = fp-1;
00138         label fp2 = fp-2;
00139 
00140         label pointI = f[fp];
00141 
00142         // Search for previous occurrence.
00143         label index = findIndex(f, 0, fp, pointI);
00144 
00145         if (index == fp1)
00146         {
00147             WarningIn
00148             (
00149                 "Foam::edgeCollapser::filterFace(const label faceI, "
00150                 "face& f) const"
00151             )   << "Removing consecutive duplicate vertex in face "
00152                 << f << endl;
00153             // Don't store current pointI
00154         }
00155         else if (index == fp2)
00156         {
00157             WarningIn
00158             (
00159                 "Foam::edgeCollapser::filterFace(const label faceI, "
00160                 "face& f) const"
00161             )   << "Removing non-consecutive duplicate vertex in face "
00162                 << f << endl;
00163             // Don't store current pointI and remove previous
00164             newFp--;
00165         }
00166         else if (index != -1)
00167         {
00168             WarningIn
00169             (
00170                 "Foam::edgeCollapser::filterFace(const label faceI, "
00171                 "face& f) const"
00172             )   << "Pinched face " << f << endl;
00173             f[newFp++] = pointI;
00174         }
00175         else
00176         {
00177             f[newFp++] = pointI;
00178         }
00179     }
00180 
00181     f.setSize(newFp);
00182 }
00183 
00184 
00185 // Debugging.
00186 void Foam::edgeCollapser::printRegions() const
00187 {
00188     forAll(pointRegionMaster_, regionI)
00189     {
00190         label master = pointRegionMaster_[regionI];
00191 
00192         if (master != -1)
00193         {
00194             Info<< "Region:" << regionI << nl
00195                 << "    master:" << master
00196                 << ' ' << mesh_.points()[master] << nl;
00197 
00198             forAll(pointRegion_, pointI)
00199             {
00200                 if (pointRegion_[pointI] == regionI && pointI != master)
00201                 {
00202                     Info<< "    slave:" << pointI
00203                         << ' ' <<  mesh_.points()[pointI] << nl;
00204                 }
00205             }
00206         }
00207     }
00208 }
00209 
00210 
00211 
00212 // Collapse list of edges
00213 void Foam::edgeCollapser::collapseEdges(const labelList& edgeLabels)
00214 {
00215     const edgeList& edges = mesh_.edges();
00216 
00217     forAll(edgeLabels, i)
00218     {
00219         label edgeI = edgeLabels[i];
00220         const edge& e = edges[edgeI];
00221 
00222         label region0 = pointRegion_[e[0]];
00223         label region1 = pointRegion_[e[1]];
00224 
00225         if (region0 == -1)
00226         {
00227             if (region1 == -1)
00228             {
00229                 // Both unaffected. Choose ad lib.
00230                 collapseEdge(edgeI, e[0]);
00231             }
00232             else
00233             {
00234                 // Collapse to whatever e[1] collapses
00235                 collapseEdge(edgeI, e[1]);
00236             }
00237         }
00238         else
00239         {
00240             if (region1 == -1)
00241             {
00242                 // Collapse to whatever e[0] collapses
00243                 collapseEdge(edgeI, e[0]);
00244             }
00245             else
00246             {
00247                 // Both collapsed.
00248                 if (pointRegionMaster_[region0] == e[0])
00249                 {
00250                     // e[0] is a master
00251                     collapseEdge(edgeI, e[0]);
00252                 }
00253                 else if (pointRegionMaster_[region1] == e[1])
00254                 {
00255                     // e[1] is a master
00256                     collapseEdge(edgeI, e[1]);
00257                 }
00258                 else
00259                 {
00260                     // Dont know
00261                     collapseEdge(edgeI, e[0]);
00262                 }
00263             }
00264         }
00265     }
00266 }
00267 
00268 
00269 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00270 
00271 // Construct from mesh
00272 Foam::edgeCollapser::edgeCollapser(const polyMesh& mesh)
00273 :
00274     mesh_(mesh),
00275     pointRegion_(mesh.nPoints(), -1),
00276     pointRegionMaster_(mesh.nPoints() / 100),
00277     freeRegions_()
00278 {}
00279 
00280 
00281 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00282 
00283 bool Foam::edgeCollapser::unaffectedEdge(const label edgeI) const
00284 {
00285     const edge& e = mesh_.edges()[edgeI];
00286 
00287     return (pointRegion_[e[0]] == -1) && (pointRegion_[e[1]] == -1);
00288 }
00289 
00290 
00291 bool Foam::edgeCollapser::collapseEdge(const label edgeI, const label master)
00292 {
00293     const edge& e = mesh_.edges()[edgeI];
00294 
00295     label pointRegion0 = pointRegion_[e[0]];
00296     label pointRegion1 = pointRegion_[e[1]];
00297 
00298     if (pointRegion0 == -1)
00299     {
00300         if (pointRegion1 == -1)
00301         {
00302             // Both endpoints not collapsed. Create new region.
00303 
00304             label freeRegion = -1;
00305 
00306             if (freeRegions_.size())
00307             {
00308                 freeRegion = freeRegions_.removeHead();
00309 
00310                 if (pointRegionMaster_[freeRegion] != -1)
00311                 {
00312                     FatalErrorIn
00313                     ("edgeCollapser::collapseEdge(const label, const label)")
00314                         << "Problem : freeed region :" << freeRegion
00315                         << " has already master "
00316                         << pointRegionMaster_[freeRegion]
00317                         << abort(FatalError);
00318                 }
00319             }
00320             else
00321             {
00322                 // If no region found create one. This is the only place where
00323                 // new regions are created.
00324                 freeRegion = pointRegionMaster_.size();
00325             }
00326 
00327             pointRegion_[e[0]] = freeRegion;
00328             pointRegion_[e[1]] = freeRegion;
00329 
00330             pointRegionMaster_(freeRegion) = master;
00331         }
00332         else
00333         {
00334             // e[1] is part of collapse network, e[0] not. Add e0 to e1 region.
00335             pointRegion_[e[0]] = pointRegion1;
00336 
00337             pointRegionMaster_[pointRegion1] = master;
00338         }
00339     }
00340     else
00341     {
00342         if (pointRegion1 == -1)
00343         {
00344             // e[0] is part of collapse network. Add e1 to e0 region
00345             pointRegion_[e[1]] = pointRegion0;
00346 
00347             pointRegionMaster_[pointRegion0] = master;
00348         }
00349         else if (pointRegion0 != pointRegion1)
00350         {
00351             // Both part of collapse network. Merge the two regions.
00352 
00353             // Use the smaller region number for the whole network.
00354             label minRegion = min(pointRegion0, pointRegion1);
00355             label maxRegion = max(pointRegion0, pointRegion1);
00356     
00357             // Use minRegion as region for combined net, free maxRegion.
00358             pointRegionMaster_[minRegion] = master;
00359             pointRegionMaster_[maxRegion] = -1;
00360             freeRegions_.insert(maxRegion);
00361 
00362             if (minRegion != pointRegion0)
00363             {
00364                 changePointRegion(e[0], pointRegion0, minRegion);
00365             }
00366             if (minRegion != pointRegion1)
00367             {
00368                 changePointRegion(e[1], pointRegion1, minRegion);
00369             }
00370         }
00371     }
00372 
00373     return true;
00374 }
00375 
00376 
00377 bool Foam::edgeCollapser::setRefinement(polyTopoChange& meshMod)
00378 {
00379     const cellList& cells = mesh_.cells();
00380     const labelList& faceOwner = mesh_.faceOwner();
00381     const labelList& faceNeighbour = mesh_.faceNeighbour();
00382     const labelListList& pointFaces = mesh_.pointFaces();
00383     const labelListList& cellEdges = mesh_.cellEdges();
00384 
00385     // Print regions:
00386     //printRegions()
00387 
00388     bool meshChanged = false;
00389 
00390 
00391     // Current faces (is also collapseStatus: f.size() < 3)
00392     faceList newFaces(mesh_.faces());
00393 
00394     // Current cellCollapse status
00395     boolList cellRemoved(mesh_.nCells(), false);
00396 
00397 
00398     do
00399     {
00400         // Update face collapse from edge collapses
00401         forAll(newFaces, faceI)
00402         {
00403             filterFace(faceI, newFaces[faceI]);
00404         }
00405 
00406         // Check if faces to be collapsed cause cells to become collapsed.
00407         label nCellCollapsed = 0;
00408 
00409         forAll(cells, cellI)
00410         {
00411             if (!cellRemoved[cellI])
00412             {
00413                 const cell& cFaces = cells[cellI];
00414 
00415                 label nFaces = cFaces.size();
00416 
00417                 forAll(cFaces, i)
00418                 {
00419                     label faceI = cFaces[i];
00420 
00421                     if (newFaces[faceI].size() < 3)
00422                     {
00423                         --nFaces;
00424 
00425                         if (nFaces < 4)
00426                         {
00427                             Info<< "Cell:" << cellI
00428                                 << " uses faces:" << cFaces
00429                                 << " of which too many are marked for removal:"
00430                                 << endl
00431                                 << "   ";
00432                             forAll(cFaces, j)
00433                             {
00434                                 if (newFaces[cFaces[j]].size() < 3)
00435                                 {
00436                                     Info<< ' '<< cFaces[j];
00437                                 }
00438                             }
00439                             Info<< endl;
00440 
00441                             cellRemoved[cellI] = true;
00442 
00443                             // Collapse all edges of cell to nothing
00444                             collapseEdges(cellEdges[cellI]);
00445 
00446                             nCellCollapsed++;
00447 
00448                             break;
00449                         }
00450                     }
00451                 }
00452             }
00453         }
00454 
00455         if (nCellCollapsed == 0)
00456         {
00457             break;
00458         }
00459     } while(true);
00460 
00461 
00462     // Keep track of faces that have been done already.
00463     boolList doneFace(mesh_.nFaces(), false);
00464 
00465     {
00466         // Mark points used.
00467         boolList usedPoint(mesh_.nPoints(), false);
00468 
00469 
00470         forAll(cellRemoved, cellI)
00471         {
00472             if (cellRemoved[cellI])
00473             {
00474                 meshMod.removeCell(cellI, -1);
00475             }
00476         }
00477 
00478 
00479         // Remove faces
00480         forAll(newFaces, faceI)
00481         {
00482             const face& f = newFaces[faceI];
00483 
00484             if (f.size() < 3)
00485             {
00486                 meshMod.removeFace(faceI, -1);
00487                 meshChanged = true;
00488 
00489                 // Mark face as been done.
00490                 doneFace[faceI] = true;
00491             }
00492             else
00493             {
00494                 // Kept face. Mark vertices
00495                 forAll(f, fp)
00496                 {
00497                     usedPoint[f[fp]] = true;
00498                 }
00499             }
00500         }
00501 
00502         // Remove unused vertices that have not been marked for removal already
00503         forAll(usedPoint, pointI)
00504         {
00505             if (!usedPoint[pointI] && !pointRemoved(pointI))
00506             {
00507                 meshMod.removePoint(pointI, -1);
00508                 meshChanged = true;
00509             }
00510         }
00511     }
00512 
00513         
00514 
00515     // Remove points.
00516     forAll(pointRegion_, pointI)
00517     {
00518         if (pointRemoved(pointI))
00519         {
00520             meshMod.removePoint(pointI, -1);
00521             meshChanged = true;
00522         }
00523     }
00524 
00525 
00526 
00527     const polyBoundaryMesh& boundaryMesh = mesh_.boundaryMesh();
00528     const faceZoneMesh& faceZones = mesh_.faceZones();
00529       
00530 
00531     // Renumber faces that use points
00532     forAll(pointRegion_, pointI)
00533     {
00534         if (pointRemoved(pointI))
00535         {
00536             const labelList& changedFaces = pointFaces[pointI];
00537 
00538             forAll(changedFaces, changedFaceI)
00539             {
00540                 label faceI = changedFaces[changedFaceI];
00541 
00542                 if (!doneFace[faceI])
00543                 {
00544                     doneFace[faceI] = true;
00545 
00546                     // Get current zone info
00547                     label zoneID = faceZones.whichZone(faceI);
00548 
00549                     bool zoneFlip = false;
00550 
00551                     if (zoneID >= 0)
00552                     {
00553                         const faceZone& fZone = faceZones[zoneID];
00554 
00555                         zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00556                     }
00557 
00558                     // Get current connectivity
00559                     label own = faceOwner[faceI];
00560                     label nei = -1;
00561                     label patchID = -1;
00562 
00563                     if (mesh_.isInternalFace(faceI))
00564                     {
00565                         nei = faceNeighbour[faceI];
00566                     }
00567                     else
00568                     {
00569                         patchID = boundaryMesh.whichPatch(faceI);
00570                     }
00571 
00572                     meshMod.modifyFace
00573                     (
00574                         newFaces[faceI],            // face
00575                         faceI,                      // faceI to change
00576                         own,                        // owner
00577                         nei,                        // neighbour
00578                         false,                      // flipFaceFlux
00579                         patchID,                    // patch
00580                         zoneID,
00581                         zoneFlip
00582                     );
00583                     meshChanged = true;
00584                 }
00585             }
00586         }
00587     }
00588 
00589     return meshChanged;
00590 }
00591 
00592 
00593 void Foam::edgeCollapser::updateMesh(const mapPolyMesh& map)
00594 {
00595     pointRegion_.setSize(mesh_.nPoints());
00596     pointRegion_ = -1;
00597     // Reset count, do not remove underlying storage
00598     pointRegionMaster_.clear();
00599     freeRegions_.clear();
00600 }
00601 
00602 
00603 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd