boundBox.C
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 "PstreamReduceOps.H"
28 #include "tmp.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 (
34  vector::rootMin/2,
35  vector::rootMax/2
36 );
37 
38 
40 (
41  vector::rootMax/2,
42  vector::rootMin/2
43 );
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
48 void Foam::boundBox::calculate(const UList<point>& points, const bool doReduce)
49 {
50  if (points.empty())
51  {
52  min_ = Zero;
53  max_ = Zero;
54 
55  if (doReduce && Pstream::parRun())
56  {
57  // Use values that get overwritten by reduce minOp, maxOp below
58  min_ = vector::rootMax/2;
59  max_ = vector::rootMin/2;
60  }
61  }
62  else
63  {
64  min_ = points[0];
65  max_ = points[0];
66 
67  for (label i = 1; i < points.size(); i++)
68  {
69  min_ = ::Foam::min(min_, points[i]);
70  max_ = ::Foam::max(max_, points[i]);
71  }
72  }
73 
74  // Reduce parallel information
75  if (doReduce)
76  {
77  reduce(min_, minOp<point>());
78  reduce(max_, maxOp<point>());
79  }
80 }
81 
82 
83 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
84 
85 Foam::boundBox::boundBox(const UList<point>& points, const bool doReduce)
86 :
87  min_(Zero),
88  max_(Zero)
89 {
90  calculate(points, doReduce);
91 }
92 
93 
94 Foam::boundBox::boundBox(const tmp<pointField>& points, const bool doReduce)
95 :
96  min_(Zero),
97  max_(Zero)
98 {
99  calculate(points(), doReduce);
100  points.clear();
101 }
102 
103 
105 (
106  const UList<point>& points,
107  const labelUList& indices,
108  const bool doReduce
109 )
110 :
111  min_(Zero),
112  max_(Zero)
113 {
114  if (points.empty() || indices.empty())
115  {
116  if (doReduce && Pstream::parRun())
117  {
118  // Use values that get overwritten by reduce minOp, maxOp below
119  min_ = vector::rootMax/2;
120  max_ = vector::rootMin/2;
121  }
122  }
123  else
124  {
125  min_ = points[indices[0]];
126  max_ = points[indices[0]];
127 
128  for (label i=1; i < indices.size(); ++i)
129  {
130  min_ = ::Foam::min(min_, points[indices[i]]);
131  max_ = ::Foam::max(max_, points[indices[i]]);
132  }
133  }
134 
135  // Reduce parallel information
136  if (doReduce)
137  {
138  reduce(min_, minOp<point>());
139  reduce(max_, maxOp<point>());
140  }
141 }
142 
143 
144 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
145 
147 {
149  pointField& pt = tPts.ref();
150 
151  pt[0] = min_; // min-x, min-y, min-z
152  pt[1] = point(max_.x(), min_.y(), min_.z()); // max-x, min-y, min-z
153  pt[2] = point(max_.x(), max_.y(), min_.z()); // max-x, max-y, min-z
154  pt[3] = point(min_.x(), max_.y(), min_.z()); // min-x, max-y, min-z
155  pt[4] = point(min_.x(), min_.y(), max_.z()); // min-x, min-y, max-z
156  pt[5] = point(max_.x(), min_.y(), max_.z()); // max-x, min-y, max-z
157  pt[6] = max_; // max-x, max-y, max-z
158  pt[7] = point(min_.x(), max_.y(), max_.z()); // min-x, max-y, max-z
159 
160  return tPts;
161 }
162 
163 
165 {
166  faceList faces(6);
167 
168  forAll(faces, fI)
169  {
170  faces[fI].setSize(4);
171  }
172 
173  faces[0][0] = 0;
174  faces[0][1] = 1;
175  faces[0][2] = 2;
176  faces[0][3] = 3;
177 
178  faces[1][0] = 2;
179  faces[1][1] = 6;
180  faces[1][2] = 7;
181  faces[1][3] = 3;
182 
183  faces[2][0] = 0;
184  faces[2][1] = 4;
185  faces[2][2] = 5;
186  faces[2][3] = 1;
187 
188  faces[3][0] = 4;
189  faces[3][1] = 7;
190  faces[3][2] = 6;
191  faces[3][3] = 5;
192 
193  faces[4][0] = 3;
194  faces[4][1] = 7;
195  faces[4][2] = 4;
196  faces[4][3] = 0;
197 
198  faces[5][0] = 1;
199  faces[5][1] = 5;
200  faces[5][2] = 6;
201  faces[5][3] = 2;
202 
203  return faces;
204 }
205 
206 
207 void Foam::boundBox::inflate(const scalar s)
208 {
209  vector ext = vector::one*s*mag();
210 
211  min_ -= ext;
212  max_ += ext;
213 }
214 
215 
217 {
218  if (points.empty())
219  {
220  return true;
221  }
222 
223  forAll(points, i)
224  {
225  if (!contains(points[i]))
226  {
227  return false;
228  }
229  }
230 
231  return true;
232 }
233 
234 
236 (
237  const UList<point>& points,
238  const labelUList& indices
239 ) const
240 {
241  if (points.empty() || indices.empty())
242  {
243  return true;
244  }
245 
246  forAll(indices, i)
247  {
248  if (!contains(points[indices[i]]))
249  {
250  return false;
251  }
252  }
253 
254  return true;
255 }
256 
257 
259 {
260  if (points.empty())
261  {
262  return true;
263  }
264 
265  forAll(points, i)
266  {
267  if (contains(points[i]))
268  {
269  return true;
270  }
271  }
272 
273  return false;
274 }
275 
276 
278 (
279  const UList<point>& points,
280  const labelUList& indices
281 ) const
282 {
283  if (points.empty() || indices.empty())
284  {
285  return true;
286  }
287 
288  forAll(indices, i)
289  {
290  if (contains(points[indices[i]]))
291  {
292  return true;
293  }
294  }
295 
296  return false;
297 }
298 
299 
301 {
302  // Clip the point to the range of the bounding box
303  const scalar surfPtx = Foam::max(Foam::min(pt.x(), max_.x()), min_.x());
304  const scalar surfPty = Foam::max(Foam::min(pt.y(), max_.y()), min_.y());
305  const scalar surfPtz = Foam::max(Foam::min(pt.z(), max_.z()), min_.z());
306 
307  return point(surfPtx, surfPty, surfPtz);
308 }
309 
310 
311 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
312 
314 {
315  if (os.format() == IOstream::ASCII)
316  {
317  os << bb.min_ << token::SPACE << bb.max_;
318  }
319  else
320  {
321  os.write
322  (
323  reinterpret_cast<const char*>(&bb.min_),
324  sizeof(boundBox)
325  );
326  }
327 
328  // Check state of Ostream
329  os.check("Ostream& operator<<(Ostream&, const boundBox&)");
330  return os;
331 }
332 
333 
335 {
336  if (is.format() == IOstream::ASCII)
337  {
338  is >> bb.min_ >> bb.max_;
339  }
340  else
341  {
342  is.read
343  (
344  reinterpret_cast<char*>(&bb.min_),
345  sizeof(boundBox)
346  );
347  }
348 
349  // Check state of Istream
350  is.check("Istream& operator>>(Istream&, boundBox&)");
351  return is;
352 }
353 
354 
355 // ************************************************************************* //
Inter-processor communication reduction functions.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:108
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
void setSize(const label)
Reset size of List.
Definition: List.C:281
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
virtual Ostream & write(const token &)
Write token.
Definition: Ostream.C:51
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
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
static const Form one
Definition: VectorSpace.H:119
static const Form rootMax
Definition: VectorSpace.H:122
static const Form rootMin
Definition: VectorSpace.H:123
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
point nearest(const point &) const
Return the nearest point on the boundBox to the supplied point.
Definition: boundBox.C:300
static const boundBox invertedBox
A very large inverted boundBox: min/max == +/- vGreat.
Definition: boundBox.H:80
static const boundBox greatBox
A very large boundBox: min/max == -/+ vGreat.
Definition: boundBox.H:77
static faceList faces()
Return faces with correct point order.
Definition: boundBox.C:164
void inflate(const scalar s)
Inflate box by factor*mag(span) in all dimensions.
Definition: boundBox.C:207
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:204
bool containsAny(const UList< point > &) const
Contains any of the points? (inside or on edge)
Definition: boundBox.C:258
boundBox()
Construct null, setting points to zero.
Definition: boundBoxI.H:39
tmp< pointField > points() const
Return corner points in an order corresponding to a 'hex' cell.
Definition: boundBox.C:146
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:197
const pointField & points
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))
static const zero Zero
Definition: zero.H:97
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
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
vector point
Point is a vector.
Definition: point.H:41
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
tmp< DimensionedField< scalar, GeoMesh, Field > > mag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)