-
Notifications
You must be signed in to change notification settings - Fork 240
fix: merge nearby same-net trace segments #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MolhamHamwi
wants to merge
2
commits into
tscircuit:main
Choose a base branch
from
MolhamHamwi:bounty-34-merge-close-same-net-segments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
lib/solvers/TraceCleanupSolver/mergeNearbySameNetSegments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import type { Point } from "graphics-debug" | ||
| import type { SolvedTracePath } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceLinesSolver" | ||
| import { simplifyPath } from "./simplifyPath" | ||
|
|
||
| const DEFAULT_MERGE_DISTANCE = 0.18 | ||
| const EPS = 1e-6 | ||
|
|
||
| type Orientation = "horizontal" | "vertical" | ||
|
|
||
| type SegmentRef = { | ||
| traceIndex: number | ||
| startIndex: number | ||
| orientation: Orientation | ||
| fixedCoord: number | ||
| min: number | ||
| max: number | ||
| } | ||
|
|
||
| const getSegmentRef = ( | ||
| trace: SolvedTracePath, | ||
| traceIndex: number, | ||
| startIndex: number, | ||
| ): SegmentRef | null => { | ||
| const p1 = trace.tracePath[startIndex]! | ||
| const p2 = trace.tracePath[startIndex + 1]! | ||
|
|
||
| if (Math.abs(p1.y - p2.y) < EPS && Math.abs(p1.x - p2.x) > EPS) { | ||
| return { | ||
| traceIndex, | ||
| startIndex, | ||
| orientation: "horizontal", | ||
| fixedCoord: p1.y, | ||
| min: Math.min(p1.x, p2.x), | ||
| max: Math.max(p1.x, p2.x), | ||
| } | ||
| } | ||
|
|
||
| if (Math.abs(p1.x - p2.x) < EPS && Math.abs(p1.y - p2.y) > EPS) { | ||
| return { | ||
| traceIndex, | ||
| startIndex, | ||
| orientation: "vertical", | ||
| fixedCoord: p1.x, | ||
| min: Math.min(p1.y, p2.y), | ||
| max: Math.max(p1.y, p2.y), | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| const getNetId = (trace: SolvedTracePath) => | ||
| trace.userNetId ?? trace.globalConnNetId ?? trace.dcConnNetId | ||
|
|
||
| const rangesOverlap = (a: SegmentRef, b: SegmentRef) => | ||
| Math.min(a.max, b.max) - Math.max(a.min, b.min) > EPS | ||
|
|
||
| const isInteriorSegment = (trace: SolvedTracePath, segmentStartIndex: number) => | ||
| segmentStartIndex > 0 && segmentStartIndex + 1 < trace.tracePath.length - 1 | ||
|
|
||
| const moveSegmentToFixedCoord = ( | ||
| trace: SolvedTracePath, | ||
| segmentStartIndex: number, | ||
| orientation: Orientation, | ||
| fixedCoord: number, | ||
| ) => { | ||
| const path = trace.tracePath.map((p) => ({ ...p })) | ||
| const p1 = path[segmentStartIndex]! | ||
| const p2 = path[segmentStartIndex + 1]! | ||
|
|
||
| if (orientation === "horizontal") { | ||
| path[segmentStartIndex] = { ...p1, y: fixedCoord } | ||
| path[segmentStartIndex + 1] = { ...p2, y: fixedCoord } | ||
| } else { | ||
| path[segmentStartIndex] = { ...p1, x: fixedCoord } | ||
| path[segmentStartIndex + 1] = { ...p2, x: fixedCoord } | ||
| } | ||
|
|
||
| return { ...trace, tracePath: simplifyPath(path as Point[]) } | ||
| } | ||
|
|
||
| export const mergeNearbySameNetSegments = ( | ||
| traces: SolvedTracePath[], | ||
| mergeDistance = DEFAULT_MERGE_DISTANCE, | ||
| ): SolvedTracePath[] => { | ||
| let output = traces.map((trace) => ({ | ||
| ...trace, | ||
| tracePath: trace.tracePath.map((p) => ({ ...p })), | ||
| })) | ||
|
|
||
| const segments: SegmentRef[] = [] | ||
| for (let traceIndex = 0; traceIndex < output.length; traceIndex++) { | ||
| const trace = output[traceIndex]! | ||
| for ( | ||
| let segmentIndex = 0; | ||
| segmentIndex < trace.tracePath.length - 1; | ||
| segmentIndex++ | ||
| ) { | ||
| if (!isInteriorSegment(trace, segmentIndex)) continue | ||
| const segment = getSegmentRef(trace, traceIndex, segmentIndex) | ||
| if (segment) segments.push(segment) | ||
| } | ||
| } | ||
|
|
||
| const visited = new Set<number>() | ||
|
|
||
| for (let startIndex = 0; startIndex < segments.length; startIndex++) { | ||
| if (visited.has(startIndex)) continue | ||
|
|
||
| const componentIndexes: number[] = [] | ||
| const queue = [startIndex] | ||
| visited.add(startIndex) | ||
|
|
||
| while (queue.length > 0) { | ||
| const currentIndex = queue.shift()! | ||
| const current = segments[currentIndex]! | ||
| componentIndexes.push(currentIndex) | ||
|
|
||
| for ( | ||
| let candidateIndex = 0; | ||
| candidateIndex < segments.length; | ||
| candidateIndex++ | ||
| ) { | ||
| if (visited.has(candidateIndex)) continue | ||
| const candidate = segments[candidateIndex]! | ||
| if ( | ||
| getNetId(output[current.traceIndex]!) !== | ||
| getNetId(output[candidate.traceIndex]!) | ||
| ) | ||
| continue | ||
| if (current.orientation !== candidate.orientation) continue | ||
| if (!rangesOverlap(current, candidate)) continue | ||
|
|
||
| const distance = Math.abs(current.fixedCoord - candidate.fixedCoord) | ||
| if (distance <= EPS || distance > mergeDistance) continue | ||
|
|
||
| visited.add(candidateIndex) | ||
| queue.push(candidateIndex) | ||
| } | ||
| } | ||
|
|
||
| if (componentIndexes.length < 2) continue | ||
|
|
||
| const component = componentIndexes.map((index) => segments[index]!) | ||
| const mergedCoord = | ||
| component.reduce((sum, segment) => sum + segment.fixedCoord, 0) / | ||
| component.length | ||
|
|
||
| for (const segment of component) { | ||
| output[segment.traceIndex] = moveSegmentToFixedCoord( | ||
| output[segment.traceIndex]!, | ||
| segment.startIndex, | ||
| segment.orientation, | ||
| mergedCoord, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return output | ||
| } |
90 changes: 90 additions & 0 deletions
90
tests/solvers/TraceCleanupSolver/mergeNearbySameNetSegments.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { mergeNearbySameNetSegments } from "lib/solvers/TraceCleanupSolver/mergeNearbySameNetSegments" | ||
| import type { SolvedTracePath } from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceLinesSolver" | ||
|
|
||
| const makeTrace = ( | ||
| mspPairId: string, | ||
| globalConnNetId: string, | ||
| tracePath: Array<{ x: number; y: number }>, | ||
| ): SolvedTracePath => | ||
| ({ | ||
| mspPairId, | ||
| dcConnNetId: globalConnNetId, | ||
| globalConnNetId, | ||
| pins: [] as any, | ||
| mspConnectionPairIds: [mspPairId], | ||
| pinIds: [], | ||
| tracePath, | ||
| }) as any | ||
|
|
||
| test("mergeNearbySameNetSegments aligns close overlapping horizontal same-net segments", () => { | ||
| const [a, b] = mergeNearbySameNetSegments([ | ||
| makeTrace("a", "net1", [ | ||
| { x: 0, y: 0 }, | ||
| { x: 0, y: 1 }, | ||
| { x: 3, y: 1 }, | ||
| { x: 3, y: 2 }, | ||
| ]), | ||
| makeTrace("b", "net1", [ | ||
| { x: 1, y: 0 }, | ||
| { x: 1, y: 1.1 }, | ||
| { x: 4, y: 1.1 }, | ||
| { x: 4, y: 2 }, | ||
| ]), | ||
| ]) | ||
|
|
||
| expect(a.tracePath[1]!.y).toBeCloseTo(1.05) | ||
| expect(a.tracePath[2]!.y).toBeCloseTo(1.05) | ||
| expect(b.tracePath[1]!.y).toBeCloseTo(1.05) | ||
| expect(b.tracePath[2]!.y).toBeCloseTo(1.05) | ||
| }) | ||
|
|
||
| test("mergeNearbySameNetSegments does not align different-net segments", () => { | ||
| const [a, b] = mergeNearbySameNetSegments([ | ||
| makeTrace("a", "net1", [ | ||
| { x: 0, y: 0 }, | ||
| { x: 0, y: 1 }, | ||
| { x: 3, y: 1 }, | ||
| { x: 3, y: 2 }, | ||
| ]), | ||
| makeTrace("b", "net2", [ | ||
| { x: 1, y: 0 }, | ||
| { x: 1, y: 1.1 }, | ||
| { x: 4, y: 1.1 }, | ||
| { x: 4, y: 2 }, | ||
| ]), | ||
| ]) | ||
|
|
||
| expect(a.tracePath[1]!.y).toBe(1) | ||
| expect(b.tracePath[1]!.y).toBe(1.1) | ||
| }) | ||
|
|
||
| test("mergeNearbySameNetSegments aligns every close segment in a same-net group", () => { | ||
| const [a, b, c] = mergeNearbySameNetSegments([ | ||
| makeTrace("a", "net1", [ | ||
| { x: 0, y: 0 }, | ||
| { x: 0, y: 1 }, | ||
| { x: 3, y: 1 }, | ||
| { x: 3, y: 2 }, | ||
| ]), | ||
| makeTrace("b", "net1", [ | ||
| { x: 1, y: 0 }, | ||
| { x: 1, y: 1.1 }, | ||
| { x: 4, y: 1.1 }, | ||
| { x: 4, y: 2 }, | ||
| ]), | ||
| makeTrace("c", "net1", [ | ||
| { x: 2, y: 0 }, | ||
| { x: 2, y: 1.2 }, | ||
| { x: 5, y: 1.2 }, | ||
| { x: 5, y: 2 }, | ||
| ]), | ||
| ]) | ||
|
|
||
| expect(a.tracePath[1]!.y).toBeCloseTo(1.1) | ||
| expect(a.tracePath[2]!.y).toBeCloseTo(1.1) | ||
| expect(b.tracePath[1]!.y).toBeCloseTo(1.1) | ||
| expect(b.tracePath[2]!.y).toBeCloseTo(1.1) | ||
| expect(c.tracePath[1]!.y).toBeCloseTo(1.1) | ||
| expect(c.tracePath[2]!.y).toBeCloseTo(1.1) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in dbcd4a3: updated the pipeline JSDoc with the new close same-net merge phase.