@nebula-spatial/viewer 0.6.4 → 0.6.5

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