| 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.ArrayList; |
| 18 |
|
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 22 |
import static uk.me.parabola.imgfmt.app.net.AccessTagsAndBits.*; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class RouteRestriction { |
| 41 |
|
| 42 |
|
| 43 |
private static final byte RESTRICTION_TYPE = 0x05; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private static final int F_INTERNAL = 0x8000; |
| 48 |
|
| 49 |
|
| 50 |
private final List<RouteArc> arcs; |
| 51 |
|
| 52 |
private final RouteNode viaNode; |
| 53 |
|
| 54 |
private int offsetSize; |
| 55 |
private int offsetC; |
| 56 |
|
| 57 |
|
| 58 |
private boolean last; |
| 59 |
|
| 60 |
|
| 61 |
private final byte exceptMask; |
| 62 |
private final byte flags; |
| 63 |
|
| 64 |
private static final byte F_EXCEPT_FOOT = 0x02; |
| 65 |
private static final byte F_EXCEPT_EMERGENCY = 0x04; |
| 66 |
private static final byte F_MORE_EXCEPTIONS = 0x08; |
| 67 |
|
| 68 |
private static final byte EXCEPT_CAR = 0x01; |
| 69 |
private static final byte EXCEPT_BUS = 0x02; |
| 70 |
private static final byte EXCEPT_TAXI = 0x04; |
| 71 |
private static final byte EXCEPT_DELIVERY = 0x10; |
| 72 |
private static final byte EXCEPT_BICYCLE = 0x20; |
| 73 |
private static final byte EXCEPT_TRUCK = 0x40; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public RouteRestriction(RouteNode viaNode, List<RouteArc> traffArcs, byte mkgmapExceptMask) { |
| 82 |
this.viaNode = viaNode; |
| 83 |
this.arcs = new ArrayList<>(traffArcs); |
| 84 |
for (int i = 0; i < arcs.size(); i++){ |
| 85 |
RouteArc arc = arcs.get(i); |
| 86 |
assert arc.getDest() != viaNode; |
| 87 |
} |
| 88 |
byte tmpFlags = 0; |
| 89 |
|
| 90 |
if ((mkgmapExceptMask & FOOT) != 0) |
| 91 |
tmpFlags |= F_EXCEPT_FOOT; |
| 92 |
if ((mkgmapExceptMask & EMERGENCY) != 0) |
| 93 |
tmpFlags |= F_EXCEPT_EMERGENCY; |
| 94 |
|
| 95 |
exceptMask = translateExceptMask(mkgmapExceptMask); |
| 96 |
if(exceptMask != 0) |
| 97 |
tmpFlags |= F_MORE_EXCEPTIONS; |
| 98 |
|
| 99 |
int numArcs = arcs.size(); |
| 100 |
assert numArcs < 8; |
| 101 |
tmpFlags |= ((numArcs) << 5); |
| 102 |
this.flags = tmpFlags; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private static byte translateExceptMask(byte mkgmapExceptMask) { |
| 112 |
byte mask = 0; |
| 113 |
if ((mkgmapExceptMask & CAR) != 0) |
| 114 |
mask |= EXCEPT_CAR; |
| 115 |
if ((mkgmapExceptMask & BUS) != 0) |
| 116 |
mask |= EXCEPT_BUS; |
| 117 |
if ((mkgmapExceptMask & TAXI) != 0) |
| 118 |
mask |= EXCEPT_TAXI; |
| 119 |
if ((mkgmapExceptMask & DELIVERY) != 0) |
| 120 |
mask |= EXCEPT_DELIVERY; |
| 121 |
if ((mkgmapExceptMask & BIKE) != 0) |
| 122 |
mask |= EXCEPT_BICYCLE; |
| 123 |
if ((mkgmapExceptMask & TRUCK) != 0) |
| 124 |
mask |= EXCEPT_TRUCK; |
| 125 |
return mask; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
private static int calcOffset(RouteNode node, int tableOffset) { |
| 130 |
int offset = tableOffset - node.getOffsetNod1(); |
| 131 |
assert offset >= 0 : "node behind start of tables"; |
| 132 |
assert offset < 0x8000 : "node offset too large"; |
| 133 |
return offset | F_INTERNAL; |
| 134 |
} |
| 135 |
|
| 136 |
public List<RouteArc> getArcs(){ |
| 137 |
return arcs; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void write(ImgFileWriter writer, int tableOffset) { |
| 148 |
writer.put1u(RESTRICTION_TYPE); |
| 149 |
|
| 150 |
writer.put(flags); |
| 151 |
writer.put1u(0); |
| 152 |
|
| 153 |
if(exceptMask != 0) |
| 154 |
writer.put1u(exceptMask); |
| 155 |
|
| 156 |
int numArcs = arcs.size(); |
| 157 |
int[] offsets = new int[numArcs+1]; |
| 158 |
int pos = 0; |
| 159 |
boolean viaWritten = false; |
| 160 |
for (int i = 0; i < numArcs; i++){ |
| 161 |
RouteArc arc = arcs.get(i); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (arc.isInternal()) |
| 168 |
offsets[pos++] = calcOffset(arc.getDest(), tableOffset); |
| 169 |
else |
| 170 |
offsets[pos++] = arc.getIndexB(); |
| 171 |
if (!viaWritten && arc.getSource() == viaNode) { |
| 172 |
|
| 173 |
offsets[pos++] = calcOffset(viaNode, tableOffset); |
| 174 |
viaWritten = true; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
for (int offset : offsets) |
| 179 |
writer.put2u(offset); |
| 180 |
|
| 181 |
for (RouteArc arc: arcs) |
| 182 |
writer.put1u(arc.getIndexA()); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public void writeOffset(ImgFileWriter writer) { |
| 189 |
assert 0 < offsetSize && offsetSize <= 2 : "illegal offset size"; |
| 190 |
int offset = offsetC; |
| 191 |
if (offsetSize == 1) { |
| 192 |
assert offset < 0x80; |
| 193 |
if (last) |
| 194 |
offset |= 0x80; |
| 195 |
writer.put1u(offset); |
| 196 |
} else { |
| 197 |
assert offset < 0x8000; |
| 198 |
if (last) |
| 199 |
offset |= 0x8000; |
| 200 |
writer.put2u(offset); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public int getSize() { |
| 208 |
int size = 3; |
| 209 |
if(exceptMask != 0) |
| 210 |
++size; |
| 211 |
size += arcs.size() + (arcs.size()+1) * 2; |
| 212 |
return size; |
| 213 |
} |
| 214 |
|
| 215 |
public void setOffsetC(int offsetC) { |
| 216 |
this.offsetC = offsetC; |
| 217 |
} |
| 218 |
|
| 219 |
public int getOffsetC() { |
| 220 |
return offsetC; |
| 221 |
} |
| 222 |
|
| 223 |
public void setOffsetSize(int size) { |
| 224 |
offsetSize = size; |
| 225 |
} |
| 226 |
|
| 227 |
public void setLast() { |
| 228 |
last = true; |
| 229 |
} |
| 230 |
} |