| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.net; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.LinkedHashMap; |
| 21 |
import java.util.LinkedList; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Map; |
| 24 |
|
| 25 |
import uk.me.parabola.imgfmt.app.Area; |
| 26 |
import uk.me.parabola.imgfmt.app.Coord; |
| 27 |
import uk.me.parabola.log.Logger; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class NOD1Part { |
| 42 |
private static final Logger log = Logger.getLogger(NOD1Part.class); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private static final int MAX_SIZE_UNSAFE = 1 << 16; |
| 70 |
private static final int MAX_SIZE = MAX_SIZE_UNSAFE - 0x800; |
| 71 |
|
| 72 |
|
| 73 |
private static final int MAX_TABA_UNSAFE = 0x100; |
| 74 |
private static final int MAX_TABA = MAX_TABA_UNSAFE - 0x8; |
| 75 |
|
| 76 |
|
| 77 |
private static final int MAX_TABB_UNSAFE = 0x100; |
| 78 |
private static final int MAX_TABB = MAX_TABB_UNSAFE - 0x2; |
| 79 |
|
| 80 |
|
| 81 |
private static final int MAX_TABC_OFFSET = 0x8000 - 1; |
| 82 |
private int lastTabCOffset; |
| 83 |
private int tabCSize; |
| 84 |
|
| 85 |
|
| 86 |
private static final int MAX_NODES_SIZE = 0x2000; |
| 87 |
private int nodesSize; |
| 88 |
|
| 89 |
public class BBox { |
| 90 |
int maxLat, minLat, maxLon, minLon; |
| 91 |
boolean empty; |
| 92 |
|
| 93 |
BBox() { |
| 94 |
empty = true; |
| 95 |
} |
| 96 |
|
| 97 |
BBox(Coord co) { |
| 98 |
empty = false; |
| 99 |
int lat = co.getLatitude(); |
| 100 |
int lon = co.getLongitude(); |
| 101 |
minLat = lat; |
| 102 |
maxLat = lat+1; |
| 103 |
minLon = lon; |
| 104 |
maxLon = lon+1; |
| 105 |
} |
| 106 |
|
| 107 |
BBox(int minLat, int maxLat, int minLon, int maxLon) { |
| 108 |
empty = false; |
| 109 |
this.minLat = minLat; |
| 110 |
this.maxLat = maxLat; |
| 111 |
this.minLon = minLon; |
| 112 |
this.maxLon = maxLon; |
| 113 |
} |
| 114 |
|
| 115 |
Area toArea() { |
| 116 |
return new Area(minLat, minLon, maxLat, maxLon); |
| 117 |
} |
| 118 |
|
| 119 |
boolean contains(BBox bbox) { |
| 120 |
return minLat <= bbox.minLat && bbox.maxLat <= maxLat |
| 121 |
&& minLon <= bbox.minLon && bbox.maxLon <= maxLon; |
| 122 |
} |
| 123 |
|
| 124 |
boolean contains(Coord co) { |
| 125 |
return contains(new BBox(co)); |
| 126 |
} |
| 127 |
|
| 128 |
void extend(BBox bbox) { |
| 129 |
if (bbox.empty) |
| 130 |
return; |
| 131 |
if (empty) { |
| 132 |
empty = false; |
| 133 |
minLat = bbox.minLat; |
| 134 |
maxLat = bbox.maxLat; |
| 135 |
minLon = bbox.minLon; |
| 136 |
maxLon = bbox.maxLon; |
| 137 |
} else { |
| 138 |
minLat = Math.min(minLat, bbox.minLat); |
| 139 |
maxLat = Math.max(maxLat, bbox.maxLat); |
| 140 |
minLon = Math.min(minLon, bbox.minLon); |
| 141 |
maxLon = Math.max(maxLon, bbox.maxLon); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void extend(Coord co) { |
| 146 |
extend(new BBox(co)); |
| 147 |
} |
| 148 |
|
| 149 |
BBox[] splitLat() { |
| 150 |
BBox[] ret = new BBox[2]; |
| 151 |
int midLat = (minLat + maxLat) / 2; |
| 152 |
ret[0] = new BBox(minLat, midLat, minLon, maxLon); |
| 153 |
ret[1] = new BBox(midLat, maxLat, minLon, maxLon); |
| 154 |
return ret; |
| 155 |
} |
| 156 |
|
| 157 |
BBox[] splitLon() { |
| 158 |
BBox[] ret = new BBox[2]; |
| 159 |
int midLon = (minLon + maxLon) / 2; |
| 160 |
ret[0] = new BBox(minLat, maxLat, minLon, midLon); |
| 161 |
ret[1] = new BBox(minLat, maxLat, midLon, maxLon); |
| 162 |
return ret; |
| 163 |
} |
| 164 |
|
| 165 |
int getWidth() { |
| 166 |
return maxLon - minLon; |
| 167 |
} |
| 168 |
|
| 169 |
int getHeight() { |
| 170 |
return maxLat - minLat; |
| 171 |
} |
| 172 |
|
| 173 |
int getMaxDimension() { |
| 174 |
return Math.max(getWidth(), getHeight()); |
| 175 |
} |
| 176 |
|
| 177 |
public String toString() { |
| 178 |
return "BBox[" + new Coord(minLat, minLon) + ", " + new Coord(maxLat, maxLon) + "]"; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
private final BBox bbox; |
| 184 |
|
| 185 |
private final BBox bboxActual = new BBox(); |
| 186 |
|
| 187 |
private List<RouteNode> nodes = new ArrayList<>(); |
| 188 |
private TableA tabA = new TableA(); |
| 189 |
private Map<RouteNode,RouteNode> destNodes = new LinkedHashMap<>(); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public NOD1Part() { |
| 198 |
log.info("creating new unbounded NOD1Part"); |
| 199 |
this.bbox = null; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private NOD1Part(BBox bbox) { |
| 209 |
log.info("creating new NOD1Part:", bbox); |
| 210 |
this.bbox = bbox; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public void addNode(RouteNode node) { |
| 222 |
assert bbox == null || bbox.contains(node.getCoord()) : "trying to add out-of-bounds node: " + node; |
| 223 |
|
| 224 |
bboxActual.extend(node.getCoord()); |
| 225 |
nodes.add(node); |
| 226 |
for (RouteArc arc : node.arcsIteration()) { |
| 227 |
tabA.addArc(arc); |
| 228 |
RouteNode dest = arc.getDest(); |
| 229 |
if (!arc.isInternal()) { |
| 230 |
destNodes.put(dest, dest); |
| 231 |
} else if (bbox != null && !bbox.contains(dest.getCoord()) || dest.getGroup() != node.getGroup()) { |
| 232 |
arc.setInternal(false); |
| 233 |
destNodes.put(dest, dest); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
for (RouteRestriction rr: node.getRestrictions()){ |
| 238 |
lastTabCOffset = tabCSize; |
| 239 |
tabCSize += rr.getSize(); |
| 240 |
List<RouteArc> arcs = rr.getArcs(); |
| 241 |
if (arcs.size() >= 3){ |
| 242 |
for (int i = 0; i < arcs.size(); i++){ |
| 243 |
RouteArc arc = arcs.get(i); |
| 244 |
if (arc.getSource() != node){ |
| 245 |
tabA.addArc(arc); |
| 246 |
RouteNode dest = arc.getDest(); |
| 247 |
if (!arc.isInternal()) |
| 248 |
destNodes.put(dest, dest); |
| 249 |
else if (bbox != null && !bbox.contains(dest.getCoord()) || dest.getGroup() != node.getGroup()) { |
| 250 |
arc.setInternal(false); |
| 251 |
destNodes.put(dest, dest); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
nodesSize += node.boundSize(); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public List<RouteCenter> subdivide() { |
| 265 |
return subdivideHelper(0); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
protected List<RouteCenter> subdivideHelper(int depth) { |
| 272 |
List<RouteCenter> centers = new LinkedList<>(); |
| 273 |
|
| 274 |
if (satisfiesConstraints()) { |
| 275 |
if (nodesSize > 0) { |
| 276 |
centers.add(this.toRouteCenter()); |
| 277 |
} |
| 278 |
return centers; |
| 279 |
} |
| 280 |
|
| 281 |
if(depth > 48) { |
| 282 |
log.error("Region contains too many nodes/arcs (discarding " + nodes.size() + " nodes to be able to continue)"); |
| 283 |
log.error(" Expect the routing to be broken near " + bbox); |
| 284 |
for (RouteNode node : nodes) |
| 285 |
node.discard(); |
| 286 |
return centers; |
| 287 |
} |
| 288 |
|
| 289 |
log.info("subdividing", bbox, bboxActual); |
| 290 |
BBox[] split ; |
| 291 |
if (bboxActual.getWidth() > bboxActual.getHeight()) |
| 292 |
split = bboxActual.splitLon(); |
| 293 |
else |
| 294 |
split = bboxActual.splitLat(); |
| 295 |
|
| 296 |
NOD1Part[] parts = new NOD1Part[2]; |
| 297 |
|
| 298 |
for (int i = 0; i < split.length; i++) |
| 299 |
parts[i] = new NOD1Part(split[i]); |
| 300 |
|
| 301 |
|
| 302 |
for (RouteNode node : nodes) { |
| 303 |
int i = 0; |
| 304 |
while (!split[i].contains(node.getCoord())) |
| 305 |
i++; |
| 306 |
parts[i].addNode(node); |
| 307 |
} |
| 308 |
this.tabA = null; |
| 309 |
this.destNodes = null; |
| 310 |
this.nodes = null; |
| 311 |
for (NOD1Part part : parts) |
| 312 |
if(!part.bboxActual.empty) |
| 313 |
centers.addAll(part.subdivideHelper(depth + 1)); |
| 314 |
|
| 315 |
return centers; |
| 316 |
} |
| 317 |
|
| 318 |
private boolean satisfiesConstraints() { |
| 319 |
log.debug("constraints:", bboxActual, tabA.size(), destNodes.size(), nodesSize, lastTabCOffset); |
| 320 |
return bboxActual.getMaxDimension() < MAX_SIZE |
| 321 |
&& tabA.size() < MAX_TABA |
| 322 |
&& destNodes.size() < MAX_TABB |
| 323 |
&& nodesSize < MAX_NODES_SIZE |
| 324 |
&& lastTabCOffset <= MAX_TABC_OFFSET; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private RouteCenter toRouteCenter() { |
| 334 |
nodes.sort((n1, n2) -> n1.getCoord().compareTo(n2.getCoord())); |
| 335 |
TableB tabB = new TableB(); |
| 336 |
for (RouteNode rn : destNodes.keySet()) |
| 337 |
tabB.addNode(rn); |
| 338 |
return new RouteCenter(bboxActual.toArea(), nodes, tabA, tabB); |
| 339 |
} |
| 340 |
} |