FreeFOAM The Cross-Platform CFD Toolkit
collapseEdge.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "collapseEdge.H"
27 
28 //- Sets point neighbours of face to val
29 static void markPointNbrs
30 (
31  const triSurface& surf,
32  const label faceI,
33  const bool val,
34  boolList& okToCollapse
35 )
36 {
37  const labelledTri& f = surf.localFaces()[faceI];
38 
39  forAll(f, fp)
40  {
41  const labelList& pFaces = surf.pointFaces()[f[fp]];
42 
43  forAll(pFaces, i)
44  {
45  okToCollapse[pFaces[i]] = false;
46  }
47  }
48 }
49 
50 
51 static triSurface pack
52 (
53  const triSurface& surf,
54  const pointField& localPoints,
55  const labelList& pointMap
56 )
57 {
58  List<labelledTri> newTriangles(surf.size());
59  label newTriangleI = 0;
60 
61  forAll(surf, faceI)
62  {
63  const labelledTri& f = surf.localFaces()[faceI];
64 
65  label newA = pointMap[f[0]];
66  label newB = pointMap[f[1]];
67  label newC = pointMap[f[2]];
68 
69  if ((newA != newB) && (newA != newC) && (newB != newC))
70  {
71  newTriangles[newTriangleI++] =
72  labelledTri(newA, newB, newC, f.region());
73  }
74  }
75  newTriangles.setSize(newTriangleI);
76 
77  return triSurface(newTriangles, surf.patches(), localPoints);
78 }
79 
80 
81 // Collapses small edge to point, thus removing triangle.
82 label collapseEdge(triSurface& surf, const scalar minLen)
83 {
84  label nTotalCollapsed = 0;
85 
86  while (true)
87  {
88  const pointField& localPoints = surf.localPoints();
89  const List<labelledTri>& localFaces = surf.localFaces();
90 
91 
92  // Mapping from old to new points
93  labelList pointMap(surf.nPoints());
94  forAll(pointMap, i)
95  {
96  pointMap[i] = i;
97  }
98 
99  // Storage for new points.
100  pointField newPoints(localPoints);
101 
102  // To protect neighbours of collapsed faces.
103  boolList okToCollapse(surf.size(), true);
104  label nCollapsed = 0;
105 
106  forAll(localFaces, faceI)
107  {
108  if (okToCollapse[faceI])
109  {
110  // Check edge lengths.
111  const labelledTri& f = localFaces[faceI];
112 
113  forAll(f, fp)
114  {
115  label v = f[fp];
116  label v1 = f[(fp+1) % 3];
117 
118  if (mag(localPoints[v1] - localPoints[v]) < minLen)
119  {
120  // Collapse f[fp1] onto f[fp].
121  pointMap[v1] = v;
122  newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
123 
124  Pout<< "Collapsing triange " << faceI << " to edge mid "
125  << newPoints[v] << endl;
126 
127  nCollapsed++;
128  okToCollapse[faceI] = false;
129 
130  // Protect point neighbours from collapsing.
131  markPointNbrs(surf, faceI, false, okToCollapse);
132 
133  break;
134  }
135  }
136  }
137  }
138 
139  Pout<< "collapseEdge : collapsing " << nCollapsed << " triangles"
140  << endl;
141 
142  nTotalCollapsed += nCollapsed;
143 
144  if (nCollapsed == 0)
145  {
146  break;
147  }
148 
149  // Pack the triangles
150  surf = pack(surf, newPoints, pointMap);
151  }
152 
153  // Remove any unused vertices
154  surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
155 
156  return nTotalCollapsed;
157 }
158 
159 
160 // ************************ vim: set sw=4 sts=4 et: ************************ //