@pascal-app/geometry-script 1.0.5 → 1.0.7
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/dist/adapter.d.ts +41 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +325 -0
- package/dist/author-scene.d.ts +7 -14
- package/dist/author-scene.d.ts.map +1 -1
- package/dist/author-scene.js +52 -7
- package/dist/blender.d.ts +1 -0
- package/dist/blender.d.ts.map +1 -1
- package/dist/blender.js +9 -1
- package/dist/compile.d.ts +36 -6
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +137 -12
- package/dist/cut.d.ts +4 -2
- package/dist/cut.d.ts.map +1 -1
- package/dist/cut.js +104 -18
- package/dist/design.d.ts +17 -0
- package/dist/design.d.ts.map +1 -0
- package/dist/design.js +52 -0
- package/dist/sandbox-worker.js +2 -2
- package/dist/sandbox.js +1 -1
- package/dist/transform.d.ts.map +1 -1
- package/dist/transform.js +115 -7
- package/dist/usage-shape.d.ts +15 -0
- package/dist/usage-shape.d.ts.map +1 -0
- package/dist/usage-shape.js +693 -0
- package/package.json +15 -5
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
/** What the shape may weigh in the manifest, which rides inline in the node (24 KiB in all). */
|
|
2
|
+
export const SHAPE_MAX_BYTES = 10 * 1024;
|
|
3
|
+
const CELL_M = 0.005;
|
|
4
|
+
const LAYER_M = 0.02;
|
|
5
|
+
const MAX_LAYERS = 200;
|
|
6
|
+
const MAX_CELLS_TOTAL = 48_000_000;
|
|
7
|
+
const MAX_BANDS = 24;
|
|
8
|
+
const EDGE_EPSILON = 1e-9;
|
|
9
|
+
/**
|
|
10
|
+
* Looser and looser readings of the same layers, tried in turn until the shape fits its budget: how
|
|
11
|
+
* much a layer may differ from the band it joins (a share of the band's cells), and how far the
|
|
12
|
+
* traced outline may leave the raster's, in cells.
|
|
13
|
+
*/
|
|
14
|
+
const LEVELS = [
|
|
15
|
+
{ grow: 0.03, simplify: 0.75 },
|
|
16
|
+
{ grow: 0.06, simplify: 2 },
|
|
17
|
+
{ grow: 0.12, simplify: 4 },
|
|
18
|
+
{ grow: 0.25, simplify: 8 },
|
|
19
|
+
];
|
|
20
|
+
function collect(meshes) {
|
|
21
|
+
let count = 0;
|
|
22
|
+
for (const mesh of meshes) {
|
|
23
|
+
const position = mesh.geometry.getAttribute('position');
|
|
24
|
+
const index = mesh.geometry.getIndex();
|
|
25
|
+
count += Math.floor((index ? index.count : position.count) / 3);
|
|
26
|
+
}
|
|
27
|
+
const vertices = new Float32Array(count * 9);
|
|
28
|
+
let at = 0;
|
|
29
|
+
const p = [0, 0, 0];
|
|
30
|
+
for (const mesh of meshes) {
|
|
31
|
+
mesh.updateWorldMatrix(true, false);
|
|
32
|
+
const e = mesh.matrixWorld.elements;
|
|
33
|
+
const position = mesh.geometry.getAttribute('position');
|
|
34
|
+
const index = mesh.geometry.getIndex();
|
|
35
|
+
const triangles = Math.floor((index ? index.count : position.count) / 3);
|
|
36
|
+
for (let t = 0; t < triangles; t++)
|
|
37
|
+
for (let k = 0; k < 3; k++) {
|
|
38
|
+
const v = index ? index.getX(t * 3 + k) : t * 3 + k;
|
|
39
|
+
const x = position.getX(v);
|
|
40
|
+
const y = position.getY(v);
|
|
41
|
+
const z = position.getZ(v);
|
|
42
|
+
p[0] = e[0] * x + e[4] * y + e[8] * z + e[12];
|
|
43
|
+
p[1] = e[1] * x + e[5] * y + e[9] * z + e[13];
|
|
44
|
+
p[2] = e[2] * x + e[6] * y + e[10] * z + e[14];
|
|
45
|
+
vertices[at++] = p[0];
|
|
46
|
+
vertices[at++] = p[1];
|
|
47
|
+
vertices[at++] = p[2];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { vertices, count, kinds: classify(vertices, count) };
|
|
51
|
+
}
|
|
52
|
+
const OUTWARD = 1;
|
|
53
|
+
const INWARD = -1;
|
|
54
|
+
const TWIN = 2;
|
|
55
|
+
const REDUNDANT = 0;
|
|
56
|
+
/**
|
|
57
|
+
* How each triangle takes part in the solid fill. A face baked twice, once wound each way (the
|
|
58
|
+
* back faces of a double-sided material, `bakeBackFaces`), says nothing of which side is out: one
|
|
59
|
+
* of the two stands for the pair, to be filled by parity (TWIN), the other is left out. Every other
|
|
60
|
+
* triangle is OUTWARD, or INWARD where its closed shell has a negative signed volume (components
|
|
61
|
+
* are triangles sharing vertices; an open one is left as authored), so that a shell, an inside-out
|
|
62
|
+
* one and shells overlapping inside one mesh all add up as solid.
|
|
63
|
+
*/
|
|
64
|
+
function classify(vertices, count) {
|
|
65
|
+
const kinds = new Int8Array(count).fill(OUTWARD);
|
|
66
|
+
const ids = new Map();
|
|
67
|
+
const corner = new Int32Array(count * 3);
|
|
68
|
+
for (let t = 0; t < count; t++)
|
|
69
|
+
for (let k = 0; k < 3; k++) {
|
|
70
|
+
const o = t * 9 + k * 3;
|
|
71
|
+
const key = `${Math.round(vertices[o] * 1e4)},${Math.round(vertices[o + 1] * 1e4)},${Math.round(vertices[o + 2] * 1e4)}`;
|
|
72
|
+
let id = ids.get(key);
|
|
73
|
+
if (id === undefined) {
|
|
74
|
+
id = ids.size;
|
|
75
|
+
ids.set(key, id);
|
|
76
|
+
}
|
|
77
|
+
corner[t * 3 + k] = id;
|
|
78
|
+
}
|
|
79
|
+
const faces = new Map();
|
|
80
|
+
for (let t = 0; t < count; t++) {
|
|
81
|
+
const [a, b, c] = [corner[t * 3], corner[t * 3 + 1], corner[t * 3 + 2]];
|
|
82
|
+
const sorted = [a, b, c].sort((x, y) => x - y);
|
|
83
|
+
const key = sorted.join(',');
|
|
84
|
+
// wound as the sorted triple is, or the other way
|
|
85
|
+
const even = (a === sorted[0] && b === sorted[1]) ||
|
|
86
|
+
(b === sorted[0] && c === sorted[1]) ||
|
|
87
|
+
(c === sorted[0] && a === sorted[1]);
|
|
88
|
+
const entry = faces.get(key) ?? { forward: -1, backward: -1 };
|
|
89
|
+
if (even)
|
|
90
|
+
entry.forward = entry.forward < 0 ? t : entry.forward;
|
|
91
|
+
else
|
|
92
|
+
entry.backward = entry.backward < 0 ? t : entry.backward;
|
|
93
|
+
faces.set(key, entry);
|
|
94
|
+
}
|
|
95
|
+
for (const { forward, backward } of faces.values())
|
|
96
|
+
if (forward >= 0 && backward >= 0) {
|
|
97
|
+
kinds[forward] = TWIN;
|
|
98
|
+
kinds[backward] = REDUNDANT;
|
|
99
|
+
}
|
|
100
|
+
const parent = Int32Array.from({ length: count }, (_, i) => i);
|
|
101
|
+
const find = (i) => {
|
|
102
|
+
while (parent[i] !== i) {
|
|
103
|
+
parent[i] = parent[parent[i]];
|
|
104
|
+
i = parent[i];
|
|
105
|
+
}
|
|
106
|
+
return i;
|
|
107
|
+
};
|
|
108
|
+
const owner = new Int32Array(ids.size).fill(-1);
|
|
109
|
+
for (let t = 0; t < count; t++) {
|
|
110
|
+
if (kinds[t] !== OUTWARD)
|
|
111
|
+
continue;
|
|
112
|
+
for (let k = 0; k < 3; k++) {
|
|
113
|
+
const id = corner[t * 3 + k];
|
|
114
|
+
if (owner[id] === -1)
|
|
115
|
+
owner[id] = t;
|
|
116
|
+
else
|
|
117
|
+
parent[find(t)] = find(owner[id]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const width = ids.size + 1;
|
|
121
|
+
const edges = new Map();
|
|
122
|
+
const edge = (t, k) => {
|
|
123
|
+
const a = corner[t * 3 + k];
|
|
124
|
+
const b = corner[t * 3 + ((k + 1) % 3)];
|
|
125
|
+
return Math.min(a, b) * width + Math.max(a, b);
|
|
126
|
+
};
|
|
127
|
+
for (let t = 0; t < count; t++)
|
|
128
|
+
if (kinds[t] === OUTWARD)
|
|
129
|
+
for (let k = 0; k < 3; k++)
|
|
130
|
+
edges.set(edge(t, k), (edges.get(edge(t, k)) ?? 0) + 1);
|
|
131
|
+
const component = new Map();
|
|
132
|
+
for (let t = 0; t < count; t++) {
|
|
133
|
+
if (kinds[t] !== OUTWARD)
|
|
134
|
+
continue;
|
|
135
|
+
const entry = component.get(find(t)) ?? { volume: 0, open: false };
|
|
136
|
+
const o = t * 9;
|
|
137
|
+
const v = (i) => vertices[o + i];
|
|
138
|
+
entry.volume +=
|
|
139
|
+
(v(0) * (v(4) * v(8) - v(5) * v(7)) -
|
|
140
|
+
v(1) * (v(3) * v(8) - v(5) * v(6)) +
|
|
141
|
+
v(2) * (v(3) * v(7) - v(4) * v(6))) /
|
|
142
|
+
6;
|
|
143
|
+
for (let k = 0; k < 3; k++)
|
|
144
|
+
if ((edges.get(edge(t, k)) ?? 0) % 2 === 1)
|
|
145
|
+
entry.open = true;
|
|
146
|
+
component.set(find(t), entry);
|
|
147
|
+
}
|
|
148
|
+
for (let t = 0; t < count; t++) {
|
|
149
|
+
if (kinds[t] !== OUTWARD)
|
|
150
|
+
continue;
|
|
151
|
+
const entry = component.get(find(t));
|
|
152
|
+
if (!entry.open && entry.volume < 0)
|
|
153
|
+
kinds[t] = INWARD;
|
|
154
|
+
}
|
|
155
|
+
return kinds;
|
|
156
|
+
}
|
|
157
|
+
/** Sutherland–Hodgman against one half-space of a convex polygon of `stride`-wide vertices. */
|
|
158
|
+
function clip(polygon, stride, axis, bound, keepAbove) {
|
|
159
|
+
const out = [];
|
|
160
|
+
const n = polygon.length / stride;
|
|
161
|
+
const side = (v) => (keepAbove ? v - bound : bound - v);
|
|
162
|
+
for (let i = 0; i < n; i++) {
|
|
163
|
+
const a = i * stride;
|
|
164
|
+
const b = ((i + 1) % n) * stride;
|
|
165
|
+
const sa = side(polygon[a + axis]);
|
|
166
|
+
const sb = side(polygon[b + axis]);
|
|
167
|
+
if (sa >= 0)
|
|
168
|
+
for (let k = 0; k < stride; k++)
|
|
169
|
+
out.push(polygon[a + k]);
|
|
170
|
+
if (sa >= 0 !== sb >= 0) {
|
|
171
|
+
const t = sa / (sa - sb);
|
|
172
|
+
for (let k = 0; k < stride; k++)
|
|
173
|
+
out.push(polygon[a + k] + (polygon[b + k] - polygon[a + k]) * t);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
/** Marks every cell the polygon (x, z pairs) touches: a conservative fill, a cell wider than the build at worst. */
|
|
179
|
+
function fill(grid, raster, polygon) {
|
|
180
|
+
const { minX, minZ, cell, nx, nz } = raster;
|
|
181
|
+
const n = polygon.length / 2;
|
|
182
|
+
if (n === 0)
|
|
183
|
+
return;
|
|
184
|
+
let zLow = Number.POSITIVE_INFINITY;
|
|
185
|
+
let zHigh = Number.NEGATIVE_INFINITY;
|
|
186
|
+
for (let i = 0; i < n; i++) {
|
|
187
|
+
zLow = Math.min(zLow, polygon[i * 2 + 1]);
|
|
188
|
+
zHigh = Math.max(zHigh, polygon[i * 2 + 1]);
|
|
189
|
+
}
|
|
190
|
+
const j0 = Math.max(0, Math.floor((zLow - minZ) / cell));
|
|
191
|
+
const j1 = Math.min(nz - 1, Math.max(j0, Math.floor((zHigh - minZ) / cell - EDGE_EPSILON)));
|
|
192
|
+
for (let j = j0; j <= j1; j++) {
|
|
193
|
+
let strip = polygon;
|
|
194
|
+
if (j1 > j0) {
|
|
195
|
+
strip = clip(polygon, 2, 1, minZ + j * cell, true);
|
|
196
|
+
strip = clip(strip, 2, 1, minZ + (j + 1) * cell, false);
|
|
197
|
+
}
|
|
198
|
+
let xLow = Number.POSITIVE_INFINITY;
|
|
199
|
+
let xHigh = Number.NEGATIVE_INFINITY;
|
|
200
|
+
for (let i = 0; i < strip.length; i += 2) {
|
|
201
|
+
xLow = Math.min(xLow, strip[i]);
|
|
202
|
+
xHigh = Math.max(xHigh, strip[i]);
|
|
203
|
+
}
|
|
204
|
+
if (xLow > xHigh)
|
|
205
|
+
continue;
|
|
206
|
+
const i0 = Math.max(0, Math.floor((xLow - minX) / cell));
|
|
207
|
+
const i1 = Math.min(nx - 1, Math.max(i0, Math.floor((xHigh - minX) / cell - EDGE_EPSILON)));
|
|
208
|
+
grid.fill(1, j * nx + i0, j * nx + i1 + 1);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Where a triangle that spans the layer's middle plane crosses the horizontal rays along x through
|
|
213
|
+
* each raster row's centre. An oriented triangle also says which way: a surface whose normal points
|
|
214
|
+
* back along the ray is one the ray enters through, +1; one it leaves through, -1.
|
|
215
|
+
*/
|
|
216
|
+
function cross(vertices, o, kind, y, raster, crossings) {
|
|
217
|
+
const { minZ, cell, nz } = raster;
|
|
218
|
+
const ax = vertices[o], ay = vertices[o + 1], az = vertices[o + 2];
|
|
219
|
+
const bx = vertices[o + 3], by = vertices[o + 4], bz = vertices[o + 5];
|
|
220
|
+
const cx = vertices[o + 6], cy = vertices[o + 7], cz = vertices[o + 8];
|
|
221
|
+
const normalX = (by - ay) * (cz - az) - (bz - az) * (cy - ay);
|
|
222
|
+
if (Math.abs(normalX) < 1e-12)
|
|
223
|
+
return;
|
|
224
|
+
const wind = (normalX < 0 ? 1 : -1) * (kind === INWARD ? -1 : 1);
|
|
225
|
+
const at = [];
|
|
226
|
+
const edge = (px, py, pz, qx, qy, qz) => {
|
|
227
|
+
if (py > y === qy > y)
|
|
228
|
+
return;
|
|
229
|
+
const t = (y - py) / (qy - py);
|
|
230
|
+
at.push([px + (qx - px) * t, pz + (qz - pz) * t]);
|
|
231
|
+
};
|
|
232
|
+
edge(ax, ay, az, bx, by, bz);
|
|
233
|
+
edge(bx, by, bz, cx, cy, cz);
|
|
234
|
+
edge(cx, cy, cz, ax, ay, az);
|
|
235
|
+
if (at.length !== 2)
|
|
236
|
+
return;
|
|
237
|
+
const [p, q] = at;
|
|
238
|
+
if (p[1] === q[1])
|
|
239
|
+
return;
|
|
240
|
+
const [zLow, zHigh] = p[1] < q[1] ? [p[1], q[1]] : [q[1], p[1]];
|
|
241
|
+
const j0 = Math.max(0, Math.ceil((zLow - minZ) / cell - 0.5));
|
|
242
|
+
const j1 = Math.min(nz - 1, Math.ceil((zHigh - minZ) / cell - 0.5) - 1);
|
|
243
|
+
for (let j = j0; j <= j1; j++) {
|
|
244
|
+
const z = minZ + (j + 0.5) * cell;
|
|
245
|
+
const x = p[0] + ((z - p[1]) / (q[1] - p[1])) * (q[0] - p[0]);
|
|
246
|
+
if (kind === TWIN) {
|
|
247
|
+
const row = crossings.parity.get(j);
|
|
248
|
+
if (row)
|
|
249
|
+
row.push(x);
|
|
250
|
+
else
|
|
251
|
+
crossings.parity.set(j, [x]);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
const row = crossings.oriented.get(j);
|
|
255
|
+
if (row)
|
|
256
|
+
row.push({ x, wind });
|
|
257
|
+
else
|
|
258
|
+
crossings.oriented.set(j, [{ x, wind }]);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Fills each row between the crossings where the surfaces enclose something: a nonzero winding for
|
|
264
|
+
* the oriented ones, so overlapping solids read as solid, and an odd count for the unoriented pairs.
|
|
265
|
+
*/
|
|
266
|
+
function solid(grid, raster, crossings) {
|
|
267
|
+
const { minX, cell, nx } = raster;
|
|
268
|
+
const span = (j, from, to) => {
|
|
269
|
+
const i0 = Math.max(0, Math.floor((from - minX) / cell));
|
|
270
|
+
const i1 = Math.min(nx - 1, Math.max(i0, Math.floor((to - minX) / cell - EDGE_EPSILON)));
|
|
271
|
+
grid.fill(1, j * nx + i0, j * nx + i1 + 1);
|
|
272
|
+
};
|
|
273
|
+
for (const [j, row] of crossings.oriented) {
|
|
274
|
+
row.sort((a, b) => a.x - b.x);
|
|
275
|
+
let winding = 0;
|
|
276
|
+
for (let k = 0; k + 1 < row.length; k++) {
|
|
277
|
+
winding += row[k].wind;
|
|
278
|
+
if (winding !== 0)
|
|
279
|
+
span(j, row[k].x, row[k + 1].x);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
for (const [j, row] of crossings.parity) {
|
|
283
|
+
row.sort((a, b) => a - b);
|
|
284
|
+
for (let k = 0; k + 1 < row.length; k += 2)
|
|
285
|
+
span(j, row[k], row[k + 1]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** The plan footprint of everything the build has in each thin layer of its height. */
|
|
289
|
+
function rasterise(triangles) {
|
|
290
|
+
const { vertices, count, kinds } = triangles;
|
|
291
|
+
if (count === 0)
|
|
292
|
+
return null;
|
|
293
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
294
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
295
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
296
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
297
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
298
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
299
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
300
|
+
minX = Math.min(minX, vertices[i]);
|
|
301
|
+
maxX = Math.max(maxX, vertices[i]);
|
|
302
|
+
minY = Math.min(minY, vertices[i + 1]);
|
|
303
|
+
maxY = Math.max(maxY, vertices[i + 1]);
|
|
304
|
+
minZ = Math.min(minZ, vertices[i + 2]);
|
|
305
|
+
maxZ = Math.max(maxZ, vertices[i + 2]);
|
|
306
|
+
}
|
|
307
|
+
if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite))
|
|
308
|
+
return null;
|
|
309
|
+
const height = maxY - minY;
|
|
310
|
+
const layers = Math.min(MAX_LAYERS, Math.max(1, Math.ceil(height / LAYER_M)));
|
|
311
|
+
const layer = Math.max(height / layers, 1e-6);
|
|
312
|
+
let cell = CELL_M;
|
|
313
|
+
const size = (c) => (Math.floor((maxX - minX) / c) + 1) * (Math.floor((maxZ - minZ) / c) + 1);
|
|
314
|
+
while (size(cell) * layers > MAX_CELLS_TOTAL)
|
|
315
|
+
cell *= 1.25;
|
|
316
|
+
const nx = Math.floor((maxX - minX) / cell) + 1;
|
|
317
|
+
const nz = Math.floor((maxZ - minZ) / cell) + 1;
|
|
318
|
+
const cells = nx * nz;
|
|
319
|
+
const raster = { minX, minZ, cell, nx, nz };
|
|
320
|
+
const grids = new Uint8Array(cells * layers);
|
|
321
|
+
const first = new Int32Array(count);
|
|
322
|
+
const last = new Int32Array(count);
|
|
323
|
+
const perLayer = new Int32Array(layers + 1);
|
|
324
|
+
for (let t = 0; t < count; t++) {
|
|
325
|
+
const o = t * 9;
|
|
326
|
+
const low = Math.min(vertices[o + 1], vertices[o + 4], vertices[o + 7]);
|
|
327
|
+
const high = Math.max(vertices[o + 1], vertices[o + 4], vertices[o + 7]);
|
|
328
|
+
first[t] = Math.min(layers - 1, Math.max(0, Math.floor((low - minY) / layer)));
|
|
329
|
+
last[t] = Math.min(layers - 1, Math.max(first[t], Math.floor((high - minY) / layer - EDGE_EPSILON)));
|
|
330
|
+
for (let l = first[t]; l <= last[t]; l++)
|
|
331
|
+
perLayer[l + 1] += 1;
|
|
332
|
+
}
|
|
333
|
+
for (let l = 0; l < layers; l++)
|
|
334
|
+
perLayer[l + 1] += perLayer[l];
|
|
335
|
+
const lists = new Int32Array(perLayer[layers]);
|
|
336
|
+
const cursor = perLayer.slice(0, layers);
|
|
337
|
+
for (let t = 0; t < count; t++)
|
|
338
|
+
for (let l = first[t]; l <= last[t]; l++)
|
|
339
|
+
lists[cursor[l]++] = t;
|
|
340
|
+
for (let l = 0; l < layers; l++) {
|
|
341
|
+
const grid = grids.subarray(l * cells, (l + 1) * cells);
|
|
342
|
+
const y0 = minY + l * layer;
|
|
343
|
+
const y1 = y0 + layer;
|
|
344
|
+
const middle = (y0 + y1) / 2;
|
|
345
|
+
const crossings = { oriented: new Map(), parity: new Map() };
|
|
346
|
+
for (let i = perLayer[l]; i < perLayer[l + 1]; i++) {
|
|
347
|
+
if (kinds[lists[i]] === REDUNDANT)
|
|
348
|
+
continue;
|
|
349
|
+
const o = lists[i] * 9;
|
|
350
|
+
let polygon = [
|
|
351
|
+
vertices[o],
|
|
352
|
+
vertices[o + 1],
|
|
353
|
+
vertices[o + 2],
|
|
354
|
+
vertices[o + 3],
|
|
355
|
+
vertices[o + 4],
|
|
356
|
+
vertices[o + 5],
|
|
357
|
+
vertices[o + 6],
|
|
358
|
+
vertices[o + 7],
|
|
359
|
+
vertices[o + 8],
|
|
360
|
+
];
|
|
361
|
+
const low = Math.min(polygon[1], polygon[4], polygon[7]);
|
|
362
|
+
const high = Math.max(polygon[1], polygon[4], polygon[7]);
|
|
363
|
+
if (low <= middle && high > middle)
|
|
364
|
+
cross(vertices, o, kinds[lists[i]], middle, raster, crossings);
|
|
365
|
+
if (low < y0)
|
|
366
|
+
polygon = clip(polygon, 3, 1, y0, true);
|
|
367
|
+
if (high > y1)
|
|
368
|
+
polygon = clip(polygon, 3, 1, y1, false);
|
|
369
|
+
const plan = [];
|
|
370
|
+
for (let k = 0; k < polygon.length; k += 3)
|
|
371
|
+
plan.push(polygon[k], polygon[k + 2]);
|
|
372
|
+
fill(grid, raster, plan);
|
|
373
|
+
}
|
|
374
|
+
solid(grid, raster, crossings);
|
|
375
|
+
}
|
|
376
|
+
return { grids, cells, count: layers, layer, minY, raster };
|
|
377
|
+
}
|
|
378
|
+
/** Runs of layers whose footprints differ by less than `grow` of the run's: a run reads as the union of its layers. */
|
|
379
|
+
function band(layers, grow) {
|
|
380
|
+
const { grids, cells, count } = layers;
|
|
381
|
+
const bands = [];
|
|
382
|
+
let current = null;
|
|
383
|
+
let area = 0;
|
|
384
|
+
for (let l = 0; l < count; l++) {
|
|
385
|
+
const grid = grids.subarray(l * cells, (l + 1) * cells);
|
|
386
|
+
let layerArea = 0;
|
|
387
|
+
for (let i = 0; i < cells; i++)
|
|
388
|
+
layerArea += grid[i];
|
|
389
|
+
if (layerArea === 0) {
|
|
390
|
+
current = null;
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (current) {
|
|
394
|
+
let added = 0;
|
|
395
|
+
let missing = 0;
|
|
396
|
+
for (let i = 0; i < cells; i++) {
|
|
397
|
+
if (grid[i] && !current.cells[i])
|
|
398
|
+
added++;
|
|
399
|
+
else if (!grid[i] && current.cells[i])
|
|
400
|
+
missing++;
|
|
401
|
+
}
|
|
402
|
+
const allowed = Math.max(4, grow * area);
|
|
403
|
+
if (added <= allowed && missing <= allowed) {
|
|
404
|
+
for (let i = 0; i < cells; i++)
|
|
405
|
+
if (grid[i])
|
|
406
|
+
current.cells[i] = 1;
|
|
407
|
+
current.to = l;
|
|
408
|
+
area += added;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
current = { from: l, to: l, cells: Uint8Array.from(grid) };
|
|
413
|
+
area = layerArea;
|
|
414
|
+
bands.push(current);
|
|
415
|
+
}
|
|
416
|
+
return bands;
|
|
417
|
+
}
|
|
418
|
+
const signedArea = (ring) => {
|
|
419
|
+
let a = 0;
|
|
420
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++)
|
|
421
|
+
a += ring[j][0] * ring[i][1] - ring[i][0] * ring[j][1];
|
|
422
|
+
return a / 2;
|
|
423
|
+
};
|
|
424
|
+
/** The boundary of the filled cells as closed rings, filled side on the left: outer rings turn counter-clockwise, holes clockwise. */
|
|
425
|
+
function trace(cells, raster) {
|
|
426
|
+
const { nx, nz } = raster;
|
|
427
|
+
const filled = (i, j) => i >= 0 && j >= 0 && i < nx && j < nz && cells[j * nx + i] === 1;
|
|
428
|
+
const next = new Map();
|
|
429
|
+
const key = (i, j) => j * (nx + 1) + i;
|
|
430
|
+
const add = (i0, j0, i1, j1) => {
|
|
431
|
+
const k = key(i0, j0);
|
|
432
|
+
const list = next.get(k);
|
|
433
|
+
if (list)
|
|
434
|
+
list.push(key(i1, j1));
|
|
435
|
+
else
|
|
436
|
+
next.set(k, [key(i1, j1)]);
|
|
437
|
+
};
|
|
438
|
+
for (let j = 0; j < nz; j++)
|
|
439
|
+
for (let i = 0; i < nx; i++) {
|
|
440
|
+
if (!cells[j * nx + i])
|
|
441
|
+
continue;
|
|
442
|
+
if (!filled(i, j - 1))
|
|
443
|
+
add(i, j, i + 1, j);
|
|
444
|
+
if (!filled(i + 1, j))
|
|
445
|
+
add(i + 1, j, i + 1, j + 1);
|
|
446
|
+
if (!filled(i, j + 1))
|
|
447
|
+
add(i + 1, j + 1, i, j + 1);
|
|
448
|
+
if (!filled(i - 1, j))
|
|
449
|
+
add(i, j + 1, i, j);
|
|
450
|
+
}
|
|
451
|
+
const rings = [];
|
|
452
|
+
const w = nx + 1;
|
|
453
|
+
for (const [start] of next) {
|
|
454
|
+
while (next.get(start)?.length) {
|
|
455
|
+
const ring = [];
|
|
456
|
+
let at = start;
|
|
457
|
+
let heading = null;
|
|
458
|
+
do {
|
|
459
|
+
const options = next.get(at);
|
|
460
|
+
if (!options?.length)
|
|
461
|
+
break;
|
|
462
|
+
const here = [at % w, Math.floor(at / w)];
|
|
463
|
+
// Take the leftmost turn so cells touching at a corner stay separate rings.
|
|
464
|
+
let pick = 0;
|
|
465
|
+
if (options.length > 1) {
|
|
466
|
+
let best = -Infinity;
|
|
467
|
+
options.forEach((to, index) => {
|
|
468
|
+
const dx = (to % w) - here[0];
|
|
469
|
+
const dz = Math.floor(to / w) - here[1];
|
|
470
|
+
const angle = Math.atan2(dz, dx);
|
|
471
|
+
const turn = heading === null ? 0 : ((angle - heading + 3 * Math.PI) % (2 * Math.PI)) - Math.PI;
|
|
472
|
+
if (turn > best) {
|
|
473
|
+
best = turn;
|
|
474
|
+
pick = index;
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
const to = options.splice(pick, 1)[0];
|
|
479
|
+
ring.push([here[0], here[1]]);
|
|
480
|
+
heading = Math.atan2(Math.floor(to / w) - here[1], (to % w) - here[0]);
|
|
481
|
+
at = to;
|
|
482
|
+
} while (at !== start);
|
|
483
|
+
if (ring.length >= 4)
|
|
484
|
+
rings.push(ring);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return rings;
|
|
488
|
+
}
|
|
489
|
+
/** Douglas–Peucker over a closed ring. */
|
|
490
|
+
function simplify(ring, tolerance) {
|
|
491
|
+
if (ring.length <= 4)
|
|
492
|
+
return ring;
|
|
493
|
+
const distance = (p, a, b) => {
|
|
494
|
+
const dx = b[0] - a[0];
|
|
495
|
+
const dz = b[1] - a[1];
|
|
496
|
+
const length = Math.hypot(dx, dz);
|
|
497
|
+
return length < 1e-12
|
|
498
|
+
? Math.hypot(p[0] - a[0], p[1] - a[1])
|
|
499
|
+
: Math.abs((p[0] - a[0]) * dz - (p[1] - a[1]) * dx) / length;
|
|
500
|
+
};
|
|
501
|
+
const keep = new Array(ring.length).fill(false);
|
|
502
|
+
const run = (from, to) => {
|
|
503
|
+
let far = -1;
|
|
504
|
+
let best = tolerance;
|
|
505
|
+
for (let i = from + 1; i < to; i++) {
|
|
506
|
+
const d = distance(ring[i], ring[from], ring[to % ring.length]);
|
|
507
|
+
if (d > best) {
|
|
508
|
+
best = d;
|
|
509
|
+
far = i;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
if (far < 0)
|
|
513
|
+
return;
|
|
514
|
+
keep[far] = true;
|
|
515
|
+
run(from, far);
|
|
516
|
+
run(far, to);
|
|
517
|
+
};
|
|
518
|
+
let farthest = 0;
|
|
519
|
+
let reach = -1;
|
|
520
|
+
for (let i = 1; i < ring.length; i++) {
|
|
521
|
+
const d = Math.hypot(ring[i][0] - ring[0][0], ring[i][1] - ring[0][1]);
|
|
522
|
+
if (d > reach) {
|
|
523
|
+
reach = d;
|
|
524
|
+
farthest = i;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
keep[0] = true;
|
|
528
|
+
keep[farthest] = true;
|
|
529
|
+
run(0, farthest);
|
|
530
|
+
run(farthest, ring.length);
|
|
531
|
+
const out = ring.filter((_, i) => keep[i]);
|
|
532
|
+
return out.length >= 3 ? out : ring;
|
|
533
|
+
}
|
|
534
|
+
function inside(point, ring) {
|
|
535
|
+
let result = false;
|
|
536
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
537
|
+
const a = ring[i];
|
|
538
|
+
const b = ring[j];
|
|
539
|
+
if (a[1] > point[1] !== b[1] > point[1] &&
|
|
540
|
+
point[0] < ((b[0] - a[0]) * (point[1] - a[1])) / (b[1] - a[1]) + a[0])
|
|
541
|
+
result = !result;
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
545
|
+
const mm = (v) => Math.round(v * 1000) / 1000;
|
|
546
|
+
function polygons(cells, raster, simplifyCells) {
|
|
547
|
+
const { minX, minZ, cell } = raster;
|
|
548
|
+
const world = (ring) => ring.map(([i, j]) => [minX + i * cell, minZ + j * cell]);
|
|
549
|
+
const rings = trace(cells, raster)
|
|
550
|
+
.map((ring) => world(simplify(ring.map(([i, j]) => [i, j]), simplifyCells)).map(([x, z]) => [mm(x), mm(z)]))
|
|
551
|
+
.filter((ring) => ring.length >= 3 && Math.abs(signedArea(ring)) >= 2 * cell * cell);
|
|
552
|
+
const outers = rings.filter((ring) => signedArea(ring) > 0);
|
|
553
|
+
const holes = rings.filter((ring) => signedArea(ring) < 0);
|
|
554
|
+
const result = outers.map((outer) => ({ outer, holes: [] }));
|
|
555
|
+
for (const hole of holes) {
|
|
556
|
+
const owner = result
|
|
557
|
+
.filter((candidate) => inside(hole[0], candidate.outer))
|
|
558
|
+
.sort((a, b) => Math.abs(signedArea(a.outer)) - Math.abs(signedArea(b.outer)))[0];
|
|
559
|
+
if (owner)
|
|
560
|
+
owner.holes.push(hole);
|
|
561
|
+
}
|
|
562
|
+
return result.map(({ outer, holes: h }) => (h.length ? { outer, holes: h } : { outer }));
|
|
563
|
+
}
|
|
564
|
+
const bytes = (shape) => new TextEncoder().encode(JSON.stringify(shape)).byteLength;
|
|
565
|
+
/** One box over the whole build: the last resort when nothing finer fits the budget. */
|
|
566
|
+
function box(bounds) {
|
|
567
|
+
return {
|
|
568
|
+
tolerance: mm(Math.max(bounds.max[0] - bounds.min[0], bounds.max[2] - bounds.min[2])),
|
|
569
|
+
bands: [
|
|
570
|
+
{
|
|
571
|
+
y0: mm(bounds.min[1]),
|
|
572
|
+
y1: mm(bounds.max[1]),
|
|
573
|
+
polygons: [
|
|
574
|
+
{
|
|
575
|
+
outer: [
|
|
576
|
+
[mm(bounds.min[0]), mm(bounds.min[2])],
|
|
577
|
+
[mm(bounds.max[0]), mm(bounds.min[2])],
|
|
578
|
+
[mm(bounds.max[0]), mm(bounds.max[2])],
|
|
579
|
+
[mm(bounds.min[0]), mm(bounds.max[2])],
|
|
580
|
+
],
|
|
581
|
+
},
|
|
582
|
+
],
|
|
583
|
+
},
|
|
584
|
+
],
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
/** Adjacent bands merged, the pair that adds the fewest cells first, until `target` remain. */
|
|
588
|
+
function merged(bands, target) {
|
|
589
|
+
const out = bands.map((b) => ({ ...b, cells: Uint8Array.from(b.cells) }));
|
|
590
|
+
while (out.length > Math.max(1, target)) {
|
|
591
|
+
let best = 0;
|
|
592
|
+
let least = Number.POSITIVE_INFINITY;
|
|
593
|
+
for (let i = 0; i + 1 < out.length; i++) {
|
|
594
|
+
const a = out[i];
|
|
595
|
+
const b = out[i + 1];
|
|
596
|
+
let extra = 0;
|
|
597
|
+
for (let k = 0; k < a.cells.length; k++)
|
|
598
|
+
if (a.cells[k] !== b.cells[k])
|
|
599
|
+
extra++;
|
|
600
|
+
// a taller gap between them costs more, so equal differences merge the nearer pair first
|
|
601
|
+
const cost = extra + (b.from - a.to - 1) * 0.5;
|
|
602
|
+
if (cost < least) {
|
|
603
|
+
least = cost;
|
|
604
|
+
best = i;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const a = out[best];
|
|
608
|
+
const b = out[best + 1];
|
|
609
|
+
for (let k = 0; k < a.cells.length; k++)
|
|
610
|
+
if (b.cells[k])
|
|
611
|
+
a.cells[k] = 1;
|
|
612
|
+
a.to = b.to;
|
|
613
|
+
out.splice(best + 1, 1);
|
|
614
|
+
}
|
|
615
|
+
return out;
|
|
616
|
+
}
|
|
617
|
+
/** The cells within `radius` cells of a filled one (a separable box dilation, by prefix sums). */
|
|
618
|
+
function dilate(cells, raster, radius) {
|
|
619
|
+
const { nx, nz } = raster;
|
|
620
|
+
const pass = (source, lines, length, at) => {
|
|
621
|
+
const out = new Uint8Array(source.length);
|
|
622
|
+
const sums = new Int32Array(length + 1);
|
|
623
|
+
for (let line = 0; line < lines; line++) {
|
|
624
|
+
for (let i = 0; i < length; i++)
|
|
625
|
+
sums[i + 1] = sums[i] + source[at(line, i)];
|
|
626
|
+
for (let i = 0; i < length; i++)
|
|
627
|
+
out[at(line, i)] =
|
|
628
|
+
sums[Math.min(length, i + radius + 1)] - sums[Math.max(0, i - radius)] > 0 ? 1 : 0;
|
|
629
|
+
}
|
|
630
|
+
return out;
|
|
631
|
+
};
|
|
632
|
+
const across = pass(cells, nz, nx, (line, i) => line * nx + i);
|
|
633
|
+
return pass(across, nx, nz, (line, i) => i * nx + line);
|
|
634
|
+
}
|
|
635
|
+
const shapeOf = (layers, bands, simplifyCells) => ({
|
|
636
|
+
tolerance: mm(layers.raster.cell * (1 + simplifyCells)),
|
|
637
|
+
bands: bands.map((b) => ({
|
|
638
|
+
y0: mm(layers.minY + b.from * layers.layer),
|
|
639
|
+
y1: mm(layers.minY + (b.to + 1) * layers.layer),
|
|
640
|
+
polygons: polygons(b.cells, layers.raster, simplifyCells),
|
|
641
|
+
})),
|
|
642
|
+
});
|
|
643
|
+
/**
|
|
644
|
+
* What the build occupies in plan at each height, for the usage checks: the footprint of everything
|
|
645
|
+
* it has in each 2 cm layer, runs of near-identical layers read as bands, each band traced to
|
|
646
|
+
* outlines. The fill is conservative, so an outline is never inside the build and at worst a cell
|
|
647
|
+
* (5 mm) outside it, plus the simplification; `tolerance` states that bound. A build too intricate
|
|
648
|
+
* for its budget is read ever more coarsely, bands merged and outlines simplified, and in the end as
|
|
649
|
+
* one box. Frames are the meshes' world frame, so call it on a built object in the artifact's frame.
|
|
650
|
+
* Null for an empty one.
|
|
651
|
+
*/
|
|
652
|
+
export function readUsageShape(meshes) {
|
|
653
|
+
const layers = rasterise(collect(meshes));
|
|
654
|
+
if (!layers)
|
|
655
|
+
return null;
|
|
656
|
+
const fits = (shape) => bytes(shape) <= SHAPE_MAX_BYTES;
|
|
657
|
+
let finest = null;
|
|
658
|
+
for (const level of LEVELS) {
|
|
659
|
+
const bands = band(layers, level.grow);
|
|
660
|
+
finest ??= bands;
|
|
661
|
+
if (bands.length > MAX_BANDS)
|
|
662
|
+
continue;
|
|
663
|
+
const shape = shapeOf(layers, bands, level.simplify);
|
|
664
|
+
if (fits(shape))
|
|
665
|
+
return shape;
|
|
666
|
+
}
|
|
667
|
+
// Too intricate to trace within the budget (foliage, a screen of slats): thicken it until nearby
|
|
668
|
+
// pieces merge into blobs, which only ever adds to the shape, and say so in the tolerance.
|
|
669
|
+
const loosest = LEVELS.at(-1);
|
|
670
|
+
for (const radius of [0, 2, 4, 8, 16, 32]) {
|
|
671
|
+
const base = band(layers, loosest.grow).map((b) => ({
|
|
672
|
+
...b,
|
|
673
|
+
cells: radius ? dilate(b.cells, layers.raster, radius) : b.cells,
|
|
674
|
+
}));
|
|
675
|
+
for (const target of [MAX_BANDS, 12, 6, 3, 1]) {
|
|
676
|
+
const bands = merged(base, target);
|
|
677
|
+
for (const simplifyCells of [loosest.simplify, loosest.simplify * 2, loosest.simplify * 4]) {
|
|
678
|
+
const shape = shapeOf(layers, bands, simplifyCells + radius);
|
|
679
|
+
if (fits(shape))
|
|
680
|
+
return shape;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const { raster } = layers;
|
|
685
|
+
return box({
|
|
686
|
+
min: [raster.minX, layers.minY, raster.minZ],
|
|
687
|
+
max: [
|
|
688
|
+
raster.minX + raster.nx * raster.cell,
|
|
689
|
+
layers.minY + layers.count * layers.layer,
|
|
690
|
+
raster.minZ + raster.nz * raster.cell,
|
|
691
|
+
],
|
|
692
|
+
});
|
|
693
|
+
}
|