1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "primitiveMesh.H"
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 // Is the point in the cell bounding box
34 bool Foam::primitiveMesh::pointInCellBB(const point& p, label celli) const
36 const pointField& points = this->points();
37 const faceList& f = faces();
38 const vectorField& centres = cellCentres();
39 const cellList& cf = cells();
41 labelList cellVertices = cf[celli].labels(f);
43 vector bbmax = -GREAT*vector::one;
44 vector bbmin = GREAT*vector::one;
46 forAll (cellVertices, vertexI)
48 bbmax = max(bbmax, points[cellVertices[vertexI]]);
49 bbmin = min(bbmin, points[cellVertices[vertexI]]);
52 scalar distance = mag(centres[celli] - p);
54 if ((distance - mag(bbmax - bbmin)) < SMALL)
65 // Is the point in the cell
66 bool Foam::primitiveMesh::pointInCell(const point& p, label celli) const
68 const labelList& f = cells()[celli];
69 const labelList& owner = this->faceOwner();
70 const vectorField& cf = faceCentres();
71 const vectorField& Sf = faceAreas();
77 label nFace = f[facei];
78 vector proj = p - cf[nFace];
79 vector normal = Sf[nFace];
80 if (owner[nFace] != celli)
84 inCell = inCell && ((normal & proj) <= 0);
91 // Find the cell with the nearest cell centre
92 Foam::label Foam::primitiveMesh::findNearestCell(const point& location) const
94 const vectorField& centres = cellCentres();
96 label nearestCelli = 0;
97 scalar minProximity = magSqr(centres[0] - location);
99 for (label celli = 1; celli < centres.size(); celli++)
101 scalar proximity = magSqr(centres[celli] - location);
103 if (proximity < minProximity)
105 nearestCelli = celli;
106 minProximity = proximity;
114 // Find cell enclosing this location
115 Foam::label Foam::primitiveMesh::findCell(const point& location) const
122 // Find the nearest cell centre to this location
123 label celli = findNearestCell(location);
125 // If point is in the nearest cell return
126 if (pointInCell(location, celli))
130 else // point is not in the nearest cell so search all cells
132 bool cellFound = false;
135 while ((!cellFound) && (n < nCells()))
137 if (pointInCell(location, n))
159 // ************************************************************************* //