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 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
36(
37 const point& p,
38 label celli,
39 scalar inflationFraction
40) const
41{
42 boundBox bb
43 (
44 cells()[celli].points
45 (
46 faces(),
47 points()
48 ),
49 false
50 );
51
52 if (inflationFraction > SMALL)
53 {
54 bb.inflate(inflationFraction);
55 }
56
57 return bb.contains(p);
58}
59
60
61bool Foam::primitiveMesh::pointInCell(const point& p, label celli) const
62{
63 const labelList& f = cells()[celli];
64 const labelList& owner = this->faceOwner();
65 const vectorField& cf = faceCentres();
66 const vectorField& Sf = faceAreas();
67
68 forAll(f, facei)
69 {
70 label nFace = f[facei];
71 vector proj = p - cf[nFace];
72 vector normal = Sf[nFace];
73 if (owner[nFace] != celli)
74 {
75 normal = -normal;
76 }
77
78 if ((normal & proj) > 0)
79 {
80 return false;
81 }
82 }
83
84 return true;
85}
86
87
88Foam::label Foam::primitiveMesh::findNearestCell(const point& location) const
89{
90 const vectorField& centres = cellCentres();
91
92 if (!centres.size())
93 {
94 return -1;
95 }
96
97 label nearestCelli = 0;
98 scalar minProximity = magSqr(centres[0] - location);
99
100 for (label celli = 1; celli < centres.size(); celli++)
101 {
102 scalar proximity = magSqr(centres[celli] - location);
103
104 if (proximity < minProximity)
105 {
106 nearestCelli = celli;
107 minProximity = proximity;
108 }
109 }
110
111 return nearestCelli;
112}
113
114
115Foam::label Foam::primitiveMesh::findCell(const point& location) const
116{
117 if (nCells() == 0)
118 {
119 return -1;
120 }
121
122 // Find the nearest cell centre to this location
123 label celli = findNearestCell(location);
124
125 // If point is in the nearest cell return
126 if (pointInCell(location, celli))
127 {
128 return celli;
129 }
130 else // point is not in the nearest cell so search all cells
131 {
132 bool cellFound = false;
133 label n = 0;
134
135 while ((!cellFound) && (n < nCells()))
136 {
137 if (pointInCell(location, n))
138 {
139 cellFound = true;
140 celli = n;
141 }
142 else
143 {
144 n++;
145 }
146 }
147 if (cellFound)
148 {
149 return celli;
150 }
151 else
152 {
153 return -1;
154 }
155 }
156}
157
158
159// ************************************************************************* //
label n
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:64
bool contains(const point &pt) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:271
void inflate(const scalar s)
Inflate box by factor*mag(span) in all dimensions.
Definition: boundBox.C:175
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
bool pointInCell(const point &p, label celli) const
Return true if the point is in the cell.
virtual const faceList & faces() const =0
Return faces.
bool pointInCellBB(const point &p, label celli, scalar inflationFraction=0) const
Return true if the point in the cell bounding box.
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.
const cellList & cells() const
volScalarField & p
const cellShapeList & cells
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
labelList f(nPoints)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333