Normand Briere
2019-11-21 ddb10cb84dddfeef1ef9946f2e13cef3c93e6cc4
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//package edu.wlu.cs.levy.CG;
 
import java.util.List;
import java.util.LinkedList;
import java.util.Stack;
 
/**
 * KDTree is a class supporting KD-tree insertion, deletion, equality
 * search, range search, and nearest neighbor(s) using double-precision
 * floating-point keys.  Splitting dimension is chosen naively, by
 * depth modulo K.  Semantics are as follows:
 *
 * <UL>
 * <LI> Two different keys containing identical numbers should retrieve the 
 *      same value from a given KD-tree.  Therefore keys are cloned when a 
 *      node is inserted.
 * <BR><BR>
 * <LI> As with Hashtables, values inserted into a KD-tree are <I>not</I>
 *      cloned.  Modifying a value between insertion and retrieval will
 *      therefore modify the value stored in the tree.
 *</UL>
 *
 * Implements the Nearest Neighbor algorithm (Table 6.4) of
 *
 * <PRE>
 * &#064;techreport{AndrewMooreNearestNeighbor,
 *   author  = {Andrew Moore},
 *   title   = {An introductory tutorial on kd-trees},
 *   institution = {Robotics Institute, Carnegie Mellon University},
 *   year    = {1991},
 *   number  = {Technical Report No. 209, Computer Laboratory, 
 *              University of Cambridge},
 *   address = {Pittsburgh, PA}
 * }
 * </PRE>
 *  
 *
 * @author      Simon Levy, Bjoern Heckel
 * @version     %I%, %G%
 * @since JDK1.2 
 */
public class KDTree<T>
{
    // number of milliseconds
 
    final long m_timeout;
    // K = number of dimensions
    final private int m_K;
    // root of KD-tree
    private KDNode<T> m_root;
    // count of nodes
    private int m_count;
 
    /**
     * Creates a KD-tree with specified number of dimensions.
     *
     * @param k number of dimensions
     */
    public KDTree(int k)
    {
        this(k, 0);
    }
 
    public KDTree(int k, long timeout)
    {
        this.m_timeout = timeout;
        m_K = k;
        m_root = null;
    }
 
    /** 
     * Insert a node in a KD-tree.  Uses algorithm translated from 352.ins.c of
     *
     *   <PRE>
     *   &#064;Book{GonnetBaezaYates1991,                                   
     *     author =    {G.H. Gonnet and R. Baeza-Yates},
     *     title =     {Handbook of Algorithms and Data Structures},
     *     publisher = {Addison-Wesley},
     *     year =      {1991}
     *   }
     *   </PRE>
     *
     * @param key key for KD-tree node
     * @param value value at that key
     *
     * @throws KeySizeException if key.length mismatches K
     * @throws KeyDuplicateException if key already in tree
     */
    public void insert(double[] key, T value)
            throws KeySizeException, KeyDuplicateException
    {
        this.edit(key, new Editor.Inserter<T>(value));
    }
 
    /** 
     * Edit a node in a KD-tree
     *
     * @param key key for KD-tree node
     * @param editor object to edit the value at that key
     *
     * @throws KeySizeException if key.length mismatches K
     * @throws KeyDuplicateException if key already in tree
     */
    public void edit(double[] key, Editor<T> editor)
            throws KeySizeException, KeyDuplicateException
    {
 
        if (key.length != m_K)
        {
            throw new KeySizeException();
        }
 
        synchronized (this)
        {
            // the first insert has to be synchronized
            if (null == m_root)
            {
                m_root = KDNode.create(new HPoint(key), editor);
                m_count = m_root.deleted ? 0 : 1;
                return;
            }
        }
 
        m_count += KDNode.edit(new HPoint(key), editor, m_root, 0, m_K);
    }
 
    /** 
     * Find  KD-tree node whose key is identical to key.  Uses algorithm 
     * translated from 352.srch.c of Gonnet & Baeza-Yates.
     *
     * @param key key for KD-tree node
     *
     * @return object at key, or null if not found
     *
     * @throws KeySizeException if key.length mismatches K
     */
    public T search(double[] key) throws KeySizeException
    {
 
        if (key.length != m_K)
        {
            throw new KeySizeException();
        }
 
        KDNode<T> kd = KDNode.srch(new HPoint(key), m_root, m_K);
 
        return (kd == null ? null : kd.v);
    }
 
    public void delete(double[] key)
            throws KeySizeException, KeyMissingException
    {
        delete(key, false);
    }
 
    /** 
     * Delete a node from a KD-tree.  Instead of actually deleting node and
     * rebuilding tree, marks node as deleted.  Hence, it is up to the caller
     * to rebuild the tree as needed for efficiency.
     *
     * @param key key for KD-tree node
     * @param optional  if false and node not found, throw an exception
     *
     * @throws KeySizeException if key.length mismatches K
     * @throws KeyMissingException if no node in tree has key
     */
    public void delete(double[] key, boolean optional)
            throws KeySizeException, KeyMissingException
    {
 
        if (key.length != m_K)
        {
            throw new KeySizeException();
        }
        KDNode<T> t = KDNode.srch(new HPoint(key), m_root, m_K);
        if (t == null)
        {
            if (optional == false)
            {
                throw new KeyMissingException();
            }
        } else
        {
            if (KDNode.del(t))
            {
                m_count--;
            }
        }
    }
 
    /**
     * Find KD-tree node whose key is nearest neighbor to
     * key. 
     *
     * @param key key for KD-tree node
     *
     * @return object at node nearest to key, or null on failure
     *
     * @throws KeySizeException if key.length mismatches K
    
     */
    public T nearest(double[] key, int n) throws KeySizeException
    {
 
        List<T> nbrs = nearest(key, n, null);
        
        int nearpoint = 0; // n-1;
        
//        if (nbrs.size() == 0)
//            nearpoint = 0;
//        
//        if (nbrs.get(nearpoint).equals(key))
//            nearpoint = 1;
        
        return nbrs.get(nearpoint);
    }
 
    /**
     * Find KD-tree nodes whose keys are <i>n</i> nearest neighbors to
     * key. 
     *
     * @param key key for KD-tree node
     * @param n number of nodes to return
     *
     * @return objects at nodes nearest to key, or null on failure
     *
     * @throws KeySizeException if key.length mismatches K
    
     */
//    public List<T> nearest(double[] key, int n)
//            throws KeySizeException, IllegalArgumentException
//    {
//        return nearest(key, n, null);
//    }
 
    /**
     * Find KD-tree nodes whose keys are within a given Euclidean distance of
     * a given key.
     *
     * @param key key for KD-tree node
     * @param d Euclidean distance
     *
     * @return objects at nodes with distance of key, or null on failure
     *
     * @throws KeySizeException if key.length mismatches K
    
     */
    public List<T> nearestEuclidean(double[] key, double dist)
            throws KeySizeException
    {
        return nearestDistance(key, dist, new EuclideanDistance());
    }
 
    /**
     * Find KD-tree nodes whose keys are within a given Hamming distance of
     * a given key.
     *
     * @param key key for KD-tree node
     * @param d Hamming distance
     *
     * @return objects at nodes with distance of key, or null on failure
     *
     * @throws KeySizeException if key.length mismatches K
    
     */
    public List<T> nearestHamming(double[] key, double dist)
            throws KeySizeException
    {
 
        return nearestDistance(key, dist, new HammingDistance());
    }
 
    /**
     * Find KD-tree nodes whose keys are <I>n</I> nearest neighbors to
     * key. Uses algorithm above.  Neighbors are returned in ascending
     * order of distance to key. 
     *
     * @param key key for KD-tree node
     * @param n how many neighbors to find
     * @param checker an optional object to filter matches
     *
     * @return objects at node nearest to key, or null on failure
     *
     * @throws KeySizeException if key.length mismatches K
     * @throws IllegalArgumentException if <I>n</I> is negative or
     * exceeds tree size 
     */
    public List<T> nearest(double[] key, int n, CheckerInterface<T> checker)
            throws KeySizeException, IllegalArgumentException
    {
 
        if (n <= 0)
        {
            return new LinkedList<T>();
        }
 
        NearestNeighborList<KDNode<T>> nnl = getnbrs(key, n, checker);
 
        n = nnl.getSize();
        Stack<T> nbrs = new Stack<T>();
 
        for (int i = 0; i < n; ++i)
        {
            KDNode<T> kd = nnl.removeHighest();
            nbrs.push(kd.v);
        }
 
        return nbrs;
    }
 
    /** 
     * Range search in a KD-tree.  Uses algorithm translated from
     * 352.range.c of Gonnet & Baeza-Yates.
     *
     * @param lowk lower-bounds for key
     * @param uppk upper-bounds for key
     *
     * @return array of Objects whose keys fall in range [lowk,uppk]
     *
     * @throws KeySizeException on mismatch among lowk.length, uppk.length, or K
     */
    public List<T> range(double[] lowk, double[] uppk)
            throws KeySizeException
    {
 
        if (lowk.length != uppk.length)
        {
            throw new KeySizeException();
        } else if (lowk.length != m_K)
        {
            throw new KeySizeException();
        } else
        {
            List<KDNode<T>> found = new LinkedList<KDNode<T>>();
            KDNode.rsearch(new HPoint(lowk), new HPoint(uppk),
                    m_root, 0, m_K, found);
            List<T> o = new LinkedList<T>();
            for (KDNode<T> node : found)
            {
                o.add(node.v);
            }
            return o;
        }
    }
 
    public int size()
    { /* added by MSL */
        return m_count;
    }
 
    public String toString()
    {
        return m_root.toString(0);
    }
 
    private NearestNeighborList<KDNode<T>> getnbrs(double[] key)
            throws KeySizeException
    {
        return getnbrs(key, m_count, null);
    }
 
    private NearestNeighborList<KDNode<T>> getnbrs(double[] key, int n,
            CheckerInterface<T> checker) throws KeySizeException
    {
 
        if (key.length != m_K)
        {
            throw new KeySizeException();
        }
 
        NearestNeighborList<KDNode<T>> nnl = new NearestNeighborList<KDNode<T>>(n);
 
        // initial call is with infinite hyper-rectangle and max distance
        HRect hr = HRect.infiniteHRect(key.length);
        double max_dist_sqd = Double.MAX_VALUE;
        HPoint keyp = new HPoint(key);
 
        if (m_count > 0)
        {
            long timeout = (this.m_timeout > 0)
                    ? (System.currentTimeMillis() + this.m_timeout)
                    : 0;
            KDNode.nnbr(m_root, keyp, hr, max_dist_sqd, 0, m_K, nnl, checker, timeout);
        }
 
        return nnl;
 
    }
 
    private List<T> nearestDistance(double[] key, double dist,
            DistanceMetric metric) throws KeySizeException
    {
 
        NearestNeighborList<KDNode<T>> nnl = getnbrs(key);
        int n = nnl.getSize();
        Stack<T> nbrs = new Stack<T>();
 
        for (int i = 0; i < n; ++i)
        {
            KDNode<T> kd = nnl.removeHighest();
            HPoint p = kd.k;
            if (metric.distance(kd.k.coord, key) < dist)
            {
                nbrs.push(kd.v);
            }
        }
 
        return nbrs;
    }
}