NearestNeighbours.h

Go to the documentation of this file.
00001 
00012 #ifndef _H_NEARESTNEIGHBOURS
00013 #define _H_NEARESTNEIGHBOURS
00014 
00015 #include<vector>
00016 
00017 using namespace std;
00018 
00019 template < typename cur_type >
00020 class NearestNeighbours {
00021         public:
00022 
00023         unsigned int   max;     
00024         unsigned int   size;    
00026         double         maxdist; 
00027         vector<int>    *ids;    
00028         vector<double> *dists;  
00035         NearestNeighbours( unsigned int k ) {
00036                 this->max     = k;
00037                 this->size    = 0;
00038                 this->maxdist = 0;
00039                 this->ids     = new vector< int >();
00040                 this->dists   = new vector< double >();
00041         }
00042 
00049         void insert( double dist, cur_type *element ) {
00050                 if ( this->max > this->size ) {
00051                         /* Less than k neighbours have been found, so add this one */
00052                         if ( dist > this->maxdist ) {
00053                                 this->maxdist = dist;
00054                         }
00055                         this->ids->push_back( element->getID() );
00056                         this->dists->push_back( dist );
00057                         this->size++;
00058                 } else if ( dist >= this->maxdist ) {
00059                         /* The element is further away than the k current neighbours, so do not add this one */
00060                         return;
00061                 } else {
00062                         /* The element is closer than one of the current neighbours, so replace that one */
00063                         double newmaxdist = 0;
00064                         for (unsigned int i = 0; i < this->ids->size(); i++) {
00065                                         if ( this->dists->at( i ) == this->maxdist ) {
00066                                                 this->dists->at( i ) = dist;
00067                                                 this->ids->at( i )   = element->getID();
00068                                                 for (; i < this->ids->size(); i++) {
00069                                                         if (this->dists->at( i ) > newmaxdist) {
00070                                                                 newmaxdist = this->dists->at( i );
00071                                                         }
00072                                                 }
00073                                         break;
00074                                 }
00075                                 if (this->dists->at( i ) > newmaxdist) {
00076                                         newmaxdist = this->dists->at( i );
00077                                 }
00078                         }
00079                         this->maxdist = newmaxdist;
00080                 }
00081         }
00082 
00089         void insert( double dist, int index ) {
00090                 if ( this->max > this->size ) {
00091                         /* Less than k neighbours have been found, so add this one */
00092                         if ( dist > this->maxdist ) {
00093                                 this->maxdist = dist;
00094                         }
00095                         this->ids->push_back( index );
00096                         this->dists->push_back( dist );
00097                         this->size++;
00098                 } else if ( dist >= this->maxdist ) {
00099                         /* The element is further away than the k current neighbours, so do not add this one */
00100                         return;
00101                 } else {
00102                         /* The element is closer than one of the current neighbours, so replace that one */
00103                         double newmaxdist = 0;
00104                         for (unsigned int i = 0; i < this->ids->size(); i++) {
00105                                         if (this->dists->at( i ) == this->maxdist) {
00106                                                 this->dists->at( i ) = dist;
00107                                                 this->ids->at( i )   = index;
00108                                                         for (; i < this->ids->size(); i++) {
00109                                                         if (this->dists->at( i ) > newmaxdist) {
00110                                                                 newmaxdist = this->dists->at( i );
00111                                                         }
00112                                         }
00113                                         break;
00114                                 }
00115                                 if (this->dists->at( i ) > newmaxdist) {
00116                                         newmaxdist = this->dists->at( i );
00117                                 }
00118                         }
00119                         this->maxdist = newmaxdist;
00120                 }
00121         }
00122 
00123 
00131         static double minimumDistance(vector <double> p, const cur_type * const in) {
00132                 const unsigned int dim = in->getDimension();
00133                 double ret = 0;
00134                 for ( unsigned int i = 0; i < dim; i++ )  {
00135                         if (p[ i ] < in->getMinCoordinateOnDimension( i )) {
00136                                 ret += (in->getMinCoordinateOnDimension( i ) - p[ i ]) * (in->getMinCoordinateOnDimension( i ) - p[ i ]);
00137                         } else if (p[ i ] > in->getMaxCoordinateOnDimension( i )) {
00138                                 ret += (p[ i ] - in->getMaxCoordinateOnDimension( i )) * (p[ i ] - in->getMaxCoordinateOnDimension( i ));
00139                         } else {
00140                                 ret += 0;
00141                         }
00142                 }
00143                 return ret;
00144         }
00145 
00146 };
00147 
00148 
00149 
00150 #endif

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