OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     treeNode
00027 
00028 Description
00029     Class to implement octree.
00030     Holds the pointers to subcubes. These are either other treeNodes or
00031     treeLeafs. treeLeafs hold the actual data as a list of indices into
00032     octreeData.
00033 
00034     To prevent calculation errors all bb's used in octrees are calculated 
00035     only once.
00036 
00037     The pointers to either treeNode/treeLeaf are implemented 'by hand'
00038     (explicitly marking type) instead of using a proper virtual mechanism
00039     to save some space in the treeLeaves.
00040 
00041 SourceFiles
00042     treeNode.C
00043 
00044 \*---------------------------------------------------------------------------*/
00045 
00046 #ifndef treeNode_H
00047 #define treeNode_H
00048 
00049 #include "treeBoundBoxList.H"
00050 #include "treeElem.H"
00051 #include "linePointRef.H"
00052 #include "labelHashSet.H"
00053 
00054 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00055 
00056 namespace Foam
00057 {
00058 
00059 //class intersection;
00060 
00061 template<class Type> class octree;
00062 template<class Type> class treeLeaf;
00063 template<class Type> class treeNode;
00064 
00065 // * * * * * * Forward declaration of template friend functions  * * * * * * //
00066 
00067 template<class Type> Istream& operator>>(Istream&, treeNode<Type>&);
00068 template<class Type> Ostream& operator<<(Ostream&, const treeNode<Type>&);
00069 
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 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         //- empty labelList to satisfy compiler
00106         static const labelList dummy;
00107 
00108         //- leaf offset for octant index
00109         static const label leafOffset;
00110 
00111 
00112     // Private Member Functions
00113 
00114         //- mark pointer to subnode as being a treeNode*
00115         void setAsNode(const label octant);
00116 
00117         //- mark pointer to subnode as being a treeLeaf*
00118         void setAsLeaf(const label octant);
00119 
00120         //- Set pointer to sub node
00121         void setNodePtr(const label octant, treeElem<Type>* treeNodePtr);
00122 
00123         //- Set pointer to sub leaf
00124         void setLeafPtr(const label octant, treeElem<Type>* treeLeafPtr);
00125 
00126         //- Set type of octant
00127         void setVolType(const label octant, const label type);
00128 
00129         //- Get type of octant
00130         inline label getVolType(const label octant) const;
00131 
00132         //- Find first leaf on line start-end. Updates start.
00133         const treeLeaf<Type>* findLeafLineOctant
00134         (
00135             const int level,
00136             const Type& shapes,
00137             const label octant,
00138             const vector& direction,
00139             point& start,
00140             const point& end
00141         ) const;
00142 
00143 
00144         //- Print spaces
00145         static void space(Ostream&, const label);
00146 
00147         //- Disallow default bitwise copy construct
00148         treeNode(const treeNode&);
00149 
00150         //- Disallow default bitwise assignment
00151         void operator=(const treeNode&);
00152 
00153 
00154 public:
00155 
00156     // Constructors
00157 
00158         //- Construct from components
00159         treeNode(const treeBoundBox& bb);
00160 
00161         //- Construct from Istream
00162         treeNode(Istream&);
00163 
00164 
00165     // Destructor
00166 
00167         ~treeNode();
00168 
00169 
00170     // Member Functions
00171 
00172         // Access
00173 
00174             //- position of midPoint
00175             const point& mid() const;
00176 
00177             //- array of 8 subNodes/leaves
00178             inline treeElem<Type>* const* subNodes() const;
00179 
00180             //- octant contains pointer to treeNode(1) or treeLeaf(0)
00181             inline label isNode(const label octant) const;
00182 
00183             //- Get pointer to sub node
00184             inline treeNode<Type>* getNodePtr(const label octant) const;
00185 
00186             //- Get pointer to sub leaf
00187             inline treeLeaf<Type>* getLeafPtr(const label octant) const;
00188 
00189         // Edit
00190 
00191             //- Take list of shapes and distribute over the 8 octants
00192             void distribute
00193             (
00194                 const label,
00195                 octree<Type>&,
00196                 const Type& shapes,
00197                 const labelList&
00198             );
00199 
00200             //- Distribute at certain level only
00201             void redistribute
00202             (
00203                 const label,
00204                 octree<Type>&,
00205                 const Type& shapes,
00206                 const label
00207             );
00208 
00209             //- Set type of subnodes
00210             label setSubNodeType
00211             (
00212                 const label level,
00213                 octree<Type>& top,
00214                 const Type& shapes
00215             );
00216 
00217         // Search
00218 
00219             //- Find type of node sample is in. Used for inside/outside
00220             //  determination
00221             label getSampleType
00222             (
00223                 const label level,
00224                 const octree<Type>& top,
00225                 const Type& shapes,
00226                 const point& sample
00227             ) const;
00228 
00229             //- Find index of shape containing sample. 
00230             label find
00231             (
00232                 const Type& shapes,
00233                 const point& sample
00234             ) const;
00235 
00236             //- Find tightest bounding box around sample which is guaranteed
00237             //  to hold at least one cell.
00238             //  Current best bb in tightest, 
00239             //  returns true if newTightest has changed, 0 otherwise.
00240             bool findTightest
00241             (
00242                 const Type& shapes,
00243                 const point& sample,
00244                 treeBoundBox& tightest
00245             ) const;
00246                 
00247             //- Find nearest shape to sample
00248             //  Returns true if found nearer shape and updates
00249             //  tightest, tightesti, tightestDist
00250             bool findNearest
00251             (
00252                 const Type& shapes,
00253                 const point& sample,
00254                 treeBoundBox& tightest,
00255                 label& tightesti,
00256                 scalar& tightestDist
00257             ) const;
00258 
00259             //- Find nearest shape to line
00260             //  Returns true if found nearer shape and updates nearest and
00261             //  tightest
00262             bool findNearest
00263             (
00264                 const Type& shapes,
00265                 const linePointRef& ln,
00266                 treeBoundBox& tightest,
00267                 label& tightesti,   // index of nearest shape
00268                 point& linePoint,   // nearest point on line
00269                 point& shapePoint   // nearest point on shape
00270             ) const;
00271 
00272             //- Find shapes not outside box. Return true if anything found.
00273             bool findBox
00274             (
00275                 const Type& shapes,
00276                 const boundBox& bb,
00277                 labelHashSet& elements
00278             ) const;
00279 
00280             //- Find treeLeaves intersecting line segment [start..end]
00281             //  Updates: start
00282             const treeLeaf<Type>* findLeafLine
00283             (
00284                 const label level,
00285                 const Type& shapes,
00286                 point& start,
00287                 const point& end
00288             ) const;
00289 
00290 
00291             //- Collect all treeLeafs in leafArray. leafIndex points to first
00292             //  empty slot in leafArray and gets updated.
00293             void findLeaves
00294             (
00295                 List<treeLeaf<Type>*>& leafArray,
00296                 label& leafIndex
00297             ) const;
00298 
00299             //- Same but for const.
00300             void findLeaves
00301             (
00302                 List<const treeLeaf<Type>*>& leafArray,
00303                 label& leafIndex
00304             ) const;
00305 
00306 
00307         // Write
00308 
00309             //- Print contents of node.
00310             void printNode
00311             (
00312                 Ostream& os,
00313                 const label level
00314             ) const;
00315 
00316             //- Write subleafs in OBJ format.
00317             void writeOBJ
00318             (
00319                 Ostream& os,
00320                 const label level,
00321                 label& vertNo
00322             ) const;
00323 
00324 
00325     // IOstream Operators
00326 
00327         friend Istream& operator>> <Type> (Istream&, treeNode<Type>&);
00328         friend Ostream& operator<< <Type> (Ostream&, const treeNode<Type>&);
00329 };
00330 
00331 
00332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00333 
00334 } // End namespace Foam
00335 
00336 
00337 #include "treeNodeI.H"
00338 
00339 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00340 
00341 #ifdef NoRepository
00342 #   include "treeNode.C"
00343 #endif
00344 
00345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00346 
00347 #endif
00348 
00349 // ************************************************************************* //
For further information go to www.openfoam.org