From f944ef5e6452ca1840f800cc6c47a5c40047523b Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 9 Sep 2020 18:45:06 +0200 Subject: [PATCH] Fix progress bar gpx approximation --- .../router/RouteCalculationProgress.java | 6 ++-- .../osmand/router/RoutePlannerFrontEnd.java | 29 +++++++++---------- .../osmand/plus/routing/GpxApproximator.java | 8 ++--- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RouteCalculationProgress.java b/OsmAnd-java/src/main/java/net/osmand/router/RouteCalculationProgress.java index eb93e95699..8bde5fb534 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteCalculationProgress.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteCalculationProgress.java @@ -49,14 +49,16 @@ public class RouteCalculationProgress { pr = Math.min(p * p / (all * all), 1); } float progress = INITIAL_PROGRESS; - if (totalIterations > 1) { + if (totalIterations <= 1) { + progress = INITIAL_PROGRESS + pr * (1 - INITIAL_PROGRESS); + } else if (totalIterations <= 2) { if (iteration < 1) { progress = pr * FIRST_ITERATION + INITIAL_PROGRESS; } else { progress = (INITIAL_PROGRESS + FIRST_ITERATION) + pr * (1 - FIRST_ITERATION - INITIAL_PROGRESS); } } else { - progress = INITIAL_PROGRESS + pr * (1 - INITIAL_PROGRESS); + progress = (iteration + Math.min(pr, 1)) / totalIterations; } return Math.min(progress * 100f, 99); } diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java b/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java index f284a205aa..5aefb2ac44 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java @@ -64,8 +64,6 @@ public class RoutePlannerFrontEnd { public int routeGapDistance; public int routeDistanceUnmatched; - public boolean calculationCancelled; - private boolean calculationDone; public GpxRouteApproximation(RoutingContext ctx) { this.ctx = ctx; @@ -82,10 +80,6 @@ public class RoutePlannerFrontEnd { routeCalculations, routeDistCalculations, routePointsSearched, routeDistance, routeDistanceUnmatched); } - public boolean isCalculationDone() { - return calculationDone; - } - public double distFromLastPoint(LatLon startPoint) { if (result.size() > 0) { return MapUtils.getDistance(getLastPoint(), startPoint); @@ -239,18 +233,22 @@ public class RoutePlannerFrontEnd { public GpxRouteApproximation searchGpxRoute(GpxRouteApproximation gctx, List gpxPoints, ResultMatcher resultMatcher) throws IOException, InterruptedException { long timeToCalculate = System.nanoTime(); gctx.ctx.keepNativeRoutingContext = true; + if (gctx.ctx.calculationProgress == null) { + gctx.ctx.calculationProgress = new RouteCalculationProgress(); + } GpxPoint start = null; GpxPoint prev = null; if (gpxPoints.size() > 0) { gctx.ctx.calculationProgress.totalIterations = (int) (gpxPoints.get(gpxPoints.size() - 1).cumDist / gctx.MAXIMUM_STEP_APPROXIMATION + 1); start = gpxPoints.get(0); } - while (start != null && !gctx.calculationCancelled) { + while (start != null && !gctx.ctx.calculationProgress.isCancelled) { double routeDist = gctx.MAXIMUM_STEP_APPROXIMATION; GpxPoint next = findNextGpxPointWithin(gctx, gpxPoints, start, routeDist); boolean routeFound = false; - gctx.ctx.calculationProgress.nextIteration(); if (next != null && initRoutingPoint(start, gctx, gctx.MINIMUM_POINT_APPROXIMATION)) { + gctx.ctx.calculationProgress.totalEstimatedDistance = 0; + gctx.ctx.calculationProgress.iteration = (int) (next.cumDist / gctx.MAXIMUM_STEP_APPROXIMATION); while (routeDist >= gctx.MINIMUM_STEP_APPROXIMATION && !routeFound) { routeFound = initRoutingPoint(next, gctx, gctx.MINIMUM_POINT_APPROXIMATION); if (routeFound) { @@ -299,20 +297,19 @@ public class RoutePlannerFrontEnd { } start = next; } - if(gctx.ctx.calculationProgress != null) { - gctx.ctx.calculationProgress.timeToCalculate = System.nanoTime() - timeToCalculate; + if (gctx.ctx.calculationProgress != null) { + gctx.ctx.calculationProgress.timeToCalculate = System.nanoTime() - timeToCalculate; } gctx.ctx.deleteNativeRoutingContext(); BinaryRoutePlanner.printDebugMemoryInformation(gctx.ctx); calculateGpxRoute(gctx, gpxPoints); - if (!gctx.result.isEmpty() && !gctx.calculationCancelled) { + if (!gctx.result.isEmpty() && !gctx.ctx.calculationProgress.isCancelled) { new RouteResultPreparation().printResults(gctx.ctx, gpxPoints.get(0).loc, gpxPoints.get(gpxPoints.size() - 1).loc, gctx.result); System.out.println(gctx); } if (resultMatcher != null) { - resultMatcher.publish(gctx.calculationCancelled ? null : gctx); + resultMatcher.publish(gctx.ctx.calculationProgress.isCancelled ? null : gctx); } - gctx.calculationDone = true; return gctx; } @@ -365,7 +362,7 @@ public class RoutePlannerFrontEnd { reg.initRouteEncodingRule(0, "highway", RouteResultPreparation.UNMATCHED_HIGHWAY_TYPE); List lastStraightLine = null; GpxPoint straightPointStart = null; - for (int i = 0; i < gpxPoints.size() && !gctx.calculationCancelled; ) { + for (int i = 0; i < gpxPoints.size() && !gctx.ctx.calculationProgress.isCancelled; ) { GpxPoint pnt = gpxPoints.get(i); if (pnt.routeToTarget != null && !pnt.routeToTarget.isEmpty()) { LatLon startPoint = pnt.routeToTarget.get(0).getStartPoint(); @@ -459,7 +456,7 @@ public class RoutePlannerFrontEnd { private void cleanupResultAndAddTurns(GpxRouteApproximation gctx) { // cleanup double joints int LOOK_AHEAD = 4; - for(int i = 0; i < gctx.result.size() && !gctx.calculationCancelled; i++) { + for(int i = 0; i < gctx.result.size() && !gctx.ctx.calculationProgress.isCancelled; i++) { RouteSegmentResult s = gctx.result.get(i); for(int j = i + 2; j <= i + LOOK_AHEAD && j < gctx.result.size(); j++) { RouteSegmentResult e = gctx.result.get(j); @@ -476,7 +473,7 @@ public class RoutePlannerFrontEnd { r.setTurnType(null); r.setDescription(""); } - if (!gctx.calculationCancelled) { + if (!gctx.ctx.calculationProgress.isCancelled) { preparation.prepareTurnResults(gctx.ctx, gctx.result); } } diff --git a/OsmAnd/src/net/osmand/plus/routing/GpxApproximator.java b/OsmAnd/src/net/osmand/plus/routing/GpxApproximator.java index a3fb41c2f2..7e6b089620 100644 --- a/OsmAnd/src/net/osmand/plus/routing/GpxApproximator.java +++ b/OsmAnd/src/net/osmand/plus/routing/GpxApproximator.java @@ -140,13 +140,13 @@ public class GpxApproximator { public void cancelApproximation() { if (gctx != null) { - gctx.calculationCancelled = true; + gctx.ctx.calculationProgress.isCancelled = true; } } public void calculateGpxApproximation(@NonNull final ResultMatcher resultMatcher) { if (gctx != null) { - gctx.calculationCancelled = true; + gctx.ctx.calculationProgress.isCancelled = true; } final GpxRouteApproximation gctx = getNewGpxApproximationContext(this.gctx); this.gctx = gctx; @@ -187,10 +187,10 @@ public class GpxApproximator { @Override public void run() { RouteCalculationProgress calculationProgress = gctx.ctx.calculationProgress; - if (gctx.isCalculationDone() && GpxApproximator.this.gctx == gctx) { + if (!gctx.result.isEmpty() && GpxApproximator.this.gctx == gctx) { finishProgress(); } - if (!gctx.isCalculationDone() && calculationProgress != null && !calculationProgress.isCancelled) { + if (gctx.result.isEmpty() && calculationProgress != null && !calculationProgress.isCancelled) { float pr = calculationProgress.getLinearProgress(); approximationProgress.updateProgress((int) pr); if (GpxApproximator.this.gctx != gctx) {