diff --git a/pom.xml b/pom.xml index f22484d0..0ec4b4ef 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ 1.4.12 Unknown - + org.bukkit @@ -34,6 +34,11 @@ ${powermock.version} test + + ch.ethz.globis.phtree + phtree + 2.3.0 + http://www.citizensnpcs.co diff --git a/src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java b/src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java index 94adef22..e3b8f2cb 100644 --- a/src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java +++ b/src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java @@ -11,6 +11,7 @@ import com.google.common.base.Function; import com.google.common.base.Supplier; +import ch.ethz.globis.phtree.PhTreeSolid; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.ai.Goal; import net.citizensnpcs.api.ai.event.NavigationCompleteEvent; @@ -19,7 +20,6 @@ import net.citizensnpcs.api.ai.tree.BehaviorStatus; import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer; import net.citizensnpcs.api.npc.NPC; -import net.citizensnpcs.api.util.cuboid.QuadTree; /** * A sample {@link Goal}/{@link Behavior} that will wander within a certain radius or {@link QuadTree}. @@ -32,11 +32,12 @@ public class WanderGoal extends BehaviorGoalAdapter implements Listener { private final NPC npc; private boolean paused; private final Random random = new Random(); - private final Supplier tree; + private final Supplier> tree; private int xrange; private int yrange; - private WanderGoal(NPC npc, int xrange, int yrange, Supplier tree, Function fallback) { + private WanderGoal(NPC npc, int xrange, int yrange, Supplier> tree, + Function fallback) { this.npc = npc; this.xrange = xrange; this.yrange = yrange; @@ -53,7 +54,8 @@ private Location findRandomPosition() { int z = base.getBlockZ() + random.nextInt(2 * xrange) - xrange; Block block = base.getWorld().getBlockAt(x, y, z); if (MinecraftBlockExaminer.canStandOn(block)) { - if (tree != null && tree.get() != null && tree.get().search(x, y, z).isEmpty()) { + long[] pt = { x, y, z }; + if (tree != null && tree.get() != null && !tree.get().contains(pt, pt)) { continue; } found = block.getLocation().add(0, 1, 0); @@ -126,7 +128,8 @@ public static WanderGoal createWithNPCAndRange(NPC npc, int xrange, int yrange) return createWithNPCAndRangeAndTree(npc, xrange, yrange, null); } - public static WanderGoal createWithNPCAndRangeAndTree(NPC npc, int xrange, int yrange, Supplier tree) { + public static WanderGoal createWithNPCAndRangeAndTree(NPC npc, int xrange, int yrange, + Supplier> tree) { return createWithNPCAndRangeAndTreeAndFallback(npc, xrange, yrange, tree, null); } @@ -140,13 +143,13 @@ public static WanderGoal createWithNPCAndRangeAndTree(NPC npc, int xrange, int y * @param yrange * y range, in blocks * @param tree - * an optional {@link QuadTree} supplier to allow only wandering within a certain {@link QuadTree} + * an optional {@link PhTreeSolid} supplier to allow only wandering within a certain {@link PhTreeSolid} * @param fallback * an optional fallback location * @return the built goal */ public static WanderGoal createWithNPCAndRangeAndTreeAndFallback(NPC npc, int xrange, int yrange, - Supplier tree, Function fallback) { + Supplier> tree, Function fallback) { return new WanderGoal(npc, xrange, yrange, tree, fallback); } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/api/hpastar/HPAGraph.java b/src/main/java/net/citizensnpcs/api/hpastar/HPAGraph.java index 36cf17c5..44ca49e6 100644 --- a/src/main/java/net/citizensnpcs/api/hpastar/HPAGraph.java +++ b/src/main/java/net/citizensnpcs/api/hpastar/HPAGraph.java @@ -13,23 +13,30 @@ import com.google.common.collect.Lists; +import ch.ethz.globis.phtree.PhTree; import net.citizensnpcs.api.astar.Plan; import net.citizensnpcs.api.astar.pathfinder.BlockSource; import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer; import net.citizensnpcs.api.astar.pathfinder.Path; public class HPAGraph { - // TODO: y-clusters private final BlockSource blockSource; public List> clusters = Lists.newArrayList(); + // TODO: y-clusters + private final int cx, cy, cz; + private final List> phtrees = Lists.newArrayList(); - public HPAGraph(BlockSource blockSource) { + public HPAGraph(BlockSource blockSource, int cx, int cy, int cz) { this.blockSource = blockSource; + this.cx = cx; + this.cy = cy; + this.cz = cz; } public void addClustersAtDepth(int depth, List other) { while (clusters.size() <= depth) { clusters.add(new ArrayList()); + // phtrees.add(new ArrayList>()); } clusters.get(depth).addAll(other); } diff --git a/src/main/java/net/citizensnpcs/api/util/cuboid/BookmarkedResult.java b/src/main/java/net/citizensnpcs/api/util/cuboid/BookmarkedResult.java deleted file mode 100644 index 63e33c98..00000000 --- a/src/main/java/net/citizensnpcs/api/util/cuboid/BookmarkedResult.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.citizensnpcs.api.util.cuboid; - -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -public class BookmarkedResult implements Iterable { - final QuadNode bookmark; - private final List results; - - BookmarkedResult(QuadNode node, List cuboids) { - bookmark = node; - results = Collections.unmodifiableList(cuboids); - } - - public Collection getResults() { - return results; - } - - @Override - public Iterator iterator() { - return results.iterator(); - } - - @SuppressWarnings("unchecked") - public static final BookmarkedResult EMPTY = new BookmarkedResult(null, (Collections.EMPTY_LIST)); -} diff --git a/src/main/java/net/citizensnpcs/api/util/cuboid/QuadCuboid.java b/src/main/java/net/citizensnpcs/api/util/cuboid/QuadCuboid.java deleted file mode 100644 index 51e1b35c..00000000 --- a/src/main/java/net/citizensnpcs/api/util/cuboid/QuadCuboid.java +++ /dev/null @@ -1,90 +0,0 @@ -package net.citizensnpcs.api.util.cuboid; - -public class QuadCuboid { - private int hashcode = 0; - int[] highCoords = { 0, 0, 0 }; - int[] highIndex = new int[3]; - int[] lowCoords = { 0, 0, 0 }; - int[] lowIndex = new int[3]; - - public QuadCuboid(int x1, int y1, int z1, int x2, int y2, int z2) { - lowCoords[0] = x1; - lowCoords[1] = y1; - lowCoords[2] = z1; - - highCoords[0] = x2; - highCoords[1] = y2; - highCoords[2] = z2; - - this.normalize(); - } - - public QuadCuboid(int[] low, int[] high) { - lowCoords = low.clone(); - highCoords = high.clone(); - normalize(); - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } else if (!(o instanceof QuadCuboid)) { - return false; - } - QuadCuboid c = (QuadCuboid) o; - - for (int i = 0; i < 3; i++) { - if (lowCoords[i] != c.lowCoords[i]) { - return false; - } - if (highCoords[i] != c.highCoords[i]) { - return false; - } - } - return true; - } - - @Override - public int hashCode() { - return hashcode; - } - - public boolean includesPoint(int x, int y, int z) { - return lowCoords[0] <= x && lowCoords[1] <= y && lowCoords[2] <= z && highCoords[0] >= x && highCoords[1] >= y - && highCoords[2] >= z; - } - - public boolean includesPoint(int[] point) { - return this.includesPoint(point[0], point[1], point[2]); - } - - /** - * Normalize the corners so that all A is <= B This is CRITICAL to have for comparison to a point - */ - private void normalize() { - for (int i = 0; i < 3; i++) { - if (lowCoords[i] > highCoords[i]) { - int temp = lowCoords[i]; - lowCoords[i] = highCoords[i]; - highCoords[i] = temp; - } - hashcode ^= highCoords[i] ^ (~lowCoords[i]); - } - } - - public boolean overlaps(QuadCuboid cuboid) { - for (int i = 0; i < 3; i++) { - if (lowCoords[i] > cuboid.highCoords[i] || cuboid.lowCoords[i] > highCoords[i]) { - return false; - } - } - return true; - } - - @Override - public String toString() { - return "x1=" + lowCoords[0] + " y1=" + lowCoords[1] + " z1=" + lowCoords[2] + " x2=" + highCoords[0] + " y2=" - + highCoords[1] + " z2=" + highCoords[2]; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/cuboid/QuadNode.java b/src/main/java/net/citizensnpcs/api/util/cuboid/QuadNode.java deleted file mode 100644 index b14da7a2..00000000 --- a/src/main/java/net/citizensnpcs/api/util/cuboid/QuadNode.java +++ /dev/null @@ -1,75 +0,0 @@ -package net.citizensnpcs.api.util.cuboid; - -import java.util.ArrayList; -import java.util.List; - -public class QuadNode { - // We only hold the cuboids that fit completely inside of us. - // Cuboids are always held in their minimal bounding node - final List cuboids = new ArrayList(); - - // We only hold our list of cuboids, but to prevent duplicating lists and - // having to climb to the root to find all of the cuboids that we need to - // look - // at - // Point to the next highest node with cuboids to examine - QuadNode nextListHolder; - - QuadNode parent = null; - - // children. Lower Left 0; Right + 1; Upper + 2 - /* - * +0 |+1 - * +2 2 | 3 - * ------------ - * +0 0 | 1 - */ - final QuadNode[] quads = new QuadNode[4]; - - // Length of a side, always a power of two - final int size; - - // Indexed by least x and least z - int x; // Traditional X in 2d - - int z; // What would be Y but this is minecraft - - QuadNode(int x, int z, int size, QuadNode parent) { - this.x = x; - this.z = z; - this.size = size; - this.parent = parent; - if (parent == null) { - return; - } - // If the parent is holding cuboids it is our next list holder - if (parent.cuboids.size() != 0) { - this.nextListHolder = parent; - } else {// Otherwise whatever it's next list holder is is also ours - // (even - // null) - this.nextListHolder = parent.nextListHolder; - } - } - - @Override - public boolean equals(Object o) { - if (o == this) { - return true; - } else if (!(o instanceof QuadNode)) { - return false; - } - QuadNode n = (QuadNode) o; - return x == n.x && z == n.z && size == n.size; - } - - @Override - public int hashCode() { - return x ^ z ^ (~size); - } - - @Override - public String toString() { - return "(" + x + "," + z + "; " + size + ")"; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/cuboid/QuadTree.java b/src/main/java/net/citizensnpcs/api/util/cuboid/QuadTree.java deleted file mode 100644 index 14d339e3..00000000 --- a/src/main/java/net/citizensnpcs/api/util/cuboid/QuadTree.java +++ /dev/null @@ -1,603 +0,0 @@ -package net.citizensnpcs.api.util.cuboid; - -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Deque; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -public class QuadTree { - QuadNode root; - - /** - * Adds the {@link QuadCuboid} to the target node and fixes up the children's list holder link - * - * @param node - * The target node - * @param cuboid - * The cuboid to add - */ - private void addAndFixListHolders(QuadNode node, QuadCuboid cuboid) { - node.cuboids.add(cuboid); - // This isn't our first cuboid, so no fix needed - if (node.cuboids.size() > 1) { - return; - } - // Descend the tree. When we find a node with no cuboids it needs a new - // list holder - Deque todo = new ArrayDeque(); - todo.push(node); - QuadNode current; - do { - current = todo.pop(); - for (QuadNode child : current.quads) { - if (child == null) { - continue; - } - // If the child isn't holding a list of cuboids itself - // (which would make it the list holder link for its children) - // Then we need to fix it's children as well - if (child.cuboids.size() == 0) { - todo.push(child); - } - child.nextListHolder = node; - } - } while (!todo.isEmpty()); - } - - private QuadNode ascendFirstSearch(QuadNode node, int x, int z) { - while (node != null && (node.x > x || node.z > z || (node.x + node.size) < x || (node.z + node.size) < z)) { - node = node.parent; - } - if (node == null) - return null; - return descendAndSearch(node, x, z); - } - - private void beginTree(QuadCuboid cuboid) { - int size = 128; - int minSize = Math.abs(cuboid.lowCoords[0] - cuboid.highCoords[0]); - int minSizeB = Math.abs(cuboid.lowCoords[2] - cuboid.highCoords[2]); - if (minSize < minSizeB) { - minSize = minSizeB; - } - while (size < minSize) { - size = size << 1; - } - root = new QuadNode(cuboid.lowCoords[0], cuboid.lowCoords[2], size - 1, null); - } - - /** - * Returns -1 for too small, 0 for minimal, 1 for larger than needed - *

- * Fit is based on the larger side. Means more tests but consumes an order of magnitude or less memory. - * - * @param node - * @param cuboid - * @return - */ - private int containerFit(QuadNode node, QuadCuboid cuboid) { - int minSizeA = Math.abs(cuboid.lowCoords[0] - cuboid.highCoords[0]); - int minSizeB = Math.abs(cuboid.lowCoords[2] - cuboid.highCoords[2]); - int fitSize; - if (minSizeA < minSizeB) { - fitSize = minSizeB; - } else { - fitSize = minSizeA; - } - - if (node.size < fitSize) { - return -1; - } else if (node.size == 1 || (node.size >> 1) < fitSize) { - return 0; - } else { - return 1; - } - } - - private QuadNode descendAndCreate(QuadNode start, QuadCuboid cuboid) { - QuadNode next = start; - while (containerFit(next, cuboid) > 0) { - int i = 0; - int nX = 0; - int nZ = 0; - int half = (next.size >> 1); - if (cuboid.lowCoords[0] > (next.x + half)) { - i++; - nX = half + 1; - } - if (cuboid.lowCoords[2] > (next.z + half)) { - i += 2; - nZ = half + 1; - } - if (next.quads[i] == null) { - next.quads[i] = new QuadNode(next.x + nX, next.z + nZ, half, next); - } - next = next.quads[i]; - } - return next; - } - - private QuadNode descendAndSearch(QuadNode node, int x, int z) { - QuadNode next = node; - while (next != null) { - node = next; - int half = node.size >> 1; - int i = 0; - if (x > (node.x + half)) { - i++; - } - if (z > (node.z + half)) { - i += 2; - } - next = node.quads[i]; - } - return node; - } - - private QuadNode descendNoCreate(QuadNode start, QuadCuboid cuboid) { - QuadNode next = start; - while (containerFit(next, cuboid) > 0) { - int i = 0; - int nX = 0; - int nZ = 0; - int half = (next.size >> 1); - if (cuboid.lowCoords[0] > (next.x + half)) { - i++; - nX = half + 1; - } - if (cuboid.lowCoords[2] > (next.z + half)) { - i += 2; - nZ = half + 1; - } - if (next.quads[i] == null) { - next = new QuadNode(next.x + nX, next.z + nZ, half, next); - } else { - next = next.quads[i]; - } - } - return next; - } - - public BookmarkedResult findOverlappingCuboids(int x, int y, int z) { - return relatedSearch(null, x, y, z); - } - - public BookmarkedResult findOverlappingCuboidsFromBookmark(BookmarkedResult bookmark, int x, int y, int z) { - return relatedSearch(bookmark.bookmark, x, y, z); - } - - /** - * Oftentimes a node will overlap with the neighbors of a node Since we always search for the next node based on the - * lower left we know that the left and bottom will not go over the edge, leaving only the top, right, and upper - * right possibilities need be regarded. Spits out a list of cuboids that are fit for "insertion" although we just - * use them for the search and actually attach the original cuboid. We also return the remainder shard if we - * generated any others. At the other end we only include a node if it's shard didn't re-shard. Keeps the tree - * search spaces minimal. - */ - private List generateShards(QuadNode node, QuadCuboid cuboid) { - List shards = new ArrayList(4); - int top = node.z + node.size; - int right = node.x + node.size; - int tmp; - - // find a shard above if it exists - if (top < cuboid.highCoords[2]) { - // Find out if it extends past the top only or the right and top - // Limit the "top" shard to only directly above the original node - if (right < cuboid.highCoords[0]) { - tmp = right; - } else { - tmp = cuboid.highCoords[0]; - } - shards.add(new QuadCuboid(cuboid.lowCoords[0], 0, top + 1, tmp, 0, cuboid.highCoords[2])); - } - // Find a shard to the right - if (right < cuboid.highCoords[0]) { - // find if we extend past the top as well - // Limit the "right" shard to only directly right - if (top < cuboid.highCoords[2]) { - tmp = top; - } else { - tmp = cuboid.highCoords[2]; - } - shards.add(new QuadCuboid(right + 1, 0, cuboid.lowCoords[2], cuboid.highCoords[0], 0, tmp)); - } - // Check for a top right shard - if (right < cuboid.highCoords[0] && top < cuboid.highCoords[2]) { - shards.add(new QuadCuboid(right + 1, 0, top + 1, cuboid.highCoords[0], 0, cuboid.highCoords[2])); - } - // include the remainder as a shard if we generated any others - if (shards.size() > 0) { - shards.add(new QuadCuboid(cuboid.lowCoords[0], 0, cuboid.lowCoords[2], right, 0, top)); - } - return shards; - } - - public List getAllOverlapsWith(QuadCuboid cuboid) { - if (root == null) - return Collections.emptyList(); - - // if this cuboid falls outside of the tree, we need to repot the tree - // to - // gain a wider perspective! - if (!nodeFullyContainsCuboid(root, cuboid)) { - repotTree(cuboid); - } - QuadNode node = root; - node = descendNoCreate(node, cuboid); - // Now that we have our target we potentially need to generate shards - // and - // target their nodes as well - List targets = getAllTargetsNoCreate(node, cuboid); - Deque children = new ArrayDeque(); - Set cuboids = new HashSet(256); - // Generous initial capacity for speed - QuadNode childTarget; - // Of note: adding all the cuboids to the set and then testing is faster - // than testing as we go and potentially getting out faster - // This is especially true when there is less likely to be an overlap - // anyway - for (QuadNode target : targets) { - // Drill down to the children nodes to get the smaller cuboids - // contained therein - children.add(target); - do { - childTarget = children.pop(); - for (QuadNode child : childTarget.quads) { - if (child == null) { - continue; - } - children.push(child); - cuboids.addAll(child.cuboids); - } - } while (!children.isEmpty()); - // Then ascend backup and add the ones there - while (target != null) { - cuboids.addAll(target.cuboids); - target = target.nextListHolder; - } - } - List overlaps = new ArrayList(); - for (QuadCuboid pc : cuboids) { - if (cuboid.overlaps(pc)) { - overlaps.add(pc); - } - } - return overlaps; - } - - // Finds all the nodes that a cuboid should reside in (handles sharding) - private List getAllTargets(QuadNode initialNode, QuadCuboid cuboid) { - List targets = new ArrayList(); - // Generate the initial shards - Deque shards = new ArrayDeque(); - shards.addAll(generateShards(initialNode, cuboid)); - - QuadNode node; - while (!shards.isEmpty()) { - QuadCuboid shard = shards.pop(); - node = descendAndCreate(root, shard); - List newShards = generateShards(node, shard); - // If no shards were made then this is is the bounding node for this - // shard. Include it. - if (newShards.size() == 0) { - targets.add(node); - } else { - shards.addAll(newShards); - } - } - - // If the initial shard attempt turns out to not have had - // to generate shards then we need to add the initial node - if (targets.size() == 0) { - targets.add(initialNode); - } - - return targets; - } - - // Finds all the nodes that a cuboid should reside in (handles sharding) - private List getAllTargetsNoCreate(QuadNode initialNode, QuadCuboid cuboid) { - List targets = new ArrayList(); - // Generate the initial shards - Deque shards = new ArrayDeque(); - shards.addAll(generateShards(initialNode, cuboid)); - - QuadNode node; - while (!shards.isEmpty()) { - QuadCuboid shard = shards.pop(); - node = descendNoCreate(root, shard); - List newShards = generateShards(node, shard); - // If no shards were made then this is is the bounding node for this - // shard. Include it. - if (newShards.size() == 0) { - targets.add(node); - } else { - shards.addAll(newShards); - } - } - - // If the initial shard attempt turns out to not have had - // to generate shards then we need to add the initial node - if (targets.size() == 0) { - targets.add(initialNode); - } - - return targets; - } - - private List getMatchingCuboids(QuadNode target, int x, int y, int z) { - List matches = new ArrayList(); - while (target != null) { - for (QuadCuboid potential : target.cuboids) { - if (potential.includesPoint(x, y, z)) { - matches.add(potential); - } - } - target = target.nextListHolder; - } - return matches; - } - - public void insert(QuadCuboid cuboid) { - if (root == null) { - beginTree(cuboid); - } - // if this cuboid falls outside of the tree, we need to repot the tree - // to - // gain a wider perspective! - if (!nodeFullyContainsCuboid(root, cuboid)) { - repotTree(cuboid); - } - QuadNode node = root; - node = descendAndCreate(node, cuboid); - // Now that we have our target we potentially need to generate shards - // and - // target their nodes as well - List targets = getAllTargets(node, cuboid); - // Add the cuboid everywhere it belongs - for (QuadNode target : targets) { - addAndFixListHolders(target, cuboid); - } - } - - /** - * Attempts to insert the node ONLY if there are no overlaps with existing nodes - * - * @param cuboid - * cuboid to insert - * @return success or failure - */ - public boolean insertIfNoOverlaps(QuadCuboid cuboid) { - if (root == null) { - insert(cuboid); - return true; - } - // if this cuboid falls outside of the tree, we need to repot the tree - // to - // gain a wider perspective! - if (!nodeFullyContainsCuboid(root, cuboid)) { - repotTree(cuboid); - } - QuadNode node = root; - node = descendAndCreate(node, cuboid); - // Now that we have our target we potentially need to generate shards - // and target their nodes as well - List targets = getAllTargets(node, cuboid); - Deque children = new ArrayDeque(); - Set cuboids = new HashSet(256); - // Generous initial capacity for speed - QuadNode childTarget; - // Of note: adding all the cuboids to the set and then testing is faster - // than testing as we go and potentially getting out faster - // This is especially true when there is less likely to be an overlap - // anyway - for (QuadNode target : targets) { - // Drill down to the children nodes to get the smaller cuboids - // contained therein - children.add(target); - do { - childTarget = children.pop(); - for (QuadNode child : childTarget.quads) { - if (child == null) { - continue; - } - children.push(child); - cuboids.addAll(child.cuboids); - } - } while (!children.isEmpty()); - // Then ascend backup and add the ones there - while (target != null) { - cuboids.addAll(target.cuboids); - target = target.nextListHolder; - } - } - for (QuadCuboid pc : cuboids) { - if (cuboid.overlaps(pc)) { - for (QuadNode target : targets) { - if (target.cuboids.size() == 0) { - pruneTree(node); - } - } - return false; - } - } - // Add the cuboid everywhere it belongs - for (QuadNode target : targets) { - addAndFixListHolders(target, cuboid); - } - return true; - } - - private boolean nodeFullyContainsCuboid(QuadNode node, QuadCuboid cuboid) { - return node.x <= cuboid.lowCoords[0] && node.z <= cuboid.lowCoords[2] - && (node.x + node.size) >= cuboid.highCoords[0] && (node.z + node.size) >= cuboid.highCoords[2]; - } - - public boolean overlapsExisting(QuadCuboid cuboid) { - if (root == null) { - return false; - } - // if this cuboid falls outside of the tree, we need to repot the tree - // to - // gain a wider perspective! - if (!nodeFullyContainsCuboid(root, cuboid)) { - repotTree(cuboid); - } - QuadNode node = root; - node = descendNoCreate(node, cuboid); - // Now that we have our target we potentially need to generate shards - // and - // target their nodes as well - List targets = getAllTargetsNoCreate(node, cuboid); - Deque children = new ArrayDeque(); - Set cuboids = new HashSet(256); - // Generous initial capacity for speed - QuadNode childTarget; - // Of note: adding all the cuboids to the set and then testing is faster - // than testing as we go and potentially getting out faster - // This is especially true when there is less likely to be an overlap - // anyway - for (QuadNode target : targets) { - // Drill down to the children nodes to get the smaller cuboids - // contained therein - children.add(target); - do { - childTarget = children.pop(); - for (QuadNode child : childTarget.quads) { - if (child == null) { - continue; - } - children.push(child); - cuboids.addAll(child.cuboids); - } - } while (!children.isEmpty()); - // Then ascend backup and add the ones there - while (target != null) { - cuboids.addAll(target.cuboids); - target = target.nextListHolder; - } - } - for (QuadCuboid pc : cuboids) { - if (cuboid.overlaps(pc)) { - return true; - } - } - return false; - } - - /** - * Removes any node from the tree that no longer serves a purpose, starting from the node given and moving up - * - * @param node - */ - private void pruneTree(QuadNode node) { - int i; - while (node.parent != null && node.quads[0] == null && node.quads[1] == null && node.quads[2] == null - && node.quads[3] == null) { - i = 0; - if (node.x != node.parent.x) - i++; - - if (node.z != node.parent.z) - i += 2; - - node = node.parent; - node.quads[i] = null; - } - } - - private BookmarkedResult relatedSearch(QuadNode bookmark, int x, int y, int z) { - if (bookmark == null) - bookmark = root; - - QuadNode node = ascendFirstSearch(bookmark, x, z); - return new BookmarkedResult(node, getMatchingCuboids(node, x, y, z)); - } - - public void remove(QuadCuboid cuboid) { - // No root? No-Op! - if (root == null) { - return; - } - QuadNode node; - // Should not create any new nodes, but only if the cuboid is, in fact, - // in - // the tree - node = descendAndCreate(root, cuboid); - // Using the same algorithm that was used during creation will give us - // the - // same list of nodes to examine - List targets = getAllTargets(node, cuboid); - for (QuadNode target : targets) { - removeAndFixListHolders(target, cuboid); - } - } - - private void removeAndFixListHolders(QuadNode node, QuadCuboid cuboid) { - node.cuboids.remove(cuboid); - // This wasn't our only cuboid, so no fix needed - if (node.cuboids.size() > 0) - return; - - // Descend the tree. When we find a node with no children we know it - // needs a new list holder - Deque todo = new ArrayDeque(); - todo.push(node); - QuadNode current; - do { - current = todo.pop(); - for (QuadNode child : current.quads) { - if (child == null) { - continue; - } - // If the child isn't holding a list of cuboids itself - // (which would make it the list holder link for its children) - // Then we need to fix its children as well - if (child.cuboids.size() == 0) - todo.push(child); - - child.nextListHolder = node.nextListHolder; - } - } while (!todo.isEmpty()); - pruneTree(node); - } - - /** - * Grow the tree beyond the root in the direction of the target node - * - * @param cuboid - */ - private void repotTree(QuadCuboid cuboid) { - QuadNode oldRoot; - int i; - do { - oldRoot = root; - root = new QuadNode(oldRoot.x, oldRoot.z, (oldRoot.size << 1) + 1, null); - oldRoot.parent = root; - // Figure out the best direction to grow in (that is, which quadrant - // is the old root in the new root?) - // We start at lower left (quad 0) - i = 0; - // The target is left of us - if (cuboid.lowCoords[0] < root.x) { - i++; - root.x -= oldRoot.size + 1; - } - // The target is below us - if (cuboid.lowCoords[2] < root.z) { - i += 2; - root.z -= oldRoot.size + 1; - } - root.quads[i] = oldRoot; - } while (!nodeFullyContainsCuboid(root, cuboid)); - } - - public List search(int x, int y, int z) { - QuadNode node = descendAndSearch(root, x, z); - return getMatchingCuboids(node, x, y, z); - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/Circle.java b/src/main/java/net/citizensnpcs/api/util/prtree/Circle.java deleted file mode 100644 index 9b4ec6ef..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/Circle.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.ArrayList; -import java.util.List; - -/** - * A simple circular data structure. - * - * @param - * the element type - */ -class Circle { - private int currentPos; - private final List data; - - public Circle(int size) { - data = new ArrayList(size); - } - - public void add(T t) { - data.add(t); - } - - public T get(int pos) { - pos %= data.size(); - return data.get(pos); - } - - public T getNext() { - T ret = data.get(currentPos++); - currentPos %= data.size(); - return ret; - } - - public int getNumElements() { - return data.size(); - } - - public void reset() { - currentPos = 0; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/DataComparators.java b/src/main/java/net/citizensnpcs/api/util/prtree/DataComparators.java deleted file mode 100644 index 17690437..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/DataComparators.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.Comparator; - -class DataComparators implements NodeComparators { - private final MBRConverter converter; - - public DataComparators(MBRConverter converter) { - this.converter = converter; - } - - public Comparator getMaxComparator(final int axis) { - return new Comparator() { - public int compare(T t1, T t2) { - double d1 = converter.getMax(axis, t1); - double d2 = converter.getMax(axis, t2); - return Double.compare(d1, d2); - } - }; - } - - public Comparator getMinComparator(final int axis) { - return new Comparator() { - public int compare(T t1, T t2) { - double d1 = converter.getMin(axis, t1); - double d2 = converter.getMin(axis, t2); - return Double.compare(d1, d2); - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/DistanceCalculator.java b/src/main/java/net/citizensnpcs/api/util/prtree/DistanceCalculator.java deleted file mode 100644 index 2c84478a..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/DistanceCalculator.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A class that can calculate the distance to a given object stored in the PRTree - * - * @param - * the data type to calculate distances to - */ -public interface DistanceCalculator { - /** - * Calculate the distance between the given object and the point - * - * @param t - * the object to calculate the distance to - * @param p - * the point - * @return The calculated distance - */ - double distanceTo(T t, PointND p); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/DistanceResult.java b/src/main/java/net/citizensnpcs/api/util/prtree/DistanceResult.java deleted file mode 100644 index 61c6292d..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/DistanceResult.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * Class to hold object and distance to it - * - * @param - * The node type - */ -public class DistanceResult { - private final double dist; - private final T t; - - /** - * Create a new DistanceResult with a given object and distance - * - * @param t - * the object we are measuring the distance to - * @param dist - * the actual distance to the object - */ - public DistanceResult(T t, double dist) { - this.t = t; - this.dist = dist; - } - - /** - * Get the object - * - * @return The node object - */ - public T get() { - return t; - } - - /** - * Get the distance - * - * @return The distance to the node object - */ - public double getDistance() { - return dist; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/InternalNode.java b/src/main/java/net/citizensnpcs/api/util/prtree/InternalNode.java deleted file mode 100644 index 9e20028e..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/InternalNode.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.List; -import java.util.PriorityQueue; - -class InternalNode extends NodeBase, T> { - public InternalNode(Object[] data) { - super(data); - } - - @Override - public MBR computeMBR(MBRConverter converter) { - MBR ret = null; - for (int i = 0, s = size(); i < s; i++) - ret = getUnion(ret, get(i).getMBR(converter)); - return ret; - } - - public void expand(MBR mbr, MBRConverter converter, List found, List> nodesToExpand) { - for (int i = 0, s = size(); i < s; i++) { - Node n = get(i); - if (mbr.intersects(n.getMBR(converter))) - nodesToExpand.add(n); - } - } - - public void find(MBR mbr, MBRConverter converter, List result) { - for (int i = 0, s = size(); i < s; i++) { - Node n = get(i); - if (mbr.intersects(n.getMBR(converter))) - n.find(mbr, converter, result); - } - } - - public void nnExpand(DistanceCalculator dc, NodeFilter filter, List> drs, int maxHits, - PriorityQueue> queue, MinDistComparator> mdc) { - int s = size(); - for (int i = 0; i < s; i++) { - Node n = get(i); - MBR mbr = n.getMBR(mdc.converter); - double minDist = MinDist.get(mbr, mdc.p); - int t = drs.size(); - // drs is sorted so we can check only the last entry - if (t < maxHits || minDist <= drs.get(t - 1).getDistance()) - queue.add(n); - } - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/InternalNodeComparators.java b/src/main/java/net/citizensnpcs/api/util/prtree/InternalNodeComparators.java deleted file mode 100644 index 8b01c672..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/InternalNodeComparators.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.Comparator; - -class InternalNodeComparators implements NodeComparators> { - private final MBRConverter converter; - - public InternalNodeComparators(MBRConverter converter) { - this.converter = converter; - } - - public Comparator> getMaxComparator(final int axis) { - return new Comparator>() { - public int compare(Node n1, Node n2) { - double d1 = n1.getMBR(converter).getMax(axis); - double d2 = n2.getMBR(converter).getMax(axis); - return Double.compare(d1, d2); - } - }; - } - - public Comparator> getMinComparator(final int axis) { - return new Comparator>() { - public int compare(Node n1, Node n2) { - double d1 = n1.getMBR(converter).getMin(axis); - double d2 = n2.getMBR(converter).getMin(axis); - return Double.compare(d1, d2); - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/LeafBuilder.java b/src/main/java/net/citizensnpcs/api/util/prtree/LeafBuilder.java deleted file mode 100644 index 6076d4e4..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/LeafBuilder.java +++ /dev/null @@ -1,189 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * A builder of internal nodes used during bulk loading of a PR-Tree. A PR-Tree is build by building a pseudo R-Tree and - * grabbing the leaf nodes (and then repeating until you have just one root node). This class creates the leaf nodes - * without building the full pseudo tree. - */ -class LeafBuilder { - - private final int branchFactor; - private final int dimensions; - - public LeafBuilder(int dimensions, int branchFactor) { - this.dimensions = dimensions; - this.branchFactor = branchFactor; - } - - private void addGetterAndSplitter(List> nodes, Comparator tcomp, - Circle> getters) { - Comparator> comp = new NodeUsageComparator(tcomp); - Collections.sort(nodes, comp); - List> sortedNodes = new ArrayList>(nodes); - getters.add(new Noder(sortedNodes)); - } - - public void buildLeafs(Collection ls, NodeComparators comparators, NodeFactory nf, - List leafNodes) { - List> nodes = new ArrayList>(ls.size()); - for (T t : ls) - nodes.add(new NodeUsage(t, 1)); - - Circle> getters = new Circle>(dimensions * 2); - - for (int i = 0; i < dimensions; i++) - addGetterAndSplitter(nodes, comparators.getMinComparator(i), getters); - - for (int i = 0; i < dimensions; i++) - addGetterAndSplitter(nodes, comparators.getMaxComparator(i), getters); - - getLeafs(1, ls.size(), getters, nf, leafNodes); - } - - private void getLeafs(int id, int totalNumberOfElements, Circle> getters, NodeFactory nf, - List leafNodes) { - List partitionsToExpand = new ArrayList(); - int[] pos = new int[2 * dimensions]; - partitionsToExpand.add(new Partition(id, totalNumberOfElements, pos)); - while (!partitionsToExpand.isEmpty()) { - Partition p = partitionsToExpand.remove(0); - // Get the extreme nodes - getters.reset(); - for (int i = 0; i < getters.getNumElements(); i++) { - int nodesToGet = Math.min(p.numElementsLeft, branchFactor); - if (nodesToGet == 0) - break; - Noder noder = getters.getNext(); - leafNodes.add(noder.getNextNode(p, i, nodesToGet, nf)); - p.numElementsLeft -= nodesToGet; - } - // Split the rest of the elements - if (p.numElementsLeft > 0) { - int splitPos = getSplitPos(p.id) % getters.getNumElements(); - Noder s = getters.get(splitPos); - s.split(p, splitPos, p.numElementsLeft, p.id, 2 * p.id, 2 * p.id + 1, partitionsToExpand); - } - } - } - - private int getSplitPos(int n) { - // id is generated by the power of twos so get biggest n where 2 ^ n < id - // id: 1 -> splitPos 0, id: 2 -> 1, 3 -> 1, 4 -> 2, 5 -> 2, 7 -> 2, 8 -> 3 - int splitPos = 0; - while (n >= 2) { - n >>= 1; - splitPos++; - } - return splitPos; - } - - private static class Noder { - private final List> data; - - private Noder(List> data) { - this.data = data; - } - - /** - * Get the next node. - * - * @param p - * the Partition to get a node from - * @param gi - * the current getter index - * @param maxObjects - * use at most this many objects - * @param nf - * the NodeFactory used to create the nodes - * @return the next node - */ - private N getNextNode(Partition p, int gi, int maxObjects, NodeFactory nf) { - - Object nodeData[] = new Object[maxObjects]; - int s = data.size(); - for (int i = 0; i < maxObjects; i++) { - while (p.currentPositions[gi] < s && isUsedNode(p, p.currentPositions[gi])) { - p.currentPositions[gi]++; - } - NodeUsage nu = data.set(p.currentPositions[gi], null); - nodeData[i] = nu.getData(); - nu.use(); - } - - for (int i = 0; i < nodeData.length; i++) { - if (nodeData[i] == null) { - for (int j = 0; j < data.size(); j++) - System.err.println(j + ": " + data.get(j)); - throw new NullPointerException("Null data found at: " + i); - } - } - return nf.create(nodeData); - } - - private boolean isUsedNode(Partition p, int pos) { - NodeUsage nu = data.get(pos); - return nu == null || nu.isUsed() || nu.getOwner() != p.id; - } - - private int markPart(int numToMark, int fromId, int toId, int startPos) { - NodeUsage nu; - while (numToMark > 0) { - while ((nu = data.get(startPos)) == null || nu.getOwner() != fromId) - startPos++; - nu.changeOwner(toId); - numToMark--; - } - return startPos; - } - - private void split(Partition p, int gi, int nodesToMark, int fromId, int toId1, int toId2, - List partitionsToExpand) { - int sizePart2 = nodesToMark / 2; - int sizePart1 = nodesToMark - sizePart2; - int startPos = p.currentPositions[gi]; - int startPos2 = markPart(sizePart1, fromId, toId1, startPos); - markPart(sizePart2, fromId, toId2, startPos2); - partitionsToExpand.add(0, new Partition(toId1, sizePart1, p.currentPositions)); - int[] pos = p.currentPositions.clone(); - pos[gi] = startPos2; - partitionsToExpand.add(1, new Partition(toId2, sizePart2, pos)); - } - } - - private static class NodeUsageComparator implements Comparator> { - private Comparator sorter; - - public NodeUsageComparator(Comparator sorter) { - this.sorter = sorter; - } - - public int compare(NodeUsage n1, NodeUsage n2) { - return sorter.compare(n1.getData(), n2.getData()); - } - } - - private static class Partition { - private int[] currentPositions; - private final int id; - private int numElementsLeft; - - public Partition(int id, int numElementsLeft, int[] currentPositions) { - this.id = id; - this.numElementsLeft = numElementsLeft; - this.currentPositions = currentPositions; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{id: " + id + ", numElementsLeft: " + numElementsLeft - + ", currentPositions: " + Arrays.toString(currentPositions) + "}"; - } - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/LeafNode.java b/src/main/java/net/citizensnpcs/api/util/prtree/LeafNode.java deleted file mode 100644 index 763d2815..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/LeafNode.java +++ /dev/null @@ -1,69 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.PriorityQueue; - -class LeafNode extends NodeBase { - - public LeafNode(Object[] data) { - super(data); - } - - private void add(List> drs, DistanceResult dr, int maxHits) { - int n = drs.size(); - if (n == maxHits) - drs.remove(n - 1); - int pos = Collections.binarySearch(drs, dr, comp); - if (pos < 0) { - // binarySearch return -(pos + 1) for new entries - pos = -(pos + 1); - } - drs.add(pos, dr); - } - - @Override - public MBR computeMBR(MBRConverter converter) { - MBR ret = null; - for (int i = 0, s = size(); i < s; i++) - ret = getUnion(ret, getMBR(get(i), converter)); - return ret; - } - - public void expand(MBR mbr, MBRConverter converter, List found, List> nodesToExpand) { - find(mbr, converter, found); - } - - public void find(MBR mbr, MBRConverter converter, List result) { - for (int i = 0, s = size(); i < s; i++) { - T t = get(i); - if (mbr.intersects(t, converter)) - result.add(t); - } - } - - public MBR getMBR(T t, MBRConverter converter) { - return new SimpleMBR(t, converter); - } - - public void nnExpand(DistanceCalculator dc, NodeFilter filter, List> drs, int maxHits, - PriorityQueue> queue, MinDistComparator> mdc) { - for (int i = 0, s = size(); i < s; i++) { - T t = get(i); - if (filter.accept(t)) { - double dist = dc.distanceTo(t, mdc.p); - int n = drs.size(); - if (n < maxHits || dist < drs.get(n - 1).getDistance()) { - add(drs, new DistanceResult(t, dist), maxHits); - } - } - } - } - - private static final Comparator> comp = new Comparator>() { - public int compare(DistanceResult d1, DistanceResult d2) { - return Double.compare(d1.getDistance(), d2.getDistance()); - } - }; -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/MBR.java b/src/main/java/net/citizensnpcs/api/util/prtree/MBR.java deleted file mode 100644 index d7f34dff..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/MBR.java +++ /dev/null @@ -1,60 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A minimum bounding box for n dimensions. - */ -public interface MBR { - /** - * @return the number of dimensions this bounding box has - */ - int getDimensions(); - - /** - * Get the maximum value for the given axis - * - * @param axis - * the axis to use - * @return the x max value - */ - double getMax(int axis); - - /** - * Get the minimum value for the given axis - * - * @param axis - * the axis to use - * @return the min value - */ - double getMin(int axis); - - /** - * Check if the other MBR intersects this one - * - * @param other - * the MBR to check against - * @return true if the given MBR intersects with this MBR - */ - boolean intersects(MBR other); - - /** - * Check if this MBR intersects the rectangle given by the object and the MBRConverter. - * - * @param t - * a rectangular object - * @param converter - * the MBRConverter - * @return true if the given MBR intersects with the given object - * @param - * the object type - */ - boolean intersects(T t, MBRConverter converter); - - /** - * Return a new MBR that is the union of this mbr and the other - * - * @param mbr - * the MBR to create a union with - * @return the new MBR - */ - MBR union(MBR mbr); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/MBR2D.java b/src/main/java/net/citizensnpcs/api/util/prtree/MBR2D.java deleted file mode 100644 index d0dddcfd..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/MBR2D.java +++ /dev/null @@ -1,52 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A minimum bounding rectangle - */ -public interface MBR2D { - /** - * Get the maximum x value - * - * @return the x max value - */ - double getMaxX(); - - /** - * Get the maximum y value - * - * @return the y max value - */ - double getMaxY(); - - /** - * Get the minimum x value - * - * @return the x min value - */ - double getMinX(); - - /** - * Get the minimum y value - * - * @return the y min value - */ - double getMinY(); - - /** - * Check if the other MBR intersects this one - * - * @param other - * the MBR to check against - * @return true if the given MBR intersects with this MBR - */ - boolean intersects(MBR2D other); - - /** - * Return a new MBR that is the union of this mbr and the other - * - * @param mbr - * the MBR to create a union with - * @return the new MBR - */ - MBR2D union(MBR2D mbr); -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/MBRConverter.java b/src/main/java/net/citizensnpcs/api/util/prtree/MBRConverter.java deleted file mode 100644 index 9679af46..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/MBRConverter.java +++ /dev/null @@ -1,36 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A class that given a T can tell the minimum and maximum ordinates for that object. - * - * @param - * the data type stored in the PRTree - */ -public interface MBRConverter { - /** - * @return the number of dimensions this converter cares about - */ - int getDimensions(); - - /** - * Get the maximum coordinate value for the given t - * - * @param axis - * the axis to get the max value for - * @param t - * the object to get the mbr ordinate for - * @return the max value for the given axis - */ - double getMax(int axis, T t); - - /** - * Get the minimum coordinate value for the given t. - * - * @param axis - * the axis to get the min value for - * @param t - * the object to get the mbr ordinate for - * @return the min value for the given axis - */ - double getMin(int axis, T t); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/MinDist.java b/src/main/java/net/citizensnpcs/api/util/prtree/MinDist.java deleted file mode 100644 index 26de6a74..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/MinDist.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * Class that can calculate the MINDIST between a point and a rectangle - */ -public class MinDist { - /** - * Do not instantiate - */ - private MinDist() { - // empty - } - - /** - * Calculate the MINDIST between the given MBRND and the given point - * - * @param mbr - * the bounding box to use - * @param p - * the point - * @return the squared distance - */ - public static double get(MBR mbr, PointND p) { - double res = 0; - for (int i = 0; i < p.getDimensions(); i++) { - double o = p.getOrd(i); - double rv = r(o, mbr.getMin(i), mbr.getMax(i)); - double dr = o - rv; - res += dr * dr; - } - return res; - } - - private static double r(double x, double min, double max) { - double r = x; - if (x < min) - r = min; - if (x > max) - r = max; - return r; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/MinDist2D.java b/src/main/java/net/citizensnpcs/api/util/prtree/MinDist2D.java deleted file mode 100644 index 12a4487c..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/MinDist2D.java +++ /dev/null @@ -1,47 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * Class that can calculate the MINDIST between a point and a rectangle - */ -public class MinDist2D { - /** - * Do not instantiate - */ - private MinDist2D() { - // empty - } - - /** - * Calculate the MINDIST between the given rectangle and the given point - * - * @param minx - * the rectangle minimum x point - * @param miny - * the rectangle minimum y point - * @param maxx - * the rectangle maximum x point - * @param maxy - * the rectangle maximum y point - * @param x - * the point - * @param y - * the point - * @return the squared distance - */ - public static double get(double minx, double miny, double maxx, double maxy, double x, double y) { - double rx = r(x, minx, maxx); - double ry = r(y, miny, maxy); - double xd = x - rx; - double yd = y - ry; - return xd * xd + yd * yd; - } - - private static double r(double x, double min, double max) { - double r = x; - if (x < min) - r = min; - if (x > max) - r = max; - return r; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/MinDistComparator.java b/src/main/java/net/citizensnpcs/api/util/prtree/MinDistComparator.java deleted file mode 100644 index 44e9b400..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/MinDistComparator.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.Comparator; - -/** - * A comparator that uses the MINDIST metrics to sort Nodes - * - * @param - * the data stored in the nodes - * @param - * the actual node - */ -class MinDistComparator> implements Comparator { - public final MBRConverter converter; - public final PointND p; - - public MinDistComparator(MBRConverter converter, PointND p) { - this.converter = converter; - this.p = p; - } - - public int compare(S t1, S t2) { - MBR mbr1 = t1.getMBR(converter); - MBR mbr2 = t2.getMBR(converter); - return Double.compare(MinDist.get(mbr1, p), MinDist.get(mbr2, p)); - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NearestNeighbour.java b/src/main/java/net/citizensnpcs/api/util/prtree/NearestNeighbour.java deleted file mode 100644 index 3f1dfd40..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NearestNeighbour.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.PriorityQueue; -import java.util.ArrayList; -import java.util.List; - -import net.citizensnpcs.api.util.prtree.DistanceResult; -import net.citizensnpcs.api.util.prtree.NodeFilter; - -class NearestNeighbour { - - private final MBRConverter converter; - private final DistanceCalculator dc; - private final NodeFilter filter; - private final int maxHits; - private final PointND p; - private final Node root; - - public NearestNeighbour(MBRConverter converter, NodeFilter filter, int maxHits, Node root, - DistanceCalculator dc, PointND p) { - this.converter = converter; - this.filter = filter; - this.maxHits = maxHits; - this.root = root; - this.dc = dc; - this.p = p; - } - - /** - * @return the nearest neighbour - */ - public List> find() { - List> ret = new ArrayList>(maxHits); - MinDistComparator> nc = new MinDistComparator>(converter, p); - PriorityQueue> queue = new PriorityQueue>(20, nc); - queue.add(root); - while (!queue.isEmpty()) { - Node n = queue.remove(); - n.nnExpand(dc, filter, ret, maxHits, queue, nc); - } - return ret; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/Node.java b/src/main/java/net/citizensnpcs/api/util/prtree/Node.java deleted file mode 100644 index 967542c2..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/Node.java +++ /dev/null @@ -1,74 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.List; -import java.util.PriorityQueue; - -/** - * A node in a Priority R-Tree - * - * @param - * the data type of the elements stored in this node - */ -interface Node { - /** - * Visit this node and add the leafs to the found list and add any child nodes to the list of nodes to expand. - * - * @param mbr - * the query rectangle - * @param converter - * the MBR converter to use for the actual objects - * @param found - * the List of leaf nodes - * @param nodesToExpand - * the List of nodes that needs to be expanded - */ - void expand(MBR mbr, MBRConverter converter, List found, List> nodesToExpand); - - /** - * Find nodes that intersect with the given MBR. - * - * @param mbr - * the query rectangle - * @param converter - * the MBR converter to use for the actual objects - * @param result - * the List to add the found nodes to - */ - void find(MBR mbr, MBRConverter converter, List result); - - /** - * Get the MBR of this node - * - * @param converter - * the MBR converter to use for the actual objects - * @return the MBR for this node - */ - MBR getMBR(MBRConverter converter); - - /** - * Expand the nearest neighbour search - * - * @param dc - * the DistanceCalculator to use when calculating distances - * @param filter - * the NodeFilter to use when getting node objects - * @param currentFinds - * the currently found results, this list will be populated - * @param maxHits - * the maximum number of entries to store - * @param queue - * where to store child nodes that needs expansion - * @param mdc - * the MinDistComparator to use - */ - void nnExpand(DistanceCalculator dc, NodeFilter filter, List> currentFinds, int maxHits, - PriorityQueue> queue, MinDistComparator> mdc); - - /** - * Get the size of the node, that is how many data elements it holds - * - * @return the number of elements (leafs or child nodes) that this node has - */ - int size(); - -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NodeBase.java b/src/main/java/net/citizensnpcs/api/util/prtree/NodeBase.java deleted file mode 100644 index 417adb0c..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NodeBase.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * @param - * the type of the child entries - * @param - * the type of the data entries - */ -abstract class NodeBase implements Node { - private Object[] data; - private MBR mbr; - - public NodeBase(Object[] data) { - this.data = data; - } - - public abstract MBR computeMBR(MBRConverter converter); - - @SuppressWarnings("unchecked") - public N get(int i) { - return (N) data[i]; - } - - public MBR getMBR(MBRConverter converter) { - if (mbr == null) - mbr = computeMBR(converter); - return mbr; - } - - public MBR getUnion(MBR m1, MBR m2) { - if (m1 == null) - return m2; - return m1.union(m2); - } - - public int size() { - return data.length; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NodeComparators.java b/src/main/java/net/citizensnpcs/api/util/prtree/NodeComparators.java deleted file mode 100644 index c93c4ee9..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NodeComparators.java +++ /dev/null @@ -1,23 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.Comparator; - -interface NodeComparators { - /** - * Get a comparator for the given axis - * - * @param axis - * the axis that the comparator will use - * @return the comparator - */ - Comparator getMaxComparator(int axis); - - /** - * Get a comparator for the given axis - * - * @param axis - * the axis that the comparator will use - * @return the comparator - */ - Comparator getMinComparator(int axis); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NodeFactory.java b/src/main/java/net/citizensnpcs/api/util/prtree/NodeFactory.java deleted file mode 100644 index 0bcf55c4..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NodeFactory.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A factory that creates the nodes (either leaf or internal). - * - * @param - * the data stored in the node - * @param - * the type of the node - */ -interface NodeFactory { - /** - * Create a new node - * - * @param data - * the data entries for the node, fully filled. - * @return the new node - */ - N create(Object[] data); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NodeFilter.java b/src/main/java/net/citizensnpcs/api/util/prtree/NodeFilter.java deleted file mode 100644 index 0a36c899..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NodeFilter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A node object filterer. - * - * @param - * the node type - */ -public interface NodeFilter { - /** - * Check if the given node object is accepted - * - * @param t - * the node user data - * @return True if the node is accepted, false otherwise - */ - boolean accept(T t); -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NodeGetter.java b/src/main/java/net/citizensnpcs/api/util/prtree/NodeGetter.java deleted file mode 100644 index b194a867..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NodeGetter.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.List; - -/** - * A class that can get the next available node. - * - * @param - * the type of the node - */ -interface NodeGetter { - /** - * Get the next node. - * - * @param maxObjects - * use at most this many objects - * @return the next node - */ - N getNextNode(int maxObjects); - - /** - * Check if there is unused data in this NodeGetter. - * - * @return true if there is unused data, false otherwise - */ - boolean hasMoreData(); - - /** - * Check if we can get more nodes from this NodeGetter. - * - * @return true if there are more nodes, false otherwise - */ - boolean hasMoreNodes(); - - /** - * Split this NodeGetter into the low and high parts. - * - * @param lowId - * the id of the low part - * @param highId - * the id of the high part - * @return a list with the two sublists - */ - List> split(int lowId, int highId); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/NodeUsage.java b/src/main/java/net/citizensnpcs/api/util/prtree/NodeUsage.java deleted file mode 100644 index 257f4244..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/NodeUsage.java +++ /dev/null @@ -1,46 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A node used while building the leaf sets, holds the actual data and an identifier for the partition this node belongs - * to. - * - * @param - * the data type of the stored item - */ -class NodeUsage { - private final T data; - private int id; - - public NodeUsage(T data, int id) { - this.data = data; - this.id = id; - } - - public void changeOwner(int id) { - this.id = id; - } - - public T getData() { - return data; - } - - public int getOwner() { - return id; - } - - public boolean isUsed() { - return id < 0; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{data: " + data + ", id: " + id + "}"; - } - - public void use() { - if (id >= 0) - id = -id; - else - throw new RuntimeException("Trying to use already used node"); - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/PRTree.java b/src/main/java/net/citizensnpcs/api/util/prtree/PRTree.java deleted file mode 100644 index 88550b4a..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/PRTree.java +++ /dev/null @@ -1,304 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -/** - * A Priority R-Tree, a spatial index, for N dimensions. This tree only supports bulk loading. - * - * @param - * the data type stored in the PRTree - */ -public class PRTree { - - private final int branchFactor; - private final MBRConverter converter; - - private int height; - private int numLeafs; - private Node root; - - /** - * Create a new PRTree using the specified branch factor. - * - * @param converter - * the MBRConverter to use for this tree - * @param branchFactor - * the number of child nodes for each internal node. - */ - public PRTree(MBRConverter converter, int branchFactor) { - this.converter = converter; - this.branchFactor = branchFactor; - } - - public void clear() { - root = null; - height = 0; - numLeafs = 0; - } - - private int estimateSize(int dataSize) { - return (int) (1.0 / (branchFactor - 1) * dataSize); - } - - /** - * Find all objects that intersect the given rectangle. Note, this find method will only use two dimensions, no - * matter how many dimensions the PRTree actually has. - * - * @param xmin - * the minimum value of the x coordinate when searching - * @param ymin - * the minimum value of the y coordinate when searching - * @param xmax - * the maximum value of the x coordinate when searching - * @param ymax - * the maximum value of the y coordinate when searching - * @return an iterable of the elements inside the query rectangle - * @throws IllegalArgumentException - * if xmin > xmax or ymin > ymax - */ - public Iterable find(double xmin, double ymin, double xmax, double ymax) { - return find(new SimpleMBR(xmin, xmax, ymin, ymax)); - } - - /** - * Finds all objects that intersect the given rectangle and stores the found node in the given list. Note, this find - * method will only use two dimensions, no matter how many dimensions the PRTree actually has. - * - * @param xmin - * the minimum value of the x coordinate when searching - * @param ymin - * the minimum value of the y coordinate when searching - * @param xmax - * the maximum value of the x coordinate when searching - * @param ymax - * the maximum value of the y coordinate when searching - * @param resultNodes - * the list that will be filled with the result - */ - public void find(double xmin, double ymin, double xmax, double ymax, List resultNodes) { - find(new SimpleMBR(xmin, xmax, ymin, ymax), resultNodes); - } - - /** - * Find all objects that intersect the given rectangle. - * - * @param query - * the bounds of the query - * @throws IllegalArgumentException - * if xmin > xmax or ymin > ymax - * @return an iterable of the elements inside the query rectangle - */ - public Iterable find(final MBR query) { - validateRect(query); - return new Iterable() { - @Override - public Iterator iterator() { - return new Finder(query); - } - }; - } - - /** - * Finds all objects that intersect the given rectangle and stores the found node in the given list. - * - * @param query - * the bounds of the query - * @param resultNodes - * the list that will be filled with the result - */ - public void find(MBR query, List resultNodes) { - validateRect(query); - root.find(query, converter, resultNodes); - } - - /** - * Get the height of this tree. - * - * @return the total height of this tree - */ - public int getHeight() { - return height; - } - - /** - * Get an N dimensional minimum bounding box of the data stored in this tree. - * - * @return the MBR of the whole PRTree - */ - public MBR getMBR() { - return root.getMBR(converter); - } - - /** - * Get a 2 dimensional minimum bounding rectangle of the data stored in this tree. - * - * @return the MBR of the whole PRTree - */ - public MBR2D getMBR2D() { - MBR mbr = getMBR(); - if (mbr == null) - return null; - return new SimpleMBR2D(mbr.getMin(0), mbr.getMin(1), mbr.getMax(0), mbr.getMax(1)); - } - - /** - * Get the number of data leafs in this tree. - * - * @return the total number of leafs in this tree - */ - public int getNumberOfLeaves() { - return numLeafs; - } - - /** - * Check if this tree is empty - * - * @return true if the number of leafs is 0, false otherwise - */ - public boolean isEmpty() { - return numLeafs == 0; - } - - /** - * Bulk load data into this tree. - * - * Create the leaf nodes that each hold (up to) branchFactor data entries. Then use the leaf nodes as data until we - * can fit all nodes into the root node. - * - * @param data - * the collection of data to store in the tree. - * @throws IllegalStateException - * if the tree is already loaded - */ - public void load(Collection data) { - if (root != null) - throw new IllegalStateException("Tree is already loaded"); - numLeafs = data.size(); - LeafBuilder lb = new LeafBuilder(converter.getDimensions(), branchFactor); - - List> leafNodes = new ArrayList>(estimateSize(numLeafs)); - lb.buildLeafs(data, new DataComparators(converter), new LeafNodeFactory(), leafNodes); - - height = 1; - List> nodes = leafNodes; - while (nodes.size() > branchFactor) { - height++; - List> internalNodes = new ArrayList>(estimateSize(nodes.size())); - lb.buildLeafs(nodes, new InternalNodeComparators(converter), new InternalNodeFactory(), internalNodes); - nodes = internalNodes; - } - setRoot(nodes); - } - - /** - * Get the nearest neighbour of the given point - * - * @param dc - * the DistanceCalculator to use. - * @param filter - * a NodeFilter that can be used to ignore some leaf nodes. - * @param maxHits - * the maximum number of entries to find. - * @param p - * the point to find the nearest neighbour to. - * @return A List of DistanceResult with up to maxHits results. Will return an empty list if this tree is empty. - */ - public List> nearestNeighbour(DistanceCalculator dc, NodeFilter filter, int maxHits, - PointND p) { - if (isEmpty()) - return Collections.emptyList(); - NearestNeighbour nn = new NearestNeighbour(converter, filter, maxHits, root, dc, p); - return nn.find(); - } - - private > void setRoot(List nodes) { - if (nodes.size() == 0) - root = new InternalNode(new Object[0]); - else if (nodes.size() == 1) { - root = nodes.get(0); - } else { - height++; - root = new InternalNode(nodes.toArray()); - } - } - - private void validateRect(MBR query) { - for (int i = 0; i < converter.getDimensions(); i++) { - double max = query.getMax(i); - double min = query.getMin(i); - if (max < min) - throw new IllegalArgumentException( - "max: " + max + " < min: " + min + ", axis: " + i + ", query: " + query); - } - } - - private class Finder implements Iterator { - private int dataNodesVisited = 0; - - private final MBR mbr; - private T next; - private final List> toVisit = new ArrayList>(); - - private final List ts = new ArrayList(); - private int visitedNodes = 0; - - public Finder(MBR mbr) { - this.mbr = mbr; - toVisit.add(root); - findNext(); - } - - private void findNext() { - while (ts.isEmpty() && !toVisit.isEmpty()) { - Node n = toVisit.remove(toVisit.size() - 1); - visitedNodes++; - n.expand(mbr, converter, ts, toVisit); - } - if (ts.isEmpty()) { - next = null; - } else { - next = ts.remove(ts.size() - 1); - dataNodesVisited++; - } - } - - @Override - public boolean hasNext() { - return next != null; - } - - @Override - public T next() { - T toReturn = next; - findNext(); - return toReturn; - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Not implemented"); - } - } - - private class InternalNodeFactory implements NodeFactory> { - @Override - public InternalNode create(Object[] data) { - return new InternalNode(data); - } - } - - private class LeafNodeFactory implements NodeFactory> { - @Override - public LeafNode create(Object[] data) { - return new LeafNode(data); - } - } - - public static PRTree create(MBRConverter converter, int branchFactor) { - return new PRTree(converter, branchFactor); - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/Point3D.java b/src/main/java/net/citizensnpcs/api/util/prtree/Point3D.java deleted file mode 100644 index 95cf16fd..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/Point3D.java +++ /dev/null @@ -1,97 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import org.bukkit.util.Vector; - -public class Point3D implements MBR { - private final Vector point; - - public Point3D(Vector point) { - this.point = point; - } - - @Override - public int getDimensions() { - return 3; - } - - @Override - public double getMax(int axis) { - switch (axis) { - case 0: - return point.getBlockX(); - case 1: - return point.getBlockY(); - case 2: - return point.getBlockZ(); - } - return 0; - } - - @Override - public double getMin(int axis) { - return getMax(axis); - } - - @Override - public boolean intersects(I t, MBRConverter converter) { - converter.getMin(getDimensions(), t); - return false; - } - - @Override - public boolean intersects(MBR other) { - if (other.getMax(0) < point.getBlockX() || other.getMax(1) < point.getBlockY() - || other.getMax(2) < point.getBlockZ()) - return false; - if (other.getMin(0) > point.getBlockX() || other.getMin(1) > point.getBlockY() - || other.getMin(2) > point.getBlockZ()) - return false; - return false; - } - - @Override - public MBR union(MBR mbr) { - Vector umin = new Vector((point.getX() + mbr.getMin(0)) / 2.0, (point.getY() + mbr.getMin(1)) / 2.0, - (point.getZ() + mbr.getMin(2) / 2.0)); - return new Point3D(umin); - } - - public static class Converter implements MBRConverter> { - @Override - public int getDimensions() { - return 3; - } - - @Override - public double getMax(int axis, Point3D t) { - return t.getMax(axis); - } - - @Override - public double getMin(int axis, Point3D t) { - return t.getMin(axis); - } - } - - public static NodeFilter> alwaysAcceptNodeFilter() { - return new NodeFilter>() { - @Override - public boolean accept(Point3D t) { - return true; - } - }; - } - - public static DistanceCalculator> distanceCalculator() { - return new DistanceCalculator>() { - @Override - public double distanceTo(Point3D t, PointND p) { - double x = p.getOrd(0); - double y = p.getOrd(1); - double z = p.getOrd(2); - return Math.sqrt(Math.pow(x - t.point.getX(), 2) + Math.pow(y - t.point.getY(), 2) - + Math.pow(z - t.point.getZ(), 2)); - } - }; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/PointND.java b/src/main/java/net/citizensnpcs/api/util/prtree/PointND.java deleted file mode 100644 index b981f049..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/PointND.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * A description of an N-dimensional point - */ -public interface PointND { - /** - * @return the number of dimensions this point has - */ - int getDimensions(); - - /** - * @param axis - * the axis to get the value for - * @return the ordinate value for the given axis - */ - double getOrd(int axis); -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/Region3D.java b/src/main/java/net/citizensnpcs/api/util/prtree/Region3D.java deleted file mode 100644 index af4f29c8..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/Region3D.java +++ /dev/null @@ -1,113 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -import org.bukkit.util.Vector; - -public class Region3D implements MBR { - private final T data; - private final Vector min, max; - - public Region3D(Vector min, Vector max, T data) { - this.min = min; - this.max = max; - this.data = data; - } - - public T getData() { - return data; - } - - @Override - public int getDimensions() { - return 3; - } - - @Override - public double getMax(int axis) { - switch (axis) { - case 0: - return max.getBlockX(); - case 1: - return max.getBlockY(); - case 2: - return max.getBlockZ(); - } - return 0; - } - - @Override - public double getMin(int axis) { - switch (axis) { - case 0: - return min.getBlockX(); - case 1: - return min.getBlockY(); - case 2: - return min.getBlockZ(); - } - return 0; - } - - @Override - public boolean intersects(I t, MBRConverter converter) { - converter.getMin(getDimensions(), t); - return false; - } - - @Override - public boolean intersects(MBR other) { - if (other.getMax(0) < min.getBlockX() || other.getMax(1) < min.getBlockY() || other.getMax(2) < min.getBlockZ()) - return false; - if (other.getMin(0) > max.getBlockX() || other.getMin(1) > max.getBlockY() || other.getMin(2) > max.getBlockZ()) - return false; - return false; - } - - @Override - public MBR union(MBR mbr) { - Vector umin = new Vector(Math.min(min.getX(), mbr.getMin(0)), Math.min(min.getY(), mbr.getMin(1)), - Math.min(min.getZ(), mbr.getMin(2))); - Vector umax = new Vector(Math.max(max.getX(), mbr.getMax(0)), Math.max(max.getY(), mbr.getMax(1)), - Math.max(max.getZ(), mbr.getMax(2))); - return new Region3D(umin, umax, data); - } - - public static class Converter implements MBRConverter> { - @Override - public int getDimensions() { - return 3; - } - - @Override - public double getMax(int axis, Region3D t) { - return t.getMax(axis); - } - - @Override - public double getMin(int axis, Region3D t) { - return t.getMin(axis); - } - } - - public static DistanceCalculator> distanceCalculator() { - return new DistanceCalculator>() { - @Override - public double distanceTo(Region3D t, PointND p) { - double x = p.getOrd(0); - double y = p.getOrd(1); - double z = p.getOrd(2); - return Math.sqrt(Math.pow(x - ((t.getMin(0) + t.getMax(0)) / 2), 2) - + Math.pow(y - ((t.getMin(1) + t.getMax(1)) / 2), 2) - + Math.pow(z - ((t.getMin(2) + t.getMax(2)) / 2), 2)); - } - }; - } - - public static NodeFilter> alwaysAcceptNodeFilter() { - return new NodeFilter>() { - @Override - public boolean accept(Region3D t) { - return true; - } - }; - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/SimpleMBR.java b/src/main/java/net/citizensnpcs/api/util/prtree/SimpleMBR.java deleted file mode 100644 index e0b274bd..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/SimpleMBR.java +++ /dev/null @@ -1,91 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * An implementation of MBRND that keeps a double array with the max and min values - * - *

- * Please note that you should not normally use this class when PRTree wants a MBR since this will actually use a lot of - * extra memory. - */ -public class SimpleMBR implements MBR { - private final double values[]; - - /** - * Create a new SimpleMBR using the given double values for max and min. Note that the values should be stored as - * min, max, min, max. - * - * @param values - * the min and max values for each dimension. - */ - public SimpleMBR(double... values) { - this.values = values.clone(); - } - - private SimpleMBR(int dimensions) { - values = new double[dimensions * 2]; - } - - /** - * Create a new SimpleMBR from a given object and a MBRConverter - * - * @param t - * the object to create the bounding box for - * @param converter - * the actual MBRConverter to use - */ - public SimpleMBR(T t, MBRConverter converter) { - int dims = converter.getDimensions(); - values = new double[dims * 2]; - int p = 0; - for (int i = 0; i < dims; i++) { - values[p++] = converter.getMin(i, t); - values[p++] = converter.getMax(i, t); - } - } - - public int getDimensions() { - return values.length / 2; - } - - public double getMax(int axis) { - return values[axis * 2 + 1]; - } - - public double getMin(int axis) { - return values[axis * 2]; - } - - public boolean intersects(MBR other) { - for (int i = 0; i < getDimensions(); i++) { - if (other.getMax(i) < getMin(i) || other.getMin(i) > getMax(i)) - return false; - } - return true; - } - - public boolean intersects(T t, MBRConverter converter) { - for (int i = 0; i < getDimensions(); i++) { - if (converter.getMax(i, t) < getMin(i) || converter.getMin(i, t) > getMax(i)) - return false; - } - return true; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{values: " + java.util.Arrays.toString(values) + "}"; - } - - public MBR union(MBR mbr) { - int dims = getDimensions(); - SimpleMBR n = new SimpleMBR(dims); - int p = 0; - for (int i = 0; i < dims; i++) { - n.values[p] = Math.min(getMin(i), mbr.getMin(i)); - p++; - n.values[p] = Math.max(getMax(i), mbr.getMax(i)); - p++; - } - return n; - } -} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/SimpleMBR2D.java b/src/main/java/net/citizensnpcs/api/util/prtree/SimpleMBR2D.java deleted file mode 100644 index 2f784e90..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/SimpleMBR2D.java +++ /dev/null @@ -1,71 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * An implementation of MBR that keeps 4 double values for the actual min and max values needed. - * - *

- * Please note that you should not normally use this class when PRTree wants a MBR since this will actually use a lot of - * extra memory. - */ -public class SimpleMBR2D implements MBR2D { - private final double xmax; - private final double xmin; - private final double ymax; - private final double ymin; - - /** - * Create a 2D minimum bounding box - * - * @param xmin - * the xmin of the MBR - * @param ymin - * the ymin of the MBR - * @param xmax - * the xmax of the MBR - * @param ymax - * the ymax of the MBR - */ - public SimpleMBR2D(double xmin, double ymin, double xmax, double ymax) { - this.xmin = xmin; - this.ymin = ymin; - this.xmax = xmax; - this.ymax = ymax; - } - - public double getMaxX() { - return xmax; - } - - public double getMaxY() { - return ymax; - } - - public double getMinX() { - return xmin; - } - - public double getMinY() { - return ymin; - } - - public boolean intersects(MBR2D other) { - return !(other.getMaxX() < xmin || other.getMinX() > xmax || other.getMaxY() < ymin || other.getMinY() > ymax); - } - - /** - * Get a string representation of this mbr. - */ - @Override - public String toString() { - return getClass().getSimpleName() + "{xmin: " + xmin + ", ymin: " + ymin + ", xmax: " + xmax + ", ymax: " + ymax - + "}"; - } - - public MBR2D union(MBR2D other) { - double uxmin = Math.min(xmin, other.getMinX()); - double uymin = Math.min(ymin, other.getMinY()); - double uxmax = Math.max(xmax, other.getMaxX()); - double uymax = Math.max(ymax, other.getMaxY()); - return new SimpleMBR2D(uxmin, uymin, uxmax, uymax); - } -} diff --git a/src/main/java/net/citizensnpcs/api/util/prtree/SimplePointND.java b/src/main/java/net/citizensnpcs/api/util/prtree/SimplePointND.java deleted file mode 100644 index f1b7f0aa..00000000 --- a/src/main/java/net/citizensnpcs/api/util/prtree/SimplePointND.java +++ /dev/null @@ -1,26 +0,0 @@ -package net.citizensnpcs.api.util.prtree; - -/** - * One implementatoin of a point - */ -public class SimplePointND implements PointND { - private final double[] ords; - - /** - * Create a new SimplePointND using the given ordinates. - * - * @param ords - * the ordinates - */ - public SimplePointND(double... ords) { - this.ords = ords; - } - - public int getDimensions() { - return ords.length; - } - - public double getOrd(int axis) { - return ords[axis]; - } -}