@rulecad/printcheck 0.1.3 → 0.1.4
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/README.md +8 -2
- package/package.json +1 -1
- package/printcheck.d.mts +20 -0
- package/printcheck.mjs +59 -1
package/README.md
CHANGED
|
@@ -26,10 +26,16 @@ What it checks:
|
|
|
26
26
|
mid-air.
|
|
27
27
|
- **Closed solid**: every edge is matched by a face on the other side. T-junctions count as closed, and so do hairline
|
|
28
28
|
seams narrower than `HAIRLINE` (0.01 mm), which booleans leave and no printer can resolve. Wider gaps are holes.
|
|
29
|
+
- **One piece**: a loose piece would print as a stray bit or fall over. A cavity inside a part (its surface faces in)
|
|
30
|
+
isn't a piece.
|
|
31
|
+
- **Nothing starting in mid-air**: the lowest point of a tip hanging down (an icicle), with nothing below it to print
|
|
32
|
+
on. Its faces point down, and for a millimetre above it the surface stays narrow. The face-by-face overhang check
|
|
33
|
+
misses a tip like this, because each of its faces may slope at 45° or less. A shallow dip in a smoothed underside
|
|
34
|
+
spreads wide, so it doesn't count.
|
|
29
35
|
|
|
30
36
|
Several parts printed together (a keycap with an inlaid legend in another colour): pass
|
|
31
37
|
`{ parts: [{ triangles, role }] }`. Then one part resting on another isn't an overhang, and each part must be closed on
|
|
32
38
|
its own. A part with `role: 'support'` is printed and then removed.
|
|
33
39
|
|
|
34
|
-
Also exported: `bounds(triangles)` (`{ lo, hi, size }`), `seams(triangles)` (`{ gaps, tjunctions, hairlines }`)
|
|
35
|
-
`widest(polys, held, under)` (the bridge measurement).
|
|
40
|
+
Also exported: `bounds(triangles)` (`{ lo, hi, size }`), `seams(triangles)` (`{ gaps, tjunctions, hairlines }`),
|
|
41
|
+
`shape(triangles)` (`{ pieces, tips }`) and `widest(polys, held, under)` (the bridge measurement).
|
package/package.json
CHANGED
package/printcheck.d.mts
CHANGED
|
@@ -46,6 +46,26 @@ export function seams(polys: Face[], eps?: number): {
|
|
|
46
46
|
tjunctions: number;
|
|
47
47
|
hairlines: number;
|
|
48
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* A model's pieces and the tips hanging down from it. Corners closer than eps are one point. A piece is a set of faces
|
|
51
|
+
* joined by their corners; one enclosing no volume, or a negative one (a cavity's surface, facing in), isn't a piece.
|
|
52
|
+
* Pieces come biggest first. A tip is a corner below every corner it shares a face with, by more than drop, on faces
|
|
53
|
+
* facing down (not a hole's floor), and narrow for hang mm above it (not a dip in an underside): the start of
|
|
54
|
+
* something hanging in the air (corners in a flat underside are level with their neighbours, so they're not tips;
|
|
55
|
+
* nor is the bed, which the caller leaves out). Tips closer than a millimetre are one.
|
|
56
|
+
* @param {Face[]} polys @param {number} [eps] @param {number} [drop] @param {number} [hang] how far a tip must hang, mm
|
|
57
|
+
* @returns {{ pieces: { volume: number, box: { lo: number[], hi: number[] } }[], tips: number[][] }}
|
|
58
|
+
*/
|
|
59
|
+
export function shape(polys: Face[], eps?: number, drop?: number, hang?: number): {
|
|
60
|
+
pieces: {
|
|
61
|
+
volume: number;
|
|
62
|
+
box: {
|
|
63
|
+
lo: number[];
|
|
64
|
+
hi: number[];
|
|
65
|
+
};
|
|
66
|
+
}[];
|
|
67
|
+
tips: number[][];
|
|
68
|
+
};
|
|
49
69
|
export const HAIRLINE: 0.01;
|
|
50
70
|
/**
|
|
51
71
|
* a corner in mm, Z up
|
package/printcheck.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Printability, from the faces and the printer's rules. Each finding is { level: 'fail' | 'warn' | 'pass', text }.
|
|
2
|
-
// fits the bed · sits flat on it · a closed solid (slicers need one) · overhangs past the printer's limit
|
|
2
|
+
// fits the bed · sits flat on it · a closed solid (slicers need one) · one piece · overhangs past the printer's limit
|
|
3
|
+
// · tips hanging down that start in mid-air
|
|
3
4
|
// flat undersides above the bed: on a built-in breakaway support (one support gap above a surface), or bridges no
|
|
4
5
|
// wider than the printer can span: the widest circle that fits in the underside, measured to the edges that are
|
|
5
6
|
// held up by material below. An underside with no such edge starts in mid-air: it needs support.
|
|
@@ -126,6 +127,17 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
126
127
|
else f('pass', `bridge ${mm(span)} mm at z ${mm(Number(z))} (up to ${bridgeMax} mm)`, 'print.bridge', { span, max: bridgeMax, z: Number(z) });
|
|
127
128
|
}
|
|
128
129
|
|
|
130
|
+
// One piece, and nothing starting in mid-air, part by part (an assembly's parts are apart on purpose)
|
|
131
|
+
for (const tris of parts && parts.length > 1 ? parts.filter((p) => p.role !== 'support').map((p) => p.triangles) : [polys]) {
|
|
132
|
+
const { pieces, tips } = shape(tris, eps, (printer.layer ?? 0.2) / 4);
|
|
133
|
+
const loose = pieces.slice(1);
|
|
134
|
+
if (loose.length) f('warn', `${pieces.length} pieces: ${loose.length} loose from the main one (${loose.length === 1 ? mm(loose[0].volume) : `${mm(Math.min(...loose.map((l) => l.volume)))} to ${mm(Math.max(...loose.map((l) => l.volume)))}`} mm³), which would print as stray bits or fall over`, 'print.pieces', { pieces: pieces.length, loose: loose.map((l) => l.volume) }, loose.reduce((b, l) => grow(grow(b, [l.box.lo]), [l.box.hi]), { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] }));
|
|
135
|
+
else if (pieces.length) f('pass', 'one piece', 'print.pieces', { pieces: 1 });
|
|
136
|
+
const hanging = tips.filter((t) => t[2] - lo[2] > eps && !rests([t, t, t]));
|
|
137
|
+
if (hanging.length) f('warn', `${hanging.length === 1 ? 'a point starts' : `${hanging.length} points start`} in mid-air (z ${mm(Math.min(...hanging.map((t) => t[2])))}${hanging.length > 1 ? ` to ${mm(Math.max(...hanging.map((t) => t[2])))}` : ''} mm): the lowest points of tips hanging down, with nothing below to print on`, 'print.island', { points: hanging }, hanging.reduce((b, t) => grow(b, [t]), { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] }));
|
|
138
|
+
else f('pass', 'nothing starts in mid-air', 'print.island', { points: [] });
|
|
139
|
+
}
|
|
140
|
+
|
|
129
141
|
// Closed: every edge is matched by an edge running the other way. An edge that meets the middle of a longer edge
|
|
130
142
|
// (a T-junction, left by booleans) is still closed if the edges along the other side cover it exactly.
|
|
131
143
|
const { gaps, tjunctions, hairlines } = parts && parts.length > 1
|
|
@@ -227,3 +239,49 @@ export function seams(polys, eps = 1e-3) {
|
|
|
227
239
|
}
|
|
228
240
|
return { gaps, tjunctions, hairlines };
|
|
229
241
|
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* A model's pieces and the tips hanging down from it. Corners closer than eps are one point. A piece is a set of faces
|
|
245
|
+
* joined by their corners; one enclosing no volume, or a negative one (a cavity's surface, facing in), isn't a piece.
|
|
246
|
+
* Pieces come biggest first. A tip is a corner below every corner it shares a face with, by more than drop, on faces
|
|
247
|
+
* facing down (not a hole's floor), and narrow for hang mm above it (not a dip in an underside): the start of
|
|
248
|
+
* something hanging in the air (corners in a flat underside are level with their neighbours, so they're not tips;
|
|
249
|
+
* nor is the bed, which the caller leaves out). Tips closer than a millimetre are one.
|
|
250
|
+
* @param {Face[]} polys @param {number} [eps] @param {number} [drop] @param {number} [hang] how far a tip must hang, mm
|
|
251
|
+
* @returns {{ pieces: { volume: number, box: { lo: number[], hi: number[] } }[], tips: number[][] }}
|
|
252
|
+
*/
|
|
253
|
+
export function shape(polys, eps = 1e-3, drop = 0.05, hang = 1) {
|
|
254
|
+
const ids = new Map(), at = [], key = (v) => v.map((c) => Math.round(c / eps)).join(',');
|
|
255
|
+
const faces = polys.map((p) => p.map((v) => { const k = key(v); if (!ids.has(k)) { ids.set(k, at.length); at.push(v); } return ids.get(k); }));
|
|
256
|
+
const up = at.map((_, i) => i), find = (i) => { while (up[i] !== i) i = up[i] = up[up[i]]; return i; };
|
|
257
|
+
const near = at.map(() => new Set()), facing = at.map(() => 0);
|
|
258
|
+
faces.forEach((f, n) => { const z = areaNormal(polys[n])[2]; for (const v of f) facing[v] += z; });
|
|
259
|
+
for (const f of faces) for (let i = 0; i < f.length; i++) { const a = f[i], b = f[(i + 1) % f.length]; up[find(a)] = find(b); near[a].add(b); near[b].add(a); }
|
|
260
|
+
const by = new Map();
|
|
261
|
+
for (const f of faces) {
|
|
262
|
+
const r = find(f[0]), piece = by.get(r) ?? { volume: 0, box: { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] } };
|
|
263
|
+
for (let i = 1; i < f.length - 1; i++) { const [a, b, c] = [at[f[0]], at[f[i]], at[f[i + 1]]]; piece.volume += vdot(a, cross(b, c)) / 6; }
|
|
264
|
+
for (const v of f) for (let k = 0; k < 3; k++) { piece.box.lo[k] = Math.min(piece.box.lo[k], at[v][k]); piece.box.hi[k] = Math.max(piece.box.hi[k], at[v][k]); }
|
|
265
|
+
by.set(r, piece);
|
|
266
|
+
}
|
|
267
|
+
const pieces = [...by.values()].filter((p) => p.volume > eps).sort((a, b) => b.volume - a.volume);
|
|
268
|
+
const tips = [];
|
|
269
|
+
// A tip hangs: within hang of its point the surface stays narrow (a cone at 45° is as wide as it is tall). A dip in a
|
|
270
|
+
// smoothed underside is shallow: within hang of it, the surface spreads wide, as far as the underside does.
|
|
271
|
+
const narrow = (i) => {
|
|
272
|
+
const seen = new Set([i]), todo = [i], v = at[i];
|
|
273
|
+
while (todo.length) {
|
|
274
|
+
for (const j of near[todo.pop()]) {
|
|
275
|
+
if (seen.has(j) || at[j][2] > v[2] + hang) continue;
|
|
276
|
+
if (Math.hypot(at[j][0] - v[0], at[j][1] - v[1]) > 2 * hang) return false;
|
|
277
|
+
seen.add(j); todo.push(j);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return true;
|
|
281
|
+
};
|
|
282
|
+
at.forEach((v, i) => {
|
|
283
|
+
if (!near[i].size || facing[i] >= 0 || [...near[i]].some((j) => at[j][2] - v[2] <= drop) || !narrow(i)) return;
|
|
284
|
+
if (!tips.some((t) => Math.hypot(t[0] - v[0], t[1] - v[1], t[2] - v[2]) < 1)) tips.push(v);
|
|
285
|
+
});
|
|
286
|
+
return { pieces, tips };
|
|
287
|
+
}
|