| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 87 |
tabC.propagateSizeBytes(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 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 |
|
| 117 |
int tablesOffset = (writer.position() + alignment) & ~alignMask; |
| 118 |
log.debug("write table a at offset", Integer.toHexString(tablesOffset)); |
| 119 |
|
| 120 |
|
| 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 |
|
| 131 |
node.writeSecond(writer); |
| 132 |
} |
| 133 |
|
| 134 |
writer.position(tablesOffset); |
| 135 |
|
| 136 |
|
| 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 |
|
| 151 |
tabA.writePost(writer); |
| 152 |
|
| 153 |
tabB.writePost(writer); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 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 |
} |