-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkdtree.cpp
More file actions
361 lines (319 loc) · 8.85 KB
/
Copy pathkdtree.cpp
File metadata and controls
361 lines (319 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* @file kdtree.cpp
* Implementation of KDTree class.
*/
#define _square(x) ((x)*(x))
#include <iostream>
using namespace std;
template<int Dim>
bool KDTree<Dim>::smallerDimVal(const Point<Dim> & first, const Point<Dim> & second, int curDim) const
{
/**
* @todo Implement this function!
*/
if (second[curDim] < first[curDim])
{
return false;
}
else
{
return true;
}
}
template<int Dim>
bool KDTree<Dim>::shouldReplace(const Point<Dim> & target, const Point<Dim> & currentBest, const Point<Dim> & potential) const
{
/**
* @todo Implement this function!
*/
/* Return true if the Euclidean Distance of target and potential points is
* smaller than the Euclidean Distance of currentBest and potential points.
*/
if (_euclideanDistance(target, potential) < _euclideanDistance(target, currentBest))
{
return true;
}
else if (_euclideanDistance(target, potential) == _euclideanDistance(target, currentBest))
{
if (currentBest < potential)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
template<int Dim>
int KDTree<Dim>::_euclideanDistance(const Point<Dim> & first, const Point<Dim> & second) const
{
int distance = 0;
/* Calculates the Euclidean Distance between two points by taking the
* square of the difference between the values of the points for each
* dimension.
*/
for (int i = 0; i < Dim; i++)
{
distance += _square(first[i] - second[i]);
}
return distance;
}
template<int Dim>
KDTree<Dim>::KDTree(const vector< Point<Dim> > & newPoints)
{
/**
* @todo Implement this function!
*/
/* Copies all the values from newPoints to the points vector of the
* KD Tree.
*/
points.resize(newPoints.size());
for (int i = 0; i < newPoints.size(); i++)
{
points[i] = newPoints[i];
}
/* Returns nothing if there are no points in the KD Tree */
if (points.size() == 0)
{
return;
}
/* Calls the _KDTree Helper function to construct the KD Tree */
else
{
_KDTree(0, points.size() - 1, 0);
}
}
template<int Dim>
void KDTree<Dim>::_KDTree(int left, int right, int curDim)
{
/* Base Case
* Return if the left and right markers are equal and there
* is no more work to be done.
*/
if (left == right)
{
return;
}
/* Recursive Definition */
else
{
/* Ensures that the current Dimension always loops around the
* total number of Dimensions.
*/
while (curDim >= Dim)
{
curDim -= Dim;
}
/* Calculate the current median */
int median = _median(left, right);
/* Select the point that is the current median by using the
* _select helper function.
*/
points[median + left] = _select(left, right, median, curDim);
/* For valid left and right markers, recursively call the helper
* function to construct the left and right subtrees.
*/
if (median + left - 1 >= left)
_KDTree(left, median + left - 1, curDim + 1);
if (median + left + 1 <= right)
_KDTree(median + left + 1, right, curDim + 1);
}
}
template<int Dim>
int KDTree<Dim>::_median(int left, int right)
{
/* Calculates the median given left and right markers. This median
* is the position within the number of elements given by the left and
* right markers only, rather than the absolute position of the element
* in the entire array.
*/
int total = right - left;
return total/2;
}
template<int Dim>
int KDTree<Dim>::_partition(int left, int right, int pivotIndex, int curDim)
{
Point<Dim> pivotValue = points[pivotIndex + left];
/* Swap function that moves pivot to end. */
Point<Dim> temp = points[pivotIndex + left];
points[pivotIndex + left] = points[right];
points[right] = temp;
/* Arranges the elements within the left and right boundaries
* such that everything to the left of the pivotIndex is smaller
* than the pivotIndex and everything to the right is larger..
*/
int storeIndex = left;
for (int i = left; i < right; i++)
{
if (smallerDimVal(points[i], pivotValue, curDim))
{
if ((points[i])[curDim] == pivotValue[curDim])
{
if (points[i] < pivotValue)
{
temp = points[i];
points[i] = points[storeIndex];
points[storeIndex] = temp;
storeIndex++;
}
}
else
{
temp = points[i];
points[i] = points[storeIndex];
points[storeIndex] = temp;
storeIndex++;
}
}
}
/* Swap function that moves pivot to its final place */
temp = points[right];
points[right] = points[storeIndex];
points[storeIndex] = temp;
return storeIndex;
}
template<int Dim>
Point<Dim> KDTree<Dim>::_select(int left, int right, int k, int curDim)
{
/* If list contains only one element, return that element. */
if (left == right)
{
return points[left];
}
/* Select pivotIndex between left and right */
int pivotNewIndex = _partition(left, right, k, curDim);
int pivotDist = pivotNewIndex - left;
/* The pivot is in its final sorted position, so pivotDist reflects
* its 0-based position if list were sorted.
*/
if (pivotDist == k)
{
return points[pivotNewIndex];
}
else if (k < pivotDist)
{
return _select(left, pivotNewIndex - 1, k, curDim);
}
else
{
return _select(pivotNewIndex + 1, right, k - pivotDist - 1, curDim);
}
}
template<int Dim>
Point<Dim> KDTree<Dim>::findNearestNeighbor(const Point<Dim> & query) const
{
/**
* @todo Implement this function!
*/
/* Calls the helper function _findNearestNeighbor, which returns the
* position of the closest neighbor.
* Returns the Point that is in that position.
*/
return points[_findNearestNeighbor(query, _median(0, points.size()-1), 0, points.size()-1, 0)];
}
template<int Dim>
int KDTree<Dim>::_findNearestNeighbor(const Point<Dim> & query, int curr, int left, int right, int curDim) const
{
/* Base Case
* Returns the current position if the left and right boundaries are the
* same.
*/
if (left >= right)
{
return curr;
}
/* Ensures that the current Dimension is always smaller than the total
* number of Dimensions.
*/
while (curDim >= Dim)
{
curDim -= Dim;
}
/* Calculates the distance between the current point and the query point
* in the current Dimension.
*/
int distance = _square(points[curr][curDim]-query[curDim]);
int curBest = curr;
int otherBest = curr;
/* Search the left subtree if the value of the current point in the
* current dimension is smaller than the value of the query point in the
* current dimension.
*/
if (smallerDimVal(query, points[curr], curDim))
{
/* Recursively call the _findNearestNeighbor helper function till the
* function reaches the leaves of the tree.
*/
curBest = _findNearestNeighbor(query, _median(left, curr-1)+left, left, curr-1, curDim+1);
/* if the current Point is closer to the query point that the
* current nearest Point, replace current best with current Point.
*/
if (shouldReplace(query, points[curBest], points[curr]))
{
curBest = curr;
}
/* Caculates the "radius" of the current nearest Point and query Point.
*/
int radius = _euclideanDistance(points[curBest], query);
/* If the distance between the current point and the query point in
* the current dimension is within the radius, check the other half
* of the subtree.
*/
if (radius >= distance)
{
otherBest = _findNearestNeighbor(query, _median(curr+1, right)+curr+1, curr+1, right, curDim+1);
/* If the other half of the subtree has a Point that is closer
* tot he query Point thatn the current nearest Point, replace
* current best with the Point in the other half.
*/
if (shouldReplace(query, points[curBest], points[otherBest]))
{
curBest = otherBest;
}
}
}
/* Search the right subtree if the value of the current point in the
* current dimension is larger than the value of the query point in the
* current dimension...
*/
else
{
/* Recursively call the _findNearestNeighbor helper function till the
* function reaches the leaves of the tree.
*/
curBest = _findNearestNeighbor(query, _median(curr+1, right)+curr+1, curr+1, right, curDim+1);
/* if the current Point is closer to the query point that the
* current nearest Point, replace current best with current Point.
*/
if (shouldReplace(query, points[curBest], points[curr]))
{
curBest = curr;
}
/* Caculates the "radius" of the current nearest Point and query Point.
*/
int radius = _euclideanDistance(points[curBest], query);
/* If the distance between the current point and the query point in
* the current dimension is within the radius, check the other half
* of the subtree.
*/
if (radius >= distance)
{
otherBest = _findNearestNeighbor(query, _median(left, curr-1)+left, left, curr-1, curDim+1);
/* If the other half of the subtree has a Point that is closer
* tot he query Point thatn the current nearest Point, replace
* current best with the Point in the other half.
*/
if (shouldReplace(query, points[curBest], points[otherBest]))
{
curBest = otherBest;
}
}
}
/* Return the position of the nearest point. */
return curBest;
}