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

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