@tscircuit/rectdiff 0.0.31 → 0.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/pages/arduino-uno/inner2-ground-bottom-power.page.tsx +16 -0
- package/pages/arduino-uno/inner2-ground-inner1-power.page.tsx +16 -0
- package/test-assets/arduino-uno-inner2-ground-bottom-power.json +9687 -0
- package/test-assets/arduino-uno-inner2-ground-inner1-power.json +9687 -0
- package/tests/solver/arduino-uno-inner2-ground-bottom-power/__snapshots__/arduino-uno-inner2-ground-bottom-power.snap.svg +44 -0
- package/tests/solver/arduino-uno-inner2-ground-bottom-power/arduino-uno-inner2-ground-bottom-power.test.ts +94 -0
- package/tests/solver/arduino-uno-inner2-ground-inner1-power/__snapshots__/arduino-uno-inner2-ground-inner1-power.snap.svg +44 -0
- package/tests/solver/arduino-uno-inner2-ground-inner1-power/arduino-uno-inner2-ground-inner1-power.test.ts +94 -0
- package/tests/srj-layer-summary.test.ts +94 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import simpleRouteJson from "../../../test-assets/arduino-uno-inner2-ground-inner1-power.json"
|
|
3
|
+
import {
|
|
4
|
+
getBounds,
|
|
5
|
+
getSvgFromGraphicsObject,
|
|
6
|
+
mergeGraphics,
|
|
7
|
+
stackGraphicsVertically,
|
|
8
|
+
type GraphicsObject,
|
|
9
|
+
type Rect,
|
|
10
|
+
} from "graphics-debug"
|
|
11
|
+
import { RectDiffPipeline } from "lib/RectDiffPipeline"
|
|
12
|
+
import { makeCapacityMeshNodeWithLayerInfo } from "tests/fixtures/makeCapacityMeshNodeWithLayerInfo"
|
|
13
|
+
import { makeSimpleRouteOutlineGraphics } from "tests/fixtures/makeSimpleRouteOutlineGraphics"
|
|
14
|
+
|
|
15
|
+
test("arduino-uno-inner2-ground-inner1-power", async () => {
|
|
16
|
+
const solver = new RectDiffPipeline({
|
|
17
|
+
simpleRouteJson,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const outline = makeSimpleRouteOutlineGraphics(simpleRouteJson)
|
|
21
|
+
|
|
22
|
+
solver.solve()
|
|
23
|
+
|
|
24
|
+
const { meshNodes } = solver.getOutput()
|
|
25
|
+
const rectsByCombo = makeCapacityMeshNodeWithLayerInfo(meshNodes)
|
|
26
|
+
const allGraphicsObjects: GraphicsObject[] = []
|
|
27
|
+
|
|
28
|
+
for (const z of Array.from(
|
|
29
|
+
{ length: simpleRouteJson.layerCount },
|
|
30
|
+
(_, index) => index,
|
|
31
|
+
)) {
|
|
32
|
+
const layerRects: Rect[] = []
|
|
33
|
+
|
|
34
|
+
for (const [key, rects] of rectsByCombo) {
|
|
35
|
+
const layers = key
|
|
36
|
+
.split(",")
|
|
37
|
+
.map((value) => Number.parseInt(value, 10))
|
|
38
|
+
.filter((value) => !Number.isNaN(value))
|
|
39
|
+
|
|
40
|
+
if (layers.includes(z)) {
|
|
41
|
+
layerRects.push(...rects)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let labelY = 0
|
|
46
|
+
|
|
47
|
+
if (layerRects.length > 0) {
|
|
48
|
+
let maxY = -Infinity
|
|
49
|
+
|
|
50
|
+
for (const rect of layerRects) {
|
|
51
|
+
const top = rect.center.y + rect.height * (2 / 3)
|
|
52
|
+
|
|
53
|
+
if (top > maxY) maxY = top
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
labelY = maxY
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const graphics: GraphicsObject = {
|
|
60
|
+
title: `RectDiffPipeline - z${z}`,
|
|
61
|
+
texts: [
|
|
62
|
+
{
|
|
63
|
+
anchorSide: "top_right",
|
|
64
|
+
text: `Layer z=${z}`,
|
|
65
|
+
x: 0,
|
|
66
|
+
y: labelY,
|
|
67
|
+
fontSize: 0.5,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
coordinateSystem: "cartesian",
|
|
71
|
+
rects: layerRects,
|
|
72
|
+
points: [],
|
|
73
|
+
lines: [],
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
allGraphicsObjects.push(mergeGraphics(graphics, outline))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const stackedGraphics = stackGraphicsVertically(allGraphicsObjects)
|
|
80
|
+
const bounds = getBounds(stackedGraphics)
|
|
81
|
+
const boundsWidth = Math.max(1, bounds.maxX - bounds.minX)
|
|
82
|
+
const boundsHeight = Math.max(1, bounds.maxY - bounds.minY)
|
|
83
|
+
const svgWidth = 640
|
|
84
|
+
const svgHeight = Math.max(
|
|
85
|
+
svgWidth,
|
|
86
|
+
Math.ceil((boundsHeight / boundsWidth) * svgWidth),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
const svg = getSvgFromGraphicsObject(stackedGraphics, {
|
|
90
|
+
svgWidth,
|
|
91
|
+
svgHeight,
|
|
92
|
+
})
|
|
93
|
+
await expect(svg).toMatchSvgSnapshot(import.meta.path)
|
|
94
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { stat } from "node:fs/promises"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import type { SimpleRouteJson } from "lib/types/srj-types"
|
|
5
|
+
import { RectDiffPipeline } from "lib/RectDiffPipeline"
|
|
6
|
+
|
|
7
|
+
const srjGlobs = ["tests/**/*.json", "test-assets/*.json"]
|
|
8
|
+
|
|
9
|
+
const readSimpleRouteJson = async (
|
|
10
|
+
filePath: string,
|
|
11
|
+
): Promise<SimpleRouteJson | null> => {
|
|
12
|
+
const json = await Bun.file(filePath).json()
|
|
13
|
+
|
|
14
|
+
if (Array.isArray(json)) {
|
|
15
|
+
const first = json[0]
|
|
16
|
+
return first?.simpleRouteJson ?? first?.simple_route_json ?? null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return json.simple_route_json ?? json.simpleRouteJson ?? json
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
test("solve srj files and print multi-layer node summary", async () => {
|
|
23
|
+
const srjFiles: Array<{
|
|
24
|
+
filePath: string
|
|
25
|
+
size: number
|
|
26
|
+
srj: SimpleRouteJson
|
|
27
|
+
}> = []
|
|
28
|
+
|
|
29
|
+
for (const pattern of srjGlobs) {
|
|
30
|
+
for await (const filePath of new Bun.Glob(pattern).scan(".")) {
|
|
31
|
+
const srj = await readSimpleRouteJson(filePath)
|
|
32
|
+
|
|
33
|
+
if (!srj?.bounds || !srj?.connections || !srj?.obstacles) continue
|
|
34
|
+
|
|
35
|
+
const { size } = await stat(filePath)
|
|
36
|
+
srjFiles.push({ filePath, size, srj })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const largestFiles = srjFiles.sort((a, b) => b.size - a.size)
|
|
41
|
+
expect(largestFiles.length).toBeGreaterThan(0)
|
|
42
|
+
|
|
43
|
+
const rows: Array<Record<string, string | number>> = []
|
|
44
|
+
|
|
45
|
+
for (const { filePath, srj } of largestFiles) {
|
|
46
|
+
const solver = new RectDiffPipeline({ simpleRouteJson: srj })
|
|
47
|
+
solver.solve()
|
|
48
|
+
|
|
49
|
+
const allMeshNodes = solver.getOutput().meshNodes
|
|
50
|
+
const meshNodes = allMeshNodes.filter((node) => !node._containsObstacle)
|
|
51
|
+
expect(meshNodes.length).toBeGreaterThan(0)
|
|
52
|
+
|
|
53
|
+
let totalVolume = 0
|
|
54
|
+
let obstacleVolume = 0
|
|
55
|
+
let multiLayerVolume = 0
|
|
56
|
+
|
|
57
|
+
for (const node of allMeshNodes) {
|
|
58
|
+
const layerSpan = node.availableZ.length
|
|
59
|
+
const volume = node.width * node.height * layerSpan
|
|
60
|
+
|
|
61
|
+
totalVolume += volume
|
|
62
|
+
|
|
63
|
+
if (node._containsObstacle) {
|
|
64
|
+
obstacleVolume += volume
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const node of meshNodes) {
|
|
69
|
+
const layerSpan = node.availableZ.length
|
|
70
|
+
const volume = node.width * node.height * layerSpan
|
|
71
|
+
|
|
72
|
+
if (layerSpan >= 2) {
|
|
73
|
+
multiLayerVolume += volume
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const usableVolume = totalVolume - obstacleVolume
|
|
78
|
+
|
|
79
|
+
rows.push({
|
|
80
|
+
file: path.basename(filePath),
|
|
81
|
+
multi_volume_pct:
|
|
82
|
+
usableVolume > 0
|
|
83
|
+
? `${((multiLayerVolume / usableVolume) * 100).toFixed(1)}%`
|
|
84
|
+
: "0.0%",
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
console.table(
|
|
89
|
+
rows.map((row) => ({
|
|
90
|
+
file: row.file,
|
|
91
|
+
multi_volume_pct: row.multi_volume_pct,
|
|
92
|
+
})),
|
|
93
|
+
)
|
|
94
|
+
})
|