refine routing controls interactions

This commit is contained in:
vcoppe
2024-08-22 10:41:04 +02:00
parent 1a4ae96782
commit b5fd8ea09b
3 changed files with 28 additions and 8 deletions

View File

@@ -1360,7 +1360,13 @@ export class GPXStatistics {
}
const earthRadius = 6371008.8;
export function distance(coord1: Coordinates, coord2: Coordinates): number {
export function distance(coord1: TrackPoint | Coordinates, coord2: TrackPoint | Coordinates): number {
if (coord1 instanceof TrackPoint) {
coord1 = coord1.getCoordinates();
}
if (coord2 instanceof TrackPoint) {
coord2 = coord2.getCoordinates();
}
const rad = Math.PI / 180;
const lat1 = coord1.lat * rad;
const lat2 = coord2.lat * rad;

View File

@@ -334,14 +334,14 @@ export class RoutingControls {
let file = get(this.file)?.file;
// Find the point closest to the temporary anchor
let minDistance = Number.MAX_VALUE;
let minDetails: any = { distance: Number.MAX_VALUE };
let minAnchor = this.temporaryAnchor as Anchor;
file?.forEachSegment((segment, trackIndex, segmentIndex) => {
if (get(selection).hasAnyParent(new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex))) {
let details: any = {};
let closest = getClosestLinePoint(segment.trkpt, this.temporaryAnchor.point, details);
if (details.distance < minDistance) {
minDistance = details.distance;
if (details.distance < minDetails.distance) {
minDetails = details;
minAnchor = {
point: closest,
segment,
@@ -352,6 +352,15 @@ export class RoutingControls {
}
});
if (minAnchor.point._data.anchor) {
minAnchor.point = minAnchor.point.clone();
if (minDetails.before) {
minAnchor.point._data.index = minAnchor.point._data.index + 0.5;
} else {
minAnchor.point._data.index = minAnchor.point._data.index - 0.5;
}
}
return minAnchor;
}

View File

@@ -5,11 +5,10 @@ import type { TransitionConfig } from "svelte/transition";
import { get } from "svelte/store";
import { map } from "./stores";
import { base } from "$app/paths";
import { browser } from "$app/environment";
import { languages } from "$lib/languages";
import { locale } from "svelte-i18n";
import { locale, t } from "svelte-i18n";
import type mapboxgl from "mapbox-gl";
import { type TrackPoint, type Coordinates, crossarcDistance } from "gpx";
import { type TrackPoint, type Coordinates, crossarcDistance, distance } from "gpx";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
@@ -75,8 +74,14 @@ export function getClosestLinePoint(points: TrackPoint[], point: TrackPoint | Co
for (let i = 0; i < points.length - 1; i++) {
let dist = crossarcDistance(points[i], points[i + 1], point);
if (dist < closestDist) {
closest = points[i];
closestDist = dist;
if (distance(points[i], point) <= distance(points[i + 1], point)) {
closest = points[i];
details['before'] = true;
} else {
closest = points[i + 1];
details['before'] = false;
}
}
}
details['distance'] = closestDist;