FreeFOAM The Cross-Platform CFD Toolkit
boundBox.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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 Class
25  Foam::boundBox
26 
27 Description
28  A bounding box defined in terms of the points at its extremities.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #ifndef boundBox_H
33 #define boundBox_H
34 
35 #include <OpenFOAM/pointField.H>
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 
42 // Forward declaration of friend functions and operators
43 
44 class boundBox;
45 template<class T> class tmp;
46 
47 Ostream& operator<<(Ostream& os, const boundBox& b);
48 
49 
50 /*---------------------------------------------------------------------------*\
51  Class boundBox Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class boundBox
55 {
56  // Private data
57 
58  //- Minimum and maximum describing the bounding box
59  point min_, max_;
60 
61  // Private Member Functions
62 
63  //- Calculate the bounding box from the given pointField.
64  // Does parallel communication (doReduce = true)
65  void calculate(const pointField&, const bool doReduce = true);
66 
67 public:
68 
69  // Static data members
70 
71  //- The great value used for greatBox and invertedBox
72  static const scalar great;
73 
74  //- A very large boundBox: min/max == -/+ VGREAT
75  static const boundBox greatBox;
76 
77  //- A very large inverted boundBox: min/max == +/- VGREAT
78  static const boundBox invertedBox;
79 
80 
81  // Constructors
82 
83  //- Construct null, setting points to zero
85  :
86  min_(point::zero),
87  max_(point::zero)
88  {}
89 
90  //- Construct from components
91  boundBox(const point& min, const point& max)
92  :
93  min_(min),
94  max_(max)
95  {}
96 
97  //- Construct as the bounding box of the given pointField.
98  // Does parallel communication (doReduce = true)
99  boundBox(const pointField&, const bool doReduce = true);
100 
101  //- Construct as the bounding box of the given temporary pointField.
102  // Does parallel communication (doReduce = true)
103  boundBox(const tmp<pointField>&, const bool doReduce = true);
104 
105  //- Construct from Istream
106  boundBox(Istream&);
107 
108 
109  // Member functions
110 
111  // Access
112 
113  //- Minimum describing the bounding box
114  const point& min() const
115  {
116  return min_;
117  }
118 
119  //- Maximum describing the bounding box
120  const point& max() const
121  {
122  return max_;
123  }
124 
125  //- Minimum describing the bounding box, non-const access
127  {
128  return min_;
129  }
130 
131  //- Maximum describing the bounding box, non-const access
133  {
134  return max_;
135  }
136 
137  //- The midpoint of the bounding box
138  point midpoint() const
139  {
140  return 0.5 * (max_ + min_);
141  }
142 
143  //- The bounding box span (from minimum to maximum)
144  vector span() const
145  {
146  return (max_ - min_);
147  }
148 
149  //- The magnitude of the bounding box span
150  scalar mag() const
151  {
152  return ::Foam::mag(max_ - min_);
153  }
154 
155  //- Smallest length/height/width dimension
156  scalar minDim() const
157  {
158  return cmptMin(span());
159  }
160 
161  //- Largest length/height/width dimension
162  scalar maxDim() const
163  {
164  return cmptMax(span());
165  }
166 
167  //- Average length/height/width dimension
168  scalar avgDim() const
169  {
170  return cmptAv(span());
171  }
172 
173 
174  // Query
175 
176  //- Overlaps/touches boundingBox?
177  bool overlaps(const boundBox& bb) const
178  {
179  return
180  (
181  bb.max_.x() >= min_.x() && bb.min_.x() <= max_.x()
182  && bb.max_.y() >= min_.y() && bb.min_.y() <= max_.y()
183  && bb.max_.z() >= min_.z() && bb.min_.z() <= max_.z()
184  );
185  }
186 
187  //- Contains point? (inside or on edge)
188  bool contains(const point& pt) const
189  {
190  return
191  (
192  pt.x() >= min_.x() && pt.x() <= max_.x()
193  && pt.y() >= min_.y() && pt.y() <= max_.y()
194  && pt.z() >= min_.z() && pt.z() <= max_.z()
195  );
196  }
197 
198  //- Contains point? (inside only)
199  bool containsInside(const point& pt) const
200  {
201  return
202  (
203  pt.x() > min_.x() && pt.x() < max_.x()
204  && pt.y() > min_.y() && pt.y() < max_.y()
205  && pt.z() > min_.z() && pt.z() < max_.z()
206  );
207  }
208 
209 
210  // Friend Operators
211 
212  friend bool operator==(const boundBox& a, const boundBox& b)
213  {
214  return (a.min_ == b.min_) && (a.max_ == b.max_);
215  }
216 
217  friend bool operator!=(const boundBox& a, const boundBox& b)
218  {
219  return !(a == b);
220  }
221 
222 
223  // IOstream operator
224 
225  friend Istream& operator>>(Istream& is, boundBox&);
226  friend Ostream& operator<<(Ostream& os, const boundBox&);
227 };
228 
229 
230 //- Data associated with boundBox type are contiguous
231 template<>
232 inline bool contiguous<boundBox>() {return contiguous<point>();}
233 
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 } // End namespace Foam
238 
239 // #include "boundBoxI.H"
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #endif
244 
245 // ************************ vim: set sw=4 sts=4 et: ************************ //