1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
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
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,
35 boolList& okToCollapse
38 const labelledTri& f = surf.localFaces()[faceI];
42 const labelList& pFaces = surf.pointFaces()[f[fp]];
46 okToCollapse[pFaces[i]] = false;
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;
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))
72 newTriangles[newTriangleI++] =
73 labelledTri(newA, newB, newC, f.region());
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;
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());
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)
109 if (okToCollapse[faceI])
111 // Check edge lengths.
112 const labelledTri& f = localFaces[faceI];
117 label v1 = f[(fp+1) % 3];
119 if (mag(localPoints[v1] - localPoints[v]) < minLen)
121 // Collapse f[fp1] onto f[fp].
123 newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
125 Pout<< "Collapsing triange " << faceI << " to edge mid "
126 << newPoints[v] << endl;
129 okToCollapse[faceI] = false;
131 // Protect point neighbours from collapsing.
132 markPointNbrs(surf, faceI, false, okToCollapse);
140 Pout<< "collapseEdge : collapsing " << nCollapsed << " triangles"
143 nTotalCollapsed += nCollapsed;
150 // Pack the triangles
151 surf = pack(surf, newPoints, pointMap);
154 // Remove any unused vertices
155 surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
157 return nTotalCollapsed;
161 // ************************************************************************* //