--- /dev/null
+package edu.berkeley.qfat.geom;
+import javax.media.opengl.*;
+import java.util.*;
+import com.infomatiq.jsi.*;
+import com.infomatiq.jsi.rtree.*;
+
+public class RTree<V extends HasBoundingBox> {
+
+ private com.infomatiq.jsi.rtree.RTree rtree =
+ new com.infomatiq.jsi.rtree.RTree();
+
+ int lowid = 0;
+ HashMap<Integer, V> idToV = new HashMap<Integer, V>();
+ HashMap<V, Integer> vToId = new HashMap<V, Integer>();
+
+ public RTree() {
+ Properties props = new Properties();
+ props.put("MinNodeEntries", "1");
+ props.put("MaxNodeEntries", "5");
+ rtree.init(props);
+ }
+
+ public void insert(V v) {
+ int id = lowid++;
+ idToV.put(id, v);
+ vToId.put(v, id);
+ rtree.add(new com.infomatiq.jsi.Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(),
+ v.getMaxX(), v.getMaxY(), v.getMaxZ()),
+ id);
+ }
+
+ public void remove(V v) {
+ int id = vToId.get(v);
+ idToV.remove(id);
+ vToId.remove(v);
+ rtree.delete(new com.infomatiq.jsi.Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(),
+ v.getMaxX(), v.getMaxY(), v.getMaxZ()),
+ id);
+ }
+
+
+ V found = null;
+
+ private IntProcedure finder = new IntProcedure() {
+ public boolean execute(int id) {
+ found = idToV.get(id);
+ return false;
+ }
+ };
+
+ public V nearest(Point p) {
+ rtree.nearest(new com.infomatiq.jsi.Point(p.x, p.y, p.z), finder, Float.POSITIVE_INFINITY);
+ V ret = found;
+ found = null;
+ return ret;
+ }
+}