Skip to content

Commit

Permalink
implement is_crossing
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Nov 8, 2024
1 parent 0a8d5c1 commit 5e3f767
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { doesLineIntersectLine } from "@tscircuit/math-utils"
import type { SoupUtilObjects } from "@tscircuit/soup-util"
import type { SchematicTrace } from "circuit-json"

Expand All @@ -13,5 +14,77 @@ export const createSchematicTraceCrossingSegments = ({
edges: SchematicTrace["edges"]
db: SoupUtilObjects
}) => {
// 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
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 5e3f767

Please sign in to comment.