From 1561e0c4d259877b04e399b04266b1e8435061bb Mon Sep 17 00:00:00 2001 From: vcoppe Date: Thu, 20 Jun 2024 18:27:22 +0200 Subject: [PATCH] prevent slope NaNs --- gpx/src/gpx.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gpx/src/gpx.ts b/gpx/src/gpx.ts index 73845aad..cd763c69 100644 --- a/gpx/src/gpx.ts +++ b/gpx/src/gpx.ts @@ -688,7 +688,7 @@ export class TrackSegment extends GPXTreeLeaf { _computeSlope(): number[] { const points = this.trkpt; - return distanceWindowSmoothingWithDistanceAccumulator(points, 50, (accumulated, start, end) => 100 * (points[end].ele - points[start].ele) / accumulated); + return distanceWindowSmoothingWithDistanceAccumulator(points, 50, (accumulated, start, end) => 100 * ((points[end].ele ?? 0) - (points[start].ele ?? 0)) / (accumulated > 0 ? accumulated : 1)); } getNumberOfTrackPoints(): number { @@ -1159,7 +1159,7 @@ function distanceWindowSmoothing(points: ReadonlyArray>, di let start = 0, end = 0, accumulated = 0; for (var i = 0; i < points.length; i++) { - while (start < i && distance(points[start].getCoordinates(), points[i].getCoordinates()) > distanceWindow) { + while (start + 1 < i && distance(points[start].getCoordinates(), points[i].getCoordinates()) > distanceWindow) { if (remove) { accumulated -= remove(start); } else {