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