Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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\*---------------------------------------------------------------------------*/
29#include "primitiveMesh.H"
30#include "cell.H"
31#include "boundBox.H"
32
33// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34
35Foam::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
65bool 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
92Foam::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;
113 }
114
115 return nearestCelli;
116}
117
118
119Foam::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 n
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A bounding box defined in terms of min/max extrema points.
Definition boundBox.H:71
bool contains(const point &pt) const
Contains point? (inside or on edge).
Definition boundBoxI.H:455
void inflate(const scalar factor)
Expand box by factor*mag(span) in all dimensions.
Definition boundBoxI.H:381
label findNearestCell(const point &location) const
Find the cell with the nearest cell centre to location.
virtual const labelList & faceOwner() const =0
Face face-owner addressing.
bool pointInCell(const point &p, label celli) const
Return true if the point is in the cell.
const vectorField & faceCentres() const
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.
const vectorField & cellCentres() const
label findCell(const point &location) const
Find cell enclosing this location (-1 if not in mesh).
const labelListList & cellPoints() const
label nCells() const noexcept
Number of mesh cells.
boundBox cellBb(const label celli) const
The bounding box for given cell index.
bool hasCellPoints() const noexcept
const vectorField & faceAreas() const
virtual const pointField & points() const =0
Return mesh points.
const cellList & cells() const
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
volScalarField & p
const cellShapeList & cells
List< label > labelList
A List of labels.
Definition List.H:62
Field< vector > vectorField
Specialisation of Field<T> for vector.
vector point
Point is a vector.
Definition point.H:37
Vector< scalar > vector
Definition vector.H:57
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:299