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