Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / applications / utilities / surface / surfaceSmooth / surfaceSmooth.C
blobb63c348024d89d43f859bf184e43ca21d7bda4cb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
26     surfaceSmooth
28 Description
29     Example of simple laplacian smoother
31 Usage
33     - surfaceSmooth [OPTIONS] \<Foam surface file\> \<underrelaxation factor (0..1)\> \<iterations\> \<Foam output surface file\>
35     @param \<Foam surface file\> \n
36     @todo Detailed description of argument.
38     @param \<underrelaxation factor (0..1)\> \n
39     @todo Detailed description of argument.
41     @param \<iterations\> \n
42     @todo Detailed description of argument.
44     @param \<Foam output surface file\> \n
45     @todo Detailed description of argument.
47     @param -help \n
48     Display help message.
50     @param -doc \n
51     Display Doxygen API documentation page for this application.
53     @param -srcDoc \n
54     Display Doxygen source documentation page for this application.
56 \*---------------------------------------------------------------------------*/
58 #include <triSurface/triSurface.H>
59 #include <OpenFOAM/argList.H>
60 #include <OpenFOAM/OFstream.H>
61 #include <OpenFOAM/boundBox.H>
63 using namespace Foam;
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 // Main program:
69 int main(int argc, char *argv[])
71     argList::noParallel();
72     argList::validOptions.clear();
73     argList::validArgs.clear();
74     argList::validArgs.append("surface file");
75     argList::validArgs.append("underrelax factor (0..1)");
76     argList::validArgs.append("iterations");
77     argList::validArgs.append("output file");
78     argList args(argc, argv);
80     fileName surfFileName(args.additionalArgs()[0]);
81     scalar relax(readScalar(IStringStream(args.additionalArgs()[1])()));
82     if ((relax <= 0) || (relax > 1))
83     {
84         FatalErrorIn(args.executable()) << "Illegal relaxation factor "
85             << relax << endl
86             << "0: no change   1: move vertices to average of neighbours"
87             << exit(FatalError);
88     }
89     label iters(readLabel(IStringStream(args.additionalArgs()[2])()));
90     fileName outFileName(args.additionalArgs()[3]);
92     Info<< "Relax:" << relax << endl;
93     Info<< "Iters:" << iters << endl;
96     Info<< "Reading surface from " << surfFileName << " ..." << endl;
98     triSurface surf1(surfFileName);
100     Info<< "Triangles    : " << surf1.size() << endl;
101     Info<< "Vertices     : " << surf1.nPoints() << endl;
102     Info<< "Bounding Box : " << boundBox(surf1.localPoints()) << endl;
104     pointField newPoints(surf1.localPoints());
106     const labelListList& pointEdges = surf1.pointEdges();
109     for(label iter = 0; iter < iters; iter++)
110     {
111         forAll(pointEdges, vertI)
112         {
113             vector avgPos(vector::zero);
115             const labelList& myEdges = pointEdges[vertI];
117             forAll(myEdges, myEdgeI)
118             {
119                 const edge& e = surf1.edges()[myEdges[myEdgeI]];
121                 label otherVertI = e.otherVertex(vertI);
123                 avgPos += surf1.localPoints()[otherVertI];
124             }
125             avgPos /= myEdges.size();
127             newPoints[vertI] = (1-relax)*newPoints[vertI] + relax*avgPos;
128         }
129     }
131     triSurface surf2
132     (
133         surf1.localFaces(),
134         surf1.patches(),
135         newPoints
136     );
138     Info<< "Writing surface to " << outFileName << " ..." << endl;
140     surf2.write(outFileName);
142     Info << "End\n" << endl;
144     return 0;
148 // ************************ vim: set sw=4 sts=4 et: ************************ //