FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

surfaceAdd.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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Application
00025     surfaceAdd
00026 
00027 Description
00028     Add two surfaces. Does geometric merge on points. Does not check for
00029     overlapping/intersecting triangles.
00030 
00031     Keeps patches separate by renumbering.
00032 
00033 Usage
00034 
00035     - surfaceAdd [OPTIONS] <Foam surface file> <Foam surface file> <Foam output surface file>
00036 
00037     @param <Foam surface file> \n
00038     @todo Detailed description of argument.
00039 
00040     @param <Foam surface file> \n
00041     @todo Detailed description of argument.
00042 
00043     @param <Foam output surface file> \n
00044     @todo Detailed description of argument.
00045 
00046     @param -points <pointsFile>\n
00047     Add points from file.
00048 
00049     @param -mergeRegions \n
00050     Merge the regions.
00051 
00052     @param -case <dir>\n
00053     Case directory.
00054 
00055     @param -help \n
00056     Display help message.
00057 
00058     @param -doc \n
00059     Display Doxygen API documentation page for this application.
00060 
00061     @param -srcDoc \n
00062     Display Doxygen source documentation page for this application.
00063 
00064 \*---------------------------------------------------------------------------*/
00065 
00066 #include <OpenFOAM/argList.H>
00067 #include <OpenFOAM/fileName.H>
00068 #include <triSurface/triSurface.H>
00069 #include <OpenFOAM/OFstream.H>
00070 #include <OpenFOAM/IFstream.H>
00071 #include <OpenFOAM/triFace.H>
00072 #include <OpenFOAM/triFaceList.H>
00073 
00074 using namespace Foam;
00075 
00076 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00077 
00078 // Main program:
00079 
00080 int main(int argc, char *argv[])
00081 {
00082     argList::noParallel();
00083     argList::validArgs.clear();
00084     argList::validArgs.append("Foam surface file");
00085     argList::validArgs.append("Foam surface file");
00086     argList::validArgs.append("Foam output file");
00087     argList::validOptions.insert("points", "pointsFile");
00088     argList::validOptions.insert("mergeRegions", "");
00089     argList args(argc, argv);
00090 
00091     fileName inFileName1(args.additionalArgs()[0]);
00092     fileName inFileName2(args.additionalArgs()[1]);
00093     fileName outFileName(args.additionalArgs()[2]);
00094 
00095     bool addPoint     = args.optionFound("points");
00096     bool mergeRegions = args.optionFound("mergeRegions");
00097 
00098     if (addPoint)
00099     {
00100         Info<< "Reading a surface and adding points from a file"
00101             << "; merging the points and writing the surface to another file"
00102             << nl << endl;
00103 
00104         Info<< "Surface  : " << inFileName1<< nl
00105             << "Points   : " << args.option("points") << nl
00106             << "Writing  : " << outFileName << nl << endl;
00107     }
00108     else
00109     {
00110         Info<< "Reading two surfaces"
00111             << "; merging points and writing the surface to another file"
00112             << nl << endl;
00113 
00114         if (mergeRegions)
00115         {
00116             Info<< "Regions from the two files will get merged" << nl
00117                 << "Do not use this option if you want to keep the regions"
00118                 << " separate" << nl << endl;
00119         }
00120         else
00121         {
00122             Info<< "Regions from the two files will not get merged" << nl
00123                 << "Regions from " << inFileName2 << " will get offset so"
00124                 << " as not to overlap with the regions in " << inFileName1
00125                 << nl << endl;
00126         }
00127 
00128 
00129         Info<< "Surface1 : " << inFileName1<< nl
00130             << "Surface2 : " << inFileName2<< nl
00131             << "Writing  : " << outFileName << nl << endl;
00132     }
00133 
00134     const triSurface surface1(inFileName1);
00135 
00136     Info<< "Surface1:" << endl;
00137     surface1.writeStats(Info);
00138     Info<< endl;
00139 
00140     const pointField& points1 = surface1.points();
00141 
00142     // Final surface
00143     triSurface combinedSurf;
00144 
00145     if (addPoint)
00146     {
00147         IFstream pointsFile(args.option("points"));
00148         pointField extraPoints(pointsFile);
00149 
00150         Info<< "Additional Points:" << extraPoints.size() << endl;
00151 
00152         vectorField pointsAll(points1);
00153         label pointI = pointsAll.size();
00154         pointsAll.setSize(pointsAll.size() + extraPoints.size());
00155 
00156         forAll(extraPoints, i)
00157         {
00158             pointsAll[pointI++] = extraPoints[i];
00159         }
00160 
00161         combinedSurf = triSurface(surface1, surface1.patches(), pointsAll);
00162     }
00163     else
00164     {
00165         const triSurface surface2(inFileName2);
00166 
00167         Info<< "Surface2:" << endl;
00168         surface2.writeStats(Info);
00169         Info<< endl;
00170 
00171 
00172         // Make new storage
00173         List<labelledTri> facesAll(surface1.size() + surface2.size());
00174 
00175         const pointField& points2 = surface2.points();
00176 
00177         vectorField pointsAll(points1.size() + points2.size());
00178 
00179 
00180         label pointi = 0;
00181         // Copy points1 into pointsAll
00182         forAll(points1, point1i)
00183         {
00184             pointsAll[pointi++] = points1[point1i];
00185         }
00186         // Add surface2 points
00187         forAll(points2, point2i)
00188         {
00189             pointsAll[pointi++] = points2[point2i];
00190         }
00191 
00192 
00193         label trianglei = 0;
00194 
00195         // Copy triangles1 into trianglesAll
00196         forAll(surface1, faceI)
00197         {
00198             facesAll[trianglei++] = surface1[faceI];
00199         }
00200         label nRegions1 = surface1.patches().size();
00201 
00202 
00203         if (!mergeRegions)
00204         {
00205             Info<< "Surface " << inFileName1 << " has " << nRegions1
00206                 << " regions"
00207                 << nl
00208                 << "All region numbers in " << inFileName2 << " will be offset"
00209                 << " by this amount" << nl << endl;
00210         }
00211 
00212         // Add (renumbered) surface2 triangles
00213         forAll(surface2, faceI)
00214         {
00215             const labelledTri& tri = surface2[faceI];
00216 
00217             labelledTri& destTri = facesAll[trianglei++];
00218             destTri[0] = tri[0] + points1.size();
00219             destTri[1] = tri[1] + points1.size();
00220             destTri[2] = tri[2] + points1.size();
00221             if (mergeRegions)
00222             {
00223                 destTri.region() = tri.region();
00224             }
00225             else
00226             {
00227                 destTri.region() = tri.region() + nRegions1;
00228             }
00229         }
00230 
00231         label nRegions2 = surface2.patches().size();
00232 
00233         geometricSurfacePatchList newPatches;
00234 
00235         if (mergeRegions)
00236         {
00237             // Overwrite
00238             newPatches.setSize(max(nRegions1, nRegions2));
00239 
00240             forAll(surface1.patches(), patchI)
00241             {
00242                 newPatches[patchI] = surface1.patches()[patchI];
00243             }
00244             forAll(surface2.patches(), patchI)
00245             {
00246                 newPatches[patchI] = surface2.patches()[patchI];
00247             }
00248         }
00249         else
00250         {
00251             Info<< "Regions from " << inFileName2 << " have been renumbered:"
00252                 << nl
00253                 << "    old\tnew" << nl;
00254 
00255             for (label regionI = 0; regionI < nRegions2; regionI++)
00256             {
00257                 Info<< "    " << regionI << '\t' << regionI+nRegions1
00258                     << nl;
00259             }
00260             Info<< nl;
00261 
00262             newPatches.setSize(nRegions1 + nRegions2);
00263 
00264             label newPatchI = 0;
00265 
00266             forAll(surface1.patches(), patchI)
00267             {
00268                 newPatches[newPatchI++] = surface1.patches()[patchI];
00269             }
00270 
00271             forAll(surface2.patches(), patchI)
00272             {
00273                 newPatches[newPatchI++] = surface2.patches()[patchI];
00274             }
00275         }
00276 
00277 
00278         Info<< "New patches:" << nl;
00279         forAll(newPatches, patchI)
00280         {
00281             Info<< "    " << patchI << '\t' << newPatches[patchI].name() << nl;
00282         }
00283         Info<< endl;
00284 
00285 
00286         // Construct new surface mesh
00287         combinedSurf = triSurface(facesAll, newPatches, pointsAll);
00288     }
00289 
00290     // Merge all common points and do some checks
00291     combinedSurf.cleanup(true);
00292 
00293     Info<< "Merged surface:" << endl;
00294 
00295     combinedSurf.writeStats(Info);
00296 
00297     Info<< endl;
00298 
00299     Info << "Writing : " << outFileName << endl;
00300 
00301     // No need to 'group' while writing since all in correct order anyway.
00302     combinedSurf.write(outFileName);
00303 
00304     Info << "End\n" << endl;
00305 
00306     return 0;
00307 }
00308 
00309 
00310 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines