initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / applications / utilities / surface / surfaceSmooth / surfaceSmooth.C
blobd2aa7593d9f52923013649fb2f985c67e9c9a4eb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 Description
26     Example of simple laplacian smoother
28 \*---------------------------------------------------------------------------*/
30 #include "triSurface.H"
31 #include "argList.H"
32 #include "OFstream.H"
33 #include "boundBox.H"
35 using namespace Foam;
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 // Main program:
41 int main(int argc, char *argv[])
43     argList::noParallel();
44     argList::validOptions.clear();
45     argList::validArgs.clear();
46     argList::validArgs.append("surface file");
47     argList::validArgs.append("underrelax factor (0..1)");
48     argList::validArgs.append("iterations");
49     argList::validArgs.append("output file");
50     argList args(argc, argv);
52     fileName surfFileName(args.additionalArgs()[0]);
53     scalar relax(readScalar(IStringStream(args.additionalArgs()[1])()));
54     if ((relax <= 0) || (relax > 1))
55     {
56         FatalErrorIn(args.executable()) << "Illegal relaxation factor "
57             << relax << endl
58             << "0: no change   1: move vertices to average of neighbours"
59             << exit(FatalError);
60     }
61     label iters(readLabel(IStringStream(args.additionalArgs()[2])()));
62     fileName outFileName(args.additionalArgs()[3]);
64     Info<< "Relax:" << relax << endl;
65     Info<< "Iters:" << iters << endl;
68     Info<< "Reading surface from " << surfFileName << " ..." << endl;
70     triSurface surf1(surfFileName);
72     Info<< "Triangles    : " << surf1.size() << endl;
73     Info<< "Vertices     : " << surf1.nPoints() << endl;
74     Info<< "Bounding Box : " << boundBox(surf1.localPoints()) << endl;
76     pointField newPoints(surf1.localPoints());
78     const labelListList& pointEdges = surf1.pointEdges();
81     for(label iter = 0; iter < iters; iter++)
82     {
83         forAll(pointEdges, vertI)
84         {
85             vector avgPos(vector::zero);
87             const labelList& myEdges = pointEdges[vertI];
89             forAll(myEdges, myEdgeI)
90             {
91                 const edge& e = surf1.edges()[myEdges[myEdgeI]];
93                 label otherVertI = e.otherVertex(vertI);
95                 avgPos += surf1.localPoints()[otherVertI];
96             }
97             avgPos /= myEdges.size();
99             newPoints[vertI] = (1-relax)*newPoints[vertI] + relax*avgPos;
100         }
101     }
103     triSurface surf2
104     (
105         surf1.localFaces(),
106         surf1.patches(),
107         newPoints
108     );
110     Info<< "Writing surface to " << outFileName << " ..." << endl;
112     surf2.write(outFileName);
114     Info << "End\n" << endl;
116     return 0;
120 // ************************************************************************* //