initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / octree / treeBoundBox.H
blob69791e04a85b8ddac668cc924a0f46f48b67a5b4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::treeBoundBox
28 Description
29     Standard boundBox + extra functionality for use in octree.
31     Numbering of corner points is according to octant numbering.
33     On the back plane (z=0):
35     @verbatim
36         Y
37         ^
38         |
39         +--------+
40         |2      3|
41         |        |
42         |        |
43         |        |
44         |0      1|
45         +--------+->X
46     @endverbatim
48     For the front plane add 4 to the point labels.
51 SourceFiles
52     treeBoundBoxI.H
53     treeBoundBox.C
55 \*---------------------------------------------------------------------------*/
57 #ifndef treeBoundBox_H
58 #define treeBoundBox_H
60 #include "boundBox.H"
61 #include "direction.H"
62 #include "pointField.H"
63 #include "faceList.H"
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 namespace Foam
70 class Random;
72 /*---------------------------------------------------------------------------*\
73                            Class treeBoundBox Declaration
74 \*---------------------------------------------------------------------------*/
76 class treeBoundBox
78     public boundBox
81 private:
83         //- To initialise faceNormals.
84         static FixedList<vector, 6> calcFaceNormals();
86 public:
88     // Static data members
90         //- The great value used for greatBox and invertedBox
91         static const scalar great;
93         //- As per boundBox::greatBox, but with GREAT instead of VGREAT
94         static const treeBoundBox greatBox;
96         //- As per boundBox::invertedBox, but with GREAT instead of VGREAT
97         static const treeBoundBox invertedBox;
99         //- Bits used for octant/point coding.
100         //  Every octant/corner point is the combination of three faces.
101         enum octantBit
102         {
103             RIGHTHALF = 0x1 << 0,
104             TOPHALF   = 0x1 << 1,
105             FRONTHALF = 0x1 << 2
106         };
108         //- Face codes
109         enum faceId
110         {
111             LEFT   = 0,
112             RIGHT  = 1,
113             BOTTOM = 2,
114             TOP    = 3,
115             BACK   = 4,
116             FRONT  = 5
117         };
119         //- Bits used for face coding
120         enum faceBit
121         {
122             NOFACE    = 0,
123             LEFTBIT   = 0x1 << LEFT,    //1
124             RIGHTBIT  = 0x1 << RIGHT,   //2
125             BOTTOMBIT = 0x1 << BOTTOM,  //4
126             TOPBIT    = 0x1 << TOP,     //8
127             BACKBIT   = 0x1 << BACK,    //16
128             FRONTBIT  = 0x1 << FRONT,   //32
129         };
131         //- Edges codes.
132         //  E01 = edge between 0 and 1.
133         enum edgeId
134         {
135             E01 = 0,
136             E13 = 1,
137             E23 = 2,
138             E02 = 3,
140             E45 = 4,
141             E57 = 5,
142             E67 = 6,
143             E46 = 7,
145             E04 = 8,
146             E15 = 9,
147             E37 = 10,
148             E26 = 11
149         };
151         //- Face to point addressing
152         static const faceList faces;
154         //- Edge to point addressing
155         static const edgeList edges;
157         //- Per face the unit normal
158         static const FixedList<vector, 6> faceNormals;
161         //- Face on which neighbour is
162         static direction neighbourFaceBits(const label&);
165     // Constructors
167         //- Construct null setting points to zero
168         inline treeBoundBox();
170         //- Construct from components
171         inline treeBoundBox(const point& min, const point& max);
173         //- Construct from components
174         inline treeBoundBox(const boundBox& bb);
176         //- Construct as the bounding box of the given pointField.
177         //  Local processor domain only (no reduce as in boundBox)
178         treeBoundBox(const UList<point>&);
180         //- Construct as subset of points
181         //  Local processor domain only (no reduce as in boundBox)
182         treeBoundBox(const UList<point>&, const UList<label>& meshPoints);
184         //- Construct from Istream
185         treeBoundBox(Istream&);
188     // Member functions
190         // Access
192             //- Typical dimension length,height,width
193             inline scalar typDim() const;
195             //- vertex coordinates. In octant coding.
196             pointField points() const;
199         // Check
201             //- Corner point given octant
202             inline point corner(const direction) const;
204             //- Sub box given by octant number. Midpoint calculated.
205             treeBoundBox subBbox(const direction) const;
207             //- Sub box given by octant number. Midpoint provided.
208             treeBoundBox subBbox(const point& mid, const direction) const;
210             //- Returns octant number given point and the calculated midpoint.
211             inline direction subOctant
212             (
213                 const point& pt
214             ) const;
216             //- Returns octant number given point and midpoint.
217             static inline direction subOctant
218             (
219                 const point& mid,
220                 const point& pt
221             );
223             //- Returns octant number given point and the calculated midpoint.
224             //  onEdge set if the point is on edge of subOctant
225             inline direction subOctant
226             (
227                 const point& pt,
228                 bool& onEdge
229             ) const;
231             //- Returns octant number given point and midpoint.
232             //  onEdge set if the point is on edge of subOctant
233             static inline direction subOctant
234             (
235                 const point& mid,
236                 const point& pt,
237                 bool& onEdge
238             );
240             //- Returns octant number given intersection and midpoint.
241             //  onEdge set if the point is on edge of subOctant
242             //  If onEdge, the direction vector determines which octant to use
243             //  (acc. to which octant the point would be if it were moved
244             //  along dir)
245             static inline direction subOctant
246             (
247                 const point& mid,
248                 const vector& dir,
249                 const point& pt,
250                 bool& onEdge
251             );
253             //- Calculates optimal order to look for nearest to point.
254             //  First will be the octant containing the point,
255             //  second the octant with boundary nearest to the point etc.
256             inline void searchOrder
257             (
258                  const point& pt,
259                  FixedList<direction, 8>& octantOrder
260             ) const;
262             //- Overlaps other boundingbox?
263             inline bool overlaps(const treeBoundBox&) const;
265             //- Overlaps boundingSphere (centre + sqr(radius))?
266             bool overlaps(const point&, const scalar radiusSqr) const;
268             //- Intersects segment; set point to intersection position and face,
269             //  return true if intersection found.
270             //  (pt argument used during calculation even if not intersecting).
271             //  Calculates intersections from outside supplied vector
272             //  (overallStart, overallVec). This is so when
273             //  e.g. tracking through lots of consecutive boxes
274             // (typical octree) we're not accumulating truncation errors. Set
275             // to start, (end-start) if not used.
276             bool intersects
277             (
278                 const point& overallStart,
279                 const vector& overallVec,
280                 const point& start,
281                 const point& end,
282                 point& pt,
283                 direction& ptBits
284             ) const;
286             //- Like above but does not return faces point is on
287             bool intersects
288             (
289                 const point& start,
290                 const point& end,
291                 point& pt
292             ) const;
294             //- fully contains other boundingBox?
295             inline bool contains(const treeBoundBox&) const;
297             //- Contains point? (inside or on edge)
298             inline bool contains(const point&) const;
300             //- Contains point (inside or on edge) and moving in direction
301             //  dir would cause it to go inside.
302             bool contains(const vector& dir, const point&) const;
304             //- Code position of pt on bounding box faces
305             direction faceBits(const point& pt) const;
307             //- Position of point relative to bounding box
308             direction posBits(const point&) const;
310             //- Calculate nearest and furthest (to point) vertex coords of
311             //  bounding box
312             void calcExtremities
313             (
314                 const point& pt,
315                 point& nearest,
316                 point& furthest
317             ) const;
319             //- Returns distance point to furthest away corner.
320             scalar maxDist(const point&) const;
322             //- Compare distance to point with other bounding box
323             //  return:
324             //  -1 : all vertices of my bounding box are nearer than any of
325             //       other
326             //  +1 : all vertices of my bounding box are further away than
327             //       any of other
328             //   0 : none of the above.
329             label distanceCmp(const point&, const treeBoundBox& other) const;
331             //- Return slightly wider bounding box
332             //  Extends all dimensions with s*span*Random::scalar01()
333             //  and guarantees in any direction s*mag(span) minimum width
334             inline treeBoundBox extend(Random&, const scalar s) const;
336     // Friend Operators
338         friend bool operator==(const treeBoundBox&, const treeBoundBox&);
339         friend bool operator!=(const treeBoundBox&, const treeBoundBox&);
341     // IOstream operator
343         friend Istream& operator>>(Istream& is, treeBoundBox&);
344         friend Ostream& operator<<(Ostream& os, const treeBoundBox&);
349 //- Data associated with treeBoundBox type are contiguous
350 template<>
351 inline bool contiguous<treeBoundBox>() {return contiguous<boundBox>();}
354 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356 } // End namespace Foam
358 #include "treeBoundBoxI.H"
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362 #endif
364 // ************************************************************************* //