@nebula-spatial/viewer 0.4.5 → 0.5.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.
@@ -1,258 +0,0 @@
1
- var M = Object.defineProperty;
2
- var O = (l, t, i) => t in l ? M(l, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : l[t] = i;
3
- var _ = (l, t, i) => O(l, typeof t != "symbol" ? t + "" : t, i);
4
- import I from "@mujoco/mujoco";
5
- import T from "@mujoco/mujoco/mujoco.wasm?url";
6
- let v = 0;
7
- function G(l, t) {
8
- const i = `/viewer-simulation-${++v}`;
9
- l.mkdir(i);
10
- const n = /* @__PURE__ */ new Set([i]), o = /* @__PURE__ */ new Set(), a = (e, s) => {
11
- const m = e.replaceAll("\\", "/").split("/");
12
- if (m[0] === "" || m.some((d) => d === ".." || d.includes(":"))) throw p(`Invalid package path: ${e}`, "E_SIMULATION_PATH");
13
- const h = m.filter((d) => d && d !== ".");
14
- if (!h.length) throw p("Package path is empty.", "E_SIMULATION_PATH");
15
- let c = i;
16
- for (const d of h.slice(0, -1))
17
- c += `/${d}`, n.has(c) || (l.mkdir(c), n.add(c));
18
- const u = `${i}/${h.join("/")}`;
19
- return l.writeFile(u, s), o.add(u), u;
20
- }, r = () => {
21
- for (const e of o) l.unlink(e);
22
- o.clear();
23
- for (const e of [...n].reverse()) l.rmdir(e);
24
- n.clear();
25
- };
26
- try {
27
- for (const [e, s] of t) a(e, s);
28
- return { writeEntry: (e) => a("__viewer_entry.xml", e), dispose: r };
29
- } catch (e) {
30
- throw r(), e;
31
- }
32
- }
33
- function p(l, t) {
34
- return Object.assign(new Error(l), { code: t });
35
- }
36
- function w({
37
- target: l,
38
- point: t,
39
- velocity: i,
40
- stiffness: n,
41
- damping: o,
42
- maxForce: a,
43
- strength: r
44
- }) {
45
- const e = [
46
- n * (l[0] - t[0]) - o * i[0],
47
- n * (l[1] - t[1]) - o * i[1],
48
- n * (l[2] - t[2]) - o * i[2]
49
- ], s = Math.hypot(e[0], e[1], e[2]), m = a * r;
50
- if (s > m && s > 0) {
51
- const h = m / s;
52
- e[0] *= h, e[1] *= h, e[2] *= h;
53
- }
54
- return e;
55
- }
56
- const g = 1 / 240, F = 0.05, x = 80, J = 1.15, b = 3.5, y = 14;
57
- class S {
58
- constructor() {
59
- _(this, "mujoco", null);
60
- _(this, "model", null);
61
- _(this, "data", null);
62
- _(this, "grab", null);
63
- _(this, "lastGrabForce", [0, 0, 0]);
64
- _(this, "velocityBuffer", new Float64Array(6));
65
- _(this, "initialControls", []);
66
- _(this, "accumulator", 0);
67
- _(this, "initialGround", null);
68
- _(this, "initialVelocities", []);
69
- }
70
- async initialize(t, i) {
71
- var e;
72
- this.mujoco ?? (this.mujoco = await I({ locateFile: (s) => s.endsWith(".wasm") ? i ? new URL(s, i).href : T : s }));
73
- const n = G(this.mujoco.FS, t.files);
74
- let o = null, a = null;
75
- try {
76
- o = this.mujoco.MjModel.from_xml_path(n.writeEntry(t.xml));
77
- for (const s of t.massOverrides ?? []) {
78
- const m = this.mujoco.mj_name2id(o, this.mujoco.mjtObj.mjOBJ_BODY.value, s.body);
79
- if (m <= 0 || !Number.isFinite(s.mass) || s.mass <= 0 || !(o.body_mass[m] > 0)) throw new Error(`Invalid mass override: ${s.body}`);
80
- const h = s.mass / o.body_mass[m];
81
- o.body_mass[m] = s.mass;
82
- for (let c = 0; c < 3; c++) o.body_inertia[m * 3 + c] *= h;
83
- }
84
- a = new this.mujoco.MjData(o), (e = t.massOverrides) != null && e.length && this.mujoco.mj_setConst(o, a), o.opt.timestep = g, this.applyInitialControls(a, t.initialControls), this.applyInitialVelocities(o, a, t.initialVelocities), this.mujoco.mj_forward(o, a), this.seatDefaultGround(o, a);
85
- } catch (s) {
86
- throw a == null || a.delete(), o == null || o.delete(), f(s, "E_MUJOCO_INITIALIZE");
87
- } finally {
88
- n.dispose();
89
- }
90
- this.disposeModel(), this.model = o, this.data = a, this.initialControls = t.initialControls ?? [], this.initialVelocities = t.initialVelocities ?? [], this.accumulator = 0;
91
- const r = this.mujoco.mj_name2id(o, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
92
- this.initialGround = r < 0 ? null : { id: r, height: o.geom_pos[r * 3 + 2], sameFrame: o.geom_sameframe[r] };
93
- }
94
- get defaultGroundHeight() {
95
- if (!this.mujoco || !this.model) return;
96
- const t = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
97
- return t < 0 ? void 0 : this.model.geom_pos[t * 3 + 2];
98
- }
99
- /** Lower only the viewer-owned plane, never authored bodies or authored floors.
100
- * Run after forward kinematics, not during stepping (the floor must not follow falling bodies).
101
- */
102
- seatDefaultGround(t, i) {
103
- const n = this.mujoco, o = n.mj_name2id(t, n.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
104
- if (o < 0 || t.geom_bodyid[o] !== 0 || t.geom_type[o] !== n.mjtGeom.mjGEOM_PLANE.value) return;
105
- let a = t.geom_pos[o * 3 + 2];
106
- for (let r = 0; r < t.ngeom; r++) {
107
- if (r === o || t.geom_type[r] === n.mjtGeom.mjGEOM_PLANE.value || !(t.geom_contype[r] & t.geom_conaffinity[o]) && !(t.geom_conaffinity[r] & t.geom_contype[o])) continue;
108
- const e = r * 6, s = r * 9, m = i.geom_xmat[s + 6], h = i.geom_xmat[s + 7], c = i.geom_xmat[s + 8], u = i.geom_xpos[r * 3 + 2] + m * t.geom_aabb[e] + h * t.geom_aabb[e + 1] + c * t.geom_aabb[e + 2], d = Math.abs(m) * t.geom_aabb[e + 3] + Math.abs(h) * t.geom_aabb[e + 4] + Math.abs(c) * t.geom_aabb[e + 5], j = u - d, E = Math.max(1e-3, t.geom_margin[r] + t.geom_margin[o]);
109
- Number.isFinite(j) && j < a && (a = Math.min(a, j - E));
110
- }
111
- a !== t.geom_pos[o * 3 + 2] && (t.geom_pos[o * 3 + 2] = a, t.geom_sameframe[o] = 0, n.mj_forward(t, i));
112
- }
113
- beginGrab({
114
- bodyId: t,
115
- localPoint: i,
116
- target: n,
117
- stiffness: o = x,
118
- damping: a,
119
- maxForce: r,
120
- dampingRatio: e = J,
121
- maxTargetSpeed: s = b,
122
- targetResponse: m = y
123
- }) {
124
- var j;
125
- const h = Math.max(((j = this.model) == null ? void 0 : j.body_mass[t]) ?? 1, 1e-3), c = a ?? 2 * Math.sqrt(o * h) * e, u = r ?? Math.max(120, h * 60), d = [n[0], n[1], n[2]];
126
- this.grab = {
127
- bodyId: t,
128
- localPoint: [i[0], i[1], i[2]],
129
- target: [...d],
130
- requestedTarget: d,
131
- stiffness: o,
132
- damping: c,
133
- maxForce: u,
134
- strength: 1,
135
- maxTargetSpeed: s,
136
- targetResponse: m
137
- };
138
- }
139
- updateGrabTarget(t) {
140
- this.grab && (this.grab.requestedTarget = [t[0], t[1], t[2]]);
141
- }
142
- updateGrabStrength(t) {
143
- this.grab && (this.grab.strength = t);
144
- }
145
- applyGrabForce() {
146
- if (!this.grab || !this.model || !this.data || !this.mujoco) return;
147
- const { bodyId: t, localPoint: i, target: n, stiffness: o, damping: a, maxForce: r, strength: e } = this.grab, s = this.data.xmat.subarray(t * 9, t * 9 + 9), m = this.data.xpos.subarray(t * 3, t * 3 + 3), h = [
148
- m[0] + s[0] * i[0] + s[1] * i[1] + s[2] * i[2],
149
- m[1] + s[3] * i[0] + s[4] * i[1] + s[5] * i[2],
150
- m[2] + s[6] * i[0] + s[7] * i[1] + s[8] * i[2]
151
- ];
152
- this.mujoco.mj_objectVelocity(
153
- this.model,
154
- this.data,
155
- this.mujoco.mjtObj.mjOBJ_BODY.value,
156
- t,
157
- this.velocityBuffer,
158
- 0
159
- );
160
- const c = this.velocityBuffer, u = [
161
- h[0] - m[0],
162
- h[1] - m[1],
163
- h[2] - m[2]
164
- ], d = [
165
- c[3] + c[1] * u[2] - c[2] * u[1],
166
- c[4] + c[2] * u[0] - c[0] * u[2],
167
- c[5] + c[0] * u[1] - c[1] * u[0]
168
- ], j = w({ target: n, point: h, velocity: d, stiffness: o, damping: a, maxForce: r, strength: e });
169
- this.lastGrabForce = [...j], this.data.qfrc_applied.fill(0), this.mujoco.mj_applyFT(
170
- this.model,
171
- this.data,
172
- j,
173
- [0, 0, 0],
174
- h,
175
- t,
176
- this.data.qfrc_applied
177
- );
178
- }
179
- endGrab() {
180
- this.data && this.data.qfrc_applied.fill(0), this.lastGrabForce = [0, 0, 0], this.grab = null;
181
- }
182
- step(t) {
183
- if (!this.mujoco || !this.model || !this.data || t <= 0) return !1;
184
- this.accumulator += Math.min(t, F);
185
- let i = !1;
186
- for (; this.accumulator >= g; )
187
- this.updateGrabTargetForStep(), this.applyGrabForce(), this.mujoco.mj_step(this.model, this.data), this.accumulator -= g, i = !0;
188
- return i;
189
- }
190
- updateGrabTargetForStep() {
191
- const t = this.grab;
192
- if (!t) return;
193
- const i = t.requestedTarget ?? t.target, n = (t.maxTargetSpeed ?? b) * Math.max(1, Math.sqrt(t.strength)), o = t.targetResponse ?? y, a = 1 - Math.exp(-o * g), r = n * g;
194
- for (let e = 0; e < 3; e++) {
195
- const s = i[e] - t.target[e], m = Math.sign(s) * Math.min(Math.abs(s) * a, r);
196
- t.target[e] += m;
197
- }
198
- }
199
- setJointValues(t) {
200
- if (!(!this.mujoco || !this.model || !this.data)) {
201
- for (const [i, n] of Object.entries(t)) {
202
- const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, i);
203
- if (o < 0 || !Number.isFinite(n)) continue;
204
- const a = this.model.jnt_type[o], r = this.mujoco.mjtJoint.mjJNT_HINGE.value, e = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
205
- a !== r && a !== e || (this.data.qpos[this.model.jnt_qposadr[o]] = n);
206
- }
207
- this.mujoco.mj_forward(this.model, this.data), this.seatDefaultGround(this.model, this.data), this.accumulator = 0;
208
- }
209
- }
210
- getJointValues(t) {
211
- const i = {};
212
- if (!this.mujoco || !this.model || !this.data) return i;
213
- for (const n of t) {
214
- const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, n);
215
- if (o < 0) continue;
216
- const a = this.model.jnt_type[o], r = this.mujoco.mjtJoint.mjJNT_HINGE.value, e = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
217
- (a === r || a === e) && (i[n] = this.data.qpos[this.model.jnt_qposadr[o]]);
218
- }
219
- return i;
220
- }
221
- reset() {
222
- if (!(!this.mujoco || !this.model || !this.data)) {
223
- if (this.endGrab(), this.initialGround) {
224
- const { id: t, height: i, sameFrame: n } = this.initialGround;
225
- this.model.geom_pos[t * 3 + 2] = i, this.model.geom_sameframe[t] = n;
226
- }
227
- 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), this.accumulator = 0;
228
- }
229
- }
230
- dispose() {
231
- this.disposeModel(), this.mujoco = null;
232
- }
233
- applyInitialControls(t, i) {
234
- if (i != null && i.length) {
235
- if (i.length !== t.ctrl.length) throw f(new Error(`Initial control count ${i.length} does not match MuJoCo actuator count ${t.ctrl.length}.`), "E_MUJOCO_INITIALIZE");
236
- t.ctrl.set(i);
237
- }
238
- }
239
- applyInitialVelocities(t, i, n) {
240
- for (const o of n ?? []) {
241
- const a = this.mujoco.mj_name2id(t, this.mujoco.mjtObj.mjOBJ_BODY.value, o.body), r = a >= 0 ? t.body_jntadr[a] : -1;
242
- if (r < 0 || t.jnt_type[r] !== this.mujoco.mjtJoint.mjJNT_FREE.value || o.velocity.length !== 6 || !o.velocity.every(Number.isFinite))
243
- throw f(new Error(`Invalid free-body initial velocity: ${o.body}`), "E_MUJOCO_INITIALIZE");
244
- i.qvel.set(o.velocity, t.jnt_dofadr[r]);
245
- }
246
- }
247
- disposeModel() {
248
- var t, i;
249
- this.endGrab(), (t = this.data) == null || t.delete(), (i = this.model) == null || i.delete(), this.data = null, this.model = null, this.initialGround = null, this.initialControls = [], this.initialVelocities = [], this.accumulator = 0;
250
- }
251
- }
252
- function f(l, t) {
253
- const i = l instanceof Error ? l : new Error(String(l));
254
- return Object.assign(i, { code: t });
255
- }
256
- export {
257
- S as MujocoRuntime
258
- };