| 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 uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 18 |
import uk.me.parabola.log.Logger; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class RouteArc { |
| 30 |
private static final Logger log = Logger.getLogger(RouteArc.class); |
| 31 |
|
| 32 |
|
| 33 |
private static final int FLAG_HASNET = 0x80; |
| 34 |
private static final int FLAG_FORWARD = 0x40; |
| 35 |
private static final int MASK_DESTCLASS = 0x7; |
| 36 |
public static final int MASK_CURVE_LEN = 0x38; |
| 37 |
|
| 38 |
|
| 39 |
private static final int FLAG_LAST_LINK = 0x80; |
| 40 |
private static final int FLAG_EXTERNAL = 0x40; |
| 41 |
|
| 42 |
private int offset; |
| 43 |
|
| 44 |
|
| 45 |
private float initialHeading; |
| 46 |
private final float directHeading; |
| 47 |
|
| 48 |
private final RoadDef roadDef; |
| 49 |
|
| 50 |
|
| 51 |
private final RouteNode source; |
| 52 |
private final RouteNode dest; |
| 53 |
|
| 54 |
|
| 55 |
private int indexA; |
| 56 |
|
| 57 |
private int indexB; |
| 58 |
|
| 59 |
private int flagA; |
| 60 |
private int flagB; |
| 61 |
|
| 62 |
private final boolean haveCurve; |
| 63 |
private final int length; |
| 64 |
private final byte lengthRatio; |
| 65 |
private final int pointsHash; |
| 66 |
private boolean isForward; |
| 67 |
private final float lengthInMeter; |
| 68 |
|
| 69 |
private boolean isDirect; |
| 70 |
|
| 71 |
|
| 72 |
private byte maxDestClass = -1; |
| 73 |
|
| 74 |
|
| 75 |
private RouteArc reverseArc; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public RouteArc(RoadDef roadDef, |
| 90 |
RouteNode source, RouteNode dest, |
| 91 |
double initialBearing, double directBearing, |
| 92 |
double arcLength, |
| 93 |
double pathLength, |
| 94 |
double directLength, |
| 95 |
int pointsHash) { |
| 96 |
this.isDirect = true; |
| 97 |
this.roadDef = roadDef; |
| 98 |
this.source = source; |
| 99 |
this.dest = dest; |
| 100 |
this.initialHeading = initialBearing >= 180 ? (float)(initialBearing - 360) : (float)initialBearing; |
| 101 |
|
| 102 |
this.directHeading = directBearing >= 180 ? (float)(directBearing - 360) : (float)directBearing; |
| 103 |
int len = NODHeader.metersToRaw(arcLength); |
| 104 |
if (len >= (1 << 22)) { |
| 105 |
log.error("Way " + roadDef.getName() + " (id " + roadDef.getId() + ") contains an arc whose length (" + len + " units) is too big to be encoded, using length",((1 << 22) - 1)); |
| 106 |
len = (1 << 22) - 1; |
| 107 |
} |
| 108 |
this.length = len; |
| 109 |
this.lengthInMeter = (float) arcLength; |
| 110 |
this.pointsHash = pointsHash; |
| 111 |
|
| 112 |
int ratio = 0; |
| 113 |
int pathEncoded = NODHeader.metersToRaw(pathLength); |
| 114 |
if (pathEncoded >= 2){ |
| 115 |
int directEncoded = NODHeader.metersToRaw(directLength); |
| 116 |
if (pathEncoded > directEncoded){ |
| 117 |
ratio = (int) Math.min(31, Math.round(32.0d * directLength/ pathLength)); |
| 118 |
} |
| 119 |
} |
| 120 |
if (ratio == 0 && len >= (1 << 14)) |
| 121 |
ratio = 0x1f; |
| 122 |
lengthRatio = (byte)ratio; |
| 123 |
haveCurve = lengthRatio > 0; |
| 124 |
} |
| 125 |
|
| 126 |
public float getInitialHeading() { |
| 127 |
return initialHeading; |
| 128 |
} |
| 129 |
|
| 130 |
public float getDirectHeading() { |
| 131 |
return directHeading; |
| 132 |
} |
| 133 |
|
| 134 |
public void setInitialHeading(float ih) { |
| 135 |
initialHeading = ih; |
| 136 |
} |
| 137 |
|
| 138 |
public void modInitialHeading(float adjust) { |
| 139 |
initialHeading += adjust; |
| 140 |
if (initialHeading >= 180) |
| 141 |
initialHeading -= 360; |
| 142 |
else if (initialHeading < -180) |
| 143 |
initialHeading += 360; |
| 144 |
} |
| 145 |
|
| 146 |
public float getFinalHeading() { |
| 147 |
float fh = 0; |
| 148 |
if (lengthInMeter != 0){ |
| 149 |
fh = getReverseArc().getInitialHeading() + 180; |
| 150 |
if (fh >= 180) |
| 151 |
fh -= 360; |
| 152 |
} |
| 153 |
return fh; |
| 154 |
} |
| 155 |
|
| 156 |
public RouteNode getSource() { |
| 157 |
return source; |
| 158 |
} |
| 159 |
|
| 160 |
public RouteNode getDest() { |
| 161 |
return dest; |
| 162 |
} |
| 163 |
|
| 164 |
public int getLength() { |
| 165 |
return length; |
| 166 |
} |
| 167 |
|
| 168 |
public int getPointsHash() { |
| 169 |
return pointsHash; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public int boundSize() { |
| 176 |
|
| 177 |
int[] lendat = encodeLength(); |
| 178 |
|
| 179 |
|
| 180 |
int size = 5 + lendat.length; |
| 181 |
if(haveCurve) |
| 182 |
size += encodeCurve().length; |
| 183 |
return size; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public boolean isInternal() { |
| 190 |
|
| 191 |
return (flagB & FLAG_EXTERNAL) == 0; |
| 192 |
} |
| 193 |
|
| 194 |
public void setInternal(boolean internal) { |
| 195 |
if (internal) |
| 196 |
flagB &= ~FLAG_EXTERNAL; |
| 197 |
else |
| 198 |
flagB |= FLAG_EXTERNAL; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public void setIndexA(int indexA) { |
| 206 |
this.indexA = indexA; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public int getIndexA() { |
| 215 |
return indexA; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public void setIndexB(int indexB) { |
| 222 |
assert !isInternal() : "Trying to set index on internal arc."; |
| 223 |
this.indexB = indexB; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public int getIndexB() { |
| 232 |
return indexB; |
| 233 |
} |
| 234 |
|
| 235 |
public int getArcDestClass(){ |
| 236 |
return Math.min(getRoadDef().getRoadClass(), dest.getGroup()); |
| 237 |
} |
| 238 |
|
| 239 |
public float getLengthInMeter(){ |
| 240 |
return lengthInMeter; |
| 241 |
} |
| 242 |
|
| 243 |
public static byte directionFromDegrees(float dir) { |
| 244 |
return (byte) Math.round(dir * 256 / 360); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public static int compactDirFromDegrees(float dir) { |
| 251 |
return ((directionFromDegrees(dir) + 8) >> 4) & 0x0f; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public void write(ImgFileWriter writer, RouteArc lastArc, boolean useCompactDirs, Byte compactedDir) { |
| 270 |
boolean first = lastArc == null; |
| 271 |
if (first){ |
| 272 |
if (useCompactDirs) |
| 273 |
flagA |= FLAG_HASNET; |
| 274 |
} else { |
| 275 |
if (lastArc.getRoadDef() != this.getRoadDef()) |
| 276 |
flagA |= FLAG_HASNET; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
if (isForward) |
| 281 |
flagA |= FLAG_FORWARD; |
| 282 |
|
| 283 |
offset = writer.position(); |
| 284 |
if(log.isDebugEnabled()) |
| 285 |
log.debug("writing arc at", offset, ", flagA=", Integer.toHexString(flagA)); |
| 286 |
|
| 287 |
|
| 288 |
setDestinationClass(getArcDestClass()); |
| 289 |
|
| 290 |
|
| 291 |
int[] lendat = encodeLength(); |
| 292 |
|
| 293 |
writer.put1u(flagA); |
| 294 |
if (isInternal()) { |
| 295 |
|
| 296 |
writer.put1u(flagB); |
| 297 |
writer.put1u(0); |
| 298 |
} else { |
| 299 |
if(indexB < 0 || indexB >= 0x3f) { |
| 300 |
writer.put1u(flagB | 0x3f); |
| 301 |
writer.put1u(indexB); |
| 302 |
} |
| 303 |
else |
| 304 |
writer.put1u(flagB | indexB); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
if (first || lastArc.indexA != this.indexA) |
| 309 |
writer.put1u(indexA); |
| 310 |
|
| 311 |
if(log.isDebugEnabled()) |
| 312 |
log.debug("writing length", length); |
| 313 |
for (int aLendat : lendat) |
| 314 |
writer.put((byte) aLendat); |
| 315 |
|
| 316 |
if (first || lastArc.indexA != this.indexA || lastArc.isForward != isForward){ |
| 317 |
if (useCompactDirs){ |
| 318 |
|
| 319 |
if (compactedDir != null) |
| 320 |
writer.put(compactedDir); |
| 321 |
} else { |
| 322 |
writer.put(directionFromDegrees(initialHeading)); |
| 323 |
} |
| 324 |
} |
| 325 |
if (haveCurve) { |
| 326 |
int[] curvedat = encodeCurve(); |
| 327 |
for (int aCurvedat : curvedat) |
| 328 |
writer.put((byte) aCurvedat); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public void writeSecond(ImgFileWriter writer) { |
| 338 |
if (!isInternal()) |
| 339 |
return; |
| 340 |
|
| 341 |
writer.position(offset + 1L); |
| 342 |
int val = flagB << 8; |
| 343 |
int diff = dest.getOffsetNod1() - source.getOffsetNod1(); |
| 344 |
assert diff < 0x2000 && diff >= -0x2000 |
| 345 |
: "relative pointer too large for 14 bits (source offset = " + source.getOffsetNod1() + ", dest offset = " + dest.getOffsetNod1() + ")"; |
| 346 |
val |= diff & 0x3fff; |
| 347 |
|
| 348 |
|
| 349 |
if(log.isDebugEnabled()) |
| 350 |
log.debug("val is", Integer.toHexString(val)); |
| 351 |
writer.put1u(val >> 8); |
| 352 |
writer.put1u(val & 0xff); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
private int[] encodeLength() { |
| 363 |
int[] lendat; |
| 364 |
if(length < 0x300 || (length < 0x400 && !haveCurve)) { |
| 365 |
|
| 366 |
|
| 367 |
flagA &= ~0x38; |
| 368 |
|
| 369 |
if(haveCurve) |
| 370 |
flagA |= 0x20; |
| 371 |
flagA |= (length >> 5) & 0x18; |
| 372 |
lendat = new int[1]; |
| 373 |
lendat[0] = length; |
| 374 |
assert (flagA & 0x38) != 0x38; |
| 375 |
} else { |
| 376 |
flagA |= 0x38; |
| 377 |
if(length >= (1 << 14)) { |
| 378 |
|
| 379 |
lendat = new int[3]; |
| 380 |
lendat[0] = 0xC0 | (length & 0x3f); |
| 381 |
lendat[1] = (length >> 6) & 0xff; |
| 382 |
lendat[2] = (length >> 14) & 0xff; |
| 383 |
} |
| 384 |
else if(haveCurve) { |
| 385 |
|
| 386 |
lendat = new int[2]; |
| 387 |
lendat[0] = (length & 0x7f); |
| 388 |
lendat[1] = (length >> 7) & 0xff; |
| 389 |
} |
| 390 |
else { |
| 391 |
|
| 392 |
lendat = new int[2]; |
| 393 |
lendat[0] = 0x80 | (length & 0x3f); |
| 394 |
lendat[1] = (length >> 6) & 0xff; |
| 395 |
} |
| 396 |
} |
| 397 |
return lendat; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
private int[] encodeCurve() { |
| 407 |
assert lengthRatio != 0; |
| 408 |
int[] curveData; |
| 409 |
int dh = directionFromDegrees(directHeading); |
| 410 |
if (lengthRatio >= 1 && lengthRatio <= 17) { |
| 411 |
|
| 412 |
curveData = new int[2]; |
| 413 |
curveData[0] = lengthRatio; |
| 414 |
curveData[1] = dh; |
| 415 |
|
| 416 |
} else { |
| 417 |
|
| 418 |
int compactedRatio = lengthRatio / 2 - 8; |
| 419 |
assert compactedRatio > 0 && compactedRatio < 8; |
| 420 |
curveData = new int[1]; |
| 421 |
curveData[0] = (compactedRatio << 5) | ((dh >> 3) & 0x1f); |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
} |
| 433 |
return curveData; |
| 434 |
} |
| 435 |
|
| 436 |
public RoadDef getRoadDef() { |
| 437 |
return roadDef; |
| 438 |
} |
| 439 |
|
| 440 |
public void setForward() { |
| 441 |
isForward = true; |
| 442 |
} |
| 443 |
|
| 444 |
public boolean isForward() { |
| 445 |
return isForward; |
| 446 |
} |
| 447 |
|
| 448 |
public void setLast() { |
| 449 |
flagB |= FLAG_LAST_LINK; |
| 450 |
} |
| 451 |
|
| 452 |
protected void setDestinationClass(int destinationClass) { |
| 453 |
if(log.isDebugEnabled()) |
| 454 |
log.debug("setting destination class", destinationClass); |
| 455 |
flagA |= (destinationClass & MASK_DESTCLASS); |
| 456 |
} |
| 457 |
|
| 458 |
public void setIndirect() { |
| 459 |
this.isDirect = false; |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
public boolean isDirect() { |
| 464 |
return isDirect; |
| 465 |
} |
| 466 |
|
| 467 |
public void setMaxDestClass(int destClass) { |
| 468 |
if (this.maxDestClass < 0) |
| 469 |
this.maxDestClass = (byte) destClass; |
| 470 |
else if (destClass < maxDestClass) |
| 471 |
maxDestClass = (byte)destClass; |
| 472 |
} |
| 473 |
|
| 474 |
@Override |
| 475 |
public String toString() { |
| 476 |
return "RouteArc [" + (isForward ? "->":"<-") + (isDirect ? "direct":"indirect") |
| 477 |
+ roadDef + " " + dest + "]"; |
| 478 |
} |
| 479 |
|
| 480 |
public RouteArc getReverseArc() { |
| 481 |
return reverseArc; |
| 482 |
} |
| 483 |
|
| 484 |
public void setReverseArc(RouteArc reverseArc) { |
| 485 |
this.reverseArc = reverseArc; |
| 486 |
} |
| 487 |
} |