boundSphere.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 Class
25  Foam::boundSphere
26 
27 Description
28  The smallest sphere enclosing a given set of points
29 
30 SourceFiles
31  boundSphereI.H
32  boundSphereTemplates.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef boundSphere_H
37 #define boundSphere_H
38 
39 #include "point.H"
40 #include "nil.H"
41 #include "randomGenerator.H"
42 #include "RemoteData.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward declaration of friend functions and operators
50 
51 class boundSphere;
52 
53 bool operator==(const boundSphere&, const boundSphere&);
54 bool operator!=(const boundSphere&, const boundSphere&);
55 
56 Istream& operator>>(Istream&, boundSphere&);
57 Ostream& operator<<(Ostream&, const boundSphere&);
58 
59 /*---------------------------------------------------------------------------*\
60  Class boundSphere Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 class boundSphere
64 {
65  // Private Data
66 
67  //- The centre
68  point c_;
69 
70  //- The radius squared
71  scalar rSqr_;
72 
73 
74  // Private Type Definitions
75 
76  //- For a given List type, return the corresponding UList-type
77  template<class PointList>
78  using PointUList =
79  typename std::conditional
80  <
81  std::is_base_of<UList<point>, PointList>::value,
83  typename std::conditional
84  <
85  std::is_base_of<UIndirectList<point>, PointList>::value,
87  nil
88  >::type
89  >::type;
90 
91 
92  // Private Classes
93 
94  //- A structure to manage a fixed set of local points and a growing
95  // list of added remote points and provides indexing into these sets
96  // as if they were in a single contiguous list. It is used when
97  // constructing global bound spheres. A global bound sphere is
98  // calculated by first constructing the local bound sphere and then
99  // synchronising the limiting set of outermost points. These points
100  // are then added to the list and the process is repeated until
101  // no more points are added and all processes arrive at the same
102  // set of boundary points.
103  template<class PointList>
104  class LocalAndRemotePoints
105  {
106  const PointList& locals_;
107 
109 
111 
112  public:
113 
114  inline LocalAndRemotePoints(const PointList& locals)
115  :
116  locals_(locals)
117  {}
118 
119  inline label nRemotes() const
120  {
121  return remotes_.size();
122  }
123 
124  inline const point& operator[](const label i) const
125  {
126  return
127  i < locals_.size()
128  ? locals_[i]
129  : remotes_[i - locals_.size()].data;
130  }
131 
132  inline RemoteData<point> operator()(const label i) const
133  {
134  return
135  i < locals_.size()
136  ? RemoteData<point>(Pstream::myProcNo(), i, locals_[i])
137  : remotes_[i - locals_.size()];
138  }
139 
140  inline bool found(const remote& r) const
141  {
142  if (r.proci == Pstream::myProcNo()) return true;
143  return remoteTable_.found(r);
144  }
145 
146  inline label insert(const RemoteData<point>& rp)
147  {
148  if (rp.proci == Pstream::myProcNo()) return rp.elementi;
149  if (remoteTable_.found(rp)) return remoteTable_[rp];
150  return insertNoCheck(rp);
151  }
152 
153  inline label insertNoCheck(const RemoteData<point>& rp)
154  {
155  remotes_.append(rp);
156  remoteTable_.insert(rp, remotes_.size() - 1);
157  return locals_.size() + remotes_.size();
158  }
159  };
160 
161 
162  // Private Constructor
163 
164  //- Construct from components
165  inline boundSphere(const point& c, const scalar rSqr);
166 
167 
168  // Private Static Member Functions
169 
170  //- Cast a List to the corresponding UList-type
171  template<class PointList>
172  static inline PointUList<PointList> pointUList(const PointList&);
173 
174  //- Sort boundary point indices
175  static inline void sortBoundaryPis(FixedList<label, 4>&);
176 
177  //- Sort boundary remote point indices
178  template<class Remote>
179  static inline void sortBoundaryPis(FixedList<Remote, 4>&);
180 
181  //- ...
182  struct implementation
183  {
184 
185  //- Return the sphere intersecting the given set of up to four points
186  template<class PointList>
187  static boundSphere intersect
188  (
189  const PointList& ps,
190  const FixedList<label, 4>& pis,
191  const label nPs
192  );
193 
194  //- Return the sphere bounding the given set of up to four points
195  template<class PointList>
196  static boundSphere trivial
197  (
198  const PointList& ps,
199  const FixedList<label, 4>& pis,
200  const label nPs,
201  FixedList<label, 4>& boundaryPis
202  );
203 
204  //- Return the sphere bounding the given set of points using an
205  // inefficient brute-force method
206  template<class PointList>
207  static boundSphere bruteForce
208  (
209  const PointList& ps,
210  FixedList<label, 4>& boundaryPis
211  );
212 
213  //- Inner/recursive implementation of Welzl's algorithm
214  template<class PointList, class DLPermutation>
215  static boundSphere Welzl
216  (
217  const PointList& ps,
218  DLPermutation& pis,
219  const typename DLPermutation::const_iterator& pisEnd,
220  FixedList<label, 4>& boundaryPis,
221  const label nBoundaryPs
222  );
223 
224  //- Return the sphere bounding the given set of points using Welzl's
225  // algorithm. Constructs the local sphere containing the points on
226  // this processor only.
227  template<class PointList>
228  static boundSphere local
229  (
230  const PointList& ps,
232  FixedList<label, 4>& boundaryPis
233  );
234 
235  //- Return the sphere bounding the given set of points using Welzl's
236  // algorithm. Constructs the global sphere containing the points on
237  // all processors.
238  template<class PointList>
239  static boundSphere global
240  (
241  const PointList& ps,
243  FixedList<RemoteData<point>, 4>& boundaryPis,
244  const bool strict
245  );
246 
247  //- ...
248  };
249 
250 
251 public:
252 
253 
254  // Constructors
255 
256  //- Construct a null/invalid sphere
257  inline boundSphere();
258 
259  //- Construct given a point and a radius
260  static inline boundSphere cr(const point& c, const scalar r);
261 
262  //- Construct given a point and a radius
263  static inline boundSphere cr(const Tuple2<point, scalar>& cr);
264 
265  //- Construct given a point and a radius squared
266  static inline boundSphere crSqr(const point& c, const scalar rSqr);
267 
268  //- Construct given a point and a radius squared
269  static inline boundSphere crSqr(const Tuple2<point, scalar>& crSqr);
270 
271 
272  // Member Functions
273 
274  //- Return the centre
275  inline const point& c() const;
276 
277  //- Return the radius squared
278  inline scalar rSqr() const;
279 
280  //- Return the radius
281  inline scalar r() const;
282 
283  //- Return whether this sphere is valid
284  inline bool valid() const;
285 
286  //- Return whether this sphere contains a given point
287  inline bool contains(const point& p) const;
288 
289  //- Expand the sphere by the given factor
290  inline void inflate(const scalar s);
291 
292 
293  // Static Member Functions
294 
295  //- Return whether two spheres overlap
296  static inline bool overlap(const boundSphere& a, const boundSphere& b);
297 
298  //- Return the sphere intersecting the given set of up to four points
299  template<class PointList>
300  static inline boundSphere intersect
301  (
302  const PointList& ps,
303  const FixedList<label, 4>& pis,
304  const label nPs
305  );
306 
307  //- Return the sphere bounding the given set of up to four points
308  template<class PointList>
309  static inline boundSphere trivial
310  (
311  const PointList& ps,
312  const FixedList<label, 4>& pis,
313  const label nPs
314  );
315 
316  //- Return the sphere bounding the given set of up to four points
317  template<class PointList>
318  static inline boundSphere trivial
319  (
320  const PointList& ps,
321  const FixedList<label, 4>& pis,
322  const label nPs,
323  FixedList<label, 4>& boundaryPis
324  );
325 
326  //- Return the sphere bounding the given set of points using an
327  // inefficient brute-force method
328  template<class PointList>
329  static inline boundSphere bruteForce
330  (
331  const PointList& ps
332  );
333 
334  //- Return the sphere bounding the given set of points using an
335  // inefficient brute-force method
336  template<class PointList>
337  static inline boundSphere bruteForce
338  (
339  const PointList& ps,
340  FixedList<label, 4>& boundaryPis
341  );
342 
343  //- Return the sphere bounding the given set of points using Welzl's
344  // algorithm. Constructs the local sphere containing the points on
345  // this processor only.
346  template<class PointList>
347  static inline boundSphere local
348  (
349  const PointList& ps
350  );
351 
352  //- Return the sphere bounding the given set of points using Welzl's
353  // algorithm. Constructs the local sphere containing the points on
354  // this processor only.
355  template<class PointList>
356  static inline boundSphere local
357  (
358  const PointList& ps,
360  );
361 
362  //- Return the sphere bounding the given set of points using Welzl's
363  // algorithm. Constructs the local sphere containing the points on
364  // this processor only.
365  template<class PointList>
366  static inline boundSphere local
367  (
368  const PointList& ps,
369  FixedList<label, 4>& boundaryPis
370  );
371 
372  //- Return the sphere bounding the given set of points using Welzl's
373  // algorithm. Constructs the local sphere containing the points on
374  // this processor only.
375  template<class PointList>
376  static inline boundSphere local
377  (
378  const PointList& ps,
380  FixedList<label, 4>& boundaryPis
381  );
382 
383  //- Return the sphere bounding the given set of points using Welzl's
384  // algorithm. Constructs the global sphere containing the points on
385  // all processors.
386  template<class PointList>
387  static inline boundSphere global
388  (
389  const PointList& ps,
390  const bool strict = false
391  );
392 
393  //- Return the sphere bounding the given set of points using Welzl's
394  // algorithm. Constructs the global sphere containing the points on
395  // all processors.
396  template<class PointList>
397  static inline boundSphere global
398  (
399  const PointList& ps,
401  const bool strict = false
402  );
403 
404  //- Return the sphere bounding the given set of points using Welzl's
405  // algorithm. Constructs the global sphere containing the points on
406  // all processors.
407  template<class PointList>
408  static inline boundSphere global
409  (
410  const PointList& ps,
411  FixedList<RemoteData<point>, 4>& boundaryPis,
412  const bool strict = false
413  );
414 
415  //- Return the sphere bounding the given set of points using Welzl's
416  // algorithm. Constructs the global sphere containing the points on
417  // all processors.
418  template<class PointList>
419  static inline boundSphere global
420  (
421  const PointList& ps,
423  FixedList<RemoteData<point>, 4>& boundaryPis,
424  const bool strict = false
425  );
426 
427 
428  // Friend Operators
429 
430  //- Equality comparison
431  inline friend bool operator==(const boundSphere&, const boundSphere&);
432 
433  //- Inequality comparison
434  inline friend bool operator!=(const boundSphere&, const boundSphere&);
435 
436 
437  // IOstream operators
438 
439  //- Read from stream
441 
442  //- Write to stream
443  friend Ostream& operator<<(Ostream&, const boundSphere&);
444 };
445 
446 
447 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
448 
449 } // End namespace Foam
450 
451 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
452 
453 #include "boundSphereI.H"
454 
455 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
456 
457 #endif
458 
459 // ************************************************************************* //
bool found
A bi-directional constant iterator.
Definition: DLPermutation.H:73
A permutation stored in the same manner as a doubly-linked list in order to facilitate reordering in ...
Definition: DLPermutation.H:51
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
An STL-conforming hash table.
Definition: HashTable.H:127
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:138
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
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
A List with indirect addressing.
Definition: UIndirectList.H:61
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:149
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
static direction size()
Return the number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpaceI.H:83
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.
friend Istream & operator>>(Istream &, boundSphere &)
Read from stream.
friend bool operator==(const boundSphere &, const boundSphere &)
Equality comparison.
friend bool operator!=(const boundSphere &, const boundSphere &)
Inequality comparison.
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
friend Ostream & operator<<(Ostream &, const boundSphere &)
Write to stream.
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
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:59
Random number generator.
Struct for keeping processor, element (cell, face, point) index.
Definition: remote.H:57
label elementi
Element index.
Definition: remote.H:70
label proci
Processor index.
Definition: remote.H:67
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
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
Namespace for OpenFOAM.
bool operator!=(const particle &, const particle &)
Definition: particle.C:445
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
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 > &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
randomGenerator rndGen(653213)
volScalarField & p