Space-efficient geometric algorithms and data structures

By Ilya Katz and Hervé Brönnimann    

kd_tree.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright © Ilya Katz and Herve Bronnimann, 2005. 
00003  */
00004 
00013 #ifndef D_KD_TREE
00014 #define D_KD_TREE
00015 
00016 #include <vector>
00017 #include "dynamic_kd_tree.hpp"
00018 
00019 namespace inplaceds
00020 {
00021 
00022 
00023         template <class _Point,
00024                           class _AxisAlignedGeometry,
00025                           class _Sequence = std::vector<_Point> >
00026         class kd_tree
00027         {
00028                 
00029                 public:
00030                 
00031                 typedef typename _Sequence::value_type                  point_type;
00032                 typedef typename _Sequence::iterator                    iterator;
00033                 typedef typename _Sequence::const_iterator      const_iterator;
00034                 typedef  _AxisAlignedGeometry                                   geometry;
00035                 typedef  _Sequence                                                              sequence;
00036         
00038                 protected:
00039                 _Sequence _points;
00040                 _AxisAlignedGeometry geom;
00042                 
00043                 public:
00044                 
00045                 
00052                 kd_tree(){};
00053                 
00064                 template<class InputIterator>  
00065                 kd_tree(InputIterator first, InputIterator last):_points(distance(first,last))
00066                 {       
00067                         std::copy(first, last, this->_points.begin());
00068                         build_dynamic_kdtree(this->_points.begin(), this->_points.end(), geom);
00069                 }
00070                 
00075                 kd_tree(const _Sequence& lhs):_points(lhs.size())
00076                 {               
00077                         std::copy(lhs.begin(), lhs.end(), this->_points.begin());
00078                         build_dynamic_kdtree(this->_points.begin(), this->_points.end(), this->geom);
00079                 }
00080                 
00084                 kd_tree(const kd_tree& lhs):_points(lhs.points.size())
00085                 {
00086                         std::copy(lhs.points._begin(), lhs._points.end(), this->_points.begin());
00087                 }
00088                 
00100                 void insert(point_type p)
00101                 {
00102                         this->_points.insert(this->_points.end(), p);
00103                         insert_dynamic_kdtree(this->_points.begin(), this->_points.end(), this->geom);
00104                 }
00105                 
00115                 void remove(iterator pos)
00116                 {
00117                         remove_dynamic_kdtree(this->_points.begin(), pos, this->_points.end(), this->geom);
00118                         iterator it=this->_points.end();
00119                         this->_points.erase(--it);
00120                 }
00121                 
00131                 const_iterator find(point_type p) const
00132                 {
00133                         for(iterator it = this->_points.begin(); it!=this->_points.end(); ++it)
00134                         {
00135                                 if(*it==p)
00136                                 {
00137                                         return it;
00138                                 }
00139                         }
00140                         
00141                         return this->_points.end();
00142                 }
00143                 
00144                         
00148                 const_iterator npos() const
00149                 {
00150                         return (this->_points).end();
00151                 }
00152                 
00172                 template <class Box>
00173                 iterator box_query( Box Q, sequence& result) const
00174                 {
00175                         
00176                         window_query_dynamic_kdtree(this->_points.begin(), this->_points.end(), this->geom,
00177                                                                                 Q, std::back_inserter(result));
00178                         return result.begin();
00179                 }
00180                 
00198                 template <class Halfplane>
00199                 iterator halfplane_query(Halfplane Q, sequence& result) const
00200                 {
00201                         
00202                         halfplane_query_dynamic_kdtree(this->_points.begin(), this->_points.end(), this->geom,
00203                                                                                 Q, std::back_inserter(result));
00204                         return result.begin();
00205                 }
00206         
00223                 template <class Comp>
00224                 iterator query(Comp Q, sequence& result) const
00225                 {
00226                         for(iterator it=this->_points.begin(); it!=this->_points.end(); ++it)
00227                         {
00228                                 if(Q(*it))
00229                                 {
00230                                         result.insert(result.end(), *it);       
00231                                 }
00232                         }
00233                         return result.begin();
00234                 }
00235                 
00239                 const_iterator points() const
00240                 {
00241                         return (this->_points).begin();
00242                 }
00243                 
00244         };
00245         
00246 }//namespaces
00247 
00248 
00249 
00250 #endif

Code Documentation generated Using Doxygen

Copyright © Ilya Katz and Hervé Brönnimann, 2005, 2006.