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
24 changes: 20 additions & 4 deletions lib/solvers/TraceCleanupSolver/TraceCleanupSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { InputProblem } from "lib/types/InputProblem"
import type { GraphicsObject, Line } from "graphics-debug"
import { minimizeTurnsWithFilteredLabels } from "./minimizeTurnsWithFilteredLabels"
import { balanceZShapes } from "./balanceZShapes"
import { combineSameNetTraceSegments } from "./combineSameNetTraceSegments"
import { BaseSolver } from "lib/solvers/BaseSolver/BaseSolver"
import type { SolvedTracePath } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceLinesSolver"
import { visualizeInputProblem } from "lib/solvers/SchematicTracePipelineSolver/visualizeInputProblem"
Expand All @@ -25,6 +26,7 @@ import { is4PointRectangle } from "./is4PointRectangle"
* Represents the different stages or steps within the trace cleanup pipeline.
*/
type PipelineStep =
| "combining_same_net_segments"
| "minimizing_turns"
| "balancing_l_shapes"
| "untangling_traces"
Expand All @@ -33,8 +35,9 @@ type PipelineStep =
* The TraceCleanupSolver is responsible for improving the aesthetics and readability of schematic traces.
* It operates in a multi-step pipeline:
* 1. **Untangling Traces**: It first attempts to untangle any overlapping or highly convoluted traces using a sub-solver.
* 2. **Minimizing Turns**: After untangling, it iterates through each trace to minimize the number of turns, simplifying their paths.
* 3. **Balancing L-Shapes**: Finally, it balances L-shaped trace segments to create more visually appealing and consistent layouts.
* 2. **Combining Same-Net Segments**: Close overlapping same-net segments are snapped together to avoid duplicate traces.
* 3. **Minimizing Turns**: After untangling, it iterates through each trace to minimize the number of turns, simplifying their paths.
* 4. **Balancing L-Shapes**: Finally, it balances L-shaped trace segments to create more visually appealing and consistent layouts.
* The solver processes traces one by one, applying these cleanup steps sequentially to refine the overall trace layout.
*/
export class TraceCleanupSolver extends BaseSolver {
Expand Down Expand Up @@ -66,10 +69,10 @@ export class TraceCleanupSolver extends BaseSolver {
this.outputTraces = output.traces
this.tracesMap = new Map(this.outputTraces.map((t) => [t.mspPairId, t]))
this.activeSubSolver = null
this.pipelineStep = "minimizing_turns"
this.pipelineStep = "combining_same_net_segments"
} else if (this.activeSubSolver.failed) {
this.activeSubSolver = null
this.pipelineStep = "minimizing_turns"
this.pipelineStep = "combining_same_net_segments"
}
return
}
Expand All @@ -78,6 +81,9 @@ export class TraceCleanupSolver extends BaseSolver {
case "untangling_traces":
this._runUntangleTracesStep()
break
case "combining_same_net_segments":
this._runCombineSameNetSegmentsStep()
break
case "minimizing_turns":
this._runMinimizeTurnsStep()
break
Expand All @@ -94,6 +100,16 @@ export class TraceCleanupSolver extends BaseSolver {
})
}

private _runCombineSameNetSegmentsStep() {
this.outputTraces = combineSameNetTraceSegments({
traces: Array.from(this.tracesMap.values()),
maxDistance: this.input.paddingBuffer * 2,
})
this.tracesMap = new Map(this.outputTraces.map((t) => [t.mspPairId, t]))
this.traceIdQueue = Array.from(this.outputTraces.map((e) => e.mspPairId))
this.pipelineStep = "minimizing_turns"
}

private _runMinimizeTurnsStep() {
if (this.traceIdQueue.length === 0) {
this.pipelineStep = "balancing_l_shapes"
Expand Down
175 changes: 175 additions & 0 deletions lib/solvers/TraceCleanupSolver/combineSameNetTraceSegments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import type { SolvedTracePath } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceLinesSolver"
import { simplifyPath } from "./simplifyPath"

type SegmentOrientation = "horizontal" | "vertical"

interface SegmentLocator {
traceIndex: number
segmentIndex: number
orientation: SegmentOrientation
fixedCoord: number
spanMin: number
spanMax: number
length: number
isAnchored: boolean
}

const EPS = 1e-9

const cloneTrace = (trace: SolvedTracePath): SolvedTracePath => ({
...trace,
tracePath: trace.tracePath.map((point) => ({ ...point })),
})

const getTraceNetId = (trace: SolvedTracePath) =>
trace.globalConnNetId ?? trace.userNetId ?? trace.dcConnNetId

const getSegments = (
trace: SolvedTracePath,
traceIndex: number,
): SegmentLocator[] => {
const segments: SegmentLocator[] = []
const { tracePath } = trace

for (
let segmentIndex = 0;
segmentIndex < tracePath.length - 1;
segmentIndex++
) {
const start = tracePath[segmentIndex]!
const end = tracePath[segmentIndex + 1]!
const isHorizontal = Math.abs(start.y - end.y) < EPS
const isVertical = Math.abs(start.x - end.x) < EPS

if (!isHorizontal && !isVertical) continue

const orientation = isHorizontal ? "horizontal" : "vertical"
const fixedCoord = isHorizontal ? start.y : start.x
const spanStart = isHorizontal ? start.x : start.y
const spanEnd = isHorizontal ? end.x : end.y
const spanMin = Math.min(spanStart, spanEnd)
const spanMax = Math.max(spanStart, spanEnd)
const length = spanMax - spanMin

if (length < EPS) continue

segments.push({
traceIndex,
segmentIndex,
orientation,
fixedCoord,
spanMin,
spanMax,
length,
isAnchored:
segmentIndex === 0 || segmentIndex + 1 === tracePath.length - 1,
})
}

return segments
}

const getSpanOverlap = (a: SegmentLocator, b: SegmentLocator) =>
Math.min(a.spanMax, b.spanMax) - Math.max(a.spanMin, b.spanMin)

const chooseMove = (
a: SegmentLocator,
b: SegmentLocator,
): { movable: SegmentLocator; target: SegmentLocator } | null => {
if (a.isAnchored && b.isAnchored) return null
if (a.isAnchored) return { movable: b, target: a }
if (b.isAnchored) return { movable: a, target: b }

if (a.length >= b.length) {
return { movable: b, target: a }
}

return { movable: a, target: b }
}

const moveSegmentToCoord = (
trace: SolvedTracePath,
segment: SegmentLocator,
targetCoord: number,
) => {
const path = trace.tracePath
const start = path[segment.segmentIndex]!
const end = path[segment.segmentIndex + 1]!

if (segment.orientation === "horizontal") {
start.y = targetCoord
end.y = targetCoord
} else {
start.x = targetCoord
end.x = targetCoord
}

trace.tracePath = simplifyPath(path)
}

export const combineSameNetTraceSegments = ({
traces,
maxDistance,
}: {
traces: SolvedTracePath[]
maxDistance: number
}): SolvedTracePath[] => {
if (traces.length === 0 || maxDistance <= EPS) return traces

const nextTraces = traces.map(cloneTrace)
const maxPasses = Math.max(20, nextTraces.length * 20)

for (let pass = 0; pass < maxPasses; pass++) {
let changed = false
const traceIndexesByNet = new Map<string, number[]>()

for (let traceIndex = 0; traceIndex < nextTraces.length; traceIndex++) {
const trace = nextTraces[traceIndex]!
const netId = getTraceNetId(trace)
if (!netId) continue
if (!traceIndexesByNet.has(netId)) traceIndexesByNet.set(netId, [])
traceIndexesByNet.get(netId)!.push(traceIndex)
}

for (const traceIndexes of traceIndexesByNet.values()) {
const segments = traceIndexes.flatMap((traceIndex) =>
getSegments(nextTraces[traceIndex]!, traceIndex),
)

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

for (let j = i + 1; j < segments.length; j++) {
const b = segments[j]!
if (a.orientation !== b.orientation) continue
if (Math.abs(a.fixedCoord - b.fixedCoord) > maxDistance) continue
if (getSpanOverlap(a, b) <= EPS) continue

const move = chooseMove(a, b)
if (!move) continue
if (
Math.abs(move.movable.fixedCoord - move.target.fixedCoord) <= EPS
) {
continue
}

moveSegmentToCoord(
nextTraces[move.movable.traceIndex]!,
move.movable,
move.target.fixedCoord,
)
changed = true
break
}

if (changed) break
}

if (changed) break
}

if (!changed) break
}

return nextTraces
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"react-cosmos-plugin-vite": "^7.0.0",
"react-dom": "^19.1.1",
"tsup": "^8.5.0",
"typescript": "^5.9.3",
"vite": "^7.1.3"
},
"peerDependencies": {
Expand Down
29 changes: 14 additions & 15 deletions tests/examples/__snapshots__/example06.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