Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bfst19.osmdrawing;
- import java.util.*;
- public class KDTree {
- Node root;
- int MAX_SIZE = 10;
- Set<Drawable> foundDrawables = new HashSet<>();
- public KDTree(List<Drawable> listOfDrawables) {
- buildKDTree(listOfDrawables);
- }
- private void buildKDTree(List<Drawable> listOfDrawables) {
- if (root == null) {
- listOfDrawables.sort(Comparator.comparing(Drawable::getMaxX));
- float max = listOfDrawables.get(listOfDrawables.size() / 2).getMaxX();
- float min = Float.MAX_VALUE;
- for (Drawable d : listOfDrawables.subList(listOfDrawables.size() / 2, listOfDrawables.size())) {
- if (d.getMinX() < min) {
- min = d.getMinX();
- }
- }
- root = new Node(min, max);
- }
- root.left = buildKDTree(listOfDrawables.subList(0,listOfDrawables.size() / 2), 1);
- root.right = buildKDTree(listOfDrawables.subList(listOfDrawables.size() / 2, listOfDrawables.size()), 1);
- }
- private Node buildKDTree(List<Drawable> drawableList, int depth) {
- float min = Float.MAX_VALUE;
- float max;
- if (depth % 2 == 0) {
- drawableList.sort(Comparator.comparing(Drawable::getMaxX));
- } else {
- drawableList.sort(Comparator.comparing(Drawable::getMaxY));
- }
- if (drawableList.size() <= MAX_SIZE) {
- if (depth % 2 == 0) {
- max = drawableList.get(drawableList.size() - 1).getMaxX();
- //min = Float.MAX_VALUE;
- for (Drawable d : drawableList) {
- if (d.getMinX() < min) {
- min = d.getMinX();
- }
- }
- } else {
- max = drawableList.get(drawableList.size() - 1).getMaxY();
- //min = Float.MAX_VALUE;
- for (Drawable d : drawableList) {
- if (d.getMinY() < min) {
- min = d.getMinY();
- }
- }
- }
- if (min == Float.MAX_VALUE) {
- System.out.println("MAX VALUE");
- }
- Node leaf = new Node(min, max);
- leaf.addLines(drawableList);
- return leaf;
- }
- Node node;
- if (depth % 2 == 0) {
- max = drawableList.get(drawableList.size() / 2).getMaxX();
- for (Drawable d : drawableList) {
- if (d.getMinX() < min) {
- min = d.getMinX();
- }
- }
- node = new Node(min, max);
- } else {
- max = drawableList.get(drawableList.size() / 2).getMaxY();
- for (Drawable d : drawableList) {
- if (d.getMinY() < min) {
- min = d.getMinY();
- }
- }
- node = new Node(min, max);
- }
- depth++;
- node.left = buildKDTree(drawableList.subList(0, drawableList.size() / 2), depth);
- node.right = buildKDTree(drawableList.subList((drawableList.size() / 2), drawableList.size()), depth);
- return node;
- }
- public void rangeSearch(float minX, float minY, float maxX, float maxY) {
- foundDrawables.clear();
- rangeSearch(root, minX, minY, maxX, maxY, 0);
- }
- private void rangeSearch(Node node, float minX, float minY, float maxX, float maxY, int depth) {
- if (node == null) {
- return;
- }
- if (node.isLeaf) {
- foundDrawables.addAll(node.lines);
- return;
- }
- if (depth % 2 == 0) {
- depth++;
- if (node.min < maxX) {
- rangeSearch(node.right, minX, minY, maxX, maxY, depth);
- }
- if (node.max > minX) {
- rangeSearch(node.left, minX, minY, maxX, maxY, depth);
- }
- } else {
- depth++;
- if (node.min < maxY) {
- rangeSearch(node.right, minX, minY, maxX, maxY, depth);
- }
- if (node.max > minY) {
- rangeSearch(node.left, minX, minY, maxX, maxY, depth);
- }
- }
- }
- private class Node {
- float min, max;
- Node left, right;
- boolean isLeaf;
- Set<Drawable> lines;
- public Node(float min, float max) {
- this.min = min;
- this.max = max;
- }
- public void addLines(List<Drawable> list) {
- lines = new HashSet<>();
- isLeaf = true;
- lines.addAll(list);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement