Fix progress bar gpx approximation

This commit is contained in:
Victor Shcherb 2020-09-09 18:45:06 +02:00
parent be1fc66bff
commit f944ef5e64
3 changed files with 21 additions and 22 deletions

View file

@ -49,14 +49,16 @@ public class RouteCalculationProgress {
pr = Math.min(p * p / (all * all), 1); pr = Math.min(p * p / (all * all), 1);
} }
float progress = INITIAL_PROGRESS; float progress = INITIAL_PROGRESS;
if (totalIterations > 1) { if (totalIterations <= 1) {
progress = INITIAL_PROGRESS + pr * (1 - INITIAL_PROGRESS);
} else if (totalIterations <= 2) {
if (iteration < 1) { if (iteration < 1) {
progress = pr * FIRST_ITERATION + INITIAL_PROGRESS; progress = pr * FIRST_ITERATION + INITIAL_PROGRESS;
} else { } else {
progress = (INITIAL_PROGRESS + FIRST_ITERATION) + pr * (1 - FIRST_ITERATION - INITIAL_PROGRESS); progress = (INITIAL_PROGRESS + FIRST_ITERATION) + pr * (1 - FIRST_ITERATION - INITIAL_PROGRESS);
} }
} else { } else {
progress = INITIAL_PROGRESS + pr * (1 - INITIAL_PROGRESS); progress = (iteration + Math.min(pr, 1)) / totalIterations;
} }
return Math.min(progress * 100f, 99); return Math.min(progress * 100f, 99);
} }

View file

@ -64,8 +64,6 @@ public class RoutePlannerFrontEnd {
public int routeGapDistance; public int routeGapDistance;
public int routeDistanceUnmatched; public int routeDistanceUnmatched;
public boolean calculationCancelled;
private boolean calculationDone;
public GpxRouteApproximation(RoutingContext ctx) { public GpxRouteApproximation(RoutingContext ctx) {
this.ctx = ctx; this.ctx = ctx;
@ -82,10 +80,6 @@ public class RoutePlannerFrontEnd {
routeCalculations, routeDistCalculations, routePointsSearched, routeDistance, routeDistanceUnmatched); routeCalculations, routeDistCalculations, routePointsSearched, routeDistance, routeDistanceUnmatched);
} }
public boolean isCalculationDone() {
return calculationDone;
}
public double distFromLastPoint(LatLon startPoint) { public double distFromLastPoint(LatLon startPoint) {
if (result.size() > 0) { if (result.size() > 0) {
return MapUtils.getDistance(getLastPoint(), startPoint); return MapUtils.getDistance(getLastPoint(), startPoint);
@ -239,18 +233,22 @@ public class RoutePlannerFrontEnd {
public GpxRouteApproximation searchGpxRoute(GpxRouteApproximation gctx, List<GpxPoint> gpxPoints, ResultMatcher<GpxRouteApproximation> resultMatcher) throws IOException, InterruptedException { public GpxRouteApproximation searchGpxRoute(GpxRouteApproximation gctx, List<GpxPoint> gpxPoints, ResultMatcher<GpxRouteApproximation> resultMatcher) throws IOException, InterruptedException {
long timeToCalculate = System.nanoTime(); long timeToCalculate = System.nanoTime();
gctx.ctx.keepNativeRoutingContext = true; gctx.ctx.keepNativeRoutingContext = true;
if (gctx.ctx.calculationProgress == null) {
gctx.ctx.calculationProgress = new RouteCalculationProgress();
}
GpxPoint start = null; GpxPoint start = null;
GpxPoint prev = null; GpxPoint prev = null;
if (gpxPoints.size() > 0) { if (gpxPoints.size() > 0) {
gctx.ctx.calculationProgress.totalIterations = (int) (gpxPoints.get(gpxPoints.size() - 1).cumDist / gctx.MAXIMUM_STEP_APPROXIMATION + 1); gctx.ctx.calculationProgress.totalIterations = (int) (gpxPoints.get(gpxPoints.size() - 1).cumDist / gctx.MAXIMUM_STEP_APPROXIMATION + 1);
start = gpxPoints.get(0); start = gpxPoints.get(0);
} }
while (start != null && !gctx.calculationCancelled) { while (start != null && !gctx.ctx.calculationProgress.isCancelled) {
double routeDist = gctx.MAXIMUM_STEP_APPROXIMATION; double routeDist = gctx.MAXIMUM_STEP_APPROXIMATION;
GpxPoint next = findNextGpxPointWithin(gctx, gpxPoints, start, routeDist); GpxPoint next = findNextGpxPointWithin(gctx, gpxPoints, start, routeDist);
boolean routeFound = false; boolean routeFound = false;
gctx.ctx.calculationProgress.nextIteration();
if (next != null && initRoutingPoint(start, gctx, gctx.MINIMUM_POINT_APPROXIMATION)) { 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) { while (routeDist >= gctx.MINIMUM_STEP_APPROXIMATION && !routeFound) {
routeFound = initRoutingPoint(next, gctx, gctx.MINIMUM_POINT_APPROXIMATION); routeFound = initRoutingPoint(next, gctx, gctx.MINIMUM_POINT_APPROXIMATION);
if (routeFound) { if (routeFound) {
@ -299,20 +297,19 @@ public class RoutePlannerFrontEnd {
} }
start = next; start = next;
} }
if(gctx.ctx.calculationProgress != null) { if (gctx.ctx.calculationProgress != null) {
gctx.ctx.calculationProgress.timeToCalculate = System.nanoTime() - timeToCalculate; gctx.ctx.calculationProgress.timeToCalculate = System.nanoTime() - timeToCalculate;
} }
gctx.ctx.deleteNativeRoutingContext(); gctx.ctx.deleteNativeRoutingContext();
BinaryRoutePlanner.printDebugMemoryInformation(gctx.ctx); BinaryRoutePlanner.printDebugMemoryInformation(gctx.ctx);
calculateGpxRoute(gctx, gpxPoints); 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); new RouteResultPreparation().printResults(gctx.ctx, gpxPoints.get(0).loc, gpxPoints.get(gpxPoints.size() - 1).loc, gctx.result);
System.out.println(gctx); System.out.println(gctx);
} }
if (resultMatcher != null) { if (resultMatcher != null) {
resultMatcher.publish(gctx.calculationCancelled ? null : gctx); resultMatcher.publish(gctx.ctx.calculationProgress.isCancelled ? null : gctx);
} }
gctx.calculationDone = true;
return gctx; return gctx;
} }
@ -365,7 +362,7 @@ public class RoutePlannerFrontEnd {
reg.initRouteEncodingRule(0, "highway", RouteResultPreparation.UNMATCHED_HIGHWAY_TYPE); reg.initRouteEncodingRule(0, "highway", RouteResultPreparation.UNMATCHED_HIGHWAY_TYPE);
List<LatLon> lastStraightLine = null; List<LatLon> lastStraightLine = null;
GpxPoint straightPointStart = 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); GpxPoint pnt = gpxPoints.get(i);
if (pnt.routeToTarget != null && !pnt.routeToTarget.isEmpty()) { if (pnt.routeToTarget != null && !pnt.routeToTarget.isEmpty()) {
LatLon startPoint = pnt.routeToTarget.get(0).getStartPoint(); LatLon startPoint = pnt.routeToTarget.get(0).getStartPoint();
@ -459,7 +456,7 @@ public class RoutePlannerFrontEnd {
private void cleanupResultAndAddTurns(GpxRouteApproximation gctx) { private void cleanupResultAndAddTurns(GpxRouteApproximation gctx) {
// cleanup double joints // cleanup double joints
int LOOK_AHEAD = 4; 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); RouteSegmentResult s = gctx.result.get(i);
for(int j = i + 2; j <= i + LOOK_AHEAD && j < gctx.result.size(); j++) { for(int j = i + 2; j <= i + LOOK_AHEAD && j < gctx.result.size(); j++) {
RouteSegmentResult e = gctx.result.get(j); RouteSegmentResult e = gctx.result.get(j);
@ -476,7 +473,7 @@ public class RoutePlannerFrontEnd {
r.setTurnType(null); r.setTurnType(null);
r.setDescription(""); r.setDescription("");
} }
if (!gctx.calculationCancelled) { if (!gctx.ctx.calculationProgress.isCancelled) {
preparation.prepareTurnResults(gctx.ctx, gctx.result); preparation.prepareTurnResults(gctx.ctx, gctx.result);
} }
} }

View file

@ -140,13 +140,13 @@ public class GpxApproximator {
public void cancelApproximation() { public void cancelApproximation() {
if (gctx != null) { if (gctx != null) {
gctx.calculationCancelled = true; gctx.ctx.calculationProgress.isCancelled = true;
} }
} }
public void calculateGpxApproximation(@NonNull final ResultMatcher<GpxRouteApproximation> resultMatcher) { public void calculateGpxApproximation(@NonNull final ResultMatcher<GpxRouteApproximation> resultMatcher) {
if (gctx != null) { if (gctx != null) {
gctx.calculationCancelled = true; gctx.ctx.calculationProgress.isCancelled = true;
} }
final GpxRouteApproximation gctx = getNewGpxApproximationContext(this.gctx); final GpxRouteApproximation gctx = getNewGpxApproximationContext(this.gctx);
this.gctx = gctx; this.gctx = gctx;
@ -187,10 +187,10 @@ public class GpxApproximator {
@Override @Override
public void run() { public void run() {
RouteCalculationProgress calculationProgress = gctx.ctx.calculationProgress; RouteCalculationProgress calculationProgress = gctx.ctx.calculationProgress;
if (gctx.isCalculationDone() && GpxApproximator.this.gctx == gctx) { if (!gctx.result.isEmpty() && GpxApproximator.this.gctx == gctx) {
finishProgress(); finishProgress();
} }
if (!gctx.isCalculationDone() && calculationProgress != null && !calculationProgress.isCancelled) { if (gctx.result.isEmpty() && calculationProgress != null && !calculationProgress.isCancelled) {
float pr = calculationProgress.getLinearProgress(); float pr = calculationProgress.getLinearProgress();
approximationProgress.updateProgress((int) pr); approximationProgress.updateProgress((int) pr);
if (GpxApproximator.this.gctx != gctx) { if (GpxApproximator.this.gctx != gctx) {