@pascal-app/geometry-script 1.0.3 → 1.0.5
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/__fixtures__/ceiling-lamp.d.ts +2 -0
- package/dist/__fixtures__/ceiling-lamp.d.ts.map +1 -0
- package/dist/__fixtures__/ceiling-lamp.js +37 -0
- package/dist/__fixtures__/fitted-cabinet.d.ts +7 -0
- package/dist/__fixtures__/fitted-cabinet.d.ts.map +1 -0
- package/dist/__fixtures__/fitted-cabinet.js +6 -0
- package/dist/__fixtures__/sliding-door.d.ts +6 -0
- package/dist/__fixtures__/sliding-door.d.ts.map +1 -0
- package/dist/__fixtures__/sliding-door.js +8 -0
- package/dist/__fixtures__/wall-screen.d.ts +2 -0
- package/dist/__fixtures__/wall-screen.d.ts.map +1 -0
- package/dist/__fixtures__/wall-screen.js +1 -0
- package/dist/author-scene.d.ts +47 -0
- package/dist/author-scene.d.ts.map +1 -0
- package/dist/author-scene.js +96 -0
- package/dist/compile.d.ts +18 -4
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +146 -17
- package/dist/coverage.d.ts +22 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +301 -0
- package/dist/cut.d.ts +24 -0
- package/dist/cut.d.ts.map +1 -0
- package/dist/cut.js +202 -0
- package/dist/sandbox-worker.js +2 -2
- package/dist/sandbox.js +1 -1
- package/dist/transform.js +1 -1
- package/package.json +10 -5
- package/dist/compile.test.d.ts +0 -2
- package/dist/compile.test.d.ts.map +0 -1
- package/dist/compile.test.js +0 -199
package/dist/coverage.js
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
const CELL = 0.02;
|
|
3
|
+
const MAX_CELLS = 250_000;
|
|
4
|
+
/**
|
|
5
|
+
* Strips between the heights where a triangle or the region has a corner hold no corner, so the
|
|
6
|
+
* covered length along each strip's middle line is its exact mean. Beyond this many corners per
|
|
7
|
+
* cell row (a rotated mosaic), strips are a fixed quarter of a row instead.
|
|
8
|
+
*/
|
|
9
|
+
const MAX_CORNERS_PER_ROW = 8;
|
|
10
|
+
const LINES = 4;
|
|
11
|
+
/** Patches nearer than this (about a board's width) are one place for the report. */
|
|
12
|
+
const NEAR = 0.3;
|
|
13
|
+
const MAX_PATCHES = 4;
|
|
14
|
+
const EPS = 1e-6;
|
|
15
|
+
function gridOver(area) {
|
|
16
|
+
let [u0, v0, u1, v1] = [Infinity, Infinity, -Infinity, -Infinity];
|
|
17
|
+
for (const { outer } of area) {
|
|
18
|
+
for (const [u, v] of outer) {
|
|
19
|
+
u0 = Math.min(u0, u);
|
|
20
|
+
v0 = Math.min(v0, v);
|
|
21
|
+
u1 = Math.max(u1, u);
|
|
22
|
+
v1 = Math.max(v1, v);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const [w, h] = [u1 - u0, v1 - v0];
|
|
26
|
+
if (!(w > EPS && h > EPS))
|
|
27
|
+
return null;
|
|
28
|
+
const cell = Math.max(CELL, Math.sqrt((w * h) / MAX_CELLS));
|
|
29
|
+
return { u0, v0, u1, v1, cell, nu: Math.ceil(w / cell - EPS), nv: Math.ceil(h / cell - EPS) };
|
|
30
|
+
}
|
|
31
|
+
/** The build's triangles seen square-on to the plane, (u, v) × 3 each; those edge-on are left out. */
|
|
32
|
+
function projectedTriangles(meshes, axis) {
|
|
33
|
+
const out = [];
|
|
34
|
+
const point = new THREE.Vector3();
|
|
35
|
+
const corner = [0, 0, 0, 0, 0, 0];
|
|
36
|
+
for (const mesh of meshes) {
|
|
37
|
+
const position = mesh.geometry.getAttribute('position');
|
|
38
|
+
const index = mesh.geometry.getIndex();
|
|
39
|
+
const count = index ? index.count : position.count;
|
|
40
|
+
for (let t = 0; t + 2 < count; t += 3) {
|
|
41
|
+
for (let k = 0; k < 3; k++) {
|
|
42
|
+
point.fromBufferAttribute(position, index ? index.getX(t + k) : t + k);
|
|
43
|
+
point.applyMatrix4(mesh.matrixWorld);
|
|
44
|
+
corner[2 * k] = point.x;
|
|
45
|
+
corner[2 * k + 1] = axis === 'y' ? point.z : point.y;
|
|
46
|
+
}
|
|
47
|
+
const [ua, va, ub, vb, uc, vc] = corner;
|
|
48
|
+
if (Math.abs((ub - ua) * (vc - va) - (uc - ua) * (vb - va)) < 1e-12)
|
|
49
|
+
continue;
|
|
50
|
+
out.push(ua, va, ub, vb, uc, vc);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return Float64Array.from(out);
|
|
54
|
+
}
|
|
55
|
+
/** The strips the region is read in: cell rows cut at every corner's height, or in LINES. */
|
|
56
|
+
function stripsOf(grid, triangles, rings) {
|
|
57
|
+
const { v0, v1, cell, nv } = grid;
|
|
58
|
+
const inside = (v) => v > v0 && v < v1;
|
|
59
|
+
const corners = [];
|
|
60
|
+
for (let k = 1; k < triangles.length; k += 2)
|
|
61
|
+
if (inside(triangles[k]))
|
|
62
|
+
corners.push(triangles[k]);
|
|
63
|
+
for (const ring of rings)
|
|
64
|
+
for (const [, v] of ring)
|
|
65
|
+
if (inside(v))
|
|
66
|
+
corners.push(v);
|
|
67
|
+
const cuts = Array.from({ length: nv }, (_, j) => v0 + j * cell);
|
|
68
|
+
cuts.push(v1);
|
|
69
|
+
const sorted = Float64Array.from(corners).sort();
|
|
70
|
+
let distinct = 0;
|
|
71
|
+
for (let k = 0; k < sorted.length; k++)
|
|
72
|
+
if (!k || sorted[k] - sorted[k - 1] > EPS)
|
|
73
|
+
distinct++;
|
|
74
|
+
if (distinct <= nv * MAX_CORNERS_PER_ROW)
|
|
75
|
+
cuts.push(...sorted);
|
|
76
|
+
else
|
|
77
|
+
for (let j = 0; j < nv; j++)
|
|
78
|
+
for (let k = 1; k < LINES; k++)
|
|
79
|
+
cuts.push(v0 + (j + k / LINES) * cell);
|
|
80
|
+
const at = Float64Array.from(cuts).sort();
|
|
81
|
+
const mid = [];
|
|
82
|
+
const height = [];
|
|
83
|
+
let from = at[0];
|
|
84
|
+
for (let k = 1; k < at.length; k++) {
|
|
85
|
+
if (at[k] - from <= EPS)
|
|
86
|
+
continue;
|
|
87
|
+
mid.push((from + at[k]) / 2);
|
|
88
|
+
height.push(at[k] - from);
|
|
89
|
+
from = at[k];
|
|
90
|
+
}
|
|
91
|
+
return { mid: Float64Array.from(mid), height: Float64Array.from(height) };
|
|
92
|
+
}
|
|
93
|
+
/** What the triangles cover along each strip's middle line, as unsorted [from, to] pairs. */
|
|
94
|
+
function coveredSpans(triangles, { mid }) {
|
|
95
|
+
const spans = Array.from({ length: mid.length }, () => []);
|
|
96
|
+
for (let t = 0; t < triangles.length; t += 6) {
|
|
97
|
+
const u = [triangles[t], triangles[t + 2], triangles[t + 4]];
|
|
98
|
+
const v = [triangles[t + 1], triangles[t + 3], triangles[t + 5]];
|
|
99
|
+
const vMin = Math.min(v[0], v[1], v[2]) - EPS;
|
|
100
|
+
const vMax = Math.max(v[0], v[1], v[2]) + EPS;
|
|
101
|
+
let first = 0;
|
|
102
|
+
let last = mid.length;
|
|
103
|
+
while (first < last) {
|
|
104
|
+
const half = (first + last) >> 1;
|
|
105
|
+
if (mid[half] < vMin)
|
|
106
|
+
first = half + 1;
|
|
107
|
+
else
|
|
108
|
+
last = half;
|
|
109
|
+
}
|
|
110
|
+
for (let s = first; s < mid.length && mid[s] <= vMax; s++) {
|
|
111
|
+
const at = mid[s];
|
|
112
|
+
let lo = Infinity;
|
|
113
|
+
let hi = -Infinity;
|
|
114
|
+
for (let a = 0, b = 2; a < 3; b = a++) {
|
|
115
|
+
const [ua, va, ub, vb] = [u[a], v[a], u[b], v[b]];
|
|
116
|
+
if (at < Math.min(va, vb) - EPS || at > Math.max(va, vb) + EPS)
|
|
117
|
+
continue;
|
|
118
|
+
if (Math.abs(vb - va) < EPS) {
|
|
119
|
+
lo = Math.min(lo, ua, ub);
|
|
120
|
+
hi = Math.max(hi, ua, ub);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const cross = ua + ((at - va) / (vb - va)) * (ub - ua);
|
|
124
|
+
lo = Math.min(lo, cross);
|
|
125
|
+
hi = Math.max(hi, cross);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (lo <= hi)
|
|
129
|
+
spans[s].push(lo - EPS, hi + EPS);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return spans;
|
|
133
|
+
}
|
|
134
|
+
/** The region along line `v`, as sorted [from, to] pairs, even–odd over every ring. */
|
|
135
|
+
function regionSpans(rings, v) {
|
|
136
|
+
const crossings = [];
|
|
137
|
+
for (const ring of rings) {
|
|
138
|
+
for (let i = 0, k = ring.length - 1; i < ring.length; k = i++) {
|
|
139
|
+
const [ua, va] = ring[k];
|
|
140
|
+
const [ub, vb] = ring[i];
|
|
141
|
+
if (va > v !== vb > v)
|
|
142
|
+
crossings.push(ua + ((v - va) * (ub - ua)) / (vb - va));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return crossings.sort((a, b) => a - b);
|
|
146
|
+
}
|
|
147
|
+
/** Adds the area [a, b) × `height` lays on each cell of row `j` to `bare`. */
|
|
148
|
+
function addToCells(bare, grid, j, a, b, height) {
|
|
149
|
+
const first = Math.max(0, Math.floor((a - grid.u0) / grid.cell));
|
|
150
|
+
const last = Math.min(grid.nu - 1, Math.floor((b - grid.u0) / grid.cell));
|
|
151
|
+
for (let i = first; i <= last; i++) {
|
|
152
|
+
const length = Math.min(b, grid.u0 + (i + 1) * grid.cell) - Math.max(a, grid.u0 + i * grid.cell);
|
|
153
|
+
if (length > 0)
|
|
154
|
+
bare[j * grid.nu + i] += length * height;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** The cells of `mask` that belong to a 2 × 2 block of them: a gap one cell wide is not a patch. */
|
|
158
|
+
function withoutThin(mask, { nu, nv }) {
|
|
159
|
+
const block = new Uint8Array(nu * nv);
|
|
160
|
+
for (let j = 0; j + 1 < nv; j++) {
|
|
161
|
+
for (let i = 0; i + 1 < nu; i++) {
|
|
162
|
+
const c = j * nu + i;
|
|
163
|
+
block[c] = mask[c] & mask[c + 1] & mask[c + nu] & mask[c + nu + 1];
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const open = new Uint8Array(nu * nv);
|
|
167
|
+
for (let j = 0; j < nv; j++) {
|
|
168
|
+
for (let i = 0; i < nu; i++) {
|
|
169
|
+
const c = j * nu + i;
|
|
170
|
+
open[c] =
|
|
171
|
+
block[c] |
|
|
172
|
+
(i > 0 ? block[c - 1] : 0) |
|
|
173
|
+
(j > 0 ? block[c - nu] : 0) |
|
|
174
|
+
(i > 0 && j > 0 ? block[c - nu - 1] : 0);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return open;
|
|
178
|
+
}
|
|
179
|
+
/** `mask` grown by `r` cells each way (a square), through running sums along rows then columns. */
|
|
180
|
+
function grown(mask, { nu, nv }, r) {
|
|
181
|
+
const rows = new Uint8Array(nu * nv);
|
|
182
|
+
const sums = new Int32Array(Math.max(nu, nv) + 1);
|
|
183
|
+
for (let j = 0; j < nv; j++) {
|
|
184
|
+
for (let i = 0; i < nu; i++)
|
|
185
|
+
sums[i + 1] = sums[i] + mask[j * nu + i];
|
|
186
|
+
for (let i = 0; i < nu; i++)
|
|
187
|
+
rows[j * nu + i] = sums[Math.min(nu, i + r + 1)] > sums[Math.max(0, i - r)] ? 1 : 0;
|
|
188
|
+
}
|
|
189
|
+
const out = new Uint8Array(nu * nv);
|
|
190
|
+
for (let i = 0; i < nu; i++) {
|
|
191
|
+
for (let j = 0; j < nv; j++)
|
|
192
|
+
sums[j + 1] = sums[j] + rows[j * nu + i];
|
|
193
|
+
for (let j = 0; j < nv; j++)
|
|
194
|
+
out[j * nu + i] = sums[Math.min(nv, j + r + 1)] > sums[Math.max(0, j - r)] ? 1 : 0;
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
/** Bounds of the patches, gathered by the places they grow into, largest first. */
|
|
199
|
+
function patchesOf(patches, grid) {
|
|
200
|
+
const { nu, nv, u0, v0, u1, v1, cell } = grid;
|
|
201
|
+
const places = grown(patches, grid, Math.ceil(NEAR / 2 / cell));
|
|
202
|
+
const label = new Int32Array(nu * nv).fill(-1);
|
|
203
|
+
const queue = new Int32Array(nu * nv);
|
|
204
|
+
const found = [];
|
|
205
|
+
for (let start = 0; start < places.length; start++) {
|
|
206
|
+
if (!places[start] || label[start] !== -1)
|
|
207
|
+
continue;
|
|
208
|
+
const id = found.length;
|
|
209
|
+
const place = { cells: 0, i0: nu, j0: nv, i1: -1, j1: -1 };
|
|
210
|
+
found.push(place);
|
|
211
|
+
let head = 0;
|
|
212
|
+
let tail = 0;
|
|
213
|
+
queue[tail++] = start;
|
|
214
|
+
label[start] = id;
|
|
215
|
+
while (head < tail) {
|
|
216
|
+
const c = queue[head++];
|
|
217
|
+
const i = c % nu;
|
|
218
|
+
const j = (c - i) / nu;
|
|
219
|
+
if (patches[c]) {
|
|
220
|
+
place.cells++;
|
|
221
|
+
place.i0 = Math.min(place.i0, i);
|
|
222
|
+
place.j0 = Math.min(place.j0, j);
|
|
223
|
+
place.i1 = Math.max(place.i1, i);
|
|
224
|
+
place.j1 = Math.max(place.j1, j);
|
|
225
|
+
}
|
|
226
|
+
for (const n of [i > 0 ? c - 1 : -1, i < nu - 1 ? c + 1 : -1, c - nu, c + nu]) {
|
|
227
|
+
if (n >= 0 && n < places.length && places[n] && label[n] === -1) {
|
|
228
|
+
label[n] = id;
|
|
229
|
+
queue[tail++] = n;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const mm = (value) => Math.round(value * 1000) / 1000;
|
|
235
|
+
return found
|
|
236
|
+
.sort((a, b) => b.cells - a.cells)
|
|
237
|
+
.slice(0, MAX_PATCHES)
|
|
238
|
+
.map(({ i0, j0, i1, j1 }) => ({
|
|
239
|
+
min: [mm(u0 + i0 * cell), mm(v0 + j0 * cell)],
|
|
240
|
+
max: [mm(Math.min(u1, u0 + (i1 + 1) * cell)), mm(Math.min(v1, v0 + (j1 + 1) * cell))],
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* How much of the region the build covers, seen square-on to its plane (a floor from above, a
|
|
245
|
+
* face from the room). `coverage` is the covered share of the region's area, two decimals,
|
|
246
|
+
* joints included: read along strips between the heights of every corner, exact for boards and
|
|
247
|
+
* tiles laid square. On a grid of 2 cm cells (coarser on a region too big for 250k of them), a
|
|
248
|
+
* cell more than half bare is bare; `uncovered` bounds the largest bare patches (two cells across
|
|
249
|
+
* at least, so no joint is one; patches nearer than NEAR are one place), largest first. Null
|
|
250
|
+
* when the region has no area.
|
|
251
|
+
*/
|
|
252
|
+
export function measureCoverage(meshes, region) {
|
|
253
|
+
const grid = gridOver(region.area);
|
|
254
|
+
if (!grid)
|
|
255
|
+
return null;
|
|
256
|
+
const rings = region.area.flatMap(({ outer, holes }) => [outer, ...holes]);
|
|
257
|
+
const triangles = projectedTriangles(meshes, region.axis);
|
|
258
|
+
const strips = stripsOf(grid, triangles, rings);
|
|
259
|
+
const spans = coveredSpans(triangles, strips);
|
|
260
|
+
const bareArea = new Float64Array(grid.nu * grid.nv);
|
|
261
|
+
let inside = 0;
|
|
262
|
+
let bare = 0;
|
|
263
|
+
for (let s = 0; s < strips.mid.length; s++) {
|
|
264
|
+
const [at, height] = [strips.mid[s], strips.height[s]];
|
|
265
|
+
const row = Math.min(grid.nv - 1, Math.floor((at - grid.v0) / grid.cell));
|
|
266
|
+
const exposed = (from, to) => {
|
|
267
|
+
bare += (to - from) * height;
|
|
268
|
+
addToCells(bareArea, grid, row, from, to, height);
|
|
269
|
+
};
|
|
270
|
+
const area = regionSpans(rings, at);
|
|
271
|
+
const pairs = spans[s];
|
|
272
|
+
const order = Array.from({ length: pairs.length / 2 }, (_, p) => p).sort((p, q) => pairs[2 * p] - pairs[2 * q]);
|
|
273
|
+
for (let r = 0; r + 1 < area.length; r += 2) {
|
|
274
|
+
const [from, to] = [area[r], area[r + 1]];
|
|
275
|
+
inside += (to - from) * height;
|
|
276
|
+
let cursor = from;
|
|
277
|
+
for (const p of order) {
|
|
278
|
+
const [lo, hi] = [pairs[2 * p], pairs[2 * p + 1]];
|
|
279
|
+
if (hi <= cursor)
|
|
280
|
+
continue;
|
|
281
|
+
if (lo >= to)
|
|
282
|
+
break;
|
|
283
|
+
if (lo > cursor)
|
|
284
|
+
exposed(cursor, lo);
|
|
285
|
+
cursor = hi;
|
|
286
|
+
if (cursor >= to)
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
if (cursor < to)
|
|
290
|
+
exposed(cursor, to);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (inside <= EPS * EPS)
|
|
294
|
+
return null;
|
|
295
|
+
const half = (grid.cell * grid.cell) / 2;
|
|
296
|
+
const bareCells = bareArea.map((area) => (area > half ? 1 : 0));
|
|
297
|
+
return {
|
|
298
|
+
coverage: Math.round((1 - bare / inside) * 100) / 100,
|
|
299
|
+
uncovered: patchesOf(withoutThin(Uint8Array.from(bareCells), grid), grid),
|
|
300
|
+
};
|
|
301
|
+
}
|
package/dist/cut.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type MultiPolygon } from '@pascal-app/core/polygon-boolean';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
/**
|
|
4
|
+
* The finish a host's build is cut to, as an area in a plane and the axis its prism runs along: a
|
|
5
|
+
* room's floor in plan (u = x, v = z, the prism along y) or a wall face (u = x, v = y, the prism
|
|
6
|
+
* along z), each without its holes.
|
|
7
|
+
*/
|
|
8
|
+
export type HostRegion = {
|
|
9
|
+
axis: 'y' | 'z';
|
|
10
|
+
area: MultiPolygon;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The region a build for these inputs is cut to: a floor's `polygon` less its `holes`, or a wall
|
|
14
|
+
* face's `outline` less its `holes` (rectangles); null when the inputs describe neither.
|
|
15
|
+
*/
|
|
16
|
+
export declare function hostRegion(inputs: Record<string, unknown>): HostRegion | null;
|
|
17
|
+
/**
|
|
18
|
+
* Cuts each visible mesh of a host's build to its region: whole inside, it is kept untouched;
|
|
19
|
+
* whole outside or in a hole, it is dropped; across the boundary, it is intersected with the
|
|
20
|
+
* region's prism (a closed mesh gains the cut faces, an open one is trimmed), keeping its
|
|
21
|
+
* material, and dropped if nothing is left. `extent` is the build's visible bounds.
|
|
22
|
+
*/
|
|
23
|
+
export declare function cutToRegion(meshes: THREE.Mesh[], region: HostRegion, extent: THREE.Box3): void;
|
|
24
|
+
//# sourceMappingURL=cut.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cut.d.ts","sourceRoot":"","sources":["../src/cut.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,YAAY,EAAa,MAAM,kCAAkC,CAAA;AAC3F,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,IAAI,EAAE,YAAY,CAAA;CAAE,CAAA;AAsBhE;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,GAAG,IAAI,CAU7E;AAiID;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,QA0CvF"}
|
package/dist/cut.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { difference } from '@pascal-app/core/polygon-boolean';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { Brush, Evaluator, HOLLOW_INTERSECTION, INTERSECTION } from 'three-bvh-csg';
|
|
4
|
+
const isPoint = (point) => Array.isArray(point) && Number.isFinite(point[0]) && Number.isFinite(point[1]);
|
|
5
|
+
const isRing = (ring) => Array.isArray(ring) && ring.length >= 3 && ring.every(isPoint);
|
|
6
|
+
const isRect = (rect) => rect !== null &&
|
|
7
|
+
typeof rect === 'object' &&
|
|
8
|
+
['x0', 'y0', 'x1', 'y1'].every((key) => Number.isFinite(rect[key]));
|
|
9
|
+
const rectRing = ({ x0, y0, x1, y1 }) => [
|
|
10
|
+
[x0, y0],
|
|
11
|
+
[x1, y0],
|
|
12
|
+
[x1, y1],
|
|
13
|
+
[x0, y1],
|
|
14
|
+
];
|
|
15
|
+
const pieces = (rings) => rings.map((outer) => ({ outer, holes: [] }));
|
|
16
|
+
/**
|
|
17
|
+
* The region a build for these inputs is cut to: a floor's `polygon` less its `holes`, or a wall
|
|
18
|
+
* face's `outline` less its `holes` (rectangles); null when the inputs describe neither.
|
|
19
|
+
*/
|
|
20
|
+
export function hostRegion(inputs) {
|
|
21
|
+
const holes = Array.isArray(inputs.holes) ? inputs.holes : [];
|
|
22
|
+
if (isRing(inputs.polygon)) {
|
|
23
|
+
return { axis: 'y', area: difference(inputs.polygon, pieces(holes.filter(isRing))) };
|
|
24
|
+
}
|
|
25
|
+
if (isRect(inputs.outline)) {
|
|
26
|
+
const rings = holes.filter(isRect).map(rectRing);
|
|
27
|
+
return { axis: 'z', area: difference(rectRing(inputs.outline), pieces(rings)) };
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
/** A mesh this close inside the boundary, or touching it, is inside: no cut for a plank laid to the edge. */
|
|
32
|
+
const EDGE = 1e-5;
|
|
33
|
+
/** The prism outreaches the build along its axis, so no face of the build lies on its ends. */
|
|
34
|
+
const MARGIN = 1;
|
|
35
|
+
function planeSpan(box, axis) {
|
|
36
|
+
return axis === 'y'
|
|
37
|
+
? { u0: box.min.x, u1: box.max.x, v0: box.min.z, v1: box.max.z }
|
|
38
|
+
: { u0: box.min.x, u1: box.max.x, v0: box.min.y, v1: box.max.y };
|
|
39
|
+
}
|
|
40
|
+
/** Whether the segment a→b passes through the closed rectangle (Liang–Barsky). */
|
|
41
|
+
function segmentMeetsSpan(a, b, span) {
|
|
42
|
+
let t0 = 0;
|
|
43
|
+
let t1 = 1;
|
|
44
|
+
const du = b[0] - a[0];
|
|
45
|
+
const dv = b[1] - a[1];
|
|
46
|
+
for (const [p, q] of [
|
|
47
|
+
[-du, a[0] - span.u0],
|
|
48
|
+
[du, span.u1 - a[0]],
|
|
49
|
+
[-dv, a[1] - span.v0],
|
|
50
|
+
[dv, span.v1 - a[1]],
|
|
51
|
+
]) {
|
|
52
|
+
if (p === 0) {
|
|
53
|
+
if (q < 0)
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
else if (p < 0) {
|
|
57
|
+
t0 = Math.max(t0, q / p);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
t1 = Math.min(t1, q / p);
|
|
61
|
+
}
|
|
62
|
+
if (t0 > t1)
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
/** Even–odd over every ring: the region's pieces are disjoint and their holes lie inside them. */
|
|
68
|
+
function regionHolds(area, [u, v]) {
|
|
69
|
+
let inside = false;
|
|
70
|
+
for (const { outer, holes } of area) {
|
|
71
|
+
for (const ring of [outer, ...holes]) {
|
|
72
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
73
|
+
const [ui, vi] = ring[i];
|
|
74
|
+
const [uj, vj] = ring[j];
|
|
75
|
+
if (vi > v !== vj > v && u < ((uj - ui) * (v - vi)) / (vj - vi) + ui)
|
|
76
|
+
inside = !inside;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return inside;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Where a mesh whose bounds span `span` stands against the region. No boundary through its bounds
|
|
84
|
+
* (shrunk by EDGE) means it is wholly in or wholly out, which its centre tells; else it is cut.
|
|
85
|
+
*/
|
|
86
|
+
function placement(area, span) {
|
|
87
|
+
const shrink = (lo, hi) => {
|
|
88
|
+
const by = Math.min(EDGE, (hi - lo) / 2);
|
|
89
|
+
return [lo + by, hi - by];
|
|
90
|
+
};
|
|
91
|
+
const [u0, u1] = shrink(span.u0, span.u1);
|
|
92
|
+
const [v0, v1] = shrink(span.v0, span.v1);
|
|
93
|
+
const inner = { u0, u1, v0, v1 };
|
|
94
|
+
for (const { outer, holes } of area) {
|
|
95
|
+
for (const ring of [outer, ...holes]) {
|
|
96
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
97
|
+
if (segmentMeetsSpan(ring[j], ring[i], inner))
|
|
98
|
+
return 'boundary';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return regionHolds(area, [(u0 + u1) / 2, (v0 + v1) / 2]) ? 'inside' : 'outside';
|
|
103
|
+
}
|
|
104
|
+
/** The region as a closed prism spanning `extent` (plus MARGIN) along its axis, in the build's frame. */
|
|
105
|
+
function regionPrism({ axis, area }, extent) {
|
|
106
|
+
// A floor's plan v is world z; the shape is drawn at -z so turning it up (-90° about x) lands it at +z.
|
|
107
|
+
const flip = axis === 'y' ? -1 : 1;
|
|
108
|
+
const shapes = area.map(({ outer, holes }) => {
|
|
109
|
+
const shape = new THREE.Shape(outer.map(([u, v]) => new THREE.Vector2(u, flip * v)));
|
|
110
|
+
shape.holes = holes.map((hole) => new THREE.Path(hole.map(([u, v]) => new THREE.Vector2(u, flip * v))));
|
|
111
|
+
return shape;
|
|
112
|
+
});
|
|
113
|
+
const from = (axis === 'y' ? extent.min.y : extent.min.z) - MARGIN;
|
|
114
|
+
const to = (axis === 'y' ? extent.max.y : extent.max.z) + MARGIN;
|
|
115
|
+
const prism = new THREE.ExtrudeGeometry(shapes, { depth: to - from, bevelEnabled: false });
|
|
116
|
+
prism.clearGroups();
|
|
117
|
+
if (axis === 'y')
|
|
118
|
+
prism.rotateX(-Math.PI / 2).translate(0, from, 0);
|
|
119
|
+
else
|
|
120
|
+
prism.translate(0, 0, from);
|
|
121
|
+
return prism;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The prism as a brush carrying every attribute the mesh has, so the cut faces it adds keep the
|
|
125
|
+
* mesh's layout. Attributes the prism has no value for are filled: white for `color`, so the
|
|
126
|
+
* material's own colour shows on the cut, zero for the rest.
|
|
127
|
+
*/
|
|
128
|
+
function prismBrush(prism, geometry, brushes) {
|
|
129
|
+
const names = Object.keys(geometry.attributes).sort();
|
|
130
|
+
const key = names.map((name) => `${name}:${geometry.getAttribute(name).itemSize}`).join(',');
|
|
131
|
+
const cached = brushes.get(key);
|
|
132
|
+
if (cached)
|
|
133
|
+
return cached;
|
|
134
|
+
const shaped = prism.clone();
|
|
135
|
+
for (const name of Object.keys(shaped.attributes)) {
|
|
136
|
+
if (!names.includes(name))
|
|
137
|
+
shaped.deleteAttribute(name);
|
|
138
|
+
}
|
|
139
|
+
const count = shaped.getAttribute('position').count;
|
|
140
|
+
for (const name of names) {
|
|
141
|
+
if (shaped.getAttribute(name))
|
|
142
|
+
continue;
|
|
143
|
+
const { itemSize } = geometry.getAttribute(name);
|
|
144
|
+
const values = new Float32Array(count * itemSize).fill(name === 'color' ? 1 : 0);
|
|
145
|
+
shaped.setAttribute(name, new THREE.BufferAttribute(values, itemSize));
|
|
146
|
+
}
|
|
147
|
+
const brush = new Brush(shaped);
|
|
148
|
+
brushes.set(key, brush);
|
|
149
|
+
return brush;
|
|
150
|
+
}
|
|
151
|
+
const triangles = (geometry) => Math.floor((geometry.index?.count ?? geometry.getAttribute('position')?.count ?? 0) / 3);
|
|
152
|
+
/**
|
|
153
|
+
* Cuts each visible mesh of a host's build to its region: whole inside, it is kept untouched;
|
|
154
|
+
* whole outside or in a hole, it is dropped; across the boundary, it is intersected with the
|
|
155
|
+
* region's prism (a closed mesh gains the cut faces, an open one is trimmed), keeping its
|
|
156
|
+
* material, and dropped if nothing is left. `extent` is the build's visible bounds.
|
|
157
|
+
*/
|
|
158
|
+
export function cutToRegion(meshes, region, extent) {
|
|
159
|
+
const box = new THREE.Box3();
|
|
160
|
+
let prism;
|
|
161
|
+
const brushes = new Map();
|
|
162
|
+
const evaluator = new Evaluator();
|
|
163
|
+
for (const mesh of meshes) {
|
|
164
|
+
mesh.geometry.computeBoundingBox();
|
|
165
|
+
box.copy(mesh.geometry.boundingBox).applyMatrix4(mesh.matrixWorld);
|
|
166
|
+
const where = placement(region.area, planeSpan(box, region.axis));
|
|
167
|
+
if (where === 'inside')
|
|
168
|
+
continue;
|
|
169
|
+
if (where === 'outside') {
|
|
170
|
+
mesh.removeFromParent();
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
prism ??= regionPrism(region, extent);
|
|
174
|
+
const target = prismBrush(prism, mesh.geometry, brushes);
|
|
175
|
+
const materials = Array.isArray(mesh.material) ? mesh.material : [mesh.material];
|
|
176
|
+
target.material = materials[0];
|
|
177
|
+
const source = new Brush(mesh.geometry.clone(), mesh.material);
|
|
178
|
+
source.matrixWorld.copy(mesh.matrixWorld);
|
|
179
|
+
source.prepareGeometry();
|
|
180
|
+
// `prepareGeometry` maps its edges; its types leave the map out.
|
|
181
|
+
const { halfEdges } = source.geometry;
|
|
182
|
+
const closed = halfEdges.isFullyConnected();
|
|
183
|
+
evaluator.attributes = Object.keys(mesh.geometry.attributes);
|
|
184
|
+
const result = evaluator.evaluate(source, target, closed ? INTERSECTION : HOLLOW_INTERSECTION);
|
|
185
|
+
source.geometry.dispose();
|
|
186
|
+
if (triangles(result.geometry) === 0) {
|
|
187
|
+
mesh.removeFromParent();
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const kept = result.material;
|
|
191
|
+
mesh.geometry = result.geometry;
|
|
192
|
+
if (kept.length === 1) {
|
|
193
|
+
mesh.geometry.clearGroups();
|
|
194
|
+
mesh.material = kept[0];
|
|
195
|
+
}
|
|
196
|
+
else
|
|
197
|
+
mesh.material = kept;
|
|
198
|
+
}
|
|
199
|
+
prism?.dispose();
|
|
200
|
+
for (const brush of brushes.values())
|
|
201
|
+
brush.geometry.dispose();
|
|
202
|
+
}
|
package/dist/sandbox-worker.js
CHANGED
|
@@ -40,9 +40,9 @@ for (const name of BLOCKED) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
self.addEventListener('message', async (event) => {
|
|
43
|
-
const { id, code, params } = event.data;
|
|
43
|
+
const { id, code, params, inputs } = event.data;
|
|
44
44
|
try {
|
|
45
|
-
const output = await compileGeometryScript({ code, params });
|
|
45
|
+
const output = await compileGeometryScript({ code, params, inputs });
|
|
46
46
|
post({ id, ok: true, output }, [output.glb]);
|
|
47
47
|
}
|
|
48
48
|
catch (error) {
|