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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "collapseEdge.H"
30 
31 static void markPointNbrs
32 (
33  const triSurface& surf,
34  const label facei,
35  const bool val,
36  boolList& okToCollapse
37 )
38 {
39  const triSurface::face_type& f = surf.localFaces()[facei];
40 
41  forAll(f, fp)
42  {
43  const labelList& pFaces = surf.pointFaces()[f[fp]];
44 
45  forAll(pFaces, i)
46  {
47  okToCollapse[pFaces[i]] = false;
48  }
49  }
50 }
51 
52 
53 static triSurface pack
54 (
55  const triSurface& surf,
56  const pointField& localPoints,
57  const labelList& pointMap
58 )
59 {
60  List<labelledTri> newTriangles(surf.size());
61  label newTriangleI = 0;
62 
63  forAll(surf, facei)
64  {
65  const labelledTri& f = surf.localFaces()[facei];
66 
67  label newA = pointMap[f[0]];
68  label newB = pointMap[f[1]];
69  label newC = pointMap[f[2]];
70 
71  if ((newA != newB) && (newA != newC) && (newB != newC))
72  {
73  newTriangles[newTriangleI++] =
74  labelledTri(newA, newB, newC, f.region());
75  }
76  }
77  newTriangles.setSize(newTriangleI);
78 
79  return triSurface(newTriangles, surf.patches(), localPoints);
80 }
81 
82 
83 // Collapses small edge to point, thus removing triangle.
84 label collapseEdge(triSurface& surf, const scalar minLen)
85 {
86  label nTotalCollapsed = 0;
87 
88  while (true)
89  {
90  const pointField& localPoints = surf.localPoints();
91  const List<labelledTri>& localFaces = surf.localFaces();
92 
93 
94  // Mapping from old to new points
95  labelList pointMap(surf.nPoints());
96  forAll(pointMap, i)
97  {
98  pointMap[i] = i;
99  }
100 
101  // Storage for new points.
102  pointField newPoints(localPoints);
103 
104  // To protect neighbours of collapsed faces.
105  boolList okToCollapse(surf.size(), true);
106  label nCollapsed = 0;
107 
108  forAll(localFaces, facei)
109  {
110  if (okToCollapse[facei])
111  {
112  // Check edge lengths.
113  const triSurface::face_type& f = localFaces[facei];
114 
115  forAll(f, fp)
116  {
117  label v = f[fp];
118  label v1 = f[f.fcIndex(fp)];
119 
120  if (mag(localPoints[v1] - localPoints[v]) < minLen)
121  {
122  // Collapse f[fp1] onto f[fp].
123  pointMap[v1] = v;
124  newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
125 
126  //Pout<< "Collapsing triangle " << facei
127  // << " to edge mid " << newPoints[v] << endl;
128 
129  nCollapsed++;
130  okToCollapse[facei] = false;
131 
132  // Protect point neighbours from collapsing.
133  markPointNbrs(surf, facei, false, okToCollapse);
134 
135  break;
136  }
137  }
138  }
139  }
140 
141  Info<< "collapseEdge : collapsing " << nCollapsed
142  << " triangles to a single edge."
143  << endl;
144 
145  nTotalCollapsed += nCollapsed;
146 
147  if (nCollapsed == 0)
148  {
149  break;
150  }
151 
152  // Pack the triangles
153  surf = pack(surf, newPoints, pointMap);
154  }
155 
156  // Remove any unused vertices
157  surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
158 
159  return nTotalCollapsed;
160 }
161 
162 
163 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::boolList
List< bool > boolList
A List of bools.
Definition: List.H:69
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
pFaces
Info<< "Finished reading KIVA file"<< endl;cellShapeList cellShapes(nPoints);labelList cellZoning(nPoints, -1);const cellModel &hex=cellModel::ref(cellModel::HEX);labelList hexLabels(8);label activeCells=0;labelList pointMap(nPoints);forAll(pointMap, i){ pointMap[i]=i;}for(label i=0;i< nPoints;i++){ if(f[i] > 0.0) { hexLabels[0]=i;hexLabels[1]=i1tab[i];hexLabels[2]=i3tab[i1tab[i]];hexLabels[3]=i3tab[i];hexLabels[4]=i8tab[i];hexLabels[5]=i1tab[i8tab[i]];hexLabels[6]=i3tab[i1tab[i8tab[i]]];hexLabels[7]=i3tab[i8tab[i]];cellShapes[activeCells]=cellShape(hex, hexLabels);edgeList edges=cellShapes[activeCells].edges();forAll(edges, ei) { if(edges[ei].mag(points)< SMALL) { label start=pointMap[edges[ei].start()];while(start !=pointMap[start]) { start=pointMap[start];} label end=pointMap[edges[ei].end()];while(end !=pointMap[end]) { end=pointMap[end];} label minLabel=min(start, end);pointMap[start]=pointMap[end]=minLabel;} } cellZoning[activeCells]=idreg[i];activeCells++;}}cellShapes.setSize(activeCells);cellZoning.setSize(activeCells);forAll(cellShapes, celli){ cellShape &cs=cellShapes[celli];forAll(cs, i) { cs[i]=pointMap[cs[i]];} cs.collapse();}label bcIDs[11]={-1, 0, 2, 4, -1, 5, -1, 6, 7, 8, 9};const label nBCs=12;const word *kivaPatchTypes[nBCs]={ &wallPolyPatch::typeName, &wallPolyPatch::typeName, &wallPolyPatch::typeName, &wallPolyPatch::typeName, &symmetryPolyPatch::typeName, &wedgePolyPatch::typeName, &polyPatch::typeName, &polyPatch::typeName, &polyPatch::typeName, &polyPatch::typeName, &symmetryPolyPatch::typeName, &oldCyclicPolyPatch::typeName};enum patchTypeNames{ PISTON, VALVE, LINER, CYLINDERHEAD, AXIS, WEDGE, INFLOW, OUTFLOW, PRESIN, PRESOUT, SYMMETRYPLANE, CYCLIC};const char *kivaPatchNames[nBCs]={ "piston", "valve", "liner", "cylinderHead", "axis", "wedge", "inflow", "outflow", "presin", "presout", "symmetryPlane", "cyclic"};List< SLList< face > > pFaces[nBCs]
Definition: readKivaGrid.H:235
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
collapseEdge.H
Routines to collapse small edges.
f
labelList f(nPoints)
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
collapseEdge
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.