cheshirekow  v0.1.0
Tree.h
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  */
19 /*
20  * @file Tree.h
21  *
22  * @date Mar 26, 2011
23  * @author Josh Bialkowski (jbialk@mit.edu)
24  * @brief basically a c++ rewrite of http://code.google.com/p/kdtree/
25  */
26 
27 #ifndef MPBLOCKS_KD_TREE_TREE_H_
28 #define MPBLOCKS_KD_TREE_TREE_H_
29 
30 #include <vector>
31 #include <memory>
32 
33 namespace mpblocks {
34 namespace kd_tree {
35 
42 template <class Traits>
43 class Tree
44 {
45  public:
47  typedef typename Traits::Format_t Format_t;
48 
50  typedef typename Traits::Node Node_t;
51 
54  typedef typename Traits::HyperRect HyperRect_t;
55 
57  typedef Eigen::Matrix<Format_t,Traits::NDim,1> Vector_t;
58 
60  typedef Vector_t Point_t;
61 
62  // these just shorten up some of the templated classes into smaller
63  // names
69 
70  protected:
74  int m_size;
75 
78 
79  public:
80 
82  Tree( );
83 
86  ~Tree();
87 
88  void set_initRect( const HyperRect_t& h );
89 
92 
100  void insert(Node_t*);
101 
104  void findNearest( const Point_t& q, NNIface_t& search );
105 
108  void findRange( RangeIface_t& search );
109 
112  typename ListBuilder_t::List_t& buildList( bool bfs=true );
113 
118 
120  Node_t* getRoot(){ return m_root; }
121 
124  void clear();
125 
127  int size();
128 };
129 
130 } // namespace kd_tree
131 } // namespace mpblocks
132 
133 
134 #endif /* CKDTREE_H_ */
HyperRect_t m_rect
hyper rectangle for searches
Definition: Tree.h:73
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
int m_size
number of points
Definition: Tree.h:74
Enumerates an entire subtree, building a list of nodes along with the hyperectangle bounding the subt...
Definition: ListBuilder.h:42
~Tree()
destructs the tree and recursively frees all node data. note that nodes do not own their data if thei...
Definition: Tree.hpp:54
Node_t * m_root
root node of the tree (0 if empty)
Definition: Tree.h:71
a simple KDtree class
Definition: Tree.h:43
Traits::Format_t Format_t
number format, i.e. double, float
Definition: Tree.h:47
Eigen::Matrix< Format_t, Traits::NDim, 1 > Vector_t
a vector is the difference of two points
Definition: Tree.h:57
List_t & getList()
return the list
Definition: ListBuilder.hpp:97
NearestSearchIface< Traits > NNIface_t
Definition: Tree.h:67
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
Node< Traits > NodeBase_t
Definition: Tree.h:65
Traits::Node Node_t
the node class, should be defined as an inner class of Traits
Definition: Tree.h:50
ListBuilder_t::List_t & getList()
return the list after buildList has been called, the reference is the same one returned by buildList ...
Definition: Tree.h:117
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
Interface for nearest node type searches.
Definition: NearestSearch.h:38
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
Traits::HyperRect HyperRect_t
the hyper rectangle class shoudl be defined as an inner class of Traits, or a typedef in Traits ...
Definition: Tree.h:54
RangeSearchIface< Traits > RangeIface_t
Definition: Tree.h:68
HyperRect_t m_bounds
bounding rectangle
Definition: Tree.h:77
ListBuilder_t m_lister
helper for building a list of all nodes
Definition: Tree.h:72
void set_initRect(const HyperRect_t &h)
Definition: Tree.hpp:59
ListBuilder< Traits > ListBuilder_t
Definition: Tree.h:66
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
Node_t * getRoot()
return the root node
Definition: Tree.h:120
std::list< Pair_t * > List_t
Definition: ListBuilder.h:56
Base class for nodes in the kd tree.
Definition: Node.h:58
HyperRect_t m_initRect
initial rectangle, probably 0,0,0...
Definition: Tree.h:76
Tree< Traits > This_t
Definition: Tree.h:64
double Format_t
number format (i.e. double, float)
Definition: Traits.h:38
void clear()
clearout the data structure, note: does not destroy any object references
Definition: Tree.hpp:129
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