queso-0.53.0
kd_fix_rad_search.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------
2 // File: kd_fix_rad_search.cpp
3 // Programmer: Sunil Arya and David Mount
4 // Description: Standard kd-tree fixed-radius kNN search
5 // Last modified: 05/03/05 (Version 1.1)
6 //----------------------------------------------------------------------
7 // Copyright (c) 1997-2005 University of Maryland and Sunil Arya and
8 // David Mount. All Rights Reserved.
9 //
10 // This software and related documentation is part of the Approximate
11 // Nearest Neighbor Library (ANN). This software is provided under
12 // the provisions of the Lesser GNU Public License (LGPL). See the
13 // file ../ReadMe.txt for further information.
14 //
15 // The University of Maryland (U.M.) and the authors make no
16 // representations about the suitability or fitness of this software for
17 // any purpose. It is provided "as is" without express or implied
18 // warranty.
19 //----------------------------------------------------------------------
20 // History:
21 // Revision 1.1 05/03/05
22 // Initial release
23 //----------------------------------------------------------------------
24 
25 #include "kd_fix_rad_search.h" // kd fixed-radius search decls
26 
27 //----------------------------------------------------------------------
28 // Approximate fixed-radius k nearest neighbor search
29 // The squared radius is provided, and this procedure finds the
30 // k nearest neighbors within the radius, and returns the total
31 // number of points lying within the radius.
32 //
33 // The method used for searching the kd-tree is a variation of the
34 // nearest neighbor search used in kd_search.cpp, except that the
35 // radius of the search ball is known. We refer the reader to that
36 // file for the explanation of the recursive search procedure.
37 //----------------------------------------------------------------------
38 
39 //----------------------------------------------------------------------
40 // To keep argument lists short, a number of global variables
41 // are maintained which are common to all the recursive calls.
42 // These are given below.
43 //----------------------------------------------------------------------
44 
45 int ANNkdFRDim; // dimension of space
46 ANNpoint ANNkdFRQ; // query point
47 ANNdist ANNkdFRSqRad; // squared radius search bound
48 double ANNkdFRMaxErr; // max tolerable squared error
49 ANNpointArray ANNkdFRPts; // the points
50 ANNmin_k* ANNkdFRPointMK; // set of k closest points
51 int ANNkdFRPtsVisited; // total points visited
52 int ANNkdFRPtsInRange; // number of points in the range
53 
54 //----------------------------------------------------------------------
55 // annkFRSearch - fixed radius search for k nearest neighbors
56 //----------------------------------------------------------------------
57 
59  ANNpoint q, // the query point
60  ANNdist sqRad, // squared radius search bound
61  int k, // number of near neighbors to return
62  ANNidxArray nn_idx, // nearest neighbor indices (returned)
63  ANNdistArray dd, // the approximate nearest neighbor
64  double eps) // the error bound
65 {
66  ANNkdFRDim = dim; // copy arguments to static equivs
67  ANNkdFRQ = q;
68  ANNkdFRSqRad = sqRad;
69  ANNkdFRPts = pts;
70  ANNkdFRPtsVisited = 0; // initialize count of points visited
71  ANNkdFRPtsInRange = 0; // ...and points in the range
72 
73  ANNkdFRMaxErr = ANN_POW(1.0 + eps);
74  ANN_FLOP(2) // increment floating op count
75 
76  ANNkdFRPointMK = new ANNmin_k(k); // create set for closest k points
77  // search starting at the root
79 
80  for (int i = 0; i < k; i++) { // extract the k-th closest points
81  if (dd != NULL)
82  dd[i] = ANNkdFRPointMK->ith_smallest_key(i);
83  if (nn_idx != NULL)
84  nn_idx[i] = ANNkdFRPointMK->ith_smallest_info(i);
85  }
86 
87  delete ANNkdFRPointMK; // deallocate closest point set
88  return ANNkdFRPtsInRange; // return final point count
89 }
90 
91 //----------------------------------------------------------------------
92 // kd_split::ann_FR_search - search a splitting node
93 // Note: This routine is similar in structure to the standard kNN
94 // search. It visits the subtree that is closer to the query point
95 // first. For fixed-radius search, there is no benefit in visiting
96 // one subtree before the other, but we maintain the same basic
97 // code structure for the sake of uniformity.
98 //----------------------------------------------------------------------
99 
101 {
102  // check dist calc term condition
104 
105  // distance to cutting plane
106  ANNcoord cut_diff = ANNkdFRQ[cut_dim] - cut_val;
107 
108  if (cut_diff < 0) { // left of cutting plane
109  child[ANN_LO]->ann_FR_search(box_dist);// visit closer child first
110 
111  ANNcoord box_diff = cd_bnds[ANN_LO] - ANNkdFRQ[cut_dim];
112  if (box_diff < 0) // within bounds - ignore
113  box_diff = 0;
114  // distance to further box
115  box_dist = (ANNdist) ANN_SUM(box_dist,
116  ANN_DIFF(ANN_POW(box_diff), ANN_POW(cut_diff)));
117 
118  // visit further child if in range
119  if (box_dist * ANNkdFRMaxErr <= ANNkdFRSqRad)
120  child[ANN_HI]->ann_FR_search(box_dist);
121 
122  }
123  else { // right of cutting plane
124  child[ANN_HI]->ann_FR_search(box_dist);// visit closer child first
125 
126  ANNcoord box_diff = ANNkdFRQ[cut_dim] - cd_bnds[ANN_HI];
127  if (box_diff < 0) // within bounds - ignore
128  box_diff = 0;
129  // distance to further box
130  box_dist = (ANNdist) ANN_SUM(box_dist,
131  ANN_DIFF(ANN_POW(box_diff), ANN_POW(cut_diff)));
132 
133  // visit further child if close enough
134  if (box_dist * ANNkdFRMaxErr <= ANNkdFRSqRad)
135  child[ANN_LO]->ann_FR_search(box_dist);
136 
137  }
138  ANN_FLOP(13) // increment floating ops
139  ANN_SPL(1) // one more splitting node visited
140 }
141 
142 //----------------------------------------------------------------------
143 // kd_leaf::ann_FR_search - search points in a leaf node
144 // Note: The unreadability of this code is the result of
145 // some fine tuning to replace indexing by pointer operations.
146 //----------------------------------------------------------------------
147 
149 {
150  register ANNdist dist; // distance to data point
151  register ANNcoord* pp; // data coordinate pointer
152  register ANNcoord* qq; // query coordinate pointer
153  register ANNcoord t;
154  register int d;
155 
156  for (int i = 0; i < n_pts; i++) { // check points in bucket
157 
158  pp = ANNkdFRPts[bkt[i]]; // first coord of next data point
159  qq = ANNkdFRQ; // first coord of query point
160  dist = 0;
161 
162  for(d = 0; d < ANNkdFRDim; d++) {
163  ANN_COORD(1) // one more coordinate hit
164  ANN_FLOP(5) // increment floating ops
165 
166  t = *(qq++) - *(pp++); // compute length and adv coordinate
167  // exceeds dist to k-th smallest?
168  if( (dist = ANN_SUM(dist, ANN_POW(t))) > ANNkdFRSqRad) {
169  break;
170  }
171  }
172 
173  if (d >= ANNkdFRDim && // among the k best?
174  (ANN_ALLOW_SELF_MATCH || dist!=0)) { // and no self-match problem
175  // add it to the list
176  ANNkdFRPointMK->insert(dist, bkt[i]);
177  ANNkdFRPtsInRange++; // increment point count
178  }
179  }
180  ANN_LEAF(1) // one more leaf node visited
181  ANN_PTS(n_pts) // increment points visited
182  ANNkdFRPtsVisited += n_pts; // increment number of points visited
183 }
ANNdist ANNkdFRSqRad
virtual void ann_FR_search(ANNdist)=0
#define ANN_LEAF(n)
Definition: ANNperf.h:132
#define ANN_PTS(n)
Definition: ANNperf.h:135
ANNcoord cd_bnds[2]
Definition: kd_tree.h:146
#define ANN_SPL(n)
Definition: ANNperf.h:133
int annkFRSearch(ANNpoint q, ANNdist sqRad, int k, ANNidxArray nn_idx=NULL, ANNdistArray dd=NULL, double eps=0.0)
double ANNcoord
Definition: ANN.h:158
double eps
Definition: ann_sample.cpp:55
const ANNbool ANN_ALLOW_SELF_MATCH
Definition: ANN.h:235
#define ANN_SUM(x, y)
Definition: ANN.h:362
int ANNkdFRPtsVisited
#define ANN_DIFF(x, y)
Definition: ANN.h:363
int n_pts
Definition: kd_tree.h:93
ANNpoint bnd_box_lo
Definition: ANN.h:713
Definition: ANNx.h:45
ANNdist * ANNdistArray
Definition: ANN.h:377
ANNkd_ptr root
Definition: ANN.h:712
int dim
Definition: ANN.h:707
ANNidxArray bkt
Definition: kd_tree.h:94
ANNkd_ptr child[2]
Definition: kd_tree.h:148
int ANNkdFRPtsInRange
ANNpoint bnd_box_hi
Definition: ANN.h:714
ANNpoint * ANNpointArray
Definition: ANN.h:376
#define ANN_FLOP(n)
Definition: ANNperf.h:131
int cut_dim
Definition: kd_tree.h:144
Definition: ANNx.h:45
ANNpointArray ANNkdFRPts
double ANNkdFRMaxErr
void insert(PQKkey kv, PQKinfo inf)
Definition: pr_queue_k.h:99
ANNmin_k * ANNkdFRPointMK
#define ANN_COORD(n)
Definition: ANNperf.h:136
ANNcoord * ANNpoint
Definition: ANN.h:375
double ANNdist
Definition: ANN.h:159
ANNpoint ANNkdFRQ
int ANNkdFRDim
PQKinfo ith_smallest_info(int i)
Definition: pr_queue_k.h:96
ANNdist annBoxDistance(const ANNpoint q, const ANNpoint lo, const ANNpoint hi, int dim)
Definition: kd_util.cpp:124
virtual void ann_FR_search(ANNdist)
virtual void ann_FR_search(ANNdist)
ANNpointArray pts
Definition: ANN.h:710
PQKkey ith_smallest_key(int i)
Definition: pr_queue_k.h:93
#define ANN_POW(v)
Definition: ANN.h:360
int k
Definition: ann_sample.cpp:53
ANNidx * ANNidxArray
Definition: ANN.h:378
int ANNmaxPtsVisited
Definition: ANN.cpp:190
ANNcoord cut_val
Definition: kd_tree.h:145

Generated on Thu Jun 11 2015 13:52:31 for queso-0.53.0 by  doxygen 1.8.5