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-2024 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
32 :
33  min_(Zero),
34  max_(Zero)
35 {}
36 
37 
38 inline Foam::boundBox::boundBox(const point& min, const point& max)
39 :
40  min_(min),
41  max_(max)
42 {}
43 
44 
46 :
47  min_(dict.lookup<point>("min", dimLength)),
48  max_(dict.lookup<point>("max", dimLength))
49 {}
50 
51 
53 {
54  operator>>(is, *this);
55 }
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 inline const Foam::point& Foam::boundBox::min() const
61 {
62  return min_;
63 }
64 
65 
66 inline const Foam::point& Foam::boundBox::max() const
67 {
68  return max_;
69 }
70 
71 
73 {
74  return min_;
75 }
76 
77 
79 {
80  return max_;
81 }
82 
83 
85 {
86  return 0.5 * (max_ + min_);
87 }
88 
89 
91 {
92  return (max_ - min_);
93 }
94 
95 
96 inline Foam::scalar Foam::boundBox::mag() const
97 {
98  return ::Foam::mag(max_ - min_);
99 }
100 
101 
102 inline Foam::scalar Foam::boundBox::volume() const
103 {
104  return cmptProduct(span());
105 }
106 
107 
108 inline Foam::scalar Foam::boundBox::minDim() const
109 {
110  return cmptMin(span());
111 }
112 
113 
114 inline Foam::scalar Foam::boundBox::maxDim() const
115 {
116  return cmptMax(span());
117 }
118 
119 
120 inline Foam::scalar Foam::boundBox::avgDim() const
121 {
122  return cmptAv(span());
123 }
124 
125 
126 inline bool Foam::boundBox::overlaps(const boundBox& bb) const
127 {
128  return
129  (
130  bb.max_.x() >= min_.x() && bb.min_.x() <= max_.x()
131  && bb.max_.y() >= min_.y() && bb.min_.y() <= max_.y()
132  && bb.max_.z() >= min_.z() && bb.min_.z() <= max_.z()
133  );
134 }
135 
136 
138 (
139  const point& centre,
140  const scalar radiusSqr
141 ) const
142 {
143  // Find out where centre is in relation to bb.
144  // Find nearest point on bb.
145  scalar distSqr = 0;
146 
147  for (direction dir = 0; dir < vector::nComponents; dir++)
148  {
149  scalar d0 = min_[dir] - centre[dir];
150  scalar d1 = max_[dir] - centre[dir];
151 
152  if ((d0 > 0) != (d1 > 0))
153  {
154  // centre inside both extrema. This component does not add any
155  // distance.
156  }
157  else if (Foam::mag(d0) < Foam::mag(d1))
158  {
159  distSqr += d0*d0;
160  }
161  else
162  {
163  distSqr += d1*d1;
164  }
165 
166  if (distSqr > radiusSqr)
167  {
168  return false;
169  }
170  }
171 
172  return true;
173 }
174 
175 
176 inline bool Foam::boundBox::contains(const point& pt) const
177 {
178  return
179  (
180  pt.x() >= min_.x() && pt.x() <= max_.x()
181  && pt.y() >= min_.y() && pt.y() <= max_.y()
182  && pt.z() >= min_.z() && pt.z() <= max_.z()
183  );
184 }
185 
186 
187 // this.bb fully contains bb
188 inline bool Foam::boundBox::contains(const boundBox& bb) const
189 {
190  return contains(bb.min()) && contains(bb.max());
191 }
192 
193 
194 inline bool Foam::boundBox::containsInside(const point& pt) const
195 {
196  return
197  (
198  pt.x() > min_.x() && pt.x() < max_.x()
199  && pt.y() > min_.y() && pt.y() < max_.y()
200  && pt.z() > min_.z() && pt.z() < max_.z()
201  );
202 }
203 
204 
205 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
206 
207 inline bool Foam::operator==(const boundBox& a, const boundBox& b)
208 {
209  return (a.min_ == b.min_) && (a.max_ == b.max_);
210 }
211 
212 
213 inline bool Foam::operator!=(const boundBox& a, const boundBox& b)
214 {
215  return !(a == b);
216 }
217 
218 
219 // ************************************************************************* //
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:59
const point & min() const
Minimum point defining the bounding box.
Definition: boundBoxI.H:60
const point & max() const
Maximum point defining the bounding box.
Definition: boundBoxI.H:66
scalar volume() const
The volume of the bound box.
Definition: boundBoxI.H:102
bool overlaps(const boundBox &) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:126
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:176
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:108
scalar mag() const
The magnitude of the bounding box span.
Definition: boundBoxI.H:96
scalar avgDim() const
Average length/height/width dimension.
Definition: boundBoxI.H:120
boundBox()
Construct null, setting points to zero.
Definition: boundBoxI.H:31
point midpoint() const
The midpoint of the bounding box.
Definition: boundBoxI.H:84
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:90
scalar maxDim() const
Largest length/height/width dimension.
Definition: boundBoxI.H:114
bool containsInside(const point &) const
Contains point? (inside only)
Definition: boundBoxI.H:194
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
volScalarField & b
Definition: createFields.H:25
static const zero Zero
Definition: zero.H:97
bool operator!=(const particle &, const particle &)
Definition: particle.C:1210
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
Cmpt cmptProduct(const VectorSpace< Form, Cmpt, Ncmpts > &vs)
Definition: VectorSpaceI.H:528
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
const dimensionSet dimLength
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh >::cmptType, GeoMesh >> cmptAv(const DimensionedField< Type, GeoMesh > &df)
Istream & operator>>(Istream &, pistonPointEdgeData &)
dimensioned< scalar > mag(const dimensioned< Type > &)
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
uint8_t direction
Definition: direction.H:45
dictionary dict