@tscircuit/rectdiff 0.0.32 → 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
CHANGED
|
@@ -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
|
+
})
|