cheshirekow  v0.1.0
KNearest.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_DUBINS_KD_TREE_KNEAREST_HPP_
28 #define MPBLOCKS_DUBINS_KD_TREE_KNEAREST_HPP_
29 
30 
31 namespace mpblocks {
32 namespace dubins {
33 namespace kd_tree {
34 
35 
36 template <typename Scalar>
37 KNearest<Scalar>::KNearest(unsigned int k, Scalar radius)
38 {
39  m_k = k;
40  m_radius = radius;
41 
42  reset();
43 }
44 
45 
46 template <typename Scalar>
48 {
49  while( m_queue.size() > 0 )
50  m_queue.pop();
51  m_nodesEvaluated = 0;
52  m_boxesEvaluated = 0;
53 }
54 
55 
56 template <typename Scalar>
57 void KNearest<Scalar>::reset( int k, Scalar radius )
58 {
59  reset();
60  m_k = k;
61  m_radius = radius;
62 }
63 
64 
65 
66 template <typename Scalar>
68 {
69  return m_queue;
70 }
71 
72 
73 
74 
75 template <typename Scalar>
77  const Point& q0, const Point& q1, Node_t* n)
78 {
79  m_nodesEvaluated++;
80 
81  using namespace curves_eigen;
82  Path<Scalar> bestSoln = solve(q0,q1,m_radius);
83 
84  Key_t key;
85  key.d2 = bestSoln.dist(m_radius);
86  key.n = n;
87  key.id = bestSoln.id;
88 
89  m_queue.push(key);
90 
91  if( m_queue.size() > m_k )
92  m_queue.pop();
93 }
94 
95 
96 
97 
98 template <typename Scalar>
100 {
101  m_boxesEvaluated++;
102 // return true;
103  using namespace curves_eigen;
104  Path<Scalar> bestSoln = hyper::solve(q,h,m_radius);
105  Scalar d2 = bestSoln.dist(m_radius);
106 
107  return ( m_queue.size() < m_k || d2 < m_queue.top().d2 );
108 }
109 
110 
111 
112 
113 
114 
115 
116 } // namespace kd_tree
117 } // namespace dubins
118 } // namespace mpblocks
119 
120 
121 
122 #endif // NEARESTNEIGHBOR_H_
virtual bool shouldRecurse(const Point &q, const HyperRect_t &r)
evaluate the Euclidean distance from q to it's closest point in r and if that distance is less than t...
Definition: KNearest.hpp:99
key for priority queue
Definition: Key.h:39
KNearest(unsigned int k=1, Scalar radius=1)
Definition: KNearest.hpp:37
std::priority_queue< Key_t, KeyVec, KeyCompare > PQueue_t
Definition: KNearest.h:52
Encodes a dubins path primitive, which is three connected arc segments.
Definition: path.h:42
virtual void evaluate(const Point &q, const Point &p, Node_t *n)
calculates Euclidean distance from q to p, and if its less than the current best replaces the current...
Definition: KNearest.hpp:76
Eigen::Matrix< Scalar, 3, 1 > Point
Definition: KNearest.h:51
Path< Format_t > solve(const Eigen::Matrix< Format_t, 3, 1 > &q0, const HyperRect< Format_t > &h, const Format_t r)