Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import RBush from "rbush"
import { rectToTree } from "../../utils/rectToTree"
import { sameTreeRect } from "../../utils/sameTreeRect"
import { ensureBoardVoidRectsInObstacleIndex } from "lib/utils/ensureBoardVoidRectsInObstacleIndex"

export type RectDiffExpansionSolverSnapshot = {
srj: SimpleRouteJson
Expand Down Expand Up @@ -108,6 +109,10 @@ export class RectDiffExpansionSolver extends BaseSolver {
})
}
}
this.input.obstacleIndexByLayer = ensureBoardVoidRectsInObstacleIndex(
this.boardVoidRects,
this.input.obstacleIndexByLayer,
)

this.placedIndexByLayer = Array.from(
{ length: this.layerCount },
Expand Down
5 changes: 5 additions & 0 deletions lib/solvers/RectDiffSeedingSolver/RectDiffSeedingSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isFullyOccupiedAtPoint } from "lib/utils/isFullyOccupiedAtPoint"
import { resizeSoftOverlaps } from "../../utils/resizeSoftOverlaps"
import RBush from "rbush"
import type { RTreeRect } from "lib/types/capacity-mesh-types"
import { ensureBoardVoidRectsInObstacleIndex } from "lib/utils/ensureBoardVoidRectsInObstacleIndex"

export type RectDiffSeedingSolverInput = {
simpleRouteJson: SimpleRouteJson
Expand Down Expand Up @@ -110,6 +111,10 @@ export class RectDiffSeedingSolver extends BaseSolver {
this.bounds = bounds
this.options = options
this.boardVoidRects = this.input.boardVoidRects
this.input.obstacleIndexByLayer = ensureBoardVoidRectsInObstacleIndex(
this.boardVoidRects,
this.input.obstacleIndexByLayer,
)
this.gridIndex = 0
this.candidates = []
this.placed = []
Expand Down
37 changes: 37 additions & 0 deletions lib/utils/ensureBoardVoidRectsInObstacleIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import RBush from "rbush"
import type { XYRect } from "lib/rectdiff-types"
import type { RTreeRect } from "lib/types/capacity-mesh-types"
import { rectToTree } from "./rectToTree"
import { sameTreeRect } from "./sameTreeRect"

export const ensureBoardVoidRectsInObstacleIndex = (
boardVoidRects: XYRect[] | undefined,
obstacleIndexByLayer: Array<RBush<RTreeRect>>,
): Array<RBush<RTreeRect>> => {
if (!boardVoidRects?.length) return obstacleIndexByLayer

for (let z = 0; z < obstacleIndexByLayer.length; z++) {
if (!obstacleIndexByLayer[z]) {
obstacleIndexByLayer[z] = new RBush<RTreeRect>()
}
const tree = obstacleIndexByLayer[z]!
const existing = tree.all()
const seen = new Set<string>()
for (const rect of existing) {
seen.add(`${rect.minX}:${rect.minY}:${rect.maxX}:${rect.maxY}`)
}

for (const rect of boardVoidRects) {
const treeRect = rectToTree(rect)
const key = `${treeRect.minX}:${treeRect.minY}:${treeRect.maxX}:${treeRect.maxY}`
if (seen.has(key)) continue
if (!existing.some((item) => sameTreeRect(item, treeRect))) {
tree.insert(treeRect)
existing.push(treeRect)
seen.add(key)
}
}
}

return obstacleIndexByLayer
}