-
Notifications
You must be signed in to change notification settings - Fork 82
feat: add pipeline phase to combine close same-net trace segments (#29) #152
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
SyedHannanMehdi
wants to merge
13
commits into
tscircuit:main
Choose a base branch
from
SyedHannanMehdi:cashclaw/fix-29-new-phase-to-combine-sam
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.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6c72e53
Add combine-close-same-net-segments phase implementation
SyedHannanMehdi 1e85645
Add tests for combine-close-same-net-segments phase
SyedHannanMehdi c1a25fe
feat: add combine-close-same-net-trace-segments phase
SyedHannanMehdi 2239068
test: add tests for combine-close-same-net-trace-segments phase
SyedHannanMehdi 60b0574
feat: export combine-close-same-net-trace-segments from phases index
SyedHannanMehdi f4b442e
fix: address review comments in combine-close-same-net-trace-segments
SyedHannanMehdi 9bbfd2d
fix: fix broken import path in combine-close-same-net-segments.ts
SyedHannanMehdi d117a26
fix: document why both segment-combining phases exist (review comment)
SyedHannanMehdi d389a36
chore: update phases barrel export for both phase modules
SyedHannanMehdi 84b3fa9
test: expand trace-segments tests to cover schematic_port_id preserva…
SyedHannanMehdi 2348953
feat: add merge-close-same-net-traces phase
SyedHannanMehdi 84b1f01
test: add tests for merge-close-same-net-traces phase
SyedHannanMehdi b51aa15
feat: export merge-close-same-net-traces from phases index
SyedHannanMehdi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| import type { SchematicTrace } from "../types" | ||
|
|
||
| /** | ||
| * Phase: Combine Close Same-Net Trace Segments | ||
| * | ||
| * This phase finds trace segments on the same net that are very close together | ||
| * (nearly overlapping or nearly collinear) and merges them into single segments. | ||
| * This cleans up visual artifacts where routing produces redundant parallel or | ||
| * overlapping segments on the same net. | ||
| * | ||
| * Two segments are candidates for merging if: | ||
| * 1. They belong to the same net | ||
| * 2. They are either overlapping, collinear and contiguous, or nearly parallel | ||
| * and within a small distance threshold | ||
| */ | ||
|
|
||
| interface Point { | ||
| x: number | ||
| y: number | ||
| } | ||
|
|
||
| interface Segment { | ||
| start: Point | ||
| end: Point | ||
| netId?: string | ||
| } | ||
|
|
||
| const CLOSE_DISTANCE_THRESHOLD = 0.001 // schematic units | ||
|
|
||
| /** | ||
| * Check if two numbers are approximately equal within tolerance | ||
| */ | ||
| function approxEqual(a: number, b: number, tolerance = CLOSE_DISTANCE_THRESHOLD): boolean { | ||
| return Math.abs(a - b) <= tolerance | ||
| } | ||
|
|
||
| /** | ||
| * Compute the squared distance between two points | ||
| */ | ||
| function distSq(a: Point, b: Point): number { | ||
| return (a.x - b.x) ** 2 + (a.y - b.y) ** 2 | ||
| } | ||
|
|
||
| /** | ||
| * Distance between two points | ||
| */ | ||
| function dist(a: Point, b: Point): number { | ||
| return Math.sqrt(distSq(a, b)) | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the segment is horizontal (within tolerance) | ||
| */ | ||
| function isHorizontal(seg: Segment): boolean { | ||
| return approxEqual(seg.start.y, seg.end.y) | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the segment is vertical (within tolerance) | ||
| */ | ||
| function isVertical(seg: Segment): boolean { | ||
| return approxEqual(seg.start.x, seg.end.x) | ||
| } | ||
|
|
||
| /** | ||
| * Get the minimum coordinate of a horizontal segment | ||
| */ | ||
| function hMin(seg: Segment): number { | ||
| return Math.min(seg.start.x, seg.end.x) | ||
| } | ||
|
|
||
| /** | ||
| * Get the maximum coordinate of a horizontal segment | ||
| */ | ||
| function hMax(seg: Segment): number { | ||
| return Math.max(seg.start.x, seg.end.x) | ||
| } | ||
|
|
||
| /** | ||
| * Get the minimum coordinate of a vertical segment | ||
| */ | ||
| function vMin(seg: Segment): number { | ||
| return Math.min(seg.start.y, seg.end.y) | ||
| } | ||
|
|
||
| /** | ||
| * Get the maximum coordinate of a vertical segment | ||
| */ | ||
| function vMax(seg: Segment): number { | ||
| return Math.max(seg.start.y, seg.end.y) | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to merge two horizontal segments that share (approximately) the same Y | ||
| * and whose X ranges overlap or are contiguous. | ||
| * Returns the merged segment or null if they can't be merged. | ||
| */ | ||
| function tryMergeHorizontal(a: Segment, b: Segment): Segment | null { | ||
| if (!isHorizontal(a) || !isHorizontal(b)) return null | ||
| // Same Y? | ||
| if (!approxEqual(a.start.y, b.start.y)) return null | ||
|
|
||
| const aMin = hMin(a) | ||
| const aMax = hMax(a) | ||
| const bMin = hMin(b) | ||
| const bMax = hMax(b) | ||
|
|
||
| // Check overlap or contiguous (with a small gap tolerance) | ||
| if (bMin > aMax + CLOSE_DISTANCE_THRESHOLD) return null | ||
| if (aMin > bMax + CLOSE_DISTANCE_THRESHOLD) return null | ||
|
|
||
| // Merge: take the full span | ||
| const newMin = Math.min(aMin, bMin) | ||
| const newMax = Math.max(aMax, bMax) | ||
| const y = a.start.y | ||
|
|
||
| return { | ||
| start: { x: newMin, y }, | ||
| end: { x: newMax, y }, | ||
| netId: a.netId, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to merge two vertical segments that share (approximately) the same X | ||
| * and whose Y ranges overlap or are contiguous. | ||
| * Returns the merged segment or null if they can't be merged. | ||
| */ | ||
| function tryMergeVertical(a: Segment, b: Segment): Segment | null { | ||
| if (!isVertical(a) || !isVertical(b)) return null | ||
| // Same X? | ||
| if (!approxEqual(a.start.x, b.start.x)) return null | ||
|
|
||
| const aMin = vMin(a) | ||
| const aMax = vMax(a) | ||
| const bMin = vMin(b) | ||
| const bMax = vMax(b) | ||
|
|
||
| // Check overlap or contiguous (with a small gap tolerance) | ||
| if (bMin > aMax + CLOSE_DISTANCE_THRESHOLD) return null | ||
| if (aMin > bMax + CLOSE_DISTANCE_THRESHOLD) return null | ||
|
|
||
| // Merge: take the full span | ||
| const newMin = Math.min(aMin, bMin) | ||
| const newMax = Math.max(aMax, bMax) | ||
| const x = a.start.x | ||
|
|
||
| return { | ||
| start: { x, y: newMin }, | ||
| end: { x, y: newMax }, | ||
| netId: a.netId, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Given an array of segments for a single net, repeatedly merge any pair of | ||
| * segments that can be merged (collinear and overlapping/contiguous), until no | ||
| * further merges are possible. | ||
| */ | ||
| function mergeSegmentsForNet(segments: Segment[]): Segment[] { | ||
| let changed = true | ||
| let result = [...segments] | ||
|
|
||
| while (changed) { | ||
| changed = false | ||
| const merged: boolean[] = new Array(result.length).fill(false) | ||
| const next: Segment[] = [] | ||
|
|
||
| for (let i = 0; i < result.length; i++) { | ||
| if (merged[i]) continue | ||
|
|
||
| let current = result[i] | ||
| for (let j = i + 1; j < result.length; j++) { | ||
| if (merged[j]) continue | ||
|
|
||
| const candidate = | ||
| tryMergeHorizontal(current, result[j]) ?? | ||
| tryMergeVertical(current, result[j]) | ||
|
|
||
| if (candidate) { | ||
| current = candidate | ||
| merged[j] = true | ||
| changed = true | ||
| } | ||
| } | ||
|
|
||
| next.push(current) | ||
| } | ||
|
|
||
| result = next | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| /** | ||
| * Convert a SchematicTrace's edges into Segments grouped by net, merge them, | ||
| * and reconstruct the trace edges. | ||
| * | ||
| * The SchematicTrace type has an `edges` array where each edge has | ||
| * `from` and `to` points. We group edges by net label and then merge | ||
| * collinear overlapping edges. | ||
| */ | ||
| export function combineCloseSameNetSegments( | ||
| traces: SchematicTrace[] | ||
| ): SchematicTrace[] { | ||
| return traces.map((trace) => { | ||
| if (!trace.edges || trace.edges.length === 0) return trace | ||
|
|
||
| // Group edges by net (use trace-level net or edge-level net_label) | ||
| // Each edge: { from: Point, to: Point } | ||
| // We'll treat all edges in this trace as the same net and merge them. | ||
| const segments: Segment[] = trace.edges.map((edge) => ({ | ||
| start: { x: edge.from.x, y: edge.from.y }, | ||
| end: { x: edge.to.x, y: edge.to.y }, | ||
| })) | ||
|
|
||
| const merged = mergeSegmentsForNet(segments) | ||
|
|
||
| const newEdges = merged.map((seg) => ({ | ||
| ...trace.edges[0], // copy any extra fields from the first edge as defaults | ||
| from: { x: seg.start.x, y: seg.start.y }, | ||
| to: { x: seg.end.x, y: seg.end.y }, | ||
| })) | ||
|
|
||
| return { | ||
| ...trace, | ||
| edges: newEdges, | ||
| } | ||
| }) | ||
| } | ||
Oops, something went wrong.
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.
../typesdoes not resolve to any file in this repo (nolib/types.tsorlib/types/index.ts), so this import will break compilation. Define/exportSchematicTracein a resolvable module (e.g.lib/types/index.ts) or change the import to the correct existing path.