primitiveMeshFindCell.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "primitiveMesh.H"
30 #include "cell.H"
31 #include "boundBox.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 Foam::boundBox Foam::primitiveMesh::cellBb(const label celli) const
36 {
37  if (hasCellPoints())
38  {
39  // No reduction!
40  return boundBox(points(), cellPoints()[celli], false);
41  }
42 
43  return boundBox(cells()[celli].box(points(), faces()));
44 }
45 
46 
48 (
49  const point& p,
50  label celli,
51  scalar inflationFraction
52 ) const
53 {
54  boundBox bb(cellBb(celli));
55 
56  if (inflationFraction > SMALL)
57  {
58  bb.inflate(inflationFraction);
59  }
60 
61  return bb.contains(p);
62 }
63 
64 
65 bool Foam::primitiveMesh::pointInCell(const point& p, label celli) const
66 {
67  const labelList& f = cells()[celli];
68  const labelList& owner = this->faceOwner();
69  const vectorField& cf = faceCentres();
70  const vectorField& Sf = faceAreas();
71 
72  forAll(f, facei)
73  {
74  label nFace = f[facei];
75  vector proj = p - cf[nFace];
76  vector normal = Sf[nFace];
77  if (owner[nFace] != celli)
78  {
79  normal = -normal;
80  }
81 
82  if ((normal & proj) > 0)
83  {
84  return false;
85  }
86  }
87 
88  return true;
89 }
90 
91 
92 Foam::label Foam::primitiveMesh::findNearestCell(const point& location) const
93 {
94  const vectorField& centres = cellCentres();
95 
96  if (!centres.size())
97  {
98  return -1;
99  }
100 
101  label nearestCelli = 0;
102  scalar minProximity = magSqr(centres[0] - location);
103 
104  for (label celli = 1; celli < centres.size(); celli++)
105  {
106  scalar proximity = magSqr(centres[celli] - location);
107 
108  if (proximity < minProximity)
109  {
110  nearestCelli = celli;
111  minProximity = proximity;
112  }
113  }
114 
115  return nearestCelli;
116 }
117 
118 
119 Foam::label Foam::primitiveMesh::findCell(const point& location) const
120 {
121  if (nCells() == 0)
122  {
123  return -1;
124  }
125 
126  // Find the nearest cell centre to this location
127  label celli = findNearestCell(location);
128 
129  // If point is in the nearest cell return
130  if (pointInCell(location, celli))
131  {
132  return celli;
133  }
134  else // point is not in the nearest cell so search all cells
135  {
136  bool cellFound = false;
137  label n = 0;
138 
139  while ((!cellFound) && (n < nCells()))
140  {
141  if (pointInCell(location, n))
142  {
143  cellFound = true;
144  celli = n;
145  }
146  else
147  {
148  n++;
149  }
150  }
151  if (cellFound)
152  {
153  return celli;
154  }
155  else
156  {
157  return -1;
158  }
159  }
160 }
161 
162 
163 // ************************************************************************* //
label findCell(const point &location) const
Find cell enclosing this location (-1 if not in mesh)
virtual const pointField & points() const =0
Return mesh points.
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
const cellList & cells() const
bool pointInCell(const point &p, label celli) const
Return true if the point is in the cell.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
const cellShapeList & cells
Vector< scalar > vector
Definition: vector.H:57
bool hasCellPoints() const noexcept
labelList f(nPoints)
vector point
Point is a vector.
Definition: point.H:37
virtual const faceList & faces() const =0
Return faces.
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
label n
Field< vector > vectorField
Specialisation of Field<T> for vector.
List< label > labelList
A List of labels.
Definition: List.H:62
volScalarField & p
boundBox cellBb(const label celli) const
The bounding box for given cell index.
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
const labelListList & cellPoints() const
bool pointInCellBB(const point &p, label celli, scalar inflationFraction=0) const
Return true if the point in the cell bounding box.