diff --git a/brouter-core/src/main/java/btools/router/RoutingContext.java b/brouter-core/src/main/java/btools/router/RoutingContext.java index 2559f3a..8e03e96 100644 --- a/brouter-core/src/main/java/btools/router/RoutingContext.java +++ b/brouter-core/src/main/java/btools/router/RoutingContext.java @@ -191,6 +191,9 @@ public final class RoutingContext { public Integer startDirection; public boolean startDirectionValid; public boolean forceUseStartDirection; + public Integer roundtripDistance; + public Integer roundtripDirectionAdd; + public boolean allowSamewayback; public CheapAngleMeter anglemeter = new CheapAngleMeter(); diff --git a/brouter-core/src/main/java/btools/router/RoutingEngine.java b/brouter-core/src/main/java/btools/router/RoutingEngine.java index 14fa36f..9e74244 100644 --- a/brouter-core/src/main/java/btools/router/RoutingEngine.java +++ b/brouter-core/src/main/java/btools/router/RoutingEngine.java @@ -21,6 +21,7 @@ import btools.mapaccess.OsmLinkHolder; import btools.mapaccess.OsmNode; import btools.mapaccess.OsmNodePairSet; import btools.mapaccess.OsmPos; +import btools.util.CheapRuler; import btools.util.CompactLongMap; import btools.util.SortedHeap; import btools.util.StackSampler; @@ -31,6 +32,7 @@ public class RoutingEngine extends Thread { public final static int BROUTER_ENGINEMODE_SEED = 1; public final static int BROUTER_ENGINEMODE_GETELEV = 2; public final static int BROUTER_ENGINEMODE_GETINFO = 3; + public final static int BROUTER_ENGINEMODE_ROUNDTRIP = 4; private NodesCache nodesCache; private SortedHeap openSet = new SortedHeap<>(); @@ -48,7 +50,7 @@ public class RoutingEngine extends Thread { private int engineMode = 0; - private int MAX_STEPS_CHECK = 10; + private int MAX_STEPS_CHECK = 20; private int MAX_DYNAMIC_RANGE = 60000; @@ -178,6 +180,11 @@ public class RoutingEngine extends Thread { } doGetInfo(); break; + case BROUTER_ENGINEMODE_ROUNDTRIP: + if (waypoints.size() < 1) + throw new IllegalArgumentException("we need one lat/lon point at least!"); + doRoundTrip(); + break; default: throw new IllegalArgumentException("not a valid engine mode"); } @@ -447,6 +454,69 @@ public class RoutingEngine extends Thread { } } + public void doRoundTrip() { + try { + startTime = System.currentTimeMillis(); + MatchedWaypoint wpt1 = new MatchedWaypoint(); + wpt1.waypoint = waypoints.get(0); + wpt1.name = "roundtrip"; + + routingContext.useDynamicDistance = true; + double searchRadius = (routingContext.roundtripDistance == null ? 1500 :routingContext.roundtripDistance); + double direction = (routingContext.startDirection == null ? -1 :routingContext.startDirection); + if (direction == -1) direction = (int) (Math.random()*360); + double directionAdd = (routingContext.roundtripDirectionAdd == null ? 20 :routingContext.roundtripDirectionAdd); + + //direction = 59; + if (routingContext.allowSamewayback) { + int[] pos = CheapRuler.destination(waypoints.get(0).ilon, waypoints.get(0).ilat, searchRadius, direction); + MatchedWaypoint wpt2 = new MatchedWaypoint(); + wpt2.waypoint = new OsmNode(pos[0], pos[1]); + wpt2.name = "rt1_" + (direction); + + OsmNodeNamed onn = new OsmNodeNamed(new OsmNode(pos[0], pos[1])); + onn.name = "via1"; + onn.radius = 1000; + waypoints.add(onn); + } else { + int[] pos = CheapRuler.destination(waypoints.get(0).ilon, waypoints.get(0).ilat, searchRadius, direction-directionAdd); + MatchedWaypoint wpt2 = new MatchedWaypoint(); + wpt2.waypoint = new OsmNode(pos[0], pos[1]); + wpt2.name = "rt1_" + (direction); + + OsmNodeNamed onn = new OsmNodeNamed(new OsmNode(pos[0], pos[1])); + onn.name = "via1"; + onn.radius = 1000; + waypoints.add(onn); + + + pos = CheapRuler.destination(waypoints.get(0).ilon, waypoints.get(0).ilat, searchRadius, direction+directionAdd); + MatchedWaypoint wpt3 = new MatchedWaypoint(); + wpt3.waypoint = new OsmNode(pos[0], pos[1]); + wpt3.name = "rt2_" + (direction); + + onn = new OsmNodeNamed(new OsmNode(pos[0], pos[1])); + onn.name = "via2"; + onn.radius = 1000; + waypoints.add(onn); + } + OsmNodeNamed onn = new OsmNodeNamed(waypoints.get(0)); + onn.name = "rt_end"; + waypoints.add(onn); + + routingContext.waypointCatchingRange = 1000; + + doRouting(0); + + long endTime = System.currentTimeMillis(); + logInfo("execution time = " + (endTime - startTime) / 1000. + " seconds"); + } catch (Exception e) { + e.getStackTrace(); + logException(e); + } + + } + private void postElevationCheck(OsmTrack track) { OsmPathElement lastPt = null; OsmPathElement startPt = null; diff --git a/brouter-core/src/main/java/btools/router/RoutingParamCollector.java b/brouter-core/src/main/java/btools/router/RoutingParamCollector.java index b8d7f6b..a169f07 100644 --- a/brouter-core/src/main/java/btools/router/RoutingParamCollector.java +++ b/brouter-core/src/main/java/btools/router/RoutingParamCollector.java @@ -202,6 +202,12 @@ public class RoutingParamCollector { rctx.forceUseStartDirection = true; } else if (key.equals("direction")) { rctx.startDirection = Integer.valueOf(value); + } else if (key.equals("roundtripDistance")) { + rctx.roundtripDistance = Integer.valueOf(value); + } else if (key.equals("roundtripDirectionAdd")) { + rctx.roundtripDirectionAdd = Integer.valueOf(value); + } else if (key.equals("allowSamewayback")) { + rctx.allowSamewayback = Integer.parseInt(value)==1; } else if (key.equals("alternativeidx")) { rctx.setAlternativeIdx(Integer.parseInt(value)); } else if (key.equals("turnInstructionMode")) {