Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2008
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 * Create date: 07-Jul-2008
14 */
15 package uk.me.parabola.imgfmt.app.net;
16
17 import java.util.List;
18
19 import uk.me.parabola.imgfmt.Utils;
20 import uk.me.parabola.imgfmt.app.Area;
21 import uk.me.parabola.imgfmt.app.Coord;
22 import uk.me.parabola.imgfmt.app.ImgFileWriter;
23 import uk.me.parabola.log.Logger;
24
25 /**
26 * Routing nodes are divided into areas which I am calling RouteCenter's.
27 * The center has a location and it contains nodes that are nearby.
28 * There is routing between nodes in the center and there are links
29 * to nodes in other centers.
30 */
31 public class RouteCenter {
32 private static final Logger log = Logger.getLogger(RouteCenter.class);
33
34 private final Area area;
35 private final Coord centralPoint;
36
37 private final List<RouteNode> nodes;
38
39 private final TableA tabA;
40 private final TableB tabB;
41 private final TableC tabC;
42
43 public RouteCenter(Area area, List<RouteNode> nodes,
44 TableA tabA, TableB tabB) {
45
46 this.area = area;
47 this.centralPoint = area.getCenter();
48 this.nodes = nodes;
49 this.tabA = tabA;
50 this.tabB = tabB;
51 this.tabC = new TableC(tabA);
52
53 log.info("new RouteCenter at " + centralPoint +
54 ", nodes: " + nodes.size() + " tabA: " + tabA.size() +
55 " tabB: " + tabB.size());
56 }
57
58 /**
59 * update arcs with table indices; populate tabC
60 */
61 private void updateOffsets(){
62 for (RouteNode node : nodes) {
63 node.setOffsets(centralPoint);
64 for (RouteArc arc : node.arcsIteration()) {
65 arc.setIndexA(tabA.getIndex(arc));
66 arc.setInternal(nodes.contains(arc.getDest()));
67 if (!arc.isInternal())
68 arc.setIndexB(tabB.getIndex(arc.getDest()));
69 }
70
71 for (RouteRestriction restr : node.getRestrictions()){
72 if (restr.getArcs().size() >= 3){
73 // only restrictions with more than 2 arcs can contain further arcs
74 for (RouteArc arc : restr.getArcs()){
75 if (arc.getSource() == node)
76 continue;
77 arc.setIndexA(tabA.getIndex(arc));
78 arc.setInternal(nodes.contains(arc.getDest()));
79 if (!arc.isInternal())
80 arc.setIndexB(tabB.getIndex(arc.getDest()));
81 }
82 }
83 restr.setOffsetC(tabC.addRestriction(restr));
84 }
85 }
86 // update size of tabC offsets, now that tabC has been populated
87 tabC.propagateSizeBytes();
88 }
89
90 /**
91 * Write a route center.
92 *
93 * writer.position() is relative to the start of NOD 1.
94 * Space for Table A is reserved but not written. See writeTableA.
95 */
96 public void write(ImgFileWriter writer, int[] classBoundaries) {
97 assert !nodes.isEmpty(): "RouteCenter without nodes";
98 updateOffsets();
99 int centerPos = writer.position();
100 for (RouteNode node : nodes){
101 node.write(writer);
102 int group = node.getGroup();
103 if (group == 0)
104 continue;
105 if (centerPos < classBoundaries[group-1]){
106 // update positions (loop is used because style might not use all classes
107 for (int i = group-1; i >= 0; i--){
108 if (centerPos < classBoundaries[i] )
109 classBoundaries[i] = centerPos;
110 }
111 }
112 }
113 int alignment = 1 << NODHeader.DEF_ALIGN;
114 int alignMask = alignment - 1;
115
116 // Calculate the position of the tables.
117 int tablesOffset = (writer.position() + alignment) & ~alignMask;
118 log.debug("write table a at offset", Integer.toHexString(tablesOffset));
119
120 // Go back and fill in all the table offsets
121 for (RouteNode node : nodes) {
122 int pos = node.getOffsetNod1();
123 log.debug("node pos", pos);
124 byte bo = (byte) calcLowByte(pos, tablesOffset);
125
126 writer.position(pos);
127 log.debug("rewrite taba offset", writer.position(), bo);
128 writer.put1u(bo);
129
130 // fill in arc pointers
131 node.writeSecond(writer);
132 }
133
134 writer.position(tablesOffset);
135
136 // Write the tables header
137 writer.put1u(tabC.getFormat());
138 Utils.put3sLongitude(writer, centralPoint.getLongitude());
139 writer.put3s(centralPoint.getLatitude());
140 writer.put1u(tabA.getNumberOfItems());
141 writer.put1u(tabB.getNumberOfItems());
142
143 tabA.write(writer);
144 tabB.write(writer);
145 tabC.write(writer, tablesOffset);
146 log.info("end of center:", writer.position());
147 }
148
149 public void writePost(ImgFileWriter writer) {
150 // NET addresses are now known
151 tabA.writePost(writer);
152 // all RouteNodes now have their NOD1 offsets
153 tabB.writePost(writer);
154 }
155
156 /**
157 * Inverse of calcTableOffset.
158 */
159 private static int calcLowByte(int nodeOffset, int tablesOffset) {
160 assert nodeOffset < tablesOffset;
161 int align = NODHeader.DEF_ALIGN;
162 int mask = (1 << align) - 1;
163 if ((tablesOffset & mask) != 0) {
164 log.warn("tablesOffset not a multiple of (1<<align): %x", tablesOffset);
165 // round up to next multiple
166 tablesOffset = ((tablesOffset >> align) + 1) << align;
167 }
168 int low = (tablesOffset >> align) - (nodeOffset >> align) - 1;
169 assert 0 <= low && low < 0x100;
170 return low;
171 }
172
173 public Area getArea() {
174 return area;
175 }
176
177 public String reportSizes() {
178 int nodesSize = 0;
179 for(RouteNode n : nodes)
180 nodesSize += n.boundSize();
181 return "n=(" + nodes.size() + "," + nodesSize + "), a=" + tabA.size() + ", b=" + tabB.size();
182 }
183 }