OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

indexedOctree.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-2007 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     indexedOctree
00027 
00028 Description
00029 
00030 SourceFiles
00031     indexedOctree.C
00032 
00033 \*---------------------------------------------------------------------------*/
00034 
00035 #ifndef indexedOctree_H
00036 #define indexedOctree_H
00037 
00038 #include "pointIndexHit.H"
00039 #include "FixedList.H"
00040 #include "treeBoundBox.H"
00041 #include "Ostream.H"
00042 #include "labelHashSet.H"
00043 #include "labelBits.H"
00044 #include "PackedList.H"
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 namespace Foam
00049 {
00050 
00051 // Class forward declarations
00052 template<class Type> class indexedOctree;
00053 template<class Type> Ostream& operator<<(Ostream&, const indexedOctree<Type>&);
00054 class Istream;
00055 
00056 
00057 /*---------------------------------------------------------------------------*\
00058                         Class indexedOctreeName Declaration
00059 \*---------------------------------------------------------------------------*/
00060 
00061 TemplateName(indexedOctree);
00062 
00063 
00064 /*---------------------------------------------------------------------------*\
00065                            Class indexedOctree Declaration
00066 \*---------------------------------------------------------------------------*/
00067 
00068 template <class Type>
00069 class indexedOctree
00070 :
00071     public indexedOctreeName
00072 {
00073 public:
00074 
00075     // Data types
00076 
00077         //- volume types
00078         enum volumeType
00079         {
00080             UNKNOWN = 0,
00081             MIXED = 1,
00082             INSIDE = 2,
00083             OUTSIDE = 3
00084         };
00085 
00086 
00087     // Tree node. Has up pointer and down pointers.
00088 
00089         class node
00090         {
00091         public:
00092 
00093             //- Bounding box of this node
00094             treeBoundBox bb_;
00095 
00096             //- parent node (index into nodes_ of tree)
00097             label parent_;
00098 
00099             //- IDs of the 8 nodes on all sides of the mid point
00100             FixedList<labelBits, 8> subNodes_;
00101 
00102             friend Ostream& operator<< (Ostream& os, const node& n)
00103             {
00104                 return os << n.bb_ << token::SPACE
00105                     << n.parent_ << token::SPACE << n.subNodes_;
00106             }
00107 
00108             friend Istream& operator>> (Istream& is, node& n)
00109             {
00110                 return is >> n.bb_ >> n.parent_ >> n.subNodes_;
00111             }
00112 
00113             friend bool operator==(const node& a, const node& b)
00114             {
00115                 return
00116                     a.bb_ == b.bb_
00117                  && a.parent_ == b.parent_
00118                  && a.subNodes_ == b.subNodes_;
00119             }
00120 
00121             friend bool operator!=(const node& a, const node& b)
00122             {
00123                 return !(a == b);
00124             }
00125         };
00126 
00127 
00128 private:
00129 
00130     // Private data
00131 
00132         //- Underlying shapes for geometric queries.
00133         const Type shapes_;
00134 
00135         //- List of all nodes
00136         List<node> nodes_;
00137 
00138         //- List of all contents (referenced by those nodes that are contents)
00139         labelListList contents_;
00140 
00141         //- Per node per octant whether is fully inside/outside/mixed.
00142         mutable PackedList<2> nodeTypes_;
00143 
00144     // Private Member Functions
00145 
00146         //- Like above but now bb is implicitly provided as parent bb + mid
00147         //  + octant
00148         static bool intersects
00149         (
00150             const treeBoundBox& parentBb,
00151             const direction octant,
00152             const scalar nearestDistSqr,
00153             const point& sample
00154         );
00155 
00156         // Construction
00157 
00158             //- Split list of indices into 8 bins according to where they are in
00159             //  relation to mid.
00160             void divide
00161             (
00162                 const labelList& indices,
00163                 const treeBoundBox& bb,
00164                 labelListList& result
00165             ) const;
00166 
00167             //- Subdivide the contents node at position contentI. Appends to
00168             //  contents.
00169             node divide
00170             (
00171                 const treeBoundBox& bb,
00172                 DynamicList<labelList>& contents,
00173                 const label contentI
00174             ) const;
00175 
00176             //- Split any contents node with more than minSize elements.
00177             void splitNodes
00178             (
00179                 const label minSize,
00180                 DynamicList<node>& nodes,
00181                 DynamicList<labelList>& contents
00182             ) const;
00183 
00184             static label compactContents
00185             (
00186                 DynamicList<node>& nodes,
00187                 DynamicList<labelList>& contents,
00188                 const label compactLevel,
00189                 const label nodeI,
00190                 const label level,
00191                 List<labelList>& compactedContents,
00192                 label& compactI
00193             );
00194 
00195             //- Determine inside/outside per node (mixed if cannot be
00196             //  determined). Only valid for closed shapes.
00197             volumeType calcVolumeType(const label nodeI) const;
00198 
00199             //- Search cached volume type.
00200             volumeType getVolumeType(const label nodeI, const point&) const;
00201 
00202 
00203         // Query
00204 
00205             //- Find nearest point to line.
00206             void findNearest
00207             (
00208                 const label nodeI,
00209                 const linePointRef& ln,
00210 
00211                 treeBoundBox& tightest,
00212                 label& nearestShapeI,
00213                 point& linePoint,
00214                 point& nearestPoint
00215             ) const;
00216 
00217             //- Walk tree to neighbouring node. Return false if edge of tree
00218             //  hit.
00219             bool walkToNeighbour
00220             (
00221                 const point& facePoint,
00222                 const direction faceID,         // direction to walk in
00223                 label& nodeI,
00224                 direction& octant
00225             ) const;
00226 
00227             //- Categorize point on bounding box.
00228             static direction getFace(const treeBoundBox&, const point&);
00229 
00230             //- Traverse a node. If intersects a triangle return first
00231             // intersection point.
00232             // findAny=true : return any intersection
00233             // findAny=false: return nearest (to start) intersection
00234             void traverseNode
00235             (
00236                 const bool findAny,
00237                 const point& start,
00238                 const point& end,
00239                 const vector& dir,
00240                 const label nodeI,
00241                 const direction octantI,
00242 
00243                 pointIndexHit& hitInfo,
00244                 direction& faceID
00245             ) const;
00246 
00247             //- Find any or nearest intersection
00248             pointIndexHit findLine
00249             (
00250                 const bool findAny,
00251                 const point& treeStart,
00252                 const point& treeEnd,
00253                 const label startNodeI,
00254                 const direction startOctantI
00255             ) const;
00256 
00257             //- Find any or nearest intersection of line between start and end.
00258             pointIndexHit findLine
00259             (
00260                 const bool findAny,
00261                 const point& start,
00262                 const point& end
00263             ) const;
00264 
00265             //- Find all elements intersecting box.
00266             void findBox
00267             (
00268                 const label nodeI,
00269                 const treeBoundBox& searchBox,
00270                 labelHashSet& elements
00271             ) const;
00272 
00273         // Other
00274 
00275             //- Count number of elements on this and sublevels
00276             label countElements(const labelBits index) const;
00277 
00278             //- Dump node+octant to an obj file
00279             void writeOBJ(const label nodeI, const direction octant) const;
00280 
00281             //- From index into contents_ to subNodes_ entry
00282             static labelBits contentPlusOctant
00283             (
00284                 const label i,
00285                 const direction octant
00286             )
00287             {
00288                 return labelBits(-i - 1, octant);
00289             }
00290 
00291             //- From index into nodes_ to subNodes_ entry
00292             static labelBits nodePlusOctant
00293             (
00294                 const label i,
00295                 const direction octant
00296             )
00297             {
00298                 return labelBits(i + 1, octant);
00299             }
00300 
00301             //- Fempty to subNodes_ entry
00302             static labelBits emptyPlusOctant
00303             (
00304                 const direction octant
00305             )
00306             {
00307                 return labelBits(0, octant);
00308             }
00309 
00310 public:
00311 
00312     // Constructors
00313 
00314         //- Construct null
00315         indexedOctree(const Type& shapes);
00316 
00317         //- Construct from components
00318         indexedOctree
00319         (
00320             const Type& shapes,
00321             const List<node>& nodes,
00322             const labelListList& contents
00323         );
00324 
00325         //- Construct from shapes
00326         indexedOctree
00327         (
00328             const Type& shapes,
00329             const treeBoundBox& bb,
00330             const label maxLevels,          // maximum number of levels
00331             const scalar maxLeafRatio,      // how many elements per leaf
00332             const scalar maxDuplicity       // in how many leaves is a shape on
00333                                             // average
00334         );
00335 
00336         //- Construct from Istream
00337         indexedOctree(const Type& shapes, Istream& is);
00338 
00339 
00340     // Member Functions
00341 
00342         // Access
00343 
00344             //- Reference to shape
00345             const Type& shapes() const
00346             {
00347                 return shapes_;
00348             }
00349 
00350             //- List of all nodes
00351             const List<node>& nodes() const
00352             {
00353                 return nodes_;
00354             }
00355 
00356             //- List of all contents (referenced by those nodes that are
00357             //  contents)
00358             const labelListList& contents() const
00359             {
00360                 return contents_;
00361             }
00362 
00363             //- Top bounding box
00364             const treeBoundBox& bb() const
00365             {
00366                 if (nodes_.size() == 0)
00367                 {
00368                     FatalErrorIn("indexedOctree<Type>::bb() const")
00369                         << "Tree is empty" << abort(FatalError);
00370                 }
00371                 return nodes_[0].bb_;
00372             }
00373 
00374 
00375         // Conversions for entries in subNodes_.
00376 
00377             static bool isContent(const labelBits i)
00378             {
00379                 return i.val() < 0;
00380             }
00381 
00382             static bool isEmpty(const labelBits i)
00383             {
00384                 return i.val() == 0;
00385             }
00386 
00387             static bool isNode(const labelBits i)
00388             {
00389                 return i.val() > 0;
00390             }
00391  
00392             static label getContent(const labelBits i)
00393             {
00394                 if (!isContent(i))
00395                 {   
00396                     FatalErrorIn("getContent(const label)")
00397                         << abort(FatalError);
00398                 }
00399                 return -i.val()-1;
00400             }
00401  
00402             static label getNode(const labelBits i)
00403             {
00404                 if (!isNode(i))
00405                 {
00406                     FatalErrorIn("getNode(const label)")
00407                         << abort(FatalError);
00408                 }
00409                 return i.val() - 1;
00410             }
00411 
00412             static direction getOctant(const labelBits i)
00413             {
00414                 return i.bits();
00415             }
00416 
00417 
00418         // Queries
00419 
00420             //- Calculate nearest point on nearest shape. Returns
00421             //  - bool : any point found nearer than nearestDistSqr
00422             //  - label: index in shapes
00423             //  - point: actual nearest point found
00424             pointIndexHit findNearest
00425             (
00426                 const point& sample,
00427                 const scalar nearestDistSqr
00428             ) const;
00429 
00430             //- Low level: calculate nearest starting from subnode.
00431             void findNearest
00432             (
00433                 const label nodeI,
00434                 const point&,
00435 
00436                 scalar& nearestDistSqr,
00437                 label& nearestShapeI,
00438                 point& nearestPoint
00439             ) const;
00440 
00441             //- Find nearest to line. Returns
00442             //  - bool : any point found?
00443             //  - label: index in shapes
00444             //  - point: actual nearest point found
00445             //  sets:
00446             //  - linePoint : corresponding nearest point on line
00447             pointIndexHit findNearest
00448             (
00449                 const linePointRef& ln,
00450                 treeBoundBox& tightest,
00451                 point& linePoint
00452             ) const;
00453 
00454             //- Find nearest intersection of line between start and end.
00455             pointIndexHit findLine
00456             (
00457                 const point& start,
00458                 const point& end
00459             ) const;
00460 
00461             //- Find any intersection of line between start and end.
00462             pointIndexHit findLineAny
00463             (
00464                 const point& start,
00465                 const point& end
00466             ) const;
00467 
00468             //- Find (in no particular order) indices of all shapes inside or
00469             //  overlapping bounding box (i.e. all shapes not outside box)
00470             labelList findBox(const boundBox& bb) const;
00471 
00472             //- Find deepest node (as parent+octant) containing point. Starts
00473             //  off from starting index in nodes_ (use 0 to start from top)
00474             //  Use getNode, getOctant to extract info.
00475             labelBits findNode(const label nodeI, const point&) const;
00476 
00477             //- Determine type (inside/outside/mixed) for point. unknown if
00478             //  cannot be determined (e.g. non-manifold surface)
00479             volumeType getVolumeType(const point&) const;
00480 
00481             //- Helper: does bb intersect a sphere around sample? Or is any
00482             //  corner point of bb closer than nearestDistSqr to sample.
00483             static bool intersects
00484             (
00485                 const point& bbMin,
00486                 const point& bbMax,
00487                 const scalar nearestDistSqr,
00488                 const point& sample
00489             );
00490 
00491         // Write
00492 
00493             //- Print tree. Either print all indices (printContent = true) or
00494             //  just size of contents nodes.
00495             void print
00496             (
00497                 prefixOSstream&,
00498                 const bool printContents,
00499                 const label
00500             ) const;
00501 
00502             bool write(Ostream& os) const;
00503 
00504     // IOstream Operators
00505 
00506         friend Ostream& operator<< <Type>(Ostream&, const indexedOctree<Type>&);
00507 
00508 };
00509 
00510 
00511 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00512 
00513 } // End namespace Foam
00514 
00515 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00516 
00517 #ifdef NoRepository
00518 #   include "indexedOctree.C"
00519 #endif
00520 
00521 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00522 
00523 #endif
00524 
00525 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd