surfaceSplitByTopology.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 Description
25 
26  Strips any baffle parts of a surface. A baffle region is one which is
27  reached by walking from an open edge, and stopping when a multiply connected
28  edge is reached.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "triSurface.H"
34 
35 using namespace Foam;
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 int main(int argc, char *argv[])
40 {
42  argList::validOptions.clear();
43  argList::validArgs.append("input surface file");
44  argList::validArgs.append("output surface file");
45  argList args(argc, argv);
46 
47  fileName surfFileName(args[1]);
48  Info<< "Reading surface from " << surfFileName << endl;
49 
50  fileName outFileName(args[2]);
51  fileName outFileBaseName = outFileName.lessExt();
52  word outExtension = outFileName.ext();
53 
54  // Load surface
55  triSurface surf(surfFileName);
56 
57  bool anyZoneRemoved = false;
58 
59  label iterationNo = 0;
60  label iterationLimit = 10;
61 
62  Info<< "Splitting off baffle parts " << endl;
63 
64  do
65  {
66  anyZoneRemoved = false;
67 
68  labelList faceZone;
69 
70  const labelListList& edFaces = surf.edgeFaces();
71  const labelListList& faceEds = surf.faceEdges();
72 
73  boolList multipleEdges(edFaces.size(), false);
74 
75  forAll(multipleEdges, i)
76  {
77  if (edFaces[i].size() > 2)
78  {
79  multipleEdges[i] = true;
80  }
81  }
82 
83  label nZones = surf.markZones(multipleEdges, faceZone);
84 
85  if (nZones < 2)
86  {
87  break;
88  }
89 
90  boolList nonBaffle(faceZone.size(), true);
91  boolList baffle(faceZone.size(), true);
92  labelList pointMap;
94 
95 
96  for (label z = 0; z < nZones; z++)
97  {
98  bool keepZone = true;
99 
100  forAll(faceZone, f)
101  {
102  if (faceZone[f] == z)
103  {
104  forAll(faceEds[f], fe)
105  {
106  if (edFaces[faceEds[f][fe]].size() < 2)
107  {
108  keepZone = false;
109 
110  anyZoneRemoved = true;
111 
112  break;
113  }
114  }
115  }
116 
117  if (!keepZone)
118  {
119  break;
120  }
121  }
122 
123  forAll(faceZone, f)
124  {
125  if (faceZone[f] == z)
126  {
127  nonBaffle[f] = keepZone;
128  baffle[f] = !keepZone;
129  }
130  }
131  }
132 
133  Info<< " Iteration " << iterationNo << endl;
134 
135  triSurface baffleSurf = surf.subsetMesh(baffle, pointMap, faceMap);
136 
137  if (baffleSurf.size())
138  {
139  fileName bafflePartFileName =
140  outFileBaseName
141  + "_bafflePart_"
142  + name(iterationNo)
143  + "." + outExtension;
144 
145  Info<< " Writing baffle part to " << bafflePartFileName << endl;
146 
147  baffleSurf.write(bafflePartFileName);
148  }
149 
150  surf = surf.subsetMesh(nonBaffle, pointMap, faceMap);
151 
152  if (iterationNo == iterationLimit)
153  {
155  << "Iteration limit of " << iterationLimit << "reached" << endl;
156  }
157 
158  iterationNo++;
159 
160  } while (anyZoneRemoved && iterationNo < iterationLimit);
161 
162  Info<< "Writing new surface to " << outFileName << endl;
163 
164  surf.write(outFileName);
165 
166  labelList faceZone;
167 
168  const labelListList& edFaces = surf.edgeFaces();
169 
170  boolList multipleEdges(edFaces.size(), false);
171 
172  forAll(multipleEdges, i)
173  {
174  if (edFaces[i].size() > 2)
175  {
176  multipleEdges[i] = true;
177  }
178  }
179 
180  label nZones = surf.markZones(multipleEdges, faceZone);
181 
182  Info<< "Splitting remaining multiply connected parts" << endl;
183 
184  for (label z = 0; z < nZones; z++)
185  {
186 
187  boolList include(faceZone.size(), false);
188  labelList pointMap;
190 
191  forAll(faceZone, f)
192  {
193  if (faceZone[f] == z)
194  {
195  include[f] = true;
196  }
197  }
198 
199  triSurface zoneSurf = surf.subsetMesh(include, pointMap, faceMap);
200 
201 
202  fileName remainingPartFileName =
203  outFileBaseName
204  + "_multiplePart_"
205  + name(z)
206  + "." + outExtension;
207 
208  Info<< " Writing mulitple part "
209  << z << " to " << remainingPartFileName << endl;
210 
211  zoneSurf.write(remainingPartFileName);
212  }
213 
214  Info << "End\n" << endl;
215 
216  return 0;
217 }
218 
219 
220 // ************************************************************************* //
#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
A class for handling file names.
Definition: fileName.H:69
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
A class for handling words, derived from string.
Definition: word.H:59
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:268
labelList f(nPoints)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
#define WarningInFunction
Report a warning using Foam::Warning.
triSurface subsetMesh(const boolList &include, labelList &pointMap, labelList &faceMap) const
Return new surface. Returns pointMap, faceMap from.
Definition: triSurface.C:991
messageStream Info
virtual Ostream & write(const token &)=0
Write next token to stream.
Triangulated surface description with patch information.
Definition: triSurface.H:65
Foam::argList args(argc, argv)
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:157
Namespace for OpenFOAM.