BUGFIX: Uninitialised member variables
[foam-extend-3.2.git] / applications / utilities / surface / surfaceClean / collapseEdge.C
blobdb43a269fae8f63b5bcf62d6351011e0d839aa8f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "collapseEdge.H"
29 //- Sets point neighbours of face to val
30 static void markPointNbrs
32     const triSurface& surf,
33     const label faceI,
34     const bool val,
35     boolList& okToCollapse
38     const labelledTri& f = surf.localFaces()[faceI];
40     forAll(f, fp)
41     {
42         const labelList& pFaces = surf.pointFaces()[f[fp]];
44         forAll(pFaces, i)
45         {
46             okToCollapse[pFaces[i]] = false;
47         }
48     }
52 static triSurface pack
54     const triSurface& surf,
55     const pointField& localPoints,
56     const labelList& pointMap
59     List<labelledTri> newTriangles(surf.size());
60     label newTriangleI = 0;
62     forAll(surf, faceI)
63     {
64         const labelledTri& f = surf.localFaces()[faceI];
66         label newA = pointMap[f[0]];
67         label newB = pointMap[f[1]];
68         label newC = pointMap[f[2]];
70         if ((newA != newB) && (newA != newC) && (newB != newC))
71         {
72             newTriangles[newTriangleI++] =
73                 labelledTri(newA, newB, newC, f.region());
74         }
75     }
76     newTriangles.setSize(newTriangleI);
78     return triSurface(newTriangles, surf.patches(), localPoints);
82 // Collapses small edge to point, thus removing triangle.
83 label collapseEdge(triSurface& surf, const scalar minLen)
85     label nTotalCollapsed = 0;
87     while (true)
88     {
89         const pointField& localPoints = surf.localPoints();
90         const List<labelledTri>& localFaces = surf.localFaces();
93         // Mapping from old to new points
94         labelList pointMap(surf.nPoints());
95         forAll(pointMap, i)
96         {
97             pointMap[i] = i;
98         }
100         // Storage for new points.
101         pointField newPoints(localPoints);
103         // To protect neighbours of collapsed faces.
104         boolList okToCollapse(surf.size(), true);
105         label nCollapsed = 0;
107         forAll(localFaces, faceI)
108         {
109             if (okToCollapse[faceI])
110             {
111                 // Check edge lengths.
112                 const labelledTri& f = localFaces[faceI];
114                 forAll(f, fp)
115                 {
116                     label v = f[fp];
117                     label v1 = f[(fp+1) % 3];
119                     if (mag(localPoints[v1] - localPoints[v]) < minLen)
120                     {
121                         // Collapse f[fp1] onto f[fp].
122                         pointMap[v1] = v;
123                         newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
125                         Pout<< "Collapsing triange " << faceI << " to edge mid "
126                             << newPoints[v] << endl;
128                         nCollapsed++;
129                         okToCollapse[faceI] = false;
131                         // Protect point neighbours from collapsing.
132                         markPointNbrs(surf, faceI, false, okToCollapse);
134                         break;
135                     }
136                 }
137             }
138         }
140         Pout<< "collapseEdge : collapsing " << nCollapsed << " triangles"
141             << endl;
143         nTotalCollapsed += nCollapsed;
145         if (nCollapsed == 0)
146         {
147             break;
148         }
150         // Pack the triangles
151         surf = pack(surf, newPoints, pointMap);
152     }
154     // Remove any unused vertices
155     surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
157     return nTotalCollapsed;
161 // ************************************************************************* //