primitiveMeshFindCell.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "primitiveMesh.H"
27 #include "cell.H"
28 #include "boundBox.H"
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
33 (
34  const point& p,
35  label celli,
36  scalar inflationFraction
37 ) const
38 {
39  boundBox bb
40  (
41  cells()[celli].points
42  (
43  faces(),
44  points()
45  ),
46  false
47  );
48 
49  if (inflationFraction > small)
50  {
51  vector inflation = inflationFraction*vector::one*mag(bb.span());
52  bb = boundBox(bb.min() - inflation, bb.max() + inflation);
53  }
54 
55  return bb.contains(p);
56 }
57 
58 
59 bool Foam::primitiveMesh::pointInCell(const point& p, label celli) const
60 {
61  const labelList& f = cells()[celli];
62  const labelList& owner = this->faceOwner();
63  const vectorField& cf = faceCentres();
64  const vectorField& Sf = faceAreas();
65 
66  forAll(f, facei)
67  {
68  label nFace = f[facei];
69  vector proj = p - cf[nFace];
70  vector normal = Sf[nFace];
71  if (owner[nFace] != celli)
72  {
73  normal = -normal;
74  }
75 
76  if ((normal & proj) > 0)
77  {
78  return false;
79  }
80  }
81 
82  return true;
83 }
84 
85 
87 {
88  const vectorField& centres = cellCentres();
89 
90  label nearestCelli = 0;
91  scalar minProximity = magSqr(centres[0] - location);
92 
93  for (label celli = 1; celli < centres.size(); celli++)
94  {
95  scalar proximity = magSqr(centres[celli] - location);
96 
97  if (proximity < minProximity)
98  {
99  nearestCelli = celli;
100  minProximity = proximity;
101  }
102  }
103 
104  return nearestCelli;
105 }
106 
107 
109 {
110  if (nCells() == 0)
111  {
112  return -1;
113  }
114 
115  // Find the nearest cell centre to this location
116  label celli = findNearestCell(location);
117 
118  // If point is in the nearest cell return
119  if (pointInCell(location, celli))
120  {
121  return celli;
122  }
123  else // point is not in the nearest cell so search all cells
124  {
125  bool cellFound = false;
126  label n = 0;
127 
128  while ((!cellFound) && (n < nCells()))
129  {
130  if (pointInCell(location, n))
131  {
132  cellFound = true;
133  celli = n;
134  }
135  else
136  {
137  n++;
138  }
139  }
140  if (cellFound)
141  {
142  return celli;
143  }
144  else
145  {
146  return -1;
147  }
148  }
149 }
150 
151 
152 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
label findCell(const point &location) const
Find cell enclosing this location (-1 if not in mesh)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static const Form max
Definition: VectorSpace.H:115
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:170
label nCells() const
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:58
const cellList & cells() const
bool pointInCell(const point &p, label celli) const
Return true if the point is in the cell.
const cellShapeList & cells
const pointField & points
const vectorField & cellCentres() const
dimensioned< scalar > magSqr(const dimensioned< Type > &)
labelList f(nPoints)
const vectorField & faceCentres() const
const vectorField & faceAreas() const
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:84
virtual const labelList & faceOwner() const =0
Face face-owner addressing.
dimensioned< scalar > mag(const dimensioned< Type > &)
label n
mesh Sf()
bool pointInCellBB(const point &p, label celli, scalar inflationFraction=0) const
Return true if the point in the cell bounding box.