cgma
OctTree.hpp
Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // Filename      : NodeSearch.hpp
00003 //
00004 // Purpose       : Class for O(ln n) search for nodes within a specified
00005 //                 tolerance of a specified position.
00006 //
00007 // Special Notes : 
00008 //
00009 // Creator       : Jason Kraftcheck
00010 //
00011 // Creation Date : 01/30/02
00012 //-------------------------------------------------------------------------
00013 
00014 #ifndef OCT_TREE_HPP
00015 #define OCT_TREE_HPP
00016 
00017 #include "CubitVector.hpp"
00018 #include "DLIList.hpp"
00019 
00020 template <class X, class E> class OctTreeCell;
00021 template <class X, class E> struct OctTreeEntry;
00022 
00023 template <class X> class DefaultPointQuery{
00024   public: static inline CubitVector coordinates(const X* x)
00025     { return x->coordinates(); }
00026 };
00027 
00028 template < class X, class E = DefaultPointQuery<X> >
00029 class OctTree
00030 {
00031 
00032   public:
00033   
00034     OctTree( DLIList<X*>& nodes, double tolerance,
00035                 int min_nodes_per_box = -1,
00036                 double min_box_dimension = -1.0 );
00037       //- Constructor
00038       //- nodes     - list of nodes to build oct-tree for
00039       //- tolerance - tolerance for comparison of node positions
00040       //- min_nodes_per_box - don't subdivide boxes in tree with 
00041       //-                     this many or fewer nodes.
00042       //- min_box_dimension - don't subdivide boxes in tree if
00043       //-                     all three dimensions are less than this
00044     
00045     ~OctTree();
00046       // - Destructor
00047     
00048     void nodes_near( const CubitVector& position,
00049                      DLIList<X*>& result_list );
00050       //- Get the list of all nodes in the tree for which 
00051       //- the distance between the passed position and the 
00052       //- node is less than or equal to the tolerance 
00053       //- value passed to the constructor.
00054     
00055     void tree_size( DLIList<int>& count_at_each_level,
00056                     DLIList<int>& leaves_at_each_level );
00057       //- Get the size of the tree at each level.
00058     
00059   private:
00060   
00061     double tolerance_;
00062     double min_box_size_;
00063     int min_nodes_;
00064       // values passed to constructor
00065   
00066     OctTreeCell<X,E>* root_;
00067       // root of oct-tree
00068     
00069     CubitVector min_;
00070     CubitVector max_;
00071       // bounding box of entire oct-tree (and size of root box)
00072     
00073     OctTreeEntry<X,E>* node_memory_;
00074       // memory allocated for linked-list entries
00075       // this is allocated by the constructor, passed to
00076       // the root node of the oct-tree for its internal use
00077       // and released by the destructor.  All use of this
00078       // memory is internal to the NodeSearchBox class.
00079 
00080     void split_node( OctTreeCell<X,E>* node,
00081                      const CubitVector& min,
00082                      const CubitVector& max );
00083       //- Recursive function to subdivide octree nodes
00084   
00085     OctTreeCell<X,E>* allocate_8();
00086       //- Allocate a block of 8 oct-tree nodes for use as
00087       //- children of some current node.
00088     
00089     DLIList<OctTreeCell<X,E>*> mem_pages_;
00090     OctTreeCell<X,E>* curr_page_end_;
00091       //- This data is the memory pool from which allocate_8()
00092       //- allocates oct-tree nodes.
00093   
00094     void push_search_box( int quadrant_flags );
00095       //- Push the child box of 'current_box' indicated
00096       //- by quadrant_flags onto box_list stack or onto
00097       //- leaf_list.
00098     
00099     void pop_to_current();
00100       //- Pop one box from box_list and store it in 
00101       //- current_* variables.  Also updates 'center'
00102     
00103     OctTreeCell<X,E>* box_list[64];  // stack of none-leaf boxes to be searched
00104     CubitVector box_min[64]; // minimum of boxes in box_list
00105     CubitVector box_max[64]; // maximum of boxes in box_list
00106     int box_count;          // count of boxes in box_list
00107     DLIList<X*> search_results; // results of a search
00108       // State information for tree search
00109     
00110     CubitVector current_min;
00111     CubitVector current_max;
00112     CubitVector center;
00113     OctTreeCell<X,E>* current_box;
00114       // More state information for tree search
00115       // The 'current' box during a search, and related info.
00116       
00117       
00118     void tree_size( OctTreeCell<X,E>* box,
00119                     DLIList<int>& boxes,
00120                     DLIList<int>& leaves );
00121                     
00122 };
00123 
00124 #include "OctTree.cpp"
00125 
00126 #endif
00127 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines