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

primitiveMeshFindCell.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "primitiveMesh.H"
00028 #include "cell.H"
00029 
00030 
00031 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00032 
00033 // Is the point in the cell bounding box
00034 bool Foam::primitiveMesh::pointInCellBB(const point& p, label celli) const
00035 {
00036     const pointField& points = this->points();
00037     const faceList& f = faces();
00038     const vectorField& centres = cellCentres();
00039     const cellList& cf = cells();
00040 
00041     labelList cellVertices = cf[celli].labels(f);
00042 
00043     vector bbmax = -GREAT*vector::one;
00044     vector bbmin = GREAT*vector::one;
00045 
00046     forAll (cellVertices, vertexI)
00047     {
00048         bbmax = max(bbmax, points[cellVertices[vertexI]]);
00049         bbmin = min(bbmin, points[cellVertices[vertexI]]);
00050     }
00051 
00052     scalar distance = mag(centres[celli] - p);
00053 
00054     if ((distance - mag(bbmax - bbmin)) < SMALL)
00055     {
00056         return true;
00057     }
00058     else
00059     {
00060         return false;
00061     }
00062 }
00063 
00064 
00065 // Is the point in the cell
00066 bool Foam::primitiveMesh::pointInCell(const point& p, label celli) const
00067 {
00068     const labelList& f = cells()[celli];
00069     const labelList& owner = this->faceOwner();
00070     const vectorField& cf = faceCentres();
00071     const vectorField& Sf = faceAreas();
00072 
00073     bool inCell = true;
00074 
00075     forAll(f, facei)
00076     {
00077         label nFace = f[facei];
00078         vector proj = p - cf[nFace];
00079         vector normal = Sf[nFace];
00080         if (owner[nFace] != celli)
00081         {
00082             normal = -normal;
00083         }
00084         inCell = inCell && ((normal & proj) <= 0);
00085     }
00086 
00087     return inCell;
00088 }
00089 
00090 
00091 // Find the cell with the nearest cell centre
00092 Foam::label Foam::primitiveMesh::findNearestCell(const point& location) const
00093 {
00094     const vectorField& centres = cellCentres();
00095 
00096     label nearestCelli = 0;
00097     scalar minProximity = magSqr(centres[0] - location);
00098 
00099     for (label celli = 1; celli < centres.size(); celli++)
00100     {
00101         scalar proximity = magSqr(centres[celli] - location);
00102 
00103         if (proximity < minProximity)
00104         {
00105             nearestCelli = celli;
00106             minProximity = proximity;
00107         }
00108     }
00109 
00110     return nearestCelli;
00111 }
00112 
00113 
00114 // Find cell enclosing this location
00115 Foam::label Foam::primitiveMesh::findCell(const point& location) const
00116 {
00117     if (nCells() == 0)
00118     {
00119         return -1;
00120     }
00121 
00122     // Find the nearest cell centre to this location
00123     label celli = findNearestCell(location);
00124 
00125     // If point is in the nearest cell return
00126     if (pointInCell(location, celli))
00127     {
00128         return celli;
00129     }
00130     else // point is not in the nearest cell so search all cells
00131     {
00132         bool cellFound = false;
00133         label n = 0;
00134 
00135         while ((!cellFound) && (n < nCells()))
00136         {
00137             if (pointInCell(location, n))
00138             {
00139                 cellFound = true;
00140                 celli = n;
00141             }
00142             else
00143             {
00144                 n++;
00145             }
00146         }
00147         if (cellFound)
00148         {
00149             return celli;
00150         }
00151         else
00152         {
00153             return -1;
00154         }
00155     }
00156 }
00157 
00158 
00159 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd