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
19 changes: 17 additions & 2 deletions lib/solvers/TraceCleanupSolver/simplifyPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ import {
isVertical,
} from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver2/collisions"

/**
* Remove consecutive duplicate points (zero-length segments) from a path.
*/
export const removeDuplicatePoints = (path: Point[]): Point[] => {
if (path.length === 0) return path
const result: Point[] = [path[0]]
for (let i = 1; i < path.length; i++) {
const prev = result[result.length - 1]
if (prev.x !== path[i].x || prev.y !== path[i].y) {
result.push(path[i])
}
}
return result
}

export const simplifyPath = (path: Point[]): Point[] => {
if (path.length < 3) return path
if (path.length < 3) return removeDuplicatePoints(path)
const newPath: Point[] = [path[0]]
for (let i = 1; i < path.length - 1; i++) {
const p1 = newPath[newPath.length - 1]
Expand Down Expand Up @@ -37,5 +52,5 @@ export const simplifyPath = (path: Point[]): Point[] => {
}
finalPath.push(newPath[newPath.length - 1])

return finalPath
return removeDuplicatePoints(finalPath)
}
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 { removeDuplicatePoints } from "../simplifyPath"

/**
* Defines the input structure for the UntangleTraceSubsolver.
Expand Down Expand Up @@ -258,11 +259,11 @@ export class UntangleTraceSubsolver extends BaseSolver {
p.x === this.currentLShape!.p2.x && p.y === this.currentLShape!.p2.y,
)
if (p2Index !== -1) {
const newTracePath = [
const newTracePath = removeDuplicatePoints([
...originalTrace.tracePath.slice(0, p2Index),
...bestRoute,
...originalTrace.tracePath.slice(p2Index + 1),
]
])
this.input.allTraces[traceIndex] = {
...originalTrace,
tracePath: newTracePath,
Expand Down
Loading