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

collapseEdge.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 "collapseEdge.H"
00028 
00029 //- Sets point neighbours of face to val
00030 static void markPointNbrs
00031 (
00032     const triSurface& surf,
00033     const label faceI,
00034     const bool val,
00035     boolList& okToCollapse
00036 )
00037 {
00038     const labelledTri& f = surf.localFaces()[faceI];
00039 
00040     forAll(f, fp)
00041     {
00042         const labelList& pFaces = surf.pointFaces()[f[fp]];
00043 
00044         forAll(pFaces, i)
00045         {
00046             okToCollapse[pFaces[i]] = false;
00047         }
00048     }
00049 }
00050 
00051 
00052 static triSurface pack
00053 (
00054     const triSurface& surf,
00055     const pointField& localPoints,
00056     const labelList& pointMap
00057 )
00058 {
00059     List<labelledTri> newTriangles(surf.size());
00060     label newTriangleI = 0;
00061     
00062     forAll(surf, faceI)
00063     {
00064         const labelledTri& f = surf.localFaces()[faceI];
00065 
00066         label newA = pointMap[f[0]];
00067         label newB = pointMap[f[1]];
00068         label newC = pointMap[f[2]];
00069 
00070         if ((newA != newB) && (newA != newC) && (newB != newC))
00071         {
00072             newTriangles[newTriangleI++] =
00073                 labelledTri(newA, newB, newC, f.region());
00074         }
00075     }
00076     newTriangles.setSize(newTriangleI);
00077 
00078     return triSurface(newTriangles, surf.patches(), localPoints);
00079 }
00080 
00081 
00082 // Collapses small edge to point, thus removing triangle.
00083 label collapseEdge(triSurface& surf, const scalar minLen)
00084 {
00085     label nTotalCollapsed = 0;
00086 
00087     while (true)
00088     {
00089         const pointField& localPoints = surf.localPoints();
00090         const List<labelledTri>& localFaces = surf.localFaces();
00091 
00092 
00093         // Mapping from old to new points
00094         labelList pointMap(surf.nPoints());
00095         forAll(pointMap, i)
00096         {
00097             pointMap[i] = i;
00098         }
00099 
00100         // Storage for new points.
00101         pointField newPoints(localPoints);
00102 
00103         // To protect neighbours of collapsed faces.
00104         boolList okToCollapse(surf.size(), true);
00105         label nCollapsed = 0;
00106 
00107         forAll(localFaces, faceI)
00108         {
00109             if (okToCollapse[faceI])
00110             {
00111                 // Check edge lengths.
00112                 const labelledTri& f = localFaces[faceI];
00113 
00114                 forAll(f, fp)
00115                 {
00116                     label v = f[fp];
00117                     label v1 = f[(fp+1) % 3];
00118 
00119                     if (mag(localPoints[v1] - localPoints[v]) < minLen)
00120                     {
00121                         // Collapse f[fp1] onto f[fp].
00122                         pointMap[v1] = v;
00123                         newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
00124 
00125                         Pout<< "Collapsing triange " << faceI << " to edge mid "
00126                             << newPoints[v] << endl;
00127 
00128                         nCollapsed++;
00129                         okToCollapse[faceI] = false;
00130 
00131                         // Protect point neighbours from collapsing.
00132                         markPointNbrs(surf, faceI, false, okToCollapse);
00133 
00134                         break;
00135                     }
00136                 }
00137             }
00138         }
00139 
00140         Pout<< "collapseEdge : collapsing " << nCollapsed << " triangles"
00141             << endl;
00142 
00143         nTotalCollapsed += nCollapsed;
00144 
00145         if (nCollapsed == 0)
00146         {
00147             break;
00148         }
00149 
00150         // Pack the triangles
00151         surf = pack(surf, newPoints, pointMap);
00152     }
00153 
00154     // Remove any unused vertices
00155     surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
00156 
00157     return nTotalCollapsed;
00158 }
00159 
00160 
00161 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd