@rulecad/printcheck 0.2.2 → 0.3.2

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 CHANGED
@@ -51,6 +51,10 @@ What it checks:
51
51
  misses a tip like this, because each of its faces may slope at 45° or less. A shallow dip in a smoothed underside
52
52
  spreads wide, so it doesn't count.
53
53
 
54
+ The layer checks (islands, the first layer, overhang as sliced) are the slow part, seconds on a curved part:
55
+ `check(triangles, printer, { slice: false })` leaves them out and says so (`print.unsliced`), for a page that shows
56
+ a part at once and slices it off its own thread (the viewer does).
57
+
54
58
  Several parts printed together (a keycap with an inlaid legend in another colour): pass
55
59
  `{ parts: [{ triangles, role }] }`. Then one part resting on another isn't an overhang, and each part must be closed on
56
60
  its own. A part with `role: 'support'` is printed and then removed.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/printcheck",
3
- "version": "0.2.2",
3
+ "version": "0.3.2",
4
4
  "description": "Is this mesh printable? Bed fit, flat base, overhangs, bridges, breakaway supports and watertightness, for any FDM printer.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -30,6 +30,10 @@
30
30
  "url": "git+https://github.com/JoniBach/rulecad.git",
31
31
  "directory": "packages/printcheck"
32
32
  },
33
+ "homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/printcheck#readme",
34
+ "bugs": {
35
+ "url": "https://github.com/JoniBach/rulecad/issues"
36
+ },
33
37
  "scripts": {
34
38
  "test": "node --test"
35
39
  },
package/printcheck.d.mts CHANGED
@@ -8,8 +8,8 @@
8
8
  /** @typedef {{ bed: number, overhang: number, layer?: number, support_gap?: number, bridge?: number, line?: number, print_gap?: number, foot?: number }} Printer mm and degrees; layer and line drive the sliced-layer island check; print_gap and foot (how far the first layer spreads each side) the first-layer check */
9
9
  /**
10
10
  * A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
11
- * it is when that's known (the box round the faces it's about, in mm).
12
- * @typedef {{ level: 'pass' | 'warn' | 'fail' | 'info', text: string, code?: string, values?: Record<string, any>, at?: { lo: number[], hi: number[] }, refs?: Record<string, any> }} Finding
11
+ * it is when that's known: at, a point (the middle of it), and box, the box round the faces it's about, in mm.
12
+ * @typedef {{ level: 'pass' | 'warn' | 'fail' | 'info', text: string, code?: string, values?: Record<string, any>, at?: number[], box?: { lo: number[], hi: number[] }, refs?: Record<string, any> }} Finding
13
13
  */
14
14
  export function bounds(polys: any): {
15
15
  lo: number[];
@@ -23,7 +23,7 @@ export function bounds(polys: any): {
23
23
  * @param {{ eps?: number, parts?: { triangles: Face[], role?: 'part' | 'support' }[] }} [options]
24
24
  * @returns {Finding[]}
25
25
  */
26
- export function check(polys: Face[], printer: Printer, { eps, parts }?: {
26
+ export function check(polys: Face[], printer: Printer, { eps, parts, slice }?: {
27
27
  eps?: number;
28
28
  parts?: {
29
29
  triangles: Face[];
@@ -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;
@@ -105,14 +107,15 @@ export type Printer = {
105
107
  };
106
108
  /**
107
109
  * A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
108
- * it is when that's known (the box round the faces it's about, in mm).
110
+ * it is when that's known: at, a point (the middle of it), and box, the box round the faces it's about, in mm.
109
111
  */
110
112
  export type Finding = {
111
113
  level: "pass" | "warn" | "fail" | "info";
112
114
  text: string;
113
115
  code?: string;
114
116
  values?: Record<string, any>;
115
- at?: {
117
+ at?: number[];
118
+ box?: {
116
119
  lo: number[];
117
120
  hi: number[];
118
121
  };
package/printcheck.mjs CHANGED
@@ -33,8 +33,8 @@ const LEDGE_SLACK = (15 * Math.PI) / 180; // a point's supports all within a hal
33
33
  /** @typedef {{ bed: number, overhang: number, layer?: number, support_gap?: number, bridge?: number, line?: number, print_gap?: number, foot?: number }} Printer mm and degrees; layer and line drive the sliced-layer island check; print_gap and foot (how far the first layer spreads each side) the first-layer check */
34
34
  /**
35
35
  * A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
36
- * it is when that's known (the box round the faces it's about, in mm).
37
- * @typedef {{ level: 'pass' | 'warn' | 'fail' | 'info', text: string, code?: string, values?: Record<string, any>, at?: { lo: number[], hi: number[] }, refs?: Record<string, any> }} Finding
36
+ * it is when that's known: at, a point (the middle of it), and box, the box round the faces it's about, in mm.
37
+ * @typedef {{ level: 'pass' | 'warn' | 'fail' | 'info', text: string, code?: string, values?: Record<string, any>, at?: number[], box?: { lo: number[], hi: number[] }, refs?: Record<string, any> }} Finding
38
38
  */
39
39
 
40
40
  export function bounds(polys) {
@@ -53,8 +53,9 @@ export function bounds(polys) {
53
53
  * @param {{ eps?: number, parts?: { triangles: Face[], role?: 'part' | 'support' }[] }} [options]
54
54
  * @returns {Finding[]}
55
55
  */
56
- export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
57
- const out = [], f = (level, text, code, values, at) => out.push({ level, text, code, ...(values && { values }), ...(at && { at }) });
56
+ export function check(polys, printer, { eps = 1e-3, parts = null, slice = true } = {}) {
57
+ // (a finding's at is a point, the middle of where it is; box is the box round it: one shape across the kit)
58
+ const out = [], f = (level, text, code, values, box) => out.push({ level, text, code, ...(values && { values }), ...(box && Number.isFinite(box.lo?.[0]) ? { at: box.lo.map((x, k) => (x + box.hi[k]) / 2), box } : {}) });
58
59
  // What can't be checked is said at the door, not found out deep inside
59
60
  for (const k of ['bed', 'overhang']) if (!(Number(printer?.[k]) > 0)) throw new Error(`check(model, printer): the printer's "${k}" is needed, a number above 0 (got ${JSON.stringify(printer?.[k])}): { bed: 256, overhang: 45, layer: 0.2 }`);
60
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])})`);
@@ -203,11 +204,17 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
203
204
  const hanging = tips.filter((t) => t[2] - lo[2] > eps && !rests([t, t, t]));
204
205
  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] }));
205
206
  else f('pass', 'nothing starts in mid-air', 'print.island', { points: [] });
206
- // And as the slicer sees it: every layer against the one below
207
+ // And as the slicer sees it: every layer against the one below (slice: false leaves this out, the slow part: a page
208
+ // can check a part at once, then slice it off its own thread)
209
+ if (!slice) { f('info', 'not sliced: the layers are checked on their own (islands, the first layer, layer overhang)', 'print.unsliced'); continue; }
207
210
  // (the first layer spreads `foot` each side as it is squashed on to the bed: two parts printed in place must stand
208
211
  // further apart there than the gap that prints apart higher up, or they print as one)
209
212
  const firstGap = printer.foot > 0 && printer.print_gap > 0 ? printer.print_gap + 2 * printer.foot : 0;
210
213
  const sliced = islands(tris, { layer: printer.layer ?? 0.2, overhang: printer.overhang, line: printer.line ?? LINE, firstGap }), isl = sliced.islands;
214
+ // A big part whose first layer is cut up (bars, spokes, a skeleton) has far more edge to lift than a plain footprint
215
+ // of the same area. A small part (a gear) is allowed its small shapes.
216
+ const { lo: lo0, hi: hi0 } = bounds(tris), across = Math.max(hi0[0] - lo0[0], hi0[1] - lo0[1]);
217
+ 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 });
211
218
  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] });
212
219
  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 });
213
220
  // Overhang as the printer meets it: layers lying out past the one below, less the flat undersides the bridge check
@@ -290,6 +297,7 @@ function spans(polys, held, under) {
290
297
  // seams(polys) -> { gaps, tjunctions, hairlines }: edges not matched by a reverse edge, split into T-junctions (covered
291
298
  // all along by reverse edges on the same line, or lying along a longer reverse edge), hairlines (within HAIRLINE of
292
299
  // another face) and real gaps.
300
+ 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
293
301
  export const HAIRLINE = 0.01; // mm: a fortieth of a print line
294
302
  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];
295
303
  function pointToTriangle(p, a, b, c) { // Ericson, Real-Time Collision Detection 5.1.5
@@ -391,6 +399,7 @@ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstG
391
399
  const byLayer = Array.from({ length: count }, () => []);
392
400
  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);
393
401
  let below = null, held = null, prev = null, counted = null, overhangArea = 0;
402
+ let firstSprawl = 0; // the first layer's outline against a circle's of the same area
394
403
  const fused = []; // on the first layer, regions nearer each other than firstGap: { at, gap } (the first layer spreads, and they print as one)
395
404
  const px = (x) => Math.floor((x - lo[0]) / pitch) + 3;
396
405
  // Only what the printer can lay counts: each layer is opened by half a line (eroded, then grown back), so anything
@@ -441,6 +450,11 @@ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstG
441
450
  if (same) continue;
442
451
  const filled = opened(raw);
443
452
  history[L] = filled;
453
+ 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
454
+ let area = 0, edges = 0;
455
+ 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++; }
456
+ firstSprawl = area ? (edges * pitch) / (2 * Math.sqrt(Math.PI * area * pitch * pitch)) : 0;
457
+ }
444
458
  if (L === 0 && firstGap > 0) {
445
459
  // Each region labelled; grown by half the gap, two that meet were nearer than it. Where they meet is the spot
446
460
  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 }; };
@@ -502,7 +516,7 @@ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstG
502
516
  }
503
517
  below = filled;
504
518
  }
505
- return { islands: out, layers: count, overhang: overhangArea, fused };
519
+ return { firstSprawl, islands: out, layers: count, overhang: overhangArea, fused };
506
520
  }
507
521
 
508
522
  export function shape(polys, eps = 1e-3, drop = 0.05, hang = 1) {