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
174 changes: 174 additions & 0 deletions generate_preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { SingleInnerPartitionPackingSolver } from "./lib/solvers/PackInnerPartitionsSolver/SingleInnerPartitionPackingSolver"
import type { PartitionInputProblem } from "./lib/types/InputProblem"

// 构造包含 5 个解耦电容的 Mock Input
const problem: PartitionInputProblem = {
isPartition: true,
partitionType: "decoupling_caps",
chipMap: {
C1: {
chipId: "C1",
pins: ["C1_P1", "C1_P2"],
size: { x: 1.0, y: 0.5 },
availableRotations: [0, 180],
},
C2: {
chipId: "C2",
pins: ["C2_P1", "C2_P2"],
size: { x: 1.2, y: 0.6 },
availableRotations: [0, 180],
},
C3: {
chipId: "C3",
pins: ["C3_P1", "C3_P2"],
size: { x: 0.8, y: 0.4 },
availableRotations: [0, 180],
},
C4: {
chipId: "C4",
pins: ["C4_P1", "C4_P2"],
size: { x: 1.5, y: 0.7 },
availableRotations: [0, 180],
},
C5: {
chipId: "C5",
pins: ["C5_P1", "C5_P2"],
size: { x: 0.9, y: 0.45 },
availableRotations: [0, 180],
},
},
chipPinMap: {
C1_P1: { pinId: "C1_P1", offset: { x: 0, y: 0.25 }, side: "y+" },
C1_P2: { pinId: "C1_P2", offset: { x: 0, y: -0.25 }, side: "y-" },
C2_P1: { pinId: "C2_P1", offset: { x: 0, y: 0.3 }, side: "y+" },
C2_P2: { pinId: "C2_P2", offset: { x: 0, y: -0.3 }, side: "y-" },
C3_P1: { pinId: "C3_P1", offset: { x: 0, y: 0.2 }, side: "y+" },
C3_P2: { pinId: "C3_P2", offset: { x: 0, y: -0.2 }, side: "y-" },
C4_P1: { pinId: "C4_P1", offset: { x: 0, y: 0.35 }, side: "y+" },
C4_P2: { pinId: "C4_P2", offset: { x: 0, y: -0.35 }, side: "y-" },
C5_P1: { pinId: "C5_P1", offset: { x: 0, y: 0.225 }, side: "y+" },
C5_P2: { pinId: "C5_P2", offset: { x: 0, y: -0.225 }, side: "y-" },
},
netMap: {
VCC: { netId: "VCC", isPositiveVoltageSource: true },
GND: { netId: "GND", isGround: true },
},
pinStrongConnMap: {},
netConnMap: {
"C1_P1-VCC": true,
"C1_P2-GND": true,
"C2_P1-VCC": true,
"C2_P2-GND": true,
"C3_P1-VCC": true,
"C3_P2-GND": true,
"C4_P1-VCC": true,
"C4_P2-GND": true,
"C5_P1-VCC": true,
"C5_P2-GND": true,
},
chipGap: 0.2,
partitionGap: 2,
decouplingCapsGap: 0.3,
}

const solver = new SingleInnerPartitionPackingSolver({
partitionInputProblem: problem,
pinIdToStronglyConnectedPins: {},
})

solver.step()

const layout = solver.layout!
const placements = layout.chipPlacements

// 计算总宽度用于 SVG 视图框
const chipIds = Object.keys(problem.chipMap).sort()
let totalWidth = 0
let maxHeight = 0
for (const id of chipIds) {
const chip = problem.chipMap[id]!
totalWidth += chip.size.x
totalWidth += problem.decouplingCapsGap!
maxHeight = Math.max(maxHeight, chip.size.y)
}
totalWidth -= problem.decouplingCapsGap!

const viewBoxWidth = totalWidth + 2
const viewBoxHeight = maxHeight + 1

// 生成 SVG
const svgParts: string[] = []
svgParts.push(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-viewBoxWidth / 2} ${-viewBoxHeight / 2} ${viewBoxWidth} ${viewBoxHeight}" width="800" height="400">`,
)
svgParts.push(
` <rect x="${-viewBoxWidth / 2}" y="${-viewBoxHeight / 2}" width="${viewBoxWidth}" height="${viewBoxHeight}" fill="#f5f5f5"/>`,
)

// 绘制每个电容
for (const id of chipIds) {
const placement = placements[id]!
const chip = problem.chipMap[id]!
const halfWidth = chip.size.x / 2
const halfHeight = chip.size.y / 2

const x = placement.x - halfWidth
const y = placement.y - halfHeight

svgParts.push(
` <rect x="${x.toFixed(2)}" y="${y.toFixed(2)}" width="${chip.size.x}" height="${chip.size.y}" fill="#4a90d9" stroke="#2c5a8c" stroke-width="0.05" rx="0.1"/>`,
)
svgParts.push(
` <text x="${placement.x.toFixed(2)}" y="${placement.y.toFixed(2)}" text-anchor="middle" dominant-baseline="middle" fill="white" font-size="0.3" font-family="Arial">${id}</text>`,
)
svgParts.push(
` <text x="${placement.x.toFixed(2)}" y="${(placement.y + halfHeight + 0.4).toFixed(2)}" text-anchor="middle" fill="#333" font-size="0.2" font-family="Arial">x:${placement.x.toFixed(2)}, y:${placement.y.toFixed(2)}</text>`,
)
}

// 添加标题
svgParts.push(
` <text x="0" y="${(-viewBoxHeight / 2 + 0.3).toFixed(2)}" text-anchor="middle" fill="#333" font-size="0.25" font-family="Arial" font-weight="bold">Decoupling Capacitors Linear Layout</text>`,
)
svgParts.push(
` <text x="0" y="${(-viewBoxHeight / 2 + 0.6).toFixed(2)}" text-anchor="middle" fill="#666" font-size="0.18" font-family="Arial">5 caps arranged horizontally with 0.3 gap</text>`,
)

svgParts.push(`</svg>`)

const htmlContent = `<!DOCTYPE html>
<html>
<head>
<title>Decoupling Caps Layout Preview</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background: #fafafa; }
.container { max-width: 900px; margin: 0 auto; }
h1 { color: #333; }
.info { background: #e8f4fc; padding: 15px; border-radius: 8px; margin-bottom: 20px; }
pre { background: #f4f4f4; padding: 15px; border-radius: 8px; overflow-x: auto; }
</style>
</head>
<body>
<div class="container">
<h1>Decoupling Capacitors Linear Layout Preview</h1>
<div class="info">
<p><strong>Algorithm:</strong> _layoutDecouplingCapsLinear()</p>
<p><strong>Gap:</strong> 0.3 | <strong>Coordinate System:</strong> Center-based</p>
<p><strong>Sorting:</strong> chipId localeCompare (deterministic)</p>
</div>
<div>${svgParts.join("\n")}</div>
<h2>Layout JSON</h2>
<pre>${JSON.stringify(layout, null, 2)}</pre>
</div>
</body>
</html>`

await Bun.write("preview.html", htmlContent)
console.log("✅ preview.html generated successfully!")
console.log("📐 Layout Summary:")
for (const id of chipIds) {
const p = placements[id]!
console.log(
` ${id}: x=${p.x.toFixed(2)}, y=${p.y.toFixed(2)}, rotation=${p.ccwRotationDegrees}°`,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export class SingleInnerPartitionPackingSolver extends BaseSolver {
}

override _step() {
// Injection point: if decoupling caps, intercept and use linear layout
if (
this.partitionInputProblem.partitionType === "decoupling_caps" &&
!this.solved
) {
this._layoutDecouplingCapsLinear()
return
}

// Initialize PackSolver2 if not already created
if (!this.activeSubSolver) {
const packInput = this.createPackInput()
Expand All @@ -64,6 +73,52 @@ export class SingleInnerPartitionPackingSolver extends BaseSolver {
}
}

private _layoutDecouplingCapsLinear() {
const input = this.partitionInputProblem

// Step 1: Deterministic sort by chipId for consistent results across environments
const chips = Object.values(input.chipMap).sort((a, b) =>
a.chipId.localeCompare(b.chipId),
)
const gap = input.decouplingCapsGap ?? input.chipGap ?? 0.2

const chipPlacements: Record<string, Placement> = {}

// Step 2: Pre-calculate total width for center alignment
let totalWidth = 0
let maxHeight = 0
for (let i = 0; i < chips.length; i++) {
totalWidth += chips[i]!.size.x
if (i < chips.length - 1) {
totalWidth += gap
}
maxHeight = Math.max(maxHeight, chips[i]!.size.y)
}

// Step 3: Linear coordinate assignment (center-based coordinate system)
let currentX = -(totalWidth / 2)
for (const chip of chips) {
const halfWidth = chip.size.x / 2
currentX += halfWidth

// Move to chip center
chipPlacements[chip.chipId] = {
x: currentX,
y: 0, // horizontal alignment
ccwRotationDegrees: chip.availableRotations?.[0] ?? 0,
}

currentX += halfWidth + gap
}

// Step 4: Package output
this.layout = {
chipPlacements,
groupPlacements: {},
}
this.solved = true
}

private createPackInput(): PackInput {
// Fall back to filtered mapping (weak + strong)
const pinToNetworkMap = createFilteredNetworkMapping({
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
},
"peerDependencies": {
"typescript": "^5"
},
"dependencies": {
"circuit-to-svg": "^0.0.332"
}
}
74 changes: 74 additions & 0 deletions preview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>
<head>
<title>Decoupling Caps Layout Preview</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background: #fafafa; }
.container { max-width: 900px; margin: 0 auto; }
h1 { color: #333; }
.info { background: #e8f4fc; padding: 15px; border-radius: 8px; margin-bottom: 20px; }
pre { background: #f4f4f4; padding: 15px; border-radius: 8px; overflow-x: auto; }
</style>
</head>
<body>
<div class="container">
<h1>Decoupling Capacitors Linear Layout Preview</h1>
<div class="info">
<p><strong>Algorithm:</strong> _layoutDecouplingCapsLinear()</p>
<p><strong>Gap:</strong> 0.3 | <strong>Coordinate System:</strong> Center-based</p>
<p><strong>Sorting:</strong> chipId localeCompare (deterministic)</p>
</div>
<div><svg xmlns="http://www.w3.org/2000/svg" viewBox="-4.3 -0.85 8.6 1.7" width="800" height="400">
<rect x="-4.3" y="-0.85" width="8.6" height="1.7" fill="#f5f5f5"/>
<rect x="-3.30" y="-0.25" width="1" height="0.5" fill="#4a90d9" stroke="#2c5a8c" stroke-width="0.05" rx="0.1"/>
<text x="-2.80" y="0.00" text-anchor="middle" dominant-baseline="middle" fill="white" font-size="0.3" font-family="Arial">C1</text>
<text x="-2.80" y="0.65" text-anchor="middle" fill="#333" font-size="0.2" font-family="Arial">x:-2.80, y:0.00</text>
<rect x="-2.00" y="-0.30" width="1.2" height="0.6" fill="#4a90d9" stroke="#2c5a8c" stroke-width="0.05" rx="0.1"/>
<text x="-1.40" y="0.00" text-anchor="middle" dominant-baseline="middle" fill="white" font-size="0.3" font-family="Arial">C2</text>
<text x="-1.40" y="0.70" text-anchor="middle" fill="#333" font-size="0.2" font-family="Arial">x:-1.40, y:0.00</text>
<rect x="-0.50" y="-0.20" width="0.8" height="0.4" fill="#4a90d9" stroke="#2c5a8c" stroke-width="0.05" rx="0.1"/>
<text x="-0.10" y="0.00" text-anchor="middle" dominant-baseline="middle" fill="white" font-size="0.3" font-family="Arial">C3</text>
<text x="-0.10" y="0.60" text-anchor="middle" fill="#333" font-size="0.2" font-family="Arial">x:-0.10, y:0.00</text>
<rect x="0.60" y="-0.35" width="1.5" height="0.7" fill="#4a90d9" stroke="#2c5a8c" stroke-width="0.05" rx="0.1"/>
<text x="1.35" y="0.00" text-anchor="middle" dominant-baseline="middle" fill="white" font-size="0.3" font-family="Arial">C4</text>
<text x="1.35" y="0.75" text-anchor="middle" fill="#333" font-size="0.2" font-family="Arial">x:1.35, y:0.00</text>
<rect x="2.40" y="-0.23" width="0.9" height="0.45" fill="#4a90d9" stroke="#2c5a8c" stroke-width="0.05" rx="0.1"/>
<text x="2.85" y="0.00" text-anchor="middle" dominant-baseline="middle" fill="white" font-size="0.3" font-family="Arial">C5</text>
<text x="2.85" y="0.63" text-anchor="middle" fill="#333" font-size="0.2" font-family="Arial">x:2.85, y:0.00</text>
<text x="0" y="-0.55" text-anchor="middle" fill="#333" font-size="0.25" font-family="Arial" font-weight="bold">Decoupling Capacitors Linear Layout</text>
<text x="0" y="-0.25" text-anchor="middle" fill="#666" font-size="0.18" font-family="Arial">5 caps arranged horizontally with 0.3 gap</text>
</svg></div>
<h2>Layout JSON</h2>
<pre>{
"chipPlacements": {
"C1": {
"x": -2.8,
"y": 0,
"ccwRotationDegrees": 0
},
"C2": {
"x": -1.4,
"y": 0,
"ccwRotationDegrees": 0
},
"C3": {
"x": -0.09999999999999998,
"y": 0,
"ccwRotationDegrees": 0
},
"C4": {
"x": 1.35,
"y": 0,
"ccwRotationDegrees": 0
},
"C5": {
"x": 2.8500000000000005,
"y": 0,
"ccwRotationDegrees": 0
}
},
"groupPlacements": {}
}</pre>
</div>
</body>
</html>
Loading