mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-08-31 23:53:25 +00:00
refine routing controls interactions
This commit is contained in:
@@ -1360,7 +1360,13 @@ export class GPXStatistics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const earthRadius = 6371008.8;
|
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 rad = Math.PI / 180;
|
||||||
const lat1 = coord1.lat * rad;
|
const lat1 = coord1.lat * rad;
|
||||||
const lat2 = coord2.lat * rad;
|
const lat2 = coord2.lat * rad;
|
||||||
|
@@ -334,14 +334,14 @@ export class RoutingControls {
|
|||||||
let file = get(this.file)?.file;
|
let file = get(this.file)?.file;
|
||||||
|
|
||||||
// Find the point closest to the temporary anchor
|
// 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;
|
let minAnchor = this.temporaryAnchor as Anchor;
|
||||||
file?.forEachSegment((segment, trackIndex, segmentIndex) => {
|
file?.forEachSegment((segment, trackIndex, segmentIndex) => {
|
||||||
if (get(selection).hasAnyParent(new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex))) {
|
if (get(selection).hasAnyParent(new ListTrackSegmentItem(this.fileId, trackIndex, segmentIndex))) {
|
||||||
let details: any = {};
|
let details: any = {};
|
||||||
let closest = getClosestLinePoint(segment.trkpt, this.temporaryAnchor.point, details);
|
let closest = getClosestLinePoint(segment.trkpt, this.temporaryAnchor.point, details);
|
||||||
if (details.distance < minDistance) {
|
if (details.distance < minDetails.distance) {
|
||||||
minDistance = details.distance;
|
minDetails = details;
|
||||||
minAnchor = {
|
minAnchor = {
|
||||||
point: closest,
|
point: closest,
|
||||||
segment,
|
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;
|
return minAnchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -5,11 +5,10 @@ import type { TransitionConfig } from "svelte/transition";
|
|||||||
import { get } from "svelte/store";
|
import { get } from "svelte/store";
|
||||||
import { map } from "./stores";
|
import { map } from "./stores";
|
||||||
import { base } from "$app/paths";
|
import { base } from "$app/paths";
|
||||||
import { browser } from "$app/environment";
|
|
||||||
import { languages } from "$lib/languages";
|
import { languages } from "$lib/languages";
|
||||||
import { locale } from "svelte-i18n";
|
import { locale, t } from "svelte-i18n";
|
||||||
import type mapboxgl from "mapbox-gl";
|
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[]) {
|
export function cn(...inputs: ClassValue[]) {
|
||||||
return twMerge(clsx(inputs));
|
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++) {
|
for (let i = 0; i < points.length - 1; i++) {
|
||||||
let dist = crossarcDistance(points[i], points[i + 1], point);
|
let dist = crossarcDistance(points[i], points[i + 1], point);
|
||||||
if (dist < closestDist) {
|
if (dist < closestDist) {
|
||||||
closest = points[i];
|
|
||||||
closestDist = dist;
|
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;
|
details['distance'] = closestDist;
|
||||||
|
Reference in New Issue
Block a user