FreeFOAM The Cross-Platform CFD Toolkit
surfaceSmooth.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) 1991-2010 OpenCFD Ltd.
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 Application
25  surfaceSmooth
26 
27 Description
28  Example of simple laplacian smoother
29 
30 Usage
31 
32  - surfaceSmooth [OPTIONS] <Foam surface file> <underrelaxation factor (0..1)> <iterations> <Foam output surface file>
33 
34  @param <Foam surface file> \n
35  @todo Detailed description of argument.
36 
37  @param <underrelaxation factor (0..1)> \n
38  @todo Detailed description of argument.
39 
40  @param <iterations> \n
41  @todo Detailed description of argument.
42 
43  @param <Foam output surface file> \n
44  @todo Detailed description of argument.
45 
46  @param -help \n
47  Display help message.
48 
49  @param -doc \n
50  Display Doxygen API documentation page for this application.
51 
52  @param -srcDoc \n
53  Display Doxygen source documentation page for this application.
54 
55 \*---------------------------------------------------------------------------*/
56 
57 #include <triSurface/triSurface.H>
58 #include <OpenFOAM/argList.H>
59 #include <OpenFOAM/OFstream.H>
60 #include <OpenFOAM/boundBox.H>
61 
62 using namespace Foam;
63 
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 // Main program:
67 
68 int main(int argc, char *argv[])
69 {
73  argList::validArgs.append("surface file");
74  argList::validArgs.append("underrelax factor (0..1)");
75  argList::validArgs.append("iterations");
76  argList::validArgs.append("output file");
77  argList args(argc, argv);
78 
79  fileName surfFileName(args.additionalArgs()[0]);
81  if ((relax <= 0) || (relax > 1))
82  {
83  FatalErrorIn(args.executable()) << "Illegal relaxation factor "
84  << relax << endl
85  << "0: no change 1: move vertices to average of neighbours"
86  << exit(FatalError);
87  }
88  label iters(readLabel(IStringStream(args.additionalArgs()[2])()));
89  fileName outFileName(args.additionalArgs()[3]);
90 
91  Info<< "Relax:" << relax << endl;
92  Info<< "Iters:" << iters << endl;
93 
94 
95  Info<< "Reading surface from " << surfFileName << " ..." << endl;
96 
97  triSurface surf1(surfFileName);
98 
99  Info<< "Triangles : " << surf1.size() << endl;
100  Info<< "Vertices : " << surf1.nPoints() << endl;
101  Info<< "Bounding Box : " << boundBox(surf1.localPoints()) << endl;
102 
103  pointField newPoints(surf1.localPoints());
104 
105  const labelListList& pointEdges = surf1.pointEdges();
106 
107 
108  for(label iter = 0; iter < iters; iter++)
109  {
110  forAll(pointEdges, vertI)
111  {
112  vector avgPos(vector::zero);
113 
114  const labelList& myEdges = pointEdges[vertI];
115 
116  forAll(myEdges, myEdgeI)
117  {
118  const edge& e = surf1.edges()[myEdges[myEdgeI]];
119 
120  label otherVertI = e.otherVertex(vertI);
121 
122  avgPos += surf1.localPoints()[otherVertI];
123  }
124  avgPos /= myEdges.size();
125 
126  newPoints[vertI] = (1-relax)*newPoints[vertI] + relax*avgPos;
127  }
128  }
129 
130  triSurface surf2
131  (
132  surf1.localFaces(),
133  surf1.patches(),
134  newPoints
135  );
136 
137  Info<< "Writing surface to " << outFileName << " ..." << endl;
138 
139  surf2.write(outFileName);
140 
141  Info << "End\n" << endl;
142 
143  return 0;
144 }
145 
146 
147 // ************************ vim: set sw=4 sts=4 et: ************************ //