Skip to content

Commit

Permalink
Merge pull request #100 from ShiboSoftwareDev/main
Browse files Browse the repository at this point in the history
create obstacles over rotated rect
  • Loading branch information
ShiboSoftwareDev authored Dec 9, 2024
2 parents 101268c + 991eb1c commit aa83192
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
86 changes: 86 additions & 0 deletions module/lib/solver-utils/generateApproximatingRects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
interface Point {
x: number
y: number
}

export interface RotatedRect {
center: Point
width: number
height: number
rotation: number
}

interface Rect {
center: Point
width: number
height: number
}

export function generateApproximatingRects(
rotatedRect: RotatedRect,
numRects: number = 4,
): Rect[] {
const { center, width, height, rotation } = rotatedRect
const rects: Rect[] = []

const angleRad = (rotation * Math.PI) / 180
const cosAngle = Math.cos(angleRad)
const sinAngle = Math.sin(angleRad)

const normalizedRotation = ((rotation % 360) + 360) % 360
const sliceAlongWidth =
height <= width
? (normalizedRotation >= 45 && normalizedRotation < 135) ||
(normalizedRotation >= 225 && normalizedRotation < 315)
: (normalizedRotation >= 135 && normalizedRotation < 225) ||
normalizedRotation >= 315 ||
normalizedRotation < 45

if (sliceAlongWidth) {
const sliceWidth = width / numRects

for (let i = 0; i < numRects; i++) {
const x = (i - numRects / 2 + 0.5) * sliceWidth

const rotatedX = -x * cosAngle
const rotatedY = -x * sinAngle

const coverageWidth = sliceWidth * 1.1
const coverageHeight =
Math.abs(height * cosAngle) + Math.abs(sliceWidth * sinAngle)

rects.push({
center: {
x: center.x + rotatedX,
y: center.y + rotatedY,
},
width: coverageWidth,
height: coverageHeight,
})
}
} else {
const sliceHeight = height / numRects

for (let i = 0; i < numRects; i++) {
const y = (i - numRects / 2 + 0.5) * sliceHeight

const rotatedX = -y * sinAngle
const rotatedY = y * cosAngle

const coverageWidth =
Math.abs(width * cosAngle) + Math.abs(sliceHeight * sinAngle)
const coverageHeight = sliceHeight * 1.1

rects.push({
center: {
x: center.x + rotatedX,
y: center.y + rotatedY,
},
width: coverageWidth,
height: coverageHeight,
})
}
}

return rects
}
22 changes: 22 additions & 0 deletions module/lib/solver-utils/getObstaclesFromCircuitJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { Obstacle } from "../types"
import { getObstaclesFromRoute } from "./getObstaclesFromRoute"
import type { ConnectivityMap } from "circuit-json-to-connectivity-map"
import type { AnyCircuitElement } from "circuit-json"
import {
generateApproximatingRects,
type RotatedRect,
} from "./generateApproximatingRects"

const EVERY_LAYER = ["top", "inner1", "inner2", "bottom"]

Expand Down Expand Up @@ -43,6 +47,24 @@ export const getObstaclesFromCircuitJson = (
height: element.height,
connectedTo: withNetId([element.pcb_smtpad_id]),
})
} else if (element.shape === "rotated_rect") {
const rotatedRect: RotatedRect = {
center: { x: element.x, y: element.y },
width: element.width,
height: element.height,
rotation: element.ccw_rotation,
}
const approximatingRects = generateApproximatingRects(rotatedRect)
for (const rect of approximatingRects) {
obstacles.push({
type: "rect",
layers: [element.layer],
center: rect.center,
width: rect.width,
height: rect.height,
connectedTo: withNetId([element.pcb_smtpad_id]),
})
}
}
} else if (element.type === "pcb_keepout") {
if (element.shape === "circle") {
Expand Down

0 comments on commit aa83192

Please sign in to comment.