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