Skip to content

Commit 5057a95

Browse files
committed
adding piggy bank benchmark for testing mesh preprocessing development
1 parent 1b962b6 commit 5057a95

4 files changed

Lines changed: 60004 additions & 1 deletion

File tree

examples/scripts/slice-piggy.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Example: slice the piggy bank test model from an STL and write G-code to resources/gcode/benchmarks.
3+
*
4+
* Usage:
5+
* node examples/scripts/slice-piggy.js
6+
*/
7+
8+
const { Polyslice, Printer, Filament } = require("../../src/index");
9+
const fs = require("fs");
10+
const path = require("path");
11+
const THREE = require("three");
12+
13+
async function loadSTLMesh(stlPath) {
14+
const { STLLoader } = await import("three/examples/jsm/loaders/STLLoader.js");
15+
const buffer = fs.readFileSync(stlPath);
16+
const loader = new STLLoader();
17+
const geometry = loader.parse(buffer.buffer);
18+
geometry.computeVertexNormals?.();
19+
const material = new THREE.MeshPhongMaterial({ color: 0x808080, specular: 0x111111, shininess: 200 });
20+
const mesh = new THREE.Mesh(geometry, material);
21+
22+
// Place bottom at Z=0 (build plate)
23+
geometry.computeBoundingBox();
24+
const bb = geometry.boundingBox;
25+
const zShift = -bb.min.z;
26+
mesh.position.set(0, 0, zShift);
27+
mesh.updateMatrixWorld();
28+
return mesh;
29+
}
30+
31+
async function main() {
32+
console.log("Polyslice Piggy Bank Example");
33+
console.log("============================\n");
34+
35+
const stlPath = path.join(__dirname, "../../resources/testing/piggy/bank.test.stl");
36+
37+
const printer = new Printer("Ender5");
38+
const filament = new Filament("GenericPLA");
39+
40+
console.log("Printer & Filament:");
41+
console.log(`- Printer: ${printer.model}`);
42+
console.log(`- Volume: ${printer.getSizeX()}x${printer.getSizeY()}x${printer.getSizeZ()} mm`);
43+
console.log(`- Filament: ${filament.name} (${filament.type.toUpperCase()})\n`);
44+
45+
console.log("Loading STL...");
46+
console.log(`- ${stlPath}`);
47+
48+
let mesh;
49+
try {
50+
mesh = await loadSTLMesh(stlPath);
51+
} catch (err) {
52+
console.error("Failed to load STL:", err.message);
53+
process.exit(1);
54+
}
55+
56+
const pos = mesh.geometry.attributes.position;
57+
const triApprox = pos ? ((pos.count / 3) | 0) : null;
58+
console.log("\u2705 Mesh loaded");
59+
console.log(`- Geometry: ${mesh.geometry.type}`);
60+
console.log(`- Vertices: ${pos ? pos.count : "unknown"}`);
61+
if (pos) console.log(`- Triangles (approx): ${triApprox}\n`);
62+
63+
const slicer = new Polyslice({
64+
printer,
65+
filament,
66+
shellSkinThickness: 0.8,
67+
shellWallThickness: 0.8,
68+
lengthUnit: "millimeters",
69+
timeUnit: "seconds",
70+
infillPattern: "grid",
71+
infillDensity: 20,
72+
bedTemperature: 0,
73+
layerHeight: 0.2,
74+
testStrip: false,
75+
metadata: false,
76+
verbose: true
77+
});
78+
79+
console.log("Slicing model...");
80+
const t0 = Date.now();
81+
const gcode = slicer.slice(mesh);
82+
const dt = Date.now() - t0;
83+
console.log(`\u2705 Sliced in ${dt} ms`);
84+
85+
// Save G-code into resources/gcode/benchmarks
86+
const outDir = path.join(__dirname, "../../resources/gcode/benchmarks");
87+
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
88+
const outPath = path.join(outDir, "piggy-bank.gcode");
89+
fs.writeFileSync(outPath, gcode);
90+
91+
const size = fs.statSync(outPath).size;
92+
const lines = gcode.split("\n");
93+
const layers = lines.filter(l => l.includes("LAYER:")).length;
94+
95+
console.log("G-code Summary:");
96+
console.log(`- File: ${outPath}`);
97+
console.log(`- Size: ${(size/1024).toFixed(1)} KB`);
98+
console.log(`- Lines: ${lines.length}`);
99+
console.log(`- Layers: ${layers}`);
100+
console.log("Done.");
101+
}
102+
103+
main().catch(err => {
104+
console.error("Error:", err);
105+
process.exit(1);
106+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"slice:wayfinding": "npm run compile && node examples/scripts/slice-holes.js && node examples/scripts/slice-pillars.js",
3232
"slice:skin": "npm run compile && node examples/scripts/slice-arch.js && node examples/scripts/slice-dome.js && node examples/scripts/slice-pyramid.js",
3333
"slice:matryoshka": "npm run compile && node examples/scripts/slice-matryoshka.js",
34-
"slice:benchmarks": "npm run compile && node examples/scripts/slice-lego.js && node examples/scripts/slice-benchy.js low-poly",
34+
"slice:benchmarks": "npm run compile && node examples/scripts/slice-lego.js && node examples/scripts/slice-piggy.js && node examples/scripts/slice-benchy.js low-poly",
3535
"lint": "eslint src/**/*.js",
3636
"lint:fix": "eslint src/**/*.js --fix",
3737
"format": "prettier --write src/**/*.js",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:805e26afd672ce467ddd062bbb2d18b78aa24aa4bdca14d0f67d313ba14d7b90
3+
size 6266307

0 commit comments

Comments
 (0)