Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions lib/solvers/TraceCleanupSolver/simplifyPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@ import {
isVertical,
} from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver2/collisions"

const EPS = 1e-9

/**
* Checks if two points are essentially the same (within epsilon tolerance)
*/
const isSamePoint = (a: Point, b: Point): boolean => {
return Math.abs(a.x - b.x) < EPS && Math.abs(a.y - b.y) < EPS
}

/**
* Removes duplicate consecutive points from a path
*/
const removeDuplicateConsecutivePoints = (path: Point[]): Point[] => {
if (path.length < 2) return path
const result: Point[] = [path[0]]
for (let i = 1; i < path.length; i++) {
if (!isSamePoint(result[result.length - 1], path[i])) {
result.push(path[i])
}
}
return result
}

export const simplifyPath = (path: Point[]): Point[] => {
if (path.length < 3) return path
const newPath: Point[] = [path[0]]
for (let i = 1; i < path.length - 1; i++) {
// First, remove any duplicate consecutive points
const dedupedPath = removeDuplicateConsecutivePoints(path)

if (dedupedPath.length < 3) return dedupedPath

// First pass: remove collinear intermediate points
const newPath: Point[] = [dedupedPath[0]]
for (let i = 1; i < dedupedPath.length - 1; i++) {
const p1 = newPath[newPath.length - 1]
const p2 = path[i]
const p3 = path[i + 1]
const p2 = dedupedPath[i]
const p3 = dedupedPath[i + 1]
if (
(isVertical(p1, p2) && isVertical(p2, p3)) ||
(isHorizontal(p1, p2) && isHorizontal(p2, p3))
Expand All @@ -19,9 +47,11 @@ export const simplifyPath = (path: Point[]): Point[] => {
}
newPath.push(p2)
}
newPath.push(path[path.length - 1])
newPath.push(dedupedPath[dedupedPath.length - 1])

if (newPath.length < 3) return newPath

// Second pass: ensure any remaining collinear segments are merged
const finalPath: Point[] = [newPath[0]]
for (let i = 1; i < newPath.length - 1; i++) {
const p1 = finalPath[finalPath.length - 1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { visualizeTightRectangle } from "../visualizeTightRectangle"
import { visualizeCandidates } from "./visualizeCandidates"
import { mergeGraphicsObjects } from "../mergeGraphicsObjects"
import { visualizeCollision } from "./visualizeCollision"
import { simplifyPath } from "../simplifyPath"

/**
* Defines the input structure for the UntangleTraceSubsolver.
Expand Down Expand Up @@ -258,11 +259,15 @@ export class UntangleTraceSubsolver extends BaseSolver {
p.x === this.currentLShape!.p2.x && p.y === this.currentLShape!.p2.y,
)
if (p2Index !== -1) {
const newTracePath = [
// Build the new trace path by replacing p2 with the bestRoute
const rawNewTracePath = [
...originalTrace.tracePath.slice(0, p2Index),
...bestRoute,
...originalTrace.tracePath.slice(p2Index + 1),
]
// Simplify the path to remove any redundant collinear points
// This fixes the issue of extra trace lines appearing after rerouting
const newTracePath = simplifyPath(rawNewTracePath)
this.input.allTraces[traceIndex] = {
...originalTrace,
tracePath: newTracePath,
Expand Down
8 changes: 4 additions & 4 deletions tests/examples/__snapshots__/example29.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions tests/functions/simplifyPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { expect, test, describe } from "bun:test"
import { simplifyPath } from "lib/solvers/TraceCleanupSolver/simplifyPath"

describe("simplifyPath", () => {
test("should return path unchanged if less than 3 points", () => {
expect(simplifyPath([])).toEqual([])
expect(simplifyPath([{ x: 0, y: 0 }])).toEqual([{ x: 0, y: 0 }])
expect(
simplifyPath([
{ x: 0, y: 0 },
{ x: 1, y: 1 },
]),
).toEqual([
{ x: 0, y: 0 },
{ x: 1, y: 1 },
])
})

test("should remove collinear horizontal points", () => {
const path = [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 2, y: 0 },
{ x: 3, y: 0 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 3, y: 0 },
])
})

test("should remove collinear vertical points", () => {
const path = [
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 0, y: 2 },
{ x: 0, y: 3 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 0, y: 3 },
])
})

test("should preserve L-shaped corners", () => {
const path = [
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 1, y: 1 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 1, y: 1 },
])
})

test("should handle Z-shape correctly", () => {
const path = [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 1, y: 1 },
{ x: 2, y: 1 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 1, y: 1 },
{ x: 2, y: 1 },
])
})

test("should remove duplicate consecutive points", () => {
const path = [
{ x: 0, y: 0 },
{ x: 0, y: 0 }, // duplicate
{ x: 1, y: 0 },
{ x: 1, y: 0 }, // duplicate
{ x: 1, y: 1 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 1, y: 1 },
])
})

test("should remove near-duplicate consecutive points (within epsilon)", () => {
const path = [
{ x: 0, y: 0 },
{ x: 1e-10, y: 1e-10 }, // near-duplicate
{ x: 1, y: 0 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 1, y: 0 },
])
})

test("should handle complex path with duplicates and collinear points", () => {
const path = [
{ x: 0, y: 0 },
{ x: 0, y: 0 }, // duplicate
{ x: 1, y: 0 },
{ x: 2, y: 0 }, // collinear with previous
{ x: 2, y: 1 },
{ x: 2, y: 2 }, // collinear with previous
{ x: 2, y: 2 }, // duplicate
{ x: 3, y: 2 },
]
const result = simplifyPath(path)
expect(result).toEqual([
{ x: 0, y: 0 },
{ x: 2, y: 0 },
{ x: 2, y: 2 },
{ x: 3, y: 2 },
])
})
})