b69edb8c5b7b32556fdb6b39fb30afdb7940a3d7
[anneal.git] / src / edu / berkeley / qfat / geom / PointSet.java
1 package edu.berkeley.qfat.geom;
2 import edu.wlu.cs.levy.CG.KDTree;
3
4 public class PointSet<V extends HasPoint> {
5
6     private /*final*/ KDTree kd = new KDTree(3);
7     private final double[] doubles = new double[3];
8
9     public void clear() {
10         kd = new KDTree(3);
11     }
12
13     public void add(V v) {
14         Point p = v.getPoint();
15         doubles[0] = p.x;
16         doubles[1] = p.y;
17         doubles[2] = p.z;
18         try {
19             kd.insert(doubles, v);
20         } catch (Exception e) {
21             throw new Error(e);
22         }
23     }
24
25     public void remove(HasPoint v) { remove(v.getPoint()); }
26     public void remove(Point p) {
27         doubles[0] = p.x;
28         doubles[1] = p.y;
29         doubles[2] = p.z;
30         try {
31             kd.delete(doubles);
32         } catch (Exception e) { }
33     }
34
35     public V nearest(Point p) {
36         Object[] results;
37         try {
38             doubles[0] = p.x;
39             doubles[1] = p.y;
40             doubles[2] = p.z;
41             results = kd.nearest(doubles,1);
42         } catch (Exception e) {
43             throw new Error(e);
44         }
45         return (V)results[0];
46     }
47 }