@nebula-spatial/viewer 0.5.17 → 0.6.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +63 -22
  2. package/USD-VISUAL-MATERIAL-SUPPORT.md +5 -1
  3. package/dist/asset-ir-BEDGD9CL.js +637 -0
  4. package/dist/assets/source-types.d.ts +10 -0
  5. package/dist/assets/types.d.ts +59 -6
  6. package/dist/index-DoxM1Msk.js +17223 -0
  7. package/dist/index.d.ts +20 -1
  8. package/dist/index.js +29 -18
  9. package/dist/{mjcf-visual-C0PWP0vB.js → mjcf-visual-CQEeWkL_.js} +12 -13
  10. package/dist/mujoco-runtime-BmuEJ_ST.js +791 -0
  11. package/dist/physics-guide-size-CplAlsve.js +12 -0
  12. package/dist/scene/instances.d.ts +36 -0
  13. package/dist/scene/joints.d.ts +129 -0
  14. package/dist/scene/session.d.ts +26 -0
  15. package/dist/scene/types.d.ts +284 -0
  16. package/dist/session-BnNntPUE.js +9169 -0
  17. package/dist/{session-D44z2smp.js → session-Cz4tFSlO.js} +6 -8
  18. package/dist/session-sBOJEWpK.js +559 -0
  19. package/dist/simulation/physics-runtime-state.d.ts +73 -0
  20. package/dist/simulation/types.d.ts +151 -0
  21. package/dist/simulation/usd-dependency-probe.d.ts +42 -0
  22. package/dist/simulation/usd-inspection.d.ts +66 -0
  23. package/dist/types.d.ts +46 -1
  24. package/dist/ui/camera-axis-gizmo.d.ts +5 -0
  25. package/dist/urdf-session-HKelZnMM.js +1230 -0
  26. package/dist/usd-session-Cn6whyh4.js +10875 -0
  27. package/dist/version.d.ts +1 -1
  28. package/dist/visual/classification.d.ts +20 -0
  29. package/dist/{xacro-CjMp9HT5.js → xacro-Cl7vUCSC.js} +1 -1
  30. package/package.json +2 -1
  31. package/runtime-assets.d.mts +9 -9
  32. package/runtime-assets.mjs +11 -11
  33. package/vite.d.mts +7 -7
  34. package/vite.mjs +115 -115
  35. package/dist/collision-debug-CHPrPNT3.js +0 -2947
  36. package/dist/index-cM9KArXm.js +0 -11499
  37. package/dist/mujoco-runtime-BV2xR38M.js +0 -739
  38. package/dist/resources-CHhCm2f7.js +0 -27
  39. package/dist/session-CKva1mAh.js +0 -801
  40. package/dist/stage-pxmOnL0U.js +0 -592
  41. package/dist/urdf-session-ChpRmt69.js +0 -1264
  42. package/dist/usd-session-auH_xmOc.js +0 -10526
@@ -1,739 +0,0 @@
1
- var F = Object.defineProperty;
2
- var B = (_, t, i) => t in _ ? F(_, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : _[t] = i;
3
- var f = (_, t, i) => B(_, typeof t != "symbol" ? t + "" : t, i);
4
- import L from "@mujoco/mujoco";
5
- import { Quaternion as x, Vector3 as I } from "three";
6
- import { b as C, z as R } from "./index-cM9KArXm.js";
7
- let z = 0;
8
- function V(_, t) {
9
- const i = `/viewer-simulation-${++z}`;
10
- _.mkdir(i);
11
- const e = /* @__PURE__ */ new Set([i]), o = /* @__PURE__ */ new Set(), r = (n, s) => {
12
- const c = n.replaceAll("\\", "/").split("/");
13
- if (c[0] === "" || c.some((u) => u === ".." || u.includes(":"))) throw J(`Invalid package path: ${n}`, "E_SIMULATION_PATH");
14
- const l = c.filter((u) => u && u !== ".");
15
- if (!l.length) throw J("Package path is empty.", "E_SIMULATION_PATH");
16
- let m = i;
17
- for (const u of l.slice(0, -1))
18
- m += `/${u}`, e.has(m) || (_.mkdir(m), e.add(m));
19
- const h = `${i}/${l.join("/")}`;
20
- return _.writeFile(h, s), o.add(h), h;
21
- }, a = () => {
22
- for (const n of o) _.unlink(n);
23
- o.clear();
24
- for (const n of [...e].reverse()) _.rmdir(n);
25
- e.clear();
26
- };
27
- try {
28
- for (const [n, s] of t) r(n, s);
29
- return { writeEntry: (n) => r("__viewer_entry.xml", n), dispose: a };
30
- } catch (n) {
31
- throw a(), n;
32
- }
33
- }
34
- function J(_, t) {
35
- return Object.assign(new Error(_), { code: t });
36
- }
37
- function $({
38
- target: _,
39
- point: t,
40
- velocity: i,
41
- stiffness: e,
42
- damping: o,
43
- maxForce: r,
44
- strength: a
45
- }) {
46
- const n = [
47
- e * (_[0] - t[0]) - o * i[0],
48
- e * (_[1] - t[1]) - o * i[1],
49
- e * (_[2] - t[2]) - o * i[2]
50
- ], s = Math.hypot(n[0], n[1], n[2]), c = r * a;
51
- if (s > c && s > 0) {
52
- const l = c / s;
53
- n[0] *= l, n[1] *= l, n[2] *= l;
54
- }
55
- return n;
56
- }
57
- class U {
58
- constructor(t, i, e) {
59
- f(this, "pairs", []);
60
- f(this, "activePairs", []);
61
- this.mujoco = t, this.model = i;
62
- const o = new Set(Array.from(i.exclude_signature)), r = /* @__PURE__ */ new Set();
63
- for (let a = 0; a < i.npair; a++)
64
- r.add(this.geomPairKey(i.pair_geom1[a], i.pair_geom2[a]));
65
- for (let a = 0; a < i.ngeom; a++)
66
- if (this.isCollider(a))
67
- for (let n = a + 1; n < i.ngeom; n++) {
68
- if (!this.isCollider(n)) continue;
69
- const s = i.geom_bodyid[a], c = i.geom_bodyid[n];
70
- if (s === c || i.body_weldid[s] === i.body_weldid[c] || this.hasNativeContact(a, n, s, c, o, r)) continue;
71
- const l = this.pairTolerance(a, n), m = this.distance(e, a, n, l), h = m < -l || Math.abs(m) < l && this.hasDegenerateOverlap(e, a, n, l);
72
- this.pairs.push({
73
- first: a,
74
- second: n,
75
- firstBody: s,
76
- secondBody: c,
77
- tolerance: l,
78
- initiallyOverlapping: h,
79
- separated: !h
80
- });
81
- }
82
- }
83
- /** Select only pairs whose relative transform can change in this edit. */
84
- prepare(t) {
85
- const i = new Array(this.model.nbody).fill("");
86
- for (let e = 1; e < this.model.nbody; e++) {
87
- let o = i[this.model.body_parentid[e]];
88
- const r = this.model.body_jntadr[e];
89
- for (let a = r; a < r + this.model.body_jntnum[e]; a++)
90
- t.has(a) && (o += `${a},`);
91
- i[e] = o;
92
- }
93
- this.activePairs = this.pairs.filter((e) => i[e.firstBody] !== i[e.secondBody]);
94
- }
95
- /** Call after updating the candidate pose's kinematics, before accepting it. */
96
- accept(t) {
97
- const i = [];
98
- for (const e of this.activePairs) {
99
- const o = this.distance(t, e.first, e.second, e.tolerance);
100
- if (!Number.isFinite(o)) return !1;
101
- if (!e.separated) {
102
- o >= e.tolerance && i.push(e);
103
- continue;
104
- }
105
- if (o < -e.tolerance || Math.abs(o) < e.tolerance && this.hasDegenerateOverlap(t, e.first, e.second, e.tolerance)) return !1;
106
- }
107
- for (const e of i) e.separated = !0;
108
- return !0;
109
- }
110
- /** Restore the original exemptions, without recapturing the current pose. */
111
- reset() {
112
- for (const t of this.pairs) t.separated = !t.initiallyOverlapping;
113
- this.activePairs = [];
114
- }
115
- /** Default assembly intersections also need an exemption during playback. */
116
- hasInitialBodyOverlap(t, i) {
117
- return this.pairs.some((e) => e.initiallyOverlapping && (e.firstBody === t && e.secondBody === i || e.firstBody === i && e.secondBody === t));
118
- }
119
- isCollider(t) {
120
- return this.model.geom_contype[t] !== 0 || this.model.geom_conaffinity[t] !== 0;
121
- }
122
- distance(t, i, e, o) {
123
- const r = this.model.geom_rbound[i], a = this.model.geom_rbound[e], n = this.mujoco.mjtGeom.mjGEOM_PLANE.value;
124
- if (r > 0 && a > 0 && this.model.geom_type[i] !== n && this.model.geom_type[e] !== n) {
125
- const s = i * 3, c = e * 3, l = t.geom_xpos[s] - t.geom_xpos[c], m = t.geom_xpos[s + 1] - t.geom_xpos[c + 1], h = t.geom_xpos[s + 2] - t.geom_xpos[c + 2], u = r + a + o;
126
- if (l * l + m * m + h * h > u * u) return o;
127
- }
128
- return this.mujoco.mj_geomDistance(this.model, t, i, e, o, null);
129
- }
130
- hasDegenerateOverlap(t, i, e, o) {
131
- const r = Math.max(o * 4, 1e-8), a = e * 3;
132
- for (let n = 0; n < 3; n++) {
133
- const s = a + n, c = t.geom_xpos[s];
134
- try {
135
- for (const l of [-1, 1])
136
- if (t.geom_xpos[s] = c + l * r, this.mujoco.mj_geomDistance(this.model, t, i, e, o, null) < -r - o) return !0;
137
- } finally {
138
- t.geom_xpos[s] = c;
139
- }
140
- }
141
- return !1;
142
- }
143
- geomPairKey(t, i) {
144
- return t < i ? `${t}:${i}` : `${i}:${t}`;
145
- }
146
- pairTolerance(t, i) {
147
- const e = this.model.geom_rbound[t], o = this.model.geom_rbound[i], r = Math.min(e || o || 1, o || e || 1);
148
- return Math.max(1e-8, Math.min(1e-5, r * 1e-5));
149
- }
150
- hasNativeContact(t, i, e, o, r, a) {
151
- const n = this.model.opt.disableflags;
152
- if (n & (this.mujoco.mjtDisableBit.mjDSBL_CONTACT.value | this.mujoco.mjtDisableBit.mjDSBL_CONSTRAINT.value)) return !1;
153
- if (a.has(this.geomPairKey(t, i))) return !0;
154
- const s = Math.min(e, o) * 65536 + Math.max(e, o);
155
- if (r.has(s) || !(this.model.geom_contype[t] & this.model.geom_conaffinity[i]) && !(this.model.geom_contype[i] & this.model.geom_conaffinity[t])) return !1;
156
- if (!(n & this.mujoco.mjtDisableBit.mjDSBL_FILTERPARENT.value)) {
157
- const c = this.model.body_weldid[e], l = this.model.body_weldid[o], m = this.model.body_weldid[this.model.body_parentid[c]], h = this.model.body_weldid[this.model.body_parentid[l]];
158
- if (c !== 0 && m === l && l !== 0 || l !== 0 && h === c && c !== 0) return !1;
159
- }
160
- return !0;
161
- }
162
- }
163
- const q = 1 / 240, k = 1 / 960, H = 0.25, N = 0.05, Y = 80, Z = 0.7, W = 60, Q = 1e-4, O = 10, S = 3.5, A = 14, X = 1 / 960, K = 2, G = 20, tt = 0.15, et = 10, it = 0.25;
164
- class at {
165
- constructor() {
166
- f(this, "mujoco", null);
167
- f(this, "model", null);
168
- f(this, "data", null);
169
- f(this, "grab", null);
170
- f(this, "lastGrabForce", [0, 0, 0]);
171
- f(this, "initialControls", []);
172
- f(this, "accumulator", 0);
173
- f(this, "timestep", q);
174
- f(this, "playbackTimestep", q);
175
- /** Authored ground pose, captured before any seating. Re-seats start here. */
176
- f(this, "initialGround", null);
177
- /** Ground pose currently in the model; restored verbatim by reset(). */
178
- f(this, "seatedGround", null);
179
- f(this, "worldRotation", new x());
180
- f(this, "topLevelBodies", []);
181
- f(this, "initialVelocities", []);
182
- f(this, "jointTargets", /* @__PURE__ */ new Map());
183
- f(this, "jointDriveActive", !1);
184
- f(this, "jointDriveAccumulator", 0);
185
- f(this, "jointDriveSettledSeconds", 0);
186
- f(this, "jointDriveElapsed", 0);
187
- f(this, "jointDriveDuration", 0);
188
- f(this, "jointDriveBestError", 0);
189
- f(this, "jointDriveStalledSeconds", 0);
190
- f(this, "heldJointPoses", []);
191
- f(this, "jointPreviewCollision", null);
192
- f(this, "jointPreviewExclusions", null);
193
- }
194
- async initialize(t, i = {}) {
195
- var j, y;
196
- const e = i.wasmModule ? await i.wasmModule : void 0;
197
- this.mujoco ?? (this.mujoco = await L({
198
- locateFile: (p) => p.endsWith(".wasm") ? R(i.baseUrl) : p,
199
- ...e ? {
200
- instantiateWasm(p, v) {
201
- return C(e, p, v);
202
- }
203
- } : {}
204
- }));
205
- const o = V(this.mujoco.FS, t.files);
206
- let r = null, a = null, n = null, s = null, c = q, l = null, m = null, h = q, u = t.initialControls ?? [];
207
- try {
208
- if (r = this.mujoco.MjModel.from_xml_path(o.writeEntry(t.xml)), c = r.opt.timestep, !Number.isFinite(c) || c <= 0)
209
- throw new Error(`Invalid MuJoCo timestep: ${c}`);
210
- for (const v of t.massOverrides ?? []) {
211
- const g = this.mujoco.mj_name2id(r, this.mujoco.mjtObj.mjOBJ_BODY.value, v.body);
212
- if (g <= 0 || !Number.isFinite(v.mass) || v.mass <= 0 || !(r.body_mass[g] > 0)) throw new Error(`Invalid mass override: ${v.body}`);
213
- const d = v.mass / r.body_mass[g];
214
- r.body_mass[g] = v.mass;
215
- for (let b = 0; b < 3; b++) r.body_inertia[g * 3 + b] *= d;
216
- }
217
- a = new this.mujoco.MjData(r), (j = t.massOverrides) != null && j.length && this.mujoco.mj_setConst(r, a), this.worldRotation.identity(), this.mujoco.mj_forward(r, a), u = this.applyAccelerationDrives(
218
- r,
219
- a,
220
- t.accelerationDrives,
221
- u
222
- ), this.applyInitialControls(a, u), this.applyInitialVelocities(r, a, t.initialVelocities), this.mujoco.mj_forward(r, a);
223
- const p = this.mujoco.mj_name2id(r, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
224
- if (n = p < 0 ? null : { id: p, height: r.geom_pos[p * 3 + 2], sameFrame: r.geom_sameframe[p] }, s = this.seatDefaultGround(r, a), Array.from(r.jnt_type).some((v) => v === this.mujoco.mjtJoint.mjJNT_HINGE.value || v === this.mujoco.mjtJoint.mjJNT_SLIDE.value) && (l = new U(this.mujoco, r, a)), l && ((y = t.jointPreviewCollisionExcludes) != null && y.length)) {
225
- m = Int32Array.from(r.exclude_signature);
226
- const v = /* @__PURE__ */ new Set();
227
- for (const b of t.jointPreviewCollisionExcludes) {
228
- const E = this.mujoco.mj_name2id(r, this.mujoco.mjtObj.mjOBJ_BODY.value, b.body1), T = this.mujoco.mj_name2id(r, this.mujoco.mjtObj.mjOBJ_BODY.value, b.body2);
229
- if (E <= 0 || T <= 0) throw new Error(`Invalid joint preview collision pair: ${b.body1}, ${b.body2}`);
230
- l.hasInitialBodyOverlap(E, T) || v.add(Math.min(E, T) * 65536 + Math.max(E, T));
231
- }
232
- const g = m.filter((b) => !v.has(b)), d = new Int32Array(m.length);
233
- d.set(g, d.length - g.length), r.exclude_signature.set(d), this.mujoco.mj_forward(r, a);
234
- }
235
- h = this.actuatorTimestep(r, c), r.opt.timestep = h;
236
- } catch (p) {
237
- throw a == null || a.delete(), r == null || r.delete(), w(p, "E_MUJOCO_INITIALIZE");
238
- } finally {
239
- o.dispose();
240
- }
241
- this.disposeModel(), this.model = r, this.data = a, this.initialControls = u, this.initialVelocities = t.initialVelocities ?? [], this.accumulator = 0, this.timestep = h, this.playbackTimestep = h, this.initialGround = n, this.seatedGround = s, this.topLevelBodies = this.captureTopLevelBodies(r), this.jointPreviewCollision = l, this.jointPreviewExclusions = m;
242
- }
243
- get defaultGroundHeight() {
244
- if (!this.mujoco || !this.model) return;
245
- const t = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
246
- return t < 0 ? void 0 : this.model.geom_pos[t * 3 + 2];
247
- }
248
- /**
249
- * Re-orients the compiled physics to the given authored up axis by rotating
250
- * every top-level body about the world origin. Gravity stays -Z and the
251
- * viewer ground stays the authored horizontal plane, so a cup that was
252
- * standing now genuinely topples and settles on its side — the alternative,
253
- * rotating only the rendered scene graph, would leave the visuals and the
254
- * simulation in different frames.
255
- *
256
- * The full current state (free-joint positions, orientations and velocities)
257
- * is rotated by the same delta, so a paused or running simulation is carried
258
- * over instead of being re-seeded.
259
- */
260
- setWorldRotation(t) {
261
- var r;
262
- if (!this.mujoco || !this.model || !this.data) return;
263
- const i = this.model, e = this.data;
264
- if (t.equals(this.worldRotation)) return;
265
- const o = t.clone().multiply(this.worldRotation.clone().invert());
266
- this.rotateReferenceFrames(this.topLevelBodies, t), this.rotateState(this.topLevelBodies, i, e, o), this.worldRotation.copy(t), this.mujoco.mj_forward(i, e), this.seatedGround = this.seatDefaultGround(i, e, (r = this.initialGround) == null ? void 0 : r.height), this.accumulator = 0;
267
- }
268
- captureTopLevelBodies(t) {
269
- const i = this.mujoco, e = [];
270
- for (let o = 1; o < t.nbody; o++) {
271
- if (t.body_parentid[o] !== 0) continue;
272
- const r = t.body_jntadr[o], a = t.body_jntnum[o] === 1 && r >= 0 && t.jnt_type[r] === i.mjtJoint.mjJNT_FREE.value ? r : -1, n = o * 3, s = o * 4;
273
- e.push({
274
- id: o,
275
- position: new I(t.body_pos[n], t.body_pos[n + 1], t.body_pos[n + 2]),
276
- quaternion: new x(t.body_quat[s + 1], t.body_quat[s + 2], t.body_quat[s + 3], t.body_quat[s]),
277
- freeJoint: a
278
- });
279
- }
280
- return e;
281
- }
282
- /** Writes the rotated reference pose, which reset() and fixed bases follow. */
283
- rotateReferenceFrames(t, i) {
284
- const e = this.model;
285
- for (const o of t) {
286
- const r = o.position.clone().applyQuaternion(i), a = i.clone().multiply(o.quaternion), n = o.id * 3, s = o.id * 4;
287
- if (e.body_pos[n] = r.x, e.body_pos[n + 1] = r.y, e.body_pos[n + 2] = r.z, e.body_quat[s] = a.w, e.body_quat[s + 1] = a.x, e.body_quat[s + 2] = a.y, e.body_quat[s + 3] = a.z, e.body_sameframe[o.id] && (e.body_sameframe[o.id] = 0), o.freeJoint < 0) continue;
288
- const c = e.jnt_qposadr[o.freeJoint];
289
- e.qpos0[c] = r.x, e.qpos0[c + 1] = r.y, e.qpos0[c + 2] = r.z, e.qpos0[c + 3] = a.w, e.qpos0[c + 4] = a.x, e.qpos0[c + 5] = a.y, e.qpos0[c + 6] = a.z;
290
- }
291
- }
292
- /** Rotates the live free-joint state so the simulation keeps its progress. */
293
- rotateState(t, i, e, o) {
294
- for (const r of t) {
295
- if (r.freeJoint < 0) continue;
296
- const a = i.jnt_qposadr[r.freeJoint], n = i.jnt_dofadr[r.freeJoint], s = new I(e.qpos[a], e.qpos[a + 1], e.qpos[a + 2]).applyQuaternion(o);
297
- e.qpos[a] = s.x, e.qpos[a + 1] = s.y, e.qpos[a + 2] = s.z;
298
- const c = new x(e.qpos[a + 4], e.qpos[a + 5], e.qpos[a + 6], e.qpos[a + 3]).premultiply(o);
299
- e.qpos[a + 3] = c.w, e.qpos[a + 4] = c.x, e.qpos[a + 5] = c.y, e.qpos[a + 6] = c.z;
300
- const l = new I(e.qvel[n], e.qvel[n + 1], e.qvel[n + 2]).applyQuaternion(o);
301
- e.qvel[n] = l.x, e.qvel[n + 1] = l.y, e.qvel[n + 2] = l.z;
302
- const m = new I(e.qvel[n + 3], e.qvel[n + 4], e.qvel[n + 5]).applyQuaternion(o);
303
- e.qvel[n + 3] = m.x, e.qvel[n + 4] = m.y, e.qvel[n + 5] = m.z;
304
- }
305
- }
306
- /** Lower only the viewer-owned plane, never authored bodies or authored floors.
307
- * Run after forward kinematics, not during stepping (the floor must not follow falling bodies).
308
- * `baseHeight` re-seats from a stable reference; omitting it lowers from the current pose.
309
- */
310
- seatDefaultGround(t, i, e) {
311
- const o = this.mujoco, r = o.mj_name2id(t, o.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
312
- if (r < 0 || t.geom_bodyid[r] !== 0 || t.geom_type[r] !== o.mjtGeom.mjGEOM_PLANE.value) return null;
313
- let a = e ?? t.geom_pos[r * 3 + 2];
314
- for (let n = 0; n < t.ngeom; n++) {
315
- if (n === r || t.geom_type[n] === o.mjtGeom.mjGEOM_PLANE.value || !(t.geom_contype[n] & t.geom_conaffinity[r]) && !(t.geom_conaffinity[n] & t.geom_contype[r])) continue;
316
- const s = this.geomBottom(t, i, n), c = Math.max(1e-3, t.geom_margin[n] + t.geom_margin[r]);
317
- Number.isFinite(s) && s < a && (a = Math.min(a, s - c));
318
- }
319
- return a !== t.geom_pos[r * 3 + 2] && (t.geom_pos[r * 3 + 2] = a, t.geom_sameframe[r] = 0, o.mj_forward(t, i)), { id: r, height: a, sameFrame: t.geom_sameframe[r] };
320
- }
321
- /**
322
- * Resolve the lowest world-space point directly from MuJoCo's primitive
323
- * parameters. Rotating a mesh's local AABB into world space is conservative:
324
- * for a long, rotated shape it can sit far below the actual geometry. Use the
325
- * compiled vertices for mesh-backed geoms so the ground reflects the real
326
- * lowest point rather than the corners of its local bounding box.
327
- */
328
- geomBottom(t, i, e) {
329
- const o = this.mujoco, r = i.geom_xpos[e * 3 + 2], a = e * 3, n = e * 9, s = t.geom_type[e], c = Math.abs(t.geom_size[a]), l = Math.abs(t.geom_size[a + 1]), m = Math.abs(t.geom_size[a + 2]);
330
- if (s === o.mjtGeom.mjGEOM_SPHERE.value)
331
- return r - c;
332
- if (s === o.mjtGeom.mjGEOM_CAPSULE.value)
333
- return r - c - Math.abs(i.geom_xmat[n + 8]) * l;
334
- if (s === o.mjtGeom.mjGEOM_CYLINDER.value) {
335
- const g = i.geom_xmat[n + 8];
336
- return r - Math.abs(g) * l - Math.sqrt(Math.max(0, 1 - g * g)) * c;
337
- }
338
- if (s === o.mjtGeom.mjGEOM_ELLIPSOID.value)
339
- return r - Math.hypot(
340
- i.geom_xmat[n + 2] * c,
341
- i.geom_xmat[n + 5] * l,
342
- i.geom_xmat[n + 8] * m
343
- );
344
- if (s === o.mjtGeom.mjGEOM_BOX.value)
345
- return r - Math.abs(i.geom_xmat[n + 2]) * c - Math.abs(i.geom_xmat[n + 5]) * l - Math.abs(i.geom_xmat[n + 8]) * m;
346
- if (s === o.mjtGeom.mjGEOM_MESH.value || s === o.mjtGeom.mjGEOM_SDF.value) {
347
- const g = t.geom_dataid[e];
348
- if (g < 0 || g >= t.nmesh) return Number.POSITIVE_INFINITY;
349
- const d = t.mesh_vertadr[g], b = t.mesh_vertnum[g];
350
- if (!Number.isInteger(d) || !Number.isInteger(b) || d < 0 || b <= 0 || d + b > t.nmeshvert)
351
- return Number.POSITIVE_INFINITY;
352
- let E = Number.POSITIVE_INFINITY;
353
- for (let T = d; T < d + b; T++) {
354
- const M = T * 3, D = r + i.geom_xmat[n + 6] * t.mesh_vert[M] + i.geom_xmat[n + 7] * t.mesh_vert[M + 1] + i.geom_xmat[n + 8] * t.mesh_vert[M + 2];
355
- Number.isFinite(D) && D < E && (E = D);
356
- }
357
- return E;
358
- }
359
- const h = e * 6, u = t.geom_aabb[h + 3], j = t.geom_aabb[h + 4], y = t.geom_aabb[h + 5];
360
- if (![u, j, y].every((g) => Number.isFinite(g) && g >= 0))
361
- return Number.POSITIVE_INFINITY;
362
- const p = r + i.geom_xmat[n + 6] * t.geom_aabb[h] + i.geom_xmat[n + 7] * t.geom_aabb[h + 1] + i.geom_xmat[n + 8] * t.geom_aabb[h + 2], v = Math.abs(i.geom_xmat[n + 6]) * u + Math.abs(i.geom_xmat[n + 7]) * j + Math.abs(i.geom_xmat[n + 8]) * y;
363
- return p - v;
364
- }
365
- beginGrab({
366
- bodyId: t,
367
- localPoint: i,
368
- target: e,
369
- stiffness: o,
370
- damping: r,
371
- maxForce: a,
372
- dampingRatio: n = Z,
373
- maxTargetSpeed: s = S,
374
- targetResponse: c = A
375
- }) {
376
- var y;
377
- const l = Math.max(((y = this.model) == null ? void 0 : y.body_mass[t]) ?? 1, 1e-6), m = o ?? Y, h = r ?? 2 * Math.sqrt(m * l) * n, u = a ?? Math.max(Q, l * W);
378
- this.setTimestep(this.grabTimestep(t, i, l, m));
379
- const j = [e[0], e[1], e[2]];
380
- this.grab = {
381
- bodyId: t,
382
- localPoint: [i[0], i[1], i[2]],
383
- target: [...j],
384
- requestedTarget: j,
385
- stiffness: m,
386
- damping: h,
387
- maxForce: u,
388
- strength: 1,
389
- maxTargetSpeed: s,
390
- targetResponse: c
391
- };
392
- }
393
- updateGrabTarget(t) {
394
- this.grab && (this.grab.requestedTarget = [t[0], t[1], t[2]]);
395
- }
396
- updateGrabStrength(t) {
397
- this.grab && (this.grab.strength = t);
398
- }
399
- applyGrabForce() {
400
- if (!this.grab || !this.model || !this.data || !this.mujoco) return;
401
- const { bodyId: t, localPoint: i, target: e, stiffness: o, damping: r, maxForce: a, strength: n } = this.grab, s = this.data.xmat.subarray(t * 9, t * 9 + 9), c = this.data.xpos.subarray(t * 3, t * 3 + 3), l = [
402
- c[0] + s[0] * i[0] + s[1] * i[1] + s[2] * i[2],
403
- c[1] + s[3] * i[0] + s[4] * i[1] + s[5] * i[2],
404
- c[2] + s[6] * i[0] + s[7] * i[1] + s[8] * i[2]
405
- ], m = this.data.cvel.subarray(t * 6, t * 6 + 6), h = this.data.xipos.subarray(t * 3, t * 3 + 3), u = [
406
- l[0] - h[0],
407
- l[1] - h[1],
408
- l[2] - h[2]
409
- ], j = [
410
- m[3] + m[1] * u[2] - m[2] * u[1],
411
- m[4] + m[2] * u[0] - m[0] * u[2],
412
- m[5] + m[0] * u[1] - m[1] * u[0]
413
- ], y = $({ target: e, point: l, velocity: j, stiffness: o, damping: r, maxForce: a, strength: n });
414
- this.lastGrabForce = [...y], this.data.qfrc_applied.fill(0);
415
- const p = Math.min(
416
- Math.max(this.model.body_inertia[t * 3], 1e-9),
417
- Math.max(this.model.body_inertia[t * 3 + 1], 1e-9),
418
- Math.max(this.model.body_inertia[t * 3 + 2], 1e-9)
419
- ), v = [
420
- -p * O * m[0],
421
- -p * O * m[1],
422
- -p * O * m[2]
423
- ];
424
- this.mujoco.mj_applyFT(
425
- this.model,
426
- this.data,
427
- y,
428
- v,
429
- l,
430
- t,
431
- this.data.qfrc_applied
432
- );
433
- }
434
- endGrab() {
435
- this.data && this.data.qfrc_applied.fill(0), this.lastGrabForce = [0, 0, 0], this.grab = null, this.setTimestep(this.playbackTimestep);
436
- }
437
- step(t) {
438
- if (!this.mujoco || !this.model || !this.data || t <= 0) return !1;
439
- this.accumulator += Math.min(t, N);
440
- let i = !1;
441
- for (; this.accumulator >= this.timestep; )
442
- this.updateGrabTargetForStep(), this.applyGrabForce(), this.mujoco.mj_step(this.model, this.data), this.accumulator -= this.timestep, i = !0;
443
- return i;
444
- }
445
- updateGrabTargetForStep() {
446
- const t = this.grab;
447
- if (!t) return;
448
- const i = t.requestedTarget ?? t.target, e = (t.maxTargetSpeed ?? S) * Math.max(1, Math.sqrt(t.strength)), o = t.targetResponse ?? A, r = 1 - Math.exp(-o * this.timestep), a = e * this.timestep;
449
- for (let n = 0; n < 3; n++) {
450
- const s = i[n] - t.target[n], c = Math.sign(s) * Math.min(Math.abs(s) * r, a);
451
- t.target[n] += c;
452
- }
453
- }
454
- get isJointDriveActive() {
455
- return this.jointDriveActive;
456
- }
457
- /** Set servo goals only. Actual coordinates are written exclusively by mj_step. */
458
- setJointTargets(t) {
459
- if (!this.mujoco || !this.model || !this.data) return;
460
- const i = this.mujoco, e = this.model, o = /* @__PURE__ */ new Map();
461
- for (const [a, n] of Object.entries(t)) {
462
- if (!Number.isFinite(n)) continue;
463
- const s = i.mj_name2id(e, i.mjtObj.mjOBJ_JOINT.value, a);
464
- if (s < 0) continue;
465
- const c = e.jnt_type[s];
466
- if (c !== i.mjtJoint.mjJNT_HINGE.value && c !== i.mjtJoint.mjJNT_SLIDE.value) continue;
467
- const l = e.jnt(s);
468
- let m = n, h;
469
- try {
470
- l.limited && (h = { lower: e.jnt_range[s * 2], upper: e.jnt_range[s * 2 + 1] }, m = Math.max(h.lower, Math.min(h.upper, m)));
471
- } finally {
472
- l.delete();
473
- }
474
- o.set(s, {
475
- qpos: e.jnt_qposadr[s],
476
- dof: e.jnt_dofadr[s],
477
- sliding: c === i.mjtJoint.mjJNT_SLIDE.value,
478
- value: m,
479
- limits: h
480
- });
481
- }
482
- if (o.size !== this.jointTargets.size || [...o].some(([a, n]) => !this.jointTargets.has(a) || Math.abs(n.value - this.jointTargets.get(a).value) > 1e-9)) {
483
- if (this.jointTargets = o, this.captureHeldJointPoses(), !o.size || [...o.values()].every((a) => Math.abs(a.value - this.data.qpos[a.qpos]) < 1e-9)) {
484
- this.jointDriveActive = !1, this.jointDriveAccumulator = 0, this.freezeJointDrivePose();
485
- return;
486
- }
487
- this.jointDriveActive = !0, this.jointDriveSettledSeconds = 0, this.jointDriveElapsed = 0, this.jointDriveStalledSeconds = 0, this.jointDriveBestError = this.jointDriveError(), this.jointDriveDuration = Math.min(et, this.jointDriveBestError * 2 + 3);
488
- }
489
- }
490
- clearJointTargets(t = {}) {
491
- this.jointTargets.clear(), this.heldJointPoses = [], this.jointDriveActive = !1, this.jointDriveAccumulator = 0, this.jointDriveSettledSeconds = 0, this.jointDriveElapsed = 0, this.jointDriveStalledSeconds = 0, t.freeze && this.freezeJointDrivePose();
492
- }
493
- jointDriveError() {
494
- let t = 0;
495
- for (const i of this.jointTargets.values())
496
- t = Math.max(t, Math.abs(i.value - this.data.qpos[i.qpos]) / (i.sliding ? 0.15 : 1));
497
- return t;
498
- }
499
- captureHeldJointPoses() {
500
- var n;
501
- const t = this.mujoco, i = this.model, e = this.data, o = new Set(this.jointTargets.keys());
502
- t.mj_forward(i, e);
503
- const r = /* @__PURE__ */ new Set();
504
- for (let s = 0; s < e.ne; s++) r.add(e.efc_id[s]);
505
- let a = !0;
506
- for (; a; ) {
507
- a = !1;
508
- for (let s = 0; s < i.neq; s++) {
509
- if (i.eq_type[s] !== t.mjtEq.mjEQ_JOINT.value || !r.has(s)) continue;
510
- const c = i.eq_obj1id[s], l = i.eq_obj2id[s];
511
- if (!(l < 0 || !o.has(c) && !o.has(l)))
512
- for (const m of [c, l]) o.has(m) || (o.add(m), a = !0);
513
- }
514
- }
515
- this.heldJointPoses = [];
516
- for (let s = 0; s < i.njnt; s++) {
517
- if (o.has(s)) continue;
518
- const c = i.jnt_type[s], l = c === t.mjtJoint.mjJNT_FREE.value, m = c === t.mjtJoint.mjJNT_BALL.value, h = i.jnt_qposadr[s], u = i.jnt_dofadr[s];
519
- this.heldJointPoses.push({ qpos: h, dof: u, values: Array.from(e.qpos.slice(h, h + (l ? 7 : m ? 4 : 1))), dofs: l ? 6 : m ? 3 : 1 });
520
- }
521
- this.holdJointPoses(), (n = this.jointPreviewCollision) == null || n.prepare(o);
522
- }
523
- holdJointPoses() {
524
- const t = this.data;
525
- for (const i of this.heldJointPoses)
526
- t.qpos.set(i.values, i.qpos), t.qvel.fill(0, i.dof, i.dof + i.dofs), t.qacc_warmstart.fill(0, i.dof, i.dof + i.dofs);
527
- }
528
- freezeJointDrivePose() {
529
- !this.model || !this.data || !this.mujoco || (this.data.qvel.fill(0), this.data.qacc_warmstart.fill(0), this.mujoco.mj_forward(this.model, this.data), this.data.qacc.fill(0), this.data.qacc_warmstart.fill(0));
530
- }
531
- /** A paused edit still needs integration and contact/equality constraint solving. */
532
- stepJointDrive(t) {
533
- if (!this.jointDriveActive || !this.mujoco || !this.model || !this.data || !(t > 0)) return !1;
534
- const i = this.mujoco, e = this.model, o = this.data, r = Math.min(this.timestep, X), a = e.opt.timestep, n = e.opt.disableflags, s = this.jointPreviewExclusions ? Int32Array.from(e.exclude_signature) : null, c = Float64Array.from(o.qfrc_applied), l = new Float64Array(e.nq), m = [];
535
- this.jointDriveAccumulator += Math.min(t, N) * K;
536
- let h = !1;
537
- try {
538
- this.jointPreviewExclusions && e.exclude_signature.set(this.jointPreviewExclusions), e.opt.timestep = r, e.opt.disableflags = n | i.mjtDisableBit.mjDSBL_ACTUATION.value;
539
- for (const u of this.heldJointPoses)
540
- for (let j = u.dof; j < u.dof + u.dofs; j++) {
541
- const y = e.dof_M0[j], p = e.dof_armature[j];
542
- m.push({ id: j, armature: p, inertia: y, invweight: e.dof_invweight0[j] });
543
- const v = Math.max(1e-9, y) * 1e6;
544
- e.dof_armature[j] = p + v, e.dof_M0[j] = y + v, e.dof_invweight0[j] = 0;
545
- }
546
- for (m.length && this.holdJointPoses(), (m.length || s) && i.mj_forward(e, o); this.jointDriveActive && this.jointDriveAccumulator >= r; ) {
547
- l.set(o.qpos);
548
- const u = o.time;
549
- o.qfrc_applied.set(c);
550
- for (const d of this.jointTargets.values()) {
551
- const b = d.value - o.qpos[d.qpos], E = d.sliding ? 0.15 : 1, T = Math.max(-E, Math.min(E, b * G / 2)), M = d.sliding ? 6 : 40, D = Math.max(-M, Math.min(
552
- M,
553
- 2 * G * (T - o.qvel[d.dof])
554
- )), P = Math.max(e.dof_M0[d.dof], 1e-9);
555
- o.qfrc_applied[d.dof] += P * D + o.qfrc_bias[d.dof];
556
- }
557
- i.mj_step(e, o), this.holdJointPoses();
558
- let j = Array.from(o.qpos).every(Number.isFinite) && [...this.jointTargets.values()].every((d) => {
559
- const b = l[d.qpos], E = o.qpos[d.qpos], T = (d.sliding ? 0.15 : 1) * r * 2, M = d.sliding ? 1e-5 : 1e-3;
560
- return Math.abs(E - b) <= T + 1e-8 && (!d.limits || E >= Math.min(b, d.limits.lower - M) - 1e-9 && E <= Math.max(b, d.limits.upper + M) + 1e-9);
561
- });
562
- if (j && this.jointPreviewCollision && (i.mj_kinematics(e, o), j = this.jointPreviewCollision.accept(o)), !j) {
563
- o.qpos.set(l), o.time = u, this.jointDriveActive = !1, this.jointDriveAccumulator = 0, h = !0;
564
- break;
565
- }
566
- this.jointDriveAccumulator -= r, h = !0, this.jointDriveElapsed += r;
567
- const y = this.jointDriveError(), p = y < this.jointDriveBestError - 1e-4;
568
- p && (this.jointDriveBestError = y);
569
- const v = [...this.jointTargets.values()].some((d) => Math.abs(o.qvel[d.dof]) > (d.sliding ? 0.15 : 1) * 1e-3);
570
- this.jointDriveStalledSeconds = p || v ? 0 : this.jointDriveStalledSeconds + r;
571
- let g = !0;
572
- for (const d of this.jointTargets.values())
573
- if (Math.abs(d.value - o.qpos[d.qpos]) > (d.sliding ? 1e-5 : 1e-4) || Math.abs(o.qvel[d.dof]) > (d.sliding ? 1e-4 : 1e-3)) {
574
- g = !1;
575
- break;
576
- }
577
- this.jointDriveSettledSeconds = g ? this.jointDriveSettledSeconds + r : 0, (this.jointDriveSettledSeconds >= 0.1 || this.jointDriveStalledSeconds >= tt || this.jointDriveElapsed >= this.jointDriveDuration) && (this.jointDriveActive = !1, this.jointDriveAccumulator = 0);
578
- }
579
- h && !m.length && !s && (this.jointDriveActive ? i.mj_forward(e, o) : this.freezeJointDrivePose());
580
- } finally {
581
- e.opt.timestep = a, e.opt.disableflags = n, s && e.exclude_signature.set(s), o.qfrc_applied.set(c);
582
- for (const u of m)
583
- e.dof_armature[u.id] = u.armature, e.dof_M0[u.id] = u.inertia, e.dof_invweight0[u.id] = u.invweight;
584
- (m.length || s) && (this.jointDriveActive ? i.mj_forward(e, o) : this.freezeJointDrivePose());
585
- }
586
- return h;
587
- }
588
- setJointValues(t) {
589
- if (!(!this.mujoco || !this.model || !this.data)) {
590
- for (const [i, e] of Object.entries(t)) {
591
- const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, i);
592
- if (o < 0 || !Number.isFinite(e)) continue;
593
- const r = this.model.jnt_type[o], a = this.mujoco.mjtJoint.mjJNT_HINGE.value, n = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
594
- r !== a && r !== n || (this.data.qpos[this.model.jnt_qposadr[o]] = e);
595
- }
596
- this.mujoco.mj_forward(this.model, this.data), this.seatDefaultGround(this.model, this.data), this.accumulator = 0;
597
- }
598
- }
599
- /** Move scalar joints for a paused visual preview without running contacts or constraints. */
600
- setJointValuesKinematic(t) {
601
- if (!this.mujoco || !this.model || !this.data) return;
602
- const i = this.mujoco, e = this.model, o = this.data;
603
- this.jointTargets.clear(), this.heldJointPoses = [], this.jointDriveActive = !1, this.jointDriveAccumulator = 0, this.jointDriveSettledSeconds = 0, this.jointDriveElapsed = 0, this.jointDriveStalledSeconds = 0;
604
- for (const [r, a] of Object.entries(t)) {
605
- if (!Number.isFinite(a)) continue;
606
- const n = i.mj_name2id(e, i.mjtObj.mjOBJ_JOINT.value, r);
607
- if (n < 0) continue;
608
- const s = e.jnt_type[n];
609
- s !== i.mjtJoint.mjJNT_HINGE.value && s !== i.mjtJoint.mjJNT_SLIDE.value || (o.qpos[e.jnt_qposadr[n]] = a);
610
- }
611
- o.qvel.fill(0), o.qacc.fill(0), o.qacc_warmstart.fill(0), o.qfrc_applied.fill(0), i.mj_kinematics(e, o), this.accumulator = 0;
612
- }
613
- getJointValues(t) {
614
- const i = {};
615
- if (!this.mujoco || !this.model || !this.data) return i;
616
- for (const e of t) {
617
- const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, e);
618
- if (o < 0) continue;
619
- const r = this.model.jnt_type[o], a = this.mujoco.mjtJoint.mjJNT_HINGE.value, n = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
620
- (r === a || r === n) && (i[e] = this.data.qpos[this.model.jnt_qposadr[o]]);
621
- }
622
- return i;
623
- }
624
- reset() {
625
- var t;
626
- if (!(!this.mujoco || !this.model || !this.data)) {
627
- if (this.clearJointTargets(), this.endGrab(), this.seatedGround) {
628
- const { id: i, height: e, sameFrame: o } = this.seatedGround;
629
- this.model.geom_pos[i * 3 + 2] = e, this.model.geom_sameframe[i] = o;
630
- }
631
- this.mujoco.mj_resetData(this.model, this.data), this.applyInitialControls(this.data, this.initialControls), this.applyInitialVelocities(this.model, this.data, this.initialVelocities), this.mujoco.mj_forward(this.model, this.data), (t = this.jointPreviewCollision) == null || t.reset(), this.accumulator = 0;
632
- }
633
- }
634
- dispose() {
635
- this.disposeModel(), this.mujoco = null;
636
- }
637
- actuatorTimestep(t, i) {
638
- const e = this.mujoco, o = /* @__PURE__ */ new Map();
639
- for (let a = 0; a < t.nu; a++) {
640
- if (t.actuator_trntype[a] !== e.mjtTrn.mjTRN_JOINT.value || t.actuator_biastype[a] !== e.mjtBias.mjBIAS_AFFINE.value) continue;
641
- const n = t.actuator_trnid[a * 2];
642
- if (n < 0 || t.jnt_type[n] !== e.mjtJoint.mjJNT_HINGE.value && t.jnt_type[n] !== e.mjtJoint.mjJNT_SLIDE.value) continue;
643
- const s = -t.actuator_biasprm[a * 10 + 1] * t.actuator_gear[a * 6] ** 2;
644
- if (!(s > 0) || !Number.isFinite(s)) continue;
645
- const c = t.jnt_dofadr[n];
646
- o.set(c, (o.get(c) ?? 0) + s);
647
- }
648
- let r = i;
649
- for (const [a, n] of o) {
650
- const s = t.dof_M0[a];
651
- !(s > 0) || !Number.isFinite(s) || (r = Math.min(r, it * Math.sqrt(s / n)));
652
- }
653
- return r;
654
- }
655
- applyInitialControls(t, i) {
656
- if (i != null && i.length) {
657
- if (i.length !== t.ctrl.length) throw w(new Error(`Initial control count ${i.length} does not match MuJoCo actuator count ${t.ctrl.length}.`), "E_MUJOCO_INITIALIZE");
658
- t.ctrl.set(i);
659
- }
660
- }
661
- applyAccelerationDrives(t, i, e, o) {
662
- if (!(e != null && e.length)) return o;
663
- if (o.length !== t.nu)
664
- throw w(new Error(`Acceleration drive control count ${o.length} does not match MuJoCo actuator count ${t.nu}.`), "E_MUJOCO_INITIALIZE");
665
- const r = new this.mujoco.DoubleBuffer(t.nv * t.nv);
666
- let a;
667
- try {
668
- this.mujoco.mj_fullM(t, i, r), a = r.GetView();
669
- } catch (s) {
670
- throw r.delete(), s;
671
- }
672
- const n = [...o];
673
- try {
674
- for (const s of e) {
675
- if (!Number.isInteger(s.actuator) || s.actuator < 0 || s.actuator >= t.nu)
676
- throw w(new Error(`Invalid acceleration actuator index: ${s.actuator}`), "E_MUJOCO_INITIALIZE");
677
- const c = this.mujoco.mj_name2id(
678
- t,
679
- this.mujoco.mjtObj.mjOBJ_JOINT.value,
680
- s.joint
681
- );
682
- if (c < 0)
683
- throw w(new Error(`Acceleration drive joint is missing: ${s.joint}`), "E_MUJOCO_INITIALIZE");
684
- const l = t.jnt_dofadr[c], m = a[l * t.nv + l];
685
- if (!(m > 0) || !Number.isFinite(m))
686
- throw w(new Error(`Acceleration drive has invalid effective inertia: ${s.joint}`), "E_MUJOCO_INITIALIZE");
687
- const h = s.stiffness * m, u = s.damping * m;
688
- if (![h, u, s.targetPosition, s.targetVelocity].every(Number.isFinite))
689
- throw w(new Error(`Acceleration drive has invalid values: ${s.joint}`), "E_MUJOCO_INITIALIZE");
690
- t.actuator_biasprm[s.actuator * 10] = 0, t.actuator_biasprm[s.actuator * 10 + 1] = -h, t.actuator_biasprm[s.actuator * 10 + 2] = -u, n[s.actuator] = h * s.targetPosition + u * s.targetVelocity;
691
- }
692
- } finally {
693
- r.delete();
694
- }
695
- return n;
696
- }
697
- /**
698
- * A point force on a light body can excite a much higher rotational
699
- * frequency than its translational mass suggests. Refine physics only while
700
- * that body is grabbed instead of adding global damping to hide the error.
701
- */
702
- grabTimestep(t, i, e, o) {
703
- if (!this.model || !this.data) return this.playbackTimestep;
704
- const r = Math.min(
705
- Math.max(this.model.body_inertia[t * 3], 1e-9),
706
- Math.max(this.model.body_inertia[t * 3 + 1], 1e-9),
707
- Math.max(this.model.body_inertia[t * 3 + 2], 1e-9)
708
- ), a = this.data.xpos.subarray(t * 3, t * 3 + 3), n = this.data.xipos.subarray(t * 3, t * 3 + 3), s = this.data.xmat.subarray(t * 9, t * 9 + 9), c = [
709
- a[0] + s[0] * i[0] + s[1] * i[1] + s[2] * i[2],
710
- a[1] + s[3] * i[0] + s[4] * i[1] + s[5] * i[2],
711
- a[2] + s[6] * i[0] + s[7] * i[1] + s[8] * i[2]
712
- ], l = (c[0] - n[0]) ** 2 + (c[1] - n[1]) ** 2 + (c[2] - n[2]) ** 2, m = Math.sqrt(o * (1 / e + l / r)), h = H / Math.max(m, 1e-9);
713
- return Math.min(this.playbackTimestep, Math.max(k, h));
714
- }
715
- setTimestep(t) {
716
- var i;
717
- this.timestep = t, (i = this.model) != null && i.opt && (this.model.opt.timestep = t);
718
- }
719
- applyInitialVelocities(t, i, e) {
720
- for (const o of e ?? []) {
721
- const r = this.mujoco.mj_name2id(t, this.mujoco.mjtObj.mjOBJ_BODY.value, o.body), a = r >= 0 ? t.body_jntadr[r] : -1;
722
- if (a < 0 || t.jnt_type[a] !== this.mujoco.mjtJoint.mjJNT_FREE.value || o.velocity.length !== 6 || !o.velocity.every(Number.isFinite))
723
- throw w(new Error(`Invalid free-body initial velocity: ${o.body}`), "E_MUJOCO_INITIALIZE");
724
- const { worldRotation: n } = this, s = new I(o.velocity[0], o.velocity[1], o.velocity[2]).applyQuaternion(n), c = new I(o.velocity[3], o.velocity[4], o.velocity[5]).applyQuaternion(n);
725
- i.qvel.set([s.x, s.y, s.z, c.x, c.y, c.z], t.jnt_dofadr[a]);
726
- }
727
- }
728
- disposeModel() {
729
- var t, i;
730
- this.clearJointTargets(), this.jointPreviewCollision = null, this.jointPreviewExclusions = null, this.endGrab(), (t = this.data) == null || t.delete(), (i = this.model) == null || i.delete(), this.data = null, this.model = null, this.initialGround = null, this.seatedGround = null, this.topLevelBodies = [], this.initialControls = [], this.initialVelocities = [], this.accumulator = 0, this.playbackTimestep = q, this.timestep = q;
731
- }
732
- }
733
- function w(_, t) {
734
- const i = _ instanceof Error ? _ : new Error(String(_));
735
- return Object.assign(i, { code: t });
736
- }
737
- export {
738
- at as MujocoRuntime
739
- };