Skip to content

Commit

Permalink
BallTree WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed Aug 11, 2024
1 parent 2be2176 commit e907764
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 76 deletions.
40 changes: 10 additions & 30 deletions src/main/java/org/tinspin/index/balltree/BTNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class BTNode<T> {
this.values = values;
}

@SuppressWarnings("unchecked")
BTNode(double[] center, double radius, BTNode<T> parent, BTNode<T> left, BTNode<T> right) {
this.center = center;
this.radius = radius;
Expand All @@ -61,7 +60,7 @@ public class BTNode<T> {
this.right = right;
}

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

ArrayList<PointEntry<T>> left = new ArrayList<>();
ArrayList<PointEntry<T>> right = new ArrayList<>();
ArrayList<PointEntry<T>> leftPoints = new ArrayList<>();
ArrayList<PointEntry<T>> rightPoints = new ArrayList<>();
for (int i = 0; i < vals.size(); i++) {
PointEntry<T> pe = vals.get(i);
if (pe.point()[splitDim] >= splitValue) {
right.add(pe);
rightPoints.add(pe);
} else {
left.add(pe);
leftPoints.add(pe);
}
}

double[] centerLeft = new double[dims];
double[] centerRight = new double[dims];
double radiusLeft = BTUtil.calcBoundingSphere(left, centerLeft);
double radiusRight = BTUtil.calcBoundingSphere(left, centerRight);
double radiusLeft = BTUtil.calcBoundingSphere(leftPoints, centerLeft);
double radiusRight = BTUtil.calcBoundingSphere(rightPoints, centerRight);

this.left = new BTNode<>(centerLeft, radiusLeft, this, left);
this.right = new BTNode<>(centerRight, radiusRight, this, right);
this.left = new BTNode<>(centerLeft, radiusLeft, this, leftPoints);
this.right = new BTNode<>(centerRight, radiusRight, this, rightPoints);

return findBestChildForInsert(e);
}
Expand Down Expand Up @@ -155,25 +154,6 @@ private BTNode<T> findBestChildForInsert(PointEntry<T> e) {
return resizeVolLeft > resizeVolRight ? this.right : this.left;
}


/**
* The subnode position has reverse ordering of the point's
* dimension ordering. Dimension 0 of a point is the highest
* ordered bit in the position.
* @param p point
* @return subnode position
*/
private int calcSubPosition(double[] p) {
int subNodePos = 0;
for (int d = 0; d < center.length; d++) {
subNodePos <<= 1;
if (p[d] >= center[d]) {
subNodePos |= 1;
}
}
return subNodePos;
}

PointEntry<T> remove(BTNode<T> parent, double[] key, int maxNodeSize, Predicate<PointEntry<T>> pred) {
if (!isLeaf()) {
if (DIST.dist(left.center, key) <= left.radius) {
Expand Down
46 changes: 0 additions & 46 deletions src/main/java/org/tinspin/index/balltree/BTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,6 @@ public static boolean isPointEqual(double[] p1, double[] p2) {
return true;
}

// public static boolean isRectEqual(double[] p1L, double[] p1U, double[] p2L, double[] p2U) {
// return isPointEqual(p1L, p2L) && isPointEqual(p1U, p2U);
// }
//
// public static <T> boolean isRectEqual(BoxEntry<T> e, double[] keyL, double[] keyU) {
// return isRectEqual(e.min(), e.max(), keyL, keyU);
// }
//
// public static <T> boolean isRectEqual(BoxEntry<T> e, BoxEntry<T> e2) {
// return isRectEqual(e.min(), e.max(), e2.min(), e2.max());
// }
//
// public static boolean overlap(double[] min, double[] max, double[] min2, double[] max2) {
// for (int d = 0; d < min.length; d++) {
// if (max[d] < min2[d] || min[d] > max2[d]) {
// return false;
// }
// }
// return true;
// }

public static boolean overlap(double[] min, double[] max, double[] center, double radius) {
double[] p = new double[min.length];
for (int d = 0; d < min.length; d++) {
Expand All @@ -94,31 +73,6 @@ public static boolean overlap(double[] min, double[] max, double[] center, doubl
return PointDistance.l2(p, center) <= radius;
}

// public static boolean isRectEnclosed(double[] minEnclosed, double[] maxEnclosed, double[] minOuter, double[] maxOuter) {
// for (int d = 0; d < minOuter.length; d++) {
// if (maxOuter[d] < maxEnclosed[d] || minOuter[d] > minEnclosed[d]) {
// return false;
// }
// }
// return true;
// }
//
// /**
// * The tests for inclusion with UPPER BOUNDARY EXCLUSIVE!
// * I.e. it firs only if maxEnclosed is SMALLER than (center + radius).
// */
// public static boolean fitsIntoNode(double[] minEnclosed, double[] maxEnclosed, double[] centerNode, double radiusNode) {
// double r2 = 0;
// for (int d = 0; d < centerNode.length; d++) {
// double r = centerNode[d] -
// r2
// if ((centerNode[d] + radiusNode) <= maxEnclosed[d] || (centerNode[d] - radiusNode) > minEnclosed[d]) {
// return false;
// }
// }
// return true;
// }

public static boolean isNodeEnclosed(double[] centerEnclosed, double radiusEnclosed, double[] centerOuter, double radiusOuter) {
return PointDistance.l2(centerEnclosed, centerOuter) + radiusEnclosed <= radiusOuter;
}
Expand Down

0 comments on commit e907764

Please sign in to comment.