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

collapseEdges.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 Description
00026     Collapse short edges and combines edges that are in line.
00027 
00028     - collapse short edges. Length of edges to collapse provided as argument.
00029     - merge two edges if they are in line. Maximum angle provided as argument.
00030     - remove unused points.
00031 
00032     Cannot remove cells. Can remove faces and points but does not check
00033     for nonsense resulting topology.
00034 
00035     When collapsing an edge with one point on the boundary it will leave
00036     the boundary point intact. When both points inside it chooses random. When
00037     both points on boundary random again. Note: it should in fact use features
00038     where if one point is on a feature it collapses to that one. Alas we don't
00039     have features on a polyMesh.
00040 
00041 \*---------------------------------------------------------------------------*/
00042 
00043 #include "argList.H"
00044 #include "Time.H"
00045 #include "edgeCollapser.H"
00046 #include "polyTopoChange.H"
00047 #include "polyTopoChanger.H"
00048 #include "polyMesh.H"
00049 #include "mapPolyMesh.H"
00050 #include "mathematicalConstants.H"
00051 #include "PackedBoolList.H"
00052 #include "SortableList.H"
00053 
00054 using namespace Foam;
00055 
00056 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00057 
00058 // Get faceEdges in order of face points, i.e. faceEdges[0] is between
00059 // f[0] and f[1]
00060 labelList getSortedEdges
00061 (
00062     const edgeList& edges,
00063     const labelList& f,
00064     const labelList& edgeLabels
00065 )
00066 {
00067     labelList faceEdges(edgeLabels.size(), -1);
00068 
00069     // Find starting pos in f for every edgeLabels
00070     forAll(edgeLabels, i)
00071     {
00072         label edgeI = edgeLabels[i];
00073 
00074         const edge& e = edges[edgeI];
00075 
00076         label fp = findIndex(f, e[0]);
00077         label fp1 = f.fcIndex(fp);
00078 
00079         if (f[fp1] == e[1])
00080         {
00081             // EdgeI between fp -> fp1
00082             faceEdges[fp] = edgeI;
00083         }
00084         else
00085         {
00086             // EdgeI between fp-1 -> fp
00087             faceEdges[f.rcIndex(fp)] = edgeI;
00088         }
00089     }
00090 
00091     return faceEdges;
00092 }
00093 
00094 
00095 // Merges edges which are in straight line. I.e. edge split by point.
00096 label mergeEdges
00097 (
00098     const polyMesh& mesh,
00099     const scalar maxCos,
00100     edgeCollapser& collapser
00101 )
00102 {
00103     const pointField& points = mesh.points();
00104     const edgeList& edges = mesh.edges();
00105     const labelListList& pointEdges = mesh.pointEdges();
00106     const labelList& region = collapser.pointRegion();
00107     const labelList& master = collapser.pointRegionMaster();
00108 
00109     label nCollapsed = 0;
00110 
00111     forAll(pointEdges, pointI)
00112     {
00113         const labelList& pEdges = pointEdges[pointI];
00114 
00115         if (pEdges.size() == 2)
00116         {
00117             const edge& leftE = edges[pEdges[0]];
00118             const edge& rightE = edges[pEdges[1]];
00119 
00120             // Get the two vertices on both sides of the point
00121             label leftV = leftE.otherVertex(pointI);
00122             label rightV = rightE.otherVertex(pointI);
00123 
00124             // Collapse only if none of the points part of merge network
00125             // or all of networks with different masters.
00126             label midMaster = -1;
00127             if (region[pointI] != -1)
00128             {
00129                 midMaster = master[region[pointI]];
00130             }
00131 
00132             label leftMaster = -2;
00133             if (region[leftV] != -1)
00134             {
00135                 leftMaster = master[region[leftV]];
00136             }
00137 
00138             label rightMaster = -3;
00139             if (region[rightV] != -1)
00140             {
00141                 rightMaster = master[region[rightV]];
00142             }
00143 
00144             if
00145             (
00146                 midMaster != leftMaster
00147              && midMaster != rightMaster
00148              && leftMaster != rightMaster
00149             )
00150             {
00151                 // Check if the two edge are in line
00152                 vector leftVec = points[pointI] - points[leftV];
00153                 leftVec /= mag(leftVec) + VSMALL;
00154 
00155                 vector rightVec = points[rightV] - points[pointI];
00156                 rightVec /= mag(rightVec) + VSMALL;
00157 
00158                 if ((leftVec & rightVec) > maxCos)
00159                 {
00160                     // Collapse one (left) side of the edge. Make left vertex
00161                     // the master.
00162                     //if (collapser.unaffectedEdge(pEdges[0]))
00163                     {
00164                         collapser.collapseEdge(pEdges[0], leftV);
00165                         nCollapsed++;
00166                     }
00167                 }
00168             }
00169         }
00170     }
00171 
00172     return nCollapsed;
00173 }
00174 
00175 
00176 // Return master point edge needs to be collapsed to (or -1)
00177 label edgeMaster(const PackedBoolList& boundaryPoint, const edge& e)
00178 {
00179     label masterPoint = -1;
00180 
00181     // Collapse edge to boundary point.
00182     if (boundaryPoint.get(e[0]))
00183     {
00184         if (boundaryPoint.get(e[1]))
00185         {
00186             // Both points on boundary. Choose one to collapse to.
00187             // Note: should look at feature edges/points!
00188             masterPoint = e[0];
00189         }
00190         else
00191         {
00192             masterPoint = e[0];
00193         }
00194     }
00195     else
00196     {
00197         if (boundaryPoint.get(e[1]))
00198         {
00199             masterPoint = e[1];
00200         }
00201         else
00202         {
00203             // None on boundary. Choose arbitrary.
00204             // Note: should look at geometry?
00205             masterPoint = e[0];
00206         }
00207     }
00208     return masterPoint;
00209 }
00210 
00211 
00212 label collapseSmallEdges
00213 (
00214     const polyMesh& mesh,
00215     const PackedBoolList& boundaryPoint,
00216     const scalar minLen,
00217     edgeCollapser& collapser
00218 )
00219 {
00220     const pointField& points = mesh.points();
00221     const edgeList& edges = mesh.edges();
00222 
00223     // Collapse all edges that are too small. Choose intelligently which
00224     // point to collapse edge to.
00225 
00226     label nCollapsed = 0;
00227 
00228     forAll(edges, edgeI)
00229     {
00230         const edge& e = edges[edgeI];
00231 
00232         if (e.mag(points) < minLen)
00233         {
00234             label master = edgeMaster(boundaryPoint, e);
00235 
00236             if (master != -1) // && collapser.unaffectedEdge(edgeI))
00237             {
00238                 collapser.collapseEdge(edgeI, master);
00239                 nCollapsed++;
00240             }
00241         }
00242     }
00243     return nCollapsed;
00244 }
00245 
00246 
00247 // Faces which have edges just larger than collapse length but faces which
00248 // are very small. This one tries to collapse them if it can be done with
00249 // edge collapse. For faces where a face gets replace by two edges use
00250 // collapseFaces
00251 label collapseHighAspectFaces
00252 (
00253     const polyMesh& mesh,
00254     const PackedBoolList& boundaryPoint,
00255     const scalar areaFac,
00256     const scalar edgeRatio,
00257     edgeCollapser& collapser
00258 )
00259 {
00260     const pointField& points = mesh.points();
00261     const edgeList& edges = mesh.edges();
00262     const faceList& faces = mesh.faces();
00263     const labelListList& faceEdges = mesh.faceEdges();
00264 
00265     scalarField magArea(mag(mesh.faceAreas()));
00266 
00267     label maxIndex = findMax(magArea);
00268 
00269     scalar minArea = areaFac * magArea[maxIndex];
00270 
00271     Info<< "Max face area:" << magArea[maxIndex] << endl
00272         << "Collapse area factor:" << areaFac << endl
00273         << "Collapse area:" << minArea << endl;
00274 
00275     label nCollapsed = 0;
00276 
00277     forAll(faces, faceI)
00278     {
00279         if (magArea[faceI] < minArea)
00280         {
00281             const face& f = faces[faceI];
00282 
00283             // Get the edges in face point order
00284             labelList fEdges(getSortedEdges(edges, f, faceEdges[faceI]));
00285 
00286             SortableList<scalar> lengths(fEdges.size());
00287             forAll(fEdges, i)
00288             {
00289                 lengths[i] = edges[fEdges[i]].mag(points);
00290             }
00291             lengths.sort();
00292 
00293 
00294             label edgeI = -1;
00295 
00296             if (f.size() == 4)
00297             {
00298                 // Compare second largest to smallest
00299                 if (lengths[2] > edgeRatio*lengths[0])
00300                 {
00301                     // Collapse smallest only. Triangle should be cleared
00302                     // next time around.
00303                     edgeI = fEdges[lengths.indices()[0]];
00304                 }
00305             }
00306             else if (f.size() == 3)
00307             {
00308                 // Compare second largest to smallest
00309                 if (lengths[1] > edgeRatio*lengths[0])
00310                 {
00311                     edgeI = fEdges[lengths.indices()[0]];
00312                 }
00313             }
00314 
00315 
00316             if (edgeI != -1)
00317             {
00318                 label master = edgeMaster(boundaryPoint, edges[edgeI]);
00319 
00320                 if (master != -1)// && collapser.unaffectedEdge(edgeI))
00321                 {
00322                     collapser.collapseEdge(edgeI, master);
00323                     nCollapsed++;
00324                 }
00325             }
00326         }
00327     }
00328 
00329     return nCollapsed;
00330 }
00331 
00332 
00333 void set(const labelList& elems, const bool val, boolList& status)
00334 {
00335     forAll(elems, i)
00336     {
00337         status[elems[i]] = val;
00338     }
00339 }
00340 
00341 
00342 // Tries to simplify polygons to face of minSize (4=quad, 3=triangle)
00343 label simplifyFaces
00344 (
00345     const polyMesh& mesh,
00346     const PackedBoolList& boundaryPoint,
00347     const label minSize,
00348     const scalar lenGap,
00349     edgeCollapser& collapser
00350 )
00351 {
00352     const pointField& points = mesh.points();
00353     const edgeList& edges = mesh.edges();
00354     const faceList& faces = mesh.faces();
00355     const cellList& cells = mesh.cells();
00356     const labelListList& faceEdges = mesh.faceEdges();
00357     const labelList& faceOwner = mesh.faceOwner();
00358     const labelList& faceNeighbour = mesh.faceNeighbour();
00359     const labelListList& pointCells = mesh.pointCells();
00360     const labelListList& cellEdges = mesh.cellEdges();
00361 
00362     label nCollapsed = 0;
00363 
00364     boolList protectedEdge(mesh.nEdges(), false);
00365 
00366     forAll(faces, faceI)
00367     {
00368         const face& f = faces[faceI];
00369 
00370         if
00371         (
00372             f.size() > minSize
00373          && cells[faceOwner[faceI]].size() >= 6
00374          && (
00375                 mesh.isInternalFace(faceI)
00376              && cells[faceNeighbour[faceI]].size() >= 6
00377             )
00378         )
00379         {
00380             // Get the edges in face point order
00381             labelList fEdges(getSortedEdges(edges, f, faceEdges[faceI]));
00382 
00383             SortableList<scalar> lengths(fEdges.size());
00384             forAll(fEdges, i)
00385             {
00386                 lengths[i] = edges[fEdges[i]].mag(points);
00387             }
00388             lengths.sort();
00389 
00390 
00391             // Now find a gap in length between consecutive elements greater
00392             // than lenGap.
00393 
00394             label gapPos = -1;
00395 
00396             for (label i = f.size()-1-minSize; i >= 0; --i)
00397             {
00398                 if (lengths[i+1] > lenGap*lengths[i])
00399                 {
00400                     gapPos = i;
00401 
00402                     break;
00403                 }
00404             }
00405 
00406             if (gapPos != -1)
00407             {
00408                 //for (label i = gapPos; i >= 0; --i)
00409                 label i = 0;  // Hack: collapse smallest edge only.
00410                 {
00411                     label edgeI = fEdges[lengths.indices()[i]];
00412 
00413                     if (!protectedEdge[edgeI])
00414                     {
00415                         const edge& e = edges[edgeI];
00416 
00417                         label master = edgeMaster(boundaryPoint, e);
00418 
00419                         if (master != -1)
00420                         {
00421                             collapser.collapseEdge(edgeI, master);
00422 
00423                             // Protect all other edges on all cells using edge
00424                             // points.
00425 
00426                             const labelList& pCells0 = pointCells[e[0]];
00427 
00428                             forAll(pCells0, i)
00429                             {
00430                                 set(cellEdges[pCells0[i]], true, protectedEdge);
00431                             }
00432                             const labelList& pCells1 = pointCells[e[1]];
00433 
00434                             forAll(pCells1, i)
00435                             {
00436                                 set(cellEdges[pCells1[i]], true, protectedEdge);
00437                             }
00438 
00439                             nCollapsed++;
00440                         }
00441                     }
00442                 }
00443             }
00444         }
00445     }
00446 
00447     return nCollapsed;
00448 }
00449 
00450 
00451 // Main program:
00452 
00453 int main(int argc, char *argv[])
00454 {
00455     argList::noParallel();
00456     argList::validOptions.insert("overwrite", "");
00457     argList::validArgs.append("edge length [m]");
00458     argList::validArgs.append("merge angle (degrees)");
00459 
00460 #   include "setRootCase.H"
00461 #   include "createTime.H"
00462     runTime.functionObjects().off();
00463 #   include "createPolyMesh.H"
00464     const word oldInstance = mesh.pointsInstance();
00465 
00466     scalar minLen(readScalar(IStringStream(args.additionalArgs()[0])()));
00467     scalar angle(readScalar(IStringStream(args.additionalArgs()[1])()));
00468     bool overwrite = args.optionFound("overwrite");
00469 
00470     scalar maxCos = Foam::cos(angle*180/mathematicalConstant::pi);
00471 
00472     Info<< "Merging:" << nl
00473         << "    edges with length less than " << minLen << " meters" << nl
00474         << "    edges split by a point with edges in line to within " << angle
00475         << " degrees" << nl
00476         << endl;
00477 
00478 
00479     bool meshChanged = false;
00480 
00481     while (true)
00482     {
00483         const faceList& faces = mesh.faces();
00484 
00485         // Get all points on the boundary
00486         PackedBoolList boundaryPoint(mesh.nPoints());
00487 
00488         label nIntFaces = mesh.nInternalFaces();
00489         for (label faceI = nIntFaces; faceI < mesh.nFaces(); faceI++)
00490         {
00491             const face& f = faces[faceI];
00492 
00493             forAll(f, fp)
00494             {
00495                 boundaryPoint.set(f[fp], 1);
00496             }
00497         }
00498 
00499         // Edge collapsing engine
00500         edgeCollapser collapser(mesh);
00501 
00502 
00503         // Collapse all edges that are too small.
00504         label nCollapsed =
00505             collapseSmallEdges
00506             (
00507                 mesh,
00508                 boundaryPoint,
00509                 minLen,
00510                 collapser
00511             );
00512         Info<< "Collapsing " << nCollapsed << " small edges" << endl;
00513 
00514 
00515         // Remove midpoints on straight edges.
00516         if (nCollapsed == 0)
00517         {
00518             nCollapsed = mergeEdges(mesh, maxCos, collapser);
00519             Info<< "Collapsing " << nCollapsed << " in line edges" << endl;
00520         }
00521 
00522 
00523         // Remove small sliver faces that can be collapsed to single edge
00524         if (nCollapsed == 0)
00525         {
00526             nCollapsed =
00527                 collapseHighAspectFaces
00528                 (
00529                     mesh,
00530                     boundaryPoint,
00531                     1E-9,       // factor of largest face area
00532                     5,          // factor between smallest and largest edge on
00533                                 // face
00534                     collapser
00535                 );
00536             Info<< "Collapsing " << nCollapsed
00537                 << " small high aspect ratio faces" << endl;
00538         }
00539 
00540         // Simplify faces to quads wherever possible
00541         //if (nCollapsed == 0)
00542         //{
00543         //    nCollapsed =
00544         //        simplifyFaces
00545         //        (
00546         //            mesh,
00547         //            boundaryPoint,
00548         //            4,              // minimum size of face
00549         //            0.2,            // gap in edge lengths on face
00550         //            collapser
00551         //        );
00552         //    Info<< "Collapsing " << nCollapsed << " polygonal faces" << endl;
00553         //}
00554 
00555 
00556         if (nCollapsed == 0)
00557         {
00558             break;
00559         }
00560 
00561         polyTopoChange meshMod(mesh);
00562 
00563         // Insert mesh refinement into polyTopoChange.
00564         collapser.setRefinement(meshMod);
00565 
00566         // Do all changes
00567         Info<< "Morphing ..." << endl;
00568 
00569         autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00570 
00571         collapser.updateMesh(morphMap());
00572 
00573         if (morphMap().hasMotionPoints())
00574         {
00575             mesh.movePoints(morphMap().preMotionPoints());
00576         }
00577 
00578         meshChanged = true;
00579     }
00580 
00581     if (meshChanged)
00582     {
00583         // Write resulting mesh
00584         if (!overwrite)
00585         {
00586             runTime++;
00587         }
00588         else
00589         {
00590             mesh.setInstance(oldInstance);
00591         }
00592 
00593         Info<< "Writing collapsed mesh to time " << runTime.timeName() << endl;
00594 
00595         mesh.write();
00596     }
00597 
00598     Info << "End\n" << endl;
00599 
00600     return 0;
00601 }
00602 
00603 
00604 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd