Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2006, 2012.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.util;
14
15 import java.awt.Rectangle;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Set;
22
23 import uk.me.parabola.imgfmt.app.Area;
24 import uk.me.parabola.log.Logger;
25 import uk.me.parabola.mkgmap.reader.osm.Element;
26 import uk.me.parabola.mkgmap.reader.osm.Node;
27 import uk.me.parabola.mkgmap.reader.osm.Way;
28
29 /**
30 * A quad tree node. All nodes in the tree can contain elements. The element is stored in the node when its bbox is too large to fit into a single child.
31 * @author Gerd Petermann
32 *
33 */
34 public final class ElementQuadTreeNode {
35
36 private static final Logger log = Logger.getLogger(ElementQuadTreeNode.class);
37
38 /** The maximum number of coords in the quadtree node. */
39 private static final int MAX_POINTS = 1000;
40
41 private List<BoxedElement> elementList; // can be null
42
43 /** The bounds of this quadtree node */
44 private final Area bounds;
45 private final Rectangle boundsRect;
46
47 /** Flag if this node and all sub-nodes are empty */
48 private Boolean empty;
49
50 /** The sub-nodes in case this node is not a leaf */
51 private ElementQuadTreeNode[] children;
52
53 /**
54 * Retrieves if this quadtree node (and all sub-nodes) contains any elements.
55 * @return <code>true</code> this quadtree node does not contain any elements; <code>false</code> else
56 */
57 public boolean isEmpty() {
58 if (empty == null) {
59 if (elementList != null && !elementList.isEmpty()) {
60 empty = false;
61 } else {
62 empty = true;
63 for (ElementQuadTreeNode child : children) {
64 if (!child.isEmpty()) {
65 empty = false;
66 break;
67 }
68 }
69 }
70 }
71 return empty;
72 }
73
74
75 /**
76 * Retrieves the number of coords hold by this quadtree node and all sub-nodes.
77 * @return the number of coords
78 */
79 private long getSize() {
80 int items = 0;
81 for (BoxedElement bel : elementList) {
82 items += bel.getSize();
83 }
84 if (children != null) {
85 for (ElementQuadTreeNode child : children) {
86 items += child.getSize();
87 }
88 }
89 return items;
90 }
91
92 /**
93 * Retrieves the depth of this quadtree node. Leaves have depth 1.
94 * @return the depth of this quadtree node
95 */
96 public int getDepth() {
97 if (children == null)
98 return 1;
99 int maxDepth = 0;
100 for (ElementQuadTreeNode node : children) {
101 maxDepth = Math.max(node.getDepth(), maxDepth);
102 }
103 return maxDepth + 1;
104 }
105
106 private ElementQuadTreeNode(Area bounds, List<BoxedElement> elements) {
107 this.bounds = bounds;
108 boundsRect = new Rectangle(bounds.getMinLong(), bounds.getMinLat(),
109 bounds.getWidth(), bounds.getHeight());
110 this.children = null;
111 elementList = elements;
112 empty = elementList.isEmpty();
113
114 checkSplit();
115 }
116
117
118 public ElementQuadTreeNode(Area bounds, Collection<Element> elements) {
119 this.bounds = bounds;
120 boundsRect = new Rectangle(bounds.getMinLong(), bounds.getMinLat(),
121 bounds.getWidth(), bounds.getHeight());
122 this.children = null;
123
124 this.elementList = new ArrayList<>();
125
126 for (Element el : elements) {
127 BoxedElement bel = new BoxedElement(el);
128 elementList.add(bel);
129 }
130 empty = elementList.isEmpty();
131 checkSplit();
132 }
133
134 public Area getBounds() {
135 return this.bounds;
136 }
137
138 public Rectangle getBoundsAsRectangle() {
139 return boundsRect;
140 }
141
142 /**
143 * Checks if this quadtree node exceeds the maximum size and splits it in such a case.
144 */
145 private void checkSplit() {
146 if (elementList != null && elementList.size() > 1 && getSize() > MAX_POINTS) {
147 split();
148 }
149 }
150
151 /**
152 * Retrieves all elements that intersects the given bounding box.
153 * @param bbox the bounding box
154 * @param resultSet results are stored in this collection
155 * @return the resultList
156 */
157 public void get(Area bbox, Set<Element> resultSet) {
158 if (isEmpty() || !bbox.intersects(bounds)) {
159 return;
160 }
161 if (elementList != null) {
162 for (BoxedElement bel : elementList) {
163 if (bbox.intersects(bel.getBBox()))
164 resultSet.add(bel.el);
165 }
166 }
167 if (children != null) {
168 for (ElementQuadTreeNode child : children) {
169 child.get(bbox, resultSet);
170 }
171 }
172 }
173
174 /**
175 * Splits this quadtree node into 4 subnodes.
176 */
177 private void split() {
178 if (bounds.getHeight() <= 5 || bounds.getWidth() <= 5) {
179 log.error("Do not split more due to too small bounds: " + bounds);
180 return;
181 }
182
183 int halfLat = (bounds.getMinLat() + bounds.getMaxLat()) / 2;
184 int halfLong = (bounds.getMinLong() + bounds.getMaxLong()) / 2;
185 Area[] childBounds = new Area[4];
186
187 childBounds[0] = new Area(bounds.getMinLat(), bounds.getMinLong(),
188 halfLat, halfLong);
189 childBounds[1] = new Area(halfLat, bounds.getMinLong(),
190 bounds.getMaxLat(), halfLong);
191 childBounds[2] = new Area(bounds.getMinLat(), halfLong, halfLat,
192 bounds.getMaxLong());
193 childBounds[3] = new Area(halfLat, halfLong, bounds.getMaxLat(),
194 bounds.getMaxLong());
195
196 List<List<BoxedElement>> childElems = new ArrayList<>();
197 for (int i = 0; i < 4; i++) {
198 childElems.add(new ArrayList<>());
199 }
200 boolean modified = false;
201 Iterator<BoxedElement> iter = elementList.iterator();
202 while (iter.hasNext()) {
203 BoxedElement bel = iter.next();
204 int count = 0;
205 int lastIndex = -1;
206 for (int i = 0; i < 4; i++) {
207 if (childBounds[i].intersects(bel.getBBox())) {
208 count++;
209 lastIndex = i;
210 if (childBounds[i].insideBoundary(bel.getBBox())) {
211 break;
212 }
213 }
214 }
215 if (count == 1) {
216 childElems.get(lastIndex).add(bel);
217 iter.remove();
218 modified = true;
219 }
220 }
221 if (modified) {
222 children = new ElementQuadTreeNode[4];
223 for (int i = 0; i < 4; i++) {
224 children[i] = new ElementQuadTreeNode(childBounds[i], childElems.get(i));
225 }
226 }
227 if (elementList.isEmpty())
228 elementList = null;
229 }
230
231 public void clear() {
232 this.children = null;
233 elementList = null;
234 }
235
236 private class BoxedElement{
237 private Area bbox;
238 private final Element el;
239
240 Area getBBox() {
241 if (el instanceof Way) {
242 if (bbox == null)
243 bbox = Area.getBBox(((Way) el).getPoints());
244 return bbox;
245 }
246 return Area.getBBox(Arrays.asList(((Node) el).getLocation()));
247 }
248
249 BoxedElement(Element el) {
250 this.el = el;
251 }
252
253 int getSize() {
254 if (el instanceof Way)
255 return ((Way) el).getPoints().size();
256 return 1;
257 }
258 }
259 }