Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/NOD1Part.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
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 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Robert Vollmert
15 * Create date: 02-Dec-2008
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 * This is a component of the RoadNetwork.
31 *
32 * Keeps track of outside neighbours and allows subdivision
33 * to satisfy NOD1 constraints.
34 *
35 * The approach to subdivision is to tile the map into RouteCenters.
36 * One could imagine that overlapping RouteCenters would be an option,
37 * say by splitting largely independent networks (motorways, footways).
38 *
39 * Could be rolled into RouteCenter.
40 */
41 public class NOD1Part {
42 private static final Logger log = Logger.getLogger(NOD1Part.class);
43
44 /*
45 * Constraints:
46 *
47 * 1. Nodes section smaller than about 0x4000, which gives
48 * a bound on the number of nodes.
49 * 2. At most 0x100 entries in Table A. This gives a bound
50 * on the number of (forward) arcs meeting this
51 * RouteCenter.
52 * 3. At most 0x40 entries in Table B. This gives a bound
53 * on the number of neighboring nodes.
54 * 4. Absolute values of coordinate offsets at most 0x8000,
55 * which translates to about 0.7 degrees, so bounding
56 * box should be at most 1.4 x 1.4 degrees assuming
57 * the reference is in the middle. (With small offsets,
58 * this would be 0.08 x 0.08 degrees.)
59 * 5. Absolute values of relative NOD1 offsets at most
60 * 0x2000, which limits the nodes section to 0x2000
61 * unless we take care to order the nodes nicely.
62 * 6. Distance between nodes and start of tables must
63 * fit in a char for writing Table C. So nodes
64 * section smaller than 0x10000.
65 */
66
67 // maximal width and height of the bounding box, since
68 // NOD 1 coordinate offsets are at most 16 bit wide.
69 private static final int MAX_SIZE_UNSAFE = 1 << 16;
70 private static final int MAX_SIZE = MAX_SIZE_UNSAFE - 0x800;
71
72 // Table A has at most 0x100 entries
73 private static final int MAX_TABA_UNSAFE = 0x100;
74 private static final int MAX_TABA = MAX_TABA_UNSAFE - 0x8;
75
76 // Table B has at most 0x100 entries
77 private static final int MAX_TABB_UNSAFE = 0x100;
78 private static final int MAX_TABB = MAX_TABB_UNSAFE - 0x2;
79
80 // Last entry in table C should not start at 0x8000 or higher, actual limit is unknown
81 private static final int MAX_TABC_OFFSET = 0x8000 - 1;
82 private int lastTabCOffset;
83 private int tabCSize;
84
85 // Nodes size is max 0x2000 to cope with signed 14 bit node offsets
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 // The area we are supposed to cover.
183 private final BBox bbox;
184 // The area that actually has nodes.
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 * Create an unbounded NOD1Part.
193 *
194 * All nodes will be accepted by addNode and
195 * all arcs will be considered internal.
196 */
197 public NOD1Part() {
198 log.info("creating new unbounded NOD1Part");
199 this.bbox = null;
200 }
201
202 /**
203 * Create a bounded NOD1Part.
204 *
205 * The bounding box is used to decide which arcs
206 * are internal.
207 */
208 private NOD1Part(BBox bbox) {
209 log.info("creating new NOD1Part:", bbox);
210 this.bbox = bbox;
211 }
212
213 /**
214 * Add a node to this part.
215 *
216 * The node is used to populate the tables. If an
217 * arc points outside the bbox, we know it's not
218 * an internal arc. It might still turn into an
219 * external arc at a deeper level of recursion.
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 * Subdivide this part recursively until it satisfies the constraints.
263 */
264 public List<RouteCenter> subdivide() {
265 return subdivideHelper(0);
266 }
267
268 /**
269 * Subdivide this part recursively until it satisfies the constraints.
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 * Convert to a RouteCenter.
329 *
330 * satisfiesConstraints() should be true for this to
331 * be a legal RouteCenter.
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 }