@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.
package/dist/contact.js CHANGED
@@ -1,27 +1,28 @@
1
1
  import { r as e, s as t } from "./rig-BawZ5OLb.js";
2
2
  import { FINGERS as n, grip as r, handRotations as i, measureHands as a, poseTable as o, relaxed as s } from "./hands.js";
3
3
  import { axisAngleToQuaternion as c, conjugateQuaternion as l, interpolateQuaternions as u, multiplyQuaternions as d, normalizeQuaternion as f, rotateByQuaternion as p, rotationToQuaternion as m } from "@margoengine/math/quat";
4
- import { CCDSolver as h } from "@margoengine/animator/ccd";
5
- import { add as g, cross as _, dot as v, length as y, normalize as b, scale as x, sub as S } from "@margoengine/math/vec3";
4
+ import { anatomyOf as h, ccdLimit as g, jointOf as _ } from "@margoengine/animator/ik";
5
+ import { CCDSolver as v } from "@margoengine/animator/ccd";
6
+ import { add as y, cross as b, dot as x, length as S, normalize as C, scale as w, sub as T } from "@margoengine/math/vec3";
6
7
  //#region src/bar-grip.ts
7
- function C(e, t, r, i, a, o, s, m) {
8
- let C = t.world(t.hand), w = b(_(r, i));
9
- if (y(w) < .99) return;
10
- let T = _(i, w), E = (e, t) => p(S(D(e).position, D(t).position), l(D(t).rotation)), D = (e) => t.world(e), O = t.fingers.index?.bones[0];
8
+ function E(e, t, r, i, a, o, s, m) {
9
+ let h = t.world(t.hand), g = C(b(r, i));
10
+ if (S(g) < .99) return;
11
+ let _ = b(i, g), E = (e, t) => p(T(D(e).position, D(t).position), l(D(t).rotation)), D = (e) => t.world(e), O = t.fingers.index?.bones[0];
11
12
  if (O === void 0) return;
12
- let k = v(p(S(D(O).position, C.position), l(C.rotation)), w), A = o / 2, j = g(a, x(i, A));
13
+ let k = x(p(T(D(O).position, h.position), l(h.rotation)), g), A = o / 2, j = y(a, w(i, A));
13
14
  for (let r of n) {
14
15
  let n = t.fingers[r];
15
- if (!n || n.curl.some((e) => y(e) < .9)) continue;
16
- let [a, o, _] = n.bones;
17
- if (e.parents[a] !== t.hand || e.parents[o] !== a || e.parents[_] !== o) continue;
18
- let C = r === "thumb", O = e.parents.findIndex((e) => e === _), M = O < 0 ? p(S(D(_).position, D(o).position), l(D(_).rotation)) : E(O, _), N = x(b(M), n.lengths[2]), P = [
16
+ if (!n || n.curl.some((e) => S(e) < .9)) continue;
17
+ let [a, o, h] = n.bones;
18
+ if (e.parents[a] !== t.hand || e.parents[o] !== a || e.parents[h] !== o) continue;
19
+ let b = r === "thumb", O = e.parents.findIndex((e) => e === h), M = O < 0 ? p(T(D(h).position, D(o).position), l(D(h).rotation)) : E(O, h), N = w(C(M), n.lengths[2]), P = [
19
20
  E(a, t.hand),
20
21
  E(o, a),
21
- E(_, o)
22
+ E(h, o)
22
23
  ];
23
- if (y(N) < 1e-6 || P.slice(1).some((e) => y(e) < 1e-6)) continue;
24
- let F = new h(P, N), I = n.lengths[1] * (C ? .23 : .28), L = /* @__PURE__ */ new Float64Array(12), R = /* @__PURE__ */ new Float64Array(12);
24
+ if (S(N) < 1e-6 || P.slice(1).some((e) => S(e) < 1e-6)) continue;
25
+ let F = new v(P, N), I = n.lengths[1] * (b ? .23 : .28), L = /* @__PURE__ */ new Float64Array(12), R = /* @__PURE__ */ new Float64Array(12);
25
26
  for (let [e, r] of n.bones.entries()) {
26
27
  let n = f(t.rest.get(r) ?? [
27
28
  0,
@@ -31,9 +32,9 @@ function C(e, t, r, i, a, o, s, m) {
31
32
  ]);
32
33
  F.rest.set(n, e * 4), L.set(m.get(r) ?? n, e * 4), R.set(s.get(r) ?? n, e * 4);
33
34
  }
34
- F.setLimit(0, C ? {
35
+ F.setLimit(0, b ? {
35
36
  type: "cone",
36
- axis: b(P[1]),
37
+ axis: C(P[1]),
37
38
  angle: 1.12,
38
39
  twist: [-.58, .58]
39
40
  } : {
@@ -46,7 +47,7 @@ function C(e, t, r, i, a, o, s, m) {
46
47
  type: "hinge",
47
48
  axis: n.curl[1],
48
49
  min: -.15,
49
- max: C ? 1.16 : 1.97
50
+ max: b ? 1.16 : 1.97
50
51
  }, B = {
51
52
  type: "hinge",
52
53
  axis: n.curl[2],
@@ -55,8 +56,8 @@ function C(e, t, r, i, a, o, s, m) {
55
56
  };
56
57
  F.setLimit(1, z), F.setLimit(2, B), F.tolerance = Math.max(5e-5, I * .018), F.iterations = 28;
57
58
  let V = (e) => {
58
- let t = S(F.positions.subarray(e, e + 3), j);
59
- return [v(t, T), v(t, i)];
59
+ let t = T(F.positions.subarray(e, e + 3), j);
60
+ return [x(t, _), x(t, i)];
60
61
  }, H = () => {
61
62
  let e = 0;
62
63
  for (let t = 0; t < 3; t++) {
@@ -64,9 +65,9 @@ function C(e, t, r, i, a, o, s, m) {
64
65
  e = Math.max(e, A + I - Math.hypot(n[0] + i * s, n[1] + a * s));
65
66
  }
66
67
  return e;
67
- }, U = v(P[0], w), W = C ? k * .7 + U * .3 : U, G = x(w, W - v(j, w)), K = g(j, G), q = (e, t, n, r = 0) => {
68
- let a = g(x(T, Math.sin(e) * (C ? -1 : 1)), x(i, -Math.cos(e)));
69
- return F.rotations.set(n), F.solve(g(g(K, x(w, r)), x(a, A + I + t))), F.rotations.slice();
68
+ }, U = x(P[0], g), W = b ? k * .7 + U * .3 : U, G = w(g, W - x(j, g)), K = y(j, G), q = (e, t, n, r = 0) => {
69
+ let a = y(w(_, Math.sin(e) * (b ? -1 : 1)), w(i, -Math.cos(e)));
70
+ return F.rotations.set(n), F.solve(y(y(K, w(g, r)), w(a, A + I + t))), F.rotations.slice();
70
71
  }, J = (e, t) => {
71
72
  let r = 0;
72
73
  for (let n of [
@@ -86,13 +87,13 @@ function C(e, t, r, i, a, o, s, m) {
86
87
  F.update(), r = Math.max(r, H());
87
88
  }
88
89
  let i = V(9), a = 0;
89
- if (C) for (let r of [1, 2]) {
90
- let i = r * 4, o = d(l(e.subarray(i, i + 4)), t.subarray(i, i + 4)), s = 2 * Math.atan2(v(o.slice(0, 3), n.curl[r]), o[3]);
90
+ if (b) for (let r of [1, 2]) {
91
+ let i = r * 4, o = d(l(e.subarray(i, i + 4)), t.subarray(i, i + 4)), s = 2 * Math.atan2(x(o.slice(0, 3), n.curl[r]), o[3]);
91
92
  a += I * 16 * Math.max(0, .16 - Math.atan2(Math.sin(s), Math.cos(s)));
92
93
  }
93
- return Math.abs(Math.hypot(...i) - A - I) + 16 * r + a + Math.max(0, C ? i[0] : -i[0]);
94
+ return Math.abs(Math.hypot(...i) - A - I) + 16 * r + a + Math.max(0, b ? i[0] : -i[0]);
94
95
  }, Y = L.slice(), X = R, Z = J(R, L);
95
- for (let e of C ? [
96
+ for (let e of b ? [
96
97
  .4,
97
98
  .68,
98
99
  .95,
@@ -121,7 +122,7 @@ function C(e, t, r, i, a, o, s, m) {
121
122
  a + 1e-6 < Z && (Z = a, Y = t, X = i);
122
123
  }
123
124
  }
124
- if (C) for (let e of [
125
+ if (b) for (let e of [
125
126
  .4,
126
127
  .68,
127
128
  .95,
@@ -174,58 +175,58 @@ function C(e, t, r, i, a, o, s, m) {
174
175
  }
175
176
  //#endregion
176
177
  //#region src/hinged-contact.ts
177
- function w(e, t) {
178
+ function D(e, t) {
178
179
  return m([
179
180
  ...e,
180
- ..._(t, e),
181
+ ...b(t, e),
181
182
  ...t
182
183
  ]);
183
184
  }
184
- function T(e, t, n, r, i) {
185
+ function O(e, t, n, r, i) {
185
186
  let a = e.joints - 1, o = a - 1, s = a - 2, u = (t) => [
186
187
  e.positions[t * 3],
187
188
  e.positions[t * 3 + 1],
188
189
  e.positions[t * 3 + 2]
189
- ], f = (t) => e.worldRotations.slice(t * 4, t * 4 + 4), m = (t) => e.rest.subarray(t * 4, t * 4 + 4), h = (t) => b(e.offsets.subarray(t * 3, t * 3 + 3)), C = h(o), T = h(a), E = b(i.axis), D = p(E, m(o)), O = p(T, m(o));
190
- if (Math.abs(v(D, C)) > 1e-5 || Math.abs(v(D, O)) > 1e-5 || i.twist && v(b(i.twist.axis), T) < .99999) throw RangeError("Hinged contact needs a bend axis perpendicular to both links and forearm roll along the last link");
191
- let k = Math.atan2(v(D, _(C, O)), v(C, O)), A = Math.max(1e-5, k + (i.min ?? -Math.PI)), j = Math.min(Math.PI - 1e-5, k + (i.max ?? Math.PI));
190
+ ], f = (t) => e.worldRotations.slice(t * 4, t * 4 + 4), m = (t) => e.rest.subarray(t * 4, t * 4 + 4), h = (t) => C(e.offsets.subarray(t * 3, t * 3 + 3)), g = h(o), _ = h(a), v = C(i.axis), E = p(v, m(o)), O = p(_, m(o));
191
+ if (Math.abs(x(E, g)) > 1e-5 || Math.abs(x(E, O)) > 1e-5 || i.twist && x(C(i.twist.axis), _) < .99999) throw RangeError("Hinged contact needs a bend axis perpendicular to both links and forearm roll along the last link");
192
+ let k = Math.atan2(x(E, b(g, O)), x(g, O)), A = Math.max(1e-5, k + (i.min ?? -Math.PI)), j = Math.min(Math.PI - 1e-5, k + (i.max ?? Math.PI));
192
193
  if (j < A) throw RangeError("Hinged contact needs a flexion range that intersects the positive bend branch");
193
194
  e.projectRotations(), e.update();
194
- let M = u(s), N = u(o), P = u(a), F = y(S(N, M)), I = y(S(P, N)), L = S(t, M), R = y(L), z = R < 1e-8 ? b(S(P, M)) : x(L, 1 / R);
195
+ let M = u(s), N = u(o), P = u(a), F = S(T(N, M)), I = S(T(P, N)), L = T(t, M), R = S(L), z = R < 1e-8 ? C(T(P, M)) : w(L, 1 / R);
195
196
  if (F < 1e-8 || I < 1e-8) throw RangeError("Hinged contact needs two links with nonzero lengths");
196
- let B = b(S(r, M)), V = _(B, z);
197
- if (y(V) < 1e-6) throw RangeError("Choose a hinged contact pole off the shoulder-to-target line to define a bend plane");
198
- let H = (e) => Math.sqrt(Math.max(0, F * F + I * I + 2 * F * I * Math.cos(e))), U = Math.max(H(j), Math.min(H(A), R)), W = b(V), G = (F * F - I * I + U * U) / (2 * U), K = Math.sqrt(Math.max(0, F * F - G * G)), q = b(g(x(z, G), x(_(z, W), K))), J = (U * U - F * F - I * I) / (2 * F * I), Y = Math.acos(Math.max(-1, Math.min(1, J))), X = d(w(q, W), l(w(C, D))), Z = s === 0 ? e.parentRotation : f(s - 1), Q = d(m(o), c(E, Y - k)), ee = d(X, Q), $ = 0;
197
+ let B = C(T(r, M)), V = b(B, z);
198
+ if (S(V) < 1e-6) throw RangeError("Choose a hinged contact pole off the shoulder-to-target line to define a bend plane");
199
+ let H = (e) => Math.sqrt(Math.max(0, F * F + I * I + 2 * F * I * Math.cos(e))), U = Math.max(H(j), Math.min(H(A), R)), W = C(V), G = (F * F - I * I + U * U) / (2 * U), K = Math.sqrt(Math.max(0, F * F - G * G)), q = C(y(w(z, G), w(b(z, W), K))), J = (U * U - F * F - I * I) / (2 * F * I), Y = Math.acos(Math.max(-1, Math.min(1, J))), X = d(D(q, W), l(D(g, E))), Z = s === 0 ? e.parentRotation : f(s - 1), Q = d(m(o), c(v, Y - k)), ee = d(X, Q), te = 0;
199
200
  if (i.twist) {
200
- let e = d(l(ee), d(n, l(m(a)))), t = 2 * Math.atan2(v(e.slice(0, 3), T), e[3]), r = Math.atan2(Math.sin(t), Math.cos(t));
201
- $ = Math.max(i.twist.range[0], Math.min(i.twist.range[1], r));
201
+ let e = d(l(ee), d(n, l(m(a)))), t = 2 * Math.atan2(x(e.slice(0, 3), _), e[3]), r = Math.atan2(Math.sin(t), Math.cos(t));
202
+ te = Math.max(i.twist.range[0], Math.min(i.twist.range[1], r));
202
203
  }
203
- let te = d(Q, c(T, $)), ne = d(X, te);
204
- e.rotations.set(d(l(Z), X), s * 4), e.rotations.set(te, o * 4), e.rotations.set(d(l(ne), n), a * 4), e.projectRotations(), e.update();
204
+ let $ = d(Q, c(_, te)), ne = d(X, $);
205
+ e.rotations.set(d(l(Z), X), s * 4), e.rotations.set($, o * 4), e.rotations.set(d(l(ne), n), a * 4), e.projectRotations(), e.update();
205
206
  }
206
207
  //#endregion
207
208
  //#region src/limb-collision.ts
208
- var E = [
209
+ var k = [
209
210
  0,
210
211
  0,
211
212
  0
212
- ], D = (e, t) => [
213
+ ], A = (e, t) => [
213
214
  e[t],
214
215
  e[t + 1],
215
216
  e[t + 2]
216
217
  ];
217
- function O(e) {
218
+ function j(e) {
218
219
  return e?.length === 3 && Number.isFinite(e[0]) && Number.isFinite(e[1]) && Number.isFinite(e[2]);
219
220
  }
220
- function k(e, t, n) {
221
- let r = v(e, t) + y(e) * n;
221
+ function M(e, t, n) {
222
+ let r = x(e, t) + S(e) * n;
222
223
  if (!Number.isFinite(r)) throw RangeError("Give the direction limit finite vectors at scene scale");
223
224
  return r;
224
225
  }
225
- function A(e, t, n, r) {
226
- let i = D(e, n), a = D(t, n), o = D(e, n + 3), s = D(t, n + 3);
227
- if (Math.abs(y(o) - 1) > 1e-9 || Math.abs(y(s) - 1) > 1e-9) throw RangeError("Direction samples need unit body axes");
228
- let c = b(o), l = b(s), u = _(c, l), d = y(u), f = v(c, l), p = Math.atan2(d, f), m = _(b(u), c), h = S(a, i), g = (1 + r) * y(h) + p * Math.max(y(i), y(a));
226
+ function N(e, t, n, r) {
227
+ let i = A(e, n), a = A(t, n), o = A(e, n + 3), s = A(t, n + 3);
228
+ if (Math.abs(S(o) - 1) > 1e-9 || Math.abs(S(s) - 1) > 1e-9) throw RangeError("Direction samples need unit body axes");
229
+ let c = C(o), l = C(s), u = b(c, l), d = S(u), f = x(c, l), p = Math.atan2(d, f), m = b(C(u), c), h = T(a, i), g = (1 + r) * S(h) + p * Math.max(S(i), S(a));
229
230
  if (!Number.isFinite(g)) throw RangeError("Give the direction sweep finite motion at scene scale");
230
231
  return {
231
232
  start: i,
@@ -238,22 +239,22 @@ function A(e, t, n, r) {
238
239
  opposed: d <= 1e-12 && f < 0
239
240
  };
240
241
  }
241
- function j(e, t) {
242
+ function P(e, t) {
242
243
  let n = e.arc * t;
243
- return k(g(e.start, x(e.delta, t)), g(x(e.axis, Math.cos(n)), x(e.tangent, Math.sin(n))), e.allowance);
244
+ return M(y(e.start, w(e.delta, t)), y(w(e.axis, Math.cos(n)), w(e.tangent, Math.sin(n))), e.allowance);
244
245
  }
245
- function M(e, t, n) {
246
- return y(S(e, x(t, Math.max(0, Math.min(n, v(e, t))))));
246
+ function F(e, t, n) {
247
+ return S(T(e, w(t, Math.max(0, Math.min(n, x(e, t))))));
247
248
  }
248
- function N(e, t, n, r) {
249
- let i = S(t, e), a = S(r, n), o = S(n, e), s = b(i), c = b(a), l = y(i), u = y(a), d = Math.min(M(x(o, -1), c, u), M(S(i, o), c, u), M(o, s, l), M(S(r, e), s, l)), f = _(s, c), p = v(f, f);
249
+ function I(e, t, n, r) {
250
+ let i = T(t, e), a = T(r, n), o = T(n, e), s = C(i), c = C(a), l = S(i), u = S(a), d = Math.min(F(w(o, -1), c, u), F(T(i, o), c, u), F(o, s, l), F(T(r, e), s, l)), f = b(s, c), p = x(f, f);
250
251
  if (p > 0) {
251
- let e = v(_(o, c), f) / p, t = v(_(o, s), f) / p;
252
- e >= 0 && e <= l && t >= 0 && t <= u && (d = Math.min(d, Math.abs(v(o, f)) / Math.sqrt(p)));
252
+ let e = x(b(o, c), f) / p, t = x(b(o, s), f) / p;
253
+ e >= 0 && e <= l && t >= 0 && t <= u && (d = Math.min(d, Math.abs(x(o, f)) / Math.sqrt(p)));
253
254
  }
254
255
  return d;
255
256
  }
256
- function P(e, t, n, r) {
257
+ function L(e, t, n, r) {
257
258
  let { position: i, linear: a } = e, o = t * 9;
258
259
  return [
259
260
  a[o] * n[0] + a[o + 3] * n[1] + a[o + 6] * n[2] + (r ? i[t * 3] : 0),
@@ -261,19 +262,19 @@ function P(e, t, n, r) {
261
262
  a[o + 2] * n[0] + a[o + 5] * n[1] + a[o + 8] * n[2] + (r ? i[t * 3 + 2] : 0)
262
263
  ];
263
264
  }
264
- var F = class {
265
+ var R = class {
265
266
  constructor(t, n, r, i = []) {
266
267
  if (this.axes = [], this.posed = !1, this.clearance = (e) => {
267
268
  this.packLimb(e, this.candidate);
268
269
  let t = this.measure(this.body, this.candidate);
269
270
  return this.limits.forEach(({ start: n, end: r, maxBackwards: i }, a) => {
270
- let o = S(D(e, r * 3), D(e, n * 3));
271
- t = Math.min(t, k(o, this.axes[a], Math.sin(i)));
271
+ let o = T(A(e, r * 3), A(e, n * 3));
272
+ t = Math.min(t, M(o, this.axes[a], Math.sin(i)));
272
273
  }), t;
273
274
  }, !t.length || t.length > 65536 || t.some((e) => !Number.isInteger(e) || e < -1 || e >= t.length)) throw RangeError("Limb collision needs 1–65,536 joints with valid parent indices or -1");
274
275
  let a = (e) => Number.isInteger(e) && e >= 0 && e < t.length, o = (e) => Number.isInteger(e) && e >= 0 && e < 65536, s = (e) => {
275
- let t = e.offset ?? E;
276
- if (!a(e.joint) || !O(t)) throw RangeError("Body collision landmarks need a valid bone and a finite local XYZ offset");
276
+ let t = e.offset ?? k;
277
+ if (!a(e.joint) || !j(t)) throw RangeError("Body collision landmarks need a valid bone and a finite local XYZ offset");
277
278
  return {
278
279
  joint: e.joint,
279
280
  offset: [...t]
@@ -296,12 +297,12 @@ var F = class {
296
297
  range: [...r]
297
298
  };
298
299
  }), this.limits = i.map(({ start: e, end: t, reference: n, forward: r, maxBackwards: i = .75 }) => {
299
- if (!(o(e) && o(t) && e !== t && a(n) && O(r) && y(r) > 0 && i >= 0 && i <= Math.PI / 2)) throw RangeError("Direction limits need distinct valid slots, a reference bone, a forward axis and an angle from 0 to π/2");
300
+ if (!(o(e) && o(t) && e !== t && a(n) && j(r) && S(r) > 0 && i >= 0 && i <= Math.PI / 2)) throw RangeError("Direction limits need distinct valid slots, a reference bone, a forward axis and an angle from 0 to π/2");
300
301
  return {
301
302
  start: e,
302
303
  end: t,
303
304
  reference: n,
304
- forward: b(r),
305
+ forward: C(r),
305
306
  maxBackwards: i
306
307
  };
307
308
  }), this.slots = [...this.links, ...this.limits].reduce((e, { start: t, end: n }) => Math.max(e, t, n), 0) + 1, this.body = new Float64Array(this.volumes.length * 6), this.candidate = new Float64Array(this.links.length * 6);
@@ -309,15 +310,15 @@ var F = class {
309
310
  setBodyPose(e) {
310
311
  if (e.length !== this.parents.length * 10) throw RangeError("A collision pose needs ten local transform components per joint");
311
312
  for (let t = 0; t < e.length; t++) if (!Number.isFinite(e[t])) throw RangeError("Give the collision rig finite translation, quaternion and scale values");
312
- let n = t(this.parents, e, this.order), r = new Float64Array(this.body.length), i = (e) => P(n, e.joint, e.offset ?? E, !0);
313
+ let n = t(this.parents, e, this.order), r = new Float64Array(this.body.length), i = (e) => L(n, e.joint, e.offset ?? k, !0);
313
314
  this.volumes.forEach(({ start: e, end: t }, n) => {
314
315
  let a = i(e), o = t ? i(t) : a;
315
- if (!O(a) || !O(o)) throw RangeError("Collision body landmarks overflowed; use finite transforms at scene scale");
316
+ if (!j(a) || !j(o)) throw RangeError("Collision body landmarks overflowed; use finite transforms at scene scale");
316
317
  r.set(a, n * 6), r.set(o, n * 6 + 3);
317
318
  });
318
319
  let a = this.limits.map(({ reference: e, forward: t }) => {
319
- let r = b(P(n, e, t, !1));
320
- if (y(r) < .9) throw RangeError("A collision direction needs a non-degenerate reference frame");
320
+ let r = C(L(n, e, t, !1));
321
+ if (S(r) < .9) throw RangeError("A collision direction needs a non-degenerate reference frame");
321
322
  return r;
322
323
  });
323
324
  this.body.set(r), this.axes = a, this.posed = !0;
@@ -331,8 +332,8 @@ var F = class {
331
332
  if (!this.limits.length) return t;
332
333
  let n = new Float64Array(this.limits.length * 6);
333
334
  return this.limits.forEach(({ start: t, end: r }, i) => {
334
- let a = S(D(e, r * 3), D(e, t * 3));
335
- if (!O(a)) throw RangeError("Give the direction sample finite joint displacements at scene scale");
335
+ let a = T(A(e, r * 3), A(e, t * 3));
336
+ if (!j(a)) throw RangeError("Give the direction sample finite joint displacements at scene scale");
336
337
  n.set(a, i * 6), n.set(this.axes[i], i * 6 + 3);
337
338
  }), {
338
339
  ...t,
@@ -345,9 +346,9 @@ var F = class {
345
346
  let i = e.directions, a = t.directions;
346
347
  if (!!i != !!a) throw RangeError("Pass direction data in both sweep snapshots, or two legacy snapshots");
347
348
  for (let e of [i, a]) if (e && (e.length !== this.limits.length * 6 || !e.every(Number.isFinite))) throw RangeError("Sweep direction samples must match the rig layout and contain finite vectors");
348
- let o = i && a ? this.limits.map(({ maxBackwards: e }, t) => A(i, a, t * 6, Math.sin(e))) : [], s = o.some((e) => e.opposed), c = e.body.slice(), l = e.limb.slice(), u = t.body.map((t, n) => t - e.body[n]), d = t.limb.map((t, n) => t - e.limb[n]), f = D(u, 0), p = (e) => {
349
+ let o = i && a ? this.limits.map(({ maxBackwards: e }, t) => N(i, a, t * 6, Math.sin(e))) : [], s = o.some((e) => e.opposed), c = e.body.slice(), l = e.limb.slice(), u = t.body.map((t, n) => t - e.body[n]), d = t.limb.map((t, n) => t - e.limb[n]), f = A(u, 0), p = (e) => {
349
350
  let t = 0;
350
- for (let n = 0; n < e.length; n += 3) t = Math.max(t, y(S(D(e, n), f)));
351
+ for (let n = 0; n < e.length; n += 3) t = Math.max(t, S(T(A(e, n), f)));
351
352
  return t;
352
353
  }, m = p(u) + p(d);
353
354
  if (!Number.isFinite(m)) throw RangeError("Give the collision sweep finite motion at scene scale");
@@ -355,7 +356,7 @@ var F = class {
355
356
  for (let t = 0; t < r; t++) {
356
357
  let t = this.measure(c, l), r = t, i = m > 0 ? (t - n * .5) / m : Infinity;
357
358
  for (let e of o) {
358
- let t = j(e, h);
359
+ let t = P(e, h);
359
360
  r = Math.min(r, t), e.speed > 0 && (i = Math.min(i, (t - n * .5) / e.speed));
360
361
  }
361
362
  if (g = Math.min(g, r), r <= n) return {
@@ -399,9 +400,9 @@ var F = class {
399
400
  measure(e, t) {
400
401
  let n = Infinity;
401
402
  return this.links.forEach(({ radius: r }, i) => {
402
- let a = D(t, i * 6), o = D(t, i * 6 + 3);
403
+ let a = A(t, i * 6), o = A(t, i * 6 + 3);
403
404
  this.volumes.forEach((t, i) => {
404
- let s = N(a, o, D(e, i * 6), D(e, i * 6 + 3)) - r - t.radius;
405
+ let s = I(a, o, A(e, i * 6), A(e, i * 6 + 3)) - r - t.radius;
405
406
  if (!Number.isFinite(s)) throw RangeError("Collision distance overflowed; use finite positions at scene scale");
406
407
  n = Math.min(n, s);
407
408
  });
@@ -410,35 +411,35 @@ var F = class {
410
411
  };
411
412
  //#endregion
412
413
  //#region src/contact.ts
413
- function I(e, t) {
414
+ function z(e, t) {
414
415
  if (!(e?.length === 4 && e.every(Number.isFinite) && Math.hypot(...e) > 0)) throw RangeError(`Contact IK ${t} needs a finite, nonzero quaternion`);
415
416
  return f(e);
416
417
  }
417
- function L(n, r) {
418
- let { parents: i, local: a } = n, { joints: o, point: s, anchor: c, rotation: l, pole: d, limits: f = [], clearance: m } = r, { weight: _ = 1, iterations: v = 64, tolerance: y = 1e-4, angleTolerance: b = .02 } = r;
418
+ function B(n, r) {
419
+ let { parents: i, local: a } = n, { joints: o, point: s, anchor: c, rotation: l, pole: d, clearance: f } = r, { weight: m = 1, iterations: b = 64, tolerance: x = 1e-4, angleTolerance: S = .02 } = r;
419
420
  if (!Array.isArray(i) || !(a instanceof Float64Array) || a.length !== i.length * 10) throw TypeError("Contact IK needs a pose with parents and 10 local values per joint");
420
421
  if (!Array.isArray(o) || o.length < 2 || o.length > 256) throw TypeError("Contact IK needs a chain of 2 to 256 joints");
421
422
  if (!o.every((e, t) => Number.isInteger(e) && e >= 0 && e < i.length && (t === 0 || i[e] === o[t - 1]))) throw RangeError("Contact IK joints must form a parent-child chain");
422
423
  if (i.some((e) => !Number.isInteger(e) || e < -1 || e >= i.length)) throw RangeError("Contact IK parents must be valid joint indices or -1");
423
424
  if (!(s?.length === 3 && c?.length === 3 && [...s, ...c].every(Number.isFinite))) throw RangeError("Contact IK point and anchor need three finite coordinates each");
424
- if (!(_ >= 0 && _ <= 1 && y > 0 && Number.isFinite(y) && b > 0 && b <= Math.PI && Number.isInteger(v) && v >= 1 && v <= 1024)) throw RangeError("Contact IK weight, tolerance or iteration count is out of range; use the supported ranges");
425
- let C = e(i, "Contact IK"), w = o.at(-1), E = w * 10, D = I(l, "rotation"), O = i[o[0]], k = [
426
- c[0] * a[E + 7],
427
- c[1] * a[E + 8],
428
- c[2] * a[E + 9]
429
- ], A = 1, j = 1;
430
- for (let e = i[w]; e >= 0; e = i[e]) {
425
+ if (!(m >= 0 && m <= 1 && x > 0 && Number.isFinite(x) && S > 0 && S <= Math.PI && Number.isInteger(b) && b >= 1 && b <= 1024)) throw RangeError("Contact IK weight, tolerance or iteration count is out of range; use the supported ranges");
426
+ let C = e(i, "Contact IK"), E = o.at(-1), D = E * 10, k = z(l, "rotation"), A = i[o[0]], j = [
427
+ c[0] * a[D + 7],
428
+ c[1] * a[D + 8],
429
+ c[2] * a[D + 9]
430
+ ], M = 1, N = 1;
431
+ for (let e = i[E]; e >= 0; e = i[e]) {
431
432
  let t = e * 10, n = a[t + 7];
432
433
  if (!(n > 0 && Number.isFinite(n) && a[t + 8] === n && a[t + 9] === n)) throw RangeError("Contact IK needs uniform positive ancestor scales; apply scale to the mesh before rigging");
433
- A *= n;
434
+ M *= n;
434
435
  }
435
- for (let e = O; e >= 0; e = i[e]) j *= a[e * 10 + 7];
436
- if (!(Number.isFinite(A) && A > 0 && k.every(Number.isFinite) && a.subarray(E + 7, E + 10).every((e) => e > 0 && Number.isFinite(e)))) throw RangeError("Contact IK needs finite positive scales");
437
- let M = x(k, A), N = t(i, a, C), P = N.rotation.subarray(w * 4, w * 4 + 4), F = g(N.position.subarray(w * 3, w * 3 + 3), p(M, P)), L = () => B(F, P, s, D, y, b), V = () => R(m, o.flatMap((e) => Array.from(N.position.subarray(e * 3, e * 3 + 3))), o.flatMap((e) => Array.from(N.rotation.subarray(e * 4, e * 4 + 4))));
438
- if (_ === 0) {
439
- let e = L();
440
- if (!m) return e;
441
- let t = V();
436
+ for (let e = A; e >= 0; e = i[e]) N *= a[e * 10 + 7];
437
+ if (!(Number.isFinite(M) && M > 0 && j.every(Number.isFinite) && a.subarray(D + 7, D + 10).every((e) => e > 0 && Number.isFinite(e)))) throw RangeError("Contact IK needs finite positive scales");
438
+ let P = w(j, M), F = t(i, a, C), I = F.rotation.subarray(E * 4, E * 4 + 4), L = y(F.position.subarray(E * 3, E * 3 + 3), p(P, I)), R = () => U(L, I, s, k, x, S), B = () => V(f, o.flatMap((e) => Array.from(F.position.subarray(e * 3, e * 3 + 3))), o.flatMap((e) => Array.from(F.rotation.subarray(e * 4, e * 4 + 4))));
439
+ if (m === 0) {
440
+ let e = R();
441
+ if (!f) return e;
442
+ let t = B();
442
443
  return {
443
444
  ...e,
444
445
  locked: e.locked && t >= 0,
@@ -446,7 +447,7 @@ function L(n, r) {
446
447
  clearance: t
447
448
  };
448
449
  }
449
- let H = new h(o.map((e) => {
450
+ let W = new v(o.map((e) => {
450
451
  let t = e * 10;
451
452
  return [
452
453
  a[t],
@@ -454,60 +455,66 @@ function L(n, r) {
454
455
  a[t + 2]
455
456
  ];
456
457
  }));
457
- H.iterations = v, H.tolerance = y, H.angleTolerance = b, O >= 0 && (H.parentPosition.set(N.position.subarray(O * 3, O * 3 + 3)), H.parentRotation.set(N.rotation.subarray(O * 4, O * 4 + 4)), H.parentScale[0] = j), o.forEach((e, t) => {
458
+ W.iterations = b, W.tolerance = x, W.angleTolerance = S, A >= 0 && (W.parentPosition.set(F.position.subarray(A * 3, A * 3 + 3)), W.parentRotation.set(F.rotation.subarray(A * 4, A * 4 + 4)), W.parentScale[0] = N), o.forEach((e, t) => {
458
459
  let n = e * 10;
459
- H.rotations.set(a.subarray(n + 3, n + 7), t * 4), e !== w && (H.scales[t] = a[n + 7]);
460
+ W.rotations.set(a.subarray(n + 3, n + 7), t * 4), e !== E && (W.scales[t] = a[n + 7]);
460
461
  });
461
- let U = /* @__PURE__ */ new Set();
462
- for (let { joint: e, neutral: t, limit: n } of f) {
462
+ let G = r.body === null ? null : r.body ?? n.body ?? h(n), K = r.limits ?? (G?.limits ? o.flatMap((e) => {
463
+ let t = _(G, e);
464
+ return t ? [{
465
+ joint: e,
466
+ ...g(t)
467
+ }] : [];
468
+ }) : []), q = /* @__PURE__ */ new Set();
469
+ for (let { joint: e, neutral: t, limit: n } of K) {
463
470
  let r = o.indexOf(e);
464
- if (r < 0 || U.has(e)) throw RangeError("Contact IK limits must name distinct joints in the chain");
465
- U.add(e), H.rest.set(I(t, "neutral rotation"), r * 4), H.setLimit(r, n);
471
+ if (r < 0 || q.has(e)) throw RangeError("Contact IK limits must name distinct joints in the chain");
472
+ q.add(e), W.rest.set(z(t, "neutral rotation"), r * 4), W.setLimit(r, n);
466
473
  }
467
- let W = u(P, D, _, .9995), G = g(F, x(S(s, F), _)), K = S(G, p(M, W)), q;
474
+ let J = u(I, k, m, .9995), Y = y(L, w(T(s, L), m)), X = T(Y, p(P, J)), Z;
468
475
  if (r.reach === "hinged") {
469
- let e = f.find(({ joint: e }) => e === o[o.length - 2])?.limit;
476
+ let e = K.find(({ joint: e }) => e === o[o.length - 2])?.limit;
470
477
  if (o.length < 3 || !d || e?.type !== "hinge") throw RangeError("Hinged contact requires three joints or more, an explicit pole and a penultimate hinge limit");
471
- if (T(H, K, W, d, e), m && (q = R(m, H.positions.subarray(0, H.joints * 3), H.worldRotations), q < 0)) return {
472
- ...L(),
478
+ if (O(W, X, J, d, e), f && (Z = V(f, W.positions.subarray(0, W.joints * 3), W.worldRotations), Z < 0)) return {
479
+ ...R(),
473
480
  locked: !1,
474
481
  blocked: !0,
475
- clearance: V()
482
+ clearance: B()
476
483
  };
477
- } else if (m) {
484
+ } else if (f) {
478
485
  let e = o[o.length - 2] * 3;
479
- if (q = z(H, K, W, d ?? [
480
- N.position[e],
481
- N.position[e + 1],
482
- N.position[e + 2]
483
- ], m), q === void 0) return {
484
- ...L(),
486
+ if (Z = H(W, X, J, d ?? [
487
+ F.position[e],
488
+ F.position[e + 1],
489
+ F.position[e + 2]
490
+ ], f), Z === void 0) return {
491
+ ...R(),
485
492
  locked: !1,
486
493
  blocked: !0,
487
- clearance: V()
494
+ clearance: B()
488
495
  };
489
- } else H.solve(K, W, d);
490
- let J = o.length - 1, Y = H.worldRotations.subarray(J * 4, J * 4 + 4), X = g(H.positions.subarray(J * 3, J * 3 + 3), p(M, Y));
491
- o.forEach((e, t) => a.set(H.rotations.subarray(t * 4, t * 4 + 4), e * 10 + 3));
492
- let Z = B(X, Y, s, D, y, b);
493
- return q === void 0 ? Z : {
494
- ...Z,
496
+ } else W.solve(X, J, d);
497
+ let Q = o.length - 1, ee = W.worldRotations.subarray(Q * 4, Q * 4 + 4), te = y(W.positions.subarray(Q * 3, Q * 3 + 3), p(P, ee));
498
+ o.forEach((e, t) => a.set(W.rotations.subarray(t * 4, t * 4 + 4), e * 10 + 3));
499
+ let $ = U(te, ee, s, k, x, S);
500
+ return Z === void 0 ? $ : {
501
+ ...$,
495
502
  blocked: !1,
496
- clearance: q
503
+ clearance: Z
497
504
  };
498
505
  }
499
- function R(e, t, n) {
506
+ function V(e, t, n) {
500
507
  let r = e(t, n);
501
508
  if (!Number.isFinite(r) && r !== Infinity) throw RangeError("Contact clearance must return a signed distance, or positive infinity when no obstacle applies");
502
509
  return r;
503
510
  }
504
- function z(e, t, n, r, i) {
505
- let a = e.positions.subarray(0, e.joints * 3), o = e.solve(t, n, r), s = R(i, a, e.worldRotations);
511
+ function H(e, t, n, r, i) {
512
+ let a = e.positions.subarray(0, e.joints * 3), o = e.solve(t, n, r), s = V(i, a, e.worldRotations);
506
513
  if (s >= 0 && o.reached) return s;
507
- let u = Math.max(0, e.joints - 3), f = a.slice(u * 3, u * 3 + 3), m = b(S(t, f));
508
- if (y(m) < .9) return s >= 0 ? s : void 0;
509
- let h = e.rotations.slice(), v = (e.joints - 1) * 4, x = u * 4, C = h.subarray(x, x + 4), w = u === 0 ? e.parentRotation : e.worldRotations.slice(x - 4, x), T = l(w), E = e.worldRotations.slice(v - 4, v), D = e.worldRotations.slice(v, v + 4), O = S(r, f);
510
- y(_(m, O)) < 1e-6 && (O = _(m, Math.abs(m[1]) < .9 ? [
514
+ let u = Math.max(0, e.joints - 3), f = a.slice(u * 3, u * 3 + 3), m = C(T(t, f));
515
+ if (S(m) < .9) return s >= 0 ? s : void 0;
516
+ let h = e.rotations.slice(), g = (e.joints - 1) * 4, _ = u * 4, v = h.subarray(_, _ + 4), x = u === 0 ? e.parentRotation : e.worldRotations.slice(_ - 4, _), w = l(x), E = e.worldRotations.slice(g - 4, g), D = e.worldRotations.slice(g, g + 4), O = T(r, f);
517
+ S(b(m, O)) < 1e-6 && (O = b(m, Math.abs(m[1]) < .9 ? [
511
518
  0,
512
519
  1,
513
520
  0
@@ -536,22 +543,22 @@ function z(e, t, n, r, i) {
536
543
  ]) {
537
544
  let o = c(m, r);
538
545
  e.rotations.set(h);
539
- let s = d(T, d(o, w));
540
- e.rotations.set(d(s, C), x), e.rotations.set(d(l(d(o, E)), D), v);
541
- let u = e.solve(t, n, g(f, p(O, o))), _ = R(i, a, e.worldRotations);
542
- if (_ < 0) continue;
543
- if (u.reached) return _;
544
- let y = k(u.distance, u.angle);
545
- (!A || y < A.distance) && (A = {
546
- distance: y,
547
- gap: _,
546
+ let s = d(w, d(o, x));
547
+ e.rotations.set(d(s, v), _), e.rotations.set(d(l(d(o, E)), D), g);
548
+ let u = e.solve(t, n, y(f, p(O, o))), b = V(i, a, e.worldRotations);
549
+ if (b < 0) continue;
550
+ if (u.reached) return b;
551
+ let S = k(u.distance, u.angle);
552
+ (!A || S < A.distance) && (A = {
553
+ distance: S,
554
+ gap: b,
548
555
  pose: e.rotations.slice()
549
556
  });
550
557
  }
551
558
  if (A) return e.rotations.set(A.pose), e.update(), A.gap;
552
559
  }
553
- function B(e, t, n, r, i, a) {
554
- let o = y(S(e, n)), s = t[0] * r[0] + t[1] * r[1] + t[2] * r[2] + t[3] * r[3], c = 2 * Math.acos(Math.min(1, Math.abs(s)));
560
+ function U(e, t, n, r, i, a) {
561
+ let o = S(T(e, n)), s = t[0] * r[0] + t[1] * r[1] + t[2] * r[2] + t[3] * r[3], c = 2 * Math.acos(Math.min(1, Math.abs(s)));
555
562
  return {
556
563
  locked: o <= i && c <= a,
557
564
  distance: o,
@@ -559,39 +566,39 @@ function B(e, t, n, r, i, a) {
559
566
  point: e
560
567
  };
561
568
  }
562
- function V(e, t) {
563
- let n = b(t), r = b(S(e, x(n, v(e, n))));
564
- if (y(n) < .9 || y(r) < .9 || y(_(b(e), n)) < 1e-5) throw RangeError("Palm orientation needs a nonzero normal and a finger direction across that normal");
569
+ function W(e, t) {
570
+ let n = C(t), r = C(T(e, w(n, x(e, n))));
571
+ if (S(n) < .9 || S(r) < .9 || S(b(C(e), n)) < 1e-5) throw RangeError("Palm orientation needs a nonzero normal and a finger direction across that normal");
565
572
  return f(m([
566
573
  ...r,
567
574
  ...n,
568
- ..._(r, n)
575
+ ...b(r, n)
569
576
  ]));
570
577
  }
571
- function H(e, t) {
578
+ function G(e, t) {
572
579
  if (e?.length !== 3 || !e.every(Number.isFinite)) throw RangeError(`${t} needs three finite coordinates`);
573
580
  }
574
- function U(e, t) {
581
+ function K(e, t) {
575
582
  let n = t * 10 + 3;
576
- return I([
583
+ return z([
577
584
  e.rest[n],
578
585
  e.rest[n + 1],
579
586
  e.rest[n + 2],
580
587
  e.rest[n + 3]
581
588
  ], "local rest rotation");
582
589
  }
583
- function W(e, t, n, r) {
590
+ function q(e, t, n, r) {
584
591
  let { joint: i, flexDirection: a, min: o = -.035, max: s = 2.55, roll: c = [-1.25, 1.25] } = r, u = n.indexOf(i);
585
592
  if (u <= 0 || u >= n.length - 1) throw RangeError("Elbow calibration needs an intermediate joint of the arm chain");
586
- H(a, "Elbow flexion direction");
587
- let d = t.world(i), f = b(S(t.world(t.hand).position, d.position)), m = _(f, b(a)), h = l(d.rotation);
588
- if (y(m) < 1e-5) throw RangeError("Elbow flexion needs a direction across the forearm");
593
+ G(a, "Elbow flexion direction");
594
+ let d = t.world(i), f = C(T(t.world(t.hand).position, d.position)), m = b(f, C(a)), h = l(d.rotation);
595
+ if (S(m) < 1e-5) throw RangeError("Elbow flexion needs a direction across the forearm");
589
596
  return {
590
597
  joint: i,
591
- neutral: U(e, i),
598
+ neutral: K(e, i),
592
599
  limit: {
593
600
  type: "hinge",
594
- axis: p(b(m), h),
601
+ axis: p(C(m), h),
595
602
  min: o,
596
603
  max: s,
597
604
  twist: c === !1 ? void 0 : {
@@ -601,64 +608,64 @@ function W(e, t, n, r) {
601
608
  }
602
609
  };
603
610
  }
604
- var G = class {
611
+ var J = class {
605
612
  constructor(n, r) {
606
613
  this.rig = n;
607
- let { side: c, preset: u, bones: d, joints: f, limits: m = [], anchor: h, elbow: _ } = r;
614
+ let { side: c, preset: u, bones: d, joints: f, limits: m = [], anchor: h, elbow: g } = r;
608
615
  if (c !== "left" && c !== "right") throw RangeError("Choose left or right for the palm calibration");
609
616
  if (n.parents.some((e) => !Number.isInteger(e) || e < -1 || e >= n.parents.length)) throw RangeError("Palm calibration needs parents with valid joint indices or −1");
610
- let C = e(n.parents, "Palm calibration"), w = a(n, u, d)[c], T = w?.fingers.middle?.bones[0];
611
- if (!w || T === void 0) throw RangeError("Palm calibration needs an index, middle and outer finger mapped on the requested hand");
612
- if (!Array.isArray(f) || f.length < 2 || f.length > 256 || f.at(-1) !== w.hand || !f.every((e, t) => Number.isInteger(e) && e >= 0 && e < n.parents.length && (!t || n.parents[e] === f[t - 1]))) throw RangeError("Palm calibration needs a consecutive 2–256 joint arm ending at the mapped wrist");
613
- let E = w.world(w.hand), D = l(I(E.rotation, "wrist rest rotation")), O = p(S(w.world(T).position, E.position), D), k = p(w.palm, D), A = x(k, y(O) * .085);
614
- this.inversePalm = l(V(O, k));
615
- let j = t(n.parents, n.rest, C).linear, M = w.hand * 9, N = [
617
+ let _ = e(n.parents, "Palm calibration"), v = a(n, u, d)[c], b = v?.fingers.middle?.bones[0];
618
+ if (!v || b === void 0) throw RangeError("Palm calibration needs an index, middle and outer finger mapped on the requested hand");
619
+ if (!Array.isArray(f) || f.length < 2 || f.length > 256 || f.at(-1) !== v.hand || !f.every((e, t) => Number.isInteger(e) && e >= 0 && e < n.parents.length && (!t || n.parents[e] === f[t - 1]))) throw RangeError("Palm calibration needs a consecutive 2–256 joint arm ending at the mapped wrist");
620
+ let E = v.world(v.hand), D = l(z(E.rotation, "wrist rest rotation")), O = p(T(v.world(b).position, E.position), D), k = p(v.palm, D), A = w(k, S(O) * .085);
621
+ this.inversePalm = l(W(O, k));
622
+ let j = t(n.parents, n.rest, _).linear, M = v.hand * 9, N = [
616
623
  0,
617
624
  1,
618
625
  2
619
626
  ].map((e) => {
620
627
  let t = M + e * 3;
621
628
  return Math.hypot(j[t], j[t + 1], j[t + 2]);
622
- }), P = y(O), F = b(O), L = w.fingers.thumb, R = L ? v(p(S(w.world(L.bones[0]).position, E.position), D), F) : 0, z = Math.min(P * .88, Math.max(P * .56, R + .9 * (L?.lengths[1] ?? 0))), B = g(x(F, z), A).map((e, t) => e / N[t]);
623
- this.anchor = h ? [...h] : B, H(this.anchor, "Palm anchor"), this.gripForward = b(O), this.gripNormal = b(k), this.gripAnchor = this.anchor.map((e, t) => e * N[t]);
624
- let { flexion: G = 1.12, extension: K = 1, deviation: q = .46 } = r.wrist || {};
629
+ }), P = S(O), F = C(O), I = v.fingers.thumb, L = I ? x(p(T(v.world(I.bones[0]).position, E.position), D), F) : 0, R = Math.min(P * .88, Math.max(P * .56, L + .9 * (I?.lengths[1] ?? 0))), B = y(w(F, R), A).map((e, t) => e / N[t]);
630
+ this.anchor = h ? [...h] : B, G(this.anchor, "Palm anchor"), this.gripForward = C(O), this.gripNormal = C(k), this.gripAnchor = this.anchor.map((e, t) => e * N[t]);
631
+ let { flexion: V = 1.12, extension: H = 1, deviation: U = .46 } = r.wrist || {};
625
632
  if (![
626
- G,
627
- K,
628
- q
633
+ V,
634
+ H,
635
+ U
629
636
  ].every((e) => e >= 0 && e <= Math.PI / 2)) throw RangeError("Palm wrist ranges need angles from zero to π/2 radians");
630
637
  this.wrist = r.wrist === !1 ? null : {
631
- flexion: G,
632
- extension: K,
633
- deviation: q
634
- }, this.hand = w, this.arm = [...f];
638
+ flexion: V,
639
+ extension: H,
640
+ deviation: U
641
+ }, this.hand = v, this.arm = [...f];
635
642
  let J = [...m];
636
- if (_ && !J.some(({ joint: e }) => e === _.joint) && J.push(W(n, w, f, _)), J.some(({ joint: e }) => e === w.hand) || J.push({
637
- joint: w.hand,
638
- neutral: U(n, w.hand),
643
+ if (g && !J.some(({ joint: e }) => e === g.joint) && J.push(q(n, v, f, g)), J.some(({ joint: e }) => e === v.hand) || J.push({
644
+ joint: v.hand,
645
+ neutral: K(n, v.hand),
639
646
  limit: {
640
647
  type: "cone",
641
- axis: b(O),
648
+ axis: C(O),
642
649
  angle: 1.15,
643
650
  twist: [-.22, .22]
644
651
  }
645
652
  }), this.limits = J, this.tolerance = r.tolerance ?? .003, this.angleTolerance = r.angleTolerance ?? Math.PI / 36, !(this.tolerance > 0 && Number.isFinite(this.tolerance) && this.angleTolerance > 0 && this.angleTolerance <= Math.PI)) throw RangeError("Palm tolerances need a finite positive distance and an angle between zero and π");
646
- this.open = i(w, o(s(), w));
647
- for (let [e, t] of w.rest) this.open.has(e) || this.open.set(e, [...t]);
653
+ this.open = i(v, o(s(), v));
654
+ for (let [e, t] of v.rest) this.open.has(e) || this.open.set(e, [...t]);
648
655
  this.joints = [...this.arm, ...this.open.keys()];
649
656
  }
650
657
  solve(e, t) {
651
658
  let { point: n, normal: a, towardsFingers: s, diameter: c, close: l = 1, weight: p = 1, elbowPole: m, clearance: h } = t;
652
- if (H(a, "Surface normal"), H(s, "Direction toward fingers"), m && H(m, "Elbow pole"), !(l >= 0 && l <= 1 && p >= 0 && p <= 1)) throw RangeError("Hand closure and reach weight must be from zero to one");
659
+ if (G(a, "Surface normal"), G(s, "Direction toward fingers"), m && G(m, "Elbow pole"), !(l >= 0 && l <= 1 && p >= 0 && p <= 1)) throw RangeError("Hand closure and reach weight must be from zero to one");
653
660
  if (this.gripPose?.diameter !== c) {
654
661
  let e = i(this.hand, o(r(c), this.hand)), t = new Map(this.open);
655
- C(this.rig, this.hand, this.gripForward, this.gripNormal, this.gripAnchor, c, t, e), this.gripPose = {
662
+ E(this.rig, this.hand, this.gripForward, this.gripNormal, this.gripAnchor, c, t, e), this.gripPose = {
656
663
  diameter: c,
657
664
  approach: t,
658
665
  closed: e
659
666
  };
660
667
  }
661
- let g = f(d(V(s, x(a, -1)), this.inversePalm)), _ = this.wrist, v = _ ? (e, t) => Math.min(this.wristGap(e, t, _), h?.(e, t) ?? Infinity) : h, y = L({
668
+ let g = f(d(W(s, w(a, -1)), this.inversePalm)), _ = this.wrist, v = _ ? (e, t) => Math.min(this.wristGap(e, t, _), h?.(e, t) ?? Infinity) : h, y = B({
662
669
  parents: this.rig.parents,
663
670
  local: e
664
671
  }, {
@@ -673,19 +680,19 @@ var G = class {
673
680
  clearance: v,
674
681
  tolerance: this.tolerance * .05,
675
682
  angleTolerance: this.angleTolerance * .05
676
- }), b = y.blocked ? Infinity : Math.max(y.distance / this.tolerance, y.angle / this.angleTolerance), S = Math.max(0, 1 - b), w = p === 0 ? 0 : l * S * S * (3 - 2 * S);
683
+ }), b = y.blocked ? Infinity : Math.max(y.distance / this.tolerance, y.angle / this.angleTolerance), x = Math.max(0, 1 - b), S = p === 0 ? 0 : l * x * x * (3 - 2 * x);
677
684
  if (p > 0) for (let [t, n] of this.gripPose.approach) {
678
- let r = u(n, this.gripPose.closed.get(t) ?? n, w, .9995);
685
+ let r = u(n, this.gripPose.closed.get(t) ?? n, S, .9995);
679
686
  e.set(r, t * 10 + 3);
680
687
  }
681
688
  return {
682
689
  ...y,
683
690
  locked: b <= 1,
684
- gripWeight: w
691
+ gripWeight: S
685
692
  };
686
693
  }
687
694
  wristGap(e, t, n) {
688
- let r = this.arm.length - 1, i = r * 3, a = r * 4, o = S([
695
+ let r = this.arm.length - 1, i = r * 3, a = r * 4, o = T([
689
696
  e[i],
690
697
  e[i + 1],
691
698
  e[i + 2]
@@ -693,23 +700,23 @@ var G = class {
693
700
  e[i - 3],
694
701
  e[i - 2],
695
702
  e[i - 1]
696
- ]), s = y(o);
703
+ ]), s = S(o);
697
704
  if (s < 1e-6) return Infinity;
698
705
  let c = [
699
706
  -t[a],
700
707
  -t[a + 1],
701
708
  -t[a + 2],
702
709
  t[a + 3]
703
- ], l = p(o, c), u = b(_(this.gripForward, this.gripNormal)), d = _(this.gripNormal, u), f = v(l, d), m = v(l, this.gripNormal), h = v(l, u), g = -Math.atan2(m, f), x = Math.atan2(Math.abs(h), Math.hypot(f, m));
704
- return s * Math.min(n.flexion - g, n.extension + g, n.deviation - x);
710
+ ], l = p(o, c), u = C(b(this.gripForward, this.gripNormal)), d = b(this.gripNormal, u), f = x(l, d), m = x(l, this.gripNormal), h = x(l, u), g = -Math.atan2(m, f), _ = Math.atan2(Math.abs(h), Math.hypot(f, m));
711
+ return s * Math.min(n.flexion - g, n.extension + g, n.deviation - _);
705
712
  }
706
713
  };
707
- function K(e, t, n, r) {
714
+ function Y(e, t, n, r) {
708
715
  let i = e.animation.model, a = i.nodes, o = i.root.engine.store, s = new Map(a.map((e, t) => [e.id, t])), c = a.map((e) => s.get(o.parents[e.id]) ?? -1), l = new Float64Array(a.length * 10);
709
716
  a.forEach((t, n) => {
710
717
  l.set(e.restPosition(n), n * 10), l.set(e.restRotation(n), n * 10 + 3), l.set(e.restScale(n), n * 10 + 7);
711
718
  });
712
- let u = new G({
719
+ let u = new J({
713
720
  names: a.map((e) => e.name ?? ""),
714
721
  parents: c,
715
722
  rest: l
@@ -736,4 +743,4 @@ function K(e, t, n, r) {
736
743
  });
737
744
  }
738
745
  //#endregion
739
- export { F as LimbCollisionRig, G as PalmContactRig, K as addPalmContact, L as solveContactIK };
746
+ export { R as LimbCollisionRig, J as PalmContactRig, Y as addPalmContact, B as solveContactIK };