Skip to content

fix(post-processing): remove zero-length segments left by diagonal fix#437

Open
sucloudflare wants to merge 2 commits into
tscircuit:mainfrom
sucloudflare:fix/remove-zero-length-segments-post-processing
Open

fix(post-processing): remove zero-length segments left by diagonal fix#437
sucloudflare wants to merge 2 commits into
tscircuit:mainfrom
sucloudflare:fix/remove-zero-length-segments-post-processing

Conversation

@sucloudflare
Copy link
Copy Markdown

Closes #78

Root cause

TraceOverlapShiftSolver.findAndFixNextDiagonalSegment fixes diagonal
segments by inserting an elbowPoint via splice(). When the diagonal
is near-zero length, the chosen elbow can coincide with p1 or p2,
producing a zero-length segment. simplifyPath only removed
collinear points — it never checked for duplicate consecutive coords —
so the spurious segment survived into the final output as an extra trace
line.

Fix

Fix 1 — simplifyPath.ts: added isSamePoint() guard (tolerance
1e-6) that drops zero-length points in both simplification passes.

Fix 2 — TraceOverlapShiftSolver.ts: before calling splice(),
detect when the elbow equals p1 or p2 and snap the existing
endpoint instead of inserting a duplicate.

Tests added

5 unit tests in tests/solvers/TraceCleanupSolver/simplifyPath-zero-length.test.ts:

  • duplicate consecutive points removed ✅
  • near-zero tolerance (1e-7) handled ✅
  • valid L-shaped path preserved ✅
  • collinear middle point removed ✅
  • multiple consecutive duplicates removed ✅

Closes tscircuit#78

Root cause: when TraceOverlapShiftSolver.findAndFixNextDiagonalSegment
fixes a near-zero diagonal segment it inserts an elbowPoint via splice().
If the chosen elbow coincides with the adjacent endpoint (p1 or p2), a
zero-length segment is produced. simplifyPath did not remove duplicate
consecutive points, so the spurious segment survived into final output.

Fix 1 – simplifyPath.ts: add isSamePoint() guard that drops zero-length
(duplicate) points in both simplification passes.

Fix 2 – TraceOverlapShiftSolver.ts: before splice(), detect when the
elbow equals p1 or p2 and snap the existing endpoint instead of
inserting a duplicate.

Tests: 5 unit tests for simplifyPath covering zero-length removal,
tolerance handling, and preservation of valid paths.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
schematic-trace-solver Ready Ready Preview, Comment May 26, 2026 2:53am

Request Review

When the diagonal segment is near-zero length, the computed elbow
can equal p1 or p2 within tolerance. Inserting via splice() produces
a zero-length segment upstream of simplifyPath. Instead, snap the
existing endpoint to make the segment axis-aligned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix extra trace lines in post-processing step

1 participant