@nebula-spatial/viewer 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -0
- package/README.md +83 -4
- package/USD-VISUAL-MATERIAL-SUPPORT.md +51 -0
- package/dist/assets/types.d.ts +30 -11
- package/dist/{index-D-o1ODOX.js → index-BMpPLW5X.js} +2374 -1783
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/inspection/types.d.ts +13 -0
- package/dist/mjcf-visual-BKHVxVrs.js +73 -0
- package/dist/mujoco-runtime-7xiBQNJm.js +324 -0
- package/dist/session-BaKbYHiJ.js +809 -0
- package/dist/{session-c7xBLA4J.js → session-DN0DckL_.js} +152 -163
- package/dist/{stage-D113SHD8.js → stage-D1FYXbPa.js} +132 -71
- package/dist/types.d.ts +11 -0
- package/dist/urdf-session-Dp4X1_x8.js +3247 -0
- package/dist/usd-session-CxjXcMVI.js +1907 -0
- package/dist/xacro-BcGEW8o5.js +1537 -0
- package/package.json +4 -2
- package/dist/mjcf-visual-M1lmyjvw.js +0 -63
- package/dist/mujoco-runtime-C29PXDOl.js +0 -258
- package/dist/session-Cz6qHYMc.js +0 -676
- package/dist/urdf-session-AP-cMuwd.js +0 -3001
- package/dist/usd-session-CmSdkRw9.js +0 -1192
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import './version';
|
|
1
2
|
export type { ViewerSceneUiOptions, ViewerSceneUiParts } from './ui/types';
|
|
2
3
|
export { createViewer } from './viewer';
|
|
3
4
|
export type { AssetChangeEvent, AssetControlState, AssetControlChangeEvent, AssetDetectionResult, AssetErrorEvent, AssetErrorSnapshot, AssetFileEntry, AssetFormat, AssetLoaderContext, AssetLoaderContribution, AssetPhysicsState, AssetPresentationProfile, AssetPreview, AssetPreviewCapabilities, AssetPreviewKind, AssetPreviewPhase, AssetPreviewSnapshot, AssetResourceResolver, AssetSession, AssetSource, AssetUpAxis, NormalizedAssetSource, OpenAssetOptions, ViewerAssetOptions, } from './assets/types';
|
|
4
5
|
export type { Disposable, ToolbarItem, ViewerState, ViewerInteractionHooks, ViewerContributionPoint, ViewerPlugin, ViewerPluginContext, ViewerPluginRegistry, } from './plugins';
|
|
5
6
|
export { configureViewerText } from './cad/text';
|
|
6
7
|
export type { ViewerTextBuilderOptions } from './cad/text';
|
|
7
|
-
export type { CadBlockModel, CadDocumentHandle, CadDocumentUnits, CadFontResolver, CadFontRequest, CadResolvedFont, CadResolvedTextFont, CadTextWarmupOptions, CadHudModel, CadHudSnapshot, CadInspectorModel, CadInspectorSnapshot, CadLayerModel, CadLayerRow, CadBlockUsageRow, CadSessionPhase, CadWorldBounds, CadScaleRulerState, CadInsertDetails, CadInsertSelection, CadInsertSelectionId, CadTextDetails, CadTextSelection, CadTextSelectionId, CadSelection, CadHit, CadViewerSnapshot, CadLoadProgress, CadViewerErrorCode, CadViewerErrorEvent, CadLoadSource, CadOpenOptions, CadSessionLoadTiming, RenderDebugBreakdownEntry, SplitBlockLayerDetail, CadViewerCapability, ResolveTextFont, CadViewerEventMap, CameraStateStore, Viewer, ViewerEventMap, ViewerEnvironment, ViewerFitViewOptions, ViewerHighlightOptions, ViewerOptions, ViewerPickOptions, ViewerPickResult, ViewerPointerEvent, ViewerObjectOptions, ViewerCameraSnapshot, ViewerOrthographicSnapshot, ViewerOrbitSnapshot, ViewerRenderStats, ViewerDocumentRect, ViewerCadUiKeyboardOptions, ViewerUiOptions, ViewerCadUiParts, ViewerCadUiOptions, ViewerUiPreset, ViewerCadUiStateStore, ViewerUiTheme, ViewerViewMode, CadInteractionMode, CameraChangeReason, ViewerUiTokens, } from './types';
|
|
8
|
+
export type { CadBlockModel, CadDocumentHandle, CadDocumentUnits, CadFontResolver, CadFontRequest, CadResolvedFont, CadResolvedTextFont, CadTextWarmupOptions, CadHudModel, CadHudSnapshot, CadInspectorModel, CadInspectorSnapshot, CadLayerModel, CadLayerRow, CadBlockUsageRow, CadSessionPhase, CadWorldBounds, CadScaleRulerState, CadInsertDetails, CadInsertSelection, CadInsertSelectionId, CadTextDetails, CadTextSelection, CadTextSelectionId, CadSelection, CadHit, CadViewerSnapshot, CadLoadProgress, CadViewerErrorCode, CadViewerErrorEvent, CadLoadSource, CadOpenOptions, CadSessionLoadTiming, RenderDebugBreakdownEntry, SplitBlockLayerDetail, CadViewerCapability, ResolveTextFont, CadViewerEventMap, CameraStateStore, Viewer, ViewerEventMap, ViewerEnvironment, ViewerFitViewOptions, ViewerHighlightOptions, ViewerOptions, ViewerPickOptions, ViewerPickResult, ViewerPointerEvent, ViewerObjectOptions, ViewerPreviewDragPreset, ViewerCameraSnapshot, ViewerOrthographicSnapshot, ViewerOrbitSnapshot, ViewerRenderStats, ViewerDocumentRect, ViewerCadUiKeyboardOptions, ViewerUiOptions, ViewerCadUiParts, ViewerCadUiOptions, ViewerUiPreset, ViewerCadUiStateStore, ViewerUiTheme, ViewerViewMode, CadInteractionMode, CameraChangeReason, ViewerUiTokens, } from './types';
|
|
8
9
|
export type { AssetInspection, UrdfEntityKind, UrdfInspection, UrdfInspectionEntity, UrdfInspectionIssue, UrdfInspectionJoint, UrdfInspectionLink, UrdfInspectionMode, UrdfInspectionNode, UrdfInspectionOverlays, UrdfInspectionRobot, UrdfInspectionSnapshot, UrdfSourceRange } from './inspection/types';
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,8 @@ export interface UrdfInspectionLink extends UrdfInspectionEntity {
|
|
|
20
20
|
readonly kind: 'link';
|
|
21
21
|
readonly mass?: number;
|
|
22
22
|
readonly centerOfMass?: readonly number[];
|
|
23
|
+
/** Authored inertial frame RPY relative to the link frame, in radians. */
|
|
24
|
+
readonly inertialRotationRpy?: readonly number[];
|
|
23
25
|
readonly inertiaTensor?: readonly number[];
|
|
24
26
|
readonly inertiaMass?: number;
|
|
25
27
|
readonly visualCount: number;
|
|
@@ -40,6 +42,15 @@ export interface UrdfInspectionJoint extends UrdfInspectionEntity {
|
|
|
40
42
|
readonly effort?: number;
|
|
41
43
|
readonly velocity?: number;
|
|
42
44
|
readonly movable: boolean;
|
|
45
|
+
readonly controlLimits?: {
|
|
46
|
+
readonly lower: number;
|
|
47
|
+
readonly upper: number;
|
|
48
|
+
};
|
|
49
|
+
readonly mimic?: {
|
|
50
|
+
readonly joint: string;
|
|
51
|
+
readonly multiplier: number;
|
|
52
|
+
readonly offset: number;
|
|
53
|
+
};
|
|
43
54
|
}
|
|
44
55
|
export interface UrdfInspectionRobot extends UrdfInspectionEntity {
|
|
45
56
|
readonly kind: 'robot';
|
|
@@ -93,6 +104,8 @@ export interface UrdfInspection {
|
|
|
93
104
|
position: import('three').Vector3;
|
|
94
105
|
axis: import('three').Vector3;
|
|
95
106
|
} | null;
|
|
107
|
+
/** World-space label anchor at the own-link visual bounds center. */
|
|
108
|
+
linkWorldPosition?(linkId: string, target: import('three').Vector3): boolean;
|
|
96
109
|
pickRoots?(): readonly import('three').Object3D[];
|
|
97
110
|
}
|
|
98
111
|
/** Tagged inspection surface. Future formats can extend this union without changing AssetPreview. */
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { e as v, s as E } from "./session-BaKbYHiJ.js";
|
|
2
|
+
import { Group as q, Euler as z, BoxGeometry as G, SphereGeometry as N, CylinderGeometry as P, CapsuleGeometry as $, PlaneGeometry as I, Mesh as X } from "three";
|
|
3
|
+
import { b as j } from "./stage-D1FYXbPa.js";
|
|
4
|
+
function U(f) {
|
|
5
|
+
const y = f.extension.documents, l = new q();
|
|
6
|
+
l.name = f.name;
|
|
7
|
+
try {
|
|
8
|
+
if (y.size !== 1) throw new Error("MJCF 独立预览暂不支持 include");
|
|
9
|
+
const u = [...y.values()][0];
|
|
10
|
+
if (u.querySelector("default, include, attach, replicate, composite, flexcomp, skin, hfield, plugin")) throw new Error("MJCF 独立预览包含尚不支持的继承、组合或扩展语义");
|
|
11
|
+
const a = u.querySelector("compiler");
|
|
12
|
+
if ((a == null ? void 0 : a.getAttribute("coordinate")) === "global" || a != null && a.hasAttribute("eulerseq")) throw new Error("MJCF 独立预览不支持此坐标设置");
|
|
13
|
+
const g = (a == null ? void 0 : a.getAttribute("angle")) === "radian" ? 1 : Math.PI / 180, b = (c, m) => {
|
|
14
|
+
const e = c ? c.trim().split(/\s+/).map(Number) : m;
|
|
15
|
+
if (e.some((s) => !Number.isFinite(s))) throw new Error("MJCF 视觉坐标无效");
|
|
16
|
+
return e;
|
|
17
|
+
}, h = (c, m, e) => {
|
|
18
|
+
const s = b(c, m);
|
|
19
|
+
if (s.length !== e) throw new Error(`MJCF 视觉变换需要 ${e} 个分量`);
|
|
20
|
+
return s;
|
|
21
|
+
}, M = (c, m) => {
|
|
22
|
+
for (const e of c.children) {
|
|
23
|
+
if (!["body", "geom"].includes(e.tagName)) continue;
|
|
24
|
+
if (["class", "childclass", "fromto", "axisangle", "xyaxes", "zaxis"].some((t) => e.hasAttribute(t))) throw new Error("MJCF 独立预览不支持此变换或默认类");
|
|
25
|
+
const s = new q();
|
|
26
|
+
if (s.name = e.getAttribute("name") ?? e.tagName, s.position.fromArray(h(e.getAttribute("pos"), [0, 0, 0], 3)), e.hasAttribute("quat")) {
|
|
27
|
+
const t = h(e.getAttribute("quat"), [1, 0, 0, 0], 4);
|
|
28
|
+
if (Math.hypot(...t) === 0) throw new Error("MJCF 视觉四元数不能为零");
|
|
29
|
+
s.quaternion.set(t[1], t[2], t[3], t[0]).normalize();
|
|
30
|
+
} else {
|
|
31
|
+
const t = h(e.getAttribute("euler"), [0, 0, 0], 3);
|
|
32
|
+
s.quaternion.setFromEuler(new z(t[0] * g, t[1] * g, t[2] * g, "XYZ"));
|
|
33
|
+
}
|
|
34
|
+
if (m.add(s), e.tagName === "body") {
|
|
35
|
+
M(e, s);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const n = e.getAttribute("type") ?? (e.hasAttribute("mesh") ? "mesh" : "sphere"), r = b(e.getAttribute("size"), []);
|
|
39
|
+
if (r.length === 0) throw new Error("MJCF 独立预览缺少可直接解析的几何或材质");
|
|
40
|
+
const F = { box: 3, sphere: 1, cylinder: 2, capsule: 2, plane: 3 }[n];
|
|
41
|
+
if (F && (r.length < F || r.some((t) => n === "plane" ? t < 0 : t <= 0)))
|
|
42
|
+
throw new Error(`MJCF geom ${n} 的尺寸缺失或无效`);
|
|
43
|
+
let i;
|
|
44
|
+
if (n === "box") i = new G(2 * r[0], 2 * r[1], 2 * r[2]);
|
|
45
|
+
else if (n === "sphere") i = new N(r[0]);
|
|
46
|
+
else if (n === "cylinder")
|
|
47
|
+
i = new P(r[0], r[0], 2 * r[1]), i.rotateX(Math.PI / 2);
|
|
48
|
+
else if (n === "capsule")
|
|
49
|
+
i = new $(r[0], 2 * r[1]), i.rotateX(Math.PI / 2);
|
|
50
|
+
else if (n === "plane") i = new I(2 * (r[0] || 5), 2 * (r[1] || 5));
|
|
51
|
+
else throw new Error(`MJCF 独立预览暂不支持 geom ${n}`);
|
|
52
|
+
const p = e.getAttribute("material"), o = [...u.querySelectorAll("asset > material")].find((t) => t.getAttribute("name") === p);
|
|
53
|
+
if (p && !o) throw new Error(`Undefined MJCF material: ${p}`);
|
|
54
|
+
if (o != null && o.hasAttribute("texture") || o != null && o.querySelector("layer")) throw new Error("MJCF textured preview requires the compiled material pipeline");
|
|
55
|
+
const x = h(e.getAttribute("rgba"), [0.5, 0.5, 0.5, 1], 4), J = o && x.every((t, d) => t === [0.5, 0.5, 0.5, 1][d]) ? h(o.getAttribute("rgba"), [1, 1, 1, 1], 4) : x, w = (t, d) => o != null && o.hasAttribute(t) ? b(o.getAttribute(t), [d])[0] : d, S = v({
|
|
56
|
+
color: J,
|
|
57
|
+
roughness: o ? E(w("roughness", -1), w("shininess", 0.5)) : E(),
|
|
58
|
+
metalness: Math.max(0, w("metallic", 0)),
|
|
59
|
+
specular: w("specular", 0.5),
|
|
60
|
+
emission: w("emission", 0)
|
|
61
|
+
}), C = new X(i, S);
|
|
62
|
+
n === "plane" && m === l && (C.userData.excludeFromSimulationFit = !0), s.add(C);
|
|
63
|
+
}
|
|
64
|
+
}, A = u.querySelector("worldbody");
|
|
65
|
+
if (!A) throw new Error("MJCF 缺少 worldbody");
|
|
66
|
+
return M(A, l), l.updateMatrixWorld(!0), l;
|
|
67
|
+
} catch (u) {
|
|
68
|
+
throw j(l), u;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
U as compileMjcfPreview
|
|
73
|
+
};
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
var G = Object.defineProperty;
|
|
2
|
+
var M = (h, t, i) => t in h ? G(h, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : h[t] = i;
|
|
3
|
+
var p = (h, t, i) => M(h, typeof t != "symbol" ? t + "" : t, i);
|
|
4
|
+
import O from "@mujoco/mujoco";
|
|
5
|
+
import T from "@mujoco/mujoco/mujoco.wasm?url";
|
|
6
|
+
import { Quaternion as b, Vector3 as j } from "three";
|
|
7
|
+
let I = 0;
|
|
8
|
+
function x(h, t) {
|
|
9
|
+
const i = `/viewer-simulation-${++I}`;
|
|
10
|
+
h.mkdir(i);
|
|
11
|
+
const e = /* @__PURE__ */ new Set([i]), o = /* @__PURE__ */ new Set(), n = (s, r) => {
|
|
12
|
+
const l = s.replaceAll("\\", "/").split("/");
|
|
13
|
+
if (l[0] === "" || l.some((u) => u === ".." || u.includes(":"))) throw q(`Invalid package path: ${s}`, "E_SIMULATION_PATH");
|
|
14
|
+
const c = l.filter((u) => u && u !== ".");
|
|
15
|
+
if (!c.length) throw q("Package path is empty.", "E_SIMULATION_PATH");
|
|
16
|
+
let m = i;
|
|
17
|
+
for (const u of c.slice(0, -1))
|
|
18
|
+
m += `/${u}`, e.has(m) || (h.mkdir(m), e.add(m));
|
|
19
|
+
const d = `${i}/${c.join("/")}`;
|
|
20
|
+
return h.writeFile(d, r), o.add(d), d;
|
|
21
|
+
}, a = () => {
|
|
22
|
+
for (const s of o) h.unlink(s);
|
|
23
|
+
o.clear();
|
|
24
|
+
for (const s of [...e].reverse()) h.rmdir(s);
|
|
25
|
+
e.clear();
|
|
26
|
+
};
|
|
27
|
+
try {
|
|
28
|
+
for (const [s, r] of t) n(s, r);
|
|
29
|
+
return { writeEntry: (s) => n("__viewer_entry.xml", s), dispose: a };
|
|
30
|
+
} catch (s) {
|
|
31
|
+
throw a(), s;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function q(h, t) {
|
|
35
|
+
return Object.assign(new Error(h), { code: t });
|
|
36
|
+
}
|
|
37
|
+
function F({
|
|
38
|
+
target: h,
|
|
39
|
+
point: t,
|
|
40
|
+
velocity: i,
|
|
41
|
+
stiffness: e,
|
|
42
|
+
damping: o,
|
|
43
|
+
maxForce: n,
|
|
44
|
+
strength: a
|
|
45
|
+
}) {
|
|
46
|
+
const s = [
|
|
47
|
+
e * (h[0] - t[0]) - o * i[0],
|
|
48
|
+
e * (h[1] - t[1]) - o * i[1],
|
|
49
|
+
e * (h[2] - t[2]) - o * i[2]
|
|
50
|
+
], r = Math.hypot(s[0], s[1], s[2]), l = n * a;
|
|
51
|
+
if (r > l && r > 0) {
|
|
52
|
+
const c = l / r;
|
|
53
|
+
s[0] *= c, s[1] *= c, s[2] *= c;
|
|
54
|
+
}
|
|
55
|
+
return s;
|
|
56
|
+
}
|
|
57
|
+
const f = 1 / 240, J = 0.05, N = 80, A = 1.15, v = 3.5, w = 14;
|
|
58
|
+
class R {
|
|
59
|
+
constructor() {
|
|
60
|
+
p(this, "mujoco", null);
|
|
61
|
+
p(this, "model", null);
|
|
62
|
+
p(this, "data", null);
|
|
63
|
+
p(this, "grab", null);
|
|
64
|
+
p(this, "lastGrabForce", [0, 0, 0]);
|
|
65
|
+
p(this, "velocityBuffer", new Float64Array(6));
|
|
66
|
+
p(this, "initialControls", []);
|
|
67
|
+
p(this, "accumulator", 0);
|
|
68
|
+
/** Authored ground pose, captured before any seating. Re-seats start here. */
|
|
69
|
+
p(this, "initialGround", null);
|
|
70
|
+
/** Ground pose currently in the model; restored verbatim by reset(). */
|
|
71
|
+
p(this, "seatedGround", null);
|
|
72
|
+
p(this, "worldRotation", new b());
|
|
73
|
+
p(this, "topLevelBodies", []);
|
|
74
|
+
p(this, "initialVelocities", []);
|
|
75
|
+
}
|
|
76
|
+
async initialize(t, i) {
|
|
77
|
+
var r;
|
|
78
|
+
this.mujoco ?? (this.mujoco = await O({ locateFile: (l) => l.endsWith(".wasm") ? i ? new URL(l, i).href : T : l }));
|
|
79
|
+
const e = x(this.mujoco.FS, t.files);
|
|
80
|
+
let o = null, n = null, a = null, s = null;
|
|
81
|
+
try {
|
|
82
|
+
o = this.mujoco.MjModel.from_xml_path(e.writeEntry(t.xml));
|
|
83
|
+
for (const c of t.massOverrides ?? []) {
|
|
84
|
+
const m = this.mujoco.mj_name2id(o, this.mujoco.mjtObj.mjOBJ_BODY.value, c.body);
|
|
85
|
+
if (m <= 0 || !Number.isFinite(c.mass) || c.mass <= 0 || !(o.body_mass[m] > 0)) throw new Error(`Invalid mass override: ${c.body}`);
|
|
86
|
+
const d = c.mass / o.body_mass[m];
|
|
87
|
+
o.body_mass[m] = c.mass;
|
|
88
|
+
for (let u = 0; u < 3; u++) o.body_inertia[m * 3 + u] *= d;
|
|
89
|
+
}
|
|
90
|
+
n = new this.mujoco.MjData(o), (r = t.massOverrides) != null && r.length && this.mujoco.mj_setConst(o, n), o.opt.timestep = f, this.worldRotation.identity(), this.applyInitialControls(n, t.initialControls), this.applyInitialVelocities(o, n, t.initialVelocities), this.mujoco.mj_forward(o, n);
|
|
91
|
+
const l = this.mujoco.mj_name2id(o, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
|
|
92
|
+
a = l < 0 ? null : { id: l, height: o.geom_pos[l * 3 + 2], sameFrame: o.geom_sameframe[l] }, s = this.seatDefaultGround(o, n);
|
|
93
|
+
} catch (l) {
|
|
94
|
+
throw n == null || n.delete(), o == null || o.delete(), g(l, "E_MUJOCO_INITIALIZE");
|
|
95
|
+
} finally {
|
|
96
|
+
e.dispose();
|
|
97
|
+
}
|
|
98
|
+
this.disposeModel(), this.model = o, this.data = n, this.initialControls = t.initialControls ?? [], this.initialVelocities = t.initialVelocities ?? [], this.accumulator = 0, this.initialGround = a, this.seatedGround = s, this.topLevelBodies = this.captureTopLevelBodies(o);
|
|
99
|
+
}
|
|
100
|
+
get defaultGroundHeight() {
|
|
101
|
+
if (!this.mujoco || !this.model) return;
|
|
102
|
+
const t = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
|
|
103
|
+
return t < 0 ? void 0 : this.model.geom_pos[t * 3 + 2];
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Re-orients the compiled physics to the given authored up axis by rotating
|
|
107
|
+
* every top-level body about the world origin. Gravity stays -Z and the
|
|
108
|
+
* viewer ground stays the authored horizontal plane, so a cup that was
|
|
109
|
+
* standing now genuinely topples and settles on its side — the alternative,
|
|
110
|
+
* rotating only the rendered scene graph, would leave the visuals and the
|
|
111
|
+
* simulation in different frames.
|
|
112
|
+
*
|
|
113
|
+
* The full current state (free-joint positions, orientations and velocities)
|
|
114
|
+
* is rotated by the same delta, so a paused or running simulation is carried
|
|
115
|
+
* over instead of being re-seeded.
|
|
116
|
+
*/
|
|
117
|
+
setWorldRotation(t) {
|
|
118
|
+
var n;
|
|
119
|
+
if (!this.mujoco || !this.model || !this.data) return;
|
|
120
|
+
const i = this.model, e = this.data;
|
|
121
|
+
if (t.equals(this.worldRotation)) return;
|
|
122
|
+
const o = t.clone().multiply(this.worldRotation.clone().invert());
|
|
123
|
+
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, (n = this.initialGround) == null ? void 0 : n.height), this.accumulator = 0;
|
|
124
|
+
}
|
|
125
|
+
captureTopLevelBodies(t) {
|
|
126
|
+
const i = this.mujoco, e = [];
|
|
127
|
+
for (let o = 1; o < t.nbody; o++) {
|
|
128
|
+
if (t.body_parentid[o] !== 0) continue;
|
|
129
|
+
const n = t.body_jntadr[o], a = t.body_jntnum[o] === 1 && n >= 0 && t.jnt_type[n] === i.mjtJoint.mjJNT_FREE.value ? n : -1, s = o * 3, r = o * 4;
|
|
130
|
+
e.push({
|
|
131
|
+
id: o,
|
|
132
|
+
position: new j(t.body_pos[s], t.body_pos[s + 1], t.body_pos[s + 2]),
|
|
133
|
+
quaternion: new b(t.body_quat[r + 1], t.body_quat[r + 2], t.body_quat[r + 3], t.body_quat[r]),
|
|
134
|
+
freeJoint: a
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return e;
|
|
138
|
+
}
|
|
139
|
+
/** Writes the rotated reference pose, which reset() and fixed bases follow. */
|
|
140
|
+
rotateReferenceFrames(t, i) {
|
|
141
|
+
const e = this.model;
|
|
142
|
+
for (const o of t) {
|
|
143
|
+
const n = o.position.clone().applyQuaternion(i), a = i.clone().multiply(o.quaternion), s = o.id * 3, r = o.id * 4;
|
|
144
|
+
if (e.body_pos[s] = n.x, e.body_pos[s + 1] = n.y, e.body_pos[s + 2] = n.z, e.body_quat[r] = a.w, e.body_quat[r + 1] = a.x, e.body_quat[r + 2] = a.y, e.body_quat[r + 3] = a.z, e.body_sameframe[o.id] && (e.body_sameframe[o.id] = 0), o.freeJoint < 0) continue;
|
|
145
|
+
const l = e.jnt_qposadr[o.freeJoint];
|
|
146
|
+
e.qpos0[l] = n.x, e.qpos0[l + 1] = n.y, e.qpos0[l + 2] = n.z, e.qpos0[l + 3] = a.w, e.qpos0[l + 4] = a.x, e.qpos0[l + 5] = a.y, e.qpos0[l + 6] = a.z;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** Rotates the live free-joint state so the simulation keeps its progress. */
|
|
150
|
+
rotateState(t, i, e, o) {
|
|
151
|
+
for (const n of t) {
|
|
152
|
+
if (n.freeJoint < 0) continue;
|
|
153
|
+
const a = i.jnt_qposadr[n.freeJoint], s = i.jnt_dofadr[n.freeJoint], r = new j(e.qpos[a], e.qpos[a + 1], e.qpos[a + 2]).applyQuaternion(o);
|
|
154
|
+
e.qpos[a] = r.x, e.qpos[a + 1] = r.y, e.qpos[a + 2] = r.z;
|
|
155
|
+
const l = new b(e.qpos[a + 4], e.qpos[a + 5], e.qpos[a + 6], e.qpos[a + 3]).premultiply(o);
|
|
156
|
+
e.qpos[a + 3] = l.w, e.qpos[a + 4] = l.x, e.qpos[a + 5] = l.y, e.qpos[a + 6] = l.z;
|
|
157
|
+
const c = new j(e.qvel[s], e.qvel[s + 1], e.qvel[s + 2]).applyQuaternion(o);
|
|
158
|
+
e.qvel[s] = c.x, e.qvel[s + 1] = c.y, e.qvel[s + 2] = c.z;
|
|
159
|
+
const m = new j(e.qvel[s + 3], e.qvel[s + 4], e.qvel[s + 5]).applyQuaternion(o);
|
|
160
|
+
e.qvel[s + 3] = m.x, e.qvel[s + 4] = m.y, e.qvel[s + 5] = m.z;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** Lower only the viewer-owned plane, never authored bodies or authored floors.
|
|
164
|
+
* Run after forward kinematics, not during stepping (the floor must not follow falling bodies).
|
|
165
|
+
* `baseHeight` re-seats from a stable reference; omitting it lowers from the current pose.
|
|
166
|
+
*/
|
|
167
|
+
seatDefaultGround(t, i, e) {
|
|
168
|
+
const o = this.mujoco, n = o.mj_name2id(t, o.mjtObj.mjOBJ_GEOM.value, "__viewer_default_ground__");
|
|
169
|
+
if (n < 0 || t.geom_bodyid[n] !== 0 || t.geom_type[n] !== o.mjtGeom.mjGEOM_PLANE.value) return null;
|
|
170
|
+
let a = e ?? t.geom_pos[n * 3 + 2];
|
|
171
|
+
for (let s = 0; s < t.ngeom; s++) {
|
|
172
|
+
if (s === n || t.geom_type[s] === o.mjtGeom.mjGEOM_PLANE.value || !(t.geom_contype[s] & t.geom_conaffinity[n]) && !(t.geom_conaffinity[s] & t.geom_contype[n])) continue;
|
|
173
|
+
const r = s * 6, l = s * 9, c = i.geom_xmat[l + 6], m = i.geom_xmat[l + 7], d = i.geom_xmat[l + 8], u = i.geom_xpos[s * 3 + 2] + c * t.geom_aabb[r] + m * t.geom_aabb[r + 1] + d * t.geom_aabb[r + 2], _ = Math.abs(c) * t.geom_aabb[r + 3] + Math.abs(m) * t.geom_aabb[r + 4] + Math.abs(d) * t.geom_aabb[r + 5], y = u - _, E = Math.max(1e-3, t.geom_margin[s] + t.geom_margin[n]);
|
|
174
|
+
Number.isFinite(y) && y < a && (a = Math.min(a, y - E));
|
|
175
|
+
}
|
|
176
|
+
return a !== t.geom_pos[n * 3 + 2] && (t.geom_pos[n * 3 + 2] = a, t.geom_sameframe[n] = 0, o.mj_forward(t, i)), { id: n, height: a, sameFrame: t.geom_sameframe[n] };
|
|
177
|
+
}
|
|
178
|
+
beginGrab({
|
|
179
|
+
bodyId: t,
|
|
180
|
+
localPoint: i,
|
|
181
|
+
target: e,
|
|
182
|
+
stiffness: o = N,
|
|
183
|
+
damping: n,
|
|
184
|
+
maxForce: a,
|
|
185
|
+
dampingRatio: s = A,
|
|
186
|
+
maxTargetSpeed: r = v,
|
|
187
|
+
targetResponse: l = w
|
|
188
|
+
}) {
|
|
189
|
+
var _;
|
|
190
|
+
const c = Math.max(((_ = this.model) == null ? void 0 : _.body_mass[t]) ?? 1, 1e-3), m = n ?? 2 * Math.sqrt(o * c) * s, d = a ?? Math.max(120, c * 60), u = [e[0], e[1], e[2]];
|
|
191
|
+
this.grab = {
|
|
192
|
+
bodyId: t,
|
|
193
|
+
localPoint: [i[0], i[1], i[2]],
|
|
194
|
+
target: [...u],
|
|
195
|
+
requestedTarget: u,
|
|
196
|
+
stiffness: o,
|
|
197
|
+
damping: m,
|
|
198
|
+
maxForce: d,
|
|
199
|
+
strength: 1,
|
|
200
|
+
maxTargetSpeed: r,
|
|
201
|
+
targetResponse: l
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
updateGrabTarget(t) {
|
|
205
|
+
this.grab && (this.grab.requestedTarget = [t[0], t[1], t[2]]);
|
|
206
|
+
}
|
|
207
|
+
updateGrabStrength(t) {
|
|
208
|
+
this.grab && (this.grab.strength = t);
|
|
209
|
+
}
|
|
210
|
+
applyGrabForce() {
|
|
211
|
+
if (!this.grab || !this.model || !this.data || !this.mujoco) return;
|
|
212
|
+
const { bodyId: t, localPoint: i, target: e, stiffness: o, damping: n, maxForce: a, strength: s } = this.grab, r = this.data.xmat.subarray(t * 9, t * 9 + 9), l = this.data.xpos.subarray(t * 3, t * 3 + 3), c = [
|
|
213
|
+
l[0] + r[0] * i[0] + r[1] * i[1] + r[2] * i[2],
|
|
214
|
+
l[1] + r[3] * i[0] + r[4] * i[1] + r[5] * i[2],
|
|
215
|
+
l[2] + r[6] * i[0] + r[7] * i[1] + r[8] * i[2]
|
|
216
|
+
];
|
|
217
|
+
this.mujoco.mj_objectVelocity(
|
|
218
|
+
this.model,
|
|
219
|
+
this.data,
|
|
220
|
+
this.mujoco.mjtObj.mjOBJ_BODY.value,
|
|
221
|
+
t,
|
|
222
|
+
this.velocityBuffer,
|
|
223
|
+
0
|
|
224
|
+
);
|
|
225
|
+
const m = this.velocityBuffer, d = [
|
|
226
|
+
c[0] - l[0],
|
|
227
|
+
c[1] - l[1],
|
|
228
|
+
c[2] - l[2]
|
|
229
|
+
], u = [
|
|
230
|
+
m[3] + m[1] * d[2] - m[2] * d[1],
|
|
231
|
+
m[4] + m[2] * d[0] - m[0] * d[2],
|
|
232
|
+
m[5] + m[0] * d[1] - m[1] * d[0]
|
|
233
|
+
], _ = F({ target: e, point: c, velocity: u, stiffness: o, damping: n, maxForce: a, strength: s });
|
|
234
|
+
this.lastGrabForce = [..._], this.data.qfrc_applied.fill(0), this.mujoco.mj_applyFT(
|
|
235
|
+
this.model,
|
|
236
|
+
this.data,
|
|
237
|
+
_,
|
|
238
|
+
[0, 0, 0],
|
|
239
|
+
c,
|
|
240
|
+
t,
|
|
241
|
+
this.data.qfrc_applied
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
endGrab() {
|
|
245
|
+
this.data && this.data.qfrc_applied.fill(0), this.lastGrabForce = [0, 0, 0], this.grab = null;
|
|
246
|
+
}
|
|
247
|
+
step(t) {
|
|
248
|
+
if (!this.mujoco || !this.model || !this.data || t <= 0) return !1;
|
|
249
|
+
this.accumulator += Math.min(t, J);
|
|
250
|
+
let i = !1;
|
|
251
|
+
for (; this.accumulator >= f; )
|
|
252
|
+
this.updateGrabTargetForStep(), this.applyGrabForce(), this.mujoco.mj_step(this.model, this.data), this.accumulator -= f, i = !0;
|
|
253
|
+
return i;
|
|
254
|
+
}
|
|
255
|
+
updateGrabTargetForStep() {
|
|
256
|
+
const t = this.grab;
|
|
257
|
+
if (!t) return;
|
|
258
|
+
const i = t.requestedTarget ?? t.target, e = (t.maxTargetSpeed ?? v) * Math.max(1, Math.sqrt(t.strength)), o = t.targetResponse ?? w, n = 1 - Math.exp(-o * f), a = e * f;
|
|
259
|
+
for (let s = 0; s < 3; s++) {
|
|
260
|
+
const r = i[s] - t.target[s], l = Math.sign(r) * Math.min(Math.abs(r) * n, a);
|
|
261
|
+
t.target[s] += l;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
setJointValues(t) {
|
|
265
|
+
if (!(!this.mujoco || !this.model || !this.data)) {
|
|
266
|
+
for (const [i, e] of Object.entries(t)) {
|
|
267
|
+
const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, i);
|
|
268
|
+
if (o < 0 || !Number.isFinite(e)) continue;
|
|
269
|
+
const n = this.model.jnt_type[o], a = this.mujoco.mjtJoint.mjJNT_HINGE.value, s = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
|
|
270
|
+
n !== a && n !== s || (this.data.qpos[this.model.jnt_qposadr[o]] = e);
|
|
271
|
+
}
|
|
272
|
+
this.mujoco.mj_forward(this.model, this.data), this.seatDefaultGround(this.model, this.data), this.accumulator = 0;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
getJointValues(t) {
|
|
276
|
+
const i = {};
|
|
277
|
+
if (!this.mujoco || !this.model || !this.data) return i;
|
|
278
|
+
for (const e of t) {
|
|
279
|
+
const o = this.mujoco.mj_name2id(this.model, this.mujoco.mjtObj.mjOBJ_JOINT.value, e);
|
|
280
|
+
if (o < 0) continue;
|
|
281
|
+
const n = this.model.jnt_type[o], a = this.mujoco.mjtJoint.mjJNT_HINGE.value, s = this.mujoco.mjtJoint.mjJNT_SLIDE.value;
|
|
282
|
+
(n === a || n === s) && (i[e] = this.data.qpos[this.model.jnt_qposadr[o]]);
|
|
283
|
+
}
|
|
284
|
+
return i;
|
|
285
|
+
}
|
|
286
|
+
reset() {
|
|
287
|
+
if (!(!this.mujoco || !this.model || !this.data)) {
|
|
288
|
+
if (this.endGrab(), this.seatedGround) {
|
|
289
|
+
const { id: t, height: i, sameFrame: e } = this.seatedGround;
|
|
290
|
+
this.model.geom_pos[t * 3 + 2] = i, this.model.geom_sameframe[t] = e;
|
|
291
|
+
}
|
|
292
|
+
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;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
dispose() {
|
|
296
|
+
this.disposeModel(), this.mujoco = null;
|
|
297
|
+
}
|
|
298
|
+
applyInitialControls(t, i) {
|
|
299
|
+
if (i != null && i.length) {
|
|
300
|
+
if (i.length !== t.ctrl.length) throw g(new Error(`Initial control count ${i.length} does not match MuJoCo actuator count ${t.ctrl.length}.`), "E_MUJOCO_INITIALIZE");
|
|
301
|
+
t.ctrl.set(i);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
applyInitialVelocities(t, i, e) {
|
|
305
|
+
for (const o of e ?? []) {
|
|
306
|
+
const n = this.mujoco.mj_name2id(t, this.mujoco.mjtObj.mjOBJ_BODY.value, o.body), a = n >= 0 ? t.body_jntadr[n] : -1;
|
|
307
|
+
if (a < 0 || t.jnt_type[a] !== this.mujoco.mjtJoint.mjJNT_FREE.value || o.velocity.length !== 6 || !o.velocity.every(Number.isFinite))
|
|
308
|
+
throw g(new Error(`Invalid free-body initial velocity: ${o.body}`), "E_MUJOCO_INITIALIZE");
|
|
309
|
+
const { worldRotation: s } = this, r = new j(o.velocity[0], o.velocity[1], o.velocity[2]).applyQuaternion(s), l = new j(o.velocity[3], o.velocity[4], o.velocity[5]).applyQuaternion(s);
|
|
310
|
+
i.qvel.set([r.x, r.y, r.z, l.x, l.y, l.z], t.jnt_dofadr[a]);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
disposeModel() {
|
|
314
|
+
var t, i;
|
|
315
|
+
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;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function g(h, t) {
|
|
319
|
+
const i = h instanceof Error ? h : new Error(String(h));
|
|
320
|
+
return Object.assign(i, { code: t });
|
|
321
|
+
}
|
|
322
|
+
export {
|
|
323
|
+
R as MujocoRuntime
|
|
324
|
+
};
|