Skip to content
Open
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
88 changes: 84 additions & 4 deletions lib/RectDiffPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import { BasePipelineSolver, definePipelineStep } from "@tscircuit/solver-utils"
import type { SimpleRouteJson } from "./types/srj-types"
import type { GridFill3DOptions } from "./solvers/rectdiff/types"
import { RectDiffSolver } from "./solvers/RectDiffSolver"
import { EdgeSpatialHashIndexManager } from "./solvers/GapFillSolver/EdgeSpatialHashIndexManager"
import type { CapacityMeshNode } from "./types/capacity-mesh-types"
import type { GraphicsObject } from "graphics-debug"
import { createBaseVisualization } from "./solvers/rectdiff/visualization"

export interface RectDiffPipelineInput {
simpleRouteJson: SimpleRouteJson
gridOptions?: Partial<GridFill3DOptions>
/** Maximum distance between edges to consider for gap filling (default: 10) */
gapFillMaxEdgeDistance?: number
/** Number of gap fill iterations to run (default: 3) */
gapFillIterations?: number
}

export class RectDiffPipeline extends BasePipelineSolver<RectDiffPipelineInput> {
rectDiffSolver?: RectDiffSolver
gapFillSolver?: EdgeSpatialHashIndexManager
override MAX_ITERATIONS: number = 100e6

override pipelineDef = [
definePipelineStep(
Expand All @@ -30,20 +37,93 @@ export class RectDiffPipeline extends BasePipelineSolver<RectDiffPipelineInput>
},
},
),
definePipelineStep(
"gapFillSolver",
EdgeSpatialHashIndexManager,
(instance) => {
const rectDiffSolver =
instance.getSolver<RectDiffSolver>("rectDiffSolver")!
const rectDiffState = (rectDiffSolver as any).state

return [
{
simpleRouteJson: instance.inputProblem.simpleRouteJson,
placedRects: rectDiffState.placed || [],
obstaclesByLayer: rectDiffState.obstaclesByLayer || [],
maxEdgeDistance: instance.inputProblem.gapFillMaxEdgeDistance ?? 10,
repeatCount: instance.inputProblem.gapFillIterations ?? 3,
},
]
},
{
onSolved: () => {
// Gap fill completed
},
},
),
]

override getConstructorParams() {
return [this.inputProblem]
}

override getOutput(): { meshNodes: CapacityMeshNode[] } {
return this.getSolver<RectDiffSolver>("rectDiffSolver")!.getOutput()
const rectDiffOutput =
this.getSolver<RectDiffSolver>("rectDiffSolver")!.getOutput()
const gapFillSolver =
this.getSolver<EdgeSpatialHashIndexManager>("gapFillSolver")

if (!gapFillSolver) {
return rectDiffOutput
}

const gapFillOutput = gapFillSolver.getOutput()

return {
meshNodes: [...rectDiffOutput.meshNodes, ...gapFillOutput.meshNodes],
}
}

override visualize(): GraphicsObject {
const solver = this.getSolver<RectDiffSolver>("rectDiffSolver")
if (solver) {
return solver.visualize()
const gapFillSolver =
this.getSolver<EdgeSpatialHashIndexManager>("gapFillSolver")
const rectDiffSolver = this.getSolver<RectDiffSolver>("rectDiffSolver")

if (gapFillSolver && !gapFillSolver.solved) {
return gapFillSolver.visualize()
}

if (rectDiffSolver) {
const baseViz = rectDiffSolver.visualize()
if (gapFillSolver?.solved) {
const gapFillOutput = gapFillSolver.getOutput()
const gapFillRects = gapFillOutput.meshNodes.map((node) => {
const minZ = Math.min(...node.availableZ)
const colors = [
{ fill: "#dbeafe", stroke: "#3b82f6" },
{ fill: "#fef3c7", stroke: "#f59e0b" },
{ fill: "#d1fae5", stroke: "#10b981" },
]
const color = colors[minZ % colors.length]!

return {
center: node.center,
width: node.width,
height: node.height,
fill: color.fill,
stroke: color.stroke,
label: `capacity node (gap fill)\nz: [${node.availableZ.join(", ")}]`,
}
})

return {
...baseViz,
title: "RectDiff Pipeline (with Gap Fill)",
rects: [...(baseViz.rects || []), ...gapFillRects],
}
}

return baseViz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use combineVisualizations, same as the AutoroutingPipelineSolver (all pipelines do is combine visualizations, they shouldnt do anything unique- each stage gets its own “step”, check AutoroutingPipelineSolver)

}

// Show board and obstacles even before solver is initialized
Expand Down
44 changes: 44 additions & 0 deletions lib/data-structures/FlatbushIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Flatbush from "flatbush"

export interface ISpatialIndex<T> {
insert(item: T, minX: number, minY: number, maxX: number, maxY: number): void
finish(): void
search(minX: number, minY: number, maxX: number, maxY: number): T[]
clear(): void
}

export class FlatbushIndex<T> implements ISpatialIndex<T> {
private index: Flatbush
private items: T[] = []
private currentIndex = 0
private capacity: number

constructor(numItems: number) {
this.capacity = Math.max(1, numItems)
this.index = new Flatbush(this.capacity)
}

insert(item: T, minX: number, minY: number, maxX: number, maxY: number) {
if (this.currentIndex >= this.index.numItems) {
throw new Error("Exceeded initial capacity")
}
this.items[this.currentIndex] = item
this.index.add(minX, minY, maxX, maxY)
this.currentIndex++
}

finish() {
this.index.finish()
}

search(minX: number, minY: number, maxX: number, maxY: number): T[] {
const ids = this.index.search(minX, minY, maxX, maxY)
return ids.map((id) => this.items[id] || null).filter(Boolean) as T[]
}

clear() {
this.items = []
this.currentIndex = 0
this.index = new Flatbush(this.capacity)
}
}
Loading