Skip to content

Commit e907764

Browse files
committed
BallTree WIP
1 parent 2be2176 commit e907764

File tree

2 files changed

+10
-76
lines changed

2 files changed

+10
-76
lines changed

src/main/java/org/tinspin/index/balltree/BTNode.java

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class BTNode<T> {
5151
this.values = values;
5252
}
5353

54-
@SuppressWarnings("unchecked")
5554
BTNode(double[] center, double radius, BTNode<T> parent, BTNode<T> left, BTNode<T> right) {
5655
this.center = center;
5756
this.radius = radius;
@@ -61,7 +60,7 @@ public class BTNode<T> {
6160
this.right = right;
6261
}
6362

64-
@SuppressWarnings({ "unchecked", "unused" })
63+
@SuppressWarnings({"unused" })
6564
BTNode<T> tryPut(PointEntry<T> e, int maxNodeSize, boolean enforceLeaf) {
6665
if (BallTree.DEBUG && !BTUtil.fitsIntoNode(e.point(), center, radius)) {
6766
throw new IllegalStateException("e=" + Arrays.toString(e.point()) +
@@ -101,26 +100,26 @@ BTNode<T> tryPut(PointEntry<T> e, int maxNodeSize, boolean enforceLeaf) {
101100
splitDim = d;
102101
}
103102
}
104-
double splitValue = ordered[splitDim][values.size() / 2];
103+
double splitValue = ordered[splitDim][vals.size() / 2];
105104

106-
ArrayList<PointEntry<T>> left = new ArrayList<>();
107-
ArrayList<PointEntry<T>> right = new ArrayList<>();
105+
ArrayList<PointEntry<T>> leftPoints = new ArrayList<>();
106+
ArrayList<PointEntry<T>> rightPoints = new ArrayList<>();
108107
for (int i = 0; i < vals.size(); i++) {
109108
PointEntry<T> pe = vals.get(i);
110109
if (pe.point()[splitDim] >= splitValue) {
111-
right.add(pe);
110+
rightPoints.add(pe);
112111
} else {
113-
left.add(pe);
112+
leftPoints.add(pe);
114113
}
115114
}
116115

117116
double[] centerLeft = new double[dims];
118117
double[] centerRight = new double[dims];
119-
double radiusLeft = BTUtil.calcBoundingSphere(left, centerLeft);
120-
double radiusRight = BTUtil.calcBoundingSphere(left, centerRight);
118+
double radiusLeft = BTUtil.calcBoundingSphere(leftPoints, centerLeft);
119+
double radiusRight = BTUtil.calcBoundingSphere(rightPoints, centerRight);
121120

122-
this.left = new BTNode<>(centerLeft, radiusLeft, this, left);
123-
this.right = new BTNode<>(centerRight, radiusRight, this, right);
121+
this.left = new BTNode<>(centerLeft, radiusLeft, this, leftPoints);
122+
this.right = new BTNode<>(centerRight, radiusRight, this, rightPoints);
124123

125124
return findBestChildForInsert(e);
126125
}
@@ -155,25 +154,6 @@ private BTNode<T> findBestChildForInsert(PointEntry<T> e) {
155154
return resizeVolLeft > resizeVolRight ? this.right : this.left;
156155
}
157156

158-
159-
/**
160-
* The subnode position has reverse ordering of the point's
161-
* dimension ordering. Dimension 0 of a point is the highest
162-
* ordered bit in the position.
163-
* @param p point
164-
* @return subnode position
165-
*/
166-
private int calcSubPosition(double[] p) {
167-
int subNodePos = 0;
168-
for (int d = 0; d < center.length; d++) {
169-
subNodePos <<= 1;
170-
if (p[d] >= center[d]) {
171-
subNodePos |= 1;
172-
}
173-
}
174-
return subNodePos;
175-
}
176-
177157
PointEntry<T> remove(BTNode<T> parent, double[] key, int maxNodeSize, Predicate<PointEntry<T>> pred) {
178158
if (!isLeaf()) {
179159
if (DIST.dist(left.center, key) <= left.radius) {

src/main/java/org/tinspin/index/balltree/BTUtil.java

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,6 @@ public static boolean isPointEqual(double[] p1, double[] p2) {
5757
return true;
5858
}
5959

60-
// public static boolean isRectEqual(double[] p1L, double[] p1U, double[] p2L, double[] p2U) {
61-
// return isPointEqual(p1L, p2L) && isPointEqual(p1U, p2U);
62-
// }
63-
//
64-
// public static <T> boolean isRectEqual(BoxEntry<T> e, double[] keyL, double[] keyU) {
65-
// return isRectEqual(e.min(), e.max(), keyL, keyU);
66-
// }
67-
//
68-
// public static <T> boolean isRectEqual(BoxEntry<T> e, BoxEntry<T> e2) {
69-
// return isRectEqual(e.min(), e.max(), e2.min(), e2.max());
70-
// }
71-
//
72-
// public static boolean overlap(double[] min, double[] max, double[] min2, double[] max2) {
73-
// for (int d = 0; d < min.length; d++) {
74-
// if (max[d] < min2[d] || min[d] > max2[d]) {
75-
// return false;
76-
// }
77-
// }
78-
// return true;
79-
// }
80-
8160
public static boolean overlap(double[] min, double[] max, double[] center, double radius) {
8261
double[] p = new double[min.length];
8362
for (int d = 0; d < min.length; d++) {
@@ -94,31 +73,6 @@ public static boolean overlap(double[] min, double[] max, double[] center, doubl
9473
return PointDistance.l2(p, center) <= radius;
9574
}
9675

97-
// public static boolean isRectEnclosed(double[] minEnclosed, double[] maxEnclosed, double[] minOuter, double[] maxOuter) {
98-
// for (int d = 0; d < minOuter.length; d++) {
99-
// if (maxOuter[d] < maxEnclosed[d] || minOuter[d] > minEnclosed[d]) {
100-
// return false;
101-
// }
102-
// }
103-
// return true;
104-
// }
105-
//
106-
// /**
107-
// * The tests for inclusion with UPPER BOUNDARY EXCLUSIVE!
108-
// * I.e. it firs only if maxEnclosed is SMALLER than (center + radius).
109-
// */
110-
// public static boolean fitsIntoNode(double[] minEnclosed, double[] maxEnclosed, double[] centerNode, double radiusNode) {
111-
// double r2 = 0;
112-
// for (int d = 0; d < centerNode.length; d++) {
113-
// double r = centerNode[d] -
114-
// r2
115-
// if ((centerNode[d] + radiusNode) <= maxEnclosed[d] || (centerNode[d] - radiusNode) > minEnclosed[d]) {
116-
// return false;
117-
// }
118-
// }
119-
// return true;
120-
// }
121-
12276
public static boolean isNodeEnclosed(double[] centerEnclosed, double radiusEnclosed, double[] centerOuter, double radiusOuter) {
12377
return PointDistance.l2(centerEnclosed, centerOuter) + radiusEnclosed <= radiusOuter;
12478
}

0 commit comments

Comments
 (0)