This is a 3D rectangle diffing algorithm made to quickly break apart a circuit board into capacity nodes for the purpose of global routing.
import { RectDiffSolver } from "@tscircuit/rectdiff" import type { SimpleRouteJson } from "@tscircuit/rectdiff" // Define your circuit board layout const simpleRouteJson: SimpleRouteJson = { bounds: { minX: 0, maxX: 10, minY: 0, maxY: 10, }, obstacles: [ { type: "rect", layers: ["top"], center: { x: 2.5, y: 2.5 }, width: 2, height: 2, connectedTo: [], }, ], connections: [], layerCount: 2, minTraceWidth: 0.15, } // Create and run the solver const solver = new RectDiffSolver({ simpleRouteJson, mode: "grid", }) solver.solve() // Get the capacity mesh nodes const output = solver.getOutput() console.log(`Generated ${output.meshNodes.length} capacity mesh nodes`) // Each mesh node contains: // - center: { x, y } - center point of the node // - width, height - dimensions // - availableZ - array of available z-layers (e.g., [0, 1, 2])
const solver = new RectDiffSolver({ simpleRouteJson, mode: "grid", gridOptions: { minSingle: { width: 0.4, height: 0.4 }, minMulti: { width: 1.0, height: 1.0, minLayers: 2 }, preferMultiLayer: true, }, }) solver.solve()
The solver supports incremental solving for visualization and progress tracking:
const solver = new RectDiffSolver({ simpleRouteJson }) solver.setup() // Step through the algorithm incrementally while (!solver.solved) { solver.step() // Get current progress const progress = solver.computeProgress() console.log(`Progress: ${(progress * 100).toFixed(1)}%`) // Visualize current state const graphicsObject = solver.visualize() // Use graphicsObject with graphics-debug package } const output = solver.getOutput()
const output = solver.getOutput() // Iterate through mesh nodes for (const node of output.meshNodes) { console.log(`Node at (${node.center.x}, ${node.center.y})`) console.log(` Size: ${node.width} x ${node.height}`) console.log(` Available layers: ${node.availableZ?.join(", ") || "none"}`) // Use node for routing decisions if (node.availableZ && node.availableZ.length >= 2) { console.log(" This node spans multiple layers!") } }
-
simpleRouteJson(required): The circuit board layout definitionbounds: Board boundaries (minX, maxX, minY, maxY)obstacles: Array of rectangular obstacles on the boardconnections: Connection requirements between pointslayerCount: Number of layers in the boardminTraceWidth: Minimum trace width for routing
-
mode: "grid" | "exact" (default: "grid")- Currently only "grid" mode is implemented
-
gridOptions: Fine-tune the grid-based expansion algorithmminSingle: Minimum dimensions for single-layer nodesminMulti: Minimum dimensions and layer count for multi-layer nodespreferMultiLayer: Whether to prefer multi-layer spanning nodes
setup(): Initialize the solverstep(): Execute one iteration stepsolve(): Run solver to completioncomputeProgress(): Get current progress (0.0 to 1.0)getOutput(): Get the capacity mesh nodes resultvisualize(): Get GraphicsObject for visualization with graphics-debug