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-2022 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 nNewTris = 0;
62 
63  // Iterate and work on a copy
64  for (labelledTri f : surf.localFaces())
65  {
66  // inplace renumber
67  f[0] = pointMap[f[0]];
68  f[1] = pointMap[f[1]];
69  f[2] = pointMap[f[2]];
70 
71  if (f.valid())
72  {
73  newTriangles[nNewTris++] = f;
74  }
75  }
76  newTriangles.resize(nNewTris);
77 
78  return triSurface(newTriangles, surf.patches(), localPoints);
79 }
80 
81 
82 // Collapses small edge to point, thus removing triangle.
83 label collapseEdge(triSurface& surf, const scalar minLen)
84 {
85  label nTotalCollapsed = 0;
86 
87  while (true)
88  {
89  const pointField& localPoints = surf.localPoints();
90  const List<labelledTri>& localFaces = surf.localFaces();
91 
92 
93  // Mapping from old to new points
94  labelList pointMap(identity(surf.nPoints()));
95 
96  // Storage for new points.
97  pointField newPoints(localPoints);
98 
99  // To protect neighbours of collapsed faces.
100  boolList okToCollapse(surf.size(), true);
101  label nCollapsed = 0;
102 
103  forAll(localFaces, facei)
104  {
105  if (okToCollapse[facei])
106  {
107  // Check edge lengths.
108  const triSurface::face_type& f = localFaces[facei];
109 
110  forAll(f, fp)
111  {
112  label v = f[fp];
113  label v1 = f[f.fcIndex(fp)];
114 
115  if (mag(localPoints[v1] - localPoints[v]) < minLen)
116  {
117  // Collapse f[fp1] onto f[fp].
118  pointMap[v1] = v;
119  newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
120 
121  //Pout<< "Collapsing triangle " << facei
122  // << " to edge mid " << newPoints[v] << endl;
123 
124  nCollapsed++;
125  okToCollapse[facei] = false;
126 
127  // Protect point neighbours from collapsing.
128  markPointNbrs(surf, facei, false, okToCollapse);
129 
130  break;
131  }
132  }
133  }
134  }
135 
136  Info<< "collapseEdge : collapsing " << nCollapsed
137  << " triangles to a single edge."
138  << endl;
139 
140  nTotalCollapsed += nCollapsed;
141 
142  if (nCollapsed == 0)
143  {
144  break;
145  }
146 
147  // Pack the triangles
148  surf = pack(surf, newPoints, pointMap);
149  }
150 
151  // Remove any unused vertices
152  surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
153 
154  return nTotalCollapsed;
155 }
156 
157 
158 // ************************************************************************* //
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:414
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:31
labelList f(nPoints)
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].reset(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
Routines to collapse small edges.
messageStream Info
Information stream (stdout output on master, null elsewhere)
List< label > labelList
A List of labels.
Definition: List.H:62
List< bool > boolList
A List of bools.
Definition: List.H:60