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

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