meshSearch.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-2025 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 "meshSearch.H"
27 #include "meshSearchBoundBox.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
34 }
35 
36 
37 // * * * * * * * * * * * * * * Protected Constructors * * * * * * * * * * * //
38 
40 :
42  <
43  polyMesh,
46  >(mesh),
47  cellTree_
48  (
49  treeDataCell(false, mesh),
51  8,
52  10,
53  scalar(5)
54  )
55 {}
56 
57 
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59 
61 (
62  const polyMesh& mesh,
64 )
65 {
66  if
67  (
70  )
71  {
72  // Force construction of face diagonals
73  (void)mesh.tetBasePtIs();
74  }
75 
76  return
78  <
79  polyMesh,
82  >::New(mesh);
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
87 
89 {}
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
95 {
96  pointIndexHit info =
97  cellTree().findNearest
98  (
99  p,
100  magSqr(cellTree().bb().max() - cellTree().bb().min())
101  );
102 
103  if (!info.hit())
104  {
105  info = cellTree().findNearest(p, Foam::sqr(great));
106  }
107 
108  return info.index();
109 }
110 
111 
113 (
114  const polyMesh& mesh,
115  const point& p
116 )
117 {
118  const pointField& cellCentres = mesh.cellCentres();
119 
120  label nearestCelli = -1;
121  scalar nearestDistSqr = vGreat;
122 
123  forAll(cellCentres, celli)
124  {
125  const scalar distSqr = magSqr(cellCentres[celli] - p);
126 
127  if (distSqr < nearestDistSqr)
128  {
129  nearestCelli = celli;
130  nearestDistSqr = distSqr;
131  }
132  }
133 
134  return nearestCelli;
135 }
136 
137 
139 (
140  const point& p
141 ) const
142 {
143  // Find the nearest cell
144  const label celli = findNearestCell(p);
145  if (celli < 0) return -1;
146 
147  const pointField& faceCentres = mesh().faceCentres();
148 
149  label nearestFacei = -1;
150  scalar nearestDistSqr = vGreat;
151 
152  // Check all faces of the nearest cell
153  const cell& cFaces = mesh().cells()[celli];
154 
155  forAll(cFaces, cFacei)
156  {
157  const label facei = cFaces[cFacei];
158 
159  const scalar distSqr = magSqr(faceCentres[facei] - p);
160 
161  if (distSqr < nearestDistSqr)
162  {
163  nearestFacei = facei;
164  nearestDistSqr = distSqr;
165  }
166  }
167 
168  return nearestFacei;
169 }
170 
171 
173 (
174  const point& p,
176 ) const
177 {
178  return cellTree().findInside(p, cellShapes);
179 }
180 
181 
183 (
184  const polyMesh& mesh,
185  const point& p,
187 )
188 {
189  // Find the nearest cell
190  const label celli = findNearestCellNoTree(mesh, p);
191  if (celli < 0) return -1;
192 
193  // If point is in the nearest cell return
194  if (pointInCell(p, mesh, celli, cellShapes))
195  {
196  return celli;
197  }
198 
199  // If the point is not in the nearest cell then search all cells
200  for (label celli = 0; celli < mesh.nCells(); celli++)
201  {
202  if (pointInCell(p, mesh, celli, cellShapes))
203  {
204  return celli;
205  }
206  }
207 
208  return -1;
209 }
210 
211 
212 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
MeshObject types:
Definition: MeshObjects.H:70
Templated abstract base-class for demand-driven mesh objects used to automate their allocation to the...
This class describes the interaction of (usually) a face and a point. It carries the info of a succes...
Definition: PointIndexHit.H:54
label index() const
Return index.
bool hit() const
Is there a hit.
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:60
Mesh object to hold a bound box for use by mesh search engines.
Mesh object that implements searches within the local cells and faces.
Definition: meshSearch.H:59
label findNearestCell(const point &p) const
Find the cell with centre closest to the given point.
Definition: meshSearch.C:94
label findCell(const point &p, const pointInCellShapes=pointInCellShapes::tets) const
Find the cell containing the given point.
Definition: meshSearch.C:173
label findNearestFace(const point &p) const
Find the face with centre closest to the given point.
Definition: meshSearch.C:139
static const meshSearch & New(const polyMesh &mesh, const pointInCellShapes=pointInCellShapes::tets)
Lookup or construct from mesh and cell decomposition option.
Definition: meshSearch.C:61
meshSearch(const polyMesh &mesh)
Construct from mesh.
Definition: meshSearch.C:39
virtual ~meshSearch()
Destructor.
Definition: meshSearch.C:88
static label findNearestCellNoTree(const polyMesh &mesh, const point &p)
Find the cell with centre closest to the given point. Do a.
Definition: meshSearch.C:113
static label findCellNoTree(const polyMesh &mesh, const point &p, const pointInCellShapes=pointInCellShapes::tets)
Find the cell containing the given point. Do a.
Definition: meshSearch.C:183
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
const labelIOList & tetBasePtIs() const
Return the tetBasePtIs.
Definition: polyMesh.C:1064
const vectorField & faceCentres() const
const vectorField & cellCentres() const
label nCells() const
const cellList & cells() const
Encapsulation of data needed to search in/for cells. Used to find the cell containing a point (e....
Definition: treeDataCell.H:56
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
const cellShapeList & cellShapes
Namespace for OpenFOAM.
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
pointInCellShapes
Enumeration for the sub-shapes used to perform the inside calculation.
Definition: pointInCell.H:52
tmp< DimensionedField< typename outerProduct< Type, Type >::type, GeoMesh, Field >> sqr(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
tmp< DimensionedField< scalar, GeoMesh, Field > > magSqr(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
bool pointInCell(const point &p, const polyMesh &mesh, const label celli, const pointInCellShapes=pointInCellShapes::tets)
Test if a point is in a given cell.
Definition: pointInCell.C:155
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
volScalarField & p