Skip to content

Commit

Permalink
attempt reproduction of remove path loop, some strange results
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 26, 2024
1 parent 909f35b commit 008b5e0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Point } from "@tscircuit/math-utils"
import type { PointWithLayer } from "solver-utils"
import {
scale,
fromTriangles,
Expand All @@ -7,7 +8,9 @@ import {
applyToPoint,
} from "transformation-matrix"

export const getPathComparisonSvg = (pathMap: Record<string, Point[]>) => {
export const getPathComparisonSvg = (
pathMap: Record<string, PointWithLayer[]>,
) => {
const svgWidth = 640
const svgHeight = 480

Expand Down Expand Up @@ -54,14 +57,23 @@ export const getPathComparisonSvg = (pathMap: Record<string, Point[]>) => {
for (let i = 0; i < points.length - 1; i++) {
const start = applyToPoint(transform, points[i])
const end = applyToPoint(transform, points[i + 1])
svg += `<line x1="${start.x}" y1="${start.y}" x2="${end.x}" y2="${end.y}" stroke="${color}" stroke-width="2" />`
start.x -= 8 * pathIndex
start.y -= 8 * pathIndex
end.x -= 8 * pathIndex
end.y -= 8 * pathIndex
const isDashed =
points[i].layer === "bottom" || points[i + 1].layer === "bottom"
svg += `<line x1="${start.x}" y1="${start.y}" x2="${end.x}" y2="${end.y}" stroke="${color}" stroke-width="2" ${isDashed ? 'stroke-dasharray="4"' : ""} />`
}

// Draw points and add index numbers
points.forEach((point, index) => {
const { x, y } = applyToPoint(transform, point)
let { x, y } = applyToPoint(transform, point)
x -= 8 * pathIndex
y -= 8 * pathIndex

svg += `<circle cx="${x}" cy="${y}" r="3" fill="${color}" />`
svg += `<text x="${x + 5}" y="${y - 5 + pathIndex * 14}" font-size="10" fill="${color}">${index}</text>`
svg += `<text x="${x + 5}" y="${y - 5}" font-size="10" fill="${color}">${index}</text>`
})

// Add legend item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getDebugSvg } from "./fixtures/get-debug-svg"
import { removePathLoops } from "solver-postprocessing/remove-path-loops"
import { getPathComparisonSvg } from "./fixtures/get-path-comparison-svg"

test("remove-path-loops: simple loop", () => {
test("remove-path-loops 1: simple loop", () => {
/**
* Ascii art of the path:
* ......
Expand Down
38 changes: 38 additions & 0 deletions algos/infinite-grid-ijump-astar/tests/remove-path-loops-2.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Circuit } from "@tscircuit/core"
import { expect, test } from "bun:test"
import type { AnyCircuitElement } from "circuit-json"
import { getSimpleRouteJson } from "solver-utils"
import { IJumpMultiMarginAutorouter } from "../v2/lib/IJumpMultiMarginAutorouter"
import { getDebugSvg } from "./fixtures/get-debug-svg"
import { removePathLoops } from "solver-postprocessing/remove-path-loops"
import { getPathComparisonSvg } from "./fixtures/get-path-comparison-svg"

test("remove-path-loops 2: simple loop", () => {
/**
* Ascii art of the path:
* ......
* . .
* ..........
* .
* .
*/
// Create a path with an intentional loop
const pathWithLoop: Array<{ x: number; y: number; layer: string }> = [
{ x: 0, y: 0, layer: "top" },
{ x: 5, y: 0, layer: "top" },
{ x: 5, y: 3, layer: "top" },
{ x: 5, y: 3, layer: "top" },
{ x: 3, y: 3, layer: "top" },
{ x: 3, y: 3, layer: "bottom" },
{ x: 3, y: -3, layer: "bottom" },
]

const simplifiedPath = removePathLoops(pathWithLoop)

expect(
getPathComparisonSvg({
pathWithLoop,
simplifiedPath,
}),
).toMatchSvgSnapshot(import.meta.path)
})

0 comments on commit 008b5e0

Please sign in to comment.