boundBox.H
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-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 Class
28  Foam::boundBox
29 
30 Description
31  A bounding box defined in terms of min/max extrema points.
32 
33 Note
34  When a bounding box is created without any points, it creates an inverted
35  bounding box. Points can be added later and the bounding box will grow to
36  include them.
37 
38 SeeAlso
39  Foam::treeBoundBox
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef Foam_boundBox_H
44 #define Foam_boundBox_H
45 
46 #include "pointField.H"
47 #include "faceList.H"
48 #include "Pair.H"
49 #include "triangleFwd.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward Declarations
57 class boundBox;
58 class plane;
59 class Random;
60 template<class T> class MinMax;
61 
62 Istream& operator>>(Istream& is, boundBox& bb);
64 
65 
66 /*---------------------------------------------------------------------------*\
67  Class boundBox Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class boundBox
71 {
72  // Private Data
73 
74  //- Minimum and maximum points describing the bounding box
75  point min_, max_;
76 
77 
78 protected:
79 
80  // Protected Member Functions
81 
82  //- Test for overlap of box and box (inclusive check)
83  inline static bool box_box_overlaps
84  (
85  const point& minA, // boxA (min)
86  const point& maxA, // boxA (max)
87  const point& minB, // boxB (min)
88  const point& maxB // boxB (max)
89  );
90 
91  //- Test for overlap of box and boundingSphere (centre + sqr(radius))
92  // Note: ordering of corners is irrelevant
93  inline static bool box_sphere_overlaps
94  (
95  const point& corner0, // box corner
96  const point& corner1, // box corner
97  const point& centre, // sphere centre
98  const scalar radiusSqr // sqr(radius)
99  );
100 
101 
102 public:
103 
104  // Static Data Members
105 
106  //- Bits used for (x/y/z) directional encoding.
107  enum directionBit : direction
108  {
109  XDIR = 1,
110  YDIR = 2,
111  ZDIR = 4
112  };
114  //- A large boundBox: min/max == -/+ ROOTVGREAT
115  static const boundBox greatBox;
116 
117  //- A large inverted boundBox: min/max == +/- ROOTVGREAT
118  static const boundBox invertedBox;
120  //- The unit normal per face
121  static const FixedList<vector, 6> faceNormals;
122 
123 
124  // Static Methods
125 
126  //- The null boundBox is the same as an inverted box
127  static const boundBox& null() noexcept
128  {
129  return invertedBox;
130  }
131 
132  //- The boundBox faces as a hexCell, using hexCorner points.
133  //- Same as hexCell::modelFaces()
134  static const Foam::faceList& hexFaces();
135 
136  //- Number of points for boundBox and HEX
137  static constexpr label nPoints() noexcept { return 8; }
138 
139  //- Number of edges for boundBox and HEX
140  static constexpr label nEdges() noexcept { return 12; }
141 
142  //- Number of faces for boundBox and HEX
143  static constexpr label nFaces() noexcept { return 6; }
144 
145 
146  // Standard (Generated) Methods
147 
148  //- Default construct: an inverted bounding box
149  inline boundBox();
150 
151  //- Copy construct
152  boundBox(const boundBox&) = default;
153 
154  //- Copy assignment
155  boundBox& operator=(const boundBox&) = default;
157 
158  // Constructors
159 
160  //- Copy construct with specified global reduction
161  boundBox(const boundBox& bb, bool doReduce);
162 
163  //- Construct a bounding box containing a single initial point
164  inline explicit boundBox(const point& p);
165 
166  //- Construct a 0/1 unit bounding box
167  inline explicit boundBox(const Foam::zero_one);
168 
169  //- Construct from bound box min/max points
170  inline boundBox(const point& min, const point& max);
171 
174 
175  //- Construct from bound box min/max points
176  inline explicit boundBox(const Pair<point>& bb);
177 
178  //- Construct as the bounding box of the given points
179  // Does parallel communication (doReduce = true)
180  explicit boundBox(const UList<point>& points, bool doReduce = true);
181 
182  //- Construct as the bounding box of the given temporary pointField.
183  // Does parallel communication (doReduce = true)
184  explicit boundBox(const tmp<pointField>& tpoints, bool doReduce = true);
185 
186  //- Construct bounding box as an indirect subset of the points.
187  // The indices could be from cell/face etc.
188  // Does parallel communication (doReduce = true)
189  boundBox
190  (
191  const UList<point>& points,
192  const labelUList& indices,
193  bool doReduce = true
194  );
195 
196  //- Construct bounding box as an indirect subset of the points.
197  // The indices could be from edge/triFace etc.
198  // Does parallel communication (doReduce = true)
199  template<unsigned N>
200  boundBox
201  (
202  const UList<point>& points,
203  const FixedList<label, N>& indices,
204  bool doReduce = true
205  );
206 
207  //- Construct from Istream
208  inline explicit boundBox(Istream& is);
209 
210 
211  // Member Functions
212 
213  // Access
214 
215  //- Bounding box is inverted, contains no points.
216  inline bool empty() const;
217 
218  //- Bounding box is non-inverted.
219  inline bool good() const;
220 
221  //- Bounding box is non-inverted - same as good().
222  bool valid() const { return good(); }
223 
224  //- Minimum describing the bounding box
225  inline const point& min() const noexcept;
226 
227  //- Maximum describing the bounding box
228  inline const point& max() const noexcept;
229 
230  //- Minimum describing the bounding box, non-const access
231  inline point& min() noexcept;
232 
233  //- Maximum describing the bounding box, non-const access
234  inline point& max() noexcept;
235 
236  //- The centre (midpoint) of the bounding box
237  inline point centre() const;
238 
239  //- The bounding box span (from minimum to maximum)
240  inline vector span() const;
241 
242  //- The magnitude/length of the bounding box diagonal
243  inline scalar mag() const;
244 
245  //- The magnitude/length squared of bounding box diagonal
246  inline scalar magSqr() const;
247 
248  //- The volume of the bound box
249  inline scalar volume() const;
250 
251  //- Smallest length/height/width dimension
252  inline scalar minDim() const;
253 
254  //- Largest length/height/width dimension
255  inline scalar maxDim() const;
256 
257  //- Average length/height/width dimension
258  inline scalar avgDim() const;
259 
260  //- Direction (X/Y/Z) of the smallest span (for empty box: 0)
261  inline direction minDir() const;
262 
263  //- Direction (X/Y/Z) of the largest span (for empty box: 0)
264  inline direction maxDir() const;
265 
266  //- Count the number of positive, non-zero dimensions.
267  // \return
268  // - -1 : if any dimensions are negative
269  // - 0 : 0D (point)
270  // - 1 : 1D (line aligned with an axis)
271  // - 2 : 2D (plane aligned with an axis)
272  // - 3 : 3D (box)
273  inline int nDim() const;
274 
275  //- Return corner point [0..7] corresponding to a 'hex' cell
276  template<direction CornerNumber>
277  inline point hexCorner() const;
278 
279  //- Corner points in an order corresponding to a 'hex' cell
280  tmp<pointField> hexCorners() const;
281 
282  //- Corner points in an order corresponding to a 'hex' cell
283  tmp<pointField> points() const { return hexCorners(); }
284 
285  //- Face midpoints
287 
288  //- Face centre of given face index
289  point faceCentre(const direction facei) const;
290 
291 
292  // Manipulate
293 
294  //- Reset to an inverted box
295  inline void reset();
296 
297  //- Reset to a 0/1 unit bounding box
298  inline void reset(const Foam::zero_one);
299 
300  //- Reset min/max to be identical to the specified point
301  inline void reset(const point& pt);
302 
303  //- Reset min/max to specified values
304  inline void reset(const point& min, const point& max);
305 
306  //- Same as reset() - reset to an inverted box
307  void clear() { reset(); }
308 
309  //- Extend to include the second box.
310  inline void add(const boundBox& bb);
311 
312  //- Extend to include the point.
313  inline void add(const point& pt);
314 
315  //- Extend to include two additional points.
316  inline void add(const point& pt0, const point& pt1);
317 
318  //- Extend to include two additional points.
319  inline void add(const Pair<point>& points);
320 
321  //- Extend to include the points.
322  inline void add(const UList<point>& points);
323 
324  //- Extend to include the points from the temporary point field.
325  inline void add(const tmp<pointField>& tpoints);
326 
327  //- Extend to include the points.
328  template<unsigned N>
329  void add(const FixedList<point, N>& points);
330 
331  //- Extend to include a (subsetted) point field.
332  // The indices could be from edge/triFace etc.
333  template<unsigned N>
334  void add
335  (
336  const UList<point>& points,
337  const FixedList<label, N>& indices
338  );
339 
340  //- Extend to include a (subsetted) point field.
341  //
342  // \tparam IntContainer A container with an iterator that
343  // dereferences to an label
344  template<class IntContainer>
345  void add
346  (
347  const UList<point>& points,
348  const IntContainer& indices
349  );
350 
351  //- Expand box by adjusting min/max by specified amount
352  //- in each dimension
353  inline void grow(const scalar delta);
354 
355  //- Expand box by adjusting min/max by specified amounts
356  inline void grow(const vector& delta);
357 
358  //- Expand box by factor*mag(span) in all dimensions
359  inline void inflate(const scalar factor);
360 
361  //- Expand box slightly by expanding all dimensions with
362  //- factor*span*(random 0-1) and guarantees
363  //- factor*mag(span) minimum width in any direction
364  void inflate(Random& rndGen, const scalar factor);
365 
366  //- As per two parameter version but with additional
367  //- grow() by given amount in each dimension
368  void inflate(Random& r, const scalar factor, const scalar delta);
369 
370 
371  // Parallel
372 
373  //- Inplace parallel reduction of min/max values
374  void reduce();
375 
376  //- Perform a reduction on a copy and return the result
377  static boundBox returnReduce(const boundBox& bb);
378 
379 
380  // Query
381 
382  //- Does plane intersect this bounding box.
383  // There is an intersection if the plane segments the corner points
384  // \note the results are unreliable when plane coincides almost
385  // exactly with a box face
386  bool intersects(const plane& pln) const;
387 
388  //- Does triangle intersect this bounding box
389  //- or is contained within this bounding box.
390  // \note results may be unreliable when it coincides almost
391  // exactly with a box face
392  bool intersects(const triPointRef& tri) const;
393 
394  //- Overlaps/touches boundingBox?
395  inline bool overlaps(const boundBox& bb) const;
396 
397  //- Overlaps boundingSphere (centre + sqr(radius))?
398  inline bool overlaps
399  (
400  const point& centre,
401  const scalar radiusSqr
402  ) const;
403 
404  //- Contains point? (inside or on edge)
405  inline bool contains(const point& pt) const;
406 
407  //- Fully contains other boundingBox?
408  inline bool contains(const boundBox& bb) const;
409 
410  //- Contains point? (inside only)
411  inline bool containsInside(const point& pt) const;
413  //- Contains all points? (inside or on edge)
414  bool contains(const UList<point>& points) const;
415 
416  //- Contains all of the (subsetted) points? (inside or on edge)
417  template<unsigned N>
418  bool contains
419  (
420  const UList<point>& points,
421  const FixedList<label, N>& indices
422  ) const;
423 
424 
425  //- Contains all of the (subsetted) points? (inside or on edge)
426  //
427  // \tparam IntContainer A container with an iterator that
428  // dereferences to an label
429  template<class IntContainer>
430  bool contains
431  (
432  const UList<point>& points,
433  const IntContainer& indices
434  ) const;
435 
436 
437  //- Contains any of the points? (inside or on edge)
438  bool containsAny(const UList<point>& points) const;
439 
440  //- Contains any of the (subsetted) points? (inside or on edge)
441  template<unsigned N>
442  bool containsAny
443  (
444  const UList<point>& points,
445  const FixedList<label, N>& indices
446  ) const;
447 
448  //- Contains any of the (subsetted) points? (inside or on edge)
449  //
450  // \tparam IntContainer A container with an iterator that
451  // dereferences to an label
452  template<class IntContainer>
453  bool containsAny
454  (
455  const UList<point>& points,
456  const IntContainer& indices
457  ) const;
458 
459 
460  //- Return the nearest point on the boundBox to the supplied point.
461  // If point is inside the boundBox then the point is returned
462  // unchanged.
463  point nearest(const point& p) const;
464 
465 
466  // Member Operators
467 
468  //- Restrict min/max to union with other box.
469  void operator&=(const boundBox& bb);
470 
471  //- Extend box to include the second box, as per the add() method.
472  // Can be used in a reduction operation.
473  void operator+=(const boundBox& bb) { add(bb); }
474 
475 
476  // IOstream Operators
477 
478  friend Istream& operator>>(Istream& is, boundBox& bb);
479  friend Ostream& operator<<(Ostream& os, const boundBox& bb);
480 
481 
482  // Housekeeping
483 
484  //- Deprecated(2022-10) - use 'operator&=' to avoid confusion with
485  //- other intersects() methods.
486  // \deprecated(2022-10) - use 'operator&='
487  FOAM_DEPRECATED_FOR(2022-10, "boundBox::operator&=(const boundBox&)")
488  bool intersect(const boundBox& bb) { *this &= bb; return good(); }
489 
490  //- Identical to centre()
491  point midpoint() const { return centre(); }
492 };
493 
494 
495 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
496 
497 //- Contiguous data for boundBox
498 template<> struct is_contiguous<boundBox> : is_contiguous<point> {};
499 
500 //- Contiguous scalar data for boundBox
501 template<> struct is_contiguous_scalar<boundBox>
502 :
503  is_contiguous_scalar<point>
504 {};
505 
506 
507 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
508 
509 inline bool operator==(const boundBox& a, const boundBox& b)
510 {
511  return (a.min() == b.min() && a.max() == b.max());
512 }
513 
514 
515 inline bool operator!=(const boundBox& a, const boundBox& b)
516 {
517  return !(a == b);
518 }
519 
520 
521 inline bool operator<(const boundBox& a, const boundBox& b)
522 {
523  return
524  (
525  a.min() < b.min()
526  || (!(b.min() < a.min()) && a.max() < b.max())
527  );
528 }
529 
530 
531 inline bool operator<=(const boundBox& a, const boundBox& b)
532 {
533  return !(b < a);
534 }
535 
536 
537 inline bool operator>(const boundBox& a, const boundBox& b)
538 {
539  return (b < a);
540 }
541 
542 
543 inline bool operator>=(const boundBox& a, const boundBox& b)
544 {
545  return !(a < b);
546 }
547 
548 
549 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
550 
551 } // End namespace Foam
552 
553 #include "boundBoxI.H"
554 
555 #ifdef NoRepository
556  #include "boundBoxTemplates.C"
557 #endif
558 
559 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
560 
561 #endif
562 
563 // ************************************************************************* //
boundBox()
Default construct: an inverted bounding box.
Definition: boundBoxI.H:101
scalar delta
static const boundBox & null() noexcept
The null boundBox is the same as an inverted box.
Definition: boundBox.H:137
static bool box_box_overlaps(const point &minA, const point &maxA, const point &minB, const point &maxB)
Test for overlap of box and box (inclusive check)
Definition: boundBoxI.H:388
A triangle primitive used to calculate face normals and swept volumes. Uses referred points...
Definition: triangle.H:217
uint8_t direction
Definition: direction.H:46
point hexCorner() const
Return corner point [0..7] corresponding to a &#39;hex&#39; cell.
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
bool contains(const point &pt) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:455
static const Foam::faceList & hexFaces()
The boundBox faces as a hexCell, using hexCorner points. Same as hexCell::modelFaces() ...
Definition: boundBox.C:57
void inflate(const scalar factor)
Expand box by factor*mag(span) in all dimensions.
Definition: boundBoxI.H:381
static constexpr label nEdges() noexcept
Number of edges for boundBox and HEX.
Definition: boundBox.H:156
bool empty() const
Bounding box is inverted, contains no points.
Definition: boundBoxI.H:144
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
directionBit
Bits used for (x/y/z) directional encoding.
Definition: boundBox.H:109
void clear()
Same as reset() - reset to an inverted box.
Definition: boundBox.H:412
Random rndGen
Definition: createFields.H:23
int nDim() const
Count the number of positive, non-zero dimensions.
Definition: boundBoxI.H:274
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:63
static boundBox returnReduce(const boundBox &bb)
Perform a reduction on a copy and return the result.
Definition: boundBox.C:185
static const FixedList< vector, 6 > faceNormals
The unit normal per face.
Definition: boundBox.H:129
void operator+=(const boundBox &bb)
Extend box to include the second box, as per the add() method.
Definition: boundBox.H:650
static const boundBox greatBox
A large boundBox: min/max == -/+ ROOTVGREAT.
Definition: boundBox.H:119
const point & min() const noexcept
Minimum describing the bounding box.
Definition: boundBoxI.H:162
Geometric class that creates a 3D plane and can return the intersection point between a line and the ...
Definition: plane.H:90
bool intersects(const plane &pln) const
Does plane intersect this bounding box.
Definition: boundBox.C:193
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
static constexpr label nPoints() noexcept
Number of points for boundBox and HEX.
Definition: boundBox.H:151
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
scalar avgDim() const
Average length/height/width dimension.
Definition: boundBoxI.H:228
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
void operator &=(const boundBox &bb)
Restrict min/max to union with other box.
scalar magSqr() const
The magnitude/length squared of bounding box diagonal.
Definition: boundBoxI.H:204
const point & max() const noexcept
Maximum describing the bounding box.
Definition: boundBoxI.H:168
tmp< pointField > hexCorners() const
Corner points in an order corresponding to a &#39;hex&#39; cell.
Definition: boundBox.C:122
void add(const boundBox &bb)
Extend to include the second box.
Definition: boundBoxI.H:323
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
friend Ostream & operator<<(Ostream &os, const boundBox &bb)
direction minDir() const
Direction (X/Y/Z) of the smallest span (for empty box: 0)
Definition: boundBoxI.H:234
bool valid() const
Bounding box is non-inverted - same as good().
Definition: boundBox.H:276
point nearest(const point &p) const
Return the nearest point on the boundBox to the supplied point.
Definition: boundBox.C:404
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Istream & operator>>(Istream &, directionInfo &)
scalar mag() const
The magnitude/length of the bounding box diagonal.
Definition: boundBoxI.H:198
boundBox & operator=(const boundBox &)=default
Copy assignment.
tmp< pointField > faceCentres() const
Face midpoints.
Definition: boundBox.C:140
Vector< scalar > vector
Definition: vector.H:57
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
void reduce()
Inplace parallel reduction of min/max values.
Definition: boundBox.C:178
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
static constexpr label nFaces() noexcept
Number of faces for boundBox and HEX.
Definition: boundBox.H:161
OBJstream os(runTime.globalPath()/outputName)
bool containsAny(const UList< point > &points) const
Contains any of the points? (inside or on edge)
Definition: boundBox.C:385
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:65
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
point faceCentre(const direction facei) const
Face centre of given face index.
Definition: boundBox.C:154
4: z-direction. Same as (1 << vector::Z)
Definition: boundBox.H:113
static const boundBox invertedBox
A large inverted boundBox: min/max == +/- ROOTVGREAT.
Definition: boundBox.H:124
vector point
Point is a vector.
Definition: point.H:37
tmp< pointField > points() const
Corner points in an order corresponding to a &#39;hex&#39; cell.
Definition: boundBox.H:374
bool good() const
Bounding box is non-inverted.
Definition: boundBoxI.H:156
point midpoint() const
Identical to centre()
Definition: boundBox.H:673
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:192
1: x-direction. Same as (1 << vector::X)
Definition: boundBox.H:111
scalar volume() const
The volume of the bound box.
Definition: boundBoxI.H:210
void reset()
Reset to an inverted box.
Definition: boundBoxI.H:295
direction maxDir() const
Direction (X/Y/Z) of the largest span (for empty box: 0)
Definition: boundBoxI.H:254
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
scalar maxDim() const
Largest length/height/width dimension.
Definition: boundBoxI.H:222
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
2: y-direction. Same as (1 << vector::Y)
Definition: boundBox.H:112
point centre() const
The centre (midpoint) of the bounding box.
Definition: boundBoxI.H:186
bool intersect(const boundBox &bb)
Deprecated(2022-10) - use &#39;operator&=&#39; to avoid confusion with other intersects() methods...
Definition: boundBox.H:668
friend Istream & operator>>(Istream &is, boundBox &bb)
bool overlaps(const boundBox &bb) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:439
void grow(const scalar delta)
Expand box by adjusting min/max by specified amount in each dimension.
Definition: boundBoxI.H:367
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:216
bool containsInside(const point &pt) const
Contains point? (inside only)
Definition: boundBoxI.H:472
static bool box_sphere_overlaps(const point &corner0, const point &corner1, const point &centre, const scalar radiusSqr)
Test for overlap of box and boundingSphere (centre + sqr(radius))
Definition: boundBoxI.H:403
Namespace for OpenFOAM.