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);
}
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);
}

View file

@ -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<GpxPoint> gpxPoints, ResultMatcher<GpxRouteApproximation> 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<LatLon> 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);
}
}

View file

@ -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<GpxRouteApproximation> 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) {