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
14 changes: 7 additions & 7 deletions lib/solvers/TraceCleanupSolver/TraceCleanupSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseSolver } from "lib/solvers/BaseSolver/BaseSolver"
import type { SolvedTracePath } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceLinesSolver"
import { visualizeInputProblem } from "lib/solvers/SchematicTracePipelineSolver/visualizeInputProblem"
import type { NetLabelPlacement } from "../NetLabelPlacementSolver/NetLabelPlacementSolver"
import { mergeSameNetTraceSegments } from "./mergeSameNetTraceSegments"

/**
* Defines the input structure for the TraceCleanupSolver.
Expand Down Expand Up @@ -49,11 +50,12 @@ export class TraceCleanupSolver extends BaseSolver {
constructor(solverInput: TraceCleanupSolverInput) {
super()
this.input = solverInput
this.outputTraces = [...solverInput.allTraces]
this.outputTraces = mergeSameNetTraceSegments({
traces: solverInput.allTraces,
tolerance: solverInput.paddingBuffer,
})
this.tracesMap = new Map(this.outputTraces.map((t) => [t.mspPairId, t]))
this.traceIdQueue = Array.from(
solverInput.allTraces.map((e) => e.mspPairId),
)
this.traceIdQueue = Array.from(this.outputTraces.map((e) => e.mspPairId))
}

override _step() {
Expand Down Expand Up @@ -97,9 +99,7 @@ export class TraceCleanupSolver extends BaseSolver {
private _runMinimizeTurnsStep() {
if (this.traceIdQueue.length === 0) {
this.pipelineStep = "balancing_l_shapes"
this.traceIdQueue = Array.from(
this.input.allTraces.map((e) => e.mspPairId),
)
this.traceIdQueue = Array.from(this.outputTraces.map((e) => e.mspPairId))
return
}

Expand Down
149 changes: 149 additions & 0 deletions lib/solvers/TraceCleanupSolver/mergeSameNetTraceSegments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import type { Point } from "@tscircuit/math-utils"
import type { SolvedTracePath } from "../SchematicTraceLinesSolver/SchematicTraceLinesSolver"
import { simplifyPath } from "./simplifyPath"

const EPSILON = 1e-9

type SegmentOrientation = "horizontal" | "vertical"

interface SegmentRef {
traceIndex: number
startIndex: number
endIndex: number
netId: string
orientation: SegmentOrientation
coordinate: number
minProjection: number
maxProjection: number
}

const getSegmentRef = (
trace: SolvedTracePath,
traceIndex: number,
startIndex: number,
): SegmentRef | null => {
const p1 = trace.tracePath[startIndex]
const p2 = trace.tracePath[startIndex + 1]

if (!p1 || !p2 || !trace.globalConnNetId) {
return null
}

if (Math.abs(p1.y - p2.y) <= EPSILON) {
return {
traceIndex,
startIndex,
endIndex: startIndex + 1,
netId: trace.globalConnNetId,
orientation: "horizontal",
coordinate: p1.y,
minProjection: Math.min(p1.x, p2.x),
maxProjection: Math.max(p1.x, p2.x),
}
}

if (Math.abs(p1.x - p2.x) <= EPSILON) {
return {
traceIndex,
startIndex,
endIndex: startIndex + 1,
netId: trace.globalConnNetId,
orientation: "vertical",
coordinate: p1.x,
minProjection: Math.min(p1.y, p2.y),
maxProjection: Math.max(p1.y, p2.y),
}
}

return null
}

const getProjectionGap = (a: SegmentRef, b: SegmentRef): number => {
if (a.maxProjection < b.minProjection) {
return b.minProjection - a.maxProjection
}
if (b.maxProjection < a.minProjection) {
return a.minProjection - b.maxProjection
}
return 0
}

const alignSegment = (
path: Point[],
segment: SegmentRef,
coordinate: number,
) => {
const start = path[segment.startIndex]!
const end = path[segment.endIndex]!

if (segment.orientation === "horizontal") {
path[segment.startIndex] = { ...start, y: coordinate }
path[segment.endIndex] = { ...end, y: coordinate }
} else {
path[segment.startIndex] = { ...start, x: coordinate }
path[segment.endIndex] = { ...end, x: coordinate }
}
}

const getSegments = (traces: SolvedTracePath[]): SegmentRef[] =>
traces.flatMap((trace, traceIndex) =>
trace.tracePath
.slice(0, -1)
.map((_, startIndex) => getSegmentRef(trace, traceIndex, startIndex))
.filter((segment): segment is SegmentRef => segment !== null),
)

export const mergeSameNetTraceSegments = ({
traces,
tolerance,
}: {
traces: SolvedTracePath[]
tolerance: number
}): SolvedTracePath[] => {
const output = traces.map((trace) => ({
...trace,
tracePath: trace.tracePath.map((point) => ({ ...point })),
}))

let changed = true
let passes = 0

while (changed && passes < 4) {
changed = false
passes++
const segments = getSegments(output)

for (let i = 0; i < segments.length; i++) {
for (let j = i + 1; j < segments.length; j++) {
const a = segments[i]!
const b = segments[j]!

if (
a.netId !== b.netId ||
a.orientation !== b.orientation ||
a.traceIndex === b.traceIndex
) {
continue
}

if (Math.abs(a.coordinate - b.coordinate) > tolerance) {
continue
}

if (getProjectionGap(a, b) > tolerance) {
continue
}

const mergedCoordinate = (a.coordinate + b.coordinate) / 2
alignSegment(output[a.traceIndex]!.tracePath, a, mergedCoordinate)
alignSegment(output[b.traceIndex]!.tracePath, b, mergedCoordinate)
changed = true
}
}
}

return output.map((trace) => ({
...trace,
tracePath: simplifyPath(trace.tracePath),
}))
}
34 changes: 12 additions & 22 deletions tests/examples/__snapshots__/example18.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading