treeDataCell.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 "treeDataCell.H"
27 #include "indexedOctree.H"
28 #include "polyMesh.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
35 }
36 
37 
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 
40 void Foam::treeDataCell::update()
41 {
42  if (cacheBb_)
43  {
44  bbs_.setSize(cellLabels_.size());
45 
46  forAll(cellLabels_, i)
47  {
48  bbs_[i] =
49  treeBoundBox
50  (
51  mesh_.cells()[cellLabels_[i]].bb
52  (
53  mesh_.points(),
54  mesh_.faces()
55  )
56  );
57  }
58  }
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
65 (
66  const bool cacheBb,
67  const polyMesh& mesh,
68  const labelUList& cellLabels
69 )
70 :
71  mesh_(mesh),
72  cellLabels_(cellLabels),
73  cacheBb_(cacheBb)
74 {
75  update();
76 }
77 
78 
80 (
81  const bool cacheBb,
82  const polyMesh& mesh,
83  labelList&& cellLabels
84 )
85 :
86  mesh_(mesh),
87  cellLabels_(move(cellLabels)),
88  cacheBb_(cacheBb)
89 {
90  update();
91 }
92 
93 
95 (
96  const bool cacheBb,
97  const polyMesh& mesh
98 )
99 :
100  mesh_(mesh),
101  cellLabels_(identityMap(mesh_.nCells())),
102  cacheBb_(cacheBb)
103 {
104  update();
105 }
106 
107 
109 (
110  const indexedOctree<treeDataCell>& tree
111 )
112 :
113  tree_(tree)
114 {}
115 
116 
118 (
119  const indexedOctree<treeDataCell>& tree
120 )
121 :
122  tree_(tree)
123 {}
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
129 {
130  pointField cc(cellLabels_.size());
131 
132  forAll(cellLabels_, i)
133  {
134  cc[i] = mesh_.cellCentres()[cellLabels_[i]];
135  }
136 
137  return cc;
138 }
139 
140 
142 (
143  const label index,
144  const treeBoundBox& cubeBb
145 ) const
146 {
147  if (cacheBb_)
148  {
149  return cubeBb.overlaps(bbs_[index]);
150  }
151  else
152  {
153  return cubeBb.overlaps
154  (
155  mesh_.cells()[cellLabels_[index]].bb
156  (
157  mesh_.points(),
158  mesh_.faces()
159  )
160  );
161  }
162 }
163 
164 
166 (
167  const label index,
168  const point& sample,
170 ) const
171 {
172  return pointInCell(sample, mesh_, cellLabels_[index], cellShapes);
173 }
174 
175 
176 void Foam::treeDataCell::findNearestOp::operator()
177 (
178  const labelUList& indices,
179  const point& sample,
180 
181  scalar& nearestDistSqr,
182  label& minIndex,
183  point& nearestPoint
184 ) const
185 {
186  const treeDataCell& shape = tree_.shapes();
187 
188  forAll(indices, i)
189  {
190  label index = indices[i];
191  label celli = shape.cellLabels()[index];
192  scalar distSqr = magSqr(sample - shape.mesh().cellCentres()[celli]);
193 
194  if (distSqr < nearestDistSqr)
195  {
196  nearestDistSqr = distSqr;
197  minIndex = index;
198  nearestPoint = shape.mesh().cellCentres()[celli];
199  }
200  }
201 }
202 
203 
204 void Foam::treeDataCell::findNearestOp::operator()
205 (
206  const labelUList& indices,
207  const linePointRef& ln,
208 
209  treeBoundBox& tightest,
210  label& minIndex,
211  point& linePoint,
212  point& nearestPoint
213 ) const
214 {
216 }
217 
218 
219 bool Foam::treeDataCell::findIntersectOp::operator()
220 (
221  const label index,
222  const point& start,
223  const point& end,
224  point& intersectionPoint
225 ) const
226 {
227  const treeDataCell& shape = tree_.shapes();
228 
229  // Do quick rejection test
230  if (shape.cacheBb_)
231  {
232  const treeBoundBox& cellBb = shape.bbs_[index];
233 
234  if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0)
235  {
236  // Start and end in same block outside of cellBb.
237  return false;
238  }
239  }
240  else
241  {
242  const treeBoundBox cellBb
243  (
244  shape.mesh_.cells()[shape.cellLabels_[index]].bb
245  (
246  shape.mesh_.points(),
247  shape.mesh_.faces()
248  )
249  );
250 
251  if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0)
252  {
253  // Start and end in same block outside of cellBb.
254  return false;
255  }
256  }
257 
258 
259  // Do intersection with all faces of cell
260  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
261 
262  // Disable picking up intersections behind us.
263  scalar oldTol = intersection::setPlanarTol(0.0);
264 
265  const cell& cFaces = shape.mesh_.cells()[shape.cellLabels_[index]];
266 
267  const vector dir(end - start);
268  scalar minDistSqr = magSqr(dir);
269  bool hasMin = false;
270 
271  forAll(cFaces, i)
272  {
273  const face& f = shape.mesh_.faces()[cFaces[i]];
274 
275  pointHit inter = f.ray
276  (
277  start,
278  dir,
279  shape.mesh_.points(),
281  );
282 
283  if (inter.hit() && sqr(inter.distance()) <= minDistSqr)
284  {
285  // Note: no extra test on whether intersection is in front of us
286  // since using half_ray AND zero tolerance. (note that tolerance
287  // is used to look behind us)
288  minDistSqr = sqr(inter.distance());
289  intersectionPoint = inter.hitPoint();
290  hasMin = true;
291  }
292  }
293 
294  // Restore picking tolerance
296 
297  return hasMin;
298 }
299 
300 
301 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
const Point & hitPoint() const
Return hit point.
Definition: PointHit.H:126
scalar distance() const
Return distance to hit.
Definition: PointHit.H:139
bool hit() const
Is there a hit.
Definition: PointHit.H:120
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:60
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:76
Non-pointer based hierarchical recursive searching.
Definition: indexedOctree.H:72
static scalar setPlanarTol(const scalar t)
Set the planar tolerance, returning the previous value.
Definition: intersection.H:89
A line primitive.
Definition: line.H:71
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1308
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1295
const vectorField & cellCentres() const
const cellList & cells() const
Standard boundBox + extra functionality for use in octree.
Definition: treeBoundBox.H:90
bool overlaps(const boundBox &) const
Overlaps other bounding box?
Definition: boundBoxI.H:154
direction posBits(const point &) const
Position of point relative to bounding box.
Definition: treeBoundBox.C:456
findIntersectOp(const indexedOctree< treeDataCell > &tree)
Definition: treeDataCell.C:118
findNearestOp(const indexedOctree< treeDataCell > &tree)
Definition: treeDataCell.C:109
Encapsulation of data needed to search in/for cells. Used to find the cell containing a point (e....
Definition: treeDataCell.H:56
bool overlaps(const label index, const treeBoundBox &sampleBb) const
Does (bb of) shape at index overlap bb.
Definition: treeDataCell.C:142
treeDataCell(const bool cacheBb, const polyMesh &, const labelUList &)
Construct from mesh and subset of cells.
Definition: treeDataCell.C:65
bool contains(const label index, const point &sample, const pointInCellShapes) const
Does shape at index contain sample?
Definition: treeDataCell.C:166
const labelList & cellLabels() const
Definition: treeDataCell.H:150
const polyMesh & mesh() const
Definition: treeDataCell.H:155
pointField shapePoints() const
Get representative point cloud for all shapes inside.
Definition: treeDataCell.C:128
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:381
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)
labelList identityMap(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
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)
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:908
labelList f(nPoints)