From fc79805bf3c33f97fd993909d5288c214e83c91e Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 29 Aug 2016 00:45:21 +0200 Subject: [PATCH] Fix complex turns --- .../osmand/router/RouteResultPreparation.java | 57 ++++++++++++++----- .../src/net/osmand/router/TurnType.java | 13 +++++ .../router/RouteResultPreparationTest.java | 2 +- .../test/resources/test_turn_lanes.json | 2 +- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java b/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java index 354ff7f4e9..edb5a9a823 100644 --- a/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java +++ b/OsmAnd-java/src/net/osmand/router/RouteResultPreparation.java @@ -667,17 +667,25 @@ public class RouteResultPreparation { } } } - int singleTurn ; + int singleTurn = 0; if(turnSet.size() == 1) { singleTurn = turnSet.iterator().next(); - } else if(currentTurn.goAhead() && nextTurn.goAhead()) { - singleTurn = currentTurn.getValue(); - } else if(currentTurn.goAhead() && turnSet.contains(nextTurn.getValue()) && - (currentTurn.isPossibleLeftTurn() && TurnType.isLeftTurn(nextTurn.getValue()) ) || - (currentTurn.isPossibleRightTurn() && TurnType.isRightTurn(nextTurn.getValue())) - ) { - singleTurn = nextTurn.getValue(); - } else { + } else if(currentTurn.goAhead() && turnSet.contains(nextTurn.getValue())) { + if(currentTurn.isPossibleLeftTurn() && + TurnType.isLeftTurn(nextTurn.getValue())) { + singleTurn = nextTurn.getValue(); + } else if(currentTurn.isPossibleLeftTurn() && + TurnType.isLeftTurn(nextTurn.getActiveCommonLaneTurn())) { + singleTurn = nextTurn.getActiveCommonLaneTurn(); + } else if(currentTurn.isPossibleRightTurn() && + TurnType.isRightTurn(nextTurn.getValue())) { + singleTurn = nextTurn.getValue(); + } else if(currentTurn.isPossibleRightTurn() && + TurnType.isRightTurn(nextTurn.getActiveCommonLaneTurn())) { + singleTurn = nextTurn.getActiveCommonLaneTurn(); + } + } + if (singleTurn == 0) { singleTurn = currentTurn.getValue(); } for(int i = 0; i < lanes.length; i++) { @@ -924,6 +932,22 @@ public class RouteResultPreparation { // Maybe going straight at a 90-degree intersection TurnType t = TurnType.valueOf(TurnType.C, leftSide); int[] rawLanes = calculateRawTurnLanes(turnLanes, TurnType.C); + boolean possiblyLeftTurn = rs.roadsOnLeft == 0; + boolean possiblyRightTurn = rs.roadsOnRight == 0; + for (int k = 0; k < rawLanes.length; k++) { + int turn = TurnType.getPrimaryTurn(rawLanes[k]); + int sturn = TurnType.getSecondaryTurn(rawLanes[k]); + int tturn = TurnType.getTertiaryTurn(rawLanes[k]); + if (turn == TurnType.TU || sturn == TurnType.TU || tturn == TurnType.TU) { + possiblyLeftTurn = true; + } + if (turn == TurnType.TRU || sturn == TurnType.TRU || sturn == TurnType.TRU) { + possiblyRightTurn = true; + } + } + + t.setPossibleLeftTurn(possiblyLeftTurn); + t.setPossibleRightTurn(possiblyRightTurn); if (rs.keepLeft || rs.keepRight) { String[] splitLaneOptions = turnLanes.split("\\|", -1); int activeBeginIndex = findActiveIndex(rawLanes, splitLaneOptions, rs.leftLanes, true, @@ -944,13 +968,11 @@ public class RouteResultPreparation { t = TurnType.valueOf(tp, leftSide); } } else { - boolean possiblyLeftTurn = rs.roadsOnLeft == 0; - boolean possiblyRightTurn = rs.roadsOnRight == 0; - t.setPossibleLeftTurn(possiblyLeftTurn); - t.setPossibleRightTurn(possiblyRightTurn); for (int k = 0; k < rawLanes.length; k++) { int turn = TurnType.getPrimaryTurn(rawLanes[k]); int sturn = TurnType.getSecondaryTurn(rawLanes[k]); + int tturn = TurnType.getTertiaryTurn(rawLanes[k]); + boolean active = false; // some turns go through many segments (to turn right or left) // so on one first segment the lane could be available and may be only 1 possible @@ -959,8 +981,13 @@ public class RouteResultPreparation { (TurnType.isLeftTurn(sturn) && possiblyLeftTurn)) { // we can't predict here whether it will be a left turn or straight on, // it could be done during 2nd pass -// TurnType.setPrimaryTurn(rawLanes, k, sturn); -// TurnType.setSecondaryTurn(rawLanes, k, turn); + TurnType.setPrimaryTurn(rawLanes, k, sturn); + TurnType.setSecondaryTurn(rawLanes, k, turn); + active = true; + } else if((TurnType.isRightTurn(tturn) && possiblyRightTurn) || + (TurnType.isLeftTurn(tturn) && possiblyLeftTurn)) { + TurnType.setPrimaryTurn(rawLanes, k, tturn); + TurnType.setTertiaryTurn(rawLanes, k, turn); active = true; } else if((TurnType.isRightTurn(turn) && possiblyRightTurn) || (TurnType.isLeftTurn(turn) && possiblyLeftTurn)) { diff --git a/OsmAnd-java/src/net/osmand/router/TurnType.java b/OsmAnd-java/src/net/osmand/router/TurnType.java index 0bac7754a4..332ed2a86f 100644 --- a/OsmAnd-java/src/net/osmand/router/TurnType.java +++ b/OsmAnd-java/src/net/osmand/router/TurnType.java @@ -1,5 +1,6 @@ package net.osmand.router; +import gnu.trove.list.array.TIntArrayList; import gnu.trove.set.hash.TIntHashSet; public class TurnType { @@ -22,6 +23,18 @@ public class TurnType { return valueOf(C, false); } + public int getActiveCommonLaneTurn() { + if(lanes == null || lanes.length == 0) { + return C; + } + for(int i = 0; i < lanes.length; i++) { + if(lanes[i] % 2 == 1) { + return TurnType.getPrimaryTurn(lanes[i]); + } + } + return C; + } + public String toXmlString() { switch (value) { case C: diff --git a/OsmAnd-java/test/java/net/osmand/router/RouteResultPreparationTest.java b/OsmAnd-java/test/java/net/osmand/router/RouteResultPreparationTest.java index 38ae193b32..fcdf2ce21e 100644 --- a/OsmAnd-java/test/java/net/osmand/router/RouteResultPreparationTest.java +++ b/OsmAnd-java/test/java/net/osmand/router/RouteResultPreparationTest.java @@ -103,7 +103,7 @@ public class RouteResultPreparationTest { if(!Algorithms.objectEquals(expectedResult, turnLanes) && !Algorithms.objectEquals(expectedResult, lanes) && !Algorithms.objectEquals(expectedResult, turn)) { - Assert.assertEquals("Segment " + segmentId, turnLanes, expectedResult); + Assert.assertEquals("Segment " + segmentId, expectedResult, turnLanes); } } diff --git a/OsmAnd-java/test/resources/test_turn_lanes.json b/OsmAnd-java/test/resources/test_turn_lanes.json index 05e105b467..235f06d834 100644 --- a/OsmAnd-java/test/resources/test_turn_lanes.json +++ b/OsmAnd-java/test/resources/test_turn_lanes.json @@ -324,7 +324,7 @@ }, "expectedResults": { "96306": null, - "96296": "TU:+C|C|C|C|C" + "96296": "TU:C|C|C|C|C" } }, {