@rulecad/printcheck 0.3.2 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/printcheck.mjs +17 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/printcheck",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
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": {
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(({ tris, box }) => {
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 tris) for (let i = 1; i < t.length - 1; i++) {
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;