cheshirekow  v0.1.0
findNearest.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_KD2_FINDNEAREST_H_
28 #define MPBLOCKS_KD2_FINDNEAREST_H_
29 
30 #include <mpblocks/kd2/BinaryKey.h>
31 
32 namespace mpblocks {
33 namespace kd2 {
34 
35 template <
36  // implicit types
37  class Point,
38  // input types
39  class SearchQueue,
40  class ResultSet,
41  // queue operations
42  class QueueMinKeyFn,
43  class QueuePopMinFn,
44  class QueueInsertFn,
45  class QueueSizeFn,
46  // set operations
47  class SetMaxKeyFn,
48  class SetPopMaxFn,
49  class SetInsertFn,
50  class SetSizeFn,
51  // distance functions
52  class PointDistanceFn,
53  class CellDistanceFn,
54  // node functions
55  class IsLeafFn,
56  class ChildrenFn,
57  class SitesFn,
58  // pair functions
59  class CellFn,
60  class NodeFn
61 >
62 void findNearest( const Point& query,
63  int k,
64  SearchQueue& q,
65  ResultSet& r,
66  const QueueMinKeyFn& minKey,
67  const QueuePopMinFn& popMin,
68  const QueueInsertFn& enqueue,
69  const QueueSizeFn& queueSize,
70  const SetMaxKeyFn& maxKey,
71  const SetPopMaxFn& popMax,
72  const SetInsertFn& insert,
73  const SetSizeFn& size,
74  const PointDistanceFn& pointDist,
75  const CellDistanceFn& cellDist,
76  const IsLeafFn& isLeaf,
77  const ChildrenFn& children,
78  const SitesFn& sites,
79  const CellFn& getCell,
80  const NodeFn& getNode)
81 {
82  // if the nearest point in the unsearched frontier is further away than
83  // the k-th nearest point found then we have found the k-NN
84  while( queueSize(q) > 0 && (size(r) < k || minKey(q) < maxKey(r)) )
85  {
86  // at each iteration expand the unsearched cell which contains the
87  // nearest point to the query
88  auto pair = popMin(q);
89  auto node = getNode(pair);
90 
91  // if it's a leaf node then evaluate the distance to each site
92  // contained in that nodes cell
93  if( isLeaf( node ) )
94  {
95  for( auto site : sites(node) )
96  {
97  insert( r, pointDist(query,site), site );
98  if( size(r) > k )
99  popMax(r);
100  }
101  }
102  else
103  {
104  for( auto childPair : children(pair) )
105  enqueue(q,cellDist(query,getCell(childPair)),childPair);
106  }
107  }
108 }
109 
110 
111 
112 } //< namespace kd2
113 } //< namespace mpblocks
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 #endif // FINDNEAREST_H_
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(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