| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public final class ElementQuadTreeNode { |
| 35 |
|
| 36 |
private static final Logger log = Logger.getLogger(ElementQuadTreeNode.class); |
| 37 |
|
| 38 |
|
| 39 |
private static final int MAX_POINTS = 1000; |
| 40 |
|
| 41 |
private List<BoxedElement> elementList; |
| 42 |
|
| 43 |
|
| 44 |
private final Area bounds; |
| 45 |
private final Rectangle boundsRect; |
| 46 |
|
| 47 |
|
| 48 |
private Boolean empty; |
| 49 |
|
| 50 |
|
| 51 |
private ElementQuadTreeNode[] children; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 94 |
|
| 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 |
|
| 144 |
|
| 145 |
private void checkSplit() { |
| 146 |
if (elementList != null && elementList.size() > 1 && getSize() > MAX_POINTS) { |
| 147 |
split(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 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 |
} |