From 06a547be3ef809f01a2bcc43b4ffc6aeed5fe641 Mon Sep 17 00:00:00 2001 From: seveibar Date: Thu, 9 Jan 2025 16:20:59 -0800 Subject: [PATCH] improve debugging of paths, fix issue with shortcuts not working --- .../remove-path-loops-1.snap.svg | 2 +- .../remove-path-loops-2.snap.svg | 2 +- .../shorten-path-with-shortcuts-1.snap.svg | 2 +- .../shorten-path-with-shortcuts-2.snap.svg | 2 +- .../shorten-path-with-shortcuts-3.snap.svg | 2 +- .../shorten-path-with-shortcuts-4.snap.svg | 2 +- ...n-path-with-shortcuts-5-projected.snap.svg | 2 +- ...rten-path-with-shortcuts-6-normal.snap.svg | 2 +- ...ten-path-with-shortcuts-6-rotated.snap.svg | 2 +- .../tests/fixtures/get-path-comparison-svg.ts | 2 +- .../shorten-path-with-shortcuts-6.test.tsx | 26 +++++++++---------- .../v2/lib/shortenPathWithShortcuts.ts | 6 +++-- 12 files changed, 27 insertions(+), 25 deletions(-) diff --git a/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-1.snap.svg b/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-1.snap.svg index b10f377..bbaba81 100644 --- a/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-1.snap.svg +++ b/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-1.snap.svg @@ -1 +1 @@ -01234012pathWithLoopsimplifiedPath \ No newline at end of file +0 (0.00, 0.00)1 (5.00, 0.00)2 (5.00, 3.00)3 (3.00, 3.00)4 (3.00, -3.00)0 (0.00, 0.00)1 (3.00, 0.00)2 (3.00, -3.00)pathWithLoopsimplifiedPath \ No newline at end of file diff --git a/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-2.snap.svg b/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-2.snap.svg index c92de33..02b8faf 100644 --- a/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-2.snap.svg +++ b/algos/infinite-grid-ijump-astar/tests/__snapshots__/remove-path-loops-2.snap.svg @@ -1 +1 @@ -0123456012345pathWithLoopsimplifiedPath \ No newline at end of file +0 (0.00, 0.00)1 (5.00, 0.00)2 (5.00, 3.00)3 (5.00, 3.00)4 (3.00, 3.00)5 (3.00, 3.00)6 (3.00, -3.00)0 (0.00, 0.00)1 (5.00, 0.00)2 (5.00, 3.00)3 (3.00, 3.00)4 (3.00, 3.00)5 (3.00, -3.00)pathWithLoopsimplifiedPath \ No newline at end of file diff --git a/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-1.snap.svg b/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-1.snap.svg index 53e6d21..cf1681d 100644 --- a/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-1.snap.svg +++ b/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-1.snap.svg @@ -1 +1 @@ -0123450123pathWithLoopsimplifiedPath \ No newline at end of file +0 (0.53, -1.95)1 (0.53, 1.90)2 (0.38, 1.90)3 (0.38, 1.34)4 (-0.53, 1.34)5 (-0.53, 1.90)0 (0.53, -1.95)1 (0.53, 1.34)2 (-0.53, 1.34)3 (-0.53, 1.90)pathWithLoopsimplifiedPath \ No newline at end of file diff --git a/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-2.snap.svg b/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-2.snap.svg index 287af41..82f55f0 100644 --- a/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-2.snap.svg +++ b/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-2.snap.svg @@ -1 +1 @@ -0123450123pathWithLoopsimplifiedPath \ No newline at end of file +0 (-1.95, 0.53)1 (1.90, 0.53)2 (1.90, 0.38)3 (1.34, 0.38)4 (1.34, -0.53)5 (1.90, -0.53)0 (-1.95, 0.53)1 (1.90, 0.53)2 (1.90, -0.53)3 (1.90, -0.53)pathWithLoopsimplifiedPath \ No newline at end of file diff --git a/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-3.snap.svg b/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-3.snap.svg index 05ac2de..723a2d6 100644 --- a/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-3.snap.svg +++ b/algos/infinite-grid-ijump-astar/tests/__snapshots__/shorten-path-with-shortcuts-3.snap.svg @@ -1,4 +1,4 @@ -0123450123450 (0.53, -1.95)1 (0.53, 1.90)2 (0.38, 1.90)3 (0.38, 1.34)4 (-0.53, 1.34)5 (-0.53, 1.90)0 (0.53, -1.95)1 (0.53, 1.90)2 (0.38, 1.90)3 (0.38, 1.34)4 (-0.53, 1.34)5 (-0.53, 1.90)01234567801234560 (-0.60, -0.50)1 (-0.60, -0.45)2 (-0.85, -0.45)3 (-0.85, -0.95)4 (-0.30, -0.95)5 (-0.30, -1.90)6 (5.53, -1.90)7 (5.53, -0.10)8 (5.53, 0.05)0 (-0.60, -0.50)1 (-0.60, -0.45)2 (-0.85, -0.45)3 (-0.85, -1.90)4 (5.53, -1.90)5 (5.53, -0.10)6 (5.53, 0.05)012345670123450 (-0.60, -0.50)1 (-0.60, -0.45)2 (-0.85, -0.45)3 (-0.85, -0.95)4 (-0.30, -0.95)5 (-0.30, -1.90)6 (5.53, -1.90)7 (5.53, -0.10)0 (-0.60, -0.50)1 (-0.60, -0.45)2 (-0.85, -0.45)3 (-0.85, -1.90)4 (5.53, -1.90)5 (5.53, -0.10)0123456012340 (0.53, 0.05)1 (0.80, 0.05)2 (0.80, -1.15)3 (1.20, -1.15)4 (1.20, -0.45)5 (1.40, -0.45)6 (1.40, 0.10)0 (0.53, 0.05)1 (0.80, 0.05)2 (0.80, -0.45)3 (1.40, -0.45)4 (1.40, 0.10)0123456012340 (0.05, 0.53)1 (0.05, 0.80)2 (-1.15, 0.80)3 (-1.15, 1.20)4 (-0.45, 1.20)5 (-0.45, 1.40)6 (0.10, 1.40)0 (0.05, 0.53)1 (0.05, 0.80)2 (-1.15, 0.80)3 (-1.15, 1.40)4 (0.10, 1.40)` - svg += `${index}` + svg += `${index} (${point.x.toFixed(2)}, ${point.y.toFixed(2)})` }) // Add legend item diff --git a/algos/infinite-grid-ijump-astar/tests/shorten-path-with-shortcuts-6.test.tsx b/algos/infinite-grid-ijump-astar/tests/shorten-path-with-shortcuts-6.test.tsx index 66caec2..5663355 100644 --- a/algos/infinite-grid-ijump-astar/tests/shorten-path-with-shortcuts-6.test.tsx +++ b/algos/infinite-grid-ijump-astar/tests/shorten-path-with-shortcuts-6.test.tsx @@ -337,20 +337,20 @@ test("shorten-path-with-shortcuts 6 repro", () => { { x: 1.4000000000000001, y: 0.09999999999999998, layer: "top" }, ] - // const shortenedPath = shortenPathWithShortcuts( - // pathToOptimize as any, - // (A, B) => isObstacleBetweenPoints(A, B, obstacleList), - // ) + const shortenedPath = shortenPathWithShortcuts( + pathToOptimize as any, + (A, B) => isObstacleBetweenPoints(A, B, obstacleList), + ) - // expect( - // getPathComparisonSvg( - // { - // pathWithLoop: pathToOptimize as any, - // simplifiedPath: shortenedPath, - // }, - // obstacles, - // ), - // ).toMatchSvgSnapshot(import.meta.path, "shorten-path-with-shortcuts-6-normal") + expect( + getPathComparisonSvg( + { + pathWithLoop: pathToOptimize as any, + simplifiedPath: shortenedPath, + }, + obstacles, + ), + ).toMatchSvgSnapshot(import.meta.path, "shorten-path-with-shortcuts-6-normal") const rotatedPathToOptimize = pathToOptimize.map((p) => ({ x: p.y, diff --git a/algos/infinite-grid-ijump-astar/v2/lib/shortenPathWithShortcuts.ts b/algos/infinite-grid-ijump-astar/v2/lib/shortenPathWithShortcuts.ts index feb8cf6..5c03ab2 100644 --- a/algos/infinite-grid-ijump-astar/v2/lib/shortenPathWithShortcuts.ts +++ b/algos/infinite-grid-ijump-astar/v2/lib/shortenPathWithShortcuts.ts @@ -1,3 +1,4 @@ +import { current } from "circuit-json" import type { PointWithLayer as Point } from "./GeneralizedAstar" export function shortenPathWithShortcuts( @@ -69,6 +70,7 @@ export function shortenPathWithShortcuts( : futureSegment.start.y let shortcutPoint: Point + const pointBeforeShortcut = shortened[shortened.length - 1] const pointAfterShortcut = route[j + 2] if (!pointAfterShortcut) continue @@ -92,8 +94,8 @@ export function shortenPathWithShortcuts( } if ( - checkIfObstacleBetweenPoints(currentSegment.end, shortcutPoint) || - checkIfObstacleBetweenPoints(futureSegment.start, shortcutPoint) + checkIfObstacleBetweenPoints(pointBeforeShortcut, shortcutPoint) || + checkIfObstacleBetweenPoints(pointAfterShortcut, shortcutPoint) ) { continue }