Advertisement
TildePD

Master KDTree

Apr 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package bfst19.osmdrawing;
  2.  
  3. import java.util.*;
  4.  
  5. public class KDTree {
  6. Node root;
  7. int MAX_SIZE = 10;
  8. Set<Drawable> foundDrawables = new HashSet<>();
  9.  
  10. public KDTree(List<Drawable> listOfDrawables) {
  11. buildKDTree(listOfDrawables);
  12. }
  13.  
  14. private void buildKDTree(List<Drawable> listOfDrawables) {
  15. if (root == null) {
  16. listOfDrawables.sort(Comparator.comparing(Drawable::getMaxX));
  17. float max = listOfDrawables.get(listOfDrawables.size() / 2).getMaxX();
  18. float min = Float.MAX_VALUE;
  19. for (Drawable d : listOfDrawables.subList(listOfDrawables.size() / 2, listOfDrawables.size())) {
  20. if (d.getMinX() < min) {
  21. min = d.getMinX();
  22. }
  23. }
  24. root = new Node(min, max);
  25. }
  26.  
  27. root.left = buildKDTree(listOfDrawables.subList(0,listOfDrawables.size() / 2), 1);
  28. root.right = buildKDTree(listOfDrawables.subList(listOfDrawables.size() / 2, listOfDrawables.size()), 1);
  29. }
  30.  
  31. private Node buildKDTree(List<Drawable> drawableList, int depth) {
  32. float min = Float.MAX_VALUE;
  33. float max;
  34.  
  35. if (depth % 2 == 0) {
  36. drawableList.sort(Comparator.comparing(Drawable::getMaxX));
  37. } else {
  38. drawableList.sort(Comparator.comparing(Drawable::getMaxY));
  39. }
  40.  
  41. if (drawableList.size() <= MAX_SIZE) {
  42. if (depth % 2 == 0) {
  43. max = drawableList.get(drawableList.size() - 1).getMaxX();
  44. //min = Float.MAX_VALUE;
  45. for (Drawable d : drawableList) {
  46. if (d.getMinX() < min) {
  47. min = d.getMinX();
  48. }
  49. }
  50. } else {
  51. max = drawableList.get(drawableList.size() - 1).getMaxY();
  52. //min = Float.MAX_VALUE;
  53. for (Drawable d : drawableList) {
  54. if (d.getMinY() < min) {
  55. min = d.getMinY();
  56. }
  57. }
  58. }
  59. if (min == Float.MAX_VALUE) {
  60. System.out.println("MAX VALUE");
  61. }
  62. Node leaf = new Node(min, max);
  63. leaf.addLines(drawableList);
  64. return leaf;
  65. }
  66.  
  67.  
  68. Node node;
  69. if (depth % 2 == 0) {
  70. max = drawableList.get(drawableList.size() / 2).getMaxX();
  71. for (Drawable d : drawableList) {
  72. if (d.getMinX() < min) {
  73. min = d.getMinX();
  74. }
  75. }
  76. node = new Node(min, max);
  77. } else {
  78. max = drawableList.get(drawableList.size() / 2).getMaxY();
  79. for (Drawable d : drawableList) {
  80. if (d.getMinY() < min) {
  81. min = d.getMinY();
  82. }
  83. }
  84. node = new Node(min, max);
  85. }
  86.  
  87. depth++;
  88. node.left = buildKDTree(drawableList.subList(0, drawableList.size() / 2), depth);
  89. node.right = buildKDTree(drawableList.subList((drawableList.size() / 2), drawableList.size()), depth);
  90. return node;
  91. }
  92.  
  93. public void rangeSearch(float minX, float minY, float maxX, float maxY) {
  94. foundDrawables.clear();
  95. rangeSearch(root, minX, minY, maxX, maxY, 0);
  96. }
  97.  
  98. private void rangeSearch(Node node, float minX, float minY, float maxX, float maxY, int depth) {
  99. if (node == null) {
  100. return;
  101. }
  102.  
  103. if (node.isLeaf) {
  104. foundDrawables.addAll(node.lines);
  105. return;
  106. }
  107.  
  108. if (depth % 2 == 0) {
  109. depth++;
  110. if (node.min < maxX) {
  111. rangeSearch(node.right, minX, minY, maxX, maxY, depth);
  112. }
  113.  
  114. if (node.max > minX) {
  115. rangeSearch(node.left, minX, minY, maxX, maxY, depth);
  116. }
  117. } else {
  118. depth++;
  119. if (node.min < maxY) {
  120. rangeSearch(node.right, minX, minY, maxX, maxY, depth);
  121. }
  122.  
  123. if (node.max > minY) {
  124. rangeSearch(node.left, minX, minY, maxX, maxY, depth);
  125. }
  126. }
  127. }
  128.  
  129. private class Node {
  130. float min, max;
  131. Node left, right;
  132. boolean isLeaf;
  133. Set<Drawable> lines;
  134.  
  135. public Node(float min, float max) {
  136. this.min = min;
  137. this.max = max;
  138. }
  139.  
  140. public void addLines(List<Drawable> list) {
  141. lines = new HashSet<>();
  142. isLeaf = true;
  143. lines.addAll(list);
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement