Debian Codegraph @

src/uk/me/parabola/util/QuadTree.java

raw symbols SCIP
1 package uk.me.parabola.util;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.ListIterator;
9
10 import uk.me.parabola.imgfmt.app.Area;
11 import uk.me.parabola.imgfmt.app.Coord;
12 import uk.me.parabola.util.QuadTreeNode.QuadTreePolygon;
13
14 public class QuadTree {
15
16 private final QuadTreeNode root;
17 private long itemCount;
18
19 public QuadTree(Area bbox) {
20 this.root = new QuadTreeNode(bbox);
21 this.itemCount = 0;
22 }
23
24 public boolean addAll(Collection<Coord> coordList) {
25 long oldCount = itemCount;
26 for (Coord c : coordList) {
27 add(c);
28 }
29 return itemCount > oldCount;
30 }
31
32 public boolean add(Coord c) {
33
34 boolean added = root.add(c);
35 if (added) {
36 itemCount++;
37 }
38 return added;
39 }
40
41 public List<Coord> get(Area bbox) {
42 return root.get(bbox, new ArrayList<>(2000));
43 }
44
45 public List<Coord> get(Collection<List<Coord>> polygons) {
46 return root.get(new QuadTreePolygon(polygons), new ArrayList<>(2000));
47 }
48
49 public List<Coord> get(List<Coord> polygon) {
50 return get(polygon, 0);
51 }
52
53 public List<Coord> get(List<Coord> polygon, int offset) {
54 if (polygon.size() < 3) {
55 return Collections.emptyList();
56 }
57 if (!polygon.get(0).equals(polygon.get(polygon.size() - 1))) {
58 throw new IllegalArgumentException("polygon is not closed");
59 }
60 List<Coord> points = root.get(new QuadTreePolygon(polygon), new ArrayList<>(2000));
61 if (offset > 0) {
62 ListIterator<Coord> pointIter = points.listIterator();
63 while (pointIter.hasNext()) {
64 if (isCloseToPolygon(pointIter.next(), polygon, offset)) {
65 pointIter.remove();
66 }
67 }
68 }
69 return points;
70 }
71
72 public void clear() {
73 itemCount = 0;
74 root.clear();
75 }
76
77 public long getSize() {
78 return itemCount;
79 }
80
81 private static boolean isCloseToPolygon(Coord point, List<Coord> polygon, int gap) {
82 Iterator<Coord> polyIter = polygon.iterator();
83 Coord c2 = polyIter.next();
84 while (polyIter.hasNext()) {
85 Coord c1 = c2;
86 c2 = polyIter.next();
87 double dist = point.shortestDistToLineSegment(c1, c2);
88 if (dist <= gap) {
89 return true;
90 }
91 }
92 return false;
93 }
94 }