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

reproduction of trace crossing issue #123

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/sch/convert-circuit-json-to-schematic-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function convertCircuitJsonToSchematicSvg(
.component { fill: none; stroke: ${colorMap.schematic.component_outline}; }
.chip { fill: ${colorMap.schematic.component_body}; stroke: ${colorMap.schematic.component_outline}; }
.component-pin { fill: none; stroke: ${colorMap.schematic.component_outline}; }
.trace { stroke: ${colorMap.schematic.wire}; }
.trace { }
.text { font-family: sans-serif; fill: ${colorMap.schematic.wire}; }
.pin-number { fill: ${colorMap.schematic.pin_number}; }
.port-label { fill: ${colorMap.schematic.reference}; }
Expand Down
14 changes: 9 additions & 5 deletions lib/sch/draw-schematic-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function drawSchematicGrid(params: {
}

// Vertical lines
for (let x = Math.ceil(minX); x <= Math.floor(maxX); x += cellSize) {
for (let x = Math.floor(minX); x <= Math.ceil(maxX); x += cellSize) {
const start = transformPoint(x, minY)
const end = transformPoint(x, maxY)

Expand All @@ -45,7 +45,7 @@ export function drawSchematicGrid(params: {
}

// Horizontal lines
for (let y = Math.ceil(minY); y <= Math.floor(maxY); y += cellSize) {
for (let y = Math.floor(minY); y <= Math.ceil(maxY); y += cellSize) {
const start = transformPoint(minX, y)
const end = transformPoint(maxX, y)

Expand All @@ -66,8 +66,12 @@ export function drawSchematicGrid(params: {

// Add cell labels if enabled
if (labelCells) {
for (let x = Math.ceil(minX); x <= Math.floor(maxX); x += cellSize) {
for (let y = Math.ceil(minY); y <= Math.floor(maxY); y += cellSize) {
const formatPoint = (x: number, y: number) => {
if (cellSize <= 0.1) return `${x.toFixed(1)},${y.toFixed(1)}`
return `${x},${y}`
}
for (let x = Math.floor(minX); x <= Math.ceil(maxX); x += cellSize) {
for (let y = Math.floor(minY); y <= Math.ceil(maxY); y += cellSize) {
const point = transformPoint(x, y)

gridLines.push({
Expand All @@ -89,7 +93,7 @@ export function drawSchematicGrid(params: {
children: [
{
type: "text",
value: `${x},${y}`,
value: formatPoint(x, y),
name: "",
attributes: {},
children: [],
Expand Down
5 changes: 5 additions & 0 deletions lib/sch/get-schematic-bounds-from-circuit-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export function getSchematicBoundsFromCircuitJson(
}
} else if (item.type === "schematic_net_label") {
updateBounds(item.center, { width: 1, height: 1 }, 0)
} else if (item.type === "schematic_trace") {
for (const edge of item.edges) {
updateBounds(edge.from, { width: 0.1, height: 0.1 }, 0)
updateBounds(edge.to, { width: 0.1, height: 0.1 }, 0)
}
}
}

Expand Down
63 changes: 40 additions & 23 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 @@ -12,7 +12,36 @@ export function createSchematicTrace(
if (edges.length === 0) return []
const svgObjects: SvgObject[] = []

let path = ""
let currentPath = ""
const paths: SvgObject[] = []

let colors = [
"rgb(255,0,0)",
"rgb(0,255,0)",
"rgb(0,0,255)",
"rgb(255,255,0)",
"rgb(255, 0, 255)",
"rgb(0, 255,255)",
]
let colorIndex = -1

function addPath() {
paths.push({
name: "path",
type: "element",
attributes: {
class: "trace",
d: currentPath,
stroke: colors[colorIndex++ % colors.length],
fill: "none",
"stroke-width": `${getSchStrokeSize(transform) * (1 + colorIndex / 2)}px`,
"stroke-linecap": "round",
opacity: "0.5",
},
value: "",
children: [],
})
}

// Process edges into an SVG path
for (let edgeIndex = 0; edgeIndex < edges.length; edgeIndex++) {
Expand All @@ -31,13 +60,16 @@ export function createSchematicTrace(
])

// Regular straight line for non-crossing traces
if (edgeIndex === 0 || edges[edgeIndex - 1]?.is_crossing) {
path += `M ${screenFromX} ${screenFromY} L ${screenToX} ${screenToY}`
} else {
path += ` L ${screenToX} ${screenToY}`
}
// if (edgeIndex === 0 || edges[edgeIndex - 1]?.is_crossing) {
addPath()
currentPath = `M ${screenFromX} ${screenFromY} L ${screenToX} ${screenToY}`
// } else {
// currentPath += ` L ${screenToX} ${screenToY}`
// }
}

if (currentPath) addPath()

// Process wire crossings with little "hops" or arcs
for (const edge of edges) {
if (!edge.is_crossing) continue
Expand Down Expand Up @@ -98,23 +130,6 @@ export function createSchematicTrace(
})
}

if (path) {
svgObjects.push({
name: "path",
type: "element",
attributes: {
class: "trace",
d: path,
stroke: colorMap.schematic.wire,
fill: "none",
"stroke-width": `${getSchStrokeSize(transform)}px`,
"stroke-linecap": "round",
},
value: "",
children: [],
})
}

// Add junction circles
if (trace.junctions) {
for (const junction of trace.junctions) {
Expand All @@ -137,5 +152,7 @@ export function createSchematicTrace(
}
}

svgObjects.push(...paths)

return svgObjects
}
12 changes: 12 additions & 0 deletions tests/sch/__snapshots__/crossing-traces.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions tests/sch/crossing-traces.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { test, expect } from "bun:test"
import type { SchematicTrace } from "circuit-json"
import { convertCircuitJsonToSchematicSvg } from "lib"

test("schematic crossing traces", () => {
expect(
convertCircuitJsonToSchematicSvg(
[
{
type: "schematic_trace",
schematic_trace_id: "schematic_trace_2",
source_trace_id: "source_trace_2",
edges: [
{
from: {
route_type: "wire",
x: -3.4662092999999996,
y: 0.004741299999999338,
width: 0.1,
layer: "top",
},
to: {
x: -2.25,
y: 0.004741299999999338,
},
},
{
from: {
x: -2.25,
y: 0.004741299999999338,
},
to: {
x: -2.1500000000000004,
y: 0.004741299999999338,
},
is_crossing: true,
},
{
from: {
route_type: "wire",
x: -3.4662092999999996,
y: 0.004741299999999338,
width: 0.1,
layer: "top",
},
to: {
x: -2.4662092999999996,
y: 0.004741299999999338,
},
},
{
from: {
x: -2.4662092999999996,
y: 0.004741299999999338,
},
to: {
x: -2.3662093,
y: 0.004741299999999338,
},
is_crossing: true,
},
{
from: {
x: -2.3662093,
y: 0.004741299999999338,
},
to: {
route_type: "wire",
x: -2.0999999999999996,
y: 0.004741299999999338,
width: 0.1,
layer: "top",
},
},
{
from: {
route_type: "wire",
x: -2.0999999999999996,
y: 0.004741299999999338,
width: 0.1,
layer: "top",
},
to: {
route_type: "wire",
x: -2.0999999999999996,
y: -0.30000000000000004,
width: 0.1,
layer: "top",
},
},
{
from: {
route_type: "wire",
x: -2.0999999999999996,
y: -0.30000000000000004,
width: 0.1,
layer: "top",
},
to: {
route_type: "wire",
x: -1.2999999999999998,
y: -0.30000000000000004,
width: 0.1,
layer: "top",
},
},
],
junctions: [],
} as SchematicTrace,
],
{
grid: {
cellSize: 0.1,
labelCells: true,
},
},
),
).toMatchSvgSnapshot(import.meta.path)

Check failure on line 118 in tests/sch/crossing-traces.test.tsx

View workflow job for this annotation

GitHub Actions / test

error:

Snapshot does not match. Diff saved at /home/runner/work/circuit-to-svg/circuit-to-svg/tests/sch/__snapshots__/crossing-traces.diff.png at /home/runner/work/circuit-to-svg/circuit-to-svg/tests/sch/crossing-traces.test.tsx:118:5
})
Loading