simplify slope segment

This commit is contained in:
vcoppe
2024-06-28 18:40:43 +02:00
parent f2014f53a1
commit cbc5c05807
3 changed files with 11 additions and 17 deletions

View File

@@ -692,10 +692,6 @@ export class TrackSegment extends GPXTreeLeaf {
} }
_computeSlopeSegments(statistics: GPXStatistics): [number[], number[]] { _computeSlopeSegments(statistics: GPXStatistics): [number[], number[]] {
function canSplit(point1: TrackPoint, point2: TrackPoint, point3: TrackPoint): boolean {
return statistics.local.distance.total[point3._data.index] - statistics.local.distance.total[point1._data.index] >= 0.5 && statistics.local.distance.total[point2._data.index] - statistics.local.distance.total[point3._data.index] >= 0.5;
}
// x-coordinates are given by: statistics.local.distance.total[point._data.index] * 1000 // x-coordinates are given by: statistics.local.distance.total[point._data.index] * 1000
// y-coordinates are given by: point.ele // y-coordinates are given by: point.ele
// Compute the distance between point3 and the line defined by point1 and point2 // Compute the distance between point3 and the line defined by point1 and point2
@@ -718,7 +714,7 @@ export class TrackSegment extends GPXTreeLeaf {
return Math.abs((y2 - y1) * x3 - (x2 - x1) * y3 + x2 * y1 - y2 * x1) / dist; return Math.abs((y2 - y1) * x3 - (x2 - x1) * y3 + x2 * y1 - y2 * x1) / dist;
} }
let simplified = ramerDouglasPeucker(this.trkpt, 25, elevationDistance, canSplit); let simplified = ramerDouglasPeucker(this.trkpt, 20, elevationDistance);
let slope = []; let slope = [];
let length = []; let length = [];

View File

@@ -5,7 +5,7 @@ export type SimplifiedTrackPoint = { point: TrackPoint, distance?: number };
const earthRadius = 6371008.8; const earthRadius = 6371008.8;
export function ramerDouglasPeucker(points: readonly TrackPoint[], epsilon: number = 50, measure: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => number = computeCrossarc, canSplit: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => boolean = () => true): SimplifiedTrackPoint[] { export function ramerDouglasPeucker(points: readonly TrackPoint[], epsilon: number = 50, measure: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => number = computeCrossarc): SimplifiedTrackPoint[] {
if (points.length == 0) { if (points.length == 0) {
return []; return [];
} else if (points.length == 1) { } else if (points.length == 1) {
@@ -17,33 +17,31 @@ export function ramerDouglasPeucker(points: readonly TrackPoint[], epsilon: numb
let simplified = [{ let simplified = [{
point: points[0] point: points[0]
}]; }];
ramerDouglasPeuckerRecursive(points, epsilon, measure, canSplit, 0, points.length - 1, simplified); ramerDouglasPeuckerRecursive(points, epsilon, measure, 0, points.length - 1, simplified);
simplified.push({ simplified.push({
point: points[points.length - 1] point: points[points.length - 1]
}); });
return simplified; return simplified;
} }
function ramerDouglasPeuckerRecursive(points: readonly TrackPoint[], epsilon: number, measure: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => number, canSplit: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => boolean, start: number, end: number, simplified: SimplifiedTrackPoint[]) { function ramerDouglasPeuckerRecursive(points: readonly TrackPoint[], epsilon: number, measure: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => number, start: number, end: number, simplified: SimplifiedTrackPoint[]) {
let largest = { let largest = {
index: 0, index: 0,
distance: 0 distance: 0
}; };
for (let i = start + 1; i < end; i++) { for (let i = start + 1; i < end; i++) {
if (canSplit(points[start], points[end], points[i])) { let distance = measure(points[start], points[end], points[i]);
let distance = measure(points[start], points[end], points[i]); if (distance > largest.distance) {
if (distance > largest.distance) { largest.index = i;
largest.index = i; largest.distance = distance;
largest.distance = distance;
}
} }
} }
if (largest.distance > epsilon && largest.index != 0) { if (largest.distance > epsilon && largest.index != 0) {
ramerDouglasPeuckerRecursive(points, epsilon, measure, canSplit, start, largest.index, simplified); ramerDouglasPeuckerRecursive(points, epsilon, measure, start, largest.index, simplified);
simplified.push({ point: points[largest.index], distance: largest.distance }); simplified.push({ point: points[largest.index], distance: largest.distance });
ramerDouglasPeuckerRecursive(points, epsilon, measure, canSplit, largest.index, end, simplified); ramerDouglasPeuckerRecursive(points, epsilon, measure, largest.index, end, simplified);
} }
} }

View File

@@ -434,7 +434,7 @@
let hue = ((0.5 - v) * 120).toString(10); let hue = ((0.5 - v) * 120).toString(10);
let lightness = 90 - Math.abs(v) * 70; let lightness = 90 - Math.abs(v) * 70;
return ['hsl(', hue, ',60%,', lightness, '%)'].join(''); return ['hsl(', hue, ',70%,', lightness, '%)'].join('');
} }
function surfaceFillCallback(context) { function surfaceFillCallback(context) {