3draft 26.11.5001 → 26.11.11032
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/APP.md +136 -28
- package/README.md +6 -1
- package/dist/camera.js +3 -2
- package/dist/csg.d.ts +2 -0
- package/dist/csg.js +13 -1
- package/dist/gl.d.ts +8 -0
- package/dist/gl.js +517 -0
- package/dist/gpu.d.ts +40 -2
- package/dist/gpu.js +374 -18
- package/dist/index.d.ts +15 -3
- package/dist/index.js +10 -3
- package/dist/instances.d.ts +19 -0
- package/dist/instances.js +41 -0
- package/dist/kernel.js +6 -5
- package/dist/lathe.d.ts +13 -0
- package/dist/lathe.js +87 -0
- package/dist/material.d.ts +2 -0
- package/dist/pick.js +3 -2
- package/dist/saw.d.ts +29 -0
- package/dist/saw.js +121 -0
- package/dist/svg.js +1 -1
- package/dist/text.d.ts +25 -0
- package/dist/text.js +225 -0
- package/dist/triangulate.d.ts +4 -0
- package/dist/triangulate.js +72 -0
- package/dist/types.d.ts +4 -0
- package/dist/wire.d.ts +13 -0
- package/dist/wire.js +111 -9
- package/package.json +1 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Triangles of one polygon face. A convex polygon (and anything with 3 or 4 well-behaved corners) is a plain fan from its first
|
|
2
|
+
// corner; a concave one (an L, a V, a star: an outline that was drawn or filled by hand) is cut by ear clipping, so no triangle ever
|
|
3
|
+
// lies outside the outline. The triangles keep the winding of the polygon.
|
|
4
|
+
import { faceNormal } from './mesh.js';
|
|
5
|
+
const fan = (n) => Array.from({ length: Math.max(0, n - 2) }, (_, j) => [0, j + 1, j + 2]);
|
|
6
|
+
/** Indices into `pts`, three per triangle, `pts.length - 2` triangles. */
|
|
7
|
+
export const triangulate = (pts) => {
|
|
8
|
+
const n = pts.length;
|
|
9
|
+
if (n <= 3)
|
|
10
|
+
return fan(n);
|
|
11
|
+
const nrm = faceNormal(pts);
|
|
12
|
+
const big = Math.max(Math.abs(nrm[0]), Math.abs(nrm[1]), Math.abs(nrm[2]));
|
|
13
|
+
if (big < 1e-12)
|
|
14
|
+
return fan(n); // no plane to speak of: nothing better than the fan
|
|
15
|
+
// the plane is dropped to 2D along the normal's biggest axis; `sign` keeps the polygon counter-clockwise there
|
|
16
|
+
const axis = big === Math.abs(nrm[0]) ? 0 : big === Math.abs(nrm[1]) ? 1 : 2;
|
|
17
|
+
const [iu, iv] = axis === 0 ? [1, 2] : axis === 1 ? [2, 0] : [0, 1];
|
|
18
|
+
const sign = nrm[axis] >= 0 ? 1 : -1;
|
|
19
|
+
const P = pts.map((p) => [p[iu] * sign, p[iv]]);
|
|
20
|
+
const turn = (a, b, c) => (P[b][0] - P[a][0]) * (P[c][1] - P[a][1]) - (P[b][1] - P[a][1]) * (P[c][0] - P[a][0]);
|
|
21
|
+
let scale = 0;
|
|
22
|
+
for (const p of P)
|
|
23
|
+
scale = Math.max(scale, Math.abs(p[0]), Math.abs(p[1]));
|
|
24
|
+
const eps = 1e-12 * scale * scale;
|
|
25
|
+
let convex = true;
|
|
26
|
+
for (let i = 0; i < n && convex; i++)
|
|
27
|
+
if (turn(i, (i + 1) % n, (i + 2) % n) < -eps)
|
|
28
|
+
convex = false;
|
|
29
|
+
if (convex)
|
|
30
|
+
return fan(n);
|
|
31
|
+
const inside = (p, a, b, c) => {
|
|
32
|
+
// on or in the triangle (a, b, c), which is counter-clockwise; corners at the same place as a, b or c do not count
|
|
33
|
+
for (const q of [a, b, c])
|
|
34
|
+
if (Math.abs(P[p][0] - P[q][0]) <= 1e-12 * scale && Math.abs(P[p][1] - P[q][1]) <= 1e-12 * scale)
|
|
35
|
+
return false;
|
|
36
|
+
return turn(a, b, p) >= -eps && turn(b, c, p) >= -eps && turn(c, a, p) >= -eps;
|
|
37
|
+
};
|
|
38
|
+
const rest = pts.map((_, i) => i);
|
|
39
|
+
const out = [];
|
|
40
|
+
let guard = 0;
|
|
41
|
+
while (rest.length > 3 && guard++ < n * n) {
|
|
42
|
+
let clipped = false;
|
|
43
|
+
for (let k = 0; k < rest.length; k++) {
|
|
44
|
+
const a = rest[(k + rest.length - 1) % rest.length];
|
|
45
|
+
const b = rest[k];
|
|
46
|
+
const c = rest[(k + 1) % rest.length];
|
|
47
|
+
if (turn(a, b, c) <= eps)
|
|
48
|
+
continue; // a reflex (or flat) corner is not an ear
|
|
49
|
+
if (rest.some((p) => p !== a && p !== b && p !== c && inside(p, a, b, c)))
|
|
50
|
+
continue;
|
|
51
|
+
out.push([a, b, c]);
|
|
52
|
+
rest.splice(k, 1);
|
|
53
|
+
clipped = true;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (!clipped) {
|
|
57
|
+
// a self-touching or degenerate outline: take the flattest corner off so the loop always ends
|
|
58
|
+
let k = 0;
|
|
59
|
+
let best = -Infinity;
|
|
60
|
+
for (let i = 0; i < rest.length; i++) {
|
|
61
|
+
const t = turn(rest[(i + rest.length - 1) % rest.length], rest[i], rest[(i + 1) % rest.length]);
|
|
62
|
+
if (t > best)
|
|
63
|
+
((best = t), (k = i));
|
|
64
|
+
}
|
|
65
|
+
out.push([rest[(k + rest.length - 1) % rest.length], rest[k], rest[(k + 1) % rest.length]]);
|
|
66
|
+
rest.splice(k, 1);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (rest.length === 3)
|
|
70
|
+
out.push([rest[0], rest[1], rest[2]]);
|
|
71
|
+
return out;
|
|
72
|
+
};
|
package/dist/types.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export interface Fit {
|
|
|
25
25
|
radius: number;
|
|
26
26
|
/** Perspective only: distance from the eye to the center. A first-person eye sits almost on the center. */
|
|
27
27
|
dist?: number;
|
|
28
|
+
/** Perspective only: the field of view in radians (default 35°). A first-person eye inside a room wants a wider one. */
|
|
29
|
+
fov?: number;
|
|
28
30
|
}
|
|
29
31
|
export interface Camera {
|
|
30
32
|
name: string;
|
|
@@ -54,4 +56,6 @@ export interface Mark {
|
|
|
54
56
|
lines: Vec3[][];
|
|
55
57
|
color?: string;
|
|
56
58
|
width?: number;
|
|
59
|
+
/** 0..1, default 1. */
|
|
60
|
+
opacity?: number;
|
|
57
61
|
}
|
package/dist/wire.d.ts
CHANGED
|
@@ -55,3 +55,16 @@ export declare const wireFromSegments: (mesh: Mesh, wire: Wire, segments: [Vec3,
|
|
|
55
55
|
* so every face stays planar.
|
|
56
56
|
*/
|
|
57
57
|
export declare const moveVertexSet: (mesh: Mesh, moves: [number, Vec3][]) => Mesh;
|
|
58
|
+
/**
|
|
59
|
+
* Moves vertices like `moveVertexSet`, but a polygon stays one polygon whenever the move leaves it flat (and facing the
|
|
60
|
+
* same way): a whole face dragged, scaled or stretched, the side of an extrusion. Only a polygon that the move would bend
|
|
61
|
+
* (or flip, or collapse) becomes triangles. This is what the grab / scale / extrude of the editor use.
|
|
62
|
+
*/
|
|
63
|
+
export declare const reshape: (mesh: Mesh, moves: [number, Vec3][]) => Mesh;
|
|
64
|
+
/**
|
|
65
|
+
* The closed outline that loose lines make, as an ordered list of vertex ids, or null when they make none. `ids` are the points taken
|
|
66
|
+
* part in, `lines` the vertex pairs between them. The points are not ordered by angle round their middle (that only works for a convex
|
|
67
|
+
* shape, an L or a V comes out knotted): the lines are followed. The walk starts at the outermost point and at a junction (more than
|
|
68
|
+
* one way on) continues in the direction that turns least, the way Rustroke's fill follows the tangent. Winding is not decided here.
|
|
69
|
+
*/
|
|
70
|
+
export declare const outlineOf: (positions: Vec3[], lines: [number, number][], ids: number[]) => number[] | null;
|
package/dist/wire.js
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
// its ends are ordinary mesh vertices, so moving a vertex moves every line (and every face) that uses it.
|
|
4
4
|
// Faces stay convex and planar: a vertex placed inside a face splits it into triangles, a cut splits a convex
|
|
5
5
|
// polygon into two convex ones, and a moved vertex turns the polygons it touches into triangles.
|
|
6
|
-
import { sub, add, scale, dot, len } from './vec.js';
|
|
6
|
+
import { sub, add, scale, dot, len, cross, norm } from './vec.js';
|
|
7
7
|
import { faceNormal, bounds } from './mesh.js';
|
|
8
|
+
import { triangulate } from './triangulate.js';
|
|
8
9
|
const sizeOf = (mesh) => bounds(mesh).radius || 1;
|
|
9
10
|
/** Distance from `p` to the segment a-b, and where along it (0..1). */
|
|
10
11
|
const toSegment = (p, a, b) => {
|
|
@@ -87,8 +88,8 @@ export const moveVertex = (mesh, i, to) => {
|
|
|
87
88
|
if (f.v.length <= 3 || !f.v.includes(i))
|
|
88
89
|
faces.push(f);
|
|
89
90
|
else
|
|
90
|
-
for (
|
|
91
|
-
faces.push({ ...f, v: [f.v[
|
|
91
|
+
for (const [a, b, c] of triangulate(f.v.map((v) => mesh.positions[v])))
|
|
92
|
+
faces.push({ ...f, v: [f.v[a], f.v[b], f.v[c]] });
|
|
92
93
|
}
|
|
93
94
|
return withFaces(mesh, faces, positions);
|
|
94
95
|
};
|
|
@@ -103,8 +104,8 @@ export const fillLoop = (mesh, loop, face = {}, awayFrom = bounds(mesh).center)
|
|
|
103
104
|
const centre = scale(pts.reduce((s, p) => add(s, p), [0, 0, 0]), 1 / pts.length);
|
|
104
105
|
const ordered = dot(faceNormal(pts), sub(centre, awayFrom)) >= 0 ? loop : [...loop].reverse();
|
|
105
106
|
const out = [];
|
|
106
|
-
for (
|
|
107
|
-
out.push({ ...face, v: [ordered[
|
|
107
|
+
for (const [a, b, c] of triangulate(ordered.map((v) => mesh.positions[v])))
|
|
108
|
+
out.push({ ...face, v: [ordered[a], ordered[b], ordered[c]] });
|
|
108
109
|
return withFaces(mesh, [...mesh.faces, ...out]);
|
|
109
110
|
};
|
|
110
111
|
/** Adds the line a-b (not twice, not a point). */
|
|
@@ -177,8 +178,8 @@ export const nearestSurface = (mesh, p, tolerance) => {
|
|
|
177
178
|
let best = null;
|
|
178
179
|
mesh.faces.forEach((f, face) => {
|
|
179
180
|
const pts = f.v.map((v) => mesh.positions[v]);
|
|
180
|
-
for (
|
|
181
|
-
const q = closestOnTriangle(p, pts[
|
|
181
|
+
for (const [a, b, c] of triangulate(pts)) {
|
|
182
|
+
const q = closestOnTriangle(p, pts[a], pts[b], pts[c]);
|
|
182
183
|
const d = len(sub(q, p));
|
|
183
184
|
if (d <= tolerance && (!best || d < best.d))
|
|
184
185
|
best = { face, point: q, d };
|
|
@@ -237,8 +238,109 @@ export const moveVertexSet = (mesh, moves) => {
|
|
|
237
238
|
if (rigid)
|
|
238
239
|
faces.push(f);
|
|
239
240
|
else
|
|
240
|
-
for (
|
|
241
|
-
faces.push({ ...f, v: [f.v[
|
|
241
|
+
for (const [a, b, c] of triangulate(f.v.map((v) => mesh.positions[v])))
|
|
242
|
+
faces.push({ ...f, v: [f.v[a], f.v[b], f.v[c]] });
|
|
242
243
|
}
|
|
243
244
|
return withFaces(mesh, faces, positions);
|
|
244
245
|
};
|
|
246
|
+
/**
|
|
247
|
+
* Moves vertices like `moveVertexSet`, but a polygon stays one polygon whenever the move leaves it flat (and facing the
|
|
248
|
+
* same way): a whole face dragged, scaled or stretched, the side of an extrusion. Only a polygon that the move would bend
|
|
249
|
+
* (or flip, or collapse) becomes triangles. This is what the grab / scale / extrude of the editor use.
|
|
250
|
+
*/
|
|
251
|
+
export const reshape = (mesh, moves) => {
|
|
252
|
+
const to = new Map(moves);
|
|
253
|
+
const positions = mesh.positions.map((p, k) => to.get(k) ?? p);
|
|
254
|
+
const tol = 1e-6 * sizeOf(mesh);
|
|
255
|
+
const faces = [];
|
|
256
|
+
for (const f of mesh.faces) {
|
|
257
|
+
if (f.v.length <= 3 || !f.v.some((v) => to.has(v))) {
|
|
258
|
+
faces.push(f);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const before = faceNormal(f.v.map((v) => mesh.positions[v]));
|
|
262
|
+
const pts = f.v.map((v) => positions[v]);
|
|
263
|
+
const after = faceNormal(pts);
|
|
264
|
+
const l = len(after);
|
|
265
|
+
const c = pts.reduce((t, p) => add(t, scale(p, 1 / pts.length)), [0, 0, 0]);
|
|
266
|
+
const flat = l > 1e-12 && dot(after, before) > 0 && pts.every((p) => Math.abs(dot(sub(p, c), after)) / l <= tol);
|
|
267
|
+
if (flat)
|
|
268
|
+
faces.push(f);
|
|
269
|
+
else
|
|
270
|
+
for (const [a, b, c] of triangulate(f.v.map((v) => mesh.positions[v])))
|
|
271
|
+
faces.push({ ...f, v: [f.v[a], f.v[b], f.v[c]] });
|
|
272
|
+
}
|
|
273
|
+
return withFaces(mesh, faces, positions);
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* The closed outline that loose lines make, as an ordered list of vertex ids, or null when they make none. `ids` are the points taken
|
|
277
|
+
* part in, `lines` the vertex pairs between them. The points are not ordered by angle round their middle (that only works for a convex
|
|
278
|
+
* shape, an L or a V comes out knotted): the lines are followed. The walk starts at the outermost point and at a junction (more than
|
|
279
|
+
* one way on) continues in the direction that turns least, the way Rustroke's fill follows the tangent. Winding is not decided here.
|
|
280
|
+
*/
|
|
281
|
+
export const outlineOf = (positions, lines, ids) => {
|
|
282
|
+
if (ids.length < 3 || !lines.length)
|
|
283
|
+
return null;
|
|
284
|
+
const P = positions;
|
|
285
|
+
// the plane: the two farthest points, and the point farthest from the line between them
|
|
286
|
+
let [a, b] = [ids[0], ids[1]];
|
|
287
|
+
let far = -1;
|
|
288
|
+
for (const i of ids)
|
|
289
|
+
for (const j of ids)
|
|
290
|
+
if (i < j) {
|
|
291
|
+
const d = len(sub(P[i], P[j]));
|
|
292
|
+
if (d > far)
|
|
293
|
+
((far = d), (a = i), (b = j));
|
|
294
|
+
}
|
|
295
|
+
const u = norm(sub(P[b], P[a]));
|
|
296
|
+
let n = null;
|
|
297
|
+
let best = 1e-12;
|
|
298
|
+
for (const i of ids) {
|
|
299
|
+
const m = cross(u, sub(P[i], P[a]));
|
|
300
|
+
if (len(m) > best)
|
|
301
|
+
((best = len(m)), (n = norm(m)));
|
|
302
|
+
}
|
|
303
|
+
if (!n)
|
|
304
|
+
return null;
|
|
305
|
+
const w = cross(n, u);
|
|
306
|
+
const flat = (i) => [dot(sub(P[i], P[a]), u), dot(sub(P[i], P[a]), w)];
|
|
307
|
+
const adj = new Map();
|
|
308
|
+
const link = (p, q) => (adj.get(p) ?? adj.set(p, []).get(p)).push(q);
|
|
309
|
+
const seen = new Set();
|
|
310
|
+
for (const [p, q] of lines) {
|
|
311
|
+
const k = p < q ? `${p},${q}` : `${q},${p}`;
|
|
312
|
+
if (p === q || seen.has(k))
|
|
313
|
+
continue;
|
|
314
|
+
seen.add(k);
|
|
315
|
+
link(p, q);
|
|
316
|
+
link(q, p);
|
|
317
|
+
}
|
|
318
|
+
const starts = [...adj.keys()].filter((i) => adj.get(i).length >= 2);
|
|
319
|
+
if (starts.length < 3)
|
|
320
|
+
return null;
|
|
321
|
+
const first = starts.reduce((m, i) => (flat(i)[0] < flat(m)[0] ? i : m));
|
|
322
|
+
const loop = [first];
|
|
323
|
+
let prev = null;
|
|
324
|
+
let at = first;
|
|
325
|
+
for (let guard = 0; guard < ids.length * 4 + 8; guard++) {
|
|
326
|
+
const options = adj.get(at).filter((q) => q !== prev);
|
|
327
|
+
if (!options.length)
|
|
328
|
+
return null;
|
|
329
|
+
const here = flat(at);
|
|
330
|
+
const came = prev === null ? [0, -1] : [here[0] - flat(prev)[0], here[1] - flat(prev)[1]];
|
|
331
|
+
const turn = (q) => {
|
|
332
|
+
const t = flat(q);
|
|
333
|
+
const d = [t[0] - here[0], t[1] - here[1]];
|
|
334
|
+
return Math.abs(Math.atan2(came[0] * d[1] - came[1] * d[0], came[0] * d[0] + came[1] * d[1]));
|
|
335
|
+
};
|
|
336
|
+
const next = options.reduce((m, q) => (turn(q) < turn(m) ? q : m));
|
|
337
|
+
if (next === first)
|
|
338
|
+
return loop.length >= 3 ? loop : null;
|
|
339
|
+
if (loop.includes(next))
|
|
340
|
+
return null;
|
|
341
|
+
loop.push(next);
|
|
342
|
+
prev = at;
|
|
343
|
+
at = next;
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
};
|