OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

surfaceSmooth.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Example of simple laplacian smoother
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "triSurface.H"
00031 #include "argList.H"
00032 #include "OFstream.H"
00033 #include "boundBox.H"
00034 
00035 using namespace Foam;
00036 
00037 
00038 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00039 // Main program:
00040 
00041 int main(int argc, char *argv[])
00042 {
00043     argList::noParallel();
00044     argList::validOptions.clear();
00045     argList::validArgs.clear();
00046     argList::validArgs.append("surface file");
00047     argList::validArgs.append("underrelax factor (0..1)");
00048     argList::validArgs.append("iterations");
00049     argList::validArgs.append("output file");
00050     argList args(argc, argv);
00051 
00052     fileName surfFileName(args.additionalArgs()[0]);
00053     scalar relax(readScalar(IStringStream(args.additionalArgs()[1])()));
00054     if ((relax <= 0) || (relax > 1))
00055     {
00056         FatalErrorIn(args.executable()) << "Illegal relaxation factor "
00057             << relax << endl
00058             << "0: no change   1: move vertices to average of neighbours"
00059             << exit(FatalError);
00060     }
00061     label iters(readLabel(IStringStream(args.additionalArgs()[2])()));
00062     fileName outFileName(args.additionalArgs()[3]);
00063 
00064     Info<< "Relax:" << relax << endl;
00065     Info<< "Iters:" << iters << endl;
00066 
00067 
00068     Info<< "Reading surface from " << surfFileName << " ..." << endl;
00069 
00070     triSurface surf1(surfFileName);
00071 
00072     Info<< "Triangles    : " << surf1.size() << endl;
00073     Info<< "Vertices     : " << surf1.nPoints() << endl;
00074     Info<< "Bounding Box : " << boundBox(surf1.localPoints()) << endl;
00075 
00076     pointField newPoints(surf1.localPoints());
00077 
00078     const labelListList& pointEdges = surf1.pointEdges();
00079 
00080 
00081     for(label iter = 0; iter < iters; iter++)
00082     {
00083         forAll(pointEdges, vertI)
00084         {
00085             vector avgPos(vector::zero);
00086 
00087             const labelList& myEdges = pointEdges[vertI];
00088 
00089             forAll(myEdges, myEdgeI)
00090             {
00091                 const edge& e = surf1.edges()[myEdges[myEdgeI]];
00092 
00093                 label otherVertI = e.otherVertex(vertI);
00094 
00095                 avgPos += surf1.localPoints()[otherVertI];
00096             }
00097             avgPos /= myEdges.size();
00098 
00099             newPoints[vertI] = (1-relax)*newPoints[vertI] + relax*avgPos;
00100         }
00101     }
00102 
00103     triSurface surf2
00104     (
00105         surf1.localFaces(),
00106         surf1.patches(),
00107         newPoints
00108     );
00109 
00110     Info<< "Writing surface to " << outFileName << " ..." << endl;
00111 
00112     surf2.write(outFileName);
00113 
00114     Info << "End\n" << endl;
00115 
00116     return 0;
00117 }
00118 
00119 
00120 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd