Space-efficient geometric algorithms and data structures

By Ilya Katz and Hervé Brönnimann    

dkd.hpp

Go to the documentation of this file.
00001 
00010 #ifndef KD_TREE_HPP
00011 #define KD_TREE_HPP
00012 
00013 #include <iostream>
00014 #include <algorithm>
00015 
00016 #include <boost/array.hpp>
00017 #include "cgl.hpp"
00018 
00019 
00020 namespace inplaceds
00021 {
00022         
00023         #define STATIC_KD_TREE_CUTOFF  16
00024         
00025         template <class RandomAccessIterator, class AxisAlignedGeometry>
00026         void
00027         build_kdtree(RandomAccessIterator first, RandomAccessIterator last,
00028                                  AxisAlignedGeometry geom,
00029                                  size_t dimensions)
00030         {
00031           build_kdtree(first, last, geom, dimensions, 1);
00032         }
00033         
00037         template <class RandomAccessIterator, class AxisAlignedGeometry>
00038         void
00039         build_kdtree(RandomAccessIterator first, RandomAccessIterator last,
00040                                  AxisAlignedGeometry geom,
00041                                  size_t dimensions,
00042                                  size_t pointDimension)
00043         {
00044 
00045           //stop recursing if minimum size is reached
00046           if(last - first < STATIC_KD_TREE_CUTOFF)
00047           {
00048             return;
00049           }
00050                 
00051           RandomAccessIterator mid = (first + (last-first+1)/2);
00052           RandomAccessIterator Lmid = (first + (mid-first+1)/2);
00053           RandomAccessIterator Rmid = (mid + (last-mid+1)/2);
00054         
00055           if(dimensions == 1)
00056           {     
00057             std::partial_sort (first, mid, last, geom.less_nth_object(pointDimension)) ;
00058           }
00059           else
00060           {
00061             std::partial_sort(first, mid, last, geom.less_nth_object(pointDimension) );
00062             build_kdtree(first, mid, geom, dimensions-1, pointDimension+1);
00063             build_kdtree(mid+1, last, geom, dimensions-1, pointDimension+1);    
00064           }
00065                 
00066           //----recursive calls----
00067         
00068           //recursively partition Lbot
00069           build_kdtree(first, Lmid, geom, dimensions, pointDimension);
00070         
00071           //recursively partition Ltop
00072           build_kdtree(Lmid+1, mid, geom, dimensions, pointDimension);
00073         
00074           //recursively partition Rbot
00075           build_kdtree(mid+1, Rmid, geom, dimensions, pointDimension);
00076         
00077           //recursively partition Rtop
00078           build_kdtree(Rmid+1, last, geom, dimensions, pointDimension);
00079         
00080         }
00081         
00082         #undef STATIC_KD_TREE_CUTOFF
00083         
00084 }       
00085 #endif

Code Documentation generated Using Doxygen

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