@rulecad/printcheck 0.1.4 → 0.2.0
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 +16 -3
- package/package.json +1 -1
- package/printcheck.d.mts +38 -20
- package/printcheck.mjs +254 -29
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ words. No dependencies; browsers and Node 20+.
|
|
|
6
6
|
```js
|
|
7
7
|
import { check } from '@rulecad/printcheck';
|
|
8
8
|
|
|
9
|
-
const printer = { bed: 256, overhang: 45, layer: 0.2, support_gap: 0.2, bridge: 5 };
|
|
9
|
+
const printer = { bed: 256, overhang: 45, layer: 0.2, support_gap: 0.2, bridge: 5, line: 0.42, print_gap: 0.3, foot: 0.15 };
|
|
10
10
|
const findings = check(triangles, printer);
|
|
11
11
|
// [{ level: 'pass', text: '20 × 4 × 12 mm, fits the bed' },
|
|
12
12
|
// { level: 'warn', text: 'an underside at z 10 starts in mid-air: needs support …' }, …]
|
|
@@ -23,7 +23,19 @@ What it checks:
|
|
|
23
23
|
- **Flat undersides above the bed**: each must start on something. Either it rests on a built-in **breakaway support**
|
|
24
24
|
(one `support_gap` below a surface), or it **bridges** no wider than `bridge` mm. The bridge is the widest circle that
|
|
25
25
|
fits in the underside, measured to the edges held up by material below. An underside held by nothing starts in
|
|
26
|
-
mid-air.
|
|
26
|
+
mid-air. One held on one side only, further out than a print line, is a **ledge**: nothing bridges it, each line
|
|
27
|
+
is laid past the last in the air, and it droops (slant its underside in at 45° to the wall instead).
|
|
28
|
+
Each underside is taken on its own (the faces at a height joined where they touch), and an edge is held wherever
|
|
29
|
+
it lies along the top edge of a wall below (booleans leave T-junctions; they don't matter). An underside no wider
|
|
30
|
+
than a print line (`line`, 0.4 mm unless given) anywhere is a step of a slope printed in layers, or a sliver the
|
|
31
|
+
slicer lays nothing on, and is not counted.
|
|
32
|
+
- **The first layer**: two parts printed in place must stand `print_gap` plus twice `foot` apart on the bed (`foot`
|
|
33
|
+
is how far the first layer spreads each side as it is squashed on: elephant's foot); nearer, they print as one.
|
|
34
|
+
Checked when the printer gives both; lifting one part a layer off the bed takes it out of the check.
|
|
35
|
+
- **Islands, as the slicer sees them**: the mesh cut at every layer (`layer`), each layer's regions against the one
|
|
36
|
+
below within the overhang reach; a region with nothing under it is an island the printer would lay on air. Drawn
|
|
37
|
+
on a grid of the print line (`line`), so slivers narrower than a line don't count. Also exported as `islands()`,
|
|
38
|
+
with each island's pixels for whatever fixes it.
|
|
27
39
|
- **Closed solid**: every edge is matched by a face on the other side. T-junctions count as closed, and so do hairline
|
|
28
40
|
seams narrower than `HAIRLINE` (0.01 mm), which booleans leave and no printer can resolve. Wider gaps are holes.
|
|
29
41
|
- **One piece**: a loose piece would print as a stray bit or fall over. A cavity inside a part (its surface faces in)
|
|
@@ -38,4 +50,5 @@ Several parts printed together (a keycap with an inlaid legend in another colour
|
|
|
38
50
|
its own. A part with `role: 'support'` is printed and then removed.
|
|
39
51
|
|
|
40
52
|
Also exported: `bounds(triangles)` (`{ lo, hi, size }`), `seams(triangles)` (`{ gaps, tjunctions, hairlines }`),
|
|
41
|
-
`shape(triangles)` (`{ pieces, tips }`)
|
|
53
|
+
`shape(triangles)` (`{ pieces, tips }`), `widest(polys, held, under)` (the bridge measurement) and
|
|
54
|
+
`ledges(polys, held, under)` (how far an underside reaches past its last support).
|
package/package.json
CHANGED
package/printcheck.d.mts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
/** @typedef {[number, number, number]} Point a corner in mm, Z up */
|
|
7
7
|
/** @typedef {Point[]} Face a polygon: three or more corners, counter-clockwise seen from outside */
|
|
8
|
-
/** @typedef {{ bed: number, overhang: number, layer?: number, support_gap?: number, bridge?: number }} Printer mm and degrees */
|
|
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
11
|
* it is when that's known (the box round the faces it's about, in mm).
|
|
@@ -36,6 +36,13 @@ export function check(polys: Face[], printer: Printer, { eps, parts }?: {
|
|
|
36
36
|
* @returns {number} mm
|
|
37
37
|
*/
|
|
38
38
|
export function widest(polys: Face[], held?: (edge: [Point, Point]) => boolean, under?: Face[]): number;
|
|
39
|
+
/**
|
|
40
|
+
* How far an underside reaches out past its last support, held on one side only (a ledge in the air): mm, 0 if it is
|
|
41
|
+
* held on both sides everywhere (a bridge), null if nothing holds it.
|
|
42
|
+
* @param {Face[]} polys @param {(edge: [Point, Point]) => boolean} [held] @param {Face[]} [under]
|
|
43
|
+
* @returns {number | null} mm
|
|
44
|
+
*/
|
|
45
|
+
export function ledges(polys: Face[], held?: (edge: [Point, Point]) => boolean, under?: Face[]): number | null;
|
|
39
46
|
/**
|
|
40
47
|
* Open edges, sorted: real gaps, T-junctions (closed) and hairline seams (closed).
|
|
41
48
|
* @param {Face[]} polys @param {number} [eps]
|
|
@@ -46,25 +53,33 @@ export function seams(polys: Face[], eps?: number): {
|
|
|
46
53
|
tjunctions: number;
|
|
47
54
|
hairlines: number;
|
|
48
55
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
};
|
|
56
|
+
export function islands(polys: any, { layer, overhang, line, firstGap, onLayer }?: {
|
|
57
|
+
layer?: number;
|
|
58
|
+
overhang?: number;
|
|
59
|
+
line?: number;
|
|
60
|
+
firstGap?: number;
|
|
61
|
+
}): {
|
|
62
|
+
islands: any[];
|
|
63
|
+
layers: number;
|
|
64
|
+
overhang: number;
|
|
65
|
+
fused?: undefined;
|
|
66
|
+
} | {
|
|
67
|
+
islands: {
|
|
68
|
+
z: number;
|
|
69
|
+
area: number;
|
|
70
|
+
at: number[];
|
|
71
|
+
cells: number[][];
|
|
66
72
|
}[];
|
|
67
|
-
|
|
73
|
+
layers: number;
|
|
74
|
+
overhang: number;
|
|
75
|
+
fused: {
|
|
76
|
+
region: number;
|
|
77
|
+
at: number[];
|
|
78
|
+
}[];
|
|
79
|
+
};
|
|
80
|
+
export function shape(polys: any, eps?: number, drop?: number, hang?: number): {
|
|
81
|
+
pieces: any[];
|
|
82
|
+
tips: any[];
|
|
68
83
|
};
|
|
69
84
|
export const HAIRLINE: 0.01;
|
|
70
85
|
/**
|
|
@@ -76,7 +91,7 @@ export type Point = [number, number, number];
|
|
|
76
91
|
*/
|
|
77
92
|
export type Face = Point[];
|
|
78
93
|
/**
|
|
79
|
-
* mm and degrees
|
|
94
|
+
* 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
|
|
80
95
|
*/
|
|
81
96
|
export type Printer = {
|
|
82
97
|
bed: number;
|
|
@@ -84,6 +99,9 @@ export type Printer = {
|
|
|
84
99
|
layer?: number;
|
|
85
100
|
support_gap?: number;
|
|
86
101
|
bridge?: number;
|
|
102
|
+
line?: number;
|
|
103
|
+
print_gap?: number;
|
|
104
|
+
foot?: number;
|
|
87
105
|
};
|
|
88
106
|
/**
|
|
89
107
|
* A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
|
package/printcheck.mjs
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
// · tips hanging down that start in mid-air
|
|
4
4
|
// flat undersides above the bed: on a built-in breakaway support (one support gap above a surface), or bridges no
|
|
5
5
|
// wider than the printer can span: the widest circle that fits in the underside, measured to the edges that are
|
|
6
|
-
// held up by material below. An underside with no such edge starts in mid-air: it needs support
|
|
6
|
+
// held up by material below. An underside with no such edge starts in mid-air: it needs support; one held on one
|
|
7
|
+
// side only past a print line is a ledge, which droops. Each underside
|
|
8
|
+
// is taken on its own (the faces at a height joined edge to edge), and one no wider than a print line anywhere is
|
|
9
|
+
// a step of a slope printed in layers, not a bridge.
|
|
7
10
|
// Z is up and the bed is z = 0: a design is checked as it will print, so print orientation is part of the design.
|
|
8
11
|
|
|
9
12
|
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
@@ -25,7 +28,7 @@ const OVERHANG_SLACK = 0.5; // degrees
|
|
|
25
28
|
*/
|
|
26
29
|
/** @typedef {[number, number, number]} Point a corner in mm, Z up */
|
|
27
30
|
/** @typedef {Point[]} Face a polygon: three or more corners, counter-clockwise seen from outside */
|
|
28
|
-
/** @typedef {{ bed: number, overhang: number, layer?: number, support_gap?: number, bridge?: number }} Printer mm and degrees */
|
|
31
|
+
/** @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 */
|
|
29
32
|
/**
|
|
30
33
|
* A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
|
|
31
34
|
* it is when that's known (the box round the faces it's about, in mm).
|
|
@@ -40,7 +43,7 @@ export function bounds(polys) {
|
|
|
40
43
|
|
|
41
44
|
// parts ([{ triangles, role }]): when parts print together (a keycap and its inlaid legend), an underside resting on
|
|
42
45
|
// another part isn't an overhang, and each part must be closed on its own (their shared faces are inside the print).
|
|
43
|
-
// printer: the printer's rules, any printer: { bed, overhang (degrees), layer, support_gap?, bridge? } in mm.
|
|
46
|
+
// printer: the printer's rules, any printer: { bed, overhang (degrees), layer, support_gap?, bridge?, line? } in mm.
|
|
44
47
|
/**
|
|
45
48
|
* Is it printable? Findings in plain words, each pass, warn or fail.
|
|
46
49
|
* @param {Face[]} polys the model as faces (triangles are fine), Z up, the bed at z = 0
|
|
@@ -50,6 +53,11 @@ export function bounds(polys) {
|
|
|
50
53
|
*/
|
|
51
54
|
export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
52
55
|
const out = [], f = (level, text, code, values, at) => out.push({ level, text, code, ...(values && { values }), ...(at && { at }) });
|
|
56
|
+
// What can't be checked is said at the door, not found out deep inside
|
|
57
|
+
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 }`);
|
|
58
|
+
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])})`);
|
|
59
|
+
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)));
|
|
60
|
+
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' }];
|
|
53
61
|
const solids = (parts || []).filter((p) => p.role !== 'support').map((p) => ({ tris: p.triangles, box: bounds(p.triangles) }));
|
|
54
62
|
// Is the point inside one of the parts? A ray straight up crosses its surface an odd number of times
|
|
55
63
|
const inPart = (pt) => solids.length > 1 && solids.some(({ tris, box }) => {
|
|
@@ -94,9 +102,10 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
94
102
|
if (nz < 0) down += area;
|
|
95
103
|
}
|
|
96
104
|
if (Math.abs(lo[2]) > eps) f('fail', `lowest point is at z = ${mm(lo[2])} mm, not on the bed (z = 0)`, 'print.off-bed', { z: lo[2] });
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
if (base <= 0) f('fail', 'nothing sits flat on the bed', 'print.base', { area: base });
|
|
106
|
+
else if (Math.abs(lo[2]) <= eps) f('pass', `${mm(base)} mm² flat on the bed`, 'print.base', { area: base }); // (off the bed, its flat underside isn't on it: the finding above says so)
|
|
107
|
+
const overAt = support > 0.5 ? overBox : null, slicing = printer.layer != null; // sliced below, the layers are what count; the facets are then a note
|
|
108
|
+
if (support > 0.5) f(slicing ? 'pass' : 'warn', slicing ? `${mm(support)} mm² of facets lean past ${printer.overhang}° (z ${mm(Math.min(...zs))} to ${mm(Math.max(...zs))} mm); sliced, the layers decide` : `${mm(support)} mm² overhangs past ${printer.overhang}° (z ${mm(Math.min(...zs))} to ${mm(Math.max(...zs))} mm): needs supports or another orientation`, 'print.overhang', { area: support, angle: printer.overhang, z: [Math.min(...zs), Math.max(...zs)] }, overAt);
|
|
100
109
|
else f('pass', `no overhangs past ${printer.overhang}°`, 'print.overhang', { area: support, angle: printer.overhang });
|
|
101
110
|
|
|
102
111
|
// Flat undersides: on a breakaway support, or bridged
|
|
@@ -113,18 +122,49 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
113
122
|
if (held.length) f('pass', `${mm(held.reduce((s, d) => s + d.area, 0))} mm² rests on a breakaway support (${mm(gap)} mm gap): snip it off after printing`, 'print.breakaway', { area: held.reduce((s, d) => s + d.area, 0), gap });
|
|
114
123
|
const levels = new Map();
|
|
115
124
|
for (const d of flatDown) if (!held.includes(d)) { const k = d.z.toFixed(2); levels.set(k, [...(levels.get(k) || []), d.p]); }
|
|
116
|
-
//
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
// A point on a segment (in the plane), within eps; and a segment lying along another, overlapping it
|
|
126
|
+
const onSeg = (pt, a, b) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = dx * dx + dy * dy; if (l < eps * eps) return Math.hypot(pt[0] - a[0], pt[1] - a[1]) <= eps; const t = ((pt[0] - a[0]) * dx + (pt[1] - a[1]) * dy) / l; return t >= -eps && t <= 1 + eps && Math.hypot(pt[0] - a[0] - t * dx, pt[1] - a[1] - t * dy) <= eps; };
|
|
127
|
+
const along = (a, b, c, d) => { // c–d lies on the line of a–b and overlaps it (an edge matched exactly, or by a part of it: a T-junction)
|
|
128
|
+
const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy); if (l < eps) return false;
|
|
129
|
+
if (Math.abs((c[0] - a[0]) * dy - (c[1] - a[1]) * dx) / l > eps || Math.abs((d[0] - a[0]) * dy - (d[1] - a[1]) * dx) / l > eps) return false;
|
|
130
|
+
const t0 = ((c[0] - a[0]) * dx + (c[1] - a[1]) * dy) / (l * l), t1 = ((d[0] - a[0]) * dx + (d[1] - a[1]) * dy) / (l * l);
|
|
131
|
+
return Math.min(t0, t1) < 1 - eps && Math.max(t0, t1) > eps;
|
|
132
|
+
};
|
|
133
|
+
const bridgeMax = printer.bridge ?? 5, line = printer.line ?? 0.4;
|
|
134
|
+
// Each underside on its own: the faces at a level joined where they touch, edge to edge or a corner on another's
|
|
135
|
+
// edge (a T-junction, which a boolean leaves plenty of). Two pockets' ceilings at one height are two bridges, each
|
|
136
|
+
// measured to its own held edges, not to the other's across the part
|
|
137
|
+
const regions = (group) => {
|
|
138
|
+
const parent = group.map((_, i) => i), root = (i) => (parent[i] === i ? i : (parent[i] = root(parent[i]))), join = (i, j) => { parent[root(i)] = root(j); };
|
|
139
|
+
const cell = 1, cellOf = (v) => `${Math.floor(v[0] / cell)},${Math.floor(v[1] / cell)}`, byCell = new Map();
|
|
140
|
+
group.forEach((p, i) => { for (const v of p) { const k = cellOf(v); byCell.set(k, [...(byCell.get(k) || []), [v, i]]); } });
|
|
141
|
+
group.forEach((p, i) => p.forEach((a, k) => { // every corner of another face on this edge joins the two
|
|
142
|
+
const b = p[(k + 1) % p.length], n = Math.ceil(Math.hypot(b[0] - a[0], b[1] - a[1]) / cell) * 2 + 1, seen = new Set();
|
|
143
|
+
for (let t = 0; t <= n; t++) {
|
|
144
|
+
const q = [a[0] + ((b[0] - a[0]) * t) / n, a[1] + ((b[1] - a[1]) * t) / n], ci = Math.floor(q[0] / cell), cj = Math.floor(q[1] / cell);
|
|
145
|
+
for (let di = -1; di <= 1; di++) for (let dj = -1; dj <= 1; dj++) { const key = `${ci + di},${cj + dj}`; if (seen.has(key)) continue; seen.add(key); for (const [v, j] of byCell.get(key) || []) if (j !== i && onSeg(v, a, b)) join(i, j); }
|
|
146
|
+
}
|
|
147
|
+
}));
|
|
148
|
+
const out = new Map();
|
|
149
|
+
group.forEach((p, i) => { const r = root(i); out.set(r, [...(out.get(r) || []), p]); });
|
|
150
|
+
return [...out.values()];
|
|
151
|
+
};
|
|
152
|
+
// The top edges of walls that reach a level from below: an underside's edge lying along one is held up there
|
|
153
|
+
const wallsAt = (z) => polys.filter((p) => Math.min(...p.map((v) => v[2])) < z - eps).flatMap((p) => p.flatMap((a, i) => { const b = p[(i + 1) % p.length]; return Math.abs(a[2] - z) <= eps && Math.abs(b[2] - z) <= eps ? [[a, b]] : []; }));
|
|
154
|
+
for (const [z, level] of levels) {
|
|
155
|
+
const walls = wallsAt(Number(z));
|
|
156
|
+
for (const group of regions(level)) {
|
|
157
|
+
const at = boxOf(group);
|
|
158
|
+
if (Math.min(at.hi[0] - at.lo[0], at.hi[1] - at.lo[1]) < line - eps) continue; // a sliver narrower than a print line: the slicer lays nothing there
|
|
159
|
+
if (widest(group) < line - eps) continue; // no wider than a line anywhere (a step of a slope cut in layers, a chamfer's tread): one layer's overhang, which the perimeter takes in its stride
|
|
160
|
+
const held = (a, b) => walls.some(([c, d]) => along(a, b, c, d));
|
|
161
|
+
const under = flatUp.filter((u) => Number(z) - u.z > eps && Number(z) - u.z <= gap * 1.5 + eps).map((u) => u.p); // breakaway supports' tops
|
|
162
|
+
const measured = spans(group, held, under), span = measured?.span ?? null;
|
|
163
|
+
if (span == null) f('warn', `an underside at z ${mm(Number(z))} starts in mid-air: needs support (or a built-in breakaway support one ${mm(gap)} mm layer below)`, 'print.mid-air', { z: Number(z) }, at);
|
|
164
|
+
else if (measured.reach > line + eps) f('warn', `a ledge at z ${mm(Number(z))} reaches ${mm(measured.reach)} mm out past what holds it, held on one side only: it droops. Slant its underside in at 45° to the wall, or support it`, 'print.ledge', { reach: measured.reach, z: Number(z) }, at);
|
|
165
|
+
else if (span > bridgeMax + eps) f('warn', `${mm(span)} mm bridge at z ${mm(Number(z))}: wider than the ${bridgeMax} mm the printer can span, needs supports`, 'print.bridge', { span, max: bridgeMax, z: Number(z) }, at);
|
|
166
|
+
else f('pass', `bridge ${mm(span)} mm at z ${mm(Number(z))} (up to ${bridgeMax} mm)`, 'print.bridge', { span, max: bridgeMax, z: Number(z) });
|
|
167
|
+
}
|
|
128
168
|
}
|
|
129
169
|
|
|
130
170
|
// One piece, and nothing starting in mid-air, part by part (an assembly's parts are apart on purpose)
|
|
@@ -136,6 +176,20 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
136
176
|
const hanging = tips.filter((t) => t[2] - lo[2] > eps && !rests([t, t, t]));
|
|
137
177
|
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
178
|
else f('pass', 'nothing starts in mid-air', 'print.island', { points: [] });
|
|
179
|
+
// And as the slicer sees it: every layer against the one below
|
|
180
|
+
// (the first layer spreads `foot` each side as it is squashed on to the bed: two parts printed in place must stand
|
|
181
|
+
// further apart there than the gap that prints apart higher up, or they print as one)
|
|
182
|
+
const firstGap = printer.foot > 0 && printer.print_gap > 0 ? printer.print_gap + 2 * printer.foot : 0;
|
|
183
|
+
const sliced = islands(tris, { layer: printer.layer ?? 0.2, overhang: printer.overhang, line: printer.line ?? 0.42, firstGap }), isl = sliced.islands;
|
|
184
|
+
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] });
|
|
185
|
+
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 });
|
|
186
|
+
// Overhang as the printer meets it: layers lying out past the one below, less the flat undersides the bridge check
|
|
187
|
+
// judges on their own. The face-by-face measure above counts the surface's facets, which on a smoothed, cell-built
|
|
188
|
+
// part are steeper than its layers are.
|
|
189
|
+
const flatArea = flatDown.reduce((s, d) => s + d.area, 0), out = Math.max(0, sliced.overhang - flatArea);
|
|
190
|
+
if (out > 2) f('warn', `${mm(out)} mm² of layers lie out past what the layer below holds at ${printer.overhang}° and a line (sliced at ${printer.layer ?? 0.2} mm): needs supports or another orientation`, 'print.overhang.sliced', { area: out, facets: support }); else f('pass', `every layer lies within ${printer.overhang}° and a line of the one below (sliced at ${printer.layer ?? 0.2} mm)`, 'print.overhang.sliced', { area: out, facets: support });
|
|
191
|
+
if (isl.length) f('warn', `${isl.length === 1 ? 'an island' : `${isl.length} islands`} when sliced: ${isl.length === 1 ? `${mm(isl[0].area)} mm² at z ${mm(isl[0].z)}` : `${mm(Math.min(...isl.map((x) => x.area)))} to ${mm(Math.max(...isl.map((x) => x.area)))} mm², z ${mm(Math.min(...isl.map((x) => x.z)))} to ${mm(Math.max(...isl.map((x) => x.z)))}`}: a layer's region with nothing under it within the overhang reach, which the printer lays on air`, 'print.islands', { islands: isl }, isl.reduce((b, x) => grow(b, [x.at]), { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] }));
|
|
192
|
+
else f('pass', `no islands in ${sliced.layers} layers of ${printer.layer ?? 0.2} mm`, 'print.islands', { islands: [] });
|
|
139
193
|
}
|
|
140
194
|
|
|
141
195
|
// Closed: every edge is matched by an edge running the other way. An edge that meets the middle of a longer edge
|
|
@@ -143,7 +197,7 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
143
197
|
const { gaps, tjunctions, hairlines } = parts && parts.length > 1
|
|
144
198
|
? parts.map((p) => seams(p.triangles, eps)).reduce((s, x) => ({ gaps: s.gaps + x.gaps, tjunctions: s.tjunctions + x.tjunctions, hairlines: s.hairlines + x.hairlines }), { gaps: 0, tjunctions: 0, hairlines: 0 })
|
|
145
199
|
: seams(polys, eps);
|
|
146
|
-
if (gaps) f('warn', `${gaps} edges with nothing on the other side: holes in the surface (slicers may repair them)`, 'print.closed', { gaps });
|
|
200
|
+
if (gaps) f('warn', `${gaps === 1 ? '1 edge' : `${gaps} edges`} with nothing on the other side: ${gaps === 1 ? 'a hole' : 'holes'} in the surface (slicers may repair ${gaps === 1 ? 'it' : 'them'})`, 'print.closed', { gaps });
|
|
147
201
|
else f('pass', `closed solid${tjunctions || hairlines ? ` (${[tjunctions && `${tjunctions} T-junction edges`, hairlines && `${hairlines} hairline seams under ${HAIRLINE} mm`].filter(Boolean).join(', ')}: watertight for printing)` : ''}`, 'print.closed', { gaps, tjunctions, hairlines });
|
|
148
202
|
return out;
|
|
149
203
|
}
|
|
@@ -152,36 +206,55 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
152
206
|
// (held(a, b) -> true), or to the tops of breakaway supports one layer below (under: faces): its diameter in mm, the
|
|
153
207
|
// gap a bridge has to span. null if nothing holds it up.
|
|
154
208
|
// Sampled: points spread over each face, each measured to the held edges of the region's outline and to the supports.
|
|
209
|
+
// A point with support on one side only (nothing held beyond it, away from its nearest held edge) is on a ledge, not
|
|
210
|
+
// a bridge: `ledges(polys, held, under)` gives how far the longest reaches.
|
|
155
211
|
/**
|
|
156
212
|
* The widest circle that fits in an underside, measured to its held-up edges (the bridge it makes).
|
|
157
213
|
* @param {Face[]} polys @param {(edge: [Point, Point]) => boolean} [held] @param {Face[]} [under]
|
|
158
214
|
* @returns {number} mm
|
|
159
215
|
*/
|
|
160
|
-
export function widest(polys, held = () => true, under = []) {
|
|
216
|
+
export function widest(polys, held = () => true, under = []) { return spans(polys, held, under)?.span ?? null; }
|
|
217
|
+
/**
|
|
218
|
+
* How far an underside reaches out past its last support, held on one side only (a ledge in the air): mm, 0 if it is
|
|
219
|
+
* held on both sides everywhere (a bridge), null if nothing holds it.
|
|
220
|
+
* @param {Face[]} polys @param {(edge: [Point, Point]) => boolean} [held] @param {Face[]} [under]
|
|
221
|
+
* @returns {number | null} mm
|
|
222
|
+
*/
|
|
223
|
+
export function ledges(polys, held = () => true, under = []) { return spans(polys, held, under)?.reach ?? null; }
|
|
224
|
+
|
|
225
|
+
function spans(polys, held, under) {
|
|
161
226
|
const key = (v) => `${v[0].toFixed(3)},${v[1].toFixed(3)}`, count = new Map(), edges = [];
|
|
162
227
|
for (const p of polys) p.forEach((a, i) => { const b = p[(i + 1) % p.length], k = [key(a), key(b)].sort().join('|'); count.set(k, (count.get(k) || 0) + 1); edges.push([k, a, b]); });
|
|
163
228
|
const outline = edges.filter(([k]) => count.get(k) === 1).map(([, a, b]) => [a, b]).filter(([a, b]) => held(a, b));
|
|
164
229
|
const tops = under.flatMap((p) => p.map((a, i) => [a, p[(i + 1) % p.length]]));
|
|
165
230
|
if (!outline.length && !tops.length) return null;
|
|
166
231
|
const onTop = (pt) => under.some((p) => { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = (b[0] - a[0]) * (pt[1] - a[1]) - (b[1] - a[1]) * (pt[0] - a[0]); if (Math.abs(c) < 1e-9) continue; if (sign && Math.sign(c) !== sign) return false; sign = Math.sign(c); } return true; });
|
|
167
|
-
const
|
|
168
|
-
const dx = b[0] - a[0], dy = b[1] - a[1], l = dx * dx + dy * dy, t = l ? Math.max(0, Math.min(1, ((pt[0] - a[0]) * dx + (pt[1] - a[1]) * dy) / l)) : 0;
|
|
169
|
-
return Math.hypot(pt[0] -
|
|
232
|
+
const nearest = (pt, [a, b]) => { // the segment's closest point to pt, and how far
|
|
233
|
+
const dx = b[0] - a[0], dy = b[1] - a[1], l = dx * dx + dy * dy, t = l ? Math.max(0, Math.min(1, ((pt[0] - a[0]) * dx + (pt[1] - a[1]) * dy) / l)) : 0, c = [a[0] + t * dx, a[1] + t * dy];
|
|
234
|
+
return { c, d: Math.hypot(pt[0] - c[0], pt[1] - c[1]) };
|
|
170
235
|
};
|
|
171
|
-
|
|
236
|
+
const supports = [...outline, ...tops];
|
|
237
|
+
let best = 0, reach = 0;
|
|
172
238
|
const N = 6;
|
|
173
239
|
for (const p of polys) for (let i = 1; i < p.length - 1; i++) {
|
|
174
240
|
const [a, b, c] = [p[0], p[i], p[i + 1]];
|
|
175
241
|
for (let u = 0; u <= N; u++) for (let v = 0; u + v <= N; v++) {
|
|
176
242
|
const w = N - u - v, pt = [0, 1].map((k) => (a[k] * u + b[k] * v + c[k] * w) / N);
|
|
177
243
|
if (onTop(pt)) continue; // right over a support
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
244
|
+
const near = supports.map((s) => nearest(pt, s)), first = near.reduce((m, x) => (x.d < m.d ? x : m));
|
|
245
|
+
best = Math.max(best, first.d);
|
|
246
|
+
// Held round it, or on one side only? The directions to its supports: all within one half-plane (a gap of more
|
|
247
|
+
// than a half turn between them) and it hangs out past the last of them, a ledge reaching first.d; supports
|
|
248
|
+
// on opposing sides, and a line between two of them crosses it: a bridge
|
|
249
|
+
if (first.d > 1e-9) {
|
|
250
|
+
const angles = near.filter((x) => x.d > 1e-9).map((x) => Math.atan2(x.c[1] - pt[1], x.c[0] - pt[0])).sort((a, b) => a - b);
|
|
251
|
+
let gap = angles.length ? angles[0] + 2 * Math.PI - angles.at(-1) : 2 * Math.PI;
|
|
252
|
+
for (let k = 1; k < angles.length; k++) gap = Math.max(gap, angles[k] - angles[k - 1]);
|
|
253
|
+
if (gap > Math.PI + 1e-6) reach = Math.max(reach, first.d);
|
|
254
|
+
}
|
|
182
255
|
}
|
|
183
256
|
}
|
|
184
|
-
return 2 * best;
|
|
257
|
+
return { span: 2 * best, reach };
|
|
185
258
|
}
|
|
186
259
|
|
|
187
260
|
// seams(polys) -> { gaps, tjunctions, hairlines }: edges not matched by a reverse edge, split into T-junctions (covered
|
|
@@ -250,6 +323,158 @@ export function seams(polys, eps = 1e-3) {
|
|
|
250
323
|
* @param {Face[]} polys @param {number} [eps] @param {number} [drop] @param {number} [hang] how far a tip must hang, mm
|
|
251
324
|
* @returns {{ pieces: { volume: number, box: { lo: number[], hi: number[] } }[], tips: number[][] }}
|
|
252
325
|
*/
|
|
326
|
+
/**
|
|
327
|
+
* The slicer's view: the mesh cut at every layer, each layer's regions checked against the one below. A region that
|
|
328
|
+
* overlaps nothing in the layer below, within the overhang reach (a layer's height over the tangent of the limit, so a
|
|
329
|
+
* 45° wall counts as held), starts in mid-air: an island, which a printer lays on nothing. Each layer is drawn on a
|
|
330
|
+
* grid of a quarter of the print line and opened by half a line, so anything narrower than a line (which the slicer
|
|
331
|
+
* never extrudes) is not a region, and the rest is compared by pixels.
|
|
332
|
+
* @param {Face[]} polys the closed mesh
|
|
333
|
+
* @param {{ layer?: number, overhang?: number, line?: number, onLayer?: (layer: number, layers: number) => void }} printer
|
|
334
|
+
* layer height, overhang limit (degrees from vertical), the line width; onLayer: called as each layer is cut (progress)
|
|
335
|
+
* @returns {{ islands: { z: number, area: number, at: number[], cells: number[][] }[], layers: number, overhang: number }}
|
|
336
|
+
* each island's layer (its z), its area (mm²), its centre, and its pixels' centres ([x, y], for whatever fixes it); how
|
|
337
|
+
* many layers were cut; and overhang: the area (mm²) of layers lying further out than the one below can hold, flat
|
|
338
|
+
* undersides included (the bridge check judges those separately)
|
|
339
|
+
*/
|
|
340
|
+
const SPANS = [5, 20]; // layers: the spans a lean is held over (see islands)
|
|
341
|
+
export function islands(polys, { layer = 0.2, overhang = 45, line = 0.42, firstGap = 0, onLayer } = {}) {
|
|
342
|
+
if (!(layer > 0) || !(line > 0) || !(overhang > 0 && overhang < 90)) throw new Error(`islands: layer and line are lengths above 0 and overhang an angle between 0 and 90 (got layer ${layer}, line ${line}, overhang ${overhang})`);
|
|
343
|
+
if (!polys.length) return { islands: [], layers: 0, overhang: 0 };
|
|
344
|
+
// The pixel: a quarter of a line, or coarser on a big part so a layer stays under about a million pixels
|
|
345
|
+
const { lo, hi } = bounds(polys), pitch = Math.max(line / 4, Math.max(hi[0] - lo[0], hi[1] - lo[1]) / 1000), nx = Math.ceil((hi[0] - lo[0]) / pitch) + 6, ny = Math.ceil((hi[1] - lo[1]) / pitch) + 6;
|
|
346
|
+
const r2 = Math.max(1, Math.round(line / 2 / pitch)); // half a line, in pixels
|
|
347
|
+
// Pixels a layer may step out past the one below: its height times the tangent of the limit (measured from upright,
|
|
348
|
+
// so a wall leaning 45° steps out one layer's height each layer), and a line beyond that: a line hanging out is held
|
|
349
|
+
// by the one beside it
|
|
350
|
+
const step = layer * Math.tan((overhang * Math.PI) / 180), reach = Math.ceil(step / pitch) + 2 * r2;
|
|
351
|
+
// That line's grace is for a ledge, once. A wall that leans on, layer after layer, would take it every layer (at
|
|
352
|
+
// 0.2 mm layers and a 0.42 mm line, a 72° lean would pass), so each layer is also held against the ones SPANS
|
|
353
|
+
// layers down, with the same single line's grace over the whole span: the longer a lean goes on, the nearer to the
|
|
354
|
+
// limit it is held.
|
|
355
|
+
const reachOver = SPANS.map((n) => Math.ceil((n * step) / pitch) + 2 * r2), history = [];
|
|
356
|
+
const tris = [];
|
|
357
|
+
for (const p of polys) for (let i = 1; i < p.length - 1; i++) tris.push([p[0], p[i], p[i + 1]]);
|
|
358
|
+
const zmin = tris.map((t) => Math.min(t[0][2], t[1][2], t[2][2])), zmax = tris.map((t) => Math.max(t[0][2], t[1][2], t[2][2]));
|
|
359
|
+
const count = Math.max(1, Math.round((hi[2] - lo[2]) / layer)), out = [];
|
|
360
|
+
// Each triangle filed under the layers whose middle it spans, so a layer looks only at its own
|
|
361
|
+
const byLayer = Array.from({ length: count }, () => []);
|
|
362
|
+
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);
|
|
363
|
+
let below = null, held = null, prev = null, counted = null, overhangArea = 0;
|
|
364
|
+
const fused = []; // on the first layer, regions nearer each other than firstGap: { at, gap } (the first layer spreads, and they print as one)
|
|
365
|
+
const px = (x) => Math.floor((x - lo[0]) / pitch) + 3;
|
|
366
|
+
// Only what the printer can lay counts: each layer is opened by half a line (eroded, then grown back), so anything
|
|
367
|
+
// narrower than a line, which the slicer drops, is not a region at all
|
|
368
|
+
// Square windows, by running sums along each row and then each column: any (grow) or all (shrink) within r pixels
|
|
369
|
+
const sweep = (f, r, all) => {
|
|
370
|
+
const tmp = new Uint8Array(nx * ny), out = new Uint8Array(nx * ny), w = 2 * r + 1, sum = new Int32Array(Math.max(nx, ny) + 1);
|
|
371
|
+
for (let j = 0; j < ny; j++) { // along x
|
|
372
|
+
const row = j * nx; sum[0] = 0;
|
|
373
|
+
for (let i = 0; i < nx; i++) sum[i + 1] = sum[i] + f[row + i];
|
|
374
|
+
for (let i = 0; i < nx; i++) { const a = Math.max(0, i - r), b = Math.min(nx, i + r + 1), n = sum[b] - sum[a]; tmp[row + i] = all ? (b - a === w && n === w ? 1 : 0) : n > 0 ? 1 : 0; }
|
|
375
|
+
}
|
|
376
|
+
for (let i = 0; i < nx; i++) { // then along y
|
|
377
|
+
sum[0] = 0;
|
|
378
|
+
for (let j = 0; j < ny; j++) sum[j + 1] = sum[j] + tmp[j * nx + i];
|
|
379
|
+
for (let j = 0; j < ny; j++) { const a = Math.max(0, j - r), b = Math.min(ny, j + r + 1), n = sum[b] - sum[a]; out[j * nx + i] = all ? (b - a === w && n === w ? 1 : 0) : n > 0 ? 1 : 0; }
|
|
380
|
+
}
|
|
381
|
+
return out;
|
|
382
|
+
};
|
|
383
|
+
const erode = (f) => sweep(f, r2, true), dilate = (f, r) => sweep(f, r, false);
|
|
384
|
+
const opened = (f) => dilate(erode(f), r2);
|
|
385
|
+
for (let L = 0; L < count; L++) {
|
|
386
|
+
onLayer?.(L, count);
|
|
387
|
+
const z = lo[2] + (L + 0.5) * layer, segs = [];
|
|
388
|
+
for (const t of byLayer[L]) {
|
|
389
|
+
if (zmin[t] > z || zmax[t] < z) continue;
|
|
390
|
+
const pts = [];
|
|
391
|
+
for (let i = 0; i < 3; i++) {
|
|
392
|
+
const a = tris[t][i], b = tris[t][(i + 1) % 3];
|
|
393
|
+
if ((a[2] < z) !== (b[2] < z)) { const u = (z - a[2]) / (b[2] - a[2]); pts.push([a[0] + u * (b[0] - a[0]), a[1] + u * (b[1] - a[1])]); }
|
|
394
|
+
}
|
|
395
|
+
if (pts.length === 2) segs.push(pts);
|
|
396
|
+
}
|
|
397
|
+
// Scanline fill, even-odd, a pixel row at a time
|
|
398
|
+
const raw = new Uint8Array(nx * ny);
|
|
399
|
+
for (let j = 0; j < ny; j++) {
|
|
400
|
+
const y = lo[1] + (j - 2.5) * pitch, xs = [];
|
|
401
|
+
for (const [a, b] of segs) if ((a[1] <= y) !== (b[1] <= y)) xs.push(a[0] + ((y - a[1]) / (b[1] - a[1])) * (b[0] - a[0]));
|
|
402
|
+
xs.sort((p, q) => p - q);
|
|
403
|
+
for (let k = 0; k + 1 < xs.length; k += 2) for (let i = Math.max(0, px(xs[k])); i <= Math.min(nx - 1, px(xs[k + 1]) - 1); i++) raw[j * nx + i] = 1;
|
|
404
|
+
}
|
|
405
|
+
// A layer drawn the same as the one below (an upright wall, most of most parts) lies wholly on it: nothing to measure
|
|
406
|
+
let same = !!prev;
|
|
407
|
+
for (let n = 0; same && n < nx * ny; n++) if (raw[n] !== prev[n]) same = false;
|
|
408
|
+
prev = raw;
|
|
409
|
+
history.push(same ? below : null); // (filled in below, once this layer is drawn)
|
|
410
|
+
if (history.length > SPANS.at(-1) + 1) history[history.length - SPANS.at(-1) - 2] = null; // older layers are let go
|
|
411
|
+
if (same) continue;
|
|
412
|
+
const filled = opened(raw);
|
|
413
|
+
history[L] = filled;
|
|
414
|
+
if (L === 0 && firstGap > 0) {
|
|
415
|
+
// Each region labelled; grown by half the gap, two that meet were nearer than it. Where they meet is the spot
|
|
416
|
+
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 }; };
|
|
417
|
+
const own = label(filled), r = Math.max(1, Math.round(firstGap / 2 / pitch)), grown = label(dilate(filled, r));
|
|
418
|
+
if (own.n > 1) {
|
|
419
|
+
const seen = new Map(); // a grown region → the first own label in it, and the first pixel of a second
|
|
420
|
+
for (let e = 0; e < nx * ny; e++) {
|
|
421
|
+
if (!own.lab[e]) continue;
|
|
422
|
+
const g = grown.lab[e], first = seen.get(g);
|
|
423
|
+
if (first == null) seen.set(g, own.lab[e]);
|
|
424
|
+
else if (first !== own.lab[e] && !fused.some((x) => x.region === g)) fused.push({ region: g, at: [lo[0] + ((e % nx) - 2.5) * pitch, lo[1] + (Math.floor(e / nx) - 2.5) * pitch, z] });
|
|
425
|
+
}
|
|
426
|
+
for (const x of fused) { // the gap itself: how far this pixel is from the other region, in pixels, by growing it until they meet
|
|
427
|
+
let d = 1; const mine = own.lab[Math.round((x.at[1] - lo[1]) / pitch + 2.5) * nx + Math.round((x.at[0] - lo[0]) / pitch + 2.5)];
|
|
428
|
+
for (; d <= 2 * r + 1; d++) { const i0 = Math.round((x.at[0] - lo[0]) / pitch + 2.5), j0 = Math.round((x.at[1] - lo[1]) / pitch + 2.5); let met = false; for (let dj = -d; dj <= d && !met; dj++) for (let di = -d; di <= d && !met; di++) { const i = i0 + di, j = j0 + dj; if (i >= 0 && j >= 0 && i < nx && j < ny && own.lab[j * nx + i] && own.lab[j * nx + i] !== mine) met = true; } if (met) break; }
|
|
429
|
+
x.gap = +(Math.max(0, d - 1) * pitch).toFixed(2); delete x.region;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (below) {
|
|
434
|
+
held = dilate(below, reach); // where this layer may lie: on the one below, or a step out from it
|
|
435
|
+
let unheld = 0;
|
|
436
|
+
for (let n = 0; n < nx * ny; n++) if (filled[n] && !held[n]) unheld++;
|
|
437
|
+
// A lean kept up over a span: what isn't straight above the layer below, and lies out past what the layer a
|
|
438
|
+
// span down allows. (What is straight above the layer below rests on it: the layers over a bridge, say.)
|
|
439
|
+
let leant = 0;
|
|
440
|
+
for (let k = 0; k < SPANS.length; k++) {
|
|
441
|
+
const far = L >= SPANS[k] ? history[L - SPANS[k]] : null;
|
|
442
|
+
if (!far || far === below) continue;
|
|
443
|
+
const heldOver = dilate(far, reachOver[k]);
|
|
444
|
+
for (let n = 0; n < nx * ny; n++) if (filled[n] && held[n] && !below[n] && !heldOver[n] && !(counted && counted[n])) { leant++; (counted ||= new Uint8Array(nx * ny))[n] = 1; }
|
|
445
|
+
}
|
|
446
|
+
counted = null;
|
|
447
|
+
overhangArea += (unheld + leant) * pitch * pitch;
|
|
448
|
+
// Regions of this layer that lie on nothing: an island is a region with no pixel within reach of the layer
|
|
449
|
+
// below, so only a region holding an unheld pixel can be one. Most layers have none, and are done here.
|
|
450
|
+
if (unheld) {
|
|
451
|
+
const seen = new Uint8Array(nx * ny), stack = new Int32Array(nx * ny);
|
|
452
|
+
for (let start = 0; start < nx * ny; start++) {
|
|
453
|
+
if (!filled[start] || held[start] || seen[start]) continue;
|
|
454
|
+
let top = 0, onSomething = false;
|
|
455
|
+
const cells = [];
|
|
456
|
+
stack[top++] = start; seen[start] = 1;
|
|
457
|
+
while (top) {
|
|
458
|
+
const e = stack[--top], i = e % nx;
|
|
459
|
+
cells.push(e);
|
|
460
|
+
if (held[e]) onSomething = true;
|
|
461
|
+
if (i + 1 < nx && filled[e + 1] && !seen[e + 1]) { seen[e + 1] = 1; stack[top++] = e + 1; }
|
|
462
|
+
if (i > 0 && filled[e - 1] && !seen[e - 1]) { seen[e - 1] = 1; stack[top++] = e - 1; }
|
|
463
|
+
if (e + nx < nx * ny && filled[e + nx] && !seen[e + nx]) { seen[e + nx] = 1; stack[top++] = e + nx; }
|
|
464
|
+
if (e >= nx && filled[e - nx] && !seen[e - nx]) { seen[e - nx] = 1; stack[top++] = e - nx; }
|
|
465
|
+
}
|
|
466
|
+
if (!onSomething) {
|
|
467
|
+
const cx = cells.reduce((s, e) => s + (e % nx), 0) / cells.length, cy = cells.reduce((s, e) => s + Math.floor(e / nx), 0) / cells.length;
|
|
468
|
+
out.push({ z: +z.toFixed(3), area: cells.length * pitch * pitch, at: [lo[0] + (cx - 2.5) * pitch, lo[1] + (cy - 2.5) * pitch, z], cells: cells.map((e) => [lo[0] + ((e % nx) - 2.5) * pitch, lo[1] + (Math.floor(e / nx) - 2.5) * pitch]) });
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
below = filled;
|
|
474
|
+
}
|
|
475
|
+
return { islands: out, layers: count, overhang: overhangArea, fused };
|
|
476
|
+
}
|
|
477
|
+
|
|
253
478
|
export function shape(polys, eps = 1e-3, drop = 0.05, hang = 1) {
|
|
254
479
|
const ids = new Map(), at = [], key = (v) => v.map((c) => Math.round(c / eps)).join(',');
|
|
255
480
|
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); }));
|