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) 2011-2016 OpenFOAM Foundation
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 static void markPointNbrs
29 (
30  const triSurface& surf,
31  const label facei,
32  const bool val,
33  boolList& okToCollapse
34 )
35 {
36  const triSurface::FaceType& f = surf.localFaces()[facei];
37 
38  forAll(f, fp)
39  {
40  const labelList& pFaces = surf.pointFaces()[f[fp]];
41 
42  forAll(pFaces, i)
43  {
44  okToCollapse[pFaces[i]] = false;
45  }
46  }
47 }
48 
49 
50 static triSurface pack
51 (
52  const triSurface& surf,
53  const pointField& localPoints,
54  const labelList& pointMap
55 )
56 {
57  List<labelledTri> newTriangles(surf.size());
58  label newTriangleI = 0;
59 
60  forAll(surf, facei)
61  {
62  const labelledTri& f = surf.localFaces()[facei];
63 
64  label newA = pointMap[f[0]];
65  label newB = pointMap[f[1]];
66  label newC = pointMap[f[2]];
67 
68  if ((newA != newB) && (newA != newC) && (newB != newC))
69  {
70  newTriangles[newTriangleI++] =
71  labelledTri(newA, newB, newC, f.region());
72  }
73  }
74  newTriangles.setSize(newTriangleI);
75 
76  return triSurface(newTriangles, surf.patches(), localPoints);
77 }
78 
79 
80 // Collapses small edge to point, thus removing triangle.
81 label collapseEdge(triSurface& surf, const scalar minLen)
82 {
83  label nTotalCollapsed = 0;
84 
85  while (true)
86  {
87  const pointField& localPoints = surf.localPoints();
88  const List<labelledTri>& localFaces = surf.localFaces();
89 
90 
91  // Mapping from old to new points
92  labelList pointMap(surf.nPoints());
93  forAll(pointMap, i)
94  {
95  pointMap[i] = i;
96  }
97 
98  // Storage for new points.
99  pointField newPoints(localPoints);
100 
101  // To protect neighbours of collapsed faces.
102  boolList okToCollapse(surf.size(), true);
103  label nCollapsed = 0;
104 
105  forAll(localFaces, facei)
106  {
107  if (okToCollapse[facei])
108  {
109  // Check edge lengths.
110  const triSurface::FaceType& f = localFaces[facei];
111 
112  forAll(f, fp)
113  {
114  label v = f[fp];
115  label v1 = f[f.fcIndex(fp)];
116 
117  if (mag(localPoints[v1] - localPoints[v]) < minLen)
118  {
119  // Collapse f[fp1] onto f[fp].
120  pointMap[v1] = v;
121  newPoints[v] = 0.5*(localPoints[v1] + localPoints[v]);
122 
123  //Pout<< "Collapsing triange " << facei
124  // << " to edge mid " << newPoints[v] << endl;
125 
126  nCollapsed++;
127  okToCollapse[facei] = false;
128 
129  // Protect point neighbours from collapsing.
130  markPointNbrs(surf, facei, false, okToCollapse);
131 
132  break;
133  }
134  }
135  }
136  }
137 
138  Info<< "collapseEdge : collapsing " << nCollapsed
139  << " triangles to a single edge."
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 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Info<< "Finished reading KIVA file"<< endl;cellShapeList cellShapes(nPoints);labelList cellZoning(nPoints,-1);const cellModel &hex=*(cellModeller::lookup("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
List< label > labelList
A List of labels.
Definition: labelList.H:56
Routines to collapse small edges.
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)