From 3bf2ded7a45b9b7300b226862f8b51fdd0a8f1dc Mon Sep 17 00:00:00 2001 From: vshcherb Date: Tue, 26 Nov 2013 16:23:05 +0100 Subject: [PATCH] Fix invisible zigzags --- .../net/osmand/binary/RouteDataObject.java | 25 ++++++++++++++++++- .../osmand/router/RouteResultPreparation.java | 17 ++++++++++--- .../net/osmand/router/RouteSegmentResult.java | 4 +++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/binary/RouteDataObject.java b/OsmAnd-java/src/net/osmand/binary/RouteDataObject.java index 798c53343f..4c4db7c077 100644 --- a/OsmAnd-java/src/net/osmand/binary/RouteDataObject.java +++ b/OsmAnd-java/src/net/osmand/binary/RouteDataObject.java @@ -7,6 +7,7 @@ import java.text.MessageFormat; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule; +import net.osmand.util.MapUtils; public class RouteDataObject { /*private */static final int RESTRICTION_SHIFT = 3; @@ -250,6 +251,24 @@ public class RouteDataObject { // So it should be fix in both places return directionRoute(startPoint, plus, 5); } + + public double distance(int startPoint, int endPoint) { + if(startPoint > endPoint) { + int k = endPoint; + endPoint = startPoint; + startPoint = k; + } + double d = 0; + for(int k = startPoint; k < endPoint && k < getPointsLength() -1; k++) { + int x = getPoint31XTile(k); + int y = getPoint31YTile(k); + int kx = getPoint31XTile(k + 1); + int ky = getPoint31YTile(k + 1); + d += simplifyDistance(kx, ky, x, y); + + } + return d; + } // Gives route direction of EAST degrees from NORTH ]-PI, PI] public double directionRoute(int startPoint, boolean plus, float dist) { @@ -274,10 +293,14 @@ public class RouteDataObject { px = getPoint31XTile(nx); py = getPoint31YTile(nx); // translate into meters - total += Math.abs(px - x) * 0.011d + Math.abs(py - y) * 0.01863d; + total += simplifyDistance(x, y, px, py); } while (total < dist); return -Math.atan2( x - px, y - py ); } + + private double simplifyDistance(int x, int y, int px, int py) { + return Math.abs(px - x) * 0.011d + Math.abs(py - y) * 0.01863d; + } @Override public String toString() { diff --git a/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java b/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java index cfcf58916a..d468b04d62 100644 --- a/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java +++ b/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java @@ -95,9 +95,20 @@ public class RouteResultPreparation { attachRoadSegments(ctx, result, i, next, plus); } List attachedRoutes = rr.getAttachedRoutes(next); - if (next != rr.getEndPointIndex() && !rr.getObject().roundabout() && attachedRoutes != null) { + boolean tryToSplit = next != rr.getEndPointIndex() && !rr.getObject().roundabout() && attachedRoutes != null; + if(rr.getDistance(next, plus ) == 0) { + // same point will be processed next step + tryToSplit = false; + } + if (tryToSplit) { + // avoid small zigzags float before = rr.getBearing(next, !plus); float after = rr.getBearing(next, plus); + if(rr.getDistance(next, plus ) < 5) { + after = before + 180; + } else if(rr.getDistance(next, !plus ) < 5) { + before = after - 180; + } boolean straight = Math.abs(MapUtils.degreesDiff(before + 180, after)) < TURN_DEGREE_MIN; boolean isSplit = false; // split if needed @@ -483,11 +494,11 @@ public class RouteResultPreparation { double devation = Math.abs(MapUtils.degreesDiff(prevSegm.getBearingEnd(), currentSegm.getBearingBegin())); boolean makeSlightTurn = devation > 5 && (!isMotorway(prevSegm) || !isMotorway(currentSegm)); if (kl) { - t = TurnType.valueOf(devation > 5 ? TurnType.TSLL : TurnType.KL, leftSide); + t = TurnType.valueOf(makeSlightTurn ? TurnType.TSLL : TurnType.KL, leftSide); t.setSkipToSpeak(!speak); } if (kr) { - t = TurnType.valueOf(devation > 5 ? TurnType.TSLR : TurnType.KR, leftSide); + t = TurnType.valueOf(makeSlightTurn ? TurnType.TSLR : TurnType.KR, leftSide); t.setSkipToSpeak(!speak); } if (t != null && lanes != null) { diff --git a/OsmAnd-java/src/net/osmand/router/RouteSegmentResult.java b/OsmAnd-java/src/net/osmand/router/RouteSegmentResult.java index b946323698..c0efcbe879 100644 --- a/OsmAnd-java/src/net/osmand/router/RouteSegmentResult.java +++ b/OsmAnd-java/src/net/osmand/router/RouteSegmentResult.java @@ -99,6 +99,10 @@ public class RouteSegmentResult { return (float) (object.directionRoute(point, plus) / Math.PI * 180); } + public float getDistance(int point, boolean plus) { + return (float) (plus? object.distance(point, endPointIndex): object.distance(startPointIndex, point)); + } + public float getBearingEnd() { return (float) (MapUtils.alignAngleDifference(object.directionRoute(endPointIndex, startPointIndex > endPointIndex) - Math.PI) / Math.PI * 180); }