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 | Copyright (C) 2011-2015 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 
108 Foam::label Foam::primitiveMesh::findCell(const point& location) const
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 // ************************************************************************* //
cell.H
primitiveMesh.H
p
p
Definition: pEqn.H:62
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::primitiveMesh::faceOwner
virtual const labelList & faceOwner() const =0
Face face-owner addresing.
Foam::primitiveMesh::cells
const cellList & cells() const
Definition: primitiveMeshCells.C:136
Foam::primitiveMesh::pointInCell
bool pointInCell(const point &p, label celli) const
Is the point in the cell.
Definition: primitiveMeshFindCell.C:44
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::vectorField
Field< vector > vectorField
Specialisation of Field<T> for vector.
Definition: primitiveFieldsFwd.H:49
Foam::boundBox::span
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:84
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::primitiveMesh::findNearestCell
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
Definition: primitiveMeshFindCell.C:70
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::primitiveMesh::findCell
label findCell(const point &location) const
Find cell enclosing this location (-1 if not in mesh)
Definition: primitiveMeshFindCell.C:93
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
boundBox.H
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::boundBox::contains
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:170
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::primitiveMesh::faceCentres
const vectorField & faceCentres() const
Definition: primitiveMeshFaceCentresAndAreas.C:130
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::boundBox
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:55
Foam::primitiveMesh::pointInCellBB
bool pointInCellBB(const point &p, label celli) const
Is the point in the cell bounding box.
Definition: primitiveMeshFindCell.C:33
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
Foam::point
vector point
Point is a vector.
Definition: point.H:41
Foam::magSqr
dimensioned< scalar > magSqr(const dimensioned< Type > &)
normal
A normal distribution model.
Foam::primitiveMesh::faceAreas
const vectorField & faceAreas() const
Definition: primitiveMeshFaceCentresAndAreas.C:141