FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

treeNode.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::treeNode
00026 
00027 Description
00028     Class to implement octree.
00029 
00030     Holds the pointers to sub-octants. These are either other treeNodes or
00031     treeLeafs. The treeLeafs hold the actual data as a list of indices into
00032     octreeData.
00033 
00034 Note
00035     To prevent calculation errors all bounding boxes used in octrees are
00036     calculated only once.
00037 
00038     The pointers to either treeNode/treeLeaf are implemented 'by hand'
00039     (explicitly marking type) instead of using a proper virtual mechanism
00040     to save some space in the treeLeaves.
00041 
00042 SourceFiles
00043     treeNode.C
00044 
00045 \*---------------------------------------------------------------------------*/
00046 
00047 #ifndef treeNode_H
00048 #define treeNode_H
00049 
00050 #include "treeBoundBoxList.H"
00051 #include "treeElem.H"
00052 #include <OpenFOAM/linePointRef.H>
00053 #include <OpenFOAM/HashSet.H>
00054 
00055 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00056 
00057 namespace Foam
00058 {
00059 
00060 // class intersection;
00061 
00062 template<class Type> class octree;
00063 template<class Type> class treeLeaf;
00064 template<class Type> class treeNode;
00065 
00066 // Forward declaration of friend functions and operators
00067 
00068 template<class Type> Istream& operator>>(Istream&, treeNode<Type>&);
00069 template<class Type> Ostream& operator<<(Ostream&, const treeNode<Type>&);
00070 
00071 
00072 /*---------------------------------------------------------------------------*\
00073                         Class treeNodeName Declaration
00074 \*---------------------------------------------------------------------------*/
00075 
00076 TemplateName(treeNode);
00077 
00078 
00079 /*---------------------------------------------------------------------------*\
00080                           Class treeNode Declaration
00081 \*---------------------------------------------------------------------------*/
00082 
00083 template <class Type>
00084 class treeNode
00085 :
00086     public treeElem<Type>,
00087     public treeNodeName
00088 {
00089     // Private data
00090 
00091         //- Position of the midpoint
00092         const point mid_;
00093 
00094         //- Type stored in subNodes_
00095         unsigned char subNodeTypes_;
00096 
00097         //- Pointers to sub treeNode or treeLeaf
00098         treeElem<Type>* subNodes_[8];
00099 
00100         //- Constant valid for whole subNode/leaf
00101         label volType_;
00102 
00103     // Static data members
00104 
00105         //- leaf offset for octant index
00106         static const label leafOffset;
00107 
00108 
00109     // Private Member Functions
00110 
00111         //- mark pointer to subnode as being a treeNode*
00112         void setAsNode(const label octant);
00113 
00114         //- mark pointer to subnode as being a treeLeaf*
00115         void setAsLeaf(const label octant);
00116 
00117         //- Set pointer to sub node
00118         void setNodePtr(const label octant, treeElem<Type>* treeNodePtr);
00119 
00120         //- Set pointer to sub leaf
00121         void setLeafPtr(const label octant, treeElem<Type>* treeLeafPtr);
00122 
00123         //- Set type of octant
00124         void setVolType(const label octant, const label type);
00125 
00126         //- Get type of octant
00127         inline label getVolType(const label octant) const;
00128 
00129         //- Find first leaf on line start-end. Updates start.
00130         const treeLeaf<Type>* findLeafLineOctant
00131         (
00132             const int level,
00133             const Type& shapes,
00134             const label octant,
00135             const vector& direction,
00136             point& start,
00137             const point& end
00138         ) const;
00139 
00140 
00141         //- Print spaces
00142         static void space(Ostream&, const label);
00143 
00144         //- Disallow default bitwise copy construct
00145         treeNode(const treeNode&);
00146 
00147         //- Disallow default bitwise assignment
00148         void operator=(const treeNode&);
00149 
00150 
00151 public:
00152 
00153     // Constructors
00154 
00155         //- Construct from components
00156         treeNode(const treeBoundBox&);
00157 
00158         //- Construct from Istream
00159         treeNode(Istream&);
00160 
00161 
00162     // Destructor
00163 
00164         ~treeNode();
00165 
00166 
00167     // Member Functions
00168 
00169         // Access
00170 
00171             //- The midpoint position
00172             inline const point& midpoint() const;
00173 
00174             //- array of 8 subNodes/leaves
00175             inline treeElem<Type>* const* subNodes() const;
00176 
00177             //- octant contains pointer to treeNode(1) or treeLeaf(0)
00178             inline label isNode(const label octant) const;
00179 
00180             //- Get pointer to sub node
00181             inline treeNode<Type>* getNodePtr(const label octant) const;
00182 
00183             //- Get pointer to sub leaf
00184             inline treeLeaf<Type>* getLeafPtr(const label octant) const;
00185 
00186         // Edit
00187 
00188             //- Take list of shapes and distribute over the 8 octants
00189             void distribute
00190             (
00191                 const label,
00192                 octree<Type>&,
00193                 const Type& shapes,
00194                 const labelList&
00195             );
00196 
00197             //- Distribute at certain level only
00198             void redistribute
00199             (
00200                 const label,
00201                 octree<Type>&,
00202                 const Type& shapes,
00203                 const label
00204             );
00205 
00206             //- Set type of subnodes
00207             label setSubNodeType
00208             (
00209                 const label level,
00210                 octree<Type>& top,
00211                 const Type& shapes
00212             );
00213 
00214         // Search
00215 
00216             //- Find type of node sample is in. Used for inside/outside
00217             //  determination
00218             label getSampleType
00219             (
00220                 const label level,
00221                 const octree<Type>& top,
00222                 const Type& shapes,
00223                 const point& sample
00224             ) const;
00225 
00226             //- Find index of shape containing sample.
00227             label find
00228             (
00229                 const Type& shapes,
00230                 const point& sample
00231             ) const;
00232 
00233             //- Find tightest bounding box around sample which is guaranteed
00234             //  to hold at least one cell.
00235             //  Current best bb in tightest,
00236             //  returns true if newTightest has changed, 0 otherwise.
00237             bool findTightest
00238             (
00239                 const Type& shapes,
00240                 const point& sample,
00241                 treeBoundBox& tightest
00242             ) const;
00243 
00244             //- Find nearest shape to sample
00245             //  Returns true if found nearer shape and updates
00246             //  tightest, tightestI, tightestDist
00247             bool findNearest
00248             (
00249                 const Type& shapes,
00250                 const point& sample,
00251                 treeBoundBox& tightest,
00252                 label& tightestI,
00253                 scalar& tightestDist
00254             ) const;
00255 
00256             //- Find nearest shape to line
00257             //  Returns true if found nearer shape and updates nearest and
00258             //  tightest
00259             bool findNearest
00260             (
00261                 const Type& shapes,
00262                 const linePointRef& ln,
00263                 treeBoundBox& tightest,
00264                 label& tightestI,   // index of nearest shape
00265                 point& linePoint,   // nearest point on line
00266                 point& shapePoint   // nearest point on shape
00267             ) const;
00268 
00269             //- Find shapes not outside box. Return true if anything found.
00270             bool findBox
00271             (
00272                 const Type& shapes,
00273                 const boundBox& bb,
00274                 labelHashSet& elements
00275             ) const;
00276 
00277             //- Find treeLeaves intersecting line segment [start..end]
00278             //  Updates: start
00279             const treeLeaf<Type>* findLeafLine
00280             (
00281                 const label level,
00282                 const Type& shapes,
00283                 point& start,
00284                 const point& end
00285             ) const;
00286 
00287 
00288             //- Collect all treeLeafs in leafArray. leafIndex points to first
00289             //  empty slot in leafArray and gets updated.
00290             void findLeaves
00291             (
00292                 List<treeLeaf<Type>*>& leafArray,
00293                 label& leafIndex
00294             ) const;
00295 
00296             //- Same but for const.
00297             void findLeaves
00298             (
00299                 List<const treeLeaf<Type>*>& leafArray,
00300                 label& leafIndex
00301             ) const;
00302 
00303 
00304         // Write
00305 
00306             //- Print contents of node.
00307             void printNode
00308             (
00309                 Ostream& os,
00310                 const label level
00311             ) const;
00312 
00313             //- Write subleafs in OBJ format.
00314             void writeOBJ
00315             (
00316                 Ostream& os,
00317                 const label level,
00318                 label& vertNo
00319             ) const;
00320 
00321 
00322     // IOstream Operators
00323 
00324         friend Istream& operator>> <Type> (Istream&, treeNode<Type>&);
00325         friend Ostream& operator<< <Type> (Ostream&, const treeNode<Type>&);
00326 };
00327 
00328 
00329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00330 
00331 } // End namespace Foam
00332 
00333 
00334 #include "treeNodeI.H"
00335 
00336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00337 
00338 #ifdef NoRepository
00339 #   include "treeNode.C"
00340 #endif
00341 
00342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00343 
00344 #endif
00345 
00346 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines