add RTree classes
[anneal.git] / src / edu / berkeley / qfat / geom / RTree.java
1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
3 import java.util.*;
4 import com.infomatiq.jsi.*;
5 import com.infomatiq.jsi.rtree.*;
6
7 public class RTree<V extends HasBoundingBox> {
8
9     private com.infomatiq.jsi.rtree.RTree rtree =
10         new com.infomatiq.jsi.rtree.RTree();
11
12     int lowid = 0;
13     HashMap<Integer, V> idToV = new HashMap<Integer, V>();
14     HashMap<V, Integer> vToId = new HashMap<V, Integer>();
15
16     public RTree() {
17         Properties props = new Properties();
18         props.put("MinNodeEntries", "1");
19         props.put("MaxNodeEntries", "5");
20         rtree.init(props);
21     }
22
23     public void insert(V v) {
24         int id = lowid++;
25         idToV.put(id, v);
26         vToId.put(v, id);
27         rtree.add(new com.infomatiq.jsi.Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(),
28                                                   v.getMaxX(), v.getMaxY(), v.getMaxZ()),
29                   id);
30     }
31
32     public void remove(V v) {
33         int id = vToId.get(v);
34         idToV.remove(id);
35         vToId.remove(v);
36         rtree.delete(new com.infomatiq.jsi.Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(),
37                                                      v.getMaxX(), v.getMaxY(), v.getMaxZ()),
38                      id);
39     }
40
41
42     V found = null;
43
44     private IntProcedure finder = new IntProcedure() {
45             public boolean execute(int id) {
46                 found = idToV.get(id);
47                 return false;
48             }
49         };
50
51     public V nearest(Point p) {
52         rtree.nearest(new com.infomatiq.jsi.Point(p.x, p.y, p.z), finder, Float.POSITIVE_INFINITY);
53         V ret = found;
54         found = null;
55         return ret;
56     }
57 }