surfaceClean.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-2013 OpenFOAM Foundation
9  Copyright (C) 2020-2023 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 Application
28  surfaceClean
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Utility to clean surfaces.
35 
36  Current functionality
37  - removes baffles
38  - collapses small edges, removing triangles.
39  - converts sliver triangles into split edges by projecting point onto
40  base of triangle.
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #include "triSurface.H"
45 #include "argList.H"
46 #include "OFstream.H"
47 
48 #include "collapseBase.H"
49 #include "collapseEdge.H"
50 
51 using namespace Foam;
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 
56 int main(int argc, char *argv[])
57 {
59  (
60  "Clean surface by removing baffles, sliver faces,"
61  " collapsing small edges, etc."
62  );
63 
65  argList::addArgument("input", "The input surface file");
66  argList::addArgument("length", "The min length");
67  argList::addArgument("quality", "The min quality");
68  argList::addArgument("output", "The output surface file");
69 
71  (
72  "no-clean",
73  "Suppress surface checking/cleanup on the input surface"
74  );
75  argList::addOptionCompat("no-clean", {"noClean", -2006});
77 
79  (
80  "scale",
81  "factor",
82  "Input geometry scaling factor"
83  );
84  argList args(argc, argv);
85 
86  const auto inFileName = args.get<fileName>(1);
87  const auto minLen = args.get<scalar>(2);
88  const auto minQuality = args.get<scalar>(3);
89  const auto outFileName = args.get<fileName>(4);
90 
91  const int optVerbose = args.verbose();
92 
93  Info<< "Reading surface " << inFileName << nl
94  << "Collapsing all triangles with" << nl
95  << " edges or heights < " << minLen << nl
96  << " quality < " << minQuality << nl
97  << "Writing result to " << outFileName << nl << endl;
98 
99 
100  Info<< "Reading surface from " << inFileName << " ..." << nl << endl;
101 
102  triSurface surf
103  (
104  inFileName,
105  args.getOrDefault<scalar>("scale", -1)
106  );
107  surf.writeStats(Info);
108 
109  if (!args.found("no-clean"))
110  {
111  Info<< "Removing duplicate and illegal triangles ..." << nl << endl;
112  surf.cleanup(optVerbose);
113  }
114 
115  Info<< "Collapsing triangles to edges ..." << nl << endl;
116 
117  while (true)
118  {
119  const label nEdgeCollapse = collapseEdge(surf, minLen);
120 
121  if (nEdgeCollapse == 0)
122  {
123  break;
124  }
125  }
126  while (true)
127  {
128  const label nSplitEdge = collapseBase(surf, minLen, minQuality);
129 
130  if (nSplitEdge == 0)
131  {
132  break;
133  }
134  }
135 
136  Info<< nl
137  << "Resulting surface:" << endl;
138  surf.writeStats(Info);
139 
140  Info<< nl
141  << "Writing refined surface to " << outFileName << " ..." << endl;
142  surf.write(outFileName);
143 
144  Info<< "\nEnd\n" << endl;
145 
146  return 0;
147 }
148 
149 
150 // ************************************************************************* //
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
label collapseBase(triSurface &surf, const scalar minLen, const scalar minQuality)
Keep collapsing all triangles whose height is < minLen or quality < minQ.
A class for handling file names.
Definition: fileName.H:72
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
label collapseEdge(triSurface &surf, const scalar minLen)
Keep collapsing all edges < minLen.
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:418
Routines collapse sliver triangles by splitting the base edge.
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
static void addVerboseOption(const string &usage="", bool advanced=false)
Enable a &#39;verbose&#39; bool option, with usage information.
Definition: argList.C:520
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
Routines to collapse small edges.
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
messageStream Info
Information stream (stdout output on master, null elsewhere)
Triangulated surface description with patch information.
Definition: triSurface.H:71
Foam::argList args(argc, argv)
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:121
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.