FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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