From 5e3f767187760bb2fa5303f86def190e930e2586 Mon Sep 17 00:00:00 2001 From: seveibar Date: Thu, 7 Nov 2024 20:10:29 -0800 Subject: [PATCH] implement is_crossing --- bun.lockb | Bin 121640 -> 121640 bytes ...reate-schematic-trace-crossing-segments.ts | 75 +++++++++++++++++- package.json | 2 +- ...schematic-trace-overlap-schematic.snap.svg | 2 +- .../repro4-schematic-trace-overlap.test.tsx | 21 +++++ 5 files changed, 97 insertions(+), 3 deletions(-) diff --git a/bun.lockb b/bun.lockb index 8d3a5dbb72a608238cc9e0188e7e7b3bfe6e0821..108ada4ff3362d0220a6bf603df4a0a2563aaa1f 100755 GIT binary patch delta 148 zcmV;F0BirKwg;%T2aqlxu#bYiHs$;}aTBd>S)pf6&8*lFI9D)6CXYS|rZ6y#u}(T8 z0b!GI6epALHwm-3BOGZ!srFS8-adap7y$P7ak>ib4zpRIf7SydRW;tw@LES85#CT0 zNW(FKr$gd-OWwO^{V|Ic61Y?i5uf(+)3|7lvlWey{{c6b5wigxmr#KLB)95}0X|N; C(Lj&@ delta 148 zcmV;F0BirKwg;%T2aqlxd`KQv&|a@N7c4`-8(P#&ORpEo3UcER4JQno?&*&Yu}(T8 z0brAH6epALHwm-3BOGZ!M&9Cs1>v**tgmWaqSr-^p { - // TODO + const otherEdges: SchematicTrace["edges"] = [] + for (const otherSchematicTrace of db.schematic_trace.list()) { + otherEdges.push(...otherSchematicTrace.edges) + } + + // For each edge in our trace + for (let i = 0; i < edges.length; i++) { + const edge = edges[i] + const edgeOrientation = + edge.from.x === edge.to.x ? "vertical" : "horizontal" + + // Check against all other trace edges for intersections + for (const otherEdge of otherEdges) { + const otherOrientation = + otherEdge.from.x === otherEdge.to.x ? "vertical" : "horizontal" + + // Only check perpendicular edges + if (edgeOrientation === otherOrientation) continue + + // Check if the edges intersect + const intersection = doesLineIntersectLine( + [edge.from, edge.to], + [otherEdge.from, otherEdge.to], + { lineThickness: 0.01 }, + ) + + if (intersection) { + // Calculate intersection point + const intersectX = + edgeOrientation === "vertical" ? edge.from.x : otherEdge.from.x + const intersectY = + edgeOrientation === "vertical" ? otherEdge.from.y : edge.from.y + + // Create 3 new edges: before crossing, crossing segment, after crossing + const crossingSegmentLength = 0.1 // mm + const crossingPoint = { x: intersectX, y: intersectY } + + // Calculate points slightly before and after crossing + const beforeCrossing = { + x: + edgeOrientation === "vertical" + ? crossingPoint.x + : crossingPoint.x - crossingSegmentLength / 2, + y: + edgeOrientation === "vertical" + ? crossingPoint.y - crossingSegmentLength / 2 + : crossingPoint.y, + } + + const afterCrossing = { + x: + edgeOrientation === "vertical" + ? crossingPoint.x + : crossingPoint.x + crossingSegmentLength / 2, + y: + edgeOrientation === "vertical" + ? crossingPoint.y + crossingSegmentLength / 2 + : crossingPoint.y, + } + + // Replace the original edge with 3 new edges + const newEdges = [ + { from: edge.from, to: beforeCrossing }, + { from: beforeCrossing, to: afterCrossing, is_crossing: true }, + { from: afterCrossing, to: edge.to }, + ] + + // Replace the original edge with our new edges + edges.splice(i, 1, ...newEdges) + i += 2 // Skip the newly inserted edges + } + } + } } diff --git a/package.json b/package.json index 01037a6..c17cbd9 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "@tscircuit/props": "^0.0.86", "@tscircuit/schematic-autolayout": "^0.0.5", "@tscircuit/soup-util": "^0.0.33", - "circuit-json": "^0.0.96", + "circuit-json": "^0.0.97", "circuit-json-to-connectivity-map": "^0.0.17", "circuit-to-svg": "^0.0.62", "format-si-unit": "^0.0.2", diff --git a/tests/repros/__snapshots__/repro4-schematic-trace-overlap-schematic.snap.svg b/tests/repros/__snapshots__/repro4-schematic-trace-overlap-schematic.snap.svg index 0ac1556..0203c5a 100644 --- a/tests/repros/__snapshots__/repro4-schematic-trace-overlap-schematic.snap.svg +++ b/tests/repros/__snapshots__/repro4-schematic-trace-overlap-schematic.snap.svg @@ -9,4 +9,4 @@ .pin-number { fill: rgb(169, 0, 0); } .port-label { fill: rgb(0, 100, 100); } .component-name { fill: rgb(0, 100, 100); } - -3,-2-3,-1-3,0-3,1-3,2-2,-2-2,-1-2,0-2,1-2,2-1,-2-1,-1-1,0-1,1-1,20,-20,-10,00,10,21,-21,-11,01,11,2R110kΩR210kΩR310kΩ \ No newline at end of file + -3,-2-3,-1-3,0-3,1-3,2-2,-2-2,-1-2,0-2,1-2,2-1,-2-1,-1-1,0-1,1-1,20,-20,-10,00,10,21,-21,-11,01,11,2R110kΩR210kΩR310kΩ \ No newline at end of file diff --git a/tests/repros/repro4-schematic-trace-overlap.test.tsx b/tests/repros/repro4-schematic-trace-overlap.test.tsx index 7c6c56d..c2acd57 100644 --- a/tests/repros/repro4-schematic-trace-overlap.test.tsx +++ b/tests/repros/repro4-schematic-trace-overlap.test.tsx @@ -29,5 +29,26 @@ test("repro4 schematic trace overlap", async () => { circuit.render() + // Get the schematic traces + const traces = circuit.db.schematic_trace.list() + + // Find edges with is_crossing=true + const crossingEdges = traces.flatMap((trace) => + trace.edges.filter((edge) => edge.is_crossing), + ) + + // There should be exactly one crossing + expect(crossingEdges.length).toBe(1) + + const crossingEdge = crossingEdges[0] + expect(crossingEdge.is_crossing).toBe(true) + + // Check crossing segment length is ~0.1mm + const length = Math.sqrt( + (crossingEdge.to.x - crossingEdge.from.x) ** 2 + + (crossingEdge.to.y - crossingEdge.from.y) ** 2, + ) + expect(length).toBeCloseTo(0.1, 2) + expect(circuit).toMatchSchematicSnapshot(import.meta.path) })