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 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 //- 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 triSurface::FaceType& 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 triSurface::FaceType& f = localFaces[faceI];
112 
113  forAll(f, fp)
114  {
115  label v = f[fp];
116  label v1 = f[f.fcIndex(fp)];
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
125  // << " to edge mid " << 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  Info<< "collapseEdge : collapsing " << nCollapsed
140  << " triangles to a single edge."
141  << endl;
142 
143  nTotalCollapsed += nCollapsed;
144 
145  if (nCollapsed == 0)
146  {
147  break;
148  }
149 
150  // Pack the triangles
151  surf = pack(surf, newPoints, pointMap);
152  }
153 
154  // Remove any unused vertices
155  surf = triSurface(surf.localFaces(), surf.patches(), surf.localPoints());
156 
157  return nTotalCollapsed;
158 }
159 
160 
161 // ************************************************************************* //
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Foam::labelList
List< label > labelList
A List of labels.
Definition: labelList.H:56
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
pFaces
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
Foam::label
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
Foam::Info
messageStream Info
collapseEdge.H
Routines to collapse small edges.
Foam::boolList
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
f
labelList f(nPoints)
List
Definition: Test.C:19
collapseEdge
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.