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-2022 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 from bound box min/max points
167  inline boundBox(const point& min, const point& max);
168 
171 
172  //- Construct from bound box min/max points
173  inline explicit boundBox(const Pair<point>& bb);
174 
175  //- Construct as the bounding box of the given points
176  // Does parallel communication (doReduce = true)
177  explicit boundBox(const UList<point>& points, bool doReduce = true);
178 
179  //- Construct as the bounding box of the given temporary pointField.
180  // Does parallel communication (doReduce = true)
181  explicit boundBox(const tmp<pointField>& tpoints, bool doReduce = true);
182 
183  //- Construct bounding box as an indirect subset of the points.
184  // The indices could be from cell/face etc.
185  // Does parallel communication (doReduce = true)
186  boundBox
187  (
188  const UList<point>& points,
189  const labelUList& indices,
190  bool doReduce = true
191  );
192 
193  //- Construct bounding box as an indirect subset of the points.
194  // The indices could be from edge/triFace etc.
195  // Does parallel communication (doReduce = true)
196  template<unsigned N>
197  boundBox
198  (
199  const UList<point>& points,
200  const FixedList<label, N>& indices,
201  bool doReduce = true
202  );
203 
204  //- Construct from Istream
205  inline explicit boundBox(Istream& is);
206 
207 
208  // Member Functions
209 
210  // Access
211 
212  //- Bounding box is inverted, contains no points.
213  inline bool empty() const;
214 
215  //- Bounding box is non-inverted.
216  inline bool good() const;
217 
218  //- Bounding box is non-inverted - same as good().
219  bool valid() const { return good(); }
220 
221  //- Minimum describing the bounding box
222  inline const point& min() const noexcept;
223 
224  //- Maximum describing the bounding box
225  inline const point& max() const noexcept;
226 
227  //- Minimum describing the bounding box, non-const access
228  inline point& min() noexcept;
229 
230  //- Maximum describing the bounding box, non-const access
231  inline point& max() noexcept;
232 
233  //- The centre (midpoint) of the bounding box
234  inline point centre() const;
235 
236  //- The bounding box span (from minimum to maximum)
237  inline vector span() const;
238 
239  //- The magnitude/length of the bounding box diagonal
240  inline scalar mag() const;
241 
242  //- The magnitude/length squared of bounding box diagonal
243  inline scalar magSqr() const;
244 
245  //- The volume of the bound box
246  inline scalar volume() const;
247 
248  //- Smallest length/height/width dimension
249  inline scalar minDim() const;
250 
251  //- Largest length/height/width dimension
252  inline scalar maxDim() const;
253 
254  //- Average length/height/width dimension
255  inline scalar avgDim() const;
256 
257  //- Direction (X/Y/Z) of the smallest span (for empty box: 0)
258  inline direction minDir() const;
259 
260  //- Direction (X/Y/Z) of the largest span (for empty box: 0)
261  inline direction maxDir() const;
262 
263  //- Count the number of positive, non-zero dimensions.
264  // \return
265  // - -1 : if any dimensions are negative
266  // - 0 : 0D (point)
267  // - 1 : 1D (line aligned with an axis)
268  // - 2 : 2D (plane aligned with an axis)
269  // - 3 : 3D (box)
270  inline int nDim() const;
272  //- Return corner point [0..7] corresponding to a 'hex' cell
273  template<direction CornerNumber>
274  inline point hexCorner() const;
275 
276  //- Corner points in an order corresponding to a 'hex' cell
277  tmp<pointField> hexCorners() const;
278 
279  //- Corner points in an order corresponding to a 'hex' cell
280  tmp<pointField> points() const { return hexCorners(); }
281 
282  //- Face midpoints
284 
285  //- Face centre of given face index
286  point faceCentre(const direction facei) const;
287 
288 
289  // Manipulate
290 
291  //- Reset to an inverted box
292  inline void reset();
293 
294  //- Reset min/max to be identical to the specified point
295  inline void reset(const point& pt);
296 
297  //- Reset min/max to specified values
298  inline void reset(const point& min, const point& max);
299 
300  //- Same as reset() - reset to an inverted box
301  void clear() { reset(); }
302 
303  //- Extend to include the second box.
304  inline void add(const boundBox& bb);
305 
306  //- Extend to include the point.
307  inline void add(const point& pt);
308 
309  //- Extend to include two additional points.
310  inline void add(const point& pt0, const point& pt1);
311 
312  //- Extend to include two additional points.
313  inline void add(const Pair<point>& points);
314 
315  //- Extend to include the points.
316  inline void add(const UList<point>& points);
317 
318  //- Extend to include the points from the temporary point field.
319  inline void add(const tmp<pointField>& tpoints);
320 
321  //- Extend to include the points.
322  template<unsigned N>
323  void add(const FixedList<point, N>& points);
324 
325  //- Extend to include a (subsetted) point field.
326  // The indices could be from edge/triFace etc.
327  template<unsigned N>
328  void add
329  (
330  const UList<point>& points,
331  const FixedList<label, N>& indices
332  );
333 
334  //- Extend to include a (subsetted) point field.
335  //
336  // \tparam IntContainer A container with an iterator that
337  // dereferences to an label
338  template<class IntContainer>
339  void add
340  (
341  const UList<point>& points,
342  const IntContainer& indices
343  );
344 
345  //- Expand box by adjusting min/max by specified amount
346  //- in each dimension
347  inline void grow(const scalar delta);
348 
349  //- Expand box by adjusting min/max by specified amounts
350  inline void grow(const vector& delta);
351 
352  //- Expand box by factor*mag(span) in all dimensions
353  inline void inflate(const scalar factor);
354 
355  //- Expand box slightly by expanding all dimensions with
356  //- factor*span*(random 0-1) and guarantees
357  //- factor*mag(span) minimum width in any direction
358  void inflate(Random& rndGen, const scalar factor);
359 
360  //- As per two parameter version but with additional
361  //- grow() by given amount in each dimension
362  void inflate(Random& r, const scalar factor, const scalar delta);
363 
364 
365  // Parallel
366 
367  //- Inplace parallel reduction of min/max values
368  void reduce();
370  //- Perform a reduction on a copy and return the result
371  static boundBox returnReduce(const boundBox& bb);
372 
373 
374  // Query
375 
376  //- Does plane intersect this bounding box.
377  // There is an intersection if the plane segments the corner points
378  // \note the results are unreliable when plane coincides almost
379  // exactly with a box face
380  bool intersects(const plane& pln) const;
381 
382  //- Does triangle intersect this bounding box
383  //- or is contained within this bounding box.
384  // \note results may be unreliable when it coincides almost
385  // exactly with a box face
386  bool intersects(const triPointRef& tri) const;
387 
388  //- Overlaps/touches boundingBox?
389  inline bool overlaps(const boundBox& bb) const;
390 
391  //- Overlaps boundingSphere (centre + sqr(radius))?
392  inline bool overlaps
393  (
394  const point& centre,
395  const scalar radiusSqr
396  ) const;
397 
398  //- Contains point? (inside or on edge)
399  inline bool contains(const point& pt) const;
400 
401  //- Fully contains other boundingBox?
402  inline bool contains(const boundBox& bb) const;
403 
404  //- Contains point? (inside only)
405  inline bool containsInside(const point& pt) const;
406 
407  //- Contains all points? (inside or on edge)
408  bool contains(const UList<point>& points) const;
409 
410  //- Contains all of the (subsetted) points? (inside or on edge)
411  template<unsigned N>
412  bool contains
413  (
414  const UList<point>& points,
415  const FixedList<label, N>& indices
416  ) const;
417 
418 
419  //- Contains all of the (subsetted) points? (inside or on edge)
420  //
421  // \tparam IntContainer A container with an iterator that
422  // dereferences to an label
423  template<class IntContainer>
424  bool contains
425  (
426  const UList<point>& points,
427  const IntContainer& indices
428  ) const;
429 
430 
431  //- Contains any of the points? (inside or on edge)
432  bool containsAny(const UList<point>& points) const;
433 
434  //- Contains any of the (subsetted) points? (inside or on edge)
435  template<unsigned N>
436  bool containsAny
437  (
438  const UList<point>& points,
439  const FixedList<label, N>& indices
440  ) const;
441 
442  //- Contains any of the (subsetted) points? (inside or on edge)
443  //
444  // \tparam IntContainer A container with an iterator that
445  // dereferences to an label
446  template<class IntContainer>
447  bool containsAny
448  (
449  const UList<point>& points,
450  const IntContainer& indices
451  ) const;
452 
453 
454  //- Return the nearest point on the boundBox to the supplied point.
455  // If point is inside the boundBox then the point is returned
456  // unchanged.
457  point nearest(const point& p) const;
458 
459 
460  // Member Operators
461 
462  //- Restrict min/max to union with other box.
463  void operator&=(const boundBox& bb);
464 
465  //- Extend box to include the second box, as per the add() method.
466  // Can be used in a reduction operation.
467  void operator+=(const boundBox& bb) { add(bb); }
468 
469 
470  // IOstream Operators
471 
472  friend Istream& operator>>(Istream& is, boundBox& bb);
473  friend Ostream& operator<<(Ostream& os, const boundBox& bb);
474 
475 
476  // Housekeeping
477 
478  //- Deprecated(2022-10) - use 'operator&=' to avoid confusion with
479  //- other intersects() methods.
480  // \deprecated(2022-10) - use 'operator&='
481  FOAM_DEPRECATED_FOR(2022-10, "boundBox::operator&=(const boundBox&)")
482  bool intersect(const boundBox& bb) { *this &= bb; return good(); }
483 
484  //- Identical to centre()
485  point midpoint() const { return centre(); }
486 };
487 
488 
489 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
490 
491 //- Contiguous data for boundBox
492 template<> struct is_contiguous<boundBox> : is_contiguous<point> {};
493 
494 //- Contiguous scalar data for boundBox
495 template<> struct is_contiguous_scalar<boundBox>
496 :
497  is_contiguous_scalar<point>
498 {};
499 
500 
501 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
502 
503 inline bool operator==(const boundBox& a, const boundBox& b)
504 {
505  return (a.min() == b.min() && a.max() == b.max());
506 }
507 
508 
509 inline bool operator!=(const boundBox& a, const boundBox& b)
510 {
511  return !(a == b);
512 }
513 
514 
515 inline bool operator<(const boundBox& a, const boundBox& b)
516 {
517  return
518  (
519  a.min() < b.min()
520  || (!(b.min() < a.min()) && a.max() < b.max())
521  );
522 }
523 
524 
525 inline bool operator<=(const boundBox& a, const boundBox& b)
526 {
527  return !(b < a);
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 !(a < b);
540 }
541 
542 
543 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
544 
545 } // End namespace Foam
546 
547 #include "boundBoxI.H"
548 
549 #ifdef NoRepository
550  #include "boundBoxTemplates.C"
551 #endif
552 
553 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
554 
555 #endif
556 
557 // ************************************************************************* //
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:374
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:101
bool contains(const point &pt) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:441
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:367
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:137
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:402
Random rndGen
Definition: createFields.H:23
int nDim() const
Count the number of positive, non-zero dimensions.
Definition: boundBoxI.H:267
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:640
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:155
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:221
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:197
const point & max() const noexcept
Maximum describing the bounding box.
Definition: boundBoxI.H:161
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:309
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:227
bool valid() const
Bounding box is non-inverted - same as good().
Definition: boundBox.H:271
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:191
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:99
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:55
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
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
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:369
bool good() const
Bounding box is non-inverted.
Definition: boundBoxI.H:149
point midpoint() const
Identical to centre()
Definition: boundBox.H:663
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:185
1: x-direction. Same as (1 << vector::X)
Definition: boundBox.H:111
scalar volume() const
The volume of the bound box.
Definition: boundBoxI.H:203
void reset()
Reset to an inverted box.
Definition: boundBoxI.H:288
direction maxDir() const
Direction (X/Y/Z) of the largest span (for empty box: 0)
Definition: boundBoxI.H:247
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:299
scalar maxDim() const
Largest length/height/width dimension.
Definition: boundBoxI.H:215
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:179
bool intersect(const boundBox &bb)
Deprecated(2022-10) - use &#39;operator&=&#39; to avoid confusion with other intersects() methods...
Definition: boundBox.H:658
friend Istream & operator>>(Istream &is, boundBox &bb)
bool overlaps(const boundBox &bb) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:425
void grow(const scalar delta)
Expand box by adjusting min/max by specified amount in each dimension.
Definition: boundBoxI.H:353
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:209
bool containsInside(const point &pt) const
Contains point? (inside only)
Definition: boundBoxI.H:458
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:389
Namespace for OpenFOAM.