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

surfaceSubset.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     A surface analysis tool which sub-sets the triSurface
00027     to choose only a part of interest. Based on subsetMesh.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "triSurface.H"
00032 #include "argList.H"
00033 #include "OFstream.H"
00034 #include "IFstream.H"
00035 #include "Switch.H"
00036 #include "IOdictionary.H"
00037 #include "boundBox.H"
00038 #include "indexedOctree.H"
00039 #include "octree.H"
00040 #include "treeDataTriSurface.H"
00041 #include "Random.H"
00042 
00043 using namespace Foam;
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 // Main program:
00047 
00048 int main(int argc, char *argv[])
00049 {
00050     argList::noParallel();
00051     argList::validArgs.clear();
00052     argList::validArgs.append("surfaceSubsetDict");
00053     argList::validArgs.append("surface file");
00054     argList::validArgs.append("output file");
00055     argList args(argc, argv);
00056 
00057     Info<< "Reading dictionary " << args.additionalArgs()[0] << " ..." << endl;
00058     IFstream dictFile(args.additionalArgs()[0]);
00059     dictionary meshSubsetDict(dictFile);
00060 
00061     Info<< "Reading surface " << args.additionalArgs()[1] << " ..." << endl;
00062     triSurface surf1(args.additionalArgs()[1]);
00063 
00064     Info<< "Original:" << endl;
00065     surf1.writeStats(Info);
00066     Info<< endl;
00067 
00068 
00069     labelList markedPoints
00070     (
00071         meshSubsetDict.lookup("localPoints")
00072     );
00073 
00074     labelList markedEdges
00075     (
00076         meshSubsetDict.lookup("edges")
00077     );
00078 
00079     labelList markedFaces
00080     (
00081         meshSubsetDict.lookup("faces")
00082     );
00083 
00084     pointField markedZone
00085     (
00086         meshSubsetDict.lookup("zone")
00087     );
00088 
00089     if (markedZone.size() && markedZone.size() != 2)
00090     {
00091         FatalErrorIn(args.executable())
00092             << "zone specification should be two points, min and max of "
00093             << "the boundingbox" << endl
00094             << "zone:" << markedZone
00095             << exit(FatalError);
00096     }
00097 
00098     Switch addFaceNeighbours
00099     (
00100         meshSubsetDict.lookup("addFaceNeighbours")
00101     );
00102 
00103     Switch invertSelection
00104     (
00105         meshSubsetDict.lookup("invertSelection")
00106     );
00107 
00108     // Mark the cells for the subset
00109 
00110     // Faces to subset
00111     boolList facesToSubset(surf1.size(), false);
00112 
00113 
00114     //
00115     // pick up faces connected to "localPoints"
00116     //
00117 
00118     if (markedPoints.size())
00119     {
00120         Info << "Found " << markedPoints.size() << " marked point(s)." << endl;
00121 
00122         // pick up cells sharing the point
00123 
00124         forAll (markedPoints, pointI)
00125         {
00126             if
00127             (
00128                 markedPoints[pointI] < 0
00129              || markedPoints[pointI] >= surf1.nPoints()
00130             )
00131             {
00132                 FatalErrorIn(args.executable())
00133                     << "localPoint label " << markedPoints[pointI]
00134                     << "out of range."
00135                     << " The mesh has got "
00136                     << surf1.nPoints() << " localPoints."
00137                     << exit(FatalError);
00138             }
00139 
00140             const labelList& curFaces =
00141                 surf1.pointFaces()[markedPoints[pointI]];
00142 
00143             forAll (curFaces, i)
00144             {
00145                 facesToSubset[curFaces[i]] =  true;
00146             }
00147         }
00148     }
00149 
00150 
00151 
00152     //
00153     // pick up faces connected to "edges"
00154     //
00155 
00156     if (markedEdges.size())
00157     {
00158         Info << "Found " << markedEdges.size() << " marked edge(s)." << endl;
00159 
00160         // pick up cells sharing the edge
00161 
00162         forAll (markedEdges, edgeI)
00163         {
00164             if
00165             (
00166                 markedEdges[edgeI] < 0
00167              || markedEdges[edgeI] >= surf1.nEdges()
00168             )
00169             {
00170                 FatalErrorIn(args.executable())
00171                     << "edge label " << markedEdges[edgeI]
00172                     << "out of range."
00173                     << " The mesh has got "
00174                     << surf1.nEdges() << " edges."
00175                     << exit(FatalError);
00176             }
00177 
00178             const labelList& curFaces = surf1.edgeFaces()[markedEdges[edgeI]];
00179 
00180             forAll (curFaces, i)
00181             {
00182                 facesToSubset[curFaces[i]] =  true;
00183             }
00184         }
00185     }
00186 
00187 
00188     //
00189     // pick up faces with centre inside "zone"
00190     //
00191 
00192     if (markedZone.size() == 2)
00193     {
00194         const point& min = markedZone[0];
00195         const point& max = markedZone[1];
00196 
00197         Info << "Using zone min:" << min << " max:" << max << endl;
00198 
00199         forAll(surf1, faceI)
00200         {
00201             const labelledTri& f = surf1[faceI];
00202             const point centre = f.centre(surf1.points());
00203 
00204             if
00205             (
00206                 (centre.x() >= min.x())
00207              && (centre.y() >= min.y())
00208              && (centre.z() >= min.z())
00209              && (centre.x() <= max.x())
00210              && (centre.y() <= max.y())
00211              && (centre.z() <= max.z())
00212             )
00213             {
00214                 facesToSubset[faceI] = true;
00215             }
00216         }
00217     }
00218 
00219 
00220     //
00221     // pick up faces on certain side of surface
00222     //
00223 
00224     if (meshSubsetDict.found("surface"))
00225     {
00226         const dictionary& surfDict = meshSubsetDict.subDict("surface");
00227 
00228         fileName surfName(surfDict.lookup("name"));
00229 
00230         Switch outside(surfDict.lookup("outside"));
00231 
00232         if (outside)
00233         {
00234             Info<< "Selecting all triangles with centre outside surface "
00235                 << surfName << endl;
00236         }
00237         else
00238         {
00239             Info<< "Selecting all triangles with centre inside surface "
00240                 << surfName << endl;
00241         }
00242 
00243         // Read surface to select on
00244         triSurface selectSurf(surfName);
00245 
00246         // bb of surface
00247         treeBoundBox bb(selectSurf.localPoints());
00248 
00249         // Radnom number generator
00250         Random rndGen(354543);
00251 
00252         // search engine
00253         indexedOctree<treeDataTriSurface> selectTree
00254         (
00255             treeDataTriSurface(selectSurf),
00256             bb.extend(rndGen, 1E-4),    // slightly randomize bb
00257             8,      // maxLevel
00258             10,     // leafsize
00259             3.0     // duplicity
00260         );
00261 
00262         // Check if face (centre) is in outside or inside.
00263         forAll(facesToSubset, faceI)
00264         {
00265             if (!facesToSubset[faceI])
00266             {
00267                 const point fc(surf1[faceI].centre(surf1.points()));
00268 
00269                 indexedOctree<treeDataTriSurface>::volumeType t =
00270                     selectTree.getVolumeType(fc);
00271 
00272                 if (t == indexedOctree<treeDataTriSurface>::INSIDE && !outside)
00273                 {
00274                     facesToSubset[faceI] = true;
00275                 }
00276                 else if
00277                 (
00278                     t == indexedOctree<treeDataTriSurface>::OUTSIDE
00279                  && outside
00280                 )
00281                 {
00282                     facesToSubset[faceI] = true;
00283                 }
00284             }
00285         }
00286     }
00287 
00288 
00289     //
00290     // pick up specified "faces"
00291     //
00292 
00293     // Number of additional faces picked up because of addFaceNeighbours
00294     label nFaceNeighbours = 0;
00295 
00296     if (markedFaces.size())
00297     {
00298         Info << "Found " << markedFaces.size() << " marked face(s)." << endl;
00299 
00300         // Check and mark faces to pick up
00301         forAll (markedFaces, faceI)
00302         {
00303             if
00304             (
00305                 markedFaces[faceI] < 0
00306              || markedFaces[faceI] >= surf1.size()
00307             )
00308             {
00309                 FatalErrorIn(args.executable())
00310                     << "Face label " << markedFaces[faceI] << "out of range."
00311                     << " The mesh has got "
00312                     << surf1.size() << " faces."
00313                     << exit(FatalError);
00314             }
00315 
00316             // Mark the face
00317             facesToSubset[markedFaces[faceI]] = true;
00318 
00319             // mark its neighbours if requested
00320             if (addFaceNeighbours)
00321             {
00322                 const labelList& curFaces =
00323                     surf1.faceFaces()[markedFaces[faceI]];
00324 
00325                 forAll (curFaces, i)
00326                 {
00327                     label faceI = curFaces[i];
00328 
00329                     if (!facesToSubset[faceI])
00330                     {
00331                         facesToSubset[faceI] =  true;
00332                         nFaceNeighbours++;
00333                     }
00334                 }
00335             }
00336         }
00337     }
00338 
00339     if (addFaceNeighbours)
00340     {
00341         Info<< "Added " << nFaceNeighbours
00342             << " faces because of addFaceNeighbours" << endl;
00343     }
00344 
00345 
00346     if (invertSelection)
00347     {
00348         Info<< "Inverting selection." << endl;
00349         boolList newFacesToSubset(facesToSubset.size());
00350 
00351         forAll(facesToSubset, i)
00352         {
00353             if (facesToSubset[i])
00354             {
00355                 newFacesToSubset[i] = false;
00356             }
00357             else
00358             {
00359                 newFacesToSubset[i] = true;
00360             }
00361         }
00362         facesToSubset.transfer(newFacesToSubset);
00363     }
00364 
00365 
00366     // Create subsetted surface
00367     labelList pointMap;
00368     labelList faceMap;
00369     triSurface surf2
00370     (
00371         surf1.subsetMesh(facesToSubset, pointMap, faceMap)
00372     );
00373 
00374     Info<< "Subset:" << endl;
00375     surf2.writeStats(Info);
00376     Info << endl;
00377 
00378     fileName outFileName(args.additionalArgs()[2]);
00379 
00380     Info << "Writing surface to " << outFileName << endl;
00381 
00382     surf2.write(outFileName);
00383 
00384     return 0;
00385 }
00386 
00387 
00388 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd