@rulecad/printcheck 0.3.1 → 0.3.3
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/printcheck.d.mts +2 -0
- package/printcheck.mjs +29 -4
package/package.json
CHANGED
package/printcheck.d.mts
CHANGED
|
@@ -62,8 +62,10 @@ export function islands(polys: any, { layer, overhang, line, firstGap, onLayer }
|
|
|
62
62
|
islands: any[];
|
|
63
63
|
layers: number;
|
|
64
64
|
overhang: number;
|
|
65
|
+
firstSprawl?: undefined;
|
|
65
66
|
fused?: undefined;
|
|
66
67
|
} | {
|
|
68
|
+
firstSprawl: number;
|
|
67
69
|
islands: {
|
|
68
70
|
z: number;
|
|
69
71
|
area: number;
|
package/printcheck.mjs
CHANGED
|
@@ -61,12 +61,26 @@ export function check(polys, printer, { eps = 1e-3, parts = null, slice = true }
|
|
|
61
61
|
for (const k of ['layer', 'line', 'bridge', 'support_gap']) if (printer[k] != null && !(Number(printer[k]) > 0)) throw new Error(`check(model, printer): the printer's "${k}" must be a number above 0 (got ${JSON.stringify(printer[k])})`);
|
|
62
62
|
polys = (Array.isArray(polys) ? polys : []).filter((p) => Array.isArray(p) && p.length >= 3 && p.every((v) => Array.isArray(v) && v.length >= 3 && v.every(Number.isFinite)));
|
|
63
63
|
if (!polys.length) return [{ level: 'fail', text: 'nothing to check: the model has no faces (each face is three or more corners, each [x, y, z] in mm)', code: 'print.empty' }];
|
|
64
|
-
const solids = (parts || []).filter((p) => p.role !== 'support').map((p) => ({ tris: p.triangles, box: bounds(p.triangles) }));
|
|
64
|
+
const solids = (parts || []).filter((p) => p.role !== 'support').map((p) => ({ tris: p.triangles, box: bounds(p.triangles), grid: null }));
|
|
65
|
+
// Each part's faces filed by where they lie across the bed, on a grid, so a ray straight up meets only those near it
|
|
66
|
+
// (a gearbox printed in place has hundreds of thousands of faces: every face against every other is hours)
|
|
67
|
+
const CELL = 2, cellKey = (i, j) => `${i},${j}`;
|
|
68
|
+
const gridOf = (s) => {
|
|
69
|
+
if (s.grid) return s.grid;
|
|
70
|
+
const g = new Map();
|
|
71
|
+
for (const t of s.tris) {
|
|
72
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
73
|
+
for (const v of t) { x0 = Math.min(x0, v[0]); x1 = Math.max(x1, v[0]); y0 = Math.min(y0, v[1]); y1 = Math.max(y1, v[1]); }
|
|
74
|
+
for (let i = Math.floor(x0 / CELL); i <= Math.floor(x1 / CELL); i++) for (let j = Math.floor(y0 / CELL); j <= Math.floor(y1 / CELL); j++) { const k = cellKey(i, j); if (!g.has(k)) g.set(k, []); g.get(k).push(t); }
|
|
75
|
+
}
|
|
76
|
+
return (s.grid = g);
|
|
77
|
+
};
|
|
65
78
|
// Is the point inside one of the parts? A ray straight up crosses its surface an odd number of times
|
|
66
|
-
const inPart = (pt) => solids.length > 1 && solids.some((
|
|
79
|
+
const inPart = (pt) => solids.length > 1 && solids.some((s) => {
|
|
80
|
+
const { box } = s;
|
|
67
81
|
if (pt.some((c, k) => c < box.lo[k] - eps || c > box.hi[k] + eps)) return false;
|
|
68
82
|
let n = 0;
|
|
69
|
-
for (const t of
|
|
83
|
+
for (const t of gridOf(s).get(cellKey(Math.floor(pt[0] / CELL), Math.floor(pt[1] / CELL))) || []) for (let i = 1; i < t.length - 1; i++) {
|
|
70
84
|
const [a, b, c] = [t[0], t[i], t[i + 1]], d = (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
|
|
71
85
|
if (Math.abs(d) < 1e-12) continue;
|
|
72
86
|
const u = ((pt[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (pt[1] - a[1])) / d, v = ((b[0] - a[0]) * (pt[1] - a[1]) - (pt[0] - a[0]) * (b[1] - a[1])) / d;
|
|
@@ -211,6 +225,10 @@ export function check(polys, printer, { eps = 1e-3, parts = null, slice = true }
|
|
|
211
225
|
// further apart there than the gap that prints apart higher up, or they print as one)
|
|
212
226
|
const firstGap = printer.foot > 0 && printer.print_gap > 0 ? printer.print_gap + 2 * printer.foot : 0;
|
|
213
227
|
const sliced = islands(tris, { layer: printer.layer ?? 0.2, overhang: printer.overhang, line: printer.line ?? LINE, firstGap }), isl = sliced.islands;
|
|
228
|
+
// A big part whose first layer is cut up (bars, spokes, a skeleton) has far more edge to lift than a plain footprint
|
|
229
|
+
// of the same area. A small part (a gear) is allowed its small shapes.
|
|
230
|
+
const { lo: lo0, hi: hi0 } = bounds(tris), across = Math.max(hi0[0] - lo0[0], hi0[1] - lo0[1]);
|
|
231
|
+
if (across > FIRST_LARGE && sliced.firstSprawl > FIRST_SPRAWL) f('warn', `the first layer's outline is ${mm(sliced.firstSprawl)} times a plain shape's of the same area, on a part ${mm(across)} mm across: edges are where a first layer lifts and curls. Make the footprint smaller and solid, and take material out above the first layer`, 'print.first-layer.sprawl', { sprawl: sliced.firstSprawl });
|
|
214
232
|
for (const x of sliced.fused) f('warn', `on the first layer, two parts are about ${mm(x.gap)} mm apart at (${mm(x.at[0])}, ${mm(x.at[1])}): the first layer spreads ${printer.foot} mm each side as it is squashed on, so under ${mm(firstGap)} mm they print as one. Keep them ${mm(firstGap)} mm apart on the bed, or lift one off it`, 'print.first-layer', { gap: x.gap, need: firstGap }, { lo: [x.at[0] - 1, x.at[1] - 1, 0], hi: [x.at[0] + 1, x.at[1] + 1, printer.layer ?? 0.2] });
|
|
215
233
|
if (firstGap && !sliced.fused.length) f('pass', `on the first layer, nothing printed in place stands within ${mm(firstGap)} mm of another part (the first layer spreads ${printer.foot} mm each side)`, 'print.first-layer', { need: firstGap });
|
|
216
234
|
// Overhang as the printer meets it: layers lying out past the one below, less the flat undersides the bridge check
|
|
@@ -293,6 +311,7 @@ function spans(polys, held, under) {
|
|
|
293
311
|
// seams(polys) -> { gaps, tjunctions, hairlines }: edges not matched by a reverse edge, split into T-junctions (covered
|
|
294
312
|
// all along by reverse edges on the same line, or lying along a longer reverse edge), hairlines (within HAIRLINE of
|
|
295
313
|
// another face) and real gaps.
|
|
314
|
+
const FIRST_LARGE = 80, FIRST_SPRAWL = 8; // mm, and times a circle's outline: a part over 80 mm across whose first layer has eight times the edge a plain shape would
|
|
296
315
|
export const HAIRLINE = 0.01; // mm: a fortieth of a print line
|
|
297
316
|
const vsub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]], vdot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
298
317
|
function pointToTriangle(p, a, b, c) { // Ericson, Real-Time Collision Detection 5.1.5
|
|
@@ -394,6 +413,7 @@ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstG
|
|
|
394
413
|
const byLayer = Array.from({ length: count }, () => []);
|
|
395
414
|
for (let t = 0; t < tris.length; t++) for (let L = Math.max(0, Math.ceil((zmin[t] - lo[2]) / layer - 0.5)); L <= Math.min(count - 1, Math.floor((zmax[t] - lo[2]) / layer - 0.5)); L++) byLayer[L].push(t);
|
|
396
415
|
let below = null, held = null, prev = null, counted = null, overhangArea = 0;
|
|
416
|
+
let firstSprawl = 0; // the first layer's outline against a circle's of the same area
|
|
397
417
|
const fused = []; // on the first layer, regions nearer each other than firstGap: { at, gap } (the first layer spreads, and they print as one)
|
|
398
418
|
const px = (x) => Math.floor((x - lo[0]) / pitch) + 3;
|
|
399
419
|
// Only what the printer can lay counts: each layer is opened by half a line (eroded, then grown back), so anything
|
|
@@ -444,6 +464,11 @@ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstG
|
|
|
444
464
|
if (same) continue;
|
|
445
465
|
const filled = opened(raw);
|
|
446
466
|
history[L] = filled;
|
|
467
|
+
if (L === 0) { // how long the first layer's outline is, against a circle's of the same area: 1 for a disc, more the more it's cut up
|
|
468
|
+
let area = 0, edges = 0;
|
|
469
|
+
for (let n = 0; n < nx * ny; n++) if (filled[n]) { area++; if (!filled[n - 1]) edges++; if (!filled[n + 1]) edges++; if (!filled[n - nx]) edges++; if (!filled[n + nx]) edges++; }
|
|
470
|
+
firstSprawl = area ? (edges * pitch) / (2 * Math.sqrt(Math.PI * area * pitch * pitch)) : 0;
|
|
471
|
+
}
|
|
447
472
|
if (L === 0 && firstGap > 0) {
|
|
448
473
|
// Each region labelled; grown by half the gap, two that meet were nearer than it. Where they meet is the spot
|
|
449
474
|
const label = (mask) => { const lab = new Int32Array(nx * ny), stack = new Int32Array(nx * ny); let n = 0; for (let st = 0; st < nx * ny; st++) { if (!mask[st] || lab[st]) continue; n++; let top = 0; stack[top++] = st; lab[st] = n; while (top) { const e = stack[--top], i = e % nx; for (const d of [i + 1 < nx ? e + 1 : -1, i > 0 ? e - 1 : -1, e + nx < nx * ny ? e + nx : -1, e >= nx ? e - nx : -1]) if (d >= 0 && mask[d] && !lab[d]) { lab[d] = n; stack[top++] = d; } } } return { lab, n }; };
|
|
@@ -505,7 +530,7 @@ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstG
|
|
|
505
530
|
}
|
|
506
531
|
below = filled;
|
|
507
532
|
}
|
|
508
|
-
return { islands: out, layers: count, overhang: overhangArea, fused };
|
|
533
|
+
return { firstSprawl, islands: out, layers: count, overhang: overhangArea, fused };
|
|
509
534
|
}
|
|
510
535
|
|
|
511
536
|
export function shape(polys, eps = 1e-3, drop = 0.05, hang = 1) {
|