@margoengine/rigging 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1112 @@
1
+ import { n as e, r as t, s as n } from "./rig-BawZ5OLb.js";
2
+ import { rotateByQuaternion as r } from "@margoengine/math/quat";
3
+ import { clampTurn as i, jointTurn as a, turnRotation as o } from "@margoengine/animator/ik";
4
+ //#region src/anatomy.ts
5
+ function s(e, t) {
6
+ if (e.length === 1 || t <= e[0][0]) return [e[0][1], e[0][2]];
7
+ let n = 1;
8
+ for (; n < e.length - 1 && t > e[n][0];) n++;
9
+ let r = e[n - 1], i = e[n], a = (t - r[0]) / (i[0] - r[0]);
10
+ return [r[1] + a * (i[1] - r[1]), r[2] + a * (i[2] - r[2])];
11
+ }
12
+ var c = 180 / Math.PI, l = Math.PI / 180, u = 1e-7, d = 5 * l, f = 20 * l, p = 60 * l, m = 30 * l, h = .25, g = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], _ = (e, t) => [
13
+ e[1] * t[2] - e[2] * t[1],
14
+ e[2] * t[0] - e[0] * t[2],
15
+ e[0] * t[1] - e[1] * t[0]
16
+ ], v = (e, t) => [
17
+ e[0] - t[0],
18
+ e[1] - t[1],
19
+ e[2] - t[2]
20
+ ], y = (e, t) => [
21
+ e[0] * t,
22
+ e[1] * t,
23
+ e[2] * t
24
+ ], b = (e, t = 1e-12) => {
25
+ let n = Math.sqrt(g(e, e));
26
+ return n > t ? y(e, 1 / n) : null;
27
+ }, x = (e, t) => {
28
+ let n = g(e, t);
29
+ return [
30
+ e[0] - t[0] * n,
31
+ e[1] - t[1] * n,
32
+ e[2] - t[2] * n
33
+ ];
34
+ }, S = (e, t) => [
35
+ e[3] * t[0] + e[0] * t[3] + e[1] * t[2] - e[2] * t[1],
36
+ e[3] * t[1] - e[0] * t[2] + e[1] * t[3] + e[2] * t[0],
37
+ e[3] * t[2] + e[0] * t[1] - e[1] * t[0] + e[2] * t[3],
38
+ e[3] * t[3] - e[0] * t[0] - e[1] * t[1] - e[2] * t[2]
39
+ ], C = (e) => [
40
+ -e[0],
41
+ -e[1],
42
+ -e[2],
43
+ e[3]
44
+ ], w = (e, t) => r(t, e), T = (e, t) => {
45
+ let n = Math.sin(t / 2);
46
+ return [
47
+ e[0] * n,
48
+ e[1] * n,
49
+ e[2] * n,
50
+ Math.cos(t / 2)
51
+ ];
52
+ }, ee = (e, t) => {
53
+ let n = g(e, t);
54
+ if (n > 1 - 1e-14) return [
55
+ 0,
56
+ 0,
57
+ 0,
58
+ 1
59
+ ];
60
+ if (n < -1 + 1e-10) {
61
+ let t = b(_(e, Math.abs(e[0]) < .9 ? [
62
+ 1,
63
+ 0,
64
+ 0
65
+ ] : [
66
+ 0,
67
+ 1,
68
+ 0
69
+ ]));
70
+ return [
71
+ t[0],
72
+ t[1],
73
+ t[2],
74
+ 0
75
+ ];
76
+ }
77
+ let r = _(e, t), i = 1 + n, a = Math.sqrt(g(r, r) + i * i);
78
+ return [
79
+ r[0] / a,
80
+ r[1] / a,
81
+ r[2] / a,
82
+ i / a
83
+ ];
84
+ }, te = (e, t, n) => {
85
+ let r = e[0] + t[1] + n[2], i;
86
+ if (r > 0) {
87
+ let a = .5 / Math.sqrt(r + 1);
88
+ i = [
89
+ (t[2] - n[1]) * a,
90
+ (n[0] - e[2]) * a,
91
+ (e[1] - t[0]) * a,
92
+ .25 / a
93
+ ];
94
+ } else if (e[0] > t[1] && e[0] > n[2]) {
95
+ let r = 2 * Math.sqrt(1 + e[0] - t[1] - n[2]);
96
+ i = [
97
+ .25 * r,
98
+ (t[0] + e[1]) / r,
99
+ (n[0] + e[2]) / r,
100
+ (t[2] - n[1]) / r
101
+ ];
102
+ } else if (t[1] > n[2]) {
103
+ let r = 2 * Math.sqrt(1 + t[1] - e[0] - n[2]);
104
+ i = [
105
+ (t[0] + e[1]) / r,
106
+ .25 * r,
107
+ (n[1] + t[2]) / r,
108
+ (n[0] - e[2]) / r
109
+ ];
110
+ } else {
111
+ let r = 2 * Math.sqrt(1 + n[2] - e[0] - t[1]);
112
+ i = [
113
+ (n[0] + e[2]) / r,
114
+ (n[1] + t[2]) / r,
115
+ .25 * r,
116
+ (e[1] - t[0]) / r
117
+ ];
118
+ }
119
+ let a = Math.hypot(i[0], i[1], i[2], i[3]);
120
+ return [
121
+ i[0] / a,
122
+ i[1] / a,
123
+ i[2] / a,
124
+ i[3] / a
125
+ ];
126
+ };
127
+ function E(e, t, n, r, i, a, o = !1, s = !1) {
128
+ let c = o ? -1 : 1, l = s ? -1 : 1, u = n[r] * c, d = n[r + 1] * c, f = n[r + 2] * c, p = n[r + 3], m = i[a] * l, h = i[a + 1] * l, g = i[a + 2] * l, _ = i[a + 3];
129
+ e[t] = p * m + u * _ + d * g - f * h, e[t + 1] = p * h - u * g + d * _ + f * m, e[t + 2] = p * g + u * h - d * m + f * _, e[t + 3] = p * _ - u * m - d * h - f * g;
130
+ }
131
+ function D(e, ...t) {
132
+ if (!(e >= 0 && e <= 180)) throw RangeError("A bound allowance must be 0 to 180 degrees");
133
+ let n = 0;
134
+ for (let e of t) {
135
+ if (!(Math.abs(e.value) <= 360)) throw RangeError(`Source value ${e.value} (${e.what}) must be within 360 degrees`);
136
+ n = Math.max(n, e.value);
137
+ }
138
+ return {
139
+ normal: t.length ? Math.min(180, Math.max(0, t[0].value)) : 0,
140
+ structural: Math.min(180, Math.ceil(n / 5 - 1e-9) * 5 + e),
141
+ allowance: e,
142
+ rows: t
143
+ };
144
+ }
145
+ function O(e, t) {
146
+ let n = (e) => [
147
+ -e[0] || 0,
148
+ e[1],
149
+ e[2]
150
+ ], r = (e) => [
151
+ e[0],
152
+ -e[1] || 0,
153
+ -e[2] || 0
154
+ ];
155
+ return {
156
+ ...e,
157
+ id: t(e.id),
158
+ parent: e.parent === null ? null : t(e.parent),
159
+ ...e.reference ? { reference: t(e.reference) } : {},
160
+ segment: t(e.segment),
161
+ side: e.side === "left" ? "right" : e.side === "right" ? "left" : "centre",
162
+ mirror: e.id,
163
+ long: n(e.long),
164
+ ...e.distal ? { distal: e.distal.map(t) } : {},
165
+ ...e.rhythm ? { rhythm: {
166
+ ...e.rhythm,
167
+ from: t(e.rhythm.from),
168
+ to: t(e.rhythm.to)
169
+ } } : {},
170
+ ...e.across ? { across: {
171
+ pairs: e.across.pairs.map(([e, n]) => [t(e), t(n)]),
172
+ neutral: n(e.across.neutral)
173
+ } } : {},
174
+ ...e.dofs ? { dofs: e.dofs.map((e) => ({
175
+ ...e,
176
+ axis: r(e.axis)
177
+ })) } : {}
178
+ };
179
+ }
180
+ function k(e) {
181
+ if (!e || typeof e.id != "string" || !Array.isArray(e.joints) || !e.joints.length) throw TypeError("A species table needs an id and a nonempty joints list, parents first");
182
+ let t = /* @__PURE__ */ new Map(), n = (e, t) => Math.abs(e - t) < 1e-6, r = (n, r, i) => {
183
+ if (!t.has(n) && !e.joints.some((e) => e.id === n)) throw RangeError(`${r}: ${i} "${n}" is not a joint of the ${e.id} table; use one of its joint ids`);
184
+ };
185
+ for (let r of e.joints) {
186
+ let i = `${e.id} joint ${r?.id}`;
187
+ if (!r || typeof r.id != "string" || !r.id) throw TypeError("Every joint needs a nonempty id");
188
+ if (t.has(r.id)) throw RangeError(`${i}: the id is used twice; give each its own id`);
189
+ if (r.parent !== null && !t.has(r.parent)) throw RangeError(`${i}: its parent "${r.parent}" must come before it in the table`);
190
+ if (r.parent === null && t.size) throw RangeError(`${i}: only the first joint may have no parent`);
191
+ if (!n(g(r.long, r.long), 1)) throw RangeError(`${i}: long must be a unit vector`);
192
+ if (r.kind !== "free" && !r.dofs) throw RangeError(`${i}: a ${r.kind} joint needs its three motions`);
193
+ if (r.dofs) {
194
+ let [t, a, o] = r.dofs;
195
+ for (let t of r.dofs) {
196
+ if (!n(g(t.axis, t.axis), 1)) throw RangeError(`${i}: the ${t.positive} axis must be a unit vector`);
197
+ for (let [r, a] of [[t.max, t.positive], [t.min, t.negative]]) {
198
+ if (!(r.normal >= 0 && r.structural >= r.normal - 1e-9 && r.structural <= 180)) throw RangeError(`${i}: ${a} needs 0 <= normal <= structural <= 180 degrees`);
199
+ for (let t of r.rows) {
200
+ if (!Object.hasOwn(e.sources, t.source)) throw RangeError(`${i}: ${a} names the source "${t.source}", which the table does not list; add it to the table's sources or use a listed one`);
201
+ if (![
202
+ "dataset",
203
+ "print",
204
+ "online",
205
+ "recalled"
206
+ ].includes(t.provenance)) throw RangeError(`${i}: ${a} has the unknown provenance "${String(t.provenance)}"; use dataset, print, online or recalled`);
207
+ if (t.provenance === "dataset" && !t.file) throw RangeError(`${i}: ${a} is a dataset row and must name its data file`);
208
+ }
209
+ if (!r.rule) {
210
+ let e = D(r.allowance, ...r.rows);
211
+ if (!n(e.normal, r.normal) || !n(e.structural, r.structural)) throw RangeError(`${i}: ${a} does not follow from its rows; build it with bound() or state its rule`);
212
+ }
213
+ }
214
+ if (t.gate && !(t.gate.to > t.gate.from && t.gate.floor >= 0)) throw RangeError(`${i}: the ${t.positive} gate needs from < to and a floor of 0 or more`);
215
+ }
216
+ if (!n(g(t.axis, r.long), 0) || !n(g(a.axis, r.long), 0) || !n(g(t.axis, a.axis), 0)) throw RangeError(`${i}: the two swing axes must be at right angles to long and to each other`);
217
+ if (!n(Math.abs(g(o.axis, r.long)), 1)) throw RangeError(`${i}: the twist axis must lie along long`);
218
+ }
219
+ t.set(r.id, r);
220
+ }
221
+ for (let t of e.joints) {
222
+ let n = `${e.id} joint ${t.id}`;
223
+ t.reference && r(t.reference, n, "reference"), t.mirror && r(t.mirror, n, "mirror");
224
+ for (let e of t.distal ?? []) r(e, n, "distal joint");
225
+ for (let e of t.across?.pairs ?? []) for (let t of e) r(t, n, "across joint");
226
+ }
227
+ let i = /* @__PURE__ */ new Set();
228
+ for (let r of e.regions) {
229
+ let a = `${e.id} region ${r.id}`;
230
+ if (i.has(r.id)) throw RangeError(`${a}: the id is used twice; give each region its own id`);
231
+ i.add(r.id);
232
+ let o = t.get(r.joints[0]);
233
+ if (!o?.dofs) throw RangeError(`${a}: needs at least one joint with motions`);
234
+ for (let e of r.joints) {
235
+ let i = t.get(e);
236
+ if (!i?.dofs || i.region !== r.id) throw RangeError(`${a}: joint "${e}" must exist, have motions and name this region`);
237
+ for (let t = 0; t < 3; t++) if (!n(g(i.dofs[t].axis, o.dofs[t].axis), 1) || i.dofs[t].positive !== o.dofs[t].positive) throw RangeError(`${a}: joint "${e}" must share the axes and motion names of "${o.id}"`);
238
+ }
239
+ }
240
+ for (let t of e.joints) if (t.region && !i.has(t.region)) throw RangeError(`${e.id} joint ${t.id}: region "${t.region}" is not in the table; use a region id the table lists`);
241
+ let a = e.frame;
242
+ for (let t of [...a.pairs, ...a.ahead]) for (let n of t) r(n, `${e.id} frame`, "joint");
243
+ for (let t of [...a.top, ...a.bottom]) r(t, `${e.id} frame`, "joint");
244
+ for (let t of e.stature) if (r(t.from, `${e.id} stature`, "joint"), r(t.to, `${e.id} stature`, "joint"), !(t.share > 0)) throw RangeError(`${e.id} stature: a share must be above 0`);
245
+ for (let t of e.proportions) {
246
+ let n = t.measure;
247
+ if (n && (r(n.from, `${e.id} ${t.id}`, "joint"), r(n.to, `${e.id} ${t.id}`, "joint"), !(n.band[0] > 0 && n.band[0] < t.share && t.share < n.band[1]))) throw RangeError(`${e.id} ${t.id}: the band must be [low, high] around the share, low above 0`);
248
+ }
249
+ let o = /* @__PURE__ */ new Set();
250
+ for (let t of e.volumes ?? []) {
251
+ let n = `${e.id} volume ${t.id}`;
252
+ if (o.has(t.id)) throw RangeError(`${n}: the id is used twice; give every volume its own id`);
253
+ o.add(t.id), r(t.from, n, "joint");
254
+ for (let e of t.to) r(e, n, "joint");
255
+ if (!t.to.length || !(t.radius[0] > 0 && t.radius[1] > 0)) throw RangeError(`${n}: needs a far joint and two radii above 0`);
256
+ }
257
+ for (let t of e.apart ?? []) for (let n of t) if (!o.has(n)) throw RangeError(`${e.id} apart: "${n}" is not a volume of the table; name only ids listed in volumes`);
258
+ for (let t of e.feet ?? []) r(t, `${e.id} feet`, "joint");
259
+ for (let t of [
260
+ ...e.proportions,
261
+ ...e.segments,
262
+ ...e.couplings,
263
+ ...e.volumes ?? []
264
+ ]) for (let n of t.rows) if (!Object.hasOwn(e.sources, n.source)) throw RangeError(`${e.id} ${t.id}: source "${n.source}" is not listed; add it to the table's sources`);
265
+ return Object.freeze(e);
266
+ }
267
+ var A = /* @__PURE__ */ new WeakMap(), j = (e) => {
268
+ let t = A.get(e);
269
+ if (!t) throw TypeError("This needs an AnatomicalRig made by fitAnatomy(rig, { species })");
270
+ return t;
271
+ };
272
+ function M(e, t, n, r) {
273
+ let { world: i, stamp: a, stack: o } = t, s = e.parents, c = 0;
274
+ for (let e = r; e >= 0 && a[e] !== t.tick; e = s[e]) o[c++] = e;
275
+ for (; c > 0;) {
276
+ let e = o[--c], r = s[e], l = e * 10 + 3;
277
+ if (r < 0) for (let t = 0; t < 4; t++) i[e * 4 + t] = n[l + t];
278
+ else E(i, e * 4, i, r * 4, n, l);
279
+ a[e] = t.tick;
280
+ }
281
+ return r * 4;
282
+ }
283
+ function N(e, t, n, r, i) {
284
+ let o = M(e, t, n, r.bone), s = M(e, t, n, r.reference);
285
+ for (let e = 0; e < 4; e++) t.q[e] = t.world[s + e], t.q[4 + e] = t.world[o + e];
286
+ a(r, t.reference, t.bone, i);
287
+ }
288
+ function P(e, t, n, r, i) {
289
+ let a = t.q;
290
+ o(r, i, t.reference);
291
+ let s = M(e, t, n, r.reference);
292
+ E(a, 0, t.world, s, a, 0);
293
+ let c = e.parents[r.bone], l = r.bone * 10 + 3;
294
+ if (c < 0) for (let e = 0; e < 4; e++) a[4 + e] = a[e];
295
+ else E(a, 4, t.world, M(e, t, n, c), a, 0, !0);
296
+ let u = Math.sqrt(a[4] * a[4] + a[5] * a[5] + a[6] * a[6] + a[7] * a[7]) || 1;
297
+ for (let e = 0; e < 4; e++) n[l + e] = a[4 + e] / u, t.world[r.bone * 4 + e] = a[e];
298
+ t.stamp[r.bone] = t.tick;
299
+ }
300
+ function ne(t) {
301
+ if (Array.isArray(t)) {
302
+ let n = t;
303
+ e(n);
304
+ let r = new Float64Array(n.length * 10);
305
+ return n.forEach((e, t) => {
306
+ let i = e.parent < 0 ? [
307
+ 0,
308
+ 0,
309
+ 0
310
+ ] : n[e.parent].position;
311
+ r.set([
312
+ e.position[0] - i[0],
313
+ e.position[1] - i[1],
314
+ e.position[2] - i[2]
315
+ ], t * 10), r.set([
316
+ 0,
317
+ 0,
318
+ 0,
319
+ 1,
320
+ 1,
321
+ 1,
322
+ 1
323
+ ], t * 10 + 3);
324
+ }), {
325
+ names: n.map((e) => e.name),
326
+ parents: n.map((e) => e.parent),
327
+ rest: r
328
+ };
329
+ }
330
+ let n = t, r = n?.names?.length;
331
+ if (!n || !Array.isArray(n.names) || !Array.isArray(n.parents) || !r || n.parents.length !== r || n.rest?.length !== r * 10) throw TypeError("fitAnatomy needs a rig: { names, parents, rest } with 10 rest numbers per bone, or an array of { name, parent, position }");
332
+ let i = Float64Array.from(n.rest);
333
+ for (let e = 0; e < r; e++) {
334
+ let t = n.parents[e];
335
+ if (!Number.isInteger(t) || t < -1 || t >= r || t === e) throw RangeError(`Bone ${n.names[e]} has the invalid parent ${t}; a parent is -1 or the index of another bone`);
336
+ let a = e * 10 + 3, o = Math.hypot(i[a], i[a + 1], i[a + 2], i[a + 3]);
337
+ if (!(o > 0) || !i.subarray(e * 10, e * 10 + 10).every(Number.isFinite)) throw RangeError(`Bone ${n.names[e]} needs a finite rest pose with a nonzero rotation`);
338
+ for (let e = 0; e < 4; e++) i[a + e] /= o;
339
+ }
340
+ return {
341
+ names: [...n.names],
342
+ parents: [...n.parents],
343
+ rest: i
344
+ };
345
+ }
346
+ function re(e, r) {
347
+ let i = r?.species;
348
+ if (!i || !Array.isArray(i.joints)) throw TypeError("fitAnatomy needs a species table: fitAnatomy(rig, { species: human }), with human from '@margoengine/rigging/anatomy/human'");
349
+ let { names: a, parents: o, rest: c } = ne(e), u = a.length, D = t(o, "Rig"), O = n(o, c, D).position, k = new Float64Array(u * 4), j = new Int32Array(u);
350
+ for (let e of D) {
351
+ let t = o[e];
352
+ t < 0 ? k.set(c.subarray(e * 10 + 3, e * 10 + 7), e * 4) : (E(k, e * 4, k, t * 4, c, e * 10 + 3), j[e] = j[t] + 1);
353
+ }
354
+ let M = new Map(i.joints.map((e) => [e.id, e])), N = [
355
+ 0,
356
+ 0,
357
+ 0,
358
+ 1
359
+ ], P = /* @__PURE__ */ new Map(), re = null, F = i.naming?.(a, r.preset) ?? null;
360
+ if (F) {
361
+ re = F.preset;
362
+ for (let [e, t] of F.joints) M.has(e) && P.set(e, t);
363
+ } else if (r.preset !== void 0) throw RangeError(`No bones of this rig match the "${r.preset}" naming of the ${i.id} table; use a rig whose bones follow it or pass options.map`);
364
+ let ie = /* @__PURE__ */ new Set();
365
+ for (let e of Object.values(r.map ?? {})) e !== null && ie.add(typeof e == "number" ? e : a.indexOf(e));
366
+ for (let [e, t] of [...P]) ie.has(t) && !(r.map && e in r.map) && P.delete(e);
367
+ for (let [e, t] of Object.entries(r.map ?? {})) {
368
+ if (!M.has(e)) throw RangeError(`options.map: "${e}" is not a joint of the ${i.id} table; use one of its joint ids`);
369
+ if (t === null) {
370
+ P.delete(e);
371
+ continue;
372
+ }
373
+ let n = typeof t == "number" ? t : a.indexOf(t);
374
+ if (!Number.isInteger(n) || n < 0 || n >= u) throw RangeError(`options.map.${e}: this rig has no bone ${String(t)}; use a bone index or a bone name the rig lists`);
375
+ P.set(e, n);
376
+ }
377
+ for (let [e, t] of Object.entries(r.roll ?? {})) if (!M.has(e) || !Number.isFinite(t)) throw RangeError(`options.roll: "${e}" must be a joint of the ${i.id} table with a finite number of degrees`);
378
+ for (let [e, t] of Object.entries(r.drop ?? {})) if (!M.get(e)?.rhythm || !Array.isArray(t) || t.length !== 2 || !t.every(Number.isFinite)) throw RangeError(`options.drop: "${e}" must be a joint of the ${i.id} table with a rhythm, with [elevation, retraction] in degrees`);
379
+ let oe = i.joints[0];
380
+ if (!P.has(oe.id)) throw RangeError(`This rig has no bone for ${oe.id}, the root of the ${i.id} table; pass options.map: { ${oe.id}: 'bone name' }`);
381
+ let se = /* @__PURE__ */ new Map();
382
+ for (let [e, t] of P) {
383
+ let n = se.get(t);
384
+ if (n) throw RangeError(`Bone ${a[t]} is mapped to both ${n} and ${e}; one bone turns at one joint`);
385
+ se.set(t, e);
386
+ }
387
+ let I = /* @__PURE__ */ new Map();
388
+ for (let e of P.keys()) I.set(e, e);
389
+ for (let e of i.regions) {
390
+ let t = null, n = [];
391
+ for (let r of e.joints) if (P.has(r)) {
392
+ t = r;
393
+ for (let e of n) I.set(e, r);
394
+ n.length = 0;
395
+ } else t ? I.set(r, t) : n.push(r);
396
+ }
397
+ let L = [], R = /* @__PURE__ */ new Map();
398
+ for (let e of i.joints) {
399
+ let t = P.get(e.id);
400
+ t !== void 0 && (R.set(e.id, L.length), L.push({
401
+ spec: e,
402
+ bone: t,
403
+ covers: [],
404
+ ref: -1,
405
+ frame: [
406
+ 0,
407
+ 0,
408
+ 0,
409
+ 1
410
+ ],
411
+ long: [
412
+ 0,
413
+ 1,
414
+ 0
415
+ ],
416
+ axes: null
417
+ }));
418
+ }
419
+ for (let e of i.joints) {
420
+ let t = I.get(e.id);
421
+ t !== void 0 && L[R.get(t)].covers.push(e.id);
422
+ }
423
+ let le = (e, t) => {
424
+ for (let n = e ?? null; n; n = M.get(n).parent) {
425
+ let e = I.get(n);
426
+ if (e !== void 0 && R.get(e) !== t) return R.get(e);
427
+ }
428
+ return -1;
429
+ };
430
+ L.forEach((e, t) => {
431
+ e.ref = le(e.spec.reference ?? e.spec.parent, t);
432
+ });
433
+ let z = (e) => [
434
+ O[e * 3],
435
+ O[e * 3 + 1],
436
+ O[e * 3 + 2]
437
+ ], B = (e) => {
438
+ let t = P.get(e);
439
+ return t === void 0 ? null : z(t);
440
+ }, ue = (e) => {
441
+ for (let t of e) {
442
+ let e = B(t);
443
+ if (e) return e;
444
+ }
445
+ return null;
446
+ }, V = null, H = null;
447
+ for (let [e, t] of i.frame.pairs) {
448
+ let n = B(e), r = B(t);
449
+ if (n && r && b(v(n, r))) {
450
+ V = n, H = r;
451
+ break;
452
+ }
453
+ }
454
+ if (!V || !H) throw RangeError(`This rig needs a left and a right joint to read its facing, such as ${i.frame.pairs[0]?.join(" and ")}; map them with options.map`);
455
+ let U = [
456
+ (V[0] + H[0]) / 2,
457
+ (V[1] + H[1]) / 2,
458
+ (V[2] + H[2]) / 2
459
+ ], W = Math.sqrt(g(v(V, H), v(V, H))), G = W * 1e-6, K = i.frame.up ? b(i.frame.up) : null;
460
+ if (!K) {
461
+ let e = ue(i.frame.top), t = ue(i.frame.bottom);
462
+ K = e && b(v(e, U), G) || t && b(v(U, t), G) || null;
463
+ }
464
+ let q = K && b(x(v(V, H), K), G);
465
+ if (!K || !q) throw RangeError("This rig needs a joint above or below its hips to read which way is up; map one with options.map");
466
+ let J = _(q, K), de = !1, fe = r.forward ? [...r.forward] : [
467
+ 0,
468
+ 0,
469
+ 0
470
+ ];
471
+ if (!r.forward) for (let [e, t] of i.frame.ahead) {
472
+ let n = B(e), r = B(t);
473
+ n && r && (fe = v(fe, x(v(n, r), K)));
474
+ }
475
+ g(fe, J) < -G && (de = !0, q = y(q, -1), J = y(J, -1));
476
+ let Y = K;
477
+ if (r.vertical) {
478
+ let e = b(r.vertical);
479
+ if (!e || !r.vertical.every(Number.isFinite)) throw RangeError("options.vertical must be a finite direction, such as [0, 1, 0]");
480
+ Y = e;
481
+ } else if (!i.frame.up) {
482
+ let e = 0;
483
+ for (let t = 1; t < 3; t++) Math.abs(K[t]) > Math.abs(K[e]) && (e = t);
484
+ let t = [
485
+ 0,
486
+ 0,
487
+ 0
488
+ ];
489
+ t[e] = Math.sign(K[e]);
490
+ let n = 0, r = g(t, K) >= Math.cos(m), a = /* @__PURE__ */ new Set();
491
+ for (let e of i.feet ?? []) {
492
+ let i = M.get(e)?.mirror;
493
+ if (!i || a.has(e)) continue;
494
+ a.add(e).add(i);
495
+ let o = B(e), s = B(i);
496
+ o && s && (n++, (Math.abs(g(v(o, s), t)) > h * W || g(v(U, o), t) < W || g(v(U, s), t) < W) && (r = !1));
497
+ }
498
+ n && r && (Y = t);
499
+ }
500
+ let pe = 0;
501
+ for (let e of i.stature) {
502
+ let t = B(e.from), n = B(e.to);
503
+ if (t && n && Math.abs(g(v(n, t), Y)) > G) {
504
+ pe = Math.abs(g(v(n, t), Y)) / e.share;
505
+ break;
506
+ }
507
+ }
508
+ let me = te(q, K, J), he = (e) => de && e.mirror ? M.get(e.mirror) : e, ge = (e, t, n) => {
509
+ for (let r of t ?? []) {
510
+ let t = P.get(r), i = t === void 0 || t === n ? null : b(v(z(t), e), G);
511
+ if (i) return i;
512
+ }
513
+ return null;
514
+ };
515
+ L.forEach((e, t) => {
516
+ let { spec: n, bone: i } = e, a = he(n), o = e.ref >= 0 ? L[e.ref].frame : me, c = z(i), u = ge(c, n.distal, i), m = o, h = [...a.long], te = !0;
517
+ if (n.neutral === "rest") {
518
+ let e = u && w(C(o), u);
519
+ e && g(e, h) > .5 && (h = e);
520
+ let t = n.rhythm, i = t ? B(t.from) : null, c = t ? B(t.to) : null, d = i && c ? b(v(c, i), G) : null;
521
+ if (t && e && u && d && h === e) {
522
+ let i = b(a.long), c = [
523
+ 0,
524
+ 1,
525
+ 0
526
+ ], f = [
527
+ 0,
528
+ 0,
529
+ -1
530
+ ], p = Math.acos(Math.min(1, Math.max(-1, -g(d, K)))) / l, v = s(t.curve, t.curve[0][0]), C = s(t.curve, p), te = Math.asin(Math.min(1, Math.max(-1, g(e, c)))), E = Math.atan2(g(e, f), g(e, i)), D = (e, t, n) => Math.min(Math.max(e - t, 0), Math.max(n, 0)), O = r.drop?.[n.id] ?? [0, 0], k = Math.max(D(te / l, v[0], C[0] - v[0]), D(O[0], 0, C[0] - v[0])) * l, A = Math.max(D(E / l, v[1], C[1] - v[1]), D(O[1], 0, C[1] - v[1])) * l;
531
+ if ((k > 0 || A > 0) && a.dofs) {
532
+ let t = e;
533
+ for (let n = 0; n < 6; n++) {
534
+ let n = b(x(a.dofs[0].axis, t)), r = _(t, n), i = g(r, a.dofs[1].axis) < 0 ? y(r, -1) : r, o = [
535
+ 0,
536
+ 1,
537
+ 2
538
+ ].map((e) => -(k * n[e] - A * i[e])), s = Math.hypot(...o);
539
+ t = s > 0 ? w(T(y(o, 1 / s), s), e) : e;
540
+ }
541
+ m = S(ee(w(o, t), u), o), h = t;
542
+ }
543
+ }
544
+ } else {
545
+ let i = (n.neutral === "straight" && e.ref >= 0 ? b(v(c, z(L[e.ref].bone)), G) : null) ?? w(o, h), a = u ?? i;
546
+ u && (m = S(ee(i, u), o)), n.neutral === "straight" && (h = b(w(C(o), i)));
547
+ for (let e = t + 1; e < L.length; e++) {
548
+ let n = L[e], r = n.spec;
549
+ if (n.ref !== t || r.kind !== "hinge" || r.neutral !== "straight" || !r.dofs) continue;
550
+ let i = z(n.bone), a = b(v(i, c), G), o = ge(i, r.distal, n.bone);
551
+ if (!a || !o) continue;
552
+ let s = Math.acos(Math.min(1, Math.max(-1, g(a, o))));
553
+ if (s < d) continue;
554
+ let l = b(_(a, o)), u = b(x(w(m, he(r).dofs[0].axis), a));
555
+ if (l && u) {
556
+ let e = Math.atan2(g(_(u, l), a), g(u, l));
557
+ (s >= f || Math.abs(e) <= p) && (m = S(T(a, e), m));
558
+ }
559
+ break;
560
+ }
561
+ for (let e = t + 1; e < L.length; e++) {
562
+ let n = L[e], r = n.spec;
563
+ if (n.ref !== t || !r.across) continue;
564
+ let i = null;
565
+ for (let [e, t] of r.across.pairs) {
566
+ let n = B(e), r = B(t);
567
+ if (n && r) {
568
+ i = v(r, n);
569
+ break;
570
+ }
571
+ }
572
+ if (!i) continue;
573
+ let o = z(n.bone), s = b(v(o, c), G) ?? a, l = ge(o, r.distal, n.bone) ?? s, u = b(x(w(S(ee(s, l), m), he(r).across.neutral), l)), d = b(x(i, l), G);
574
+ u && d && (m = S(T(a, Math.atan2(g(_(u, d), l), g(u, d))), m));
575
+ break;
576
+ }
577
+ let s = r.roll?.[n.id];
578
+ s && (m = S(T(a, s * l), m)), n.neutral === "body" && !u && (te = !1);
579
+ }
580
+ if (e.frame = m, e.long = h, a.dofs && te) {
581
+ let t = b(x(a.dofs[0].axis, h));
582
+ if (!t) throw RangeError(`${n.id}: its segment lies along its own flexion axis in this rig; check the joint map`);
583
+ let r = _(h, t);
584
+ e.axes = [
585
+ t,
586
+ g(r, a.dofs[1].axis) < 0 ? y(r, -1) : r,
587
+ y(h, g(a.dofs[2].axis, h) < 0 ? -1 : 1)
588
+ ];
589
+ }
590
+ });
591
+ let _e = new Map(i.regions.map((e) => [e.id, e])), X = (e, t) => {
592
+ let n = [
593
+ 0,
594
+ 0,
595
+ 0,
596
+ 0,
597
+ 0,
598
+ 0
599
+ ];
600
+ for (let r of e) {
601
+ let e = M.get(r).dofs;
602
+ if (e) for (let r = 0; r < 3; r++) n[r * 2] = Math.max(-180, n[r * 2] - e[r].min[t]), n[r * 2 + 1] = Math.min(180, n[r * 2 + 1] + e[r].max[t]);
603
+ }
604
+ return n;
605
+ }, ve = (e) => [
606
+ k[e * 4],
607
+ k[e * 4 + 1],
608
+ k[e * 4 + 2],
609
+ k[e * 4 + 3]
610
+ ], Z = L.map((e) => S(C(ve(e.bone)), e.frame)), ye = new Set(L.map((e) => e.bone)), be = /* @__PURE__ */ new Set();
611
+ L.forEach((e, t) => {
612
+ if (!e.axes || e.ref < 0 || !e.spec.dofs?.[2].spill) return;
613
+ let n = L[e.ref].bone, r = z(n), i = v(z(e.bone), r), a = g(i, i);
614
+ if (a > 0) for (let e = 0; e < u; e++) {
615
+ if (ye.has(e)) continue;
616
+ let s = o[e];
617
+ for (; s >= 0 && s !== n && !ye.has(s);) s = o[s];
618
+ if (s !== n) continue;
619
+ let c = v(z(e), r), l = g(c, i) / a, u = v(c, y(i, l));
620
+ if (l > .1 && l < .95 && g(u, u) < .0225 * a) {
621
+ be.add(t);
622
+ return;
623
+ }
624
+ }
625
+ });
626
+ let xe = (e, t) => [
627
+ e[0],
628
+ e[1],
629
+ e[2],
630
+ e[3],
631
+ Math.max(-180, e[4] + t[4]),
632
+ Math.min(180, e[5] + t[5])
633
+ ], Q = L.map((e, t) => {
634
+ let { spec: n, bone: r, ref: i } = e, a = n.region ? _e.get(n.region) : null, s = !!e.axes && i >= 0, l = e.axes ?? [
635
+ [
636
+ 1,
637
+ 0,
638
+ 0
639
+ ],
640
+ [
641
+ 0,
642
+ 0,
643
+ 1
644
+ ],
645
+ [
646
+ 0,
647
+ 1,
648
+ 0
649
+ ]
650
+ ], u = i >= 0 ? L[i].frame : me, d = o[r], f = S(u, C(Z[t])), p = [
651
+ c[r * 10 + 3],
652
+ c[r * 10 + 4],
653
+ c[r * 10 + 5],
654
+ c[r * 10 + 6]
655
+ ], m = s ? d < 0 ? f : S(C(ve(d)), f) : p, h = i >= 0 ? Z[i] : N, g = de && n.side !== "centre" ? n.side === "left" ? "right" : "left" : n.side;
656
+ return {
657
+ id: n.id,
658
+ spec: n,
659
+ bone: r,
660
+ reference: i >= 0 ? L[i].bone : -1,
661
+ covers: e.covers,
662
+ side: g,
663
+ kind: n.kind,
664
+ region: n.region ?? null,
665
+ limited: s,
666
+ long: e.long,
667
+ axes: l,
668
+ boneAxes: [
669
+ w(Z[t], l[0]),
670
+ w(Z[t], l[1]),
671
+ w(Z[t], l[2])
672
+ ],
673
+ frame: Z[t],
674
+ referenceFrame: h,
675
+ neutral: m,
676
+ rest: {
677
+ flexion: 0,
678
+ abduction: 0,
679
+ rotation: 0
680
+ },
681
+ normal: be.has(t) ? xe(X(e.covers, "normal"), X(L[i].covers, "normal")) : X(e.covers, "normal"),
682
+ structural: be.has(t) ? xe(X(e.covers, "structural"), X(L[i].covers, "structural")) : X(a?.shared ? a.joints : e.covers, "structural")
683
+ };
684
+ }), Se = /* @__PURE__ */ new Map();
685
+ Q.forEach((e, t) => {
686
+ for (let n of e.covers) Se.set(n, t);
687
+ });
688
+ let Ce = [];
689
+ for (let e of i.regions) {
690
+ let t = e.joints.flatMap((e) => R.has(e) ? [R.get(e)] : []);
691
+ e.shared && t.length && Q[t[0]].limited && Ce.push({
692
+ id: e.id,
693
+ joints: t,
694
+ normal: X(e.joints, "normal"),
695
+ structural: X(e.joints, "structural")
696
+ });
697
+ }
698
+ let $ = {
699
+ species: i,
700
+ names: a,
701
+ parents: o,
702
+ rest: c,
703
+ neutral: c.slice(),
704
+ frame: {
705
+ origin: U,
706
+ left: q,
707
+ up: K,
708
+ forward: J,
709
+ vertical: Y
710
+ },
711
+ mirrored: de,
712
+ stature: pe,
713
+ restPose: "",
714
+ preset: re,
715
+ limits: r.limits !== !1,
716
+ joints: Q,
717
+ regions: Ce,
718
+ index: Se
719
+ }, we = Q.map((e, t) => [e, t]).sort((e, t) => j[e[0].bone] - j[t[0].bone] || e[1] - t[1]), Te = /* @__PURE__ */ new Float64Array(16);
720
+ A.set($, {
721
+ order: we.map(([e]) => e),
722
+ spills: Q.flatMap((e, t) => e.limited && e.spec.dofs[2].spill && !be.has(t) && L[t].ref >= 0 && Q[L[t].ref].limited ? [[e, Q[L[t].ref]]] : []),
723
+ world: new Float64Array(u * 4),
724
+ stamp: new Uint32Array(u),
725
+ stack: new Int32Array(u),
726
+ q: Te,
727
+ reference: Te.subarray(0, 4),
728
+ bone: Te.subarray(4, 8),
729
+ v: /* @__PURE__ */ new Float64Array(3),
730
+ w: /* @__PURE__ */ new Float64Array(3),
731
+ tick: 0
732
+ });
733
+ let Ee = ae($, c);
734
+ for (let e of Q) e.limited && Object.assign(e.rest, Ee[e.id]);
735
+ let De = {};
736
+ for (let e of Q) e.limited && (De[e.id] = {
737
+ flexion: 0,
738
+ abduction: 0,
739
+ rotation: 0
740
+ });
741
+ return $.neutral.set(ce($, De)), $.restPose = i.describeRest?.(Ee) ?? "rest", $;
742
+ }
743
+ function F(e, t, n) {
744
+ if (!t || t.length !== e.names.length * 10) throw TypeError(`${n} needs a local pose with 10 numbers per bone (translation, rotation xyzw, scale)`);
745
+ }
746
+ function ie(e, t = e.rest, n) {
747
+ let r = j(e);
748
+ F(e, t, "readJointAngles");
749
+ let i = n ?? new Float64Array(e.joints.length * 3);
750
+ if (i.length !== e.joints.length * 3) throw TypeError("readJointAngles: out needs 3 numbers per fitted joint");
751
+ return r.tick++, e.joints.forEach((n, a) => {
752
+ n.limited ? (N(e, r, t, n, r.v), i.set(r.v, a * 3)) : i.fill(0, a * 3, a * 3 + 3);
753
+ }), i;
754
+ }
755
+ function ae(e, t = e.rest) {
756
+ let n = ie(e, t), r = {};
757
+ return e.joints.forEach((e, t) => {
758
+ e.limited && (r[e.id] = {
759
+ flexion: n[t * 3],
760
+ abduction: n[t * 3 + 1],
761
+ rotation: n[t * 3 + 2]
762
+ });
763
+ }), r;
764
+ }
765
+ function oe(e, t) {
766
+ let n = j(e), r = e.joints.filter((e) => e.limited && e.kind === "hinge" && e.spec.neutral === "straight"), i = r.map(() => [
767
+ 0,
768
+ 0,
769
+ 0,
770
+ 0,
771
+ 0
772
+ ]);
773
+ for (let a of t) F(e, a, "hingeRolls"), n.tick++, r.forEach((t, r) => {
774
+ N(e, n, a, t, n.v);
775
+ let o = n.v[0], s = n.v[1], c = i[r];
776
+ c[0] += o * s, c[1] += o * o - s * s, c[2] += o, c[3] += s, c[4] = Math.max(c[4], Math.hypot(o, s));
777
+ });
778
+ let a = {};
779
+ return r.forEach((t, n) => {
780
+ let r = e.joints.find((e) => e.bone === t.reference), o = i[n];
781
+ if (!r || r.spec.neutral === "rest" || o[4] < 30) return;
782
+ let s = .5 * Math.atan2(2 * o[0], o[1]);
783
+ o[2] * Math.cos(s) + o[3] * Math.sin(s) < 0 && (s += s > 0 ? -Math.PI : Math.PI);
784
+ let [l, u] = t.axes;
785
+ a[r.id] = (g(_(t.long, l), u) >= 0 ? s : -s) * c;
786
+ }), a;
787
+ }
788
+ function se(e, t) {
789
+ let n = j(e), r = e.joints.flatMap((t) => {
790
+ let n = t.spec.rhythm, r = n && e.joints[e.index.get(n.from) ?? -1], i = n && e.joints[e.index.get(n.to) ?? -1];
791
+ return n && t.limited && r && i && n.curve.length > 1 ? [{
792
+ joint: t,
793
+ rhythm: n,
794
+ from: r.bone,
795
+ to: i.bone,
796
+ seen: []
797
+ }] : [];
798
+ });
799
+ if (!r.length) return {};
800
+ let i = e.parents, a = (t, r) => {
801
+ let a = [];
802
+ for (let e = r; e >= 0; e = i[e]) a.push(e);
803
+ let o = [
804
+ 0,
805
+ 0,
806
+ 0
807
+ ];
808
+ for (let r = a.length - 1; r >= 0; r--) {
809
+ let s = a[r], c = [
810
+ t[s * 10],
811
+ t[s * 10 + 1],
812
+ t[s * 10 + 2]
813
+ ], l = i[s];
814
+ if (l < 0) o = c;
815
+ else {
816
+ let r = M(e, n, t, l), i = [
817
+ n.world[r],
818
+ n.world[r + 1],
819
+ n.world[r + 2],
820
+ n.world[r + 3]
821
+ ];
822
+ o = [
823
+ o[0] + w(i, c)[0],
824
+ o[1] + w(i, c)[1],
825
+ o[2] + w(i, c)[2]
826
+ ];
827
+ }
828
+ }
829
+ return o;
830
+ }, o = (t, r) => {
831
+ let i = b(v(a(t, r.to), a(t, r.from)), 1e-12), o = r.joint.reference;
832
+ if (!i || o < 0) return i;
833
+ let s = M(e, n, t, o);
834
+ return w([
835
+ -n.world[s],
836
+ -n.world[s + 1],
837
+ -n.world[s + 2],
838
+ n.world[s + 3]
839
+ ], i);
840
+ }, c = e.frame.up, u = [];
841
+ n.tick++;
842
+ for (let t of r) {
843
+ let r = t.joint.reference, i = r < 0 ? -1 : M(e, n, e.rest, r);
844
+ u.push(i < 0 ? [...c] : w([
845
+ -n.world[i],
846
+ -n.world[i + 1],
847
+ -n.world[i + 2],
848
+ n.world[i + 3]
849
+ ], c));
850
+ }
851
+ for (let i of t) F(e, i, "rhythmDrops"), n.tick++, r.forEach((t, r) => {
852
+ let a = o(i, t);
853
+ if (!a) return;
854
+ let c = Math.acos(Math.min(1, Math.max(-1, -g(a, u[r])))) / l, d = t.rhythm.curve;
855
+ if (c > d[1][0]) return;
856
+ N(e, n, i, t.joint, n.v);
857
+ let [f, p] = s(d, d[0][0]), [m, h] = s(d, c), _ = t.joint.rest;
858
+ t.seen.push([_.flexion - n.v[0] + (m - f), n.v[1] - _.abduction + (h - p)]);
859
+ });
860
+ let d = {}, f = (e) => {
861
+ let t = [...e].sort((e, t) => e - t), n = t.length >> 1;
862
+ return t.length % 2 ? t[n] : (t[n - 1] + t[n]) / 2;
863
+ };
864
+ for (let e of r) e.seen.length && (d[e.joint.id] = [f(e.seen.map((e) => e[0])), f(e.seen.map((e) => e[1]))]);
865
+ return d;
866
+ }
867
+ var I = (e, t) => {
868
+ let n = e.joints[t.joints[0]], r = e.joints[t.joints.at(-1)];
869
+ return {
870
+ bone: r.bone,
871
+ reference: n.reference,
872
+ frame: r.frame,
873
+ referenceFrame: n.referenceFrame,
874
+ axes: n.axes
875
+ };
876
+ };
877
+ function L(e, t = e.rest) {
878
+ let n = j(e), r = {};
879
+ F(e, t, "regionAngles"), n.tick++;
880
+ for (let i of e.regions) N(e, n, t, I(e, i), n.v), r[i.id] = {
881
+ flexion: n.v[0],
882
+ abduction: n.v[1],
883
+ rotation: n.v[2]
884
+ };
885
+ return r;
886
+ }
887
+ function ce(e, t, n = e.rest) {
888
+ let r = j(e);
889
+ F(e, n, "poseFromAngles");
890
+ for (let n of Object.keys(t)) {
891
+ let t = e.index.get(n);
892
+ if (t === void 0 || e.joints[t].id !== n || !e.joints[t].limited) throw RangeError(`poseFromAngles: "${n}" is not a limited joint of this body; its joints are in body.joints`);
893
+ }
894
+ let i = Float64Array.from(n);
895
+ r.tick++;
896
+ for (let n of r.order) {
897
+ let a = t[n.id];
898
+ if (a && n.limited) {
899
+ if (N(e, r, i, n, r.v), a.flexion !== void 0 && (r.v[0] = a.flexion), a.abduction !== void 0 && (r.v[1] = a.abduction), a.rotation !== void 0 && (r.v[2] = a.rotation), !(Number.isFinite(r.v[0]) && Number.isFinite(r.v[1]) && Number.isFinite(r.v[2]))) throw RangeError(`poseFromAngles: ${n.id} needs finite degrees`);
900
+ P(e, r, i, n, r.v);
901
+ }
902
+ }
903
+ return i;
904
+ }
905
+ var R = (e, t) => t === "normal" ? e.normal : e.structural;
906
+ function le(e, t, n = {}) {
907
+ let r = j(e), a = n.tier ?? "structural", { v: o, w: s } = r;
908
+ if (!(t instanceof Float64Array)) throw TypeError("clampPose needs the pose as a Float64Array");
909
+ if (F(e, t, "clampPose"), !e.limits && !n.force) return 0;
910
+ let c = 0, d = (e, t, r) => {
911
+ for (let i = 0; i < 3; i++) {
912
+ if (Math.abs(s[i] - o[i]) <= 1e-6) continue;
913
+ c++;
914
+ let l = r[i];
915
+ n.issues?.push({
916
+ joint: e.id,
917
+ bone: t,
918
+ motion: s[i] >= 0 ? l.positive : l.negative,
919
+ value: s[i],
920
+ limit: o[i],
921
+ tier: a,
922
+ source: (s[i] >= 0 ? l.max : l.min).rows[0]?.source ?? null
923
+ });
924
+ }
925
+ };
926
+ r.tick++;
927
+ for (let [i, s] of r.spills) {
928
+ N(e, r, t, i, o);
929
+ let d = R(i, a), f = o[2] - Math.min(d[5], Math.max(d[4], o[2]));
930
+ if (Math.abs(f) <= u) continue;
931
+ c++;
932
+ let p = i.spec.dofs[2];
933
+ n.issues?.push({
934
+ joint: i.id,
935
+ bone: i.bone,
936
+ motion: o[2] >= 0 ? p.positive : p.negative,
937
+ value: o[2],
938
+ limit: o[2] - f,
939
+ tier: a,
940
+ source: (o[2] >= 0 ? p.max : p.min).rows[0]?.source ?? null
941
+ });
942
+ let m = M(e, r, t, i.bone), h = [
943
+ r.world[m],
944
+ r.world[m + 1],
945
+ r.world[m + 2],
946
+ r.world[m + 3]
947
+ ], g = S(S(s.frame, T(i.axes[2], f * l)), C(s.frame)), _ = s.bone * 10 + 3;
948
+ t.set(S([
949
+ t[_],
950
+ t[_ + 1],
951
+ t[_ + 2],
952
+ t[_ + 3]
953
+ ], g), _), r.tick++;
954
+ let v = e.parents[i.bone], y = v < 0 ? null : M(e, r, t, v);
955
+ t.set(y === null ? h : S(C([
956
+ r.world[y],
957
+ r.world[y + 1],
958
+ r.world[y + 2],
959
+ r.world[y + 3]
960
+ ]), h), i.bone * 10 + 3), r.tick++;
961
+ }
962
+ r.tick++;
963
+ for (let n of r.order) n.limited && (N(e, r, t, n, o), s.set(o), i(n, o, a) && (P(e, r, t, n, o), d(n, n.bone, n.spec.dofs)));
964
+ for (let n of e.regions) {
965
+ let c = I(e, n), l = e.joints[n.joints[0]].spec.dofs;
966
+ for (let u = 0; u < 8 && (r.tick++, N(e, r, t, c, o), s.set(o), i(n, o, a)); u++) {
967
+ u === 0 && d(n, -1, l);
968
+ let i = [
969
+ 0,
970
+ 1,
971
+ 2
972
+ ].map((e) => Math.abs(s[e]) > 1e-9 ? o[e] / s[e] : 1);
973
+ for (let a of n.joints) {
974
+ let n = e.joints[a];
975
+ r.tick++, N(e, r, t, n, o);
976
+ for (let e = 0; e < 3; e++) o[e] *= i[e];
977
+ P(e, r, t, n, o);
978
+ }
979
+ }
980
+ }
981
+ return c;
982
+ }
983
+ function z(e, t) {
984
+ let n = R(e, t), [r, , i] = e.boneAxes, a = [n[4] * l, n[5] * l];
985
+ if (e.kind === "hinge") return {
986
+ neutral: [...e.neutral],
987
+ limit: {
988
+ type: "hinge",
989
+ axis: [...r],
990
+ min: n[0] * l,
991
+ max: n[1] * l,
992
+ ...a[0] || a[1] ? { twist: {
993
+ axis: [...i],
994
+ range: a
995
+ } } : {}
996
+ }
997
+ };
998
+ let o = (n[0] + n[1]) / 2 * l, s = (n[2] + n[3]) / 2 * l, [c, u] = e.axes, d = [
999
+ o * c[0] + s * u[0],
1000
+ o * c[1] + s * u[1],
1001
+ o * c[2] + s * u[2]
1002
+ ], f = Math.sqrt(g(d, d)), p = f > 1e-12 ? T(y(d, 1 / f), f) : [
1003
+ 0,
1004
+ 0,
1005
+ 0,
1006
+ 1
1007
+ ];
1008
+ return {
1009
+ neutral: S(e.neutral, S(S(e.frame, p), C(e.frame))),
1010
+ limit: {
1011
+ type: "cone",
1012
+ angle: Math.min(Math.PI, Math.max(n[1] - n[0], n[3] - n[2]) / 2 * l),
1013
+ axis: [...i],
1014
+ twist: a
1015
+ }
1016
+ };
1017
+ }
1018
+ function B(e, t = "structural") {
1019
+ return j(e), e.joints.flatMap((e) => e.limited ? [{
1020
+ joint: e.bone,
1021
+ ...z(e, t)
1022
+ }] : []);
1023
+ }
1024
+ function ue(e, t, n = "structural") {
1025
+ return j(e), t.map((t) => {
1026
+ let r = typeof t == "number" ? t : e.names.indexOf(t);
1027
+ if (!Number.isInteger(r) || r < 0 || r >= e.names.length) throw RangeError(`ikLimits: this rig has no bone ${String(t)}; use a bone index or a bone name the rig lists`);
1028
+ let i = e.joints.find((e) => e.bone === r);
1029
+ return i?.limited ? {
1030
+ joint: r,
1031
+ ...z(i, n)
1032
+ } : null;
1033
+ });
1034
+ }
1035
+ function V(e, t = "structural") {
1036
+ return j(e), e.joints.flatMap((n) => {
1037
+ if (!n.limited) return [];
1038
+ let r = R(n, t), { flexion: i, abduction: a, rotation: o } = n.rest, s = [(r[4] - o) * l, (r[5] - o) * l];
1039
+ if (n.kind === "hinge") return [{
1040
+ bone: n.bone,
1041
+ name: e.names[n.bone],
1042
+ joint: {
1043
+ type: "hinge",
1044
+ axis: [...n.boneAxes[0]],
1045
+ limits: [(r[0] - i) * l, (r[1] - i) * l]
1046
+ }
1047
+ }];
1048
+ let c = Math.max(Math.abs(r[0] - i), Math.abs(r[1] - i), Math.abs(r[2] - a), Math.abs(r[3] - a));
1049
+ return [{
1050
+ bone: n.bone,
1051
+ name: e.names[n.bone],
1052
+ joint: {
1053
+ type: "cone",
1054
+ swing: Math.min(180, c) * l,
1055
+ twist: s
1056
+ }
1057
+ }];
1058
+ });
1059
+ }
1060
+ function H(e, t = e.joints.map((e) => e.bone), n = "structural") {
1061
+ return j(e), t.map((t) => {
1062
+ let r = typeof t == "number" ? t : e.names.indexOf(t);
1063
+ if (!Number.isInteger(r) || r < 0 || r >= e.names.length) throw RangeError(`kinematicsLimits: this rig has no bone ${String(t)}; use a bone index or a bone name the rig lists`);
1064
+ let i = e.joints.find((e) => e.bone === r);
1065
+ if (!i?.limited) return null;
1066
+ let a = R(i, n);
1067
+ return i.kind === "hinge" ? {
1068
+ type: "hinge",
1069
+ axis: [...i.boneAxes[0]],
1070
+ min: a[0] * l,
1071
+ max: a[1] * l
1072
+ } : {
1073
+ type: "cone",
1074
+ axis: [...i.boneAxes[2]],
1075
+ angle: Math.min(180, Math.max(-a[0], a[1], -a[2], a[3])) * l,
1076
+ twist: [a[4] * l, a[5] * l]
1077
+ };
1078
+ });
1079
+ }
1080
+ function U(e, t, n = {}) {
1081
+ j(t);
1082
+ let r = e.animation.model.nodes, i = r.length;
1083
+ if (i !== t.names.length) throw RangeError(`addAnatomyLimits: the body has ${t.names.length} bones and the Animator's model ${i}; fit the body to that model`);
1084
+ let a = e.animation.model.root.engine.store, o = a.transforms, s = t.rest.slice(), c = new Float64Array(i * 4), l = [
1085
+ 0,
1086
+ 0,
1087
+ 0,
1088
+ 1
1089
+ ], u = t.joints.flatMap((e) => e.limited ? [e.bone] : []), d = [];
1090
+ return e.addSolver(() => {
1091
+ for (let e = 0; e < i; e++) {
1092
+ let { id: t } = r[e], n = e * 10 + 3;
1093
+ c[e * 4] = s[n] = o.qx[t], c[e * 4 + 1] = s[n + 1] = o.qy[t], c[e * 4 + 2] = s[n + 2] = o.qz[t], c[e * 4 + 3] = s[n + 3] = o.qw[t];
1094
+ }
1095
+ d.length = 0, le(t, s, n.onClamp ? {
1096
+ tier: n.tier,
1097
+ issues: d
1098
+ } : { tier: n.tier });
1099
+ let f = !1;
1100
+ for (let t of u) {
1101
+ let n = t * 10 + 3, i = !0;
1102
+ for (let e = 0; e < 4; e++) s[n + e] !== c[t * 4 + e] && (i = !1);
1103
+ if (!i) {
1104
+ for (let e = 0; e < 4; e++) l[e] = s[n + e];
1105
+ e.writeNodeRotation(r[t], l), f = !0;
1106
+ }
1107
+ }
1108
+ f && a.tick(!1), d.length && n.onClamp?.(d);
1109
+ });
1110
+ }
1111
+ //#endregion
1112
+ export { U as addAnatomyLimits, D as bound, le as clampPose, k as defineSpecies, re as fitAnatomy, oe as hingeRolls, ue as ikLimits, ae as jointAngles, H as kinematicsLimits, O as mirrorJoint, ce as poseFromAngles, B as poseLimits, V as ragdollJoints, ie as readJointAngles, L as regionAngles, se as rhythmDrops };