OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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