Skip to content
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

initial crossing trace implementation #110

Merged
merged 2 commits into from
Nov 8, 2024
Merged
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
Binary file modified bun.lockb
Binary file not shown.
61 changes: 40 additions & 21 deletions lib/sch/svg-object-fns/create-svg-objects-from-sch-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,53 @@ export function createSchematicTrace(
let path = ""

// Process all edges
edges.forEach((edge: any, index: number) => {
// Get the points, applying trace offset
const fromPoint = {
x: edge.from.x ?? edge.from.center?.x,
y: edge.from.y ?? edge.from.center?.y,
}
const toPoint = {
x: edge.to.x ?? edge.to.center?.x,
y: edge.to.y ?? edge.to.center?.y,
}
for (let edgeIndex = 0; edgeIndex < edges.length; edgeIndex++) {
const edge = edges[edgeIndex]!

// Transform the points using the matrix
const [transformedFromX, transformedFromY] = applyToPoint(transform, [
fromPoint.x,
fromPoint.y,
const [screenFromX, screenFromY] = applyToPoint(transform, [
edge.from.x,
edge.from.y,
])
const [transformedToX, transformedToY] = applyToPoint(transform, [
toPoint.x,
toPoint.y,
const [screenToX, screenToY] = applyToPoint(transform, [
edge.to.x,
edge.to.y,
])

// Build the path string
if (index === 0) {
path += `M ${transformedFromX} ${transformedFromY} L ${transformedToX} ${transformedToY}`
if (edge.is_crossing) {
// For crossing traces, create a small arc/hop
const midX = (screenFromX + screenToX) / 2
const midY = (screenFromY + screenToY) / 2

// Calculate perpendicular offset for the arc
const dx = screenToX - screenFromX
const dy = screenToY - screenFromY
const len = Math.sqrt(dx * dx + dy * dy)
const hopHeight = len * 0.7

// Perpendicular vector
const perpX = (-dy / len) * hopHeight
const perpY = (dx / len) * hopHeight

// Control point for the quadratic curve
const controlX = midX + perpX
const controlY = midY - Math.abs(perpY)

// Build the path string with a quadratic curve for the hop
if (edgeIndex === 0) {
path += `M ${screenFromX} ${screenFromY} Q ${controlX} ${controlY} ${screenToX} ${screenToY}`
} else {
path += ` Q ${controlX} ${controlY} ${screenToX} ${screenToY}`
}
}

// Regular straight line for non-crossing traces
if (edgeIndex === 0) {
path += `M ${screenFromX} ${screenFromY} L ${screenToX} ${screenToY}`
} else {
path += ` L ${transformedToX} ${transformedToY}`
path += ` L ${screenToX} ${screenToY}`
}
})
}

// Only create SVG object if we have a valid path
return path
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@storybook/react": "^8.2.5",
"@storybook/react-vite": "^8.2.5",
"@storybook/test": "^8.2.5",
"@tscircuit/core": "^0.0.153",
"@tscircuit/core": "^0.0.155",
"@tscircuit/plop": "^0.0.10",
"@types/bun": "^1.1.9",
"bun-match-svg": "^0.0.6",
Expand All @@ -41,7 +41,7 @@
"vite-tsconfig-paths": "^5.0.1"
},
"peerDependencies": {
"circuit-json": "^0.0.95"
"circuit-json": "^0.0.97"
},
"dependencies": {
"@tscircuit/footprinter": "^0.0.57",
Expand Down
12 changes: 12 additions & 0 deletions tests/sch/__snapshots__/trace-overlap.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions tests/sch/trace-overlap.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test, expect } from "bun:test"
import { convertCircuitJsonToSchematicSvg } from "lib"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("schematic trace overlap", () => {
const { project } = getTestFixture()

project.add(
<board width="20mm" height="20mm">
<resistor
name="R1"
resistance="10k"
footprint="0402"
schX={0}
schY={-2}
/>
<resistor
name="R2"
resistance="10k"
footprint="0402"
schX={-2}
schY={-1}
/>
<resistor name="R3" resistance="10k" footprint="0402" schX={0} schY={2} />

<trace from=".R1 > .pin2" to=".R3 > .pin1" />
<trace from=".R2 > .pin2" to=".R3 > .pin2" />
</board>,
)

expect(
convertCircuitJsonToSchematicSvg(project.getCircuitJson()),
).toMatchSvgSnapshot(import.meta.path)
})
Loading