boundSphereI.H
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) 2026 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 "boundSphere.H"
27 
28 // * * * * * * * * * * * * * * Private Constructors * * * * * * * * * * * * //
29 
30 inline Foam::boundSphere::boundSphere(const point& c, const scalar rSqr)
31 :
32  c_(c),
33  rSqr_(rSqr)
34 {}
35 
36 
37 // * * * * * * * * * * * Private Static Member Functions * * * * * * * * * * //
38 
39 template<class PointList>
40 inline Foam::boundSphere::PointUList<PointList>
41 Foam::boundSphere::pointUList(const PointList& ps)
42 {
43  return ps;
44 }
45 
46 
47 inline void Foam::boundSphere::sortBoundaryPis
48 (
49  FixedList<label, 4>& boundaryPis
50 )
51 {
52  for (label iter = 0; iter < 3; ++ iter)
53  {
54  for (label i = 0; i < 3; ++ i)
55  {
56  if
57  (
58  boundaryPis[i] == -1 ? true
59  : boundaryPis[i + 1] == -1 ? false
60  : boundaryPis[i] > boundaryPis[i + 1]
61  )
62  {
63  Swap(boundaryPis[i], boundaryPis[i + 1]);
64  }
65  }
66  }
67 }
68 
69 
70 template<class Remote>
71 inline void Foam::boundSphere::sortBoundaryPis
72 (
73  FixedList<Remote, 4>& boundaryPis
74 )
75 {
76  for (label iter = 0; iter < 3; ++ iter)
77  {
78  for (label i = 0; i < 3; ++ i)
79  {
80  if
81  (
82  boundaryPis[i].proci == -1 ? true
83  : boundaryPis[i + 1].proci == -1 ? false
84  : boundaryPis[i].proci > boundaryPis[i + 1].proci ? true
85  : boundaryPis[i].proci < boundaryPis[i + 1].proci ? false
86  : boundaryPis[i].elementi > boundaryPis[i + 1].elementi
87  )
88  {
89  Swap(boundaryPis[i], boundaryPis[i + 1]);
90  }
91  }
92  }
93 }
94 
95 
96 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
97 
99 :
100  c_(point::uniform(NaN)),
101  rSqr_(-vGreat)
102 {}
103 
104 
105 inline Foam::boundSphere Foam::boundSphere::cr(const point& c, const scalar r)
106 {
107  return r < 0 ? boundSphere() : boundSphere(c, sqr(r));
108 }
109 
110 
112 {
113  return
114  cr.second() < 0
115  ? boundSphere()
116  : boundSphere(cr.first(), sqr(cr.second()));
117 }
118 
119 
121 (
122  const point& c,
123  const scalar rSqr
124 )
125 {
126  return rSqr < 0 ? boundSphere() : boundSphere(c, rSqr);
127 }
128 
129 
131 (
132  const Tuple2<point, scalar>& crSqr
133 )
134 {
135  return
136  crSqr.second() < 0
137  ? boundSphere()
138  : boundSphere(crSqr.first(), crSqr.second());
139 }
140 
141 
142 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
143 
144 inline const Foam::point& Foam::boundSphere::c() const
145 {
146  return c_;
147 }
148 
149 
150 inline Foam::scalar Foam::boundSphere::rSqr() const
151 {
152  return rSqr_;
153 }
154 
155 
156 inline Foam::scalar Foam::boundSphere::r() const
157 {
158  return sqrt(rSqr_);
159 }
160 
161 
162 inline bool Foam::boundSphere::valid() const
163 {
164  return rSqr_ >= 0;
165 }
166 
167 
168 inline bool Foam::boundSphere::contains(const point& p) const
169 {
170  return valid() ? magSqr(p - c_) <= rSqr_ : false;
171 }
172 
173 
174 inline void Foam::boundSphere::inflate(const scalar s)
175 {
176  rSqr_ *= sqr(1 + s);
177 }
178 
179 
180 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
181 
183 (
184  const boundSphere& a,
185  const boundSphere& b
186 )
187 {
188  if (!a.valid() || !b.valid()) return false;
189 
190  return magSqr(a.c_ - b.c_) < sqr(a.r() + b.r());
191 }
192 
193 
194 template<class PointList>
196 (
197  const PointList& ps,
198  const FixedList<label, 4>& pis,
199  const label nPs
200 )
201 {
202  return implementation::intersect(pointUList(ps), pis, nPs);
203 }
204 
205 
206 template<class PointList>
208 (
209  const PointList& ps,
210  const FixedList<label, 4>& pis,
211  const label nPs
212 )
213 {
214  FixedList<label, 4> boundaryPis({-1, -1, -1, -1});
215 
216  return implementation::trivial(pointUList(ps), pis, nPs, boundaryPis);
217 }
218 
219 
220 template<class PointList>
222 (
223  const PointList& ps,
224  const FixedList<label, 4>& pis,
225  const label nPs,
226  FixedList<label, 4>& boundaryPis
227 )
228 {
229  return implementation::trivial(pointUList(ps), pis, nPs, boundaryPis);
230 }
231 
232 
233 template<class PointList>
235 (
236  const PointList& ps
237 )
238 {
239  FixedList<label, 4> boundaryPis({-1, -1, -1, -1});
240 
241  return implementation::bruteForce(pointUList(ps), boundaryPis);
242 }
243 
244 
245 template<class PointList>
247 (
248  const PointList& ps,
249  FixedList<label, 4>& boundaryPis
250 )
251 {
252  return implementation::bruteForce(pointUList(ps), boundaryPis);
253 }
254 
255 
256 template<class PointList>
257 inline Foam::boundSphere Foam::boundSphere::local
258 (
259  const PointList& ps
260 )
261 {
263  FixedList<label, 4> boundaryPis({-1, -1, -1, -1});
264 
265  return implementation::local(pointUList(ps), rndGen, boundaryPis);
266 }
267 
268 
269 template<class PointList>
270 inline Foam::boundSphere Foam::boundSphere::local
271 (
272  const PointList& ps,
273  randomGenerator& rndGen
274 )
275 {
276  FixedList<label, 4> boundaryPis({-1, -1, -1, -1});
277 
278  return implementation::local(pointUList(ps), rndGen, boundaryPis);
279 }
280 
281 
282 template<class PointList>
283 inline Foam::boundSphere Foam::boundSphere::local
284 (
285  const PointList& ps,
286  FixedList<label, 4>& boundaryPis
287 )
288 {
289  randomGenerator rndGen(0);
290 
291  return implementation::local(pointUList(ps), rndGen, boundaryPis);
292 }
293 
294 
295 template<class PointList>
296 inline Foam::boundSphere Foam::boundSphere::local
297 (
298  const PointList& ps,
299  randomGenerator& rndGen,
300  FixedList<label, 4>& boundaryPis
301 )
302 {
303  return implementation::local(pointUList(ps), rndGen, boundaryPis);
304 }
305 
306 
307 template<class PointList>
309 (
310  const PointList& ps,
311  const bool strict
312 )
313 {
315  FixedList<RemoteData<point>, 4> boundaryPis;
316 
317  return implementation::global(pointUList(ps), rndGen, boundaryPis, strict);
318 }
319 
320 
321 template<class PointList>
323 (
324  const PointList& ps,
326  const bool strict
327 )
328 {
329  FixedList<RemoteData<point>, 4> boundaryPis;
330 
331  return implementation::global(pointUList(ps), rndGen, boundaryPis, strict);
332 }
333 
334 
335 template<class PointList>
337 (
338  const PointList& ps,
339  FixedList<RemoteData<point>, 4>& boundaryPis,
340  const bool strict
341 )
342 {
344 
345  return implementation::global(pointUList(ps), rndGen, boundaryPis, strict);
346 }
347 
348 
349 template<class PointList>
351 (
352  const PointList& ps,
354  FixedList<RemoteData<point>, 4>& boundaryPis,
355  const bool strict
356 )
357 {
358  return implementation::global(pointUList(ps), rndGen, boundaryPis, strict);
359 }
360 
361 
362 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
363 
364 inline bool Foam::operator==(const boundSphere& a, const boundSphere& b)
365 {
366  return a.c_ == b.c_ && a.rSqr_ == b.rSqr_;
367 }
368 
369 
370 inline bool Foam::operator!=(const boundSphere& a, const boundSphere& b)
371 {
372  return !(a == b);
373 }
374 
375 
376 // ************************************************************************* //
Struct for keeping processor, element (cell, face, point) and a piece of data. Used for finding minim...
Definition: RemoteData.H:65
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
const Type2 & second() const
Return second.
Definition: Tuple2.H:131
const Type1 & first() const
Return first.
Definition: Tuple2.H:119
The smallest sphere enclosing a given set of points.
Definition: boundSphere.H:63
boundSphere()
Construct a null/invalid sphere.
Definition: boundSphereI.H:98
static bool overlap(const boundSphere &a, const boundSphere &b)
Return whether two spheres overlap.
Definition: boundSphereI.H:183
bool valid() const
Return whether this sphere is valid.
Definition: boundSphereI.H:162
scalar r() const
Return the radius.
Definition: boundSphereI.H:156
static boundSphere trivial(const PointList &ps, const FixedList< label, 4 > &pis, const label nPs)
Return the sphere bounding the given set of up to four points.
static boundSphere bruteForce(const PointList &ps)
Return the sphere bounding the given set of points using an.
static boundSphere crSqr(const point &c, const scalar rSqr)
Construct given a point and a radius squared.
Definition: boundSphereI.H:121
void inflate(const scalar s)
Expand the sphere by the given factor.
Definition: boundSphereI.H:174
static boundSphere intersect(const PointList &ps, const FixedList< label, 4 > &pis, const label nPs)
Return the sphere intersecting the given set of up to four points.
scalar rSqr() const
Return the radius squared.
Definition: boundSphereI.H:150
static boundSphere cr(const point &c, const scalar r)
Construct given a point and a radius.
Definition: boundSphereI.H:105
const point & c() const
Return the centre.
Definition: boundSphereI.H:144
static boundSphere global(const PointList &ps, const bool strict=false)
Return the sphere bounding the given set of points using Welzl's.
bool contains(const point &p) const
Return whether this sphere contains a given point.
Definition: boundSphereI.H:168
Random number generator.
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
volScalarField & b
Definition: createFields.H:27
bool valid(const PtrList< ModelType > &l)
const dimensionedScalar c
Speed of light in a vacuum.
bool operator!=(const particle &, const particle &)
Definition: particle.C:445
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
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
vector point
Point is a vector.
Definition: point.H:41
tmp< DimensionedField< typename outerProduct< Type, Type >::type, GeoMesh, Field >> sqr(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
void Swap(T &a, T &b)
Definition: Swap.H:43
void sqrt(LagrangianPatchField< scalar > &f, const LagrangianPatchField< scalar > &f1)
tmp< DimensionedField< scalar, GeoMesh, Field > > magSqr(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
randomGenerator rndGen(653213)
volScalarField & p