private void split() { double centerX = (minX + maxX) / 2; double centerY = (minY + maxY) / 2; this.children = new QuadTreeNode[4]; this.children[ChildPosition.CHILD_NW.ordinal()] = new QuadTreeNode(this.minX, centerY, centerX, this.maxY); this.children[ChildPosition.CHILD_NE.ordinal()] = new QuadTreeNode(centerX, centerY, this.maxX, this.maxY); this.children[ChildPosition.CHILD_SE.ordinal()] = new QuadTreeNode(centerX, this.minY, this.maxX, centerY); this.children[ChildPosition.CHILD_SW.ordinal()] = new QuadTreeNode(this.minX, this.minY, centerX, centerY); List<LinkWrapper> keep = new ArrayList<>(this.links.size() / 2); for (LinkWrapper w : this.links) { ChildPosition pos = getChildPosition(w); if (pos == ChildPosition.NO_CHILD) { keep.add(w); } else { this.children[pos.ordinal()].put(w); // (seems to me that this cannot happen to more than one link since the quad tree node will split // as soon as a second link is added that can be assigned to a child node. kai, jul'12) // (hä? kai, aug'16) } } this.links.clear(); this.links.ensureCapacity(keep.size() + 5); this.links.addAll(keep); }
public boolean remove(final LinkWrapper w) { ChildPosition pos = getChildPosition(w); if (pos == ChildPosition.NO_CHILD || this.children == null) { for (int i = 0, n = this.links.size(); i < n; i++) { LinkWrapper w2 = this.links.get(i); if (w2.link.equals(w.link)) { this.links.remove(i); return true; } } } else { return this.children[pos.ordinal()].remove(w); } return false; }
public void put(final LinkWrapper w) { if (this.children == null && this.links.isEmpty()) { // (means quadtree node neither has children nor contains a link yet) this.links.add(w); // (node now contains this link) } else { ChildPosition pos = getChildPosition(w); if (pos == ChildPosition.NO_CHILD) { // (i.e. link extends over more than one child node, so the current node is the smallest one that fully contains the link. this.links.add(w); } else { if (this.children == null) { split(); } this.children[pos.ordinal()].put(w); } } }
ChildPosition childPos = this.getChildPosition(x, y); LinkWrapper tmp = this.children[childPos.ordinal()].getNearest(x, y, bestDistanceIndicator); if (tmp != null) { closest = tmp; if (child.calcDistanceIndicator(x, y) < bestDistanceIndicator.value) { tmp = child.getNearest(x, y, bestDistanceIndicator); if (tmp != null) { closest = tmp;
public void put(final Link link) { this.top.put(new LinkWrapper(link)); }
public void remove(final Link link) { this.top.remove(new LinkWrapper(link)); }
public LinkQuadTree(final double minX, final double minY, final double maxX, final double maxY) { this.top = new QuadTreeNode(minX, minY, maxX, maxY); }
public Link getNearest(final double x, final double y) { LinkWrapper w = this.top.getNearest(x, y, new MutableDouble(Double.POSITIVE_INFINITY)); if (w == null) { return null; } return w.link; }