NPL
Neurological Programs and Libraries
kdtree.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file kdtree.h
9  *
10  *****************************************************************************/
11 
12 #ifndef KDTREE_H
13 #define KDTREE_H
14 
15 #include <list>
16 #include <vector>
17 
18 namespace npl {
19 
20 template <size_t K, size_t E, typename T = double, typename D = double>
21 class KDTree;
22 
23 template <size_t K, size_t E, typename T = double, typename D = double>
25 {
26  public:
27  KDTreeNode(const std::vector<T>& pt, const std::vector<D>& data) :
28  left(NULL), right(NULL)
29  {
30  size_t ii;
31  for(ii=0; ii<K && ii<pt.size(); ii++)
32  m_point[ii] = pt[ii];
33  for(; ii<K; ii++)
34  m_point[ii] = 0;
35 
36  for(ii=0; ii<E && ii<data.size(); ii++)
37  m_data[ii] = data[ii];
38  for(; ii<E; ii++)
39  m_data[ii] = 0;
40  };
41 
42  KDTreeNode(size_t plen, const T* pt, size_t dlen, const D* data) :
43  left(NULL), right(NULL)
44  {
45  size_t ii;
46  for(ii=0; ii<K && ii<plen; ii++)
47  m_point[ii] = pt[ii];
48  for(; ii<K; ii++)
49  m_point[ii] = 0;
50 
51  for(ii=0; ii<E && ii<dlen; ii++)
52  m_data[ii] = data[ii];
53  for(; ii<E; ii++)
54  m_data[ii] = 0;
55  };
56 
57  T m_point[K];
58  D m_data[E];
59  private:
60  KDTreeNode* left;
61  KDTreeNode* right;
62 
63  friend KDTree<K,E,T,D>;
64 
65  KDTreeNode(const KDTreeNode& other)
66  {
67  (void)(other);
68  throw -1;
69  }
70 
71  KDTreeNode(KDTreeNode&& other)
72  {
73  (void)(other);
74  throw -1;
75  }
76 };
77 
78 template <size_t K, size_t E, typename T, typename D>
79 class KDTree
80 {
81 public:
82 
86  KDTree() : m_built(false),m_treehead(NULL) { };
87 
88  KDTree(KDTree&& other) {
89  m_allnodes = std::move(other.m_allnodes);
90  m_built = other.m_built;
91  m_treehead = other.m_treehead;
92  other.m_allnodes.clear();
93  };
94 
95  KDTree(const KDTree& other) {
96  (void)other;
97  throw "Copy Constructor Not Yet Implemented";
98  };
99 
101  while(!m_allnodes.empty()) {
102  delete m_allnodes.back() ;
103  m_allnodes.pop_back();
104  }
105  };
106 
114  void insert(const std::vector<T>& pt, const std::vector<D>& data);
115 
125  void insert(size_t plen, const T* pt, size_t dlen, const D* data);
126 
130  void build();
131  bool built() { return built; };
132 
136  void clear();
137 
138  const KDTreeNode<K,E,T,D>* nearest(const std::vector<T>& pt, double& dist) const;
139  const KDTreeNode<K,E,T,D>* nearest(size_t len, const T* pt, double& dist) const;
140 
141  std::list<const KDTreeNode<K,E,T,D>*> withindist(const std::vector<T>& pt, double dist) const;
142  std::list<const KDTreeNode<K,E,T,D>*> withindist(size_t len, const T* pt, double dist) const;
143 
144 private:
145 
146  /**********
147  * Data
148  **********/
149  // whether we have constructed the tree yet
150  bool m_built;
151 
152  KDTreeNode<K,E,T,D>* m_treehead;
153  std::vector<KDTreeNode<K,E,T,D>*> m_allnodes;
154 
155  /********************
156  * Helper Functions
157  ********************/
158  const KDTreeNode<K,E,T,D>* nearest_help(size_t depth, KDTreeNode<K,E,T,D>* pos,
159  const T* pt, double& distsq) const;
160 
161  std::list<const KDTreeNode<K,E,T,D>*> withindist_help(size_t depth,
162  KDTreeNode<K,E,T,D>* pos, const T* pt, double distsq) const;
163 
164  KDTreeNode<K,E,T,D>* build_helper(
165  typename std::vector<KDTreeNode<K,E,T,D>*>::iterator begin,
166  typename std::vector<KDTreeNode<K,E,T,D>*>::iterator end,
167  size_t depth);
168 };
169 
170 }
171 
172 #include "kdtree.txx"
173 
174 #endif //KDTREE_H
const KDTreeNode< K, E, T, D > * nearest(const std::vector< T > &pt, double &dist) const
Definition: accessors.h:29
KDTreeNode(size_t plen, const T *pt, size_t dlen, const D *data)
Definition: kdtree.h:42
std::list< const KDTreeNode< K, E, T, D > * > withindist(const std::vector< T > &pt, double dist) const
D m_data[E]
Definition: kdtree.h:58
void clear()
Remove all elements.
void insert(const std::vector< T > &pt, const std::vector< D > &data)
Insert a node (note that this is not dynamic, new nodes won't be found by search until build() is cal...
KDTreeNode(const std::vector< T > &pt, const std::vector< D > &data)
Definition: kdtree.h:27
T m_point[K]
Definition: kdtree.h:55
void build()
Create Tree, until this is called search won't find anything.
KDTree(const KDTree &other)
Definition: kdtree.h:95
KDTree(KDTree &&other)
Definition: kdtree.h:88
bool built()
Definition: kdtree.h:131
KDTree()
Constructor.
Definition: kdtree.h:86