castle-web-cli 0.4.203 → 0.4.205

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/dist/agent-failures.d.ts +3 -1
  2. package/dist/agent-failures.js +19 -0
  3. package/dist/agent-prompts.d.ts +1 -0
  4. package/dist/agent-prompts.js +20 -8
  5. package/dist/agent.d.ts +5 -2
  6. package/dist/agent.js +59 -125
  7. package/dist/castle-host/host.d.ts +17 -0
  8. package/dist/castle-host/host.js +551 -4
  9. package/dist/castleJson.d.ts +1 -0
  10. package/dist/devSessionStorage.js +65 -4
  11. package/dist/effortSupport.d.ts +7 -0
  12. package/dist/effortSupport.js +31 -0
  13. package/dist/init.js +13 -3
  14. package/dist/platformDoc.js +6 -1
  15. package/dist/routerFences.d.ts +22 -0
  16. package/dist/routerFences.js +139 -0
  17. package/dist/serve.js +35 -2
  18. package/dist/shell/assets/index-b_bebkRb.css +1 -0
  19. package/dist/shell/assets/{index-CxrtbzHf.js → index-hWQTC8q6.js} +5 -5
  20. package/dist/shell/index.html +2 -2
  21. package/dist/taskAccount.d.ts +13 -0
  22. package/dist/taskAccount.js +38 -0
  23. package/dist/vitePlugins.js +19 -1
  24. package/kits/base/CLAUDE.md +2 -0
  25. package/kits/base/castle.json +1 -1
  26. package/kits/base/sdk/README.md +131 -0
  27. package/kits/base/sdk/agent.d.ts +6 -0
  28. package/kits/base/sdk/agent.js +70 -0
  29. package/kits/base/sdk/castle.d.ts +4 -0
  30. package/kits/base/sdk/castle.js +2 -0
  31. package/kits/base/sdk/commands.d.ts +106 -0
  32. package/kits/base/sdk/commands.js +21 -0
  33. package/kits/base/sdk/decks.d.ts +7 -0
  34. package/kits/base/sdk/decks.js +31 -0
  35. package/kits/base/sdk/runtime.d.ts +4 -2
  36. package/kits/base/sdk/runtime.js +51 -40
  37. package/kits/base/sdk/server/storage.d.ts +44 -0
  38. package/kits/base/sdk/server/storage.js +54 -0
  39. package/kits/base/sdk/transport.d.ts +5 -1
  40. package/kits/base/sdk/transport.js +106 -23
  41. package/kits/multiplayer-2d/castle.json +4 -4
  42. package/kits/multiplayer-2d/code/client/poses.js +7 -0
  43. package/kits/multiplayer-2d/code/server/limits.js +3 -0
  44. package/kits/multiplayer-2d/code/server/ownership.js +36 -8
  45. package/kits/multiplayer-2d/code/systems/multiplayer.js +23 -2
  46. package/kits/multiplayer-3d/CLAUDE.md +12 -1
  47. package/kits/multiplayer-3d/castle.json +4 -4
  48. package/kits/multiplayer-3d/code/client/avatars.js +7 -1
  49. package/kits/multiplayer-3d/code/client/poses.js +61 -1
  50. package/kits/multiplayer-3d/code/client/steer.js +383 -0
  51. package/kits/multiplayer-3d/code/server/limits.js +3 -0
  52. package/kits/multiplayer-3d/code/server/ownership.js +114 -18
  53. package/kits/multiplayer-3d/code/server/world.js +12 -4
  54. package/kits/multiplayer-3d/code/systems/multiplayer.js +315 -34
  55. package/kits/physics-3d/CLAUDE.md +65 -7
  56. package/kits/physics-3d/castle.json +3 -3
  57. package/kits/physics-3d/docs/pxmodel-format.md +916 -15
  58. package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
  59. package/kits/physics-3d/engine3d/artobjects.js +218 -0
  60. package/kits/physics-3d/engine3d/blob.js +1058 -0
  61. package/kits/physics-3d/engine3d/branches.js +300 -0
  62. package/kits/physics-3d/engine3d/cage.js +496 -0
  63. package/kits/physics-3d/engine3d/cageops.js +516 -0
  64. package/kits/physics-3d/engine3d/frame.js +669 -0
  65. package/kits/physics-3d/engine3d/look.js +675 -0
  66. package/kits/physics-3d/engine3d/materialpresets.js +486 -0
  67. package/kits/physics-3d/engine3d/modelEditorWorld.js +3 -2
  68. package/kits/physics-3d/engine3d/modelrefs.js +120 -0
  69. package/kits/physics-3d/engine3d/motion.js +560 -0
  70. package/kits/physics-3d/engine3d/objtext.js +37 -0
  71. package/kits/physics-3d/engine3d/optimize.js +1083 -0
  72. package/kits/physics-3d/engine3d/paint.js +674 -0
  73. package/kits/physics-3d/engine3d/partgeometry.js +467 -0
  74. package/kits/physics-3d/engine3d/paths.js +324 -0
  75. package/kits/physics-3d/engine3d/pxmodel.js +429 -26
  76. package/kits/physics-3d/engine3d/scatter.js +102 -0
  77. package/kits/physics-3d/engine3d/sculpt.js +1148 -0
  78. package/kits/physics-3d/engine3d/surfacedetail.js +267 -0
  79. package/kits/physics-3d/engine3d/sweep.js +480 -0
  80. package/kits/physics-3d/engine3d/thumbnails.js +6 -4
  81. package/kits/physics-3d/engine3d/world3d.js +119 -17
  82. package/kits/physics-3d/eslint.config.js +13 -0
  83. package/kits/physics-3d/models/parts/axe.pxmodel +40 -0
  84. package/kits/physics-3d/models/parts/backpack.pxmodel +47 -0
  85. package/kits/physics-3d/models/parts/barrel.pxmodel +22 -0
  86. package/kits/physics-3d/models/parts/bedroll.pxmodel +28 -0
  87. package/kits/physics-3d/models/parts/belt-pouch.pxmodel +22 -0
  88. package/kits/physics-3d/models/parts/bolt.pxmodel +10 -0
  89. package/kits/physics-3d/models/parts/bracer.pxmodel +26 -0
  90. package/kits/physics-3d/models/parts/chain-span.pxmodel +12 -0
  91. package/kits/physics-3d/models/parts/chimney.pxmodel +11 -0
  92. package/kits/physics-3d/models/parts/crate.pxmodel +44 -0
  93. package/kits/physics-3d/models/parts/crystal.pxmodel +22 -0
  94. package/kits/physics-3d/models/parts/door-arched.pxmodel +12 -0
  95. package/kits/physics-3d/models/parts/engine-block.pxmodel +39 -0
  96. package/kits/physics-3d/models/parts/exhaust-pipe.pxmodel +30 -0
  97. package/kits/physics-3d/models/parts/fence-span.pxmodel +10 -0
  98. package/kits/physics-3d/models/parts/fender.pxmodel +19 -0
  99. package/kits/physics-3d/models/parts/headlight.pxmodel +24 -0
  100. package/kits/physics-3d/models/parts/hood.pxmodel +17 -0
  101. package/kits/physics-3d/models/parts/horned-helmet.pxmodel +37 -0
  102. package/kits/physics-3d/models/parts/jerry-can.pxmodel +26 -0
  103. package/kits/physics-3d/models/parts/ladder.pxmodel +10 -0
  104. package/kits/physics-3d/models/parts/lamp.pxmodel +20 -0
  105. package/kits/physics-3d/models/parts/lantern.pxmodel +30 -0
  106. package/kits/physics-3d/models/parts/leaf-cluster.pxmodel +21 -0
  107. package/kits/physics-3d/models/parts/offroad-tire.pxmodel +48 -0
  108. package/kits/physics-3d/models/parts/pauldron.pxmodel +26 -0
  109. package/kits/physics-3d/models/parts/rivet.pxmodel +8 -0
  110. package/kits/physics-3d/models/parts/rock.pxmodel +21 -0
  111. package/kits/physics-3d/models/parts/roll-bar-light.pxmodel +26 -0
  112. package/kits/physics-3d/models/parts/round-shield.pxmodel +31 -0
  113. package/kits/physics-3d/models/parts/saddlebag.pxmodel +30 -0
  114. package/kits/physics-3d/models/parts/spoked-wheel.pxmodel +39 -0
  115. package/kits/physics-3d/models/parts/staff.pxmodel +33 -0
  116. package/kits/physics-3d/models/parts/stepping-stones.pxmodel +8 -0
  117. package/kits/physics-3d/models/parts/stone-base.pxmodel +10 -0
  118. package/kits/physics-3d/models/parts/sword.pxmodel +33 -0
  119. package/kits/physics-3d/models/parts/window-lit.pxmodel +13 -0
  120. package/kits/physics-3d/models/parts/wizard-hat.pxmodel +28 -0
  121. package/kits/physics-3d/models/templates/biped-stout.pxmodel +59 -0
  122. package/kits/physics-3d/models/templates/biped-tall.pxmodel +63 -0
  123. package/kits/physics-3d/models/templates/quadruped.pxmodel +52 -0
  124. package/kits/physics-3d/scripts/render/filmstrip.js +217 -0
  125. package/kits/physics-3d/scripts/render/index.html +20 -0
  126. package/kits/physics-3d/scripts/render/sheet.js +411 -0
  127. package/kits/physics-3d/scripts/render.mjs +646 -0
  128. package/kits/real-time/castle.json +2 -2
  129. package/kits/real-time/code/client/ownership.js +14 -4
  130. package/kits/real-time/code/client/smooth.js +2 -2
  131. package/kits/real-time/code/server/session.js +20 -4
  132. package/package.json +2 -2
  133. package/dist/shell/assets/index-CHMcsJrW.css +0 -1
@@ -0,0 +1,516 @@
1
+ // Box modeling on a cage: start from a box cut into segments, then extrude,
2
+ // inset, move and scale faces picked by the way they face and where they are.
3
+ // Also picks the edges that `crease` keeps sharp through subdivision.
4
+ //
5
+ // A cage is { vertices, faces } as in cage.js. Ops run in order, before the
6
+ // cage is mirrored and smoothed. An op whose selection finds no face does
7
+ // nothing.
8
+
9
+ // Points closer than this to x = 0 are on the mirror seam of a symmetric cage.
10
+ export const SEAM = 1e-4;
11
+
12
+ // A hard crease stays sharp for this many subdivision steps, more than a part
13
+ // is ever given.
14
+ export const HARD = 10;
15
+
16
+ const AXES = {
17
+ '+x': [1, 0, 0],
18
+ '-x': [-1, 0, 0],
19
+ '+y': [0, 1, 0],
20
+ '-y': [0, -1, 0],
21
+ '+z': [0, 0, 1],
22
+ '-z': [0, 0, -1],
23
+ };
24
+
25
+ // A face is picked only if its normal is within 60 degrees of `facing`; with
26
+ // `all`, within 45 degrees.
27
+ const PICK_ALIGN = 0.5;
28
+ const ALL_ALIGN = 0.7;
29
+
30
+ export function edgeKey(a, b) {
31
+ return a < b ? `${a},${b}` : `${b},${a}`;
32
+ }
33
+
34
+ //
35
+ // Box
36
+ //
37
+
38
+ // A box of `box: [w, h, d]` centered on the origin, cut into `segments` along
39
+ // each axis. With `half` only the x >= 0 half is made, open at x = 0, for a
40
+ // symmetric part; `segments[0]` then counts across that half.
41
+ export function boxCage(from, half) {
42
+ const size = vector3(from.box, [1, 1, 1]).map((value) => Math.abs(value) || 1);
43
+ const segments = [0, 1, 2].map((axis) => clampInt(from.segments?.[axis], 1, 16, 1));
44
+ const low = size.map((value) => -value / 2);
45
+ const span = [...size];
46
+ if (half) {
47
+ low[0] = 0;
48
+ span[0] = size[0] / 2;
49
+ }
50
+
51
+ // Grid points on the surface, shared by the faces that meet at them.
52
+ const vertices = [];
53
+ const indexOf = new Map();
54
+ const point = (grid) => {
55
+ const key = grid.join(',');
56
+ if (!indexOf.has(key)) {
57
+ vertices.push(grid.map((step, axis) => low[axis] + (span[axis] * step) / segments[axis]));
58
+ indexOf.set(key, vertices.length - 1);
59
+ }
60
+ return indexOf.get(key);
61
+ };
62
+
63
+ // Each side is a grid of quads across the other two axes. Going around
64
+ // (u, v) -> (u + 1, v) -> (u + 1, v + 1) faces +axis, so the low side is
65
+ // reversed.
66
+ const faces = [];
67
+ for (let axis = 0; axis < 3; axis++) {
68
+ const [b, c] = [(axis + 1) % 3, (axis + 2) % 3];
69
+ for (const side of [0, segments[axis]]) {
70
+ if (half && axis === 0 && side === 0) {
71
+ continue;
72
+ }
73
+ for (let u = 0; u < segments[b]; u++) {
74
+ for (let v = 0; v < segments[c]; v++) {
75
+ const corner = (du, dv) => {
76
+ const grid = [0, 0, 0];
77
+ grid[axis] = side;
78
+ grid[b] = u + du;
79
+ grid[c] = v + dv;
80
+ return point(grid);
81
+ };
82
+ const face = [corner(0, 0), corner(1, 0), corner(1, 1), corner(0, 1)];
83
+ faces.push(side === 0 ? face.reverse() : face);
84
+ }
85
+ }
86
+ }
87
+ }
88
+ return { vertices, faces };
89
+ }
90
+
91
+ //
92
+ // Selection
93
+ //
94
+
95
+ // `select` is { facing, near, all } or a list of them, and picks faces:
96
+ // - `facing: "+z"` (or a direction [x, y, z]) alone: the faces furthest that
97
+ // way, all of them if several are level, as on a box cut into segments.
98
+ // - with `near: [x, y, z]`: the face facing that way nearest the point; a list
99
+ // of points picks one face for each.
100
+ // - `all: true`: every face facing that way.
101
+ // Faces lying on the seam of a symmetric cage are never picked.
102
+ export function selectFaces(cage, select, symmetric) {
103
+ const selectors = Array.isArray(select) ? select : [select];
104
+ const chosen = new Set();
105
+ for (const selector of selectors) {
106
+ if (selector && typeof selector === 'object') {
107
+ pickFaces(cage, selector, symmetric).forEach((index) => chosen.add(index));
108
+ }
109
+ }
110
+ return [...chosen].sort((a, b) => a - b);
111
+ }
112
+
113
+ function pickFaces(cage, selector, symmetric) {
114
+ const direction = facingVector(selector.facing);
115
+ const candidates = [];
116
+ cage.faces.forEach((face, index) => {
117
+ if (symmetric && face.every((vertex) => Math.abs(cage.vertices[vertex][0]) < SEAM)) {
118
+ return;
119
+ }
120
+ const normal = faceNormal(cage.vertices, face);
121
+ const align = direction ? dot(normal, direction) : 1;
122
+ if (align > (selector.all ? ALL_ALIGN : PICK_ALIGN)) {
123
+ candidates.push({ index, align, center: faceCenter(cage.vertices, face) });
124
+ }
125
+ });
126
+ if (selector.all) {
127
+ return candidates.map((candidate) => candidate.index);
128
+ }
129
+
130
+ const points = nearPoints(selector.near);
131
+ if (points.length === 0) {
132
+ return furthest(candidates, direction);
133
+ }
134
+
135
+ // Ties between equally near faces go to the one that faces the right way
136
+ // more squarely.
137
+ return points
138
+ .map((point) => bestOf(candidates, (c) => distance(c.center, point) - 1e-3 * c.align))
139
+ .filter(Boolean)
140
+ .map((candidate) => candidate.index);
141
+ }
142
+
143
+ function furthest(candidates, direction) {
144
+ if (!direction || candidates.length === 0) {
145
+ return [];
146
+ }
147
+ const reach = Math.max(...candidates.map((c) => dot(c.center, direction)));
148
+ return candidates.filter((c) => dot(c.center, direction) > reach - 1e-3).map((c) => c.index);
149
+ }
150
+
151
+ function facingVector(facing) {
152
+ if (typeof facing === 'string') {
153
+ return AXES[facing] ?? null;
154
+ }
155
+ if (Array.isArray(facing) && facing.slice(0, 3).every(Number.isFinite)) {
156
+ const length = Math.hypot(facing[0], facing[1], facing[2]);
157
+ return length > 0 ? facing.slice(0, 3).map((value) => value / length) : null;
158
+ }
159
+ return null;
160
+ }
161
+
162
+ function nearPoints(near) {
163
+ if (!Array.isArray(near)) {
164
+ return [];
165
+ }
166
+ if (near.slice(0, 3).every(Number.isFinite) && near.length >= 3) {
167
+ return [near];
168
+ }
169
+ return near.filter((point) => Array.isArray(point) && point.slice(0, 3).every(Number.isFinite));
170
+ }
171
+
172
+ function bestOf(list, cost) {
173
+ let best = null;
174
+ let bestCost = Infinity;
175
+ for (const item of list) {
176
+ const value = cost(item);
177
+ if (value < bestCost) {
178
+ best = item;
179
+ bestCost = value;
180
+ }
181
+ }
182
+ return best;
183
+ }
184
+
185
+ //
186
+ // Ops
187
+ //
188
+
189
+ const OPS = { extrude, inset, move, scale };
190
+
191
+ // Runs `ops` in order on a copy of the cage.
192
+ export function applyOps(cage, ops, symmetric) {
193
+ let current = {
194
+ vertices: cage.vertices.map((v) => [...v]),
195
+ faces: cage.faces.map((f) => [...f]),
196
+ };
197
+ if (!Array.isArray(ops)) {
198
+ return current;
199
+ }
200
+ for (const op of ops) {
201
+ const run = op && typeof op === 'object' ? OPS[op.op] : null;
202
+ if (!run) {
203
+ continue;
204
+ }
205
+ const faces = selectFaces(current, op.select, symmetric);
206
+ if (faces.length > 0) {
207
+ current = run(current, faces, op, symmetric);
208
+ }
209
+ }
210
+ return current;
211
+ }
212
+
213
+ // Faces that share an edge are extruded together as one region, with walls
214
+ // only around its outside. Each step moves the end faces out along the
215
+ // region's normal by `distance`, shrinks them about their middle by `scale`,
216
+ // turns them about their middle by `turn` degrees, and shifts them by `move`.
217
+ // The next step goes out along the turned face, so a few steps with a turn
218
+ // make a curl: a tail, a horn, a mug handle.
219
+ function extrude(cage, faces, op, symmetric) {
220
+ const shape = {
221
+ distance: finite(op.distance, 0.2),
222
+ scale: scaleVector(op.scale),
223
+ turn: vector3(op.turn, [0, 0, 0]),
224
+ move: vector3(op.move, [0, 0, 0]),
225
+ };
226
+ const steps = clampInt(op.steps, 1, 32, 1);
227
+ for (let step = 0; step < steps; step++) {
228
+ cage = extrudeOnce(cage, regions(cage, faces, false, symmetric), shape, symmetric);
229
+ }
230
+ return cage;
231
+ }
232
+
233
+ // An extrusion of each face on its own that by default stays in place and
234
+ // shrinks: a smaller face with a ring of quads around it.
235
+ function inset(cage, faces, op, symmetric) {
236
+ const shape = {
237
+ distance: finite(op.distance, 0),
238
+ scale: scaleVector(op.scale ?? 0.6),
239
+ turn: [0, 0, 0],
240
+ move: vector3(op.move, [0, 0, 0]),
241
+ };
242
+ return extrudeOnce(cage, regions(cage, faces, true, symmetric), shape, symmetric);
243
+ }
244
+
245
+ function move(cage, faces, op, symmetric) {
246
+ const by = vector3(op.by, [0, 0, 0]);
247
+ return transformRegions(cage, regions(cage, faces, false, symmetric), symmetric, (point) =>
248
+ point.map((value, axis) => value + by[axis])
249
+ );
250
+ }
251
+
252
+ // Scales each region about its middle.
253
+ function scale(cage, faces, op, symmetric) {
254
+ const by = scaleVector(op.by);
255
+ return transformRegions(
256
+ cage,
257
+ regions(cage, faces, false, symmetric),
258
+ symmetric,
259
+ (point, center) => point.map((value, axis) => center[axis] + by[axis] * (value - center[axis]))
260
+ );
261
+ }
262
+
263
+ // The selected faces keep their places in the face list, now made of the new
264
+ // vertices, so the same indices select the end faces for the next step. The
265
+ // walls are added at the end.
266
+ function extrudeOnce(cage, regionList, shape, symmetric) {
267
+ const vertices = cage.vertices.map((v) => [...v]);
268
+ const faces = cage.faces.map((f) => [...f]);
269
+ for (const region of regionList) {
270
+ const normal = region.normal;
271
+ const copy = new Map();
272
+ for (const index of region.vertices) {
273
+ const point = cage.vertices[index];
274
+ const scaled = point.map((value, axis) => shape.scale[axis] * (value - region.center[axis]));
275
+ const moved = turnVector(scaled, shape.turn).map(
276
+ (value, axis) =>
277
+ region.center[axis] + value + normal[axis] * shape.distance + shape.move[axis]
278
+ );
279
+ vertices.push(keepSeam(point, moved, symmetric));
280
+ copy.set(index, vertices.length - 1);
281
+ }
282
+
283
+ // A wall on every edge used by only one face of the region, wound the
284
+ // same way as that face so it faces out.
285
+ const uses = edgeUses(region.faces.map((index) => cage.faces[index]));
286
+ for (const faceIndex of region.faces) {
287
+ eachEdge(cage.faces[faceIndex], (a, b) => {
288
+ if (uses.get(edgeKey(a, b)) === 1) {
289
+ faces.push([a, b, copy.get(b), copy.get(a)]);
290
+ }
291
+ });
292
+ faces[faceIndex] = cage.faces[faceIndex].map((index) => copy.get(index));
293
+ }
294
+ }
295
+ return { vertices, faces };
296
+ }
297
+
298
+ function transformRegions(cage, regionList, symmetric, transform) {
299
+ const vertices = cage.vertices.map((v) => [...v]);
300
+ for (const region of regionList) {
301
+ for (const index of region.vertices) {
302
+ const point = cage.vertices[index];
303
+ vertices[index] = keepSeam(point, transform(point, region.center), symmetric);
304
+ }
305
+ }
306
+ return { vertices, faces: cage.faces.map((f) => [...f]) };
307
+ }
308
+
309
+ // A point on the seam of a symmetric cage stays on it.
310
+ function keepSeam(before, after, symmetric) {
311
+ if (symmetric && Math.abs(before[0]) < SEAM) {
312
+ return [0, after[1], after[2]];
313
+ }
314
+ return after;
315
+ }
316
+
317
+ // Groups the faces into regions joined by shared edges, or one region per face
318
+ // when `separate`. Each region has its faces, its vertices, their middle and
319
+ // the region's normal. On a symmetric cage a region that touches the seam is
320
+ // half of a region centered on x = 0, so its middle and normal are moved there.
321
+ function regions(cage, faceIndices, separate, symmetric) {
322
+ const groups = separate ? faceIndices.map((index) => [index]) : joinByEdges(cage, faceIndices);
323
+ return groups.map((group) => {
324
+ const vertexSet = [...new Set(group.flatMap((index) => cage.faces[index]))];
325
+ const center = average(vertexSet.map((index) => cage.vertices[index]));
326
+ let normal = regionNormal(
327
+ cage.vertices,
328
+ group.map((index) => cage.faces[index])
329
+ );
330
+ if (symmetric && vertexSet.some((index) => Math.abs(cage.vertices[index][0]) < SEAM)) {
331
+ center[0] = 0;
332
+ normal = normalize([0, normal[1], normal[2]]);
333
+ }
334
+ return { faces: group, vertices: vertexSet, center, normal };
335
+ });
336
+ }
337
+
338
+ function joinByEdges(cage, faceIndices) {
339
+ const parent = new Map(faceIndices.map((index) => [index, index]));
340
+ const root = (index) => {
341
+ while (parent.get(index) !== index) {
342
+ index = parent.get(index);
343
+ }
344
+ return index;
345
+ };
346
+ const owner = new Map();
347
+ for (const faceIndex of faceIndices) {
348
+ eachEdge(cage.faces[faceIndex], (a, b) => {
349
+ const key = edgeKey(a, b);
350
+ if (owner.has(key)) {
351
+ parent.set(root(faceIndex), root(owner.get(key)));
352
+ } else {
353
+ owner.set(key, faceIndex);
354
+ }
355
+ });
356
+ }
357
+ const groups = new Map();
358
+ for (const faceIndex of faceIndices) {
359
+ const key = root(faceIndex);
360
+ if (!groups.has(key)) {
361
+ groups.set(key, []);
362
+ }
363
+ groups.get(key).push(faceIndex);
364
+ }
365
+ return [...groups.values()];
366
+ }
367
+
368
+ //
369
+ // Creases
370
+ //
371
+
372
+ // `crease` is { select, sharpness } or a list of them. The outline of each
373
+ // selection -- the edges with a selected face on only one side -- is creased.
374
+ // A crease with `sharpness: n` stays sharp for n subdivision steps and then
375
+ // rounds off; without it the crease is hard. Returns edge key -> sharpness.
376
+ export function creaseEdges(cage, crease, symmetric) {
377
+ const creases = new Map();
378
+ const entries = Array.isArray(crease) ? crease : [crease];
379
+ for (const entry of entries) {
380
+ if (!entry || typeof entry !== 'object') {
381
+ continue;
382
+ }
383
+ const sharpness = finite(entry.sharpness, HARD);
384
+ const faces = selectFaces(cage, entry.select, symmetric).map((index) => cage.faces[index]);
385
+ for (const key of outline(cage, faces, symmetric)) {
386
+ if (sharpness > 0) {
387
+ creases.set(key, Math.max(creases.get(key) ?? 0, sharpness));
388
+ }
389
+ }
390
+ }
391
+ return creases;
392
+ }
393
+
394
+ // An edge on the seam joins the two mirrored halves of the selection, so it is
395
+ // inside the outline.
396
+ function outline(cage, faces, symmetric) {
397
+ const onSeam = (index) => Math.abs(cage.vertices[index][0]) < SEAM;
398
+ const keys = [];
399
+ for (const [key, count] of edgeUses(faces)) {
400
+ const ends = key.split(',').map(Number);
401
+ if (count === 1 && !(symmetric && ends.every(onSeam))) {
402
+ keys.push(key);
403
+ }
404
+ }
405
+ return keys;
406
+ }
407
+
408
+ // How many of the faces use each edge.
409
+ function edgeUses(faces) {
410
+ const uses = new Map();
411
+ for (const face of faces) {
412
+ eachEdge(face, (a, b) => {
413
+ const key = edgeKey(a, b);
414
+ uses.set(key, (uses.get(key) ?? 0) + 1);
415
+ });
416
+ }
417
+ return uses;
418
+ }
419
+
420
+ //
421
+ // Vectors
422
+ //
423
+
424
+ export function faceNormal(vertices, face) {
425
+ return normalize(newell(vertices, face));
426
+ }
427
+
428
+ // Newell's method: the sum of this over a polygon is its normal scaled by twice
429
+ // its area, and it works for faces that are not quite flat.
430
+ function newell(vertices, face) {
431
+ const sum = [0, 0, 0];
432
+ face.forEach((index, i) => {
433
+ const a = vertices[index];
434
+ const b = vertices[face[(i + 1) % face.length]];
435
+ sum[0] += (a[1] - b[1]) * (a[2] + b[2]);
436
+ sum[1] += (a[2] - b[2]) * (a[0] + b[0]);
437
+ sum[2] += (a[0] - b[0]) * (a[1] + b[1]);
438
+ });
439
+ return sum;
440
+ }
441
+
442
+ // Larger faces count for more.
443
+ function regionNormal(vertices, faces) {
444
+ const sum = [0, 0, 0];
445
+ for (const face of faces) {
446
+ newell(vertices, face).forEach((value, axis) => (sum[axis] += value));
447
+ }
448
+ return normalize(sum);
449
+ }
450
+
451
+ // Turns v by angles in degrees about x, then y, then z.
452
+ function turnVector(v, degrees) {
453
+ let [x, y, z] = v;
454
+ const [ax, ay, az] = degrees.map((value) => (value * Math.PI) / 180);
455
+ [y, z] = [y * Math.cos(ax) - z * Math.sin(ax), y * Math.sin(ax) + z * Math.cos(ax)];
456
+ [x, z] = [x * Math.cos(ay) + z * Math.sin(ay), -x * Math.sin(ay) + z * Math.cos(ay)];
457
+ [x, y] = [x * Math.cos(az) - y * Math.sin(az), x * Math.sin(az) + y * Math.cos(az)];
458
+ return [x, y, z];
459
+ }
460
+
461
+ function faceCenter(vertices, face) {
462
+ return average(face.map((index) => vertices[index]));
463
+ }
464
+
465
+ function eachEdge(face, visit) {
466
+ face.forEach((a, i) => visit(a, face[(i + 1) % face.length]));
467
+ }
468
+
469
+ function average(points) {
470
+ const sum = [0, 0, 0];
471
+ for (const point of points) {
472
+ sum[0] += point[0];
473
+ sum[1] += point[1];
474
+ sum[2] += point[2];
475
+ }
476
+ return sum.map((value) => value / Math.max(1, points.length));
477
+ }
478
+
479
+ function normalize(v) {
480
+ const length = Math.hypot(v[0], v[1], v[2]);
481
+ return length > 0 ? v.map((value) => value / length) : [0, 0, 0];
482
+ }
483
+
484
+ function dot(a, b) {
485
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
486
+ }
487
+
488
+ function distance(a, b) {
489
+ return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
490
+ }
491
+
492
+ function finite(value, fallback) {
493
+ return Number.isFinite(value) ? value : fallback;
494
+ }
495
+
496
+ function vector3(value, fallback) {
497
+ if (!Array.isArray(value)) {
498
+ return [...fallback];
499
+ }
500
+ return [0, 1, 2].map((axis) => finite(value[axis], fallback[axis]));
501
+ }
502
+
503
+ // A number scales evenly; [x, y, z] scales along the model's axes.
504
+ function scaleVector(value) {
505
+ if (Number.isFinite(value)) {
506
+ return [value, value, value];
507
+ }
508
+ return vector3(value, [1, 1, 1]);
509
+ }
510
+
511
+ function clampInt(value, min, max, fallback) {
512
+ if (!Number.isFinite(value)) {
513
+ return fallback;
514
+ }
515
+ return Math.max(min, Math.min(max, Math.round(value)));
516
+ }