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