initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / octree / octree.H
blob8f7972324228c532cef97039661a8a4189098b51
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::octree
28 Description
29     Octree, templated on type of shapes it refers to.
31     Uses the octreeData class, which is a list of 1D, 2D or 3D shapes.
32     Various implementations of octreeData which know about cell&meshes,
33     patches&meshes and points.
35     The octree can be used to
36       - find the "nearest" shape to a point
37       - find the shape which contains a point (only sensible for 3D shapes)
38       - find intersections of line with shapes
40     The tree consists of
41       - treeNode : holds sub-treeNodes or treeLeaves
42       - treeLeaf : is the one that actually holds data
44     The data is stored purely as a list of indices into octreeData.
46     The construction on the depth of the tree is:
47       - one can specify a minimum depth
48         (though the tree will never be refined if all leaves contain <=1
49          shapes)
50       - after the minimum depth two statistics are used to decide further
51         refinement:
52         - average number of entries per leaf (leafRatio). Since inside a
53           leaf most algorithms are n or n^2 this value has to be small.
54         - average number of leaves a shape is in. Because of bounding boxes,
55           a single shape can be in multiple leaves. If the bbs are large
56           compared to the leaf size this multiplicity can become extremely
57           large and will become larger with more levels.
59     Note: the octree gets constructed with an overall bounding box. If your
60     mesh is regular it is a good idea to make this overall bb a bit wider than
61     the bb of the shapes itself. Otherwise lots of shapes end up exactly on the
62     edge of a bb and hence go into more than one subcube.
64     E.g. octree of face centres of lid driven cavity.
66       -# bb exact -> total 1457 leaves (every point in 7.1 leaves)
67       -# bb.max incremented by 1% -> total 80 leaves
68          (every point in exactly 1 leaf)
70     @par Ideas for parallel implementation:
72     The data inserted into the octree (the
73     'octreeData') has to be local to the processor. The data to be looked
74     for (usually a sample point) can be global to the domain.
75     The algorithm:
76       - search for all my points
77       - collect results which have to do with a processor patch
78       - global sum all these. If 0 exit.
79       - exchange data on all processor patches
80       - start again
82     So data transfers one processor per iteration. One could think of having
83     an extra search mechanism one level above which does an initial search
84     knowing the average shape of a processor distribution (square/cube for
85     most decomposition methods) and can assign sample points to the (almost)
86     correct processor at once.
88 SourceFiles
89     octree.C
91 \*---------------------------------------------------------------------------*/
93 #ifndef octree_H
94 #define octree_H
96 #include "treeBoundBoxList.H"
97 #include "treeLeaf.H"
98 #include "pointIndexHit.H"
99 #include "linePointRef.H"
101 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
103 namespace Foam
106 // Forward declaration of classes
107 template<class Type> class treeNode;
109 // Forward declaration of friend functions and operators
111 template<class Type>
112 Ostream& operator<<(Ostream&, const octree<Type>&);
115 /*---------------------------------------------------------------------------*\
116                         Class octreeName Declaration
117 \*---------------------------------------------------------------------------*/
119 TemplateName(octree);
123 /*---------------------------------------------------------------------------*\
124                            Class octree Declaration
125 \*---------------------------------------------------------------------------*/
127 template <class Type>
128 class octree
130     public octreeName
132     // Private data
134         //- Top treeNode. Modified by lower levels.
135         treeNode<Type>* topNode_;
137         //- all shapes
138         const Type shapes_;
140         //- Overall bb of octree
141         const treeBoundBox octreeBb_;
143         //- Refinement crit: size of leaves. Average number of entries per
144         //  leaf. Should be fine enough for efficient searching at lowest level.
145         const scalar maxLeafRatio_;
147         //- Refinement crit: multiplicity of entries (so in how many leaves
148         //  each shape is mentioned)
149         const scalar maxShapeRatio_;
151         //- Refinement crit: min no. of levels
152         const label minNLevels_;
154         //- Actual depth
155         label deepestLevel_;
157         //- Total number of (references to) shapes in octree
158         //  (shapes can be stored in more than one treeLeaf)
159         label nEntries_;
161         //- Total number of treeNodes
162         label nNodes_;
164         //- Total number of treeLeaves
165         label nLeaves_;
168     // Static data members
169         //- Refinement crit: max number of level
170         static const label maxNLevels = 20;
174     // Private Member Functions
176 public:
178     // Data types
180         //- volume types
181         enum volumeType
182         {
183             UNKNOWN,
184             MIXED,
185             INSIDE,
186             OUTSIDE
187         };
190     // Static data members
192         //- for debugging:return printable representation of volumeType
193         static string volType(const label);
195         //- Code the vector with respect to the geometry. geomNormal guaranteed
196         //  to point outside.
197         static label getVolType
198         (
199             const vector& geomNormal,
200             const vector& vec
201         );
203     // Constructors
205         //- Construct from components
206         octree
207         (
208             const treeBoundBox& octreeBb,
209             const Type& shapes,
210             const label minNLevels,     // minimum number of levels
211             const scalar maxLeafRatio,  // max avg. size of leaves
212             const scalar maxShapeRatio  // max avg. duplicity.
213         );
216     // Destructor
218         ~octree();
221     // Member Functions
223         // Access
225             const Type& shapes() const
226             {
227                 return shapes_;
228             }
230             const treeBoundBox& octreeBb() const
231             {
232                 return octreeBb_;
233             }
235             scalar maxShapeRatio() const
236             {
237                 return maxShapeRatio_;
238             }
240             scalar maxLeafRatio() const
241             {
242                 return maxLeafRatio_;
243             }
245             label minNLevels() const
246             {
247                 return minNLevels_;
248             }
250         // After construction: octree statistics
252             treeNode<Type>* topNode() const
253             {
254                 return topNode_;
255             }
257             label deepestLevel() const
258             {
259                 return deepestLevel_;
260             }
262             label nEntries() const
263             {
264                 return nEntries_;
265             }
267             label nNodes() const
268             {
269                 return nNodes_;
270             }
272             label nLeaves() const
273             {
274                 return nLeaves_;
275             }
277             void setEntries(const label n)
278             {
279                 nEntries_ = n;
280             }
282             void setNodes(const label n)
283             {
284                 nNodes_ = n;
285             }
287             void setLeaves(const label n)
288             {
289                 nLeaves_ = n;
290             }
292         // Queries
294             //- Returns type of sample with respect to nearest shape.
295             //  Meaning of type is determined by shapes.getSampleType().
296             //  Normally based on outwards pointing normal. For e.g.
297             //  octreeDataFace returns one of
298             //      inside  : on other side of normal on nearest face
299             //      outside : on same side as normal on nearest face
300             //      unknown : not above nearest face; surface probably not
301             //                closed.
302             //      mixed   : should never happen
303             label getSampleType(const point& sample) const;
305             //- Find shape containing point in tree
306             //  Returns -1 if not in found. Uses Type::contains.
307             label find(const point& sample) const;
309             //- Calculate tightest fitting bounding box. Uses
310             //  Type::findTightest.
311             bool findTightest
312             (
313                 const point& sample,
314                 treeBoundBox& tightest
315             ) const;
317             //- Find nearest shape. Returns index of shape or -1 if not found.
318             //  tightestDist is both input and output. Uses Type::calcNearest.
319             label findNearest
320             (
321                 const point& sample,
322                 treeBoundBox& tightest,
323                 scalar& tightestDist
324             ) const;
326             //- Find nearest to line. Returns -1 or index of shape and
327             //  sets:
328             //  - tightest (is both input and output).
329             //  - linePoint : point on line (-GREAT,-GREAT,-GREAT if not found)
330             //  - shapePoint : point on shape (GREAT, GREAT, GREAT if not found)
331             //  Uses Type::calcNearest.
332             label findNearest
333             (
334                 const linePointRef& ln,
335                 treeBoundBox& tightest,
336                 point& linePoint,
337                 point& shapePoint
338             ) const;
340             //- Find (in no particular order) indices of all shapes inside or
341             //  overlapping bounding box (i.e. all shapes not outside box)
342             labelList findBox(const boundBox& bb) const;
344             //- Find intersected shape along line. pointIndexHit contains index
345             //  of nearest shape intersected and the intersection point. Uses
346             //  findLeafLine.
347             pointIndexHit findLine(const point& start, const point& end) const;
349             //- Like above but just tests whether line hits anything. So
350             //   returns first intersection found, not nessecarily nearest.
351             pointIndexHit findLineAny(const point& start, const point& end)
352              const;
354             //- Find leaf along line. Set leafIntPoint to leave point of
355             //  treeLeaf.
356             const treeLeaf<Type>* findLeafLine
357             (
358                 const point& start,
359                 const point& end,
360                 point& leafIntPoint
361             ) const;
364         // Write
366             //- Dump graphical representation in .obj format
367             void writeOBJ(Ostream& os, label& vertNo) const;
369             //- Print some stats on octree
370             void printStats(Ostream& os) const;
373     // STL iterator
375         class iterator;
376         friend class iterator;
378         //- An STL iterator for octree
379         class iterator
380         {
381             // Private data
383                 //- Reference to the octree this is an iterator for
384                 octree& octree_;
386                 //- List of pointers to treeLeaves
387                 List<treeLeaf<Type>*> leaves_;
389                 //- Current treeLeaf index
390                 label curLeaf_;
392         public:
394             //- Construct for a given octree
395             iterator(octree&);
397             //- Contruct for a given octree, at a certain position
398             iterator(octree& oc, const label index);
401             // Member operators
403                 void operator=(const iterator&);
405                 bool operator==(const iterator&) const;
406                 bool operator!=(const iterator&) const;
408                 treeLeaf<Type>& operator*();
410                 iterator& operator++();
411                 iterator operator++(int);
412         };
414         //- iterator set to the begining of the octree
415         iterator begin();
417         //- iterator set to beyond the end of the octree
418         const iterator& end();
421     // STL const_iterator
423         class const_iterator;
424         friend class const_iterator;
426         //- An STL const_iterator for octree
427         class const_iterator
428         {
429             // Private data
431                 //- Reference to the list this is an iterator for
432                 const octree& octree_;
434                 //- List of treeLeafs
435                 List<const treeLeaf<Type>*> leaves_;
437                 //- Current treeLeaf index
438                 label curLeaf_;
440         public:
442             //- Construct for a given octree
443             const_iterator(const octree&);
445             //- Construct for a given octree and index
446             const_iterator(const octree&, label);
448             // Member operators
450                 void operator=(const const_iterator&);
452                 bool operator==(const const_iterator&) const;
453                 bool operator!=(const const_iterator&) const;
455                 const treeLeaf<Type>& operator*();
457                 const_iterator& operator++();
458                 const_iterator operator++(int);
459         };
462         //- const_iterator set to the begining of the octree
463         inline const_iterator begin() const;
464         inline const_iterator cbegin() const;
466         //- const_iterator set to beyond the end of the octree
467         inline const const_iterator& end() const;
468         inline const const_iterator& cend() const;
471     // IOstream Operators
473         friend Ostream& operator<< <Type>(Ostream&, const octree<Type>&);
476 private:
478         //- iterator returned by end()
479         iterator endIter_;
481         //- const_iterator returned by end()
482         const_iterator endConstIter_;
486 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
488 } // End namespace Foam
490 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
492 #ifdef NoRepository
493 #   include "octree.C"
494 #endif
496 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
498 #endif
500 // ************************************************************************* //