boundBoxI.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) 2011-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 "boundBox.H"
27 #include "pointField.H"
28 
29 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
30 
32 {
33  return dict.found("box") || dict.found("min") || dict.found("max");
34 }
35 
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
40 :
41  min_(Zero),
42  max_(Zero)
43 {}
44 
45 
46 inline Foam::boundBox::boundBox(const point& min, const point& max)
47 :
48  min_(min),
49  max_(max)
50 {}
51 
52 
54 {
55  operator>>(is, *this);
56 }
57 
58 
60 {
61  const bool haveBox = dict.found("box");
62  const bool haveMinMax = dict.found("min") && dict.found("max");
63 
64  if (haveBox && !haveMinMax)
65  {
66  *this = dict.lookup<boundBox>("box", dimLength);
67  return;
68  }
69 
70  if (!haveBox && haveMinMax)
71  {
72  min_ = dict.lookup<point>("min", dimLength);
73  max_ = dict.lookup<point>("max", dimLength);
74  return;
75  }
76 
78  << (haveBox ? "both" : "neither")
79  << " of keywords box "
80  << (haveBox ? "and" : "or")
81  << " min/max defined in dictionary"
82  << dict.name() << exit(FatalIOError);
83 }
84 
85 
86 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
87 
88 inline const Foam::point& Foam::boundBox::min() const
89 {
90  return min_;
91 }
92 
93 
94 inline const Foam::point& Foam::boundBox::max() const
95 {
96  return max_;
97 }
98 
99 
101 {
102  return min_;
103 }
104 
105 
107 {
108  return max_;
109 }
110 
111 
113 {
114  return 0.5 * (max_ + min_);
115 }
116 
117 
119 {
120  return (max_ - min_);
121 }
122 
123 
124 inline Foam::scalar Foam::boundBox::mag() const
125 {
126  return ::Foam::mag(max_ - min_);
127 }
128 
129 
130 inline Foam::scalar Foam::boundBox::volume() const
131 {
132  return cmptProduct(span());
133 }
134 
135 
136 inline Foam::scalar Foam::boundBox::minDim() const
137 {
138  return cmptMin(span());
139 }
140 
141 
142 inline Foam::scalar Foam::boundBox::maxDim() const
143 {
144  return cmptMax(span());
145 }
146 
147 
148 inline Foam::scalar Foam::boundBox::avgDim() const
149 {
150  return cmptAv(span());
151 }
152 
153 
154 inline bool Foam::boundBox::overlaps(const boundBox& bb) const
155 {
156  return
157  (
158  bb.max_.x() >= min_.x() && bb.min_.x() <= max_.x()
159  && bb.max_.y() >= min_.y() && bb.min_.y() <= max_.y()
160  && bb.max_.z() >= min_.z() && bb.min_.z() <= max_.z()
161  );
162 }
163 
164 
166 (
167  const point& centre,
168  const scalar radiusSqr
169 ) const
170 {
171  // Find out where centre is in relation to bb.
172  // Find nearest point on bb.
173  scalar distSqr = 0;
174 
175  for (direction dir = 0; dir < vector::nComponents; dir++)
176  {
177  scalar d0 = min_[dir] - centre[dir];
178  scalar d1 = max_[dir] - centre[dir];
179 
180  if ((d0 > 0) != (d1 > 0))
181  {
182  // centre inside both extrema. This component does not add any
183  // distance.
184  }
185  else if (Foam::mag(d0) < Foam::mag(d1))
186  {
187  distSqr += d0*d0;
188  }
189  else
190  {
191  distSqr += d1*d1;
192  }
193 
194  if (distSqr > radiusSqr)
195  {
196  return false;
197  }
198  }
199 
200  return true;
201 }
202 
203 
204 inline bool Foam::boundBox::contains(const point& pt) const
205 {
206  return
207  (
208  pt.x() >= min_.x() && pt.x() <= max_.x()
209  && pt.y() >= min_.y() && pt.y() <= max_.y()
210  && pt.z() >= min_.z() && pt.z() <= max_.z()
211  );
212 }
213 
214 
215 // this.bb fully contains bb
216 inline bool Foam::boundBox::contains(const boundBox& bb) const
217 {
218  return contains(bb.min()) && contains(bb.max());
219 }
220 
221 
222 inline bool Foam::boundBox::containsInside(const point& pt) const
223 {
224  return
225  (
226  pt.x() > min_.x() && pt.x() < max_.x()
227  && pt.y() > min_.y() && pt.y() < max_.y()
228  && pt.z() > min_.z() && pt.z() < max_.z()
229  );
230 }
231 
232 
233 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
234 
235 inline bool Foam::operator==(const boundBox& a, const boundBox& b)
236 {
237  return (a.min_ == b.min_) && (a.max_ == b.max_);
238 }
239 
240 
241 inline bool Foam::operator!=(const boundBox& a, const boundBox& b)
242 {
243  return !(a == b);
244 }
245 
246 
247 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:105
const Cmpt & z() const
Definition: VectorI.H:87
const Cmpt & y() const
Definition: VectorI.H:81
const Cmpt & x() const
Definition: VectorI.H:75
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:60
static bool found(const dictionary &dict)
Does the dictionary have a bound box specified in it?
Definition: boundBoxI.H:31
const point & min() const
Minimum point defining the bounding box.
Definition: boundBoxI.H:88
const point & max() const
Maximum point defining the bounding box.
Definition: boundBoxI.H:94
scalar volume() const
The volume of the bound box.
Definition: boundBoxI.H:130
bool overlaps(const boundBox &) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:154
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:204
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:136
scalar mag() const
The magnitude of the bounding box span.
Definition: boundBoxI.H:124
scalar avgDim() const
Average length/height/width dimension.
Definition: boundBoxI.H:148
boundBox()
Construct null, setting points to zero.
Definition: boundBoxI.H:39
point midpoint() const
The midpoint of the bounding box.
Definition: boundBoxI.H:112
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:118
scalar maxDim() const
Largest length/height/width dimension.
Definition: boundBoxI.H:142
bool containsInside(const point &) const
Contains point? (inside only)
Definition: boundBoxI.H:222
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:669
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
volScalarField & b
Definition: createFields.H:27
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const zero Zero
Definition: zero.H:97
bool operator!=(const particle &, const particle &)
Definition: particle.C:445
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:528
const dimensionSet & dimLength
Definition: dimensions.C:141
void cmptMax(Field< typename Field< Type >::cmptType > &res, const UList< Type > &f)
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
void cmptMin(Field< typename Field< Type >::cmptType > &res, const UList< Type > &f)
IOerror FatalIOError
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh, PrimitiveField >::cmptType, GeoMesh, Field >> cmptAv(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
tmp< DimensionedField< scalar, GeoMesh, Field > > mag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
uint8_t direction
Definition: direction.H:45
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
dictionary dict