cheshirekow  v0.1.0
KNearest.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  */
27 #ifndef MPBLOCKS_KD_TREE_R2_S1_BLOCKS_KNEAREST_H_
28 #define MPBLOCKS_KD_TREE_R2_S1_BLOCKS_KNEAREST_H_
29 
30 #include <set>
31 
32 namespace mpblocks {
33 namespace kd_tree {
34 namespace r2_s1 {
35 namespace blocks {
36 
37 
38 template <class Traits,
39  template<class> class Allocator = std::allocator >
40 class KNearest
41 {
42  public:
43  typedef typename Traits::Format_t Format_t;
44  typedef typename Traits::Node Node_t;
45 
46  typedef Eigen::Matrix<Format_t,Traits::NDim,1> Point_t;
47 
51  typedef typename Search_t::PQueue_t PQueue_t;
52 
53  protected:
56 
57  public:
58  sigc::signal<void,Node_t*> sig_result;
59 
60  KNearest(Tree_t* tree=0):
61  m_tree(tree)
62  {}
63 
64  void setTree( Tree_t* tree )
65  { m_tree = tree; }
66 
67  void search( const Point_t& q, unsigned int k )
68  {
69  m_search.reset(k);
70  if(m_tree)
71  {
73  const PQueue_t& list = m_search.result();
74  typename PQueue_t::const_iterator iKey;
75  for( iKey = list.begin(); iKey != list.end(); ++iKey )
76  {
77  sig_result.emit(iKey->n);
78  }
79  }
80  }
81 
82  sigc::slot<void,const Point_t&,unsigned int> slot_search()
83  { return sigc::mem_fun(this,&This_t::search); }
84 };
85 
86 
87 
88 
89 
90 } // namespace blocks
91 } // namespace r2_s1
92 } // namespace kd_tree
93 } // namespace mpblocks
94 
95 
96 
97 
98 
99 
100 
101 
102 #endif // NEARESTNEIGHBOR_H_
a simple KDtree class
Definition: Tree.h:43
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
void search(const Point_t &q, unsigned int k)
Definition: KNearest.h:67
KNearest< Traits, Allocator > This_t
Definition: KNearest.h:48
r2_s1::KNearest< Traits, Allocator > Search_t
Definition: KNearest.h:49
sigc::slot< void, const Point_t &, unsigned int > slot_search()
Definition: KNearest.h:82
std::set< Key_t, KeyCompare_t, Allocator_t > PQueue_t
Definition: KNearest.h:53
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
example traits class, can also be used as a default if you're lazy and your problem happens to be 2d ...
Definition: Traits.h:36
sigc::signal< void, Node_t * > sig_result
Definition: KNearest.h:58
Eigen::Matrix< Format_t, Traits::NDim, 1 > Point_t
Definition: KNearest.h:46
double Format_t
number format (i.e. double, float)
Definition: Traits.h:38