library of assembled shared sources

http://lass.cocamware.com

kd_tree.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::spat::KdTree
00046  *  @brief a KD tree for point-like objects
00047  *  @author Bram de Greve [BdG]
00048  *
00049  *  the KD tree does NOT own the objects.  You must keep them yourself!
00050  */
00051 
00052 #ifndef LASS_GUARDIAN_OF_INCLUSION_SPAT_KD_TREE_H
00053 #define LASS_GUARDIAN_OF_INCLUSION_SPAT_KD_TREE_H
00054 
00055 //#define LASS_SPAT_KD_TREE_DIAGNOSTICS
00056 
00057 #include "spat_common.h"
00058 
00059 namespace lass
00060 {
00061 namespace spat
00062 {
00063 
00064 template <typename ObjectType>
00065 struct KdTreeObjectTraits
00066 {
00067     typedef const ObjectType* TObjectIterator;
00068     typedef const ObjectType& TObjectReference;
00069     typedef ObjectType TPoint;
00070     typedef typename TPoint::TValue TValue;
00071     typedef typename TPoint::TParam TParam;
00072     typedef typename TPoint::TReference TReference;
00073     typedef typename TPoint::TConstReference TConstReference;
00074     enum { dimension = TPoint::dimension };
00075 
00076     static const TPoint& position(TObjectIterator object) { return *object; }
00077 };
00078 
00079 
00080 
00081 template
00082 <
00083     class ObjectType,
00084     class ObjectTraits = KdTreeObjectTraits<ObjectType>
00085 >
00086 class KdTree
00087 {
00088 public:
00089 
00090     typedef KdTree<ObjectType, ObjectTraits> TSelf;
00091 
00092     typedef ObjectType TObject;
00093     typedef ObjectTraits TObjectTraits;
00094 
00095     typedef typename TObjectTraits::TObjectIterator TObjectIterator;
00096     typedef typename TObjectTraits::TObjectReference TObjectReference;
00097     typedef typename TObjectTraits::TPoint TPoint;
00098     typedef typename TObjectTraits::TValue TValue;
00099     typedef typename TObjectTraits::TParam TParam;
00100     typedef typename TObjectTraits::TParam TReference;
00101     typedef typename TObjectTraits::TParam TConstReference;
00102 
00103     enum { dimension = TObjectTraits::dimension };
00104 
00105     class Neighbour
00106     {
00107     public:
00108         Neighbour();
00109         Neighbour(TObjectIterator object, TValue squaredDistance);
00110 
00111         TObjectIterator object() const;
00112         TPoint position() const;
00113         TValue squaredDistance() const;
00114 
00115         TObjectIterator operator->() const;
00116         TObjectReference operator*() const;
00117         bool operator<(const Neighbour& other) const;
00118     private:
00119         TObjectIterator object_;
00120         TValue squaredDistance_;
00121     };
00122 
00123     typedef std::vector<Neighbour> TNeighbourhood;
00124 
00125     KdTree();
00126     KdTree(TObjectIterator first, TObjectIterator last);
00127 
00128     void reset();
00129     void reset(TObjectIterator first, TObjectIterator last);
00130 
00131     Neighbour nearestNeighbour(const TPoint& target) const; 
00132     TValue rangeSearch(const TPoint& target, TParam maxRadius, size_t maxCount,
00133             TNeighbourhood& neighbourhood) const;
00134 
00135     template <typename OutputIterator> 
00136     OutputIterator rangeSearch(const TPoint& target, TParam maxRadius, 
00137             OutputIterator first) const;    
00138     template <typename RandomIterator>
00139     RandomIterator rangeSearch(const TPoint& target, TParam maxRadius, size_t maxCount,
00140             RandomIterator first) const;    
00141 
00142     void swap(TSelf& other);
00143     const bool isEmpty() const;
00144     void clear();
00145 
00146     const TObjectIterator end() const;
00147 
00148 #ifdef LASS_SPAT_KD_TREE_DIAGNOSTICS
00149     void diagnostics();
00150 #endif
00151 
00152 private:
00153 
00154     typedef std::vector<TObjectIterator> TObjectIterators;
00155     typedef typename TObjectIterators::iterator TIteratorIterator;
00156 
00157     typedef unsigned char TAxis;
00158     typedef std::vector<TAxis> TAxes;
00159 
00160     enum { dummyAxis_ = TAxis(-1)} ;
00161 
00162     class LessDim
00163     {
00164     public:
00165         LessDim(TAxis split): split_(split) {}
00166         bool operator()(const TObjectIterator& a, const TObjectIterator& b) const
00167         {
00168             return TObjectTraits::position(a)[split_] < TObjectTraits::position(b)[split_];
00169         }
00170     private:
00171         TAxis split_;
00172     };
00173 
00174     class Node
00175     {
00176     public:
00177         Node(TObjectIterator object, TAxis axis = dummyAxis_): object_(object), axis_(axis) {}
00178         const TObjectIterator& object() const { return object_; }
00179         const TAxis axis() const { return axis_; }
00180         const TPoint position() const { return TObjectTraits::position(object_); }
00181     private:
00182         TObjectIterator object_;
00183         TAxis axis_;
00184     };
00185     typedef std::vector<Node> TNodes;
00186 
00187     void buildHeap();
00188     void balance(size_t index, TIteratorIterator first, TIteratorIterator last);
00189     TAxis findSplitAxis(TIteratorIterator first, TIteratorIterator last) const;
00190     void assignNode(size_t index, TObjectIterator object, TAxis splitAxis);
00191     size_t findNode(size_t index, const TPoint& target) const;
00192 
00193     void doNearestNeighbour(size_t index, const TPoint& target, Neighbour& best) const;
00194 
00195     template <typename OutputIterator>
00196     OutputIterator doRangeSearch(size_t index, const TPoint& target, 
00197         TParam squaredRadius, OutputIterator output) const;
00198     template <typename RandomIterator>
00199     RandomIterator doRangeSearch(size_t index, const TPoint& target, TReference squaredRadius, 
00200         size_t maxCount, RandomIterator first, RandomIterator last) const;
00201 
00202     static TValue squaredDistance(const TPoint& a, const TPoint& b);
00203 
00204     TNodes heap_;
00205     TObjectIterator begin_;
00206     TObjectIterator end_;
00207     size_t size_;
00208 };
00209 
00210 
00211 }
00212 
00213 }
00214 
00215 #include "kd_tree.inl"
00216 
00217 #endif
00218 
00219 // EOF

Generated on Mon Nov 10 14:20:06 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo