cheshirekow  v0.1.0
Tree.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_KD_TREE_TREE_HPP_
28 #define MPBLOCKS_KD_TREE_TREE_HPP_
29 
30 
31 #include <vector>
32 #include <cassert>
33 #include <iostream>
34 #include <limits>
35 
36 
37 namespace mpblocks {
38 namespace kd_tree {
39 
40 
41 
42 
43 
44 
45 template <class Traits>
47  m_root(0),
48  m_size(0)
49 {
50  clear();
51 }
52 
53 template <class Traits>
55 {
56 }
57 
58 template <class Traits>
60 {
61  m_initRect = h;
62 }
63 
64 template <class Traits>
66 {
67  m_size++;
68 
69  const Point_t& pt = n->getPoint();
70  for(int i=0; i < pt.size(); i++)
71  {
72  if( pt[i] < m_bounds.minExt[i] )
73  m_bounds.minExt[i] = pt[i];
74  if( pt[i] > m_bounds.maxExt[i] )
75  m_bounds.maxExt[i] = pt[i];
76  }
77 
78 
79  if(m_root)
80  return static_cast<NodeBase_t*>(m_root)->insert(n);
81  else
82  {
83  m_root = n;
84  return static_cast<NodeBase_t*>(m_root)->construct(0,0);
85  }
86 }
87 
88 
89 
90 template <class Traits>
91 void Tree<Traits>::findNearest( const Point_t& q, NNIface_t& search )
92 {
93  if(m_root)
94  {
95  m_rect = m_bounds;
96  static_cast<NodeBase_t*>(m_root)->findNearest(q,m_rect,search);
97  }
98 }
99 
100 
101 template <class Traits>
103 {
104  if(m_root)
105  {
106  m_rect = m_bounds;
107  static_cast<NodeBase_t*>(m_root)->findRange(search,m_rect);
108  }
109 }
110 
111 
112 
113 template <class Traits>
116 {
117  m_lister.reset();
118  if( bfs )
119  m_lister.buildBFS(m_root);
120  else
121  m_lister.buildDFS(m_root);
122 
123  return m_lister.getList();
124 }
125 
126 
127 
128 template <class Traits>
130 {
131  m_root = 0;
132  m_size = 0;
133  m_bounds = m_initRect;
134 }
135 
136 
137 template <class Traits>
139 {
140  return m_size;
141 }
142 
143 
144 
145 } // namespace kd_tree
146 } // namespace mpblocks
147 
148 
149 #endif /* CKDTREE_H_ */
__host__ __device__ __forceinline__ void construct(SturmSequence2< Scalar, Spec > &sturm, const Exp &exp)
construct a sturm sequence
void findRange(RangeIface_t &search)
generic range search, specific search depends on the implementing class of the RangeIface ...
Definition: Tree.hpp:102
Tree()
constructs a new kd-tree
Definition: Tree.hpp:46
~Tree()
destructs the tree and recursively frees all node data. note that nodes do not own their data if thei...
Definition: Tree.hpp:54
ListBuilder_t::List_t & buildList(bool bfs=true)
create a list of all the nodes in the tree, mostly only used for debug drawing
Definition: Tree.hpp:115
void findNearest(const Point_t &q, NNIface_t &search)
generic NN search, specific search depends on the implementing class of the NNIface ...
Definition: Tree.hpp:91
const Point_t & getPoint()
returns a Point_t of the point stored at this node
Definition: Node.hpp:65
Interface for nearest node type searches.
Definition: NearestSearch.h:38
void findNearest(const Point &query, int k, SearchQueue &q, ResultSet &r, const QueueMinKeyFn &minKey, const QueuePopMinFn &popMin, const QueueInsertFn &enqueue, const QueueSizeFn &queueSize, const SetMaxKeyFn &maxKey, const SetPopMaxFn &popMax, const SetInsertFn &insert, const SetSizeFn &size, const PointDistanceFn &pointDist, const CellDistanceFn &cellDist, const IsLeafFn &isLeaf, const ChildrenFn &children, const SitesFn &sites, const CellFn &getCell, const NodeFn &getNode)
Definition: findNearest.h:62
void insert(Node_t *)
insert a node into the kd tree. The node should be newly created and contain no children ...
Definition: Tree.hpp:65
void set_initRect(const HyperRect_t &h)
Definition: Tree.hpp:59
Vector_t Point_t
the storage type for points
Definition: Tree.h:60
the node class must be defined in traits since it uses the CTRP, it must derive from kd_tree::Node<Tr...
Definition: Traits.h:49
std::list< Pair_t * > List_t
Definition: ListBuilder.h:56
Base class for nodes in the kd tree.
Definition: Node.h:58
void clear()
clearout the data structure, note: does not destroy any object references
Definition: Tree.hpp:129
void insert(NodeRef node, PointRef point, const IsLeafFn &isLeaf, const LeafInsertFn &leafInsert, const IdxFn &idx, const ValueFn &value, const ChildFn &child, const PointGetFn &pointGet)
Definition: insert.h:46
int size()
return the number of points in the tree
Definition: Tree.hpp:138
an NDim dimensional hyperrectangle, represented as a min and max extent
Definition: HyperRect.h:23