r-tree.h

Go to the documentation of this file.
00001 
00011 #ifndef _H_R_tree
00012 #define _H_R_tree
00013 
00014 #include "NearestNeighbours.h"
00015 #include "Cubic_Bounding_Box.h"
00016 #include "Cubic_Bounding_Box_Container.h"
00017 #include "Spatial_Tree.h"
00018 #include "Cart.h"
00019 
00020 #include <vector>
00021 
00022 #define LINEAR_SPLIT 0
00023 #define QUADRATIC_SPLIT 1
00024 #define EXPONENTIAL_SPLIT 2
00025 
00026 //#define SPLIT_STRATEGY LINEAR_SPLIT
00027 #define SPLIT_STRATEGY QUADRATIC_SPLIT
00028 
00029 //gives tree output to files after each insert operation
00030 //#define _OUTPUT_TREE
00031 
00032 //this flags disables safety checks to gain performance. Use only for production code.
00033 //#define _HP
00034 
00035 #ifdef _NODE_COUNT
00036 
00037 unsigned int _node_count;
00038 
00039 void resetNodeCount() {
00040         _node_count = 0;
00041 }
00042 
00043 #endif
00044 
00045 //checks invariants, prints no debug info to stdout unless the debug flag is set
00046 //warning -- this flag extremely slows program execution
00047 //#define _GREATER_CHECK
00048 
00049 //#define _DEBUG
00050 //#define _DEBUG_LINEAR_SPLIT
00051 
00052 typedef Cubic_Bounding_Box BB_type;
00053 typedef Cubic_Bounding_Box_Container BB_container;
00054 typedef Cart Current_Metric;
00055 
00059 class R_tree_props {
00060    public:
00061         unsigned int maximum; 
00062         unsigned int minimum; 
00063 };
00064 
00068 //template < typename bb_type > Pending...
00069 template < typename r_tree_variation >
00070 class R_tree : public Spatial_Tree< r_tree_variation, BB_type, BB_container > {
00071 
00072         friend class Basic_R_tree;
00073 
00074   private:
00075   
00076         template <class T>
00077         bool from_string(T& t, 
00078                  const std::string& s, 
00079                  std::ios_base& (*f)(std::ios_base&)) const
00080         {
00081                 std::istringstream iss(s);
00082                 return !(iss >> f >> t).fail();
00083         }
00084 
00085   protected:
00086 
00093         void setElements( vector< BB_container* > newElements, BB_type *newMbr );
00094 
00095         //internally used convenience methods
00096 
00101         void init();
00102 
00109         bool full() const;
00110 
00111 #ifdef _GREATER_CHECK
00112 
00119         void checkDepth( const int depth, const int level ) const;
00120 
00124         void checkInvariants() const;
00125 
00129         void checkInvariants( int level ) const;
00130 
00135         void checkEntireTree() const;
00136 
00143         void checkEntireTreeRecursive( int level ) const;
00144 
00148         void checkEntireTreeRecursive() const;
00149 
00156         int getHeightEstimate() const;
00157 
00164         int getLocalHeightEstimate() const;
00165 #endif
00166 
00167         //tree construction helper algorithms
00168 
00178         double areaEnlargement( const BB_type bb1, const BB_type bb2 ) const;
00179 
00189         double deadSpace( const BB_type bb1, const BB_type bb2 ) const;
00190 
00197         void writeTreeToFile( ofstream &ofs ) const;
00198         
00205         void readTreeFromStream( ifstream &ifs ) const;
00206 
00207 
00209         BB_type mbr;
00210 
00212         R_tree_props *properties;
00213 
00218         void nullifyProperties();
00219 
00223         void updateAllMBRs();
00224 
00228         void updateMBRLocal();
00229 
00234         void updateMBRNode();
00235 
00236 
00245         void *updateMBR();
00246 
00247         //spatial queries
00248 
00256         void containedIn( const BB_type *box, vector< int > *ret ) const;
00257 
00267         void intersects( const vector< double > &begin, const vector< double > &end, vector< int > *ret ) const;
00268 
00276         void intersects( const Point &point, vector< int > *ret ) const;
00277 
00294         void intersects( const vector<double> &a, const double b, vector< int > *ret ) const;
00295 
00304         void neighboursOf( const BB_container *item, vector< int > *ret ) const;
00305 
00313         void neighboursOf( const vector<double> &point, NearestNeighbours<BB_container> *ns, MinDistTreeOrdering<BB_type, r_tree_variation> *mdt) const;
00314 
00315         typedef typename vector< r_tree_variation * >::const_iterator childIterator;
00316 
00317   public:
00318 
00320         bool deleteLeaves;
00321 
00322         //constructors & deconstructor
00323 
00327         R_tree< r_tree_variation >();
00328 
00332         R_tree< r_tree_variation >( R_tree_props *props );
00333 
00337         virtual ~R_tree< r_tree_variation >();
00338 
00346         void gatherTreeInfo( int *x, int *y ) const;
00347 
00355          const BB_type *getMBR() const { return &mbr; }
00356 
00363         virtual void postBulkLoad() { }
00364 
00370         R_tree< r_tree_variation >* getRoot() { if ( this->parent == 0 ) return this; else return this->parent->getRoot(); }
00371 
00372         //(spatial) tree methods:
00373 
00374                 //not implemented in this abstract class
00375 
00376         //spatial queries
00377 
00379         virtual vector< int > containedIn( const BB_type &box ) const;
00380 
00382         virtual vector< int > intersects( const vector< double > &begin, const vector< double > &end ) const;
00383 
00385         virtual vector< int > intersects( const Point &point ) const;
00386 
00388         virtual vector< int > intersects( const vector<double> &a, const double b ) const;
00389 
00391         virtual vector< int > neighboursOf( const BB_container *item ) const;
00392 
00394         virtual vector< int > neighboursOf( const vector<double> &point, const unsigned int k ) const;
00395 
00396         //special
00397 
00404         void writeTreeToFile( string fn ) const;
00405 
00412         void readTreeFromFile( string fn ) const;
00413 
00420         void printTreeInfo( ostringstream &oss ) const;
00421 
00422 #ifdef _NODE_COUNT
00423 
00429         virtual int visitedNodes() const { return _node_count; }
00430 #endif
00431 
00432 
00433         //debug funcs -- not documented.
00434         void printTreeToStdOut( const int x, const void* par ) const;
00435 
00436 };
00437 
00438 
00439 //debug funcs:
00440 template< typename r_tree_variation >
00441 void R_tree< r_tree_variation >::printTreeToStdOut( const int x, const void* par ) const {
00442         for( int i=0; i<x; i++ )
00443                 cout << " ";
00444         cout << "|";
00445         if ( this->parent != par )
00446                 cout << "*";
00447         else if ( this->children.size() < properties->minimum && !this->isLeaf() )
00448                 cout << "u";
00449         else if ( this->children.size() > properties->maximum && !this->isLeaf() )
00450                 cout << "O";
00451         else if ( this->elements.size() < properties->minimum && this->isLeaf() )
00452                 cout << "u";
00453         else if ( this->elements.size() > properties->maximum && this->isLeaf() )
00454                 cout << "O";
00455         else
00456                 cout << "-";
00457         cout << endl;
00458         if ( this->isLeaf() )
00459                 return;
00460         for( unsigned int i=0; i<this->children.size(); i++ ) {
00461                 this->children[i]->printTreeToStdOut( x+1, this );
00462         }
00463 }
00464 
00465 /****************************************
00466  *          PRIVATE FUNCTIONS           *
00467  ****************************************/
00468 
00469 template< typename r_tree_variation >
00470 void R_tree< r_tree_variation >::writeTreeToFile( ofstream &ofs ) const {
00471 
00472         mbr.writeToFile( ofs );
00473 
00474         if ( this->isLeaf() ) {
00475                 for ( unsigned int i=0; i<this->elements.size(); i++ ) {
00476                         this->elements[i]->writeToFile( ofs );
00477                         ofs << "-2 0 0 0 0 0 0" << endl;
00478                 }
00479         } else {
00480                 for ( unsigned int i=0; i<this->children.size(); i++ ) {
00481                         this->children[i]->writeTreeToFile( ofs );
00482                         ofs << "-2 0 0 0 0 0 0" << endl;
00483                 }
00484         }
00485 }
00486 
00487 template< typename r_tree_variation >
00488 void R_tree< r_tree_variation >::readTreeFromStream( ifstream &ifs ) const {
00489 
00490         //get line
00491         string line; ifs>>line;
00492         
00493         //split off the first number
00494         string first = line.substr( 0, line.length() - line.find( " " ) - 1 );
00495         
00496         //get tag
00497         int tag;
00498         from_string< int >( tag, first, std::dec );
00499         cout << tag;
00500         
00501         exit( 1 ); //debug TODO !
00502         
00503         //case split
00504         if ( tag==-2 ) {
00505                 //going up
00506         } else if ( tag==-1 ) {
00507                 //new internal node
00508         } else {
00509                 //new external node
00510                 unsigned int id;
00511         }
00512 
00513 }
00514 
00515 template< typename r_tree_variation >
00516 void R_tree< r_tree_variation >::gatherTreeInfo( int *numNodes, int *numLeafs ) const {
00517 
00518         (*numNodes)++;
00519         if ( this->isLeaf() ) {
00520                 (*numLeafs) += this->elements.size();
00521         } else {
00522                 childIterator it = this->children.begin();
00523                 for( ; it!=this->children.end(); ++it ) {
00524                         (*it)->gatherTreeInfo( numNodes, numLeafs );
00525                 }
00526         }
00527 
00528 }
00529 
00530 template< typename r_tree_variation >
00531 void R_tree< r_tree_variation >::init() {
00532         this->children.clear();
00533         this->elements.clear();
00534         this->parent = 0;
00535         this->deleteLeaves = true;
00536 }
00537 
00538 #ifdef _GREATER_CHECK
00539 
00540 template< typename r_tree_variation >
00541 void R_tree< r_tree_variation >::checkEntireTree() const {
00542 
00543         getRoot()->checkEntireTreeRecursive( -1 );
00544 
00545 }
00546 
00547 template< typename r_tree_variation >
00548 void R_tree< r_tree_variation >::checkEntireTreeRecursive() const { checkEntireTreeRecursive( 0 ); }
00549 
00550 template< typename r_tree_variation >
00551 void R_tree< r_tree_variation >::checkEntireTreeRecursive( int level ) const {
00552 
00553         checkInvariants( level );
00554 
00555         for ( unsigned int i = 0; i<this->children.size(); i++ )
00556                         this->children[i]->checkEntireTreeRecursive( level + 1 );
00557 
00558 }
00559 
00560 template< typename r_tree_variation >
00561 void R_tree< r_tree_variation >::checkInvariants() const { checkInvariants( -1 ); }
00562 
00563 template< typename r_tree_variation >
00564 void R_tree< r_tree_variation >::checkInvariants( int level ) const {
00565 
00566         //mbr check (are mbr really mbrs?)
00567         if ( isLeaf() && this->elements.size() > 0 ) {
00568                 for ( unsigned int i=0; i<this->elements.size(); i++ ) {
00569                         if ( !mbr.contains( *this->elements[i] ) ) {
00570                                 cerr << "At level " << level << ":" << endl;
00571                                 cerr << "MBR does not contain all elements!" << endl;
00572                                 cerr << "MBR: " << mbr;
00573                                 cerr << "MBR not in it: " << endl << (*this->elements[i]);
00574                                 exit(1);
00575                         }
00576                 }
00577         } else {
00578                 for ( unsigned int i=0; i<this->children.size(); i++ ) {
00579                         if ( !mbr.contains( this->children[i]->mbr ) ) {
00580                                 cerr << "At level " << level << ":" << endl;
00581                                 cerr << "MBR does not contain all children MBR!" << endl;
00582                                 cerr << "MBR: " << endl << mbr;
00583                                 cerr << "MBR not in it: " << endl << this->children[i]->mbr;
00584                                 cerr << "Tree output given to debug_tree_out.crh" << endl;
00585                                 getRoot()->writeTreeToFile( "debug_tree_out.crh" );
00586                                 exit(1);
00587                         }
00588                 }
00589         }
00590 
00591         //max elements invariant
00592         if ( isLeaf() ) {
00593                 if ( this->elements.size() > properties->maximum ) {
00594                         cerr << "At level " << level << ":" << endl;
00595                         cerr << "Maximum elements at leaf node invariant corrupt!" << endl;
00596                         cerr << this->elements.size() << " elements stored at checked node." << endl;
00597                         exit(1);
00598                 }
00599         } else {
00600                 if ( this->children.size() > properties->maximum ) {
00601                         cerr << "At level " << level << ":" << endl;
00602                         cerr << "Maximum children at internal node invariant corrupt!" << endl;
00603                         cerr << this->children.size() << " children stored at checked node." << endl;
00604                         exit(1);
00605                 }
00606         }
00607 
00608         //min elements invariant
00609         if ( !this->isRoot() ) {
00610                 if ( isLeaf() ) {
00611                         if ( this->elements.size() < properties->minimum ) {
00612                                 cerr << "At level " << level << ":" << endl;
00613                                 cerr << "Minimum elements at leaf node invariant corrupt!" << endl;
00614                                 cerr << this->children.size() << " children stored at checked node." << endl;
00615                                 cerr << this->elements.size() << " elements stored at checked node." << endl;
00616                                 exit(1);
00617                         }
00618                 } else {
00619                         if ( this->children.size() < properties->minimum ) {
00620                                 cerr << "At level " << level << ":" << endl;
00621                                 cerr << "Minimum children at internal node invariant corrupt!" << endl;
00622                                 cerr << this->children.size() << " children stored at checked node." << endl;
00623                                 cerr << this->elements.size() << " elements stored at checked node." << endl;
00624                                 exit(1);
00625                         }
00626                 }
00627         }
00628 
00629         //leaf depth invariant
00630         //only for this subtree
00631         checkDepth( getHeightEstimate(), level );
00632 
00633         //parent-child invariant
00634         if ( !isLeaf() ) {
00635                 for ( unsigned int i=0; i<this->children.size(); i++ ) {
00636                         if ( this->children[i]->parent != this ) {
00637                                 cerr << "At level " << level << ":" << endl;
00638                                 cerr << "Childs parent is not me!" << endl;
00639                                 cerr << this->children[i]->parent << " != " << this << endl;
00640                                 exit(1);
00641                         }
00642                 }
00643         }
00644         if ( !this->isRoot() ) {
00645                 bool okidoki = false;
00646                 for ( unsigned int i=0; i<this->parent->children.size(); i++ ) {
00647                         if ( this->parent->children[i] == this )
00648                                 okidoki = true;
00649                 }
00650                 if ( !okidoki ) {
00651                         cerr << "At level " << level << ":" << endl;
00652                         cerr << "Im not a child of my parent!" << endl;
00653                         cerr << this << " is not a child of " << this->parent << endl;
00654                         exit(1);
00655                 }
00656         }
00657 
00658         //no-clones
00659         if ( !isLeaf() ) {
00660                 for ( unsigned int i=0; i<this->children.size(); i++ ) {
00661                         for ( unsigned int j=0; j<this->children.size(); j++ ) {
00662                                 if ( (i!=j ) && ( this->children[i]==this->children[j] ) ) {
00663                                         cerr << "At level " << level << ":" << endl;
00664                                         cerr << "I have cloned children!" << endl;
00665                                         cerr << "At " << i << " " << this->children[i] << " == " << this->children[j] << " at " << j << endl;
00666                                         exit(1);
00667                                 }
00668                         }
00669                 }
00670         } else {
00671                 for ( unsigned int i=0; i<this->elements.size(); i++ ) {
00672                         for ( unsigned int j=0; j<this->elements.size(); j++ ) {
00673                                 if ( (i!=j ) && ( this->elements[i]->getID()==this->elements[j]->getID() ) ) {
00674                                         cerr << "At level " << level << ":" << endl;
00675                                         cerr << "I have cloned elements!" << endl;
00676                                         cerr << "At " << i << " " << this->elements[i] << " == " << this->elements[j] << " at " << j << endl;
00677                                         exit(1);
00678                                 }
00679                         }
00680                 }
00681         }
00682 }
00683 
00684 template< typename r_tree_variation >
00685 int R_tree< r_tree_variation >::getHeightEstimate() const {
00686         const R_tree< r_tree_variation > *walk = this;
00687         while ( ! (walk->isLeaf() ) ) {
00688                 walk = walk->children.at(0);
00689         }
00690         return walk->getDepth();
00691 }
00692 
00693 template< typename r_tree_variation >
00694 int R_tree< r_tree_variation >::getLocalHeightEstimate() const {
00695         int c=0;
00696         const R_tree< r_tree_variation > *walk = this;
00697         while ( ! (walk->isLeaf() ) ) {
00698                 walk = walk->children.at(0);
00699                 c++;
00700         }
00701         return c;
00702 }
00703 
00704 template< typename r_tree_variation >
00705 void R_tree< r_tree_variation >::checkDepth( const int depth, const int level ) const {
00706 
00707         if ( isLeaf() ) {
00708                 if ( getDepth() != depth ) {
00709                         cerr << "At level " << level << ":" << endl;
00710                         cerr << "Nodes are not at the same depth!" << endl;
00711                         cerr << depth << " != " << getDepth() << endl;
00712                         exit(1);
00713                 }
00714         } else {
00715                 for( unsigned int i=0; i<this->children.size(); i++ )
00716                         this->children[i]->checkDepth( depth, level );
00717         }
00718 
00719 }
00720 #endif
00721 
00722 template< typename r_tree_variation >
00723 double R_tree< r_tree_variation >::deadSpace( const BB_type bb1, const BB_type bb2 ) const {
00724         return Current_Metric::Volume( bb1.unionWith( bb2 ) ) - Current_Metric::Volume( bb1 ) - Current_Metric::Volume( bb2 );
00725 }
00726 
00727 template< typename r_tree_variation >
00728 double R_tree< r_tree_variation >::areaEnlargement( BB_type bb1, BB_type bb2 ) const {
00729 
00730 #ifdef _DEBUG
00731         cout << "areaEnlargement called" << endl; fflush( stdout );
00732         cout << "bb1: " << bb1 << endl; fflush( stdout );
00733         cout << "bb2: " << bb2 << endl; fflush( stdout );
00734         double temp = Current_Metric::Volume( bb1.unionWith( bb2 ) ) - Current_Metric::Volume( bb1 );
00735         cout << "areaEnlargement: " << temp << endl; fflush( stdout );
00736         return temp;
00737 #else
00738         return Current_Metric::Volume( bb1.unionWith( bb2 ) ) - Current_Metric::Volume( bb1 );
00739 #endif
00740 }
00741 
00742 template< typename r_tree_variation >
00743 bool R_tree< r_tree_variation >::full() const {
00744 
00745 #ifdef _DEBUG
00746         cout << "R_tree< r_tree_variation >::full() called" << endl; fflush( stdout );
00747 #endif
00748 
00749         if ( this->isLeaf() )
00750                 return properties->maximum <= this->elements.size();
00751         else
00752                 return properties->maximum <= this->children.size();
00753 }
00754 
00755 
00756 /***************************************
00757  *        PROTECTED FUNCTIONS          *
00758  ***************************************/
00759 
00760 template< typename r_tree_variation >
00761 void R_tree< r_tree_variation >::nullifyProperties() {
00762         properties = 0;
00763         childIterator it = this->children.begin();
00764         for( ; it!=this->children.end(); it++ )
00765                 (*it)->nullifyProperties();
00766 }
00767 
00768 template< typename r_tree_variation >
00769 void R_tree< r_tree_variation >::updateAllMBRs() {
00770         if ( this->isLeaf() )
00771                 updateMBRLocal();
00772         else {
00773                 for ( unsigned int i=0; i<this->children.size(); i++ )
00774                         this->children[i]->updateAllMBRs();
00775                 updateMBRLocal();
00776         }
00777 }
00778 
00779 template< typename r_tree_variation >
00780 void R_tree< r_tree_variation >::updateMBRLocal() {
00781         if ( this->isLeaf() ) {
00782                 mbr.clear();
00783 
00784                 vector< BB_container* >::iterator it = this->elements.begin();
00785                 for ( ; it!=this->elements.end(); it++ ) {
00786                         mbr.unite( *it );
00787                         //mbr = mbr.unite( *( *it ) );
00788 
00789 #ifndef _HP
00790                         if ( !mbr.contains( *( *it ) ) ) {
00791                                 cerr << "Weird!" << endl;
00792                                 exit(1);
00793                         }
00794 #endif
00795 
00796                 }
00797         } else {
00798                 mbr.clear();
00799                 childIterator it = this->children.begin();
00800                 for ( ; it!=this->children.end(); it++ ) {
00801                         mbr.unite( &( (*it)->mbr ) );
00802                         //mbr = mbr.unite( (*it)->mbr );
00803 
00804 #ifndef _HP
00805                         if ( !mbr.contains( (*it)->mbr ) ) {
00806                                 cerr << "Weird!" << endl;
00807                                 exit(1);
00808                         }
00809 #endif
00810 
00811                 }
00812         }
00813 
00814 #ifdef _GREATER_CHECK
00815         if ( !this->isLeaf() ) {
00816                 vector< R_tree< r_tree_variation >* >::iterator it = this->children.begin();
00817                 for ( ; it!=this->children.end(); it++ ) {
00818                         if ( !mbr.contains( (*it)->mbr ) ) {
00819                                 cerr << "Error in updateMBRLocal! (non-leaf)" << endl;
00820                                 exit(1);
00821                         }
00822                 }
00823         }
00824 #endif
00825 
00826 }
00827 
00828 template< typename r_tree_variation >
00829 void R_tree< r_tree_variation >::updateMBRNode() {
00830         updateAllMBRs();
00831         updateMBR();
00832 }
00833 
00834 template< typename r_tree_variation >
00835 void *R_tree< r_tree_variation >::updateMBR() {
00836 #ifdef _DEBUG
00837         cout << "Updating MBR" << endl; fflush( stdout );
00838 #endif
00839 
00840         updateMBRLocal();
00841 
00842         if ( !this->isRoot() ) {
00843 
00844 #ifdef _DEBUG
00845                 cout << "\tDone Updating MBR, going for parent" << endl; fflush( stdout );
00846 #endif
00847 
00848                 return this->parent->updateMBR();
00849         } else {
00850 
00851 #ifdef _DEBUG
00852                 cout << "Done Updating MBRs!" << endl; fflush( stdout );
00853 #endif
00854 
00855                 return this;
00856 
00857         }
00858 }
00859 
00860 template< typename r_tree_variation >
00861 void R_tree< r_tree_variation >::containedIn( const BB_type *box, vector< int > *ret ) const {
00862         typedef typename vector< BB_container * >::const_iterator elemIterator;
00863         if ( this->isLeaf() ) {
00864                 //add the elements satisfying the query
00865                 elemIterator it = this->elements.begin();
00866 
00867                 for ( ; it!=this->elements.end(); it++ ) {
00868 #ifdef _NODE_COUNT
00869                         _node_count++;
00870 #endif
00871                         if ( box->intersects( *it ) ) {
00872                                 ret->push_back( (*it)->getID() );
00873                         }
00874                 }
00875         } else {
00876                 //recurse into the children of this node based on MBR
00877                 childIterator it = this->children.begin();
00878 
00879                 for( ; it!=this->children.end(); it++ ) {
00880 #ifdef _NODE_COUNT
00881                         _node_count++;
00882 #endif
00883                         if ( box->intersects( (*it)->mbr ) ) {
00884                                 (*it)->containedIn( box, ret );
00885                         }
00886                 }
00887         }
00888 }
00889 
00890 template< typename r_tree_variation >
00891 void R_tree< r_tree_variation >::intersects( const vector< double > &begin, const vector< double > &end, vector< int > *ret ) const {
00892 
00893         if ( this->isLeaf() ) {
00894                 //add the elements satisfying the query
00895                 vector< BB_container* >::const_iterator it = this->elements.begin();
00896 
00897                 for ( ; it!=this->elements.end(); it++ ) {
00898 #ifdef _NODE_COUNT
00899                                 _node_count++;
00900 #endif
00901                         if ( (*it)->lineIntersect( begin, end ) )
00902                                 ret->push_back( (*it)->getID() );
00903                 }
00904 
00905         } else {
00906                 //recurse into the children of this node based on MBR
00907                 childIterator it = this->children.begin();
00908                 for( ; it!=this->children.end(); it++ ) {
00909 #ifdef _NODE_COUNT
00910                                 _node_count++;
00911 #endif
00912                         if ( (*it)->mbr.lineIntersect( begin, end ) ) {
00913                                 (*it)->intersects( begin, end, ret );
00914                         }
00915                 }
00916         }
00917 
00918 #ifdef _GREATER_CHECK
00919         checkEntireTree();
00920 #endif
00921 
00922 }
00923 
00924 //should do sth about all this code duplication... function pointers?
00925 
00926 template< typename r_tree_variation >
00927 void R_tree< r_tree_variation >::intersects( const Point &point, vector< int > *ret ) const {
00928 
00929 #ifdef _GREATER_CHECK
00930         checkEntireTree();
00931 #endif
00932 
00933         if ( this->isLeaf() ) {
00934 
00935 #ifdef _DEBUG
00936                 cout << "Pointintersect in leaf, " << this->elements.size() << " elements stored." << endl; fflush( stdout );
00937 #endif
00938 
00939                 vector< BB_container* >::const_iterator it = this->elements.begin();
00940 
00941                 for ( ; it!=this->elements.end(); it++ ) {
00942 #ifdef _DEBUG
00943                         cout << "Visitting element " << (*it) << (*it) << endl; fflush( stdout );
00944                         cout << "With ID " << (*it)->getID() << endl; fflush( stdout );
00945                         cout << "Intersection detection..." << endl; fflush( stdout );
00946 #endif
00947 #ifdef _NODE_COUNT
00948                                 _node_count++;
00949 #endif
00950                         if ( (*it)->contains( point ) ) {
00951                                 ret->push_back( (*it)->getID() );
00952                         }
00953                 }
00954 
00955 #ifdef _DEBUG
00956                 cout << "Exitting pointintersect" << endl; fflush( stdout );
00957 #endif
00958 
00959         } else {
00960                 childIterator it = this->children.begin();
00961                 for ( ; it!=this->children.end(); it++ ) {
00962 #ifdef _NODE_COUNT
00963                                 _node_count++;
00964 #endif
00965                         if ( (*it)->mbr.contains( point ) ) {
00966                                 (*it)->intersects( point, ret );
00967                         }
00968                 }
00969         }
00970 
00971 #ifdef _GREATER_CHECK
00972         checkEntireTree();
00973 #endif
00974 
00975 }
00976 
00977 template< typename r_tree_variation >
00978 void R_tree< r_tree_variation >::intersects( const vector<double> &a, const double b, vector< int > *ret ) const {}
00979 
00980 template< typename r_tree_variation >
00981 void R_tree< r_tree_variation >::neighboursOf( const BB_container *item, vector< int > *ret ) const {}
00982 
00983 template< typename r_tree_variation >
00984 void R_tree< r_tree_variation >::neighboursOf( const vector<double> &point, NearestNeighbours<BB_container> *ns, MinDistTreeOrdering<BB_type, r_tree_variation> *mdt) const {
00985 
00986         
00987         for (unsigned int i = 0; i < this->elements.size(); i++) {
00988                 ns->insert(NearestNeighbours<BB_container>::minimumDistance(point, this->elements.at(i)), this->elements.at(i));
00989 
00990                 #ifdef _NODE_COUNT
00991                         _node_count++;
00992                 #endif
00993         }
00994 
00995         // ga door met de children in volgorde van de mindist van hun boudning box
00996         vector<r_tree_variation *> ch(this->children);
00997         if (ch.size() > 0)
00998                 mdt->sort(&ch);
00999 
01000         
01001 
01002         for (childIterator child = ch.begin(); child != ch.end(); child++) {
01003                 // ga niet in kinderen als ret vol zit en mindist > max van de dists in ret
01004                 if (ns->max <= ns->size && NearestNeighbours<BB_type>::minimumDistance(point, (*child)->getMBR()) >= ns->maxdist) {
01005                         break;
01006                 }
01007                 (*child)->neighboursOf(point, ns, mdt);
01008 
01009                 #ifdef _NODE_COUNT
01010                         _node_count++;
01011                 #endif
01012         }
01013 
01014         return;
01015 };
01016 
01017 
01018 
01019 /****************************************
01020  *          PUBLIC FUNCTIONS            *
01021  ****************************************/
01022 
01023 template< typename r_tree_variation >
01024 R_tree< r_tree_variation >::R_tree() {
01025         properties = new R_tree_props();
01026         properties->minimum=2;
01027         properties->maximum=4;
01028         init();
01029 }
01030 
01031 template< typename r_tree_variation >
01032 R_tree< r_tree_variation >::R_tree( R_tree_props *props ) {
01033         properties = props;
01034         init();
01035 }
01036 
01037 template< typename r_tree_variation >
01038 R_tree< r_tree_variation >::~R_tree() {
01039         if ( this->isRoot() && properties!=0 ) {
01040                 //delete properties;
01041                 nullifyProperties();
01042         }
01043 
01044         if ( !this->isLeaf() ) {
01045                 childIterator it = this->children.begin();
01046                 for( ; it!=this->children.end(); it++ )
01047                         delete *it;
01048         } else if ( deleteLeaves ) {
01049                 vector< BB_container* >::iterator it = this->elements.begin();
01050                 for( ; it!=this->elements.end(); it++ )
01051                         delete *it;
01052         }
01053 
01054 #ifdef _ROOT_NODE_DESTRUCTION_WARNING
01055         if ( this->isRoot() )
01056                 cerr << endl << "WARNING: ROOT NODE DESTROYED!" << endl;
01057 #endif
01058 
01059 }
01060 
01061 template< typename r_tree_variation >
01062 void R_tree< r_tree_variation >::writeTreeToFile( string fn ) const {
01063         ofstream ofs;
01064         ofs.open( fn.c_str(), ios::out | ios::trunc );
01065         writeTreeToFile( ofs );
01066         ofs.close();
01067 }
01068 
01069 template< typename r_tree_variation >
01070 void R_tree< r_tree_variation >::readTreeFromFile( string fn ) const {
01071         ifstream ifs;
01072         ifs.open( fn.c_str(), ios::in );
01073         readTreeFromStream( ifs );
01074         ifs.close();
01075 }
01076 
01077 template< typename r_tree_variation >
01078 vector< int > R_tree< r_tree_variation >::containedIn( const BB_type &box ) const {
01079 #ifdef _NODE_COUNT
01080         resetNodeCount();
01081 #endif
01082         vector< int > ret;
01083 
01084         containedIn( &box, &ret );
01085 
01086 #ifdef _GREATER_CHECK
01087         checkEntireTree();
01088 #endif
01089 
01090         return ret;
01091 }
01092 
01093 template< typename r_tree_variation >
01094 vector< int > R_tree< r_tree_variation >::intersects( const vector< double > &begin, const vector< double > &end ) const {
01095 #ifdef _NODE_COUNT
01096         resetNodeCount();
01097 #endif
01098         vector< int > ret;
01099 
01100         intersects( begin, end, &ret );
01101 
01102         //return
01103         return ret;
01104 }
01105 
01106 template< typename r_tree_variation >
01107 vector< int > R_tree< r_tree_variation >::intersects( const Point &point ) const {
01108 #ifdef _NODE_COUNT
01109         resetNodeCount();
01110 #endif
01111         vector< int > ret;
01112 
01113         intersects( point, &ret );
01114 
01115         return ret;
01116 }
01117 
01118 template< typename r_tree_variation >
01119 vector< int > R_tree< r_tree_variation >::intersects( const vector<double> &a, const double b ) const {
01120 #ifdef _NODE_COUNT
01121         resetNodeCount();
01122 #endif
01123         cerr << "Error: plane intersection not yet implemented!" << endl;
01124         exit(1);
01125         return vector< int >();
01126 }
01127 
01128 template< typename r_tree_variation >
01129 vector< int > R_tree< r_tree_variation >::neighboursOf( const BB_container *item ) const {
01130 #ifdef _NODE_COUNT
01131         resetNodeCount();
01132 #endif
01133         cerr << "Error: neighbour search not yet implemented!" << endl;
01134         exit(1);
01135         return vector< int >();
01136 }
01137 
01138 
01139 template< typename r_tree_variation >
01140 vector< int > R_tree< r_tree_variation >::neighboursOf( const vector<double> &point, const unsigned int k ) const {
01141 #ifdef _NODE_COUNT
01142         resetNodeCount();
01143 #endif
01144 
01145         NearestNeighbours<BB_container> *ns = new NearestNeighbours<BB_container>(k);
01146         MinDistTreeOrdering<BB_type, r_tree_variation> *mdt = new MinDistTreeOrdering<BB_type, r_tree_variation>(point);
01147 
01148         neighboursOf( point, ns, mdt );
01149 
01150         vector<int> ret = *(ns->ids);
01151         delete ns;
01152         delete mdt;
01153 
01154         return ret;
01155 }
01156 
01157 
01158 
01159 template< typename r_tree_variation >
01160 void R_tree< r_tree_variation >::printTreeInfo( ostringstream &oss ) const {
01161         int numNodes = 0;
01162         int numLeafs = 0;
01163         int height = this->getHeight();
01164 
01165         this->gatherTreeInfo( &numNodes, &numLeafs );
01166 
01167         oss << "Tree height: \t\t" << height << endl;
01168         oss << "Internal nodes: \t" << numNodes << endl;
01169         oss << "Leaf nodes: \t\t" << numLeafs << endl;
01170 
01171 }
01172 
01173 template< typename r_tree_variation >
01174 void R_tree< r_tree_variation >::setElements( vector< BB_container* > newElements, BB_type *newMbr ) {
01175 
01176 #ifdef _DEBUG
01177         cout << "Entering setElements" << endl; fflush( stdout );
01178 #endif
01179 
01180         this->elements.clear();
01181         vector< BB_container* >::iterator it = newElements.begin();
01182         for( ; it!=newElements.end(); it++ ) {
01183 #ifdef _DEBUG
01184                 cout << "Adding element " << endl << *(*it) << endl; fflush( stdout );
01185 #endif
01186                 this->elements.push_back( *it );
01187         }
01188 
01189         this->children.clear();
01190 
01191         mbr = BB_type( newMbr );
01192 
01193 #ifdef _GREATER_CHECK
01194         checkInvariants();
01195 #endif
01196 
01197 #ifdef _DEBUG
01198         cout << "Exitting setElements" << endl; fflush( stdout );
01199 #endif
01200 
01201 }
01202 
01203 #endif

Generated on Sat Oct 13 17:34:42 2007 for R-Tree by  doxygen 1.5.2