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

surfaceToCell.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 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "surfaceToCell.H"
00030 #include "polyMesh.H"
00031 #include "meshSearch.H"
00032 #include "triSurface.H"
00033 #include "triSurfaceSearch.H"
00034 #include "cellClassification.H"
00035 
00036 #include "addToRunTimeSelectionTable.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042 
00043 defineTypeNameAndDebug(surfaceToCell, 0);
00044 
00045 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, word);
00046 
00047 addToRunTimeSelectionTable(topoSetSource, surfaceToCell, istream);
00048 
00049 }
00050 
00051 
00052 Foam::topoSetSource::addToUsageTable Foam::surfaceToCell::usage_
00053 (
00054     surfaceToCell::typeName,
00055     "\n    Usage: surfaceToCell"
00056     "<surface> <outsidePoints> <cut> <inside> <outside> <near> <curvature>\n\n"
00057     "    <surface> name of triSurface\n"
00058     "    <outsidePoints> list of points that define outside\n"
00059     "    <cut> boolean whether to include cells cut by surface\n"
00060     "    <inside>   ,,                 ,,       inside surface\n"
00061     "    <outside>  ,,                 ,,       outside surface\n"
00062     "    <near> scalar; include cells with centre <= near to surface\n"
00063     "    <curvature> scalar; include cells close to strong curvature"
00064     " on surface\n"
00065     "    (curvature defined as difference in surface normal at nearest"
00066     " point on surface for each vertex of cell)\n\n"
00067 );
00068 
00069 
00070 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00071 
00072 Foam::label Foam::surfaceToCell::getNearest
00073 (
00074     const triSurfaceSearch& querySurf,
00075     const label pointI,
00076     const point& pt,
00077     const vector& span,
00078     Map<label>& cache
00079 )
00080 {
00081     Map<label>::const_iterator iter = cache.find(pointI);
00082 
00083     if (iter != cache.end())
00084     {
00085         // Found cached answer
00086         return iter();
00087     }
00088     else
00089     {
00090         pointIndexHit inter = querySurf.nearest(pt, span);
00091 
00092         // Triangle label (can be -1)
00093         label triI = inter.index();
00094 
00095         // Store triangle on point
00096         cache.insert(pointI, triI);
00097 
00098         return triI;
00099     }
00100 }
00101 
00102 
00103 // Return true if nearest surface to points on cell makes largish angle
00104 // with nearest surface to cell centre. Returns false otherwise. Points visited
00105 // are cached in pointToNearest
00106 bool Foam::surfaceToCell::differingPointNormals
00107 (
00108     const triSurfaceSearch& querySurf,
00109 
00110     const vector& span,         // current search span
00111     const label cellI,
00112     const label cellTriI,       // nearest (to cell centre) surface triangle 
00113 
00114     Map<label>& pointToNearest  // cache for nearest triangle to point
00115 ) const
00116 {
00117     const triSurface& surf = querySurf.surface();
00118     const vectorField& normals = surf.faceNormals();
00119 
00120     const faceList& faces = mesh().faces();
00121     const pointField& points = mesh().points();
00122 
00123     const labelList& cFaces = mesh().cells()[cellI];
00124 
00125     forAll(cFaces, cFaceI)
00126     {
00127         const face& f = faces[cFaces[cFaceI]];
00128 
00129         forAll(f, fp)
00130         {
00131             label pointI = f[fp];
00132 
00133             label pointTriI =
00134                 getNearest
00135                 (
00136                     querySurf,
00137                     pointI,
00138                     points[pointI],
00139                     span,
00140                     pointToNearest
00141                 );
00142 
00143             if (pointTriI != -1 && pointTriI != cellTriI)
00144             {
00145                 scalar cosAngle = normals[pointTriI] & normals[cellTriI];
00146 
00147                 if (cosAngle < 0.9)
00148                 {
00149                     return true;
00150                 }
00151             }
00152         }
00153     }
00154     return false;
00155 }
00156 
00157 
00158 void Foam::surfaceToCell::combine(topoSet& set, const bool add) const
00159 {
00160     cpuTime timer;
00161 
00162     if (includeCut_ || includeInside_ || includeOutside_)
00163     {
00164         //
00165         // Cut cells with surface and classify cells
00166         //
00167 
00168 
00169         // Construct search engine on mesh
00170 
00171         meshSearch queryMesh(mesh_, true);
00172 
00173 
00174         // Check all 'outside' points
00175         forAll(outsidePoints_, outsideI)
00176         {
00177             const point& outsidePoint = outsidePoints_[outsideI];
00178 
00179             // Find cell point is in. Linear search.
00180             if (queryMesh.findCell(outsidePoint, -1, false) == -1)
00181             {
00182                 FatalErrorIn("surfaceToCell::combine(topoSet&, const bool)")
00183                     << "outsidePoint " << outsidePoint
00184                     << " is not inside any cell"
00185                     << exit(FatalError);
00186             }
00187         }
00188 
00189         // Cut faces with surface and classify cells
00190 
00191         cellClassification cellType
00192         (
00193             mesh_,
00194             queryMesh,
00195             querySurf(),
00196             outsidePoints_
00197         );
00198 
00199 
00200         Info<< "    Marked inside/outside in = "
00201             << timer.cpuTimeIncrement() << " s" << endl << endl;
00202 
00203 
00204         forAll(cellType, cellI)
00205         {
00206             label cType = cellType[cellI];
00207 
00208             if
00209             (
00210                 (
00211                     includeCut_
00212                  && (cType == cellClassification::CUT)
00213                 )
00214              || (
00215                     includeInside_
00216                  && (cType == cellClassification::INSIDE)
00217                 )
00218              || (
00219                     includeOutside_
00220                  && (cType == cellClassification::OUTSIDE)
00221                 )
00222             )
00223             {
00224                 addOrDelete(set, cellI, add);
00225             }
00226         }
00227     }
00228 
00229 
00230     if (nearDist_ > 0)
00231     {
00232         //
00233         // Determine distance to surface
00234         //
00235 
00236         const pointField& ctrs = mesh_.cellCentres();
00237 
00238         // Box dimensions to search in octree.
00239         const vector span(nearDist_, nearDist_, nearDist_);
00240 
00241 
00242         if (curvature_ < -1)
00243         {
00244             Info<< "    Selecting cells with cellCentre closer than "
00245                 << nearDist_ << " to surface" << endl;
00246 
00247             // No need to test curvature. Insert near cells into set.
00248 
00249             forAll(ctrs, cellI)
00250             {
00251                 const point& c = ctrs[cellI];
00252 
00253                 pointIndexHit inter = querySurf().nearest(c, span);
00254 
00255                 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
00256                 {
00257                     addOrDelete(set, cellI, add);
00258                 }
00259             }
00260 
00261             Info<< "    Determined nearest surface point in = "
00262                 << timer.cpuTimeIncrement() << " s" << endl << endl;
00263 
00264         }
00265         else
00266         {
00267             // Test near cells for curvature
00268 
00269             Info<< "    Selecting cells with cellCentre closer than "
00270                 << nearDist_ << " to surface and curvature factor"
00271                 << " less than " << curvature_ << endl;
00272 
00273             // Cache for nearest surface triangle for a point
00274             Map<label> pointToNearest(mesh_.nCells()/10);
00275 
00276             forAll(ctrs, cellI)
00277             {
00278                 const point& c = ctrs[cellI];
00279 
00280                 pointIndexHit inter = querySurf().nearest(c, span);
00281 
00282                 if (inter.hit() && (mag(inter.hitPoint() - c) < nearDist_))
00283                 {
00284                     if
00285                     (
00286                         differingPointNormals
00287                         (
00288                             querySurf(),
00289                             span,
00290                             cellI,
00291                             inter.index(),      // nearest surface triangle
00292                             pointToNearest
00293                         )
00294                     )
00295                     {
00296                         addOrDelete(set, cellI, add);
00297                     }
00298                 }
00299             }
00300 
00301             Info<< "    Determined nearest surface point in = "
00302                 << timer.cpuTimeIncrement() << " s" << endl << endl;
00303         }
00304     }
00305 }
00306 
00307 
00308 void Foam::surfaceToCell::checkSettings() const
00309 {
00310     if
00311     (
00312         (nearDist_ < 0)
00313      && (curvature_ < -1)
00314      && (
00315             (includeCut_ && includeInside_ && includeOutside_)
00316          || (!includeCut_ && !includeInside_ && !includeOutside_)
00317         )
00318     )
00319     {
00320         FatalErrorIn
00321         (
00322             "surfaceToCell:checkSettings()"
00323         )   << "Illegal include cell specification."
00324             << " Result would be either all or no cells." << endl
00325             << "Please set one of includeCut, includeInside, includeOutside"
00326             << " to true, set nearDistance to a value > 0"
00327             << " or set curvature to a value -1 .. 1."
00328             << exit(FatalError);
00329     }
00330 }
00331 
00332 
00333 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00334 
00335 // Construct from components
00336 Foam::surfaceToCell::surfaceToCell
00337 (
00338     const polyMesh& mesh,
00339     const fileName& surfName,
00340     const pointField& outsidePoints,
00341     const bool includeCut,
00342     const bool includeInside,
00343     const bool includeOutside,
00344     const scalar nearDist,
00345     const scalar curvature
00346 )
00347 :
00348     topoSetSource(mesh),
00349     surfName_(surfName),
00350     outsidePoints_(outsidePoints),
00351     includeCut_(includeCut),
00352     includeInside_(includeInside),
00353     includeOutside_(includeOutside),
00354     nearDist_(nearDist),
00355     curvature_(curvature),
00356     surfPtr_(new triSurface(surfName_)),
00357     querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
00358     IOwnPtrs_(true)
00359 {
00360     checkSettings();
00361 }
00362 
00363 
00364 // Construct from components. Externally supplied surface.
00365 Foam::surfaceToCell::surfaceToCell
00366 (
00367     const polyMesh& mesh,
00368     const fileName& surfName,
00369     const triSurface& surf,
00370     const triSurfaceSearch& querySurf,
00371     const pointField& outsidePoints,
00372     const bool includeCut,
00373     const bool includeInside,
00374     const bool includeOutside,
00375     const scalar nearDist,
00376     const scalar curvature
00377 )
00378 :
00379     topoSetSource(mesh),
00380     surfName_(surfName),
00381     outsidePoints_(outsidePoints),
00382     includeCut_(includeCut),
00383     includeInside_(includeInside),
00384     includeOutside_(includeOutside),
00385     nearDist_(nearDist),
00386     curvature_(curvature),
00387     surfPtr_(&surf),
00388     querySurfPtr_(&querySurf),
00389     IOwnPtrs_(false)
00390 {
00391     checkSettings();
00392 }
00393 
00394 
00395 // Construct from dictionary
00396 Foam::surfaceToCell::surfaceToCell
00397 (
00398     const polyMesh& mesh,
00399     const dictionary& dict          
00400 )
00401 :
00402     topoSetSource(mesh),
00403     surfName_(dict.lookup("file")),
00404     outsidePoints_(dict.lookup("outsidePoints")),
00405     includeCut_(readBool(dict.lookup("includeCut"))),
00406     includeInside_(readBool(dict.lookup("includeInside"))),
00407     includeOutside_(readBool(dict.lookup("includeOutside"))),
00408     nearDist_(readScalar(dict.lookup("nearDistance"))),
00409     curvature_(readScalar(dict.lookup("curvature"))),
00410     surfPtr_(new triSurface(surfName_)),
00411     querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
00412     IOwnPtrs_(true)
00413 {
00414     checkSettings();
00415 }
00416 
00417 
00418 // Construct from Istream
00419 Foam::surfaceToCell::surfaceToCell
00420 (
00421     const polyMesh& mesh,
00422     Istream& is
00423 )
00424 :
00425     topoSetSource(mesh),
00426     surfName_(checkIs(is)),
00427     outsidePoints_(checkIs(is)),
00428     includeCut_(readBool(checkIs(is))),
00429     includeInside_(readBool(checkIs(is))),
00430     includeOutside_(readBool(checkIs(is))),
00431     nearDist_(readScalar(checkIs(is))),
00432     curvature_(readScalar(checkIs(is))),
00433     surfPtr_(new triSurface(surfName_)),
00434     querySurfPtr_(new triSurfaceSearch(*surfPtr_)),
00435     IOwnPtrs_(true)
00436 {
00437     checkSettings();
00438 }
00439 
00440 
00441 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00442 
00443 Foam::surfaceToCell::~surfaceToCell()
00444 {
00445     if (IOwnPtrs_)
00446     {
00447         if (surfPtr_)
00448         {
00449             delete surfPtr_;
00450         }
00451         if (querySurfPtr_)
00452         {
00453             delete querySurfPtr_;
00454         }
00455     }
00456 }
00457 
00458 
00459 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00460 
00461 void Foam::surfaceToCell::applyToSet
00462 (
00463     const topoSetSource::setAction action,
00464     topoSet& set
00465 ) const
00466 {
00467     if ( (action == topoSetSource::NEW) || (action == topoSetSource::ADD))
00468     {
00469         Info<< "    Adding cells in relation to surface " << surfName_
00470             << " ..." << endl;
00471 
00472         combine(set, true);
00473     }
00474     else if (action == topoSetSource::DELETE)
00475     {
00476         Info<< "    Removing cells in relation to surface " << surfName_
00477             << " ..." << endl;
00478 
00479         combine(set, false);
00480     }
00481 }
00482 
00483 
00484 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd