Skip to content
Merged
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
186 changes: 174 additions & 12 deletions src/helpers/boolean-difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface FootprintElement {
y?: number
width?: number
height?: number
radius?: number
outer_diameter?: number
hole_diameter?: number
outer_height?: number
Expand All @@ -30,6 +31,145 @@ interface FootprintElement {
route?: Array<{ x: number; y: number }>
points?: Array<{ x: number; y: number }>
shape?: string
corner_radius?: number
}

const EPSILON = 1e-9

function createRectanglePolygon(
centerX: number,
centerY: number,
width: number,
height: number,
): Flatten.Polygon {
const halfWidth = width / 2
const halfHeight = height / 2

return new Flatten.Polygon([
[centerX - halfWidth, centerY - halfHeight],
[centerX + halfWidth, centerY - halfHeight],
[centerX + halfWidth, centerY + halfHeight],
[centerX - halfWidth, centerY + halfHeight],
])
}

function createRoundedRectanglePolygon(
centerX: number,
centerY: number,
width: number,
height: number,
cornerRadius: number,
): Flatten.Polygon {
const halfWidth = width / 2
const halfHeight = height / 2
const radius = Math.max(0, Math.min(cornerRadius, halfWidth, halfHeight))

if (radius <= EPSILON) {
return createRectanglePolygon(centerX, centerY, width, height)
}

const shapes: Array<Flatten.Segment | Flatten.Arc> = []
const addSegment = (
startX: number,
startY: number,
endX: number,
endY: number,
) => {
if (Math.hypot(endX - startX, endY - startY) <= EPSILON) return
shapes.push(
new Flatten.Segment(
new Flatten.Point(startX, startY),
new Flatten.Point(endX, endY),
),
)
}

addSegment(
centerX - halfWidth + radius,
centerY - halfHeight,
centerX + halfWidth - radius,
centerY - halfHeight,
)
shapes.push(
new Flatten.Arc(
new Flatten.Point(
centerX + halfWidth - radius,
centerY - halfHeight + radius,
),
radius,
-Math.PI / 2,
0,
true,
),
)
addSegment(
centerX + halfWidth,
centerY - halfHeight + radius,
centerX + halfWidth,
centerY + halfHeight - radius,
)
shapes.push(
new Flatten.Arc(
new Flatten.Point(
centerX + halfWidth - radius,
centerY + halfHeight - radius,
),
radius,
0,
Math.PI / 2,
true,
),
)
addSegment(
centerX + halfWidth - radius,
centerY + halfHeight,
centerX - halfWidth + radius,
centerY + halfHeight,
)
shapes.push(
new Flatten.Arc(
new Flatten.Point(
centerX - halfWidth + radius,
centerY + halfHeight - radius,
),
radius,
Math.PI / 2,
Math.PI,
true,
),
)
addSegment(
centerX - halfWidth,
centerY + halfHeight - radius,
centerX - halfWidth,
centerY - halfHeight + radius,
)
shapes.push(
new Flatten.Arc(
new Flatten.Point(
centerX - halfWidth + radius,
centerY - halfHeight + radius,
),
radius,
Math.PI,
(3 * Math.PI) / 2,
true,
),
)

return new Flatten.Polygon(shapes)
}

function getSmtPadCornerRadius(element: FootprintElement): number {
if (typeof element.corner_radius === "number") {
return element.corner_radius
}

if (element.shape === "pill") {
return Math.min(element.width ?? 0, element.height ?? 0) / 2
}

return 0
}

/**
Expand All @@ -39,21 +179,17 @@ function elementToPolygon(element: FootprintElement): Flatten.Polygon | null {
try {
if (
element.type === "pcb_smtpad" &&
element.width &&
element.height &&
element.shape === "circle" &&
typeof element.radius === "number" &&
element.x !== undefined &&
element.y !== undefined
) {
// Create rectangular pad polygon
const halfWidth = element.width / 2
const halfHeight = element.height / 2
const points = [
new Flatten.Point(element.x - halfWidth, element.y - halfHeight),
new Flatten.Point(element.x + halfWidth, element.y - halfHeight),
new Flatten.Point(element.x + halfWidth, element.y + halfHeight),
new Flatten.Point(element.x - halfWidth, element.y + halfHeight),
]
return new Flatten.Polygon(points)
return new Flatten.Polygon(
new Flatten.Circle(
new Flatten.Point(element.x, element.y),
element.radius,
),
)
}

if (
Expand All @@ -66,6 +202,32 @@ function elementToPolygon(element: FootprintElement): Flatten.Polygon | null {
return new Flatten.Polygon(points)
}

if (
element.type === "pcb_smtpad" &&
element.width &&
element.height &&
element.x !== undefined &&
element.y !== undefined
) {
const cornerRadius = getSmtPadCornerRadius(element)
if (cornerRadius > EPSILON) {
return createRoundedRectanglePolygon(
element.x,
element.y,
element.width,
element.height,
cornerRadius,
)
}

return createRectanglePolygon(
element.x,
element.y,
element.width,
element.height,
)
}

if (
element.type === "pcb_plated_hole" &&
element.outer_diameter &&
Expand Down
56 changes: 56 additions & 0 deletions tests/boolean-difference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect, test } from "bun:test"
import { createBooleanDifferenceVisualization } from "../src/helpers/boolean-difference"

test("boolean difference preserves rounded SMT pad corners", () => {
const svg = createBooleanDifferenceVisualization(
[
{
type: "pcb_smtpad",
shape: "rect",
x: 0,
y: 0,
width: 1,
height: 0.5,
corner_radius: 0.1,
},
],
[
{
type: "pcb_smtpad",
shape: "rect",
x: 0,
y: 0,
width: 1,
height: 0.5,
corner_radius: 0.1,
},
],
)

expect(svg).toContain("A0.1,0.1")
})

test("boolean difference renders circular SMT pads as circles", () => {
const svg = createBooleanDifferenceVisualization(
[
{
type: "pcb_smtpad",
shape: "circle",
x: 0,
y: 0,
radius: 0.5,
},
],
[
{
type: "pcb_smtpad",
shape: "circle",
x: 0,
y: 0,
radius: 0.5,
},
],
)

expect(svg).toContain("A0.5,0.5")
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading