FreeFOAM The Cross-Platform CFD Toolkit
treeBoundBox.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::treeBoundBox
26 
27 Description
28  Standard boundBox + extra functionality for use in octree.
29 
30  Numbering of corner points is according to octant numbering.
31 
32  On the back plane (z=0):
33 
34  @verbatim
35  Y
36  ^
37  |
38  +--------+
39  |2 3|
40  | |
41  | |
42  | |
43  |0 1|
44  +--------+->X
45  @endverbatim
46 
47  For the front plane add 4 to the point labels.
48 
49 
50 SourceFiles
51  treeBoundBoxI.H
52  treeBoundBox.C
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef treeBoundBox_H
57 #define treeBoundBox_H
58 
59 #include <OpenFOAM/boundBox.H>
60 #include <OpenFOAM/direction.H>
61 #include <OpenFOAM/pointField.H>
62 #include <OpenFOAM/faceList.H>
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 
69 class Random;
70 
71 /*---------------------------------------------------------------------------*\
72  Class treeBoundBox Declaration
73 \*---------------------------------------------------------------------------*/
74 
76 :
77  public boundBox
78 {
79 
80 private:
81 
82  //- To initialise faceNormals.
83  static FixedList<vector, 6> calcFaceNormals();
84 
85 public:
86 
87  // Static data members
88 
89  //- The great value used for greatBox and invertedBox
90  static const scalar great;
91 
92  //- As per boundBox::greatBox, but with GREAT instead of VGREAT
93  static const treeBoundBox greatBox;
94 
95  //- As per boundBox::invertedBox, but with GREAT instead of VGREAT
96  static const treeBoundBox invertedBox;
97 
98  //- Bits used for octant/point coding.
99  // Every octant/corner point is the combination of three faces.
101  {
102  RIGHTHALF = 0x1 << 0,
103  TOPHALF = 0x1 << 1,
104  FRONTHALF = 0x1 << 2
105  };
106 
107  //- Face codes
108  enum faceId
109  {
110  LEFT = 0,
111  RIGHT = 1,
112  BOTTOM = 2,
113  TOP = 3,
114  BACK = 4,
115  FRONT = 5
116  };
117 
118  //- Bits used for face coding
119  enum faceBit
120  {
121  NOFACE = 0,
122  LEFTBIT = 0x1 << LEFT, //1
123  RIGHTBIT = 0x1 << RIGHT, //2
124  BOTTOMBIT = 0x1 << BOTTOM, //4
125  TOPBIT = 0x1 << TOP, //8
126  BACKBIT = 0x1 << BACK, //16
127  FRONTBIT = 0x1 << FRONT, //32
128  };
129 
130  //- Edges codes.
131  // E01 = edge between 0 and 1.
132  enum edgeId
133  {
134  E01 = 0,
135  E13 = 1,
136  E23 = 2,
137  E02 = 3,
138 
139  E45 = 4,
140  E57 = 5,
141  E67 = 6,
142  E46 = 7,
143 
144  E04 = 8,
145  E15 = 9,
146  E37 = 10,
147  E26 = 11
148  };
149 
150  //- Face to point addressing
151  static const faceList faces;
152 
153  //- Edge to point addressing
154  static const edgeList edges;
155 
156  //- Per face the unit normal
158 
159 
160  //- Face on which neighbour is
161  static direction neighbourFaceBits(const label&);
162 
163 
164  // Constructors
165 
166  //- Construct null setting points to zero
167  inline treeBoundBox();
168 
169  //- Construct from components
170  inline treeBoundBox(const point& min, const point& max);
171 
172  //- Construct from components
173  explicit inline treeBoundBox(const boundBox& bb);
174 
175  //- Construct as the bounding box of the given pointField.
176  // Local processor domain only (no reduce as in boundBox)
177  explicit treeBoundBox(const UList<point>&);
178 
179  //- Construct as subset of points
180  // Local processor domain only (no reduce as in boundBox)
181  treeBoundBox(const UList<point>&, const UList<label>& meshPoints);
182 
183  //- Construct from Istream
185 
186 
187  // Member functions
188 
189  // Access
190 
191  //- Typical dimension length,height,width
192  inline scalar typDim() const;
193 
194  //- vertex coordinates. In octant coding.
195  pointField points() const;
196 
197 
198  // Check
199 
200  //- Corner point given octant
201  inline point corner(const direction) const;
202 
203  //- Sub box given by octant number. Midpoint calculated.
204  treeBoundBox subBbox(const direction) const;
205 
206  //- Sub box given by octant number. Midpoint provided.
207  treeBoundBox subBbox(const point& mid, const direction) const;
208 
209  //- Returns octant number given point and the calculated midpoint.
210  inline direction subOctant
211  (
212  const point& pt
213  ) const;
214 
215  //- Returns octant number given point and midpoint.
216  static inline direction subOctant
217  (
218  const point& mid,
219  const point& pt
220  );
221 
222  //- Returns octant number given point and the calculated midpoint.
223  // onEdge set if the point is on edge of subOctant
224  inline direction subOctant
225  (
226  const point& pt,
227  bool& onEdge
228  ) const;
229 
230  //- Returns octant number given point and midpoint.
231  // onEdge set if the point is on edge of subOctant
232  static inline direction subOctant
233  (
234  const point& mid,
235  const point& pt,
236  bool& onEdge
237  );
238 
239  //- Returns octant number given intersection and midpoint.
240  // onEdge set if the point is on edge of subOctant
241  // If onEdge, the direction vector determines which octant to use
242  // (acc. to which octant the point would be if it were moved
243  // along dir)
244  static inline direction subOctant
245  (
246  const point& mid,
247  const vector& dir,
248  const point& pt,
249  bool& onEdge
250  );
251 
252  //- Calculates optimal order to look for nearest to point.
253  // First will be the octant containing the point,
254  // second the octant with boundary nearest to the point etc.
255  inline void searchOrder
256  (
257  const point& pt,
258  FixedList<direction, 8>& octantOrder
259  ) const;
260 
261  //- Overlaps other boundingbox?
262  inline bool overlaps(const treeBoundBox&) const;
263 
264  //- Overlaps boundingSphere (centre + sqr(radius))?
265  bool overlaps(const point&, const scalar radiusSqr) const;
266 
267  //- Intersects segment; set point to intersection position and face,
268  // return true if intersection found.
269  // (pt argument used during calculation even if not intersecting).
270  // Calculates intersections from outside supplied vector
271  // (overallStart, overallVec). This is so when
272  // e.g. tracking through lots of consecutive boxes
273  // (typical octree) we're not accumulating truncation errors. Set
274  // to start, (end-start) if not used.
275  bool intersects
276  (
277  const point& overallStart,
278  const vector& overallVec,
279  const point& start,
280  const point& end,
281  point& pt,
282  direction& ptBits
283  ) const;
284 
285  //- Like above but does not return faces point is on
286  bool intersects
287  (
288  const point& start,
289  const point& end,
290  point& pt
291  ) const;
292 
293  //- fully contains other boundingBox?
294  inline bool contains(const treeBoundBox&) const;
295 
296  //- Contains point? (inside or on edge)
297  inline bool contains(const point&) const;
298 
299  //- Contains point (inside or on edge) and moving in direction
300  // dir would cause it to go inside.
301  bool contains(const vector& dir, const point&) const;
302 
303  //- Code position of pt on bounding box faces
304  direction faceBits(const point& pt) const;
305 
306  //- Position of point relative to bounding box
307  direction posBits(const point&) const;
308 
309  //- Calculate nearest and furthest (to point) vertex coords of
310  // bounding box
311  void calcExtremities
312  (
313  const point& pt,
314  point& nearest,
315  point& furthest
316  ) const;
317 
318  //- Returns distance point to furthest away corner.
319  scalar maxDist(const point&) const;
320 
321  //- Compare distance to point with other bounding box
322  // return:
323  // -1 : all vertices of my bounding box are nearer than any of
324  // other
325  // +1 : all vertices of my bounding box are further away than
326  // any of other
327  // 0 : none of the above.
328  label distanceCmp(const point&, const treeBoundBox& other) const;
329 
330  //- Return slightly wider bounding box
331  // Extends all dimensions with s*span*Random::scalar01()
332  // and guarantees in any direction s*mag(span) minimum width
333  inline treeBoundBox extend(Random&, const scalar s) const;
334 
335  // Friend Operators
336 
337  friend bool operator==(const treeBoundBox&, const treeBoundBox&);
338  friend bool operator!=(const treeBoundBox&, const treeBoundBox&);
339 
340  // IOstream operator
341 
342  friend Istream& operator>>(Istream& is, treeBoundBox&);
343  friend Ostream& operator<<(Ostream& os, const treeBoundBox&);
344 
345 };
346 
347 
348 //- Data associated with treeBoundBox type are contiguous
349 template<>
351 
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 } // End namespace Foam
356 
357 #include <meshTools/treeBoundBoxI.H>
358 
359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
360 
361 #endif
362 
363 // ************************ vim: set sw=4 sts=4 et: ************************ //