Debian Codegraph @

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

raw symbols SCIP
1 package uk.me.parabola.util;
2
3 import java.awt.Rectangle;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8
9 import uk.me.parabola.imgfmt.app.Area;
10 import uk.me.parabola.imgfmt.app.Coord;
11
12 public class QuadTreeNode {
13
14 private static final int MAX_POINTS = 20;
15
16 private Collection<Coord> points;
17 private final Area bounds;
18 private Area coveredBounds;
19
20 public Area getCoveredBounds() {
21 return coveredBounds;
22 }
23
24 private QuadTreeNode[] children;
25
26 public static final class QuadTreePolygon {
27 private final java.awt.geom.Area javaArea;
28 private final Area bbox;
29
30 public QuadTreePolygon(java.awt.geom.Area javaArea) {
31 this.javaArea = javaArea;
32 Rectangle bboxRect = javaArea.getBounds();
33 bbox = new Area(bboxRect.y, bboxRect.x, bboxRect.y
34 + bboxRect.height, bboxRect.x + bboxRect.width);
35 }
36
37 public QuadTreePolygon(List<Coord> points) {
38 this(Java2DConverter.createArea(points));
39 }
40
41 public QuadTreePolygon(Collection<List<Coord>> polygonList) {
42 this.javaArea = new java.awt.geom.Area();
43 for (List<Coord> polygon : polygonList) {
44 javaArea.add(Java2DConverter.createArea(polygon));
45 }
46 Rectangle bboxRect = javaArea.getBounds();
47 bbox = new Area(bboxRect.y, bboxRect.x, bboxRect.y
48 + bboxRect.height, bboxRect.x + bboxRect.width);
49 }
50
51 public Area getBbox() {
52 return bbox;
53 }
54
55 public java.awt.geom.Area getArea() {
56 return javaArea;
57 }
58 }
59
60 public QuadTreeNode(Area bounds) {
61 this(bounds, Collections.<Coord>emptyList());
62 }
63
64 public QuadTreeNode(Area bounds, Collection<Coord> points) {
65 this.bounds = bounds;
66 this.children = null;
67
68 int minLat = Integer.MAX_VALUE;
69 int maxLat = Integer.MIN_VALUE;
70 int minLong = Integer.MAX_VALUE;
71 int maxLong = Integer.MIN_VALUE;
72 for (Coord c : points) {
73 if (c.getLatitude() < minLat) {
74 minLat = c.getLatitude();
75 }
76 if (c.getLatitude() > maxLat) {
77 maxLat = c.getLatitude();
78 }
79 if (c.getLongitude() < minLong) {
80 minLong = c.getLongitude();
81 }
82 if (c.getLongitude() > maxLong) {
83 maxLong = c.getLongitude();
84 }
85 }
86 coveredBounds = new Area(minLat, minLong, maxLat, maxLong);
87
88 if (points.size() > MAX_POINTS) {
89 this.points = points;
90 split();
91 } else {
92 this.points = new HashSet<>(points);
93 }
94 }
95
96 public Area getBounds() {
97 return this.bounds;
98 }
99
100 public boolean add(Coord c) {
101 if (coveredBounds == null) {
102 coveredBounds = new Area(c.getLatitude(), c.getLongitude(),
103 c.getLatitude(), c.getLongitude());
104 } else if (!coveredBounds.contains(c)) {
105 coveredBounds = new Area(Math.min(coveredBounds.getMinLat(),
106 c.getLatitude()), Math.min(coveredBounds.getMinLong(),
107 c.getLongitude()), Math.max(coveredBounds.getMaxLat(),
108 c.getLatitude()), Math.max(coveredBounds.getMaxLong(),
109 c.getLongitude()));
110 }
111 if (isLeaf()) {
112 boolean added = points.add(c);
113 if (points.size() > MAX_POINTS)
114 split();
115 return added;
116 } else {
117 for (QuadTreeNode nodes : children) {
118 if (nodes.getBounds().contains(c)) {
119 return nodes.add(c);
120 }
121 }
122 return false;
123 }
124 }
125
126 public List<Coord> get(Area bbox, List<Coord> resultList) {
127 if (isLeaf()) {
128 if (bbox.contains(coveredBounds)) {
129 // the bounding box is contained completely in the bbox
130 // => add all points without further check
131 resultList.addAll(points);
132 } else {
133 // check each point
134 for (Coord c : points) {
135 if (bbox.contains(c)) {
136 resultList.add(c);
137 }
138 }
139 }
140 } else {
141 for (QuadTreeNode child : children) {
142 if (bbox.intersects(child.getCoveredBounds())) {
143 resultList = child.get(bbox, resultList);
144 }
145 }
146 }
147 return resultList;
148 }
149
150 public List<Coord> get(QuadTreePolygon polygon, List<Coord> resultList) {
151 if (polygon.getBbox().intersects(getBounds())) {
152 if (isLeaf()) {
153 for (Coord c : points) {
154 if (polygon.getArea().contains(c.getLongitude(),
155 c.getLatitude())) {
156 resultList.add(c);
157 }
158 }
159 } else {
160 for (QuadTreeNode child : children) {
161 if (polygon.getBbox().intersects(child.getBounds())) {
162 java.awt.geom.Area subArea = (java.awt.geom.Area) polygon
163 .getArea().clone();
164 subArea.intersect(createArea(child.getBounds()));
165 child.get(new QuadTreePolygon(subArea), resultList);
166 }
167 }
168 }
169 }
170 return resultList;
171
172 }
173
174 private static java.awt.geom.Area createArea(Area bbox) {
175 return new java.awt.geom.Area(
176 new Rectangle(bbox.getMinLong(), bbox.getMinLat(), bbox.getWidth(), bbox.getHeight()));
177 }
178
179 public boolean isLeaf() {
180 return points != null;
181 }
182
183 private void split() {
184 if (bounds.getHeight() <= 1 || bounds.getWidth() <= 1) {
185 return;
186 }
187
188 int halfLat = (bounds.getMinLat() + bounds.getMaxLat()) / 2;
189 int halfLong = (bounds.getMinLong() + bounds.getMaxLong()) / 2;
190 children = new QuadTreeNode[4];
191
192 Area swBounds = new Area(bounds.getMinLat(), bounds.getMinLong(),
193 halfLat, halfLong);
194 Area nwBounds = new Area(halfLat, bounds.getMinLong(),
195 bounds.getMaxLat(), halfLong);
196 Area seBounds = new Area(bounds.getMinLat(), halfLong, halfLat,
197 bounds.getMaxLong());
198 Area neBounds = new Area(halfLat, halfLong, bounds.getMaxLat(),
199 bounds.getMaxLong());
200
201 children[0] = new QuadTreeNode(swBounds);
202 children[1] = new QuadTreeNode(nwBounds);
203 children[2] = new QuadTreeNode(seBounds);
204 children[3] = new QuadTreeNode(neBounds);
205
206 Collection<Coord> copyPoints = points;
207 points = null;
208 for (Coord c : copyPoints) {
209 add(c);
210 }
211 }
212
213 public void clear() {
214 this.children = null;
215 points = new HashSet<>();
216 coveredBounds = new Area(Integer.MAX_VALUE, Integer.MAX_VALUE,
217 Integer.MIN_VALUE, Integer.MIN_VALUE);
218 }
219 }