@nebula-spatial/viewer 0.5.6 → 0.5.8

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,3263 +0,0 @@
1
- var dn = Object.defineProperty;
2
- var pn = (n, t, i) => t in n ? dn(n, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : n[t] = i;
3
- var $ = (n, t, i) => pn(n, typeof t != "symbol" ? t + "" : t, i);
4
- import { d as mn, e as hn, c as yn, a as gn, b as ri, g as wn, w as ai, h as ci, S as li, p as bn } from "./collision-debug-CqRm6cC8.js";
5
- import { S as xn, m as ui } from "./stage-CSRMl8kx.js";
6
- import { Quaternion as K, Euler as Zt, LoadingManager as Oi, Group as lt, Mesh as N, MeshLambertMaterial as An, MeshPhongMaterial as fi, MeshStandardMaterial as He, Matrix4 as H, TextureLoader as Mn, SRGBColorSpace as vn, RepeatWrapping as Pn, BoxGeometry as Ze, SphereGeometry as Qe, CylinderGeometry as Vi, CapsuleGeometry as Wi, Vector3 as z, MathUtils as Sn, Matrix3 as di, Box3 as it, BufferAttribute as Gi, Line3 as ut, Plane as Xi, Vector2 as yt, Triangle as $t, BackSide as Tn, DoubleSide as Rn, REVISION as Ji, Ray as Bn, FrontSide as pi, LineSegments as Ye, LineLoop as Hi, Line as Ke, Points as Zi, Sphere as _n, BatchedMesh as En, AxesHelper as zn, ArrowHelper as mi, BufferGeometry as Un, LineBasicMaterial as hi, Float32BufferAttribute as Fn, MeshBasicMaterial as yi, EdgesGeometry as $n } from "three";
7
- import { c as gi } from "./session-DgvOdcLe.js";
8
- import { GLTFLoader as In } from "three/examples/jsm/loaders/GLTFLoader.js";
9
- import { ColladaLoader as Dn } from "three/examples/jsm/loaders/ColladaLoader.js";
10
- import { disposeModelResources as ft } from "./resources-CHhCm2f7.js";
11
- import { OBJLoader as Ln } from "three/examples/jsm/loaders/OBJLoader.js";
12
- import { STLLoader as kn } from "three/examples/jsm/loaders/STLLoader.js";
13
- function Qi(n) {
14
- const t = new Map(n.map((o) => [o.id, o])), i = /* @__PURE__ */ new Map(), e = /* @__PURE__ */ new Set(), s = (o) => {
15
- throw Object.assign(new Error(`Invalid URDF mimic relationship: ${o}`), { code: "E_URDF_MIMIC" });
16
- }, r = (o) => {
17
- var l;
18
- const d = i.get(o);
19
- if (d) return d;
20
- const a = t.get(o);
21
- if (!a || e.has(o)) return s(o);
22
- e.add(o);
23
- let f = { root: o, multiplier: 1, offset: 0 };
24
- if (a.mimic) {
25
- const p = a.mimic;
26
- if (![p.multiplier, p.offset].every(Number.isFinite) || !["revolute", "continuous", "prismatic"].includes(a.type) || !["revolute", "continuous", "prismatic"].includes(((l = t.get(p.joint)) == null ? void 0 : l.type) ?? "")) return s(o);
27
- const c = r(p.joint);
28
- if (f = { root: c.root, multiplier: p.multiplier * c.multiplier, offset: p.multiplier * c.offset + p.offset }, ![f.multiplier, f.offset].every(Number.isFinite)) return s(o);
29
- }
30
- return e.delete(o), i.set(o, f), f;
31
- };
32
- for (const o of n) r(o.id);
33
- return i;
34
- }
35
- function Ae(n) {
36
- const t = Qi(n), i = /* @__PURE__ */ new Map();
37
- for (const e of n) {
38
- const s = t.get(e.id);
39
- if (i.has(s.root) || i.set(s.root, { lower: -1 / 0, upper: 1 / 0 }), e.type === "continuous" || !e.limits || e.limits.lower > e.limits.upper) continue;
40
- const r = i.get(s.root);
41
- if (s.multiplier === 0) {
42
- if (s.offset < e.limits.lower || s.offset > e.limits.upper) throw new Error(`URDF mimic constant violates limits: ${e.id}`);
43
- continue;
44
- }
45
- const o = (e.limits.lower - s.offset) / s.multiplier, d = (e.limits.upper - s.offset) / s.multiplier;
46
- if (r.lower = Math.max(r.lower, Math.min(o, d)), r.upper = Math.min(r.upper, Math.max(o, d)), r.lower > r.upper) throw new Error(`URDF mimic limits have no feasible range: ${s.root}`);
47
- }
48
- return i;
49
- }
50
- function ti(n, t = {}) {
51
- const i = Qi(n), e = Ae(n);
52
- return Object.fromEntries(n.map((s) => {
53
- const r = i.get(s.id), o = e.get(r.root), d = Math.max(o.lower, Math.min(o.upper, t[r.root] ?? 0));
54
- return [s.id, d * r.multiplier + r.offset];
55
- }));
56
- }
57
- function Qt(n) {
58
- return n.rotationQuaternion ? new K().fromArray(n.rotationQuaternion).normalize() : new K().setFromEuler(new Zt(n.rotationRpy[0], n.rotationRpy[1], n.rotationRpy[2], "ZYX"));
59
- }
60
- function jn(n) {
61
- const t = /* @__PURE__ */ new Map(), i = (e) => {
62
- if (!(e instanceof An || e instanceof fi)) return e;
63
- const s = t.get(e);
64
- if (s) return s;
65
- const r = new He({
66
- name: e.name,
67
- color: e.color.clone(),
68
- emissive: e.emissive.clone(),
69
- emissiveIntensity: e.emissiveIntensity,
70
- map: e.map,
71
- emissiveMap: e.emissiveMap,
72
- normalMap: e.normalMap,
73
- bumpMap: e.bumpMap,
74
- bumpScale: e.bumpScale,
75
- alphaMap: e.alphaMap,
76
- aoMap: e.aoMap,
77
- aoMapIntensity: e.aoMapIntensity,
78
- lightMap: e.lightMap,
79
- lightMapIntensity: e.lightMapIntensity,
80
- opacity: e.opacity,
81
- transparent: e.transparent,
82
- alphaTest: e.alphaTest,
83
- side: e.side,
84
- depthWrite: e.depthWrite,
85
- vertexColors: e.vertexColors,
86
- flatShading: e.flatShading,
87
- roughness: e instanceof fi ? Math.sqrt(2 / (Math.max(0, e.shininess) + 2)) : 0.5,
88
- metalness: 0
89
- });
90
- return r.normalScale.copy(e.normalScale), r.normalMapType = e.normalMapType, r.userData = { ...e.userData, colladaSourceMaterial: e.type }, t.set(e, r), r;
91
- };
92
- n.traverse((e) => {
93
- e instanceof N && (e.material = Array.isArray(e.material) ? e.material.map(i) : i(e.material));
94
- });
95
- for (const e of t.keys()) e.dispose();
96
- }
97
- async function Nn(n, t) {
98
- var p, c, u;
99
- const i = await t.text(n), e = new DOMParser().parseFromString(i, "application/xml");
100
- if (e.querySelector("parsererror") || e.documentElement.localName !== "COLLADA")
101
- throw new Error(`Invalid URDF COLLADA document: ${n}`);
102
- const s = ((c = (p = e.querySelector("asset > up_axis")) == null ? void 0 : p.textContent) == null ? void 0 : c.trim()) ?? "Y_UP";
103
- if (!["X_UP", "Y_UP", "Z_UP"].includes(s)) throw new Error(`Unsupported COLLADA up-axis: ${s}`);
104
- const r = /* @__PURE__ */ new Map();
105
- for (const y of e.querySelectorAll("library_images image init_from")) {
106
- const g = (u = y.textContent) == null ? void 0 : u.trim();
107
- if (!g || g.startsWith("data:")) continue;
108
- const x = t.resolve(g, n);
109
- await t.ensure(x), r.set(g, t.objectUrl(x));
110
- }
111
- const o = new Oi();
112
- o.setURLModifier((y) => {
113
- if (y.startsWith("data:")) return y;
114
- const g = r.get(y);
115
- if (!g) throw new Error(`Unresolved COLLADA image: ${y}`);
116
- return g;
117
- });
118
- let d;
119
- const a = new Promise((y) => {
120
- d = y;
121
- });
122
- let f;
123
- o.onLoad = d, o.onError = (y) => {
124
- f = y;
125
- }, o.itemStart("urdf-collada-parse");
126
- const l = new lt();
127
- try {
128
- const y = new Dn(o).parse(i, "");
129
- if (!(y != null && y.scene)) throw new Error(`COLLADA has no scene: ${n}`);
130
- if (l.add(y.scene), s === "X_UP" ? y.scene.rotateY(-Math.PI / 2) : y.scene.rotateX(Math.PI / 2), o.itemEnd("urdf-collada-parse"), await a, f) throw new Error(`COLLADA texture failed to load: ${f}`);
131
- let g = 0;
132
- if (l.traverse((x) => {
133
- x instanceof N && g++;
134
- }), !g) throw new Error(`COLLADA has no visual meshes: ${n}`);
135
- return jn(l), l.updateMatrixWorld(!0), l;
136
- } catch (y) {
137
- throw ft(l), y;
138
- }
139
- }
140
- function Yi(n) {
141
- const [t, i, e, s, r, o] = n;
142
- if (![t, i, e, s, r, o].every(Number.isFinite)) return !1;
143
- const d = t * i - s * s, a = t * (i * e - o * o) - s * (s * e - o * r) + r * (s * o - i * r);
144
- return t > 0 && d > 0 && a > 0 ? t <= i + e + 1e-12 && i <= t + e + 1e-12 && e <= t + i + 1e-12 : !1;
145
- }
146
- function Cn(n, t) {
147
- if (!(t > 0) || !Number.isFinite(t) || n.length !== 6 || !n.every(Number.isFinite)) return null;
148
- const i = [[n[0], n[3], n[4]], [n[3], n[1], n[5]], [n[4], n[5], n[2]]], e = [[1, 0, 0], [0, 1, 0], [0, 0, 1]], s = Math.max(...n.map(Math.abs));
149
- for (let a = 0; a < 40; a++) {
150
- let f = 0, l = 1;
151
- for (const [w, m] of [[0, 1], [0, 2], [1, 2]]) Math.abs(i[w][m]) > Math.abs(i[f][l]) && (f = w, l = m);
152
- if (Math.abs(i[f][l]) <= s * 1e-14) break;
153
- const p = 0.5 * Math.atan2(2 * i[f][l], i[l][l] - i[f][f]), c = Math.cos(p), u = Math.sin(p), y = i[f][f], g = i[l][l], x = i[f][l];
154
- for (let w = 0; w < 3; w++) {
155
- if (w !== f && w !== l) {
156
- const b = i[w][f], A = i[w][l];
157
- i[w][f] = i[f][w] = c * b - u * A, i[w][l] = i[l][w] = u * b + c * A;
158
- }
159
- const m = e[w][f], h = e[w][l];
160
- e[w][f] = c * m - u * h, e[w][l] = u * m + c * h;
161
- }
162
- i[f][f] = c * c * y - 2 * u * c * x + u * u * g, i[l][l] = u * u * y + 2 * u * c * x + c * c * g, i[f][l] = i[l][f] = 0;
163
- }
164
- const r = [i[0][0], i[1][1], i[2][2]], o = r.map((a, f) => 5 * (r[(f + 1) % 3] + r[(f + 2) % 3] - a) / (2 * t));
165
- if (r.some((a) => a <= 0) || o.some((a) => a < -s * 1e-10 / t)) return null;
166
- const d = new H().set(e[0][0], e[0][1], e[0][2], 0, e[1][0], e[1][1], e[1][2], 0, e[2][0], e[2][1], e[2][2], 0, 0, 0, 0, 1);
167
- return { radii: o.map((a) => Math.sqrt(Math.max(0, a))), rotation: new K().setFromRotationMatrix(d) };
168
- }
169
- function qn(n, t) {
170
- const i = Cn(n, t);
171
- return i ? { size: i.radii.map((e) => e * Math.sqrt(12 / 5)), rotation: i.rotation } : null;
172
- }
173
- function Lt(n, t) {
174
- if (!n) return [...t];
175
- const i = n.trim().split(/\s+/).map(Number);
176
- if (i.some((e) => !Number.isFinite(e))) throw O(`Invalid numeric value: ${n}`, "E_URDF_PARSE");
177
- return i;
178
- }
179
- function ye(n) {
180
- const t = (n == null ? void 0 : n.querySelector("origin")) ?? null, i = Lt((t == null ? void 0 : t.getAttribute("quat_xyzw")) ?? null, []);
181
- if (i.length !== 0 && i.length !== 4) throw O("URDF origin quat_xyzw requires four values.", "E_URDF_PARSE");
182
- if (i.length && (t != null && t.hasAttribute("rpy"))) throw O("URDF origin cannot declare both rpy and quat_xyzw.", "E_URDF_PARSE");
183
- return {
184
- position: Lt((t == null ? void 0 : t.getAttribute("xyz")) ?? null, [0, 0, 0]),
185
- rotationRpy: Lt((t == null ? void 0 : t.getAttribute("rpy")) ?? null, [0, 0, 0]),
186
- ...i.length ? { rotationQuaternion: i } : {}
187
- };
188
- }
189
- function wi(n, t, i) {
190
- const e = n.querySelector("geometry"), s = e == null ? void 0 : e.querySelector("box");
191
- if (s) return { type: "box", size: Qn(s.getAttribute("size"), 3, "box size") };
192
- const r = e == null ? void 0 : e.querySelector("sphere");
193
- if (r) return { type: "sphere", radius: jt(r.getAttribute("radius"), "sphere radius") };
194
- const o = e == null ? void 0 : e.querySelector("cylinder");
195
- if (o) return { type: "cylinder", radius: jt(o.getAttribute("radius"), "cylinder radius"), length: jt(o.getAttribute("length"), "cylinder length") };
196
- const d = e == null ? void 0 : e.querySelector("capsule");
197
- if (d) return { type: "capsule", radius: jt(d.getAttribute("radius"), "capsule radius"), length: jt(d.getAttribute("length"), "capsule length") };
198
- const a = e == null ? void 0 : e.querySelector("mesh"), f = a == null ? void 0 : a.getAttribute("filename");
199
- return f ? { type: "mesh", resourcePath: i.resolve(f, t), scale: Lt(a.getAttribute("scale"), [1, 1, 1]) } : null;
200
- }
201
- function On(n, t, i) {
202
- const s = Zn(n, "URDF").querySelector("robot");
203
- if (!s) throw O("URDF requires a robot root element.", "E_URDF_PARSE");
204
- const r = hn(), o = /* @__PURE__ */ new Map();
205
- for (const f of s.children) if (f.tagName === "material" && f.getAttribute("name")) {
206
- const l = f.getAttribute("name");
207
- if (o.has(l)) throw O(`Duplicate URDF material: ${l}`, "E_URDF_MATERIAL");
208
- o.set(l, f);
209
- }
210
- const d = [...s.querySelectorAll("link")].map((f) => ({
211
- id: rt(f, "name"),
212
- name: rt(f, "name"),
213
- inertial: (() => {
214
- const l = Yn(f);
215
- return l && (!(l.mass > 0) || !Number.isFinite(l.mass) || !Yi(l.tensor)) && r.errors.push({ code: "E_URDF_INERTIA", message: `连杆 ${f.getAttribute("name")} 的质量或惯性张量无效。`, source: f.getAttribute("name") ?? void 0 }), l;
216
- })(),
217
- visuals: [...f.querySelectorAll("visual")].map((l) => {
218
- const p = wi(l, t, i), c = l.querySelector("material"), u = c == null ? void 0 : c.getAttribute("name"), y = u ? o.get(u) : void 0, g = (c == null ? void 0 : c.querySelector("color")) ?? (y == null ? void 0 : y.querySelector("color")), x = (c == null ? void 0 : c.querySelector("texture")) ?? (y == null ? void 0 : y.querySelector("texture"));
219
- u && !y && !g && !x && r.warnings.push({ code: "URDF_MATERIAL_UNRESOLVED", message: `Undefined material: ${u}`, source: f.getAttribute("name") ?? void 0 });
220
- const w = g ? (g.getAttribute("rgba") ?? "").trim().split(/\s+/).map(Number) : x ? [1, 1, 1, 1] : void 0;
221
- if (w && (w.length !== 4 || w.some((b) => !Number.isFinite(b) || b < 0 || b > 1))) throw O(`Invalid URDF material RGBA: ${u ?? ""}`, "E_URDF_MATERIAL");
222
- const m = x == null ? void 0 : x.getAttribute("filename");
223
- if (x && !m) throw O("URDF texture requires a filename", "E_URDF_MATERIAL");
224
- const h = m ? i.resolve(m, t) : void 0;
225
- return p ? { origin: ye(l), geometry: p, color: w, texturePath: h } : null;
226
- }).filter((l) => !!l),
227
- collisions: [...f.querySelectorAll("collision")].map((l) => {
228
- const p = wi(l, t, i);
229
- return p ? { origin: ye(l), geometry: p } : null;
230
- }).filter((l) => !!l)
231
- })), a = [...s.querySelectorAll("joint")].map((f) => {
232
- var w;
233
- const l = f.querySelector("limit"), p = f.querySelector("mimic"), c = p ? { joint: rt(p, "joint"), multiplier: Kt(p.getAttribute("multiplier"), "mimic multiplier") ?? 1, offset: Kt(p.getAttribute("offset"), "mimic offset") ?? 0 } : void 0, u = f.querySelector("dynamics"), y = u ? Ai(u.getAttribute("damping"), 0) : 0, g = u ? Ai(u.getAttribute("friction"), 0) : 0, x = u && (y > 0 || g > 0) ? { damping: y, friction: g } : void 0;
234
- return {
235
- id: rt(f, "name"),
236
- type: f.getAttribute("type") ?? "fixed",
237
- parent: rt(f.querySelector("parent"), "link"),
238
- child: rt(f.querySelector("child"), "link"),
239
- origin: ye(f),
240
- axis: Lt(((w = f.querySelector("axis")) == null ? void 0 : w.getAttribute("xyz")) ?? null, [1, 0, 0]),
241
- ...l ? {
242
- limits: { lower: ke(l.getAttribute("lower"), `Joint ${rt(f, "name")} limit lower`), upper: ke(l.getAttribute("upper"), `Joint ${rt(f, "name")} limit upper`) },
243
- effort: Kt(l.getAttribute("effort"), `Joint ${rt(f, "name")} limit effort`),
244
- velocity: Kt(l.getAttribute("velocity"), `Joint ${rt(f, "name")} limit velocity`)
245
- } : {},
246
- ...x ? { dynamics: x } : {},
247
- ...c ? { mimic: c } : {}
248
- };
249
- });
250
- return Ae(a), s.querySelector("transmission, gazebo") && r.droppedFeatures.push({ code: "UNSUPPORTED_URDF_EXTENSION", message: "URDF transmission/gazebo extensions are not compiled." }), mn({ id: s.getAttribute("name") || t, name: s.getAttribute("name") || t, source: { format: "urdf", uri: t }, links: d, joints: a, diagnostics: r, extension: { files: i } }, { allowInvalidGraph: !0 });
251
- }
252
- function bi(n) {
253
- const t = Qt(n);
254
- return `quat="${t.w} ${t.x} ${t.y} ${t.z}"`;
255
- }
256
- function De(n) {
257
- const t = new H(), i = Qt(n);
258
- return t.compose(new z().fromArray(n.position), i, new z(1, 1, 1));
259
- }
260
- function Vn(n) {
261
- if (n.type === "continuous" || !n.limits) return 0;
262
- const { lower: t, upper: i } = n.limits;
263
- return !Number.isFinite(t) || !Number.isFinite(i) ? 0 : Sn.clamp(0, t, i);
264
- }
265
- function Wn(n) {
266
- let t;
267
- if (n.type === "box")
268
- t = [n.size[0] / 2, n.size[1] / 2, n.size[2] / 2];
269
- else if (n.type === "sphere")
270
- t = [n.radius, n.radius, n.radius];
271
- else if (n.type === "cylinder")
272
- t = [n.radius, n.radius, n.length / 2];
273
- else
274
- return null;
275
- const i = [];
276
- for (const e of [-t[0], t[0]])
277
- for (const s of [-t[1], t[1]])
278
- for (const r of [-t[2], t[2]])
279
- i.push(new z(e, s, r));
280
- return i;
281
- }
282
- function Ki(n, t, i) {
283
- let e = 0;
284
- const s = new Map(n.links.map((l) => [l.id, l])), r = ti(n.joints), o = (l) => {
285
- let p = 1 / 0;
286
- const c = (u, y) => {
287
- const g = s.get(u);
288
- if (g) {
289
- for (const x of g.collisions) {
290
- const w = Wn(x.geometry);
291
- if (!w) {
292
- l && (e += 1);
293
- continue;
294
- }
295
- const m = y.clone().multiply(De(x.origin));
296
- for (const h of w)
297
- p = Math.min(p, h.applyMatrix4(m).z);
298
- }
299
- for (const x of i.get(u) ?? []) {
300
- const w = y.clone().multiply(De(x.origin)), m = l ? r[x.id] : 0;
301
- m && x.type === "revolute" ? w.multiply(new H().makeRotationAxis(new z().fromArray(x.axis).normalize(), m)) : m && x.type === "prismatic" && w.multiply(new H().makeTranslation(x.axis[0] * m, x.axis[1] * m, x.axis[2] * m)), c(x.child, w);
302
- }
303
- }
304
- };
305
- return c(t, new H()), p;
306
- }, d = o(!0), a = o(!1), f = Math.min(d, a);
307
- return e && n.diagnostics.warnings.push({
308
- code: "GROUNDING_IGNORED_MESH_COLLIDERS",
309
- message: `自动落地忽略了 ${e} 个网格碰撞体`
310
- }), Number.isFinite(f) ? Math.max(0, 0.015 - f) : 0;
311
- }
312
- async function Gn(n) {
313
- const t = n.extension.files, i = /* @__PURE__ */ new Map(), e = new Set(n.joints.map((w) => w.child));
314
- for (const w of n.joints) i.set(w.parent, [...i.get(w.parent) ?? [], w]);
315
- const s = n.links.filter((w) => !e.has(w.id));
316
- if (s.length !== 1) throw O(`URDF requires exactly one root link; found ${s.length}.`, "E_URDF_COMPILE");
317
- const r = Ki(n, s[0].id, i);
318
- r > 0 && n.diagnostics.approximations.push({
319
- code: "AUTO_GROUNDED_FREE_ROOT",
320
- message: `自由根初始高度自动抬升 ${r.toFixed(4)} m`
321
- });
322
- const o = n.joints.filter((w) => Vn(w) !== 0);
323
- o.length && n.diagnostics.approximations.push({
324
- code: "JOINT_REFERENCE_CLAMPED",
325
- message: `${o.length} 个关节的默认参考值已夹到合法限位`
326
- });
327
- const d = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), f = [], l = ti(n.joints), p = async (w) => {
328
- var A;
329
- if (w.type === "box") return `type="box" size="${w.size.map((P) => P / 2).join(" ")}"`;
330
- if (w.type === "sphere") return `type="sphere" size="${w.radius}"`;
331
- if (w.type === "cylinder") return `type="cylinder" size="${w.radius} ${w.length / 2}"`;
332
- if (w.type === "capsule") return `type="capsule" size="${w.radius} ${w.length / 2}"`;
333
- const m = (A = w.resourcePath.split(".").pop()) == null ? void 0 : A.toLowerCase();
334
- if (m !== "stl" && m !== "obj") return null;
335
- const h = `${w.resourcePath}|${w.scale.join(" ")}`;
336
- let b = a.get(h);
337
- if (!b) {
338
- const P = { name: `mesh_${a.size}`, path: w.resourcePath, scale: w.scale };
339
- a.set(h, P);
340
- const M = await t.bytes(w.resourcePath);
341
- d.set(`${P.name}.${m}`, M), b = P;
342
- }
343
- return `type="mesh" mesh="${b.name}"`;
344
- }, c = async (w, m) => {
345
- var M, T;
346
- const h = n.links.find((R) => R.id === w), b = [...(m == null ? void 0 : m.origin.position) ?? [0, 0, 0]];
347
- m || (b[2] += r);
348
- const A = (m == null ? void 0 : m.origin) ?? { rotationRpy: [0, 0, 0] }, P = [`<body name="${St(w)}" pos="${b.join(" ")}" ${bi(A)}>`];
349
- if (h.inertial) {
350
- const { origin: R, mass: v, tensor: B } = h.inertial, S = new di().setFromMatrix4(new H().makeRotationFromQuaternion(Qt(R))), _ = new di().set(B[0], B[3], B[4], B[3], B[1], B[5], B[4], B[5], B[2]);
351
- _.premultiply(S).multiply(S.clone().transpose());
352
- const E = _.elements;
353
- P.push(`<inertial pos="${R.position.join(" ")}" mass="${v}" fullinertia="${[E[0], E[4], E[8], E[3], E[6], E[7]].join(" ")}"/>`);
354
- }
355
- if (!m) P.push("<freejoint/>");
356
- else if (["fixed", "planar", "floating"].includes(m.type))
357
- (m == null ? void 0 : m.type) === "floating" && (P.push("<freejoint/>"), f.push(0, 0, 0, 1, 0, 0, 0));
358
- else {
359
- const R = m.type === "prismatic" ? "slide" : "hinge", v = m.type !== "continuous" && !!m.limits && Number.isFinite(m.limits.lower) && Number.isFinite(m.limits.upper), B = l[m.id];
360
- f.push(B);
361
- const S = ((M = m.dynamics) == null ? void 0 : M.damping) ?? 0, _ = ((T = m.dynamics) == null ? void 0 : T.friction) ?? 0;
362
- P.push(`<joint name="${St(m.id)}" type="${R}" axis="${m.axis.join(" ")}"${v ? ` limited="true" range="${m.limits.lower} ${m.limits.upper}" ref="0"` : ""} damping="${S}" frictionloss="${_}"/>`);
363
- }
364
- for (const R of h.collisions) {
365
- const v = await p(R.geometry);
366
- if (v) {
367
- const B = ` ${bi(R.origin)}`;
368
- P.push(`<geom ${v} pos="${R.origin.position.join(" ")}"${B} rgba="0.55 0.62 0.59 1"/>`);
369
- }
370
- }
371
- for (const R of i.get(w) ?? []) P.push(await c(R.child, R));
372
- return P.push("</body>"), P.join("");
373
- }, u = await c(s[0].id), y = [...a.values()].map((w) => {
374
- var m;
375
- return `<mesh name="${w.name}" file="${w.name}.${(m = w.path.split(".").pop()) == null ? void 0 : m.toLowerCase()}" scale="${w.scale.join(" ")}"/>`;
376
- }).join(""), g = `<keyframe><key name="home" qpos="0 0 ${r} 1 0 0 0 ${f.join(" ")}"/></keyframe>`, x = n.joints.filter((w) => w.mimic).map((w) => {
377
- const m = w.mimic;
378
- return `<joint name="mimic_${St(w.id)}" joint1="${St(w.id)}" joint2="${St(m.joint)}" polycoef="${m.offset} ${m.multiplier} 0 0 0"/>`;
379
- }).join("");
380
- return {
381
- format: "mjcf",
382
- files: d,
383
- xml: `<mujoco model="${St(n.name)}"><compiler angle="radian" autolimits="true"/><option gravity="0 0 -9.81"/><asset>${y}</asset><worldbody><geom name="__viewer_default_ground__" type="plane" size="5 5 0.1" contype="1" conaffinity="1" friction="0.8 0.01 0.0002"/>${u}</worldbody>${x ? `<equality>${x}</equality>` : ""}${g}</mujoco>`
384
- };
385
- }
386
- async function tn(n) {
387
- const t = n.extension.files, i = new lt();
388
- i.name = "URDF authored collisions";
389
- const e = /* @__PURE__ */ new Map(), s = yn(), r = gn();
390
- let o = 0;
391
- for (const d of n.links) {
392
- const a = new lt();
393
- a.name = d.id, a.userData.urdfEntityId = `link:${d.id}`, e.set(d.id, a), i.add(a);
394
- for (const f of d.collisions) {
395
- const l = new lt();
396
- if (l.matrixAutoUpdate = !1, l.matrix.copy(De(f.origin)), a.add(l), f.geometry.type === "mesh") {
397
- const p = await Le(f.geometry, t, new He());
398
- p.updateMatrixWorld(!0), p.traverse((c) => {
399
- if (!(c instanceof N)) return;
400
- const u = ri(c.geometry, s, "mesh", r);
401
- u.matrixAutoUpdate = !1, u.matrix.copy(c.matrixWorld), u.name = `Collision:${d.id}`, u.userData.urdfLink = d.id, l.add(u), o += 1;
402
- }), ft(p);
403
- } else {
404
- const p = Jn(f.geometry);
405
- if (!p) continue;
406
- const c = ri(p, s, "feature", r);
407
- p.dispose(), c.name = `Collision:${d.id}`, c.userData.urdfLink = d.id, l.add(c), o += 1;
408
- }
409
- }
410
- }
411
- if (o === 0) {
412
- s.dispose(), r.dispose(), ft(i);
413
- return;
414
- }
415
- return { root: i, bindings: e, expectedLinkCount: e.size };
416
- }
417
- async function Xn(n, t) {
418
- const i = await tn(n);
419
- if (!i) return;
420
- const e = () => {
421
- t.root.updateMatrixWorld(!0);
422
- for (const [s, r] of i.bindings) {
423
- const o = t.bindings.get(s);
424
- o && (r.matrixAutoUpdate = !1, r.matrix.copy(o.matrixWorld));
425
- }
426
- i.root.updateMatrixWorld(!0);
427
- };
428
- return e(), i.root.visible = !1, { root: i.root, sync: e, dispose() {
429
- ft(i.root);
430
- } };
431
- }
432
- function Jn(n) {
433
- if (n.type === "box") return new Ze(...n.size);
434
- if (n.type === "sphere") return new Qe(n.radius, 24, 16);
435
- if (n.type === "cylinder") {
436
- const t = new Vi(n.radius, n.radius, n.length, 24);
437
- return t.rotateX(Math.PI / 2), t;
438
- }
439
- if (n.type === "capsule") {
440
- const t = new Wi(n.radius, n.length, 8, 24);
441
- return t.rotateX(Math.PI / 2), t;
442
- }
443
- return null;
444
- }
445
- async function xi(n) {
446
- const t = n.extension.files, i = new lt();
447
- i.name = n.name;
448
- const e = /* @__PURE__ */ new Map();
449
- try {
450
- for (const s of n.links) {
451
- const r = new lt();
452
- r.name = s.id, r.userData.urdfEntityId = `link:${s.id}`, e.set(s.id, r), i.add(r);
453
- for (const o of s.visuals) {
454
- const d = await Hn(o, t, n.diagnostics), a = new lt();
455
- a.position.fromArray(o.origin.position), a.quaternion.copy(Qt(o.origin)), a.add(d), r.add(a);
456
- }
457
- }
458
- return { root: i, bindings: e, expectedLinkCount: n.links.length };
459
- } catch (s) {
460
- throw ft(i), s;
461
- }
462
- }
463
- async function Hn(n, t, i) {
464
- var r;
465
- if (n.geometry.type === "mesh" && /\.gl(?:tf|b)$/i.test(n.geometry.resourcePath)) {
466
- (n.color || n.texturePath) && i.warnings.push({ code: "URDF_EMBEDDED_MATERIAL_PRESERVED", message: "Embedded glTF materials take precedence over the URDF visual material.", source: n.geometry.resourcePath });
467
- const o = gi();
468
- try {
469
- return await Le(n.geometry, t, o);
470
- } finally {
471
- o.dispose();
472
- }
473
- }
474
- const e = gi({ color: n.color });
475
- let s;
476
- try {
477
- if (n.texturePath && (await t.ensure(n.texturePath), e.map = await new Mn().loadAsync(t.objectUrl(n.texturePath)), e.transparent = !0, e.map.colorSpace = vn, e.map.wrapS = e.map.wrapT = Pn), s = await Le(n.geometry, t, e), n.geometry.type === "mesh" && /\.dae$/i.test(n.geometry.resourcePath))
478
- if (n.color || n.texturePath) {
479
- const o = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set();
480
- s.traverse((a) => {
481
- if (a instanceof N) {
482
- for (const f of Array.isArray(a.material) ? a.material : [a.material]) o.add(f);
483
- a.material = e;
484
- }
485
- });
486
- for (const a of o) {
487
- for (const f of Object.values(a)) f != null && f.isTexture && d.add(f);
488
- a.dispose();
489
- }
490
- for (const a of d) a.dispose();
491
- } else e.dispose();
492
- return e.map && s.traverse((o) => {
493
- if (o instanceof N && !o.geometry.getAttribute("uv")) throw O(`URDF texture requires authored UVs: ${n.texturePath}`, "E_URDF_TEXTURE_UV");
494
- }), s;
495
- } catch (o) {
496
- throw s ? ft(s) : ((r = e.map) == null || r.dispose(), e.dispose()), o;
497
- }
498
- }
499
- async function Le(n, t, i) {
500
- var r;
501
- if (n.type === "box") return new N(new Ze(...n.size), i);
502
- if (n.type === "sphere") return new N(new Qe(n.radius, 24, 16), i);
503
- if (n.type === "cylinder") {
504
- const o = new N(new Vi(n.radius, n.radius, n.length, 24), i);
505
- return o.quaternion.copy(new K().setFromEuler(new Zt(Math.PI / 2, 0, 0))), o;
506
- }
507
- if (n.type === "capsule") {
508
- const o = new N(new Wi(n.radius, n.length, 8, 24), i);
509
- return o.quaternion.copy(new K().setFromEuler(new Zt(Math.PI / 2, 0, 0))), o;
510
- }
511
- const e = (r = n.resourcePath.split(".").pop()) == null ? void 0 : r.toLowerCase();
512
- await t.ensure(n.resourcePath);
513
- let s;
514
- if (e === "stl") s = new N(new kn().parse(await t.getBlob(n.resourcePath).arrayBuffer()), i);
515
- else if (e === "obj")
516
- s = new Ln().parse(await t.getBlob(n.resourcePath).text()), s.traverse((o) => {
517
- if (o instanceof N) {
518
- for (const d of Array.isArray(o.material) ? o.material : [o.material]) d.dispose();
519
- o.material = i;
520
- }
521
- });
522
- else if (e === "gltf" || e === "glb") {
523
- const o = new Oi();
524
- o.setURLModifier((d) => {
525
- try {
526
- return t.objectUrl(t.resolve(d, n.resourcePath));
527
- } catch {
528
- return d;
529
- }
530
- }), s = (await new In(o).parseAsync(await t.getBlob(n.resourcePath).arrayBuffer(), "")).scene;
531
- } else if (e === "dae") s = await Nn(n.resourcePath, t);
532
- else throw O(`Unsupported URDF visual mesh: .${e ?? ""}`, "E_URDF_VISUAL");
533
- return s.scale.fromArray(n.scale), s;
534
- }
535
- function Zn(n, t) {
536
- const i = new DOMParser().parseFromString(n, "application/xml"), e = i.querySelector("parsererror");
537
- if (e) throw O(`${t} XML parse failed: ${e.textContent ?? ""}`, `E_${t}_PARSE`);
538
- return i;
539
- }
540
- function rt(n, t) {
541
- const i = n == null ? void 0 : n.getAttribute(t);
542
- if (!i) throw O(`Missing ${t}.`, "E_URDF_PARSE");
543
- return i;
544
- }
545
- function Ai(n, t) {
546
- const i = Number(n);
547
- return Number.isFinite(i) ? i : t;
548
- }
549
- function ke(n, t) {
550
- if (n === null || n.trim() === "") throw O(`${t} is required.`, "E_URDF_PARSE");
551
- const i = Number(n);
552
- if (!Number.isFinite(i)) throw O(`${t} must be finite.`, "E_URDF_PARSE");
553
- return i;
554
- }
555
- function jt(n, t) {
556
- const i = ke(n, t);
557
- if (!(i > 0)) throw O(`${t} must be greater than zero.`, "E_URDF_PARSE");
558
- return i;
559
- }
560
- function Qn(n, t, i) {
561
- if (n === null || n.trim() === "") throw O(`${i} is required.`, "E_URDF_PARSE");
562
- const e = Lt(n, []);
563
- if (e.length !== t || e.some((s) => !(s > 0))) throw O(`${i} requires ${t} values greater than zero.`, "E_URDF_PARSE");
564
- return e;
565
- }
566
- function Kt(n, t) {
567
- if (n === null || n.trim() === "") return;
568
- const i = Number(n);
569
- if (!Number.isFinite(i)) throw O(`${t} must be finite.`, "E_URDF_PARSE");
570
- return i;
571
- }
572
- function St(n) {
573
- return n.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
574
- }
575
- function O(n, t) {
576
- return Object.assign(new Error(n), { code: t });
577
- }
578
- function Yn(n) {
579
- var o;
580
- const t = n.querySelector("inertial");
581
- if (!t) return;
582
- const i = t.querySelector("inertia"), e = (o = t.querySelector("mass")) == null ? void 0 : o.getAttribute("value"), s = e == null || e.trim() === "" ? Number.NaN : Number(e), r = ["ixx", "iyy", "izz", "ixy", "ixz", "iyz"].map((d) => {
583
- const a = i == null ? void 0 : i.getAttribute(d);
584
- return a == null || a.trim() === "" ? Number.NaN : Number(a);
585
- });
586
- return { origin: ye(t), mass: s, tensor: r };
587
- }
588
- const en = 0, Kn = 1, ts = 2, Mi = 2, Se = 1.25, vi = 1, X = 6 * 4 + 4 + 4, C = X / 4, nn = 65535, ge = Math.pow(2, -24), ei = Symbol("SKIP_GENERATION"), sn = {
589
- strategy: en,
590
- maxDepth: 40,
591
- maxLeafSize: 10,
592
- useSharedArrayBuffer: !1,
593
- setBoundingBox: !0,
594
- onProgress: null,
595
- indirect: !1,
596
- verbose: !0,
597
- range: null,
598
- [ei]: !1
599
- };
600
- function k(n, t, i) {
601
- return i.min.x = t[n], i.min.y = t[n + 1], i.min.z = t[n + 2], i.max.x = t[n + 3], i.max.y = t[n + 4], i.max.z = t[n + 5], i;
602
- }
603
- function Pi(n) {
604
- let t = -1, i = -1 / 0;
605
- for (let e = 0; e < 3; e++) {
606
- const s = n[e + 3] - n[e];
607
- s > i && (i = s, t = e);
608
- }
609
- return t;
610
- }
611
- function Si(n, t) {
612
- t.set(n);
613
- }
614
- function Ti(n, t, i) {
615
- let e, s;
616
- for (let r = 0; r < 3; r++) {
617
- const o = r + 3;
618
- e = n[r], s = t[r], i[r] = e < s ? e : s, e = n[o], s = t[o], i[o] = e > s ? e : s;
619
- }
620
- }
621
- function te(n, t, i) {
622
- for (let e = 0; e < 3; e++) {
623
- const s = t[n + 2 * e], r = t[n + 2 * e + 1], o = s - r, d = s + r;
624
- o < i[e] && (i[e] = o), d > i[e + 3] && (i[e + 3] = d);
625
- }
626
- }
627
- function Nt(n) {
628
- const t = n[3] - n[0], i = n[4] - n[1], e = n[5] - n[2];
629
- return 2 * (t * i + i * e + e * t);
630
- }
631
- function q(n, t) {
632
- return t[n + 15] === nn;
633
- }
634
- function J(n, t) {
635
- return t[n + 6];
636
- }
637
- function Z(n, t) {
638
- return t[n + 14];
639
- }
640
- function V(n) {
641
- return n + C;
642
- }
643
- function W(n, t) {
644
- const i = t[n + 6];
645
- return n + i * C;
646
- }
647
- function ii(n, t) {
648
- return t[n + 7];
649
- }
650
- function Te(n, t, i, e, s) {
651
- let r = 1 / 0, o = 1 / 0, d = 1 / 0, a = -1 / 0, f = -1 / 0, l = -1 / 0, p = 1 / 0, c = 1 / 0, u = 1 / 0, y = -1 / 0, g = -1 / 0, x = -1 / 0;
652
- const w = n.offset || 0;
653
- for (let m = (t - w) * 6, h = (t + i - w) * 6; m < h; m += 6) {
654
- const b = n[m + 0], A = n[m + 1], P = b - A, M = b + A;
655
- P < r && (r = P), M > a && (a = M), b < p && (p = b), b > y && (y = b);
656
- const T = n[m + 2], R = n[m + 3], v = T - R, B = T + R;
657
- v < o && (o = v), B > f && (f = B), T < c && (c = T), T > g && (g = T);
658
- const S = n[m + 4], _ = n[m + 5], E = S - _, F = S + _;
659
- E < d && (d = E), F > l && (l = F), S < u && (u = S), S > x && (x = S);
660
- }
661
- e[0] = r, e[1] = o, e[2] = d, e[3] = a, e[4] = f, e[5] = l, s[0] = p, s[1] = c, s[2] = u, s[3] = y, s[4] = g, s[5] = x;
662
- }
663
- const at = 32, es = (n, t) => n.candidate - t.candidate, pt = /* @__PURE__ */ new Array(at).fill().map(() => ({
664
- count: 0,
665
- bounds: new Float32Array(6),
666
- rightCacheBounds: new Float32Array(6),
667
- leftCacheBounds: new Float32Array(6),
668
- candidate: 0
669
- })), ee = /* @__PURE__ */ new Float32Array(6);
670
- function is(n, t, i, e, s, r) {
671
- let o = -1, d = 0;
672
- if (r === en)
673
- o = Pi(t), o !== -1 && (d = (t[o] + t[o + 3]) / 2);
674
- else if (r === Kn)
675
- o = Pi(n), o !== -1 && (d = ns(i, e, s, o));
676
- else if (r === ts) {
677
- const a = Nt(n);
678
- let f = Se * s;
679
- const l = i.offset || 0, p = (e - l) * 6, c = (e + s - l) * 6;
680
- for (let u = 0; u < 3; u++) {
681
- const y = t[u], w = (t[u + 3] - y) / at;
682
- if (s < at / 4) {
683
- const m = [...pt];
684
- m.length = s;
685
- let h = 0;
686
- for (let A = p; A < c; A += 6, h++) {
687
- const P = m[h];
688
- P.candidate = i[A + 2 * u], P.count = 0;
689
- const {
690
- bounds: M,
691
- leftCacheBounds: T,
692
- rightCacheBounds: R
693
- } = P;
694
- for (let v = 0; v < 3; v++)
695
- R[v] = 1 / 0, R[v + 3] = -1 / 0, T[v] = 1 / 0, T[v + 3] = -1 / 0, M[v] = 1 / 0, M[v + 3] = -1 / 0;
696
- te(A, i, M);
697
- }
698
- m.sort(es);
699
- let b = s;
700
- for (let A = 0; A < b; A++) {
701
- const P = m[A];
702
- for (; A + 1 < b && m[A + 1].candidate === P.candidate; )
703
- m.splice(A + 1, 1), b--;
704
- }
705
- for (let A = p; A < c; A += 6) {
706
- const P = i[A + 2 * u];
707
- for (let M = 0; M < b; M++) {
708
- const T = m[M];
709
- P >= T.candidate ? te(A, i, T.rightCacheBounds) : (te(A, i, T.leftCacheBounds), T.count++);
710
- }
711
- }
712
- for (let A = 0; A < b; A++) {
713
- const P = m[A], M = P.count, T = s - P.count, R = P.leftCacheBounds, v = P.rightCacheBounds;
714
- let B = 0;
715
- M !== 0 && (B = Nt(R) / a);
716
- let S = 0;
717
- T !== 0 && (S = Nt(v) / a);
718
- const _ = vi + Se * (B * M + S * T);
719
- _ < f && (o = u, f = _, d = P.candidate);
720
- }
721
- } else {
722
- for (let b = 0; b < at; b++) {
723
- const A = pt[b];
724
- A.count = 0, A.candidate = y + w + b * w;
725
- const P = A.bounds;
726
- for (let M = 0; M < 3; M++)
727
- P[M] = 1 / 0, P[M + 3] = -1 / 0;
728
- }
729
- for (let b = p; b < c; b += 6) {
730
- let M = ~~((i[b + 2 * u] - y) / w);
731
- M >= at && (M = at - 1);
732
- const T = pt[M];
733
- T.count++, te(b, i, T.bounds);
734
- }
735
- const m = pt[at - 1];
736
- Si(m.bounds, m.rightCacheBounds);
737
- for (let b = at - 2; b >= 0; b--) {
738
- const A = pt[b], P = pt[b + 1];
739
- Ti(A.bounds, P.rightCacheBounds, A.rightCacheBounds);
740
- }
741
- let h = 0;
742
- for (let b = 0; b < at - 1; b++) {
743
- const A = pt[b], P = A.count, M = A.bounds, R = pt[b + 1].rightCacheBounds;
744
- P !== 0 && (h === 0 ? Si(M, ee) : Ti(M, ee, ee)), h += P;
745
- let v = 0, B = 0;
746
- h !== 0 && (v = Nt(ee) / a);
747
- const S = s - h;
748
- S !== 0 && (B = Nt(R) / a);
749
- const _ = vi + Se * (v * h + B * S);
750
- _ < f && (o = u, f = _, d = A.candidate);
751
- }
752
- }
753
- }
754
- } else
755
- console.warn(`BVH: Invalid build strategy value ${r} used.`);
756
- return { axis: o, pos: d };
757
- }
758
- function ns(n, t, i, e) {
759
- let s = 0;
760
- const r = n.offset;
761
- for (let o = t, d = t + i; o < d; o++)
762
- s += n[(o - r) * 6 + e * 2];
763
- return s / i;
764
- }
765
- class Re {
766
- constructor() {
767
- this.boundingData = new Float32Array(6);
768
- }
769
- }
770
- function ss(n, t, i, e, s, r) {
771
- let o = e, d = e + s - 1;
772
- const a = r.pos, f = r.axis * 2, l = i.offset || 0;
773
- for (; ; ) {
774
- for (; o <= d && i[(o - l) * 6 + f] < a; )
775
- o++;
776
- for (; o <= d && i[(d - l) * 6 + f] >= a; )
777
- d--;
778
- if (o < d) {
779
- for (let p = 0; p < t; p++) {
780
- let c = n[o * t + p];
781
- n[o * t + p] = n[d * t + p], n[d * t + p] = c;
782
- }
783
- for (let p = 0; p < 6; p++) {
784
- const c = o - l, u = d - l, y = i[c * 6 + p];
785
- i[c * 6 + p] = i[u * 6 + p], i[u * 6 + p] = y;
786
- }
787
- o++, d--;
788
- } else
789
- return o;
790
- }
791
- }
792
- let on, we, je, rn;
793
- const os = Math.pow(2, 32);
794
- function Ne(n) {
795
- return "count" in n ? 1 : 1 + Ne(n.left) + Ne(n.right);
796
- }
797
- function rs(n, t, i) {
798
- return on = new Float32Array(i), we = new Uint32Array(i), je = new Uint16Array(i), rn = new Uint8Array(i), Ce(n, t);
799
- }
800
- function Ce(n, t) {
801
- const i = n / 4, e = n / 2, s = "count" in t, r = t.boundingData;
802
- for (let o = 0; o < 6; o++)
803
- on[i + o] = r[o];
804
- if (s)
805
- return t.buffer ? (rn.set(new Uint8Array(t.buffer), n), n + t.buffer.byteLength) : (we[i + 6] = t.offset, je[e + 14] = t.count, je[e + 15] = nn, n + X);
806
- {
807
- const { left: o, right: d, splitAxis: a } = t, f = n + X;
808
- let l = Ce(f, o);
809
- const p = n / X, u = l / X - p;
810
- if (u > os)
811
- throw new Error("MeshBVH: Cannot store relative child node offset greater than 32 bits.");
812
- return we[i + 6] = u, we[i + 7] = a, Ce(l, d);
813
- }
814
- }
815
- function as(n, t, i, e, s, r) {
816
- const {
817
- maxDepth: o,
818
- verbose: d,
819
- maxLeafSize: a,
820
- strategy: f,
821
- onProgress: l
822
- } = s, p = n.primitiveBuffer, c = n.primitiveBufferStride, u = new Float32Array(6);
823
- let y = !1;
824
- const g = new Re();
825
- return Te(t, i, e, g.boundingData, u), w(g, i, e, u), g;
826
- function x(m) {
827
- l && l((m - r.offset) / r.count);
828
- }
829
- function w(m, h, b, A = null, P = 0) {
830
- if (!y && P >= o && (y = !0, d && console.warn(`BVH: Max depth of ${o} reached when generating BVH. Consider increasing maxDepth.`)), b <= a || P >= o)
831
- return x(h + b), m.offset = h, m.count = b, m;
832
- const M = is(m.boundingData, A, t, h, b, f);
833
- if (M.axis === -1)
834
- return x(h + b), m.offset = h, m.count = b, m;
835
- const T = ss(p, c, t, h, b, M);
836
- if (T === h || T === h + b)
837
- x(h + b), m.offset = h, m.count = b;
838
- else {
839
- m.splitAxis = M.axis;
840
- const R = new Re(), v = h, B = T - h;
841
- m.left = R, Te(t, v, B, R.boundingData, u), w(R, v, B, u, P + 1);
842
- const S = new Re(), _ = T, E = b - B;
843
- m.right = S, Te(t, _, E, S.boundingData, u), w(S, _, E, u, P + 1);
844
- }
845
- return m;
846
- }
847
- }
848
- function cs(n, t) {
849
- const i = t.useSharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer, e = n.getRootRanges(t.range), s = e[0], r = e[e.length - 1], o = {
850
- offset: s.offset,
851
- count: r.offset + r.count - s.offset
852
- }, d = new Float32Array(6 * o.count);
853
- d.offset = o.offset, n.computePrimitiveBounds(o.offset, o.count, d), n._roots = e.map((a) => {
854
- const f = as(n, d, a.offset, a.count, t, o), l = Ne(f), p = new i(X * l);
855
- return rs(0, f, p), p;
856
- });
857
- }
858
- class ni {
859
- constructor(t) {
860
- this._getNewPrimitive = t, this._primitives = [];
861
- }
862
- getPrimitive() {
863
- const t = this._primitives;
864
- return t.length === 0 ? this._getNewPrimitive() : t.pop();
865
- }
866
- releasePrimitive(t) {
867
- this._primitives.push(t);
868
- }
869
- }
870
- class ls {
871
- constructor() {
872
- this.float32Array = null, this.uint16Array = null, this.uint32Array = null;
873
- const t = [];
874
- let i = null;
875
- this.setBuffer = (e) => {
876
- i && t.push(i), i = e, this.float32Array = new Float32Array(e), this.uint16Array = new Uint16Array(e), this.uint32Array = new Uint32Array(e);
877
- }, this.clearBuffer = () => {
878
- i = null, this.float32Array = null, this.uint16Array = null, this.uint32Array = null, t.length !== 0 && this.setBuffer(t.pop());
879
- };
880
- }
881
- }
882
- const I = /* @__PURE__ */ new ls();
883
- let ht, Dt;
884
- const Tt = [], ie = /* @__PURE__ */ new ni(() => new it());
885
- function us(n, t, i, e, s, r) {
886
- ht = ie.getPrimitive(), Dt = ie.getPrimitive(), Tt.push(ht, Dt), I.setBuffer(n._roots[t]);
887
- const o = qe(0, n.geometry, i, e, s, r);
888
- I.clearBuffer(), ie.releasePrimitive(ht), ie.releasePrimitive(Dt), Tt.pop(), Tt.pop();
889
- const d = Tt.length;
890
- return d > 0 && (Dt = Tt[d - 1], ht = Tt[d - 2]), o;
891
- }
892
- function qe(n, t, i, e, s = null, r = 0, o = 0) {
893
- const { float32Array: d, uint16Array: a, uint32Array: f } = I;
894
- let l = n * 2;
895
- if (q(l, a)) {
896
- const c = J(n, f), u = Z(l, a);
897
- return k(n, d, ht), e(c, u, !1, o, r + n / C, ht);
898
- } else {
899
- let v = function(S) {
900
- const { uint16Array: _, uint32Array: E } = I;
901
- let F = S * 2;
902
- for (; !q(F, _); )
903
- S = V(S), F = S * 2;
904
- return J(S, E);
905
- }, B = function(S) {
906
- const { uint16Array: _, uint32Array: E } = I;
907
- let F = S * 2;
908
- for (; !q(F, _); )
909
- S = W(S, E), F = S * 2;
910
- return J(S, E) + Z(F, _);
911
- };
912
- const c = V(n), u = W(n, f);
913
- let y = c, g = u, x, w, m, h;
914
- if (s && (m = ht, h = Dt, k(y, d, m), k(g, d, h), x = s(m), w = s(h), w < x)) {
915
- y = u, g = c;
916
- const S = x;
917
- x = w, w = S, m = h;
918
- }
919
- m || (m = ht, k(y, d, m));
920
- const b = q(y * 2, a), A = i(m, b, x, o + 1, r + y / C);
921
- let P;
922
- if (A === Mi) {
923
- const S = v(y), E = B(y) - S;
924
- P = e(S, E, !0, o + 1, r + y / C, m);
925
- } else
926
- P = A && qe(
927
- y,
928
- t,
929
- i,
930
- e,
931
- s,
932
- r,
933
- o + 1
934
- );
935
- if (P) return !0;
936
- h = Dt, k(g, d, h);
937
- const M = q(g * 2, a), T = i(h, M, w, o + 1, r + g / C);
938
- let R;
939
- if (T === Mi) {
940
- const S = v(g), E = B(g) - S;
941
- R = e(S, E, !0, o + 1, r + g / C, h);
942
- } else
943
- R = T && qe(
944
- g,
945
- t,
946
- i,
947
- e,
948
- s,
949
- r,
950
- o + 1
951
- );
952
- return !!R;
953
- }
954
- }
955
- const Ht = /* @__PURE__ */ new I.constructor(), be = /* @__PURE__ */ new I.constructor(), mt = /* @__PURE__ */ new ni(() => new it()), Rt = /* @__PURE__ */ new it(), Bt = /* @__PURE__ */ new it(), Be = /* @__PURE__ */ new it(), _e = /* @__PURE__ */ new it();
956
- let Ee = !1;
957
- function fs(n, t, i, e) {
958
- if (Ee)
959
- throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");
960
- Ee = !0;
961
- const s = n._roots, r = t._roots;
962
- let o, d = 0, a = 0;
963
- const f = new H().copy(i).invert();
964
- for (let l = 0, p = s.length; l < p; l++) {
965
- Ht.setBuffer(s[l]), a = 0;
966
- const c = mt.getPrimitive();
967
- k(0, Ht.float32Array, c), c.applyMatrix4(f);
968
- for (let u = 0, y = r.length; u < y && (be.setBuffer(r[u]), o = st(
969
- 0,
970
- 0,
971
- i,
972
- f,
973
- e,
974
- d,
975
- a,
976
- 0,
977
- 0,
978
- c
979
- ), be.clearBuffer(), a += r[u].byteLength / X, !o); u++)
980
- ;
981
- if (mt.releasePrimitive(c), Ht.clearBuffer(), d += s[l].byteLength / X, o)
982
- break;
983
- }
984
- return Ee = !1, o;
985
- }
986
- function st(n, t, i, e, s, r = 0, o = 0, d = 0, a = 0, f = null, l = !1) {
987
- let p, c;
988
- l ? (p = be, c = Ht) : (p = Ht, c = be);
989
- const u = p.float32Array, y = p.uint32Array, g = p.uint16Array, x = c.float32Array, w = c.uint32Array, m = c.uint16Array, h = n * 2, b = t * 2, A = q(h, g), P = q(b, m);
990
- let M = !1;
991
- if (P && A)
992
- l ? M = s(
993
- J(t, w),
994
- Z(t * 2, m),
995
- J(n, y),
996
- Z(n * 2, g),
997
- a,
998
- o + t / C,
999
- d,
1000
- r + n / C
1001
- ) : M = s(
1002
- J(n, y),
1003
- Z(n * 2, g),
1004
- J(t, w),
1005
- Z(t * 2, m),
1006
- d,
1007
- r + n / C,
1008
- a,
1009
- o + t / C
1010
- );
1011
- else if (P) {
1012
- const T = mt.getPrimitive();
1013
- k(t, x, T), T.applyMatrix4(i);
1014
- const R = V(n), v = W(n, y);
1015
- k(R, u, Rt), k(v, u, Bt);
1016
- const B = T.intersectsBox(Rt), S = T.intersectsBox(Bt);
1017
- M = B && st(
1018
- t,
1019
- R,
1020
- e,
1021
- i,
1022
- s,
1023
- o,
1024
- r,
1025
- a,
1026
- d + 1,
1027
- T,
1028
- !l
1029
- ) || S && st(
1030
- t,
1031
- v,
1032
- e,
1033
- i,
1034
- s,
1035
- o,
1036
- r,
1037
- a,
1038
- d + 1,
1039
- T,
1040
- !l
1041
- ), mt.releasePrimitive(T);
1042
- } else {
1043
- const T = V(t), R = W(t, w);
1044
- k(T, x, Be), k(R, x, _e);
1045
- const v = f.intersectsBox(Be), B = f.intersectsBox(_e);
1046
- if (v && B)
1047
- M = st(
1048
- n,
1049
- T,
1050
- i,
1051
- e,
1052
- s,
1053
- r,
1054
- o,
1055
- d,
1056
- a + 1,
1057
- f,
1058
- l
1059
- ) || st(
1060
- n,
1061
- R,
1062
- i,
1063
- e,
1064
- s,
1065
- r,
1066
- o,
1067
- d,
1068
- a + 1,
1069
- f,
1070
- l
1071
- );
1072
- else if (v)
1073
- if (A)
1074
- M = st(
1075
- n,
1076
- T,
1077
- i,
1078
- e,
1079
- s,
1080
- r,
1081
- o,
1082
- d,
1083
- a + 1,
1084
- f,
1085
- l
1086
- );
1087
- else {
1088
- const S = mt.getPrimitive();
1089
- S.copy(Be).applyMatrix4(i);
1090
- const _ = V(n), E = W(n, y);
1091
- k(_, u, Rt), k(E, u, Bt);
1092
- const F = S.intersectsBox(Rt), D = S.intersectsBox(Bt);
1093
- M = F && st(
1094
- T,
1095
- _,
1096
- e,
1097
- i,
1098
- s,
1099
- o,
1100
- r,
1101
- a,
1102
- d + 1,
1103
- S,
1104
- !l
1105
- ) || D && st(
1106
- T,
1107
- E,
1108
- e,
1109
- i,
1110
- s,
1111
- o,
1112
- r,
1113
- a,
1114
- d + 1,
1115
- S,
1116
- !l
1117
- ), mt.releasePrimitive(S);
1118
- }
1119
- else if (B)
1120
- if (A)
1121
- M = st(
1122
- n,
1123
- R,
1124
- i,
1125
- e,
1126
- s,
1127
- r,
1128
- o,
1129
- d,
1130
- a + 1,
1131
- f,
1132
- l
1133
- );
1134
- else {
1135
- const S = mt.getPrimitive();
1136
- S.copy(_e).applyMatrix4(i);
1137
- const _ = V(n), E = W(n, y);
1138
- k(_, u, Rt), k(E, u, Bt);
1139
- const F = S.intersectsBox(Rt), D = S.intersectsBox(Bt);
1140
- M = F && st(
1141
- R,
1142
- _,
1143
- e,
1144
- i,
1145
- s,
1146
- o,
1147
- r,
1148
- a,
1149
- d + 1,
1150
- S,
1151
- !l
1152
- ) || D && st(
1153
- R,
1154
- E,
1155
- e,
1156
- i,
1157
- s,
1158
- o,
1159
- r,
1160
- a,
1161
- d + 1,
1162
- S,
1163
- !l
1164
- ), mt.releasePrimitive(S);
1165
- }
1166
- }
1167
- return M;
1168
- }
1169
- const Ri = /* @__PURE__ */ new it(), _t = /* @__PURE__ */ new Float32Array(6);
1170
- class ds {
1171
- constructor() {
1172
- this._roots = null, this.primitiveBuffer = null, this.primitiveBufferStride = null;
1173
- }
1174
- init(t) {
1175
- t = {
1176
- ...sn,
1177
- ...t
1178
- }, cs(this, t);
1179
- }
1180
- getRootRanges() {
1181
- throw new Error("BVH: getRootRanges() not implemented");
1182
- }
1183
- // write the i-th primitive bounds in a 6-value min / max format to the buffer
1184
- // starting at the given "writeOffset"
1185
- writePrimitiveBounds() {
1186
- throw new Error("BVH: writePrimitiveBounds() not implemented");
1187
- }
1188
- // writes the union bounds of all primitives in the given range in a min / max format
1189
- // to the buffer
1190
- writePrimitiveRangeBounds(t, i, e, s) {
1191
- let r = 1 / 0, o = 1 / 0, d = 1 / 0, a = -1 / 0, f = -1 / 0, l = -1 / 0;
1192
- for (let p = t, c = t + i; p < c; p++) {
1193
- this.writePrimitiveBounds(p, _t, 0);
1194
- const [u, y, g, x, w, m] = _t;
1195
- u < r && (r = u), x > a && (a = x), y < o && (o = y), w > f && (f = w), g < d && (d = g), m > l && (l = m);
1196
- }
1197
- return e[s + 0] = r, e[s + 1] = o, e[s + 2] = d, e[s + 3] = a, e[s + 4] = f, e[s + 5] = l, e;
1198
- }
1199
- computePrimitiveBounds(t, i, e) {
1200
- const s = e.offset || 0;
1201
- for (let r = t, o = t + i; r < o; r++) {
1202
- this.writePrimitiveBounds(r, _t, 0);
1203
- const [d, a, f, l, p, c] = _t, u = (d + l) / 2, y = (a + p) / 2, g = (f + c) / 2, x = (l - d) / 2, w = (p - a) / 2, m = (c - f) / 2, h = (r - s) * 6;
1204
- e[h + 0] = u, e[h + 1] = x + (Math.abs(u) + x) * ge, e[h + 2] = y, e[h + 3] = w + (Math.abs(y) + w) * ge, e[h + 4] = g, e[h + 5] = m + (Math.abs(g) + m) * ge;
1205
- }
1206
- return e;
1207
- }
1208
- shiftPrimitiveOffsets(t) {
1209
- const i = this._indirectBuffer;
1210
- if (i)
1211
- for (let e = 0, s = i.length; e < s; e++)
1212
- i[e] += t;
1213
- else {
1214
- const e = this._roots;
1215
- for (let s = 0; s < e.length; s++) {
1216
- const r = e[s], o = new Uint32Array(r), d = new Uint16Array(r), a = r.byteLength / X;
1217
- for (let f = 0; f < a; f++) {
1218
- const l = C * f, p = 2 * l;
1219
- q(p, d) && (o[l + 6] += t);
1220
- }
1221
- }
1222
- }
1223
- }
1224
- traverse(t, i = 0) {
1225
- const e = this._roots[i], s = new Uint32Array(e), r = new Uint16Array(e);
1226
- o(0);
1227
- function o(d, a = 0) {
1228
- const f = d * 2, l = q(f, r);
1229
- if (l) {
1230
- const p = s[d + 6], c = r[f + 14];
1231
- t(a, l, new Float32Array(e, d * 4, 6), p, c);
1232
- } else {
1233
- const p = V(d), c = W(d, s), u = ii(d, s);
1234
- t(a, l, new Float32Array(e, d * 4, 6), u) || (o(p, a + 1), o(c, a + 1));
1235
- }
1236
- }
1237
- }
1238
- refit() {
1239
- const t = this._roots;
1240
- for (let i = 0, e = t.length; i < e; i++) {
1241
- const s = t[i], r = new Uint32Array(s), o = new Uint16Array(s), d = new Float32Array(s), a = s.byteLength / X;
1242
- for (let f = a - 1; f >= 0; f--) {
1243
- const l = f * C, p = l * 2;
1244
- if (q(p, o)) {
1245
- const u = J(l, r), y = Z(p, o);
1246
- this.writePrimitiveRangeBounds(u, y, _t, 0), d.set(_t, l);
1247
- } else {
1248
- const u = V(l), y = W(l, r);
1249
- for (let g = 0; g < 3; g++) {
1250
- const x = d[u + g], w = d[u + g + 3], m = d[y + g], h = d[y + g + 3];
1251
- d[l + g] = x < m ? x : m, d[l + g + 3] = w > h ? w : h;
1252
- }
1253
- }
1254
- }
1255
- }
1256
- }
1257
- getBoundingBox(t) {
1258
- return t.makeEmpty(), this._roots.forEach((e) => {
1259
- k(0, new Float32Array(e), Ri), t.union(Ri);
1260
- }), t;
1261
- }
1262
- // Base shapecast implementation that can be used by subclasses
1263
- // TODO: see if we can get rid of "iterateFunc" here as well as the primitive so the function
1264
- // API aligns with the "shapecast" implementation
1265
- shapecast(t) {
1266
- let {
1267
- boundsTraverseOrder: i,
1268
- intersectsBounds: e,
1269
- intersectsRange: s,
1270
- intersectsPrimitive: r,
1271
- scratchPrimitive: o,
1272
- iterate: d
1273
- } = t;
1274
- if (s && r) {
1275
- const p = s;
1276
- s = (c, u, y, g, x) => p(c, u, y, g, x) ? !0 : d(c, u, this, r, y, g, o);
1277
- } else s || (r ? s = (p, c, u, y) => d(p, c, this, r, u, y, o) : s = (p, c, u) => u);
1278
- let a = !1, f = 0;
1279
- const l = this._roots;
1280
- for (let p = 0, c = l.length; p < c; p++) {
1281
- const u = l[p];
1282
- if (a = us(this, p, e, s, i, f), a)
1283
- break;
1284
- f += u.byteLength / X;
1285
- }
1286
- return a;
1287
- }
1288
- bvhcast(t, i, e) {
1289
- let { intersectsRanges: s } = e;
1290
- return fs(this, t, i, s);
1291
- }
1292
- }
1293
- function ps() {
1294
- return typeof SharedArrayBuffer < "u";
1295
- }
1296
- function si(n) {
1297
- return n.index ? n.index.count : n.attributes.position.count;
1298
- }
1299
- function Me(n) {
1300
- return si(n) / 3;
1301
- }
1302
- function ms(n, t = ArrayBuffer) {
1303
- return n > 65535 ? new Uint32Array(new t(4 * n)) : new Uint16Array(new t(2 * n));
1304
- }
1305
- function hs(n, t) {
1306
- if (!n.index) {
1307
- const i = n.attributes.position.count, e = t.useSharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer, s = ms(i, e);
1308
- n.setIndex(new Gi(s, 1));
1309
- for (let r = 0; r < i; r++)
1310
- s[r] = r;
1311
- }
1312
- }
1313
- function ys(n, t, i) {
1314
- const e = si(n) / i, s = t || n.drawRange, r = s.start / i, o = (s.start + s.count) / i, d = Math.max(0, r), a = Math.min(e, o) - d;
1315
- return {
1316
- offset: Math.floor(d),
1317
- count: Math.floor(a)
1318
- };
1319
- }
1320
- function gs(n, t) {
1321
- return n.groups.map((i) => ({
1322
- offset: i.start / t,
1323
- count: i.count / t
1324
- }));
1325
- }
1326
- function Bi(n, t, i) {
1327
- const e = ys(n, t, i), s = gs(n, i);
1328
- if (!s.length)
1329
- return [e];
1330
- const r = [], o = e.offset, d = e.offset + e.count, a = si(n) / i, f = [];
1331
- for (const c of s) {
1332
- const { offset: u, count: y } = c, g = u, x = isFinite(y) ? y : a - u, w = u + x;
1333
- g < d && w > o && (f.push({ pos: Math.max(o, g), isStart: !0 }), f.push({ pos: Math.min(d, w), isStart: !1 }));
1334
- }
1335
- f.sort((c, u) => c.pos !== u.pos ? c.pos - u.pos : c.type === "end" ? -1 : 1);
1336
- let l = 0, p = null;
1337
- for (const c of f) {
1338
- const u = c.pos;
1339
- l !== 0 && u !== p && r.push({
1340
- offset: p,
1341
- count: u - p
1342
- }), l += c.isStart ? 1 : -1, p = u;
1343
- }
1344
- return r;
1345
- }
1346
- function ws(n, t) {
1347
- const i = n[n.length - 1], e = i.offset + i.count > 2 ** 16, s = n.reduce((f, l) => f + l.count, 0), r = e ? 4 : 2, o = t ? new SharedArrayBuffer(s * r) : new ArrayBuffer(s * r), d = e ? new Uint32Array(o) : new Uint16Array(o);
1348
- let a = 0;
1349
- for (let f = 0; f < n.length; f++) {
1350
- const { offset: l, count: p } = n[f];
1351
- for (let c = 0; c < p; c++)
1352
- d[a + c] = l + c;
1353
- a += p;
1354
- }
1355
- return d;
1356
- }
1357
- class bs extends ds {
1358
- get indirect() {
1359
- return !!this._indirectBuffer;
1360
- }
1361
- get primitiveStride() {
1362
- return null;
1363
- }
1364
- get primitiveBufferStride() {
1365
- return this.indirect ? 1 : this.primitiveStride;
1366
- }
1367
- set primitiveBufferStride(t) {
1368
- }
1369
- get primitiveBuffer() {
1370
- return this.indirect ? this._indirectBuffer : this.geometry.index.array;
1371
- }
1372
- set primitiveBuffer(t) {
1373
- }
1374
- constructor(t, i = {}) {
1375
- if (t.isBufferGeometry) {
1376
- if (t.index && t.index.isInterleavedBufferAttribute)
1377
- throw new Error("BVH: InterleavedBufferAttribute is not supported for the index attribute.");
1378
- } else throw new Error("BVH: Only BufferGeometries are supported.");
1379
- if (i.useSharedArrayBuffer && !ps())
1380
- throw new Error("BVH: SharedArrayBuffer is not available.");
1381
- super(), this.geometry = t, this.resolvePrimitiveIndex = i.indirect ? (e) => this._indirectBuffer[e] : (e) => e, this.primitiveBuffer = null, this.primitiveBufferStride = null, this._indirectBuffer = null, i = {
1382
- ...sn,
1383
- ...i
1384
- }, i[ei] || this.init(i);
1385
- }
1386
- init(t) {
1387
- const { geometry: i, primitiveStride: e } = this;
1388
- if (t.indirect) {
1389
- const s = Bi(i, t.range, e), r = ws(s, t.useSharedArrayBuffer);
1390
- this._indirectBuffer = r;
1391
- } else
1392
- hs(i, t);
1393
- super.init(t), !i.boundingBox && t.setBoundingBox && (i.boundingBox = this.getBoundingBox(new it()));
1394
- }
1395
- // Abstract methods to be implemented by subclasses
1396
- getRootRanges(t) {
1397
- return this.indirect ? [{ offset: 0, count: this._indirectBuffer.length }] : Bi(this.geometry, t, this.primitiveStride);
1398
- }
1399
- raycastObject3D() {
1400
- throw new Error("BVH: raycastObject3D() not implemented");
1401
- }
1402
- }
1403
- class dt {
1404
- constructor() {
1405
- this.min = 1 / 0, this.max = -1 / 0;
1406
- }
1407
- setFromPointsField(t, i) {
1408
- let e = 1 / 0, s = -1 / 0;
1409
- for (let r = 0, o = t.length; r < o; r++) {
1410
- const a = t[r][i];
1411
- e = a < e ? a : e, s = a > s ? a : s;
1412
- }
1413
- this.min = e, this.max = s;
1414
- }
1415
- setFromPoints(t, i) {
1416
- let e = 1 / 0, s = -1 / 0;
1417
- for (let r = 0, o = i.length; r < o; r++) {
1418
- const d = i[r], a = t.dot(d);
1419
- e = a < e ? a : e, s = a > s ? a : s;
1420
- }
1421
- this.min = e, this.max = s;
1422
- }
1423
- isSeparated(t) {
1424
- return this.min > t.max || t.min > this.max;
1425
- }
1426
- }
1427
- dt.prototype.setFromBox = /* @__PURE__ */ function() {
1428
- const n = /* @__PURE__ */ new z();
1429
- return function(i, e) {
1430
- const s = e.min, r = e.max;
1431
- let o = 1 / 0, d = -1 / 0;
1432
- for (let a = 0; a <= 1; a++)
1433
- for (let f = 0; f <= 1; f++)
1434
- for (let l = 0; l <= 1; l++) {
1435
- n.x = s.x * a + r.x * (1 - a), n.y = s.y * f + r.y * (1 - f), n.z = s.z * l + r.z * (1 - l);
1436
- const p = i.dot(n);
1437
- o = Math.min(p, o), d = Math.max(p, d);
1438
- }
1439
- this.min = o, this.max = d;
1440
- };
1441
- }();
1442
- const xs = /* @__PURE__ */ function() {
1443
- const n = /* @__PURE__ */ new z(), t = /* @__PURE__ */ new z(), i = /* @__PURE__ */ new z();
1444
- return function(s, r, o) {
1445
- const d = s.start, a = n, f = r.start, l = t;
1446
- i.subVectors(d, f), n.subVectors(s.end, s.start), t.subVectors(r.end, r.start);
1447
- const p = i.dot(l), c = l.dot(a), u = l.dot(l), y = i.dot(a), x = a.dot(a) * u - c * c;
1448
- let w, m;
1449
- x !== 0 ? w = (p * c - y * u) / x : w = 0, m = (p + w * c) / u, o.x = w, o.y = m;
1450
- };
1451
- }(), oi = /* @__PURE__ */ function() {
1452
- const n = /* @__PURE__ */ new yt(), t = /* @__PURE__ */ new z(), i = /* @__PURE__ */ new z();
1453
- return function(s, r, o, d) {
1454
- xs(s, r, n);
1455
- let a = n.x, f = n.y;
1456
- if (a >= 0 && a <= 1 && f >= 0 && f <= 1) {
1457
- s.at(a, o), r.at(f, d);
1458
- return;
1459
- } else if (a >= 0 && a <= 1) {
1460
- f < 0 ? r.at(0, d) : r.at(1, d), s.closestPointToPoint(d, !0, o);
1461
- return;
1462
- } else if (f >= 0 && f <= 1) {
1463
- a < 0 ? s.at(0, o) : s.at(1, o), r.closestPointToPoint(o, !0, d);
1464
- return;
1465
- } else {
1466
- let l;
1467
- a < 0 ? l = s.start : l = s.end;
1468
- let p;
1469
- f < 0 ? p = r.start : p = r.end;
1470
- const c = t, u = i;
1471
- if (s.closestPointToPoint(p, !0, t), r.closestPointToPoint(l, !0, i), c.distanceToSquared(p) <= u.distanceToSquared(l)) {
1472
- o.copy(c), d.copy(p);
1473
- return;
1474
- } else {
1475
- o.copy(l), d.copy(u);
1476
- return;
1477
- }
1478
- }
1479
- };
1480
- }(), As = /* @__PURE__ */ function() {
1481
- const n = /* @__PURE__ */ new z(), t = /* @__PURE__ */ new z(), i = /* @__PURE__ */ new Xi(), e = /* @__PURE__ */ new ut();
1482
- return function(r, o) {
1483
- const { radius: d, center: a } = r, { a: f, b: l, c: p } = o;
1484
- if (e.start = f, e.end = l, e.closestPointToPoint(a, !0, n).distanceTo(a) <= d || (e.start = f, e.end = p, e.closestPointToPoint(a, !0, n).distanceTo(a) <= d) || (e.start = l, e.end = p, e.closestPointToPoint(a, !0, n).distanceTo(a) <= d)) return !0;
1485
- const g = o.getPlane(i);
1486
- if (Math.abs(g.distanceToPoint(a)) <= d) {
1487
- const w = g.projectPoint(a, t);
1488
- if (o.containsPoint(w)) return !0;
1489
- }
1490
- return !1;
1491
- };
1492
- }(), Ms = ["x", "y", "z"], ct = 1e-15, _i = ct * ct;
1493
- function tt(n) {
1494
- return Math.abs(n) < ct;
1495
- }
1496
- class ot extends $t {
1497
- constructor(...t) {
1498
- super(...t), this.isExtendedTriangle = !0, this.satAxes = new Array(4).fill().map(() => new z()), this.satBounds = new Array(4).fill().map(() => new dt()), this.points = [this.a, this.b, this.c], this.plane = new Xi(), this.isDegenerateIntoSegment = !1, this.isDegenerateIntoPoint = !1, this.degenerateSegment = new ut(), this.needsUpdate = !0;
1499
- }
1500
- intersectsSphere(t) {
1501
- return As(t, this);
1502
- }
1503
- update() {
1504
- const t = this.a, i = this.b, e = this.c, s = this.points, r = this.satAxes, o = this.satBounds, d = r[0], a = o[0];
1505
- this.getNormal(d), a.setFromPoints(d, s);
1506
- const f = r[1], l = o[1];
1507
- f.subVectors(t, i), l.setFromPoints(f, s);
1508
- const p = r[2], c = o[2];
1509
- p.subVectors(i, e), c.setFromPoints(p, s);
1510
- const u = r[3], y = o[3];
1511
- u.subVectors(e, t), y.setFromPoints(u, s);
1512
- const g = f.length(), x = p.length(), w = u.length();
1513
- this.isDegenerateIntoPoint = !1, this.isDegenerateIntoSegment = !1, g < ct ? x < ct || w < ct ? this.isDegenerateIntoPoint = !0 : (this.isDegenerateIntoSegment = !0, this.degenerateSegment.start.copy(t), this.degenerateSegment.end.copy(e)) : x < ct ? w < ct ? this.isDegenerateIntoPoint = !0 : (this.isDegenerateIntoSegment = !0, this.degenerateSegment.start.copy(i), this.degenerateSegment.end.copy(t)) : w < ct && (this.isDegenerateIntoSegment = !0, this.degenerateSegment.start.copy(e), this.degenerateSegment.end.copy(i)), this.plane.setFromNormalAndCoplanarPoint(d, t), this.needsUpdate = !1;
1514
- }
1515
- }
1516
- ot.prototype.closestPointToSegment = /* @__PURE__ */ function() {
1517
- const n = /* @__PURE__ */ new z(), t = /* @__PURE__ */ new z(), i = /* @__PURE__ */ new ut();
1518
- return function(s, r = null, o = null) {
1519
- const { start: d, end: a } = s, f = this.points;
1520
- let l, p = 1 / 0;
1521
- for (let c = 0; c < 3; c++) {
1522
- const u = (c + 1) % 3;
1523
- i.start.copy(f[c]), i.end.copy(f[u]), oi(i, s, n, t), l = n.distanceToSquared(t), l < p && (p = l, r && r.copy(n), o && o.copy(t));
1524
- }
1525
- return this.closestPointToPoint(d, n), l = d.distanceToSquared(n), l < p && (p = l, r && r.copy(n), o && o.copy(d)), this.closestPointToPoint(a, n), l = a.distanceToSquared(n), l < p && (p = l, r && r.copy(n), o && o.copy(a)), Math.sqrt(p);
1526
- };
1527
- }();
1528
- ot.prototype.intersectsTriangle = /* @__PURE__ */ function() {
1529
- const n = /* @__PURE__ */ new ot(), t = /* @__PURE__ */ new dt(), i = /* @__PURE__ */ new dt(), e = /* @__PURE__ */ new z(), s = /* @__PURE__ */ new z(), r = /* @__PURE__ */ new z(), o = /* @__PURE__ */ new z(), d = /* @__PURE__ */ new ut(), a = /* @__PURE__ */ new ut(), f = /* @__PURE__ */ new z(), l = /* @__PURE__ */ new yt(), p = /* @__PURE__ */ new yt();
1530
- function c(h, b, A, P) {
1531
- const M = e;
1532
- !h.isDegenerateIntoPoint && !h.isDegenerateIntoSegment ? M.copy(h.plane.normal) : M.copy(b.plane.normal);
1533
- const T = h.satBounds, R = h.satAxes;
1534
- for (let S = 1; S < 4; S++) {
1535
- const _ = T[S], E = R[S];
1536
- if (t.setFromPoints(E, b.points), _.isSeparated(t) || (o.copy(M).cross(E), t.setFromPoints(o, h.points), i.setFromPoints(o, b.points), t.isSeparated(i))) return !1;
1537
- }
1538
- const v = b.satBounds, B = b.satAxes;
1539
- for (let S = 1; S < 4; S++) {
1540
- const _ = v[S], E = B[S];
1541
- if (t.setFromPoints(E, h.points), _.isSeparated(t) || (o.crossVectors(M, E), t.setFromPoints(o, h.points), i.setFromPoints(o, b.points), t.isSeparated(i))) return !1;
1542
- }
1543
- return A && (P || console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."), A.start.set(0, 0, 0), A.end.set(0, 0, 0)), !0;
1544
- }
1545
- function u(h, b, A, P, M, T, R, v, B, S, _) {
1546
- let E = R / (R - v);
1547
- S.x = P + (M - P) * E, _.start.subVectors(b, h).multiplyScalar(E).add(h), E = R / (R - B), S.y = P + (T - P) * E, _.end.subVectors(A, h).multiplyScalar(E).add(h);
1548
- }
1549
- function y(h, b, A, P, M, T, R, v, B, S, _) {
1550
- if (M > 0)
1551
- u(h.c, h.a, h.b, P, b, A, B, R, v, S, _);
1552
- else if (T > 0)
1553
- u(h.b, h.a, h.c, A, b, P, v, R, B, S, _);
1554
- else if (v * B > 0 || R != 0)
1555
- u(h.a, h.b, h.c, b, A, P, R, v, B, S, _);
1556
- else if (v != 0)
1557
- u(h.b, h.a, h.c, A, b, P, v, R, B, S, _);
1558
- else if (B != 0)
1559
- u(h.c, h.a, h.b, P, b, A, B, R, v, S, _);
1560
- else
1561
- return !0;
1562
- return !1;
1563
- }
1564
- function g(h, b, A, P) {
1565
- const M = b.degenerateSegment, T = h.plane.distanceToPoint(M.start), R = h.plane.distanceToPoint(M.end);
1566
- return tt(T) ? tt(R) ? c(h, b, A, P) : (A && (A.start.copy(M.start), A.end.copy(M.start)), h.containsPoint(M.start)) : tt(R) ? (A && (A.start.copy(M.end), A.end.copy(M.end)), h.containsPoint(M.end)) : h.plane.intersectLine(M, e) != null ? (A && (A.start.copy(e), A.end.copy(e)), h.containsPoint(e)) : !1;
1567
- }
1568
- function x(h, b, A) {
1569
- const P = b.a;
1570
- return tt(h.plane.distanceToPoint(P)) && h.containsPoint(P) ? (A && (A.start.copy(P), A.end.copy(P)), !0) : !1;
1571
- }
1572
- function w(h, b, A) {
1573
- const P = h.degenerateSegment, M = b.a;
1574
- return P.closestPointToPoint(M, !0, e), M.distanceToSquared(e) < _i ? (A && (A.start.copy(M), A.end.copy(M)), !0) : !1;
1575
- }
1576
- function m(h, b, A, P) {
1577
- if (h.isDegenerateIntoSegment)
1578
- if (b.isDegenerateIntoSegment) {
1579
- const M = h.degenerateSegment, T = b.degenerateSegment, R = s, v = r;
1580
- M.delta(R), T.delta(v);
1581
- const B = e.subVectors(T.start, M.start), S = R.x * v.y - R.y * v.x;
1582
- if (tt(S))
1583
- return !1;
1584
- const _ = (B.x * v.y - B.y * v.x) / S, E = -(R.x * B.y - R.y * B.x) / S;
1585
- if (_ < 0 || _ > 1 || E < 0 || E > 1)
1586
- return !1;
1587
- const F = M.start.z + R.z * _, D = T.start.z + v.z * E;
1588
- return tt(F - D) ? (A && (A.start.copy(M.start).addScaledVector(R, _), A.end.copy(M.start).addScaledVector(R, _)), !0) : !1;
1589
- } else return b.isDegenerateIntoPoint ? w(h, b, A) : g(b, h, A, P);
1590
- else {
1591
- if (h.isDegenerateIntoPoint)
1592
- return b.isDegenerateIntoPoint ? b.a.distanceToSquared(h.a) < _i ? (A && (A.start.copy(h.a), A.end.copy(h.a)), !0) : !1 : b.isDegenerateIntoSegment ? w(b, h, A) : x(b, h, A);
1593
- if (b.isDegenerateIntoPoint)
1594
- return x(h, b, A);
1595
- if (b.isDegenerateIntoSegment)
1596
- return g(h, b, A, P);
1597
- }
1598
- }
1599
- return function(b, A = null, P = !1) {
1600
- this.needsUpdate && this.update(), b.isExtendedTriangle ? b.needsUpdate && b.update() : (n.copy(b), n.update(), b = n);
1601
- const M = m(this, b, A, P);
1602
- if (M !== void 0)
1603
- return M;
1604
- const T = this.plane, R = b.plane;
1605
- let v = R.distanceToPoint(this.a), B = R.distanceToPoint(this.b), S = R.distanceToPoint(this.c);
1606
- tt(v) && (v = 0), tt(B) && (B = 0), tt(S) && (S = 0);
1607
- const _ = v * B, E = v * S;
1608
- if (_ > 0 && E > 0)
1609
- return !1;
1610
- let F = T.distanceToPoint(b.a), D = T.distanceToPoint(b.b), U = T.distanceToPoint(b.c);
1611
- tt(F) && (F = 0), tt(D) && (D = 0), tt(U) && (U = 0);
1612
- const L = F * D, Y = F * U;
1613
- if (L > 0 && Y > 0)
1614
- return !1;
1615
- s.copy(T.normal), r.copy(R.normal);
1616
- const Mt = s.cross(r);
1617
- let vt = 0, Pt = Math.abs(Mt.x);
1618
- const kt = Math.abs(Mt.y);
1619
- kt > Pt && (Pt = kt, vt = 1), Math.abs(Mt.z) > Pt && (vt = 2);
1620
- const nt = Ms[vt], Yt = this.a[nt], an = this.b[nt], cn = this.c[nt], ln = b.a[nt], un = b.b[nt], fn = b.c[nt];
1621
- if (y(this, Yt, an, cn, _, E, v, B, S, l, d))
1622
- return c(this, b, A, P);
1623
- if (y(b, ln, un, fn, L, Y, F, D, U, p, a))
1624
- return c(this, b, A, P);
1625
- if (l.y < l.x) {
1626
- const Pe = l.y;
1627
- l.y = l.x, l.x = Pe, f.copy(d.start), d.start.copy(d.end), d.end.copy(f);
1628
- }
1629
- if (p.y < p.x) {
1630
- const Pe = p.y;
1631
- p.y = p.x, p.x = Pe, f.copy(a.start), a.start.copy(a.end), a.end.copy(f);
1632
- }
1633
- return l.y < p.x || p.y < l.x ? !1 : (A && (p.x > l.x ? A.start.copy(a.start) : A.start.copy(d.start), p.y < l.y ? A.end.copy(a.end) : A.end.copy(d.end)), !0);
1634
- };
1635
- }();
1636
- ot.prototype.distanceToPoint = /* @__PURE__ */ function() {
1637
- const n = /* @__PURE__ */ new z();
1638
- return function(i) {
1639
- return this.closestPointToPoint(i, n), i.distanceTo(n);
1640
- };
1641
- }();
1642
- ot.prototype.distanceToTriangle = /* @__PURE__ */ function() {
1643
- const n = /* @__PURE__ */ new z(), t = /* @__PURE__ */ new z(), i = ["a", "b", "c"], e = /* @__PURE__ */ new ut(), s = /* @__PURE__ */ new ut();
1644
- return function(o, d = null, a = null) {
1645
- const f = d || a ? e : null;
1646
- if (this.intersectsTriangle(o, f))
1647
- return (d || a) && (d && f.getCenter(d), a && f.getCenter(a)), 0;
1648
- let l = 1 / 0;
1649
- for (let p = 0; p < 3; p++) {
1650
- let c;
1651
- const u = i[p], y = o[u];
1652
- this.closestPointToPoint(y, n), c = y.distanceToSquared(n), c < l && (l = c, d && d.copy(n), a && a.copy(y));
1653
- const g = this[u];
1654
- o.closestPointToPoint(g, n), c = g.distanceToSquared(n), c < l && (l = c, d && d.copy(g), a && a.copy(n));
1655
- }
1656
- for (let p = 0; p < 3; p++) {
1657
- const c = i[p], u = i[(p + 1) % 3];
1658
- e.set(this[c], this[u]);
1659
- for (let y = 0; y < 3; y++) {
1660
- const g = i[y], x = i[(y + 1) % 3];
1661
- s.set(o[g], o[x]), oi(e, s, n, t);
1662
- const w = n.distanceToSquared(t);
1663
- w < l && (l = w, d && d.copy(n), a && a.copy(t));
1664
- }
1665
- }
1666
- return Math.sqrt(l);
1667
- };
1668
- }();
1669
- class Q {
1670
- constructor(t, i, e) {
1671
- this.isOrientedBox = !0, this.min = new z(), this.max = new z(), this.matrix = new H(), this.invMatrix = new H(), this.points = new Array(8).fill().map(() => new z()), this.satAxes = new Array(3).fill().map(() => new z()), this.satBounds = new Array(3).fill().map(() => new dt()), this.alignedSatBounds = new Array(3).fill().map(() => new dt()), this.needsUpdate = !1, t && this.min.copy(t), i && this.max.copy(i), e && this.matrix.copy(e);
1672
- }
1673
- set(t, i, e) {
1674
- this.min.copy(t), this.max.copy(i), this.matrix.copy(e), this.needsUpdate = !0;
1675
- }
1676
- copy(t) {
1677
- this.min.copy(t.min), this.max.copy(t.max), this.matrix.copy(t.matrix), this.needsUpdate = !0;
1678
- }
1679
- }
1680
- Q.prototype.update = /* @__PURE__ */ function() {
1681
- return function() {
1682
- const t = this.matrix, i = this.min, e = this.max, s = this.points;
1683
- for (let f = 0; f <= 1; f++)
1684
- for (let l = 0; l <= 1; l++)
1685
- for (let p = 0; p <= 1; p++) {
1686
- const c = 1 * f | 2 * l | 4 * p, u = s[c];
1687
- u.x = f ? e.x : i.x, u.y = l ? e.y : i.y, u.z = p ? e.z : i.z, u.applyMatrix4(t);
1688
- }
1689
- const r = this.satBounds, o = this.satAxes, d = s[0];
1690
- for (let f = 0; f < 3; f++) {
1691
- const l = o[f], p = r[f], c = 1 << f, u = s[c];
1692
- l.subVectors(d, u), p.setFromPoints(l, s);
1693
- }
1694
- const a = this.alignedSatBounds;
1695
- a[0].setFromPointsField(s, "x"), a[1].setFromPointsField(s, "y"), a[2].setFromPointsField(s, "z"), this.invMatrix.copy(this.matrix).invert(), this.needsUpdate = !1;
1696
- };
1697
- }();
1698
- Q.prototype.intersectsBox = /* @__PURE__ */ function() {
1699
- const n = /* @__PURE__ */ new dt();
1700
- return function(i) {
1701
- this.needsUpdate && this.update();
1702
- const e = i.min, s = i.max, r = this.satBounds, o = this.satAxes, d = this.alignedSatBounds;
1703
- if (n.min = e.x, n.max = s.x, d[0].isSeparated(n) || (n.min = e.y, n.max = s.y, d[1].isSeparated(n)) || (n.min = e.z, n.max = s.z, d[2].isSeparated(n))) return !1;
1704
- for (let a = 0; a < 3; a++) {
1705
- const f = o[a], l = r[a];
1706
- if (n.setFromBox(f, i), l.isSeparated(n)) return !1;
1707
- }
1708
- return !0;
1709
- };
1710
- }();
1711
- Q.prototype.intersectsTriangle = /* @__PURE__ */ function() {
1712
- const n = /* @__PURE__ */ new ot(), t = /* @__PURE__ */ new Array(3), i = /* @__PURE__ */ new dt(), e = /* @__PURE__ */ new dt(), s = /* @__PURE__ */ new z();
1713
- return function(o) {
1714
- this.needsUpdate && this.update(), o.isExtendedTriangle ? o.needsUpdate && o.update() : (n.copy(o), n.update(), o = n);
1715
- const d = this.satBounds, a = this.satAxes;
1716
- t[0] = o.a, t[1] = o.b, t[2] = o.c;
1717
- for (let c = 0; c < 3; c++) {
1718
- const u = d[c], y = a[c];
1719
- if (i.setFromPoints(y, t), u.isSeparated(i)) return !1;
1720
- }
1721
- const f = o.satBounds, l = o.satAxes, p = this.points;
1722
- for (let c = 0; c < 3; c++) {
1723
- const u = f[c], y = l[c];
1724
- if (i.setFromPoints(y, p), u.isSeparated(i)) return !1;
1725
- }
1726
- for (let c = 0; c < 3; c++) {
1727
- const u = a[c];
1728
- for (let y = 0; y < 4; y++) {
1729
- const g = l[y];
1730
- if (s.crossVectors(u, g), i.setFromPoints(s, t), e.setFromPoints(s, p), i.isSeparated(e)) return !1;
1731
- }
1732
- }
1733
- return !0;
1734
- };
1735
- }();
1736
- Q.prototype.closestPointToPoint = /* @__PURE__ */ function() {
1737
- return function(t, i) {
1738
- return this.needsUpdate && this.update(), i.copy(t).applyMatrix4(this.invMatrix).clamp(this.min, this.max).applyMatrix4(this.matrix), i;
1739
- };
1740
- }();
1741
- Q.prototype.distanceToPoint = function() {
1742
- const n = new z();
1743
- return function(i) {
1744
- return this.closestPointToPoint(i, n), i.distanceTo(n);
1745
- };
1746
- }();
1747
- Q.prototype.distanceToBox = /* @__PURE__ */ function() {
1748
- const n = ["x", "y", "z"], t = /* @__PURE__ */ new Array(12).fill().map(() => new ut()), i = /* @__PURE__ */ new Array(12).fill().map(() => new ut()), e = /* @__PURE__ */ new z(), s = /* @__PURE__ */ new z();
1749
- return function(o, d = 0, a = null, f = null) {
1750
- if (this.needsUpdate && this.update(), this.intersectsBox(o))
1751
- return (a || f) && (o.getCenter(s), this.closestPointToPoint(s, e), o.closestPointToPoint(e, s), a && a.copy(e), f && f.copy(s)), 0;
1752
- const l = d * d, p = o.min, c = o.max, u = this.points;
1753
- let y = 1 / 0;
1754
- for (let x = 0; x < 8; x++) {
1755
- const w = u[x];
1756
- s.copy(w).clamp(p, c);
1757
- const m = w.distanceToSquared(s);
1758
- if (m < y && (y = m, a && a.copy(w), f && f.copy(s), m < l))
1759
- return Math.sqrt(m);
1760
- }
1761
- let g = 0;
1762
- for (let x = 0; x < 3; x++)
1763
- for (let w = 0; w <= 1; w++)
1764
- for (let m = 0; m <= 1; m++) {
1765
- const h = (x + 1) % 3, b = (x + 2) % 3, A = w << h | m << b, P = 1 << x | w << h | m << b, M = u[A], T = u[P];
1766
- t[g].set(M, T);
1767
- const v = n[x], B = n[h], S = n[b], _ = i[g], E = _.start, F = _.end;
1768
- E[v] = p[v], E[B] = w ? p[B] : c[B], E[S] = m ? p[S] : c[B], F[v] = c[v], F[B] = w ? p[B] : c[B], F[S] = m ? p[S] : c[B], g++;
1769
- }
1770
- for (let x = 0; x <= 1; x++)
1771
- for (let w = 0; w <= 1; w++)
1772
- for (let m = 0; m <= 1; m++) {
1773
- s.x = x ? c.x : p.x, s.y = w ? c.y : p.y, s.z = m ? c.z : p.z, this.closestPointToPoint(s, e);
1774
- const h = s.distanceToSquared(e);
1775
- if (h < y && (y = h, a && a.copy(e), f && f.copy(s), h < l))
1776
- return Math.sqrt(h);
1777
- }
1778
- for (let x = 0; x < 12; x++) {
1779
- const w = t[x];
1780
- for (let m = 0; m < 12; m++) {
1781
- const h = i[m];
1782
- oi(w, h, e, s);
1783
- const b = e.distanceToSquared(s);
1784
- if (b < y && (y = b, a && a.copy(e), f && f.copy(s), b < l))
1785
- return Math.sqrt(b);
1786
- }
1787
- }
1788
- return Math.sqrt(y);
1789
- };
1790
- }();
1791
- class vs extends ni {
1792
- constructor() {
1793
- super(() => new ot());
1794
- }
1795
- }
1796
- const et = /* @__PURE__ */ new vs(), Ct = /* @__PURE__ */ new z(), ze = /* @__PURE__ */ new z();
1797
- function Ps(n, t, i = {}, e = 0, s = 1 / 0) {
1798
- const r = e * e, o = s * s;
1799
- let d = 1 / 0, a = null;
1800
- if (n.shapecast(
1801
- {
1802
- boundsTraverseOrder: (l) => (Ct.copy(t).clamp(l.min, l.max), Ct.distanceToSquared(t)),
1803
- intersectsBounds: (l, p, c) => c < d && c < o,
1804
- intersectsTriangle: (l, p) => {
1805
- l.closestPointToPoint(t, Ct);
1806
- const c = t.distanceToSquared(Ct);
1807
- return c < d && (ze.copy(Ct), d = c, a = p), c < r;
1808
- }
1809
- }
1810
- ), d === 1 / 0) return null;
1811
- const f = Math.sqrt(d);
1812
- return i.point ? i.point.copy(ze) : i.point = ze.clone(), i.distance = f, i.faceIndex = a, i;
1813
- }
1814
- const ne = parseInt(Ji) >= 169, Ss = parseInt(Ji) <= 161, bt = /* @__PURE__ */ new z(), xt = /* @__PURE__ */ new z(), At = /* @__PURE__ */ new z(), se = /* @__PURE__ */ new yt(), oe = /* @__PURE__ */ new yt(), re = /* @__PURE__ */ new yt(), Ei = /* @__PURE__ */ new z(), zi = /* @__PURE__ */ new z(), Ui = /* @__PURE__ */ new z(), qt = /* @__PURE__ */ new z();
1815
- function Ts(n, t, i, e, s, r, o, d) {
1816
- let a;
1817
- if (r === Tn ? a = n.intersectTriangle(e, i, t, !0, s) : a = n.intersectTriangle(t, i, e, r !== Rn, s), a === null) return null;
1818
- const f = n.origin.distanceTo(s);
1819
- return f < o || f > d ? null : {
1820
- distance: f,
1821
- point: s.clone()
1822
- };
1823
- }
1824
- function Fi(n, t, i, e, s, r, o, d, a, f, l) {
1825
- bt.fromBufferAttribute(t, r), xt.fromBufferAttribute(t, o), At.fromBufferAttribute(t, d);
1826
- const p = Ts(n, bt, xt, At, qt, a, f, l);
1827
- if (p) {
1828
- if (e) {
1829
- se.fromBufferAttribute(e, r), oe.fromBufferAttribute(e, o), re.fromBufferAttribute(e, d), p.uv = new yt();
1830
- const u = $t.getInterpolation(qt, bt, xt, At, se, oe, re, p.uv);
1831
- ne || (p.uv = u);
1832
- }
1833
- if (s) {
1834
- se.fromBufferAttribute(s, r), oe.fromBufferAttribute(s, o), re.fromBufferAttribute(s, d), p.uv1 = new yt();
1835
- const u = $t.getInterpolation(qt, bt, xt, At, se, oe, re, p.uv1);
1836
- ne || (p.uv1 = u), Ss && (p.uv2 = p.uv1);
1837
- }
1838
- if (i) {
1839
- Ei.fromBufferAttribute(i, r), zi.fromBufferAttribute(i, o), Ui.fromBufferAttribute(i, d), p.normal = new z();
1840
- const u = $t.getInterpolation(qt, bt, xt, At, Ei, zi, Ui, p.normal);
1841
- p.normal.dot(n.direction) > 0 && p.normal.multiplyScalar(-1), ne || (p.normal = u);
1842
- }
1843
- const c = {
1844
- a: r,
1845
- b: o,
1846
- c: d,
1847
- normal: new z(),
1848
- materialIndex: 0
1849
- };
1850
- if ($t.getNormal(bt, xt, At, c.normal), p.face = c, p.faceIndex = r, ne) {
1851
- const u = new z();
1852
- $t.getBarycoord(qt, bt, xt, At, u), p.barycoord = u;
1853
- }
1854
- }
1855
- return p;
1856
- }
1857
- function $i(n) {
1858
- return n && n.isMaterial ? n.side : n;
1859
- }
1860
- function ve(n, t, i, e, s, r, o) {
1861
- const d = e * 3;
1862
- let a = d + 0, f = d + 1, l = d + 2;
1863
- const { index: p, groups: c } = n;
1864
- n.index && (a = p.getX(a), f = p.getX(f), l = p.getX(l));
1865
- const { position: u, normal: y, uv: g, uv1: x } = n.attributes;
1866
- if (Array.isArray(t)) {
1867
- const w = e * 3;
1868
- for (let m = 0, h = c.length; m < h; m++) {
1869
- const { start: b, count: A, materialIndex: P } = c[m];
1870
- if (w >= b && w < b + A) {
1871
- const M = $i(t[P]), T = Fi(i, u, y, g, x, a, f, l, M, r, o);
1872
- if (T)
1873
- if (T.faceIndex = e, T.face.materialIndex = P, s)
1874
- s.push(T);
1875
- else
1876
- return T;
1877
- }
1878
- }
1879
- } else {
1880
- const w = $i(t), m = Fi(i, u, y, g, x, a, f, l, w, r, o);
1881
- if (m)
1882
- if (m.faceIndex = e, m.face.materialIndex = 0, s)
1883
- s.push(m);
1884
- else
1885
- return m;
1886
- }
1887
- return null;
1888
- }
1889
- function j(n, t, i, e) {
1890
- const s = n.a, r = n.b, o = n.c;
1891
- let d = t, a = t + 1, f = t + 2;
1892
- i && (d = i.getX(d), a = i.getX(a), f = i.getX(f)), s.x = e.getX(d), s.y = e.getY(d), s.z = e.getZ(d), r.x = e.getX(a), r.y = e.getY(a), r.z = e.getZ(a), o.x = e.getX(f), o.y = e.getY(f), o.z = e.getZ(f);
1893
- }
1894
- function Rs(n, t, i, e, s, r, o, d) {
1895
- const { geometry: a, _indirectBuffer: f } = n;
1896
- for (let l = e, p = e + s; l < p; l++)
1897
- ve(a, t, i, l, r, o, d);
1898
- }
1899
- function Bs(n, t, i, e, s, r, o) {
1900
- const { geometry: d, _indirectBuffer: a } = n;
1901
- let f = 1 / 0, l = null;
1902
- for (let p = e, c = e + s; p < c; p++) {
1903
- let u;
1904
- u = ve(d, t, i, p, null, r, o), u && u.distance < f && (l = u, f = u.distance);
1905
- }
1906
- return l;
1907
- }
1908
- function _s(n, t, i, e, s, r, o) {
1909
- const { geometry: d } = i, { index: a } = d, f = d.attributes.position;
1910
- for (let l = n, p = t + n; l < p; l++) {
1911
- let c;
1912
- if (c = l, j(o, c * 3, a, f), o.needsUpdate = !0, e(o, c, s, r))
1913
- return !0;
1914
- }
1915
- return !1;
1916
- }
1917
- function Es(n, t = null) {
1918
- t && Array.isArray(t) && (t = new Set(t));
1919
- const i = n.geometry, e = i.index ? i.index.array : null, s = i.attributes.position;
1920
- let r, o, d, a, f = 0;
1921
- const l = n._roots;
1922
- for (let c = 0, u = l.length; c < u; c++)
1923
- r = l[c], o = new Uint32Array(r), d = new Uint16Array(r), a = new Float32Array(r), p(0, f), f += r.byteLength;
1924
- function p(c, u, y = !1) {
1925
- const g = c * 2;
1926
- if (q(g, d)) {
1927
- const x = J(c, o), w = Z(g, d);
1928
- let m = 1 / 0, h = 1 / 0, b = 1 / 0, A = -1 / 0, P = -1 / 0, M = -1 / 0;
1929
- for (let T = 3 * x, R = 3 * (x + w); T < R; T++) {
1930
- let v = e[T];
1931
- const B = s.getX(v), S = s.getY(v), _ = s.getZ(v);
1932
- B < m && (m = B), B > A && (A = B), S < h && (h = S), S > P && (P = S), _ < b && (b = _), _ > M && (M = _);
1933
- }
1934
- return a[c + 0] !== m || a[c + 1] !== h || a[c + 2] !== b || a[c + 3] !== A || a[c + 4] !== P || a[c + 5] !== M ? (a[c + 0] = m, a[c + 1] = h, a[c + 2] = b, a[c + 3] = A, a[c + 4] = P, a[c + 5] = M, !0) : !1;
1935
- } else {
1936
- const x = V(c), w = W(c, o);
1937
- let m = y, h = !1, b = !1;
1938
- if (t) {
1939
- if (!m) {
1940
- const v = x / C + u / X, B = w / C + u / X;
1941
- h = t.has(v), b = t.has(B), m = !h && !b;
1942
- }
1943
- } else
1944
- h = !0, b = !0;
1945
- const A = m || h, P = m || b;
1946
- let M = !1;
1947
- A && (M = p(x, u, m));
1948
- let T = !1;
1949
- P && (T = p(w, u, m));
1950
- const R = M || T;
1951
- if (R)
1952
- for (let v = 0; v < 3; v++) {
1953
- const B = x + v, S = w + v, _ = a[B], E = a[B + 3], F = a[S], D = a[S + 3];
1954
- a[c + v] = _ < F ? _ : F, a[c + v + 3] = E > D ? E : D;
1955
- }
1956
- return R;
1957
- }
1958
- }
1959
- }
1960
- function gt(n, t, i, e, s) {
1961
- let r, o, d, a, f, l;
1962
- const p = 1 / i.direction.x, c = 1 / i.direction.y, u = 1 / i.direction.z, y = i.origin.x, g = i.origin.y, x = i.origin.z;
1963
- let w = t[n], m = t[n + 3], h = t[n + 1], b = t[n + 3 + 1], A = t[n + 2], P = t[n + 3 + 2];
1964
- return p >= 0 ? (r = (w - y) * p, o = (m - y) * p) : (r = (m - y) * p, o = (w - y) * p), c >= 0 ? (d = (h - g) * c, a = (b - g) * c) : (d = (b - g) * c, a = (h - g) * c), r > a || d > o || ((d > r || isNaN(r)) && (r = d), (a < o || isNaN(o)) && (o = a), u >= 0 ? (f = (A - x) * u, l = (P - x) * u) : (f = (P - x) * u, l = (A - x) * u), r > l || f > o) ? !1 : ((f > r || r !== r) && (r = f), (l < o || o !== o) && (o = l), r <= s && o >= e);
1965
- }
1966
- function zs(n, t, i, e, s, r, o, d) {
1967
- const { geometry: a, _indirectBuffer: f } = n;
1968
- for (let l = e, p = e + s; l < p; l++) {
1969
- let c = f ? f[l] : l;
1970
- ve(a, t, i, c, r, o, d);
1971
- }
1972
- }
1973
- function Us(n, t, i, e, s, r, o) {
1974
- const { geometry: d, _indirectBuffer: a } = n;
1975
- let f = 1 / 0, l = null;
1976
- for (let p = e, c = e + s; p < c; p++) {
1977
- let u;
1978
- u = ve(d, t, i, a ? a[p] : p, null, r, o), u && u.distance < f && (l = u, f = u.distance);
1979
- }
1980
- return l;
1981
- }
1982
- function Fs(n, t, i, e, s, r, o) {
1983
- const { geometry: d } = i, { index: a } = d, f = d.attributes.position;
1984
- for (let l = n, p = t + n; l < p; l++) {
1985
- let c;
1986
- if (c = i.resolveTriangleIndex(l), j(o, c * 3, a, f), o.needsUpdate = !0, e(o, c, s, r))
1987
- return !0;
1988
- }
1989
- return !1;
1990
- }
1991
- function $s(n, t, i, e, s, r, o) {
1992
- I.setBuffer(n._roots[t]), Oe(0, n, i, e, s, r, o), I.clearBuffer();
1993
- }
1994
- function Oe(n, t, i, e, s, r, o) {
1995
- const { float32Array: d, uint16Array: a, uint32Array: f } = I, l = n * 2;
1996
- if (q(l, a)) {
1997
- const c = J(n, f), u = Z(l, a);
1998
- Rs(t, i, e, c, u, s, r, o);
1999
- } else {
2000
- const c = V(n);
2001
- gt(c, d, e, r, o) && Oe(c, t, i, e, s, r, o);
2002
- const u = W(n, f);
2003
- gt(u, d, e, r, o) && Oe(u, t, i, e, s, r, o);
2004
- }
2005
- }
2006
- const Is = ["x", "y", "z"];
2007
- function Ds(n, t, i, e, s, r) {
2008
- I.setBuffer(n._roots[t]);
2009
- const o = Ve(0, n, i, e, s, r);
2010
- return I.clearBuffer(), o;
2011
- }
2012
- function Ve(n, t, i, e, s, r) {
2013
- const { float32Array: o, uint16Array: d, uint32Array: a } = I;
2014
- let f = n * 2;
2015
- if (q(f, d)) {
2016
- const p = J(n, a), c = Z(f, d);
2017
- return Bs(t, i, e, p, c, s, r);
2018
- } else {
2019
- const p = ii(n, a), c = Is[p], y = e.direction[c] >= 0;
2020
- let g, x;
2021
- y ? (g = V(n), x = W(n, a)) : (g = W(n, a), x = V(n));
2022
- const m = gt(g, o, e, s, r) ? Ve(g, t, i, e, s, r) : null;
2023
- if (m) {
2024
- const A = m.point[c];
2025
- if (y ? A <= o[x + p] : (
2026
- // min bounding data
2027
- A >= o[x + p + 3]
2028
- ))
2029
- return m;
2030
- }
2031
- const b = gt(x, o, e, s, r) ? Ve(x, t, i, e, s, r) : null;
2032
- return m && b ? m.distance <= b.distance ? m : b : m || b || null;
2033
- }
2034
- }
2035
- const ae = /* @__PURE__ */ new it(), Et = /* @__PURE__ */ new ot(), zt = /* @__PURE__ */ new ot(), Ot = /* @__PURE__ */ new H(), Ii = /* @__PURE__ */ new Q(), ce = /* @__PURE__ */ new Q();
2036
- function Ls(n, t, i, e) {
2037
- I.setBuffer(n._roots[t]);
2038
- const s = We(0, n, i, e);
2039
- return I.clearBuffer(), s;
2040
- }
2041
- function We(n, t, i, e, s = null) {
2042
- const { float32Array: r, uint16Array: o, uint32Array: d } = I;
2043
- let a = n * 2;
2044
- if (s === null && (i.boundingBox || i.computeBoundingBox(), Ii.set(i.boundingBox.min, i.boundingBox.max, e), s = Ii), q(a, o)) {
2045
- const l = t.geometry, p = l.index, c = l.attributes.position, u = i.index, y = i.attributes.position, g = J(n, d), x = Z(a, o);
2046
- if (Ot.copy(e).invert(), i.boundsTree)
2047
- return k(n, r, ce), ce.matrix.copy(Ot), ce.needsUpdate = !0, i.boundsTree.shapecast({
2048
- intersectsBounds: (m) => ce.intersectsBox(m),
2049
- intersectsTriangle: (m) => {
2050
- m.a.applyMatrix4(e), m.b.applyMatrix4(e), m.c.applyMatrix4(e), m.needsUpdate = !0;
2051
- for (let h = g * 3, b = (x + g) * 3; h < b; h += 3)
2052
- if (j(zt, h, p, c), zt.needsUpdate = !0, m.intersectsTriangle(zt))
2053
- return !0;
2054
- return !1;
2055
- }
2056
- });
2057
- {
2058
- const w = Me(i);
2059
- for (let m = g * 3, h = (x + g) * 3; m < h; m += 3) {
2060
- j(Et, m, p, c), Et.a.applyMatrix4(Ot), Et.b.applyMatrix4(Ot), Et.c.applyMatrix4(Ot), Et.needsUpdate = !0;
2061
- for (let b = 0, A = w * 3; b < A; b += 3)
2062
- if (j(zt, b, u, y), zt.needsUpdate = !0, Et.intersectsTriangle(zt))
2063
- return !0;
2064
- }
2065
- }
2066
- } else {
2067
- const l = V(n), p = W(n, d);
2068
- return k(l, r, ae), !!(s.intersectsBox(ae) && We(l, t, i, e, s) || (k(p, r, ae), s.intersectsBox(ae) && We(p, t, i, e, s)));
2069
- }
2070
- }
2071
- const le = /* @__PURE__ */ new H(), Ue = /* @__PURE__ */ new Q(), Vt = /* @__PURE__ */ new Q(), ks = /* @__PURE__ */ new z(), js = /* @__PURE__ */ new z(), Ns = /* @__PURE__ */ new z(), Cs = /* @__PURE__ */ new z();
2072
- function qs(n, t, i, e = {}, s = {}, r = 0, o = 1 / 0) {
2073
- t.boundingBox || t.computeBoundingBox(), Ue.set(t.boundingBox.min, t.boundingBox.max, i), Ue.needsUpdate = !0;
2074
- const d = n.geometry, a = d.attributes.position, f = d.index, l = t.attributes.position, p = t.index, c = et.getPrimitive(), u = et.getPrimitive();
2075
- let y = ks, g = js, x = null, w = null;
2076
- s && (x = Ns, w = Cs);
2077
- let m = 1 / 0, h = null, b = null;
2078
- return le.copy(i).invert(), Vt.matrix.copy(le), n.shapecast(
2079
- {
2080
- boundsTraverseOrder: (A) => Ue.distanceToBox(A),
2081
- intersectsBounds: (A, P, M) => M < m && M < o ? (P && (Vt.min.copy(A.min), Vt.max.copy(A.max), Vt.needsUpdate = !0), !0) : !1,
2082
- intersectsRange: (A, P) => {
2083
- if (t.boundsTree)
2084
- return t.boundsTree.shapecast({
2085
- boundsTraverseOrder: (T) => Vt.distanceToBox(T),
2086
- intersectsBounds: (T, R, v) => v < m && v < o,
2087
- intersectsRange: (T, R) => {
2088
- for (let v = T, B = T + R; v < B; v++) {
2089
- j(u, 3 * v, p, l), u.a.applyMatrix4(i), u.b.applyMatrix4(i), u.c.applyMatrix4(i), u.needsUpdate = !0;
2090
- for (let S = A, _ = A + P; S < _; S++) {
2091
- j(c, 3 * S, f, a), c.needsUpdate = !0;
2092
- const E = c.distanceToTriangle(u, y, x);
2093
- if (E < m && (g.copy(y), w && w.copy(x), m = E, h = S, b = v), E < r)
2094
- return !0;
2095
- }
2096
- }
2097
- }
2098
- });
2099
- {
2100
- const M = Me(t);
2101
- for (let T = 0, R = M; T < R; T++) {
2102
- j(u, 3 * T, p, l), u.a.applyMatrix4(i), u.b.applyMatrix4(i), u.c.applyMatrix4(i), u.needsUpdate = !0;
2103
- for (let v = A, B = A + P; v < B; v++) {
2104
- j(c, 3 * v, f, a), c.needsUpdate = !0;
2105
- const S = c.distanceToTriangle(u, y, x);
2106
- if (S < m && (g.copy(y), w && w.copy(x), m = S, h = v, b = T), S < r)
2107
- return !0;
2108
- }
2109
- }
2110
- }
2111
- }
2112
- }
2113
- ), et.releasePrimitive(c), et.releasePrimitive(u), m === 1 / 0 ? null : (e.point ? e.point.copy(g) : e.point = g.clone(), e.distance = m, e.faceIndex = h, s && (s.point ? s.point.copy(w) : s.point = w.clone(), s.point.applyMatrix4(le), g.applyMatrix4(le), s.distance = g.sub(s.point).length(), s.faceIndex = b), e);
2114
- }
2115
- function Os(n, t = null) {
2116
- t && Array.isArray(t) && (t = new Set(t));
2117
- const i = n.geometry, e = i.index ? i.index.array : null, s = i.attributes.position;
2118
- let r, o, d, a, f = 0;
2119
- const l = n._roots;
2120
- for (let c = 0, u = l.length; c < u; c++)
2121
- r = l[c], o = new Uint32Array(r), d = new Uint16Array(r), a = new Float32Array(r), p(0, f), f += r.byteLength;
2122
- function p(c, u, y = !1) {
2123
- const g = c * 2;
2124
- if (q(g, d)) {
2125
- const x = J(c, o), w = Z(g, d);
2126
- let m = 1 / 0, h = 1 / 0, b = 1 / 0, A = -1 / 0, P = -1 / 0, M = -1 / 0;
2127
- for (let T = x, R = x + w; T < R; T++) {
2128
- const v = 3 * n.resolveTriangleIndex(T);
2129
- for (let B = 0; B < 3; B++) {
2130
- let S = v + B;
2131
- S = e ? e[S] : S;
2132
- const _ = s.getX(S), E = s.getY(S), F = s.getZ(S);
2133
- _ < m && (m = _), _ > A && (A = _), E < h && (h = E), E > P && (P = E), F < b && (b = F), F > M && (M = F);
2134
- }
2135
- }
2136
- return a[c + 0] !== m || a[c + 1] !== h || a[c + 2] !== b || a[c + 3] !== A || a[c + 4] !== P || a[c + 5] !== M ? (a[c + 0] = m, a[c + 1] = h, a[c + 2] = b, a[c + 3] = A, a[c + 4] = P, a[c + 5] = M, !0) : !1;
2137
- } else {
2138
- const x = V(c), w = W(c, o);
2139
- let m = y, h = !1, b = !1;
2140
- if (t) {
2141
- if (!m) {
2142
- const v = x / C + u / X, B = w / C + u / X;
2143
- h = t.has(v), b = t.has(B), m = !h && !b;
2144
- }
2145
- } else
2146
- h = !0, b = !0;
2147
- const A = m || h, P = m || b;
2148
- let M = !1;
2149
- A && (M = p(x, u, m));
2150
- let T = !1;
2151
- P && (T = p(w, u, m));
2152
- const R = M || T;
2153
- if (R)
2154
- for (let v = 0; v < 3; v++) {
2155
- const B = x + v, S = w + v, _ = a[B], E = a[B + 3], F = a[S], D = a[S + 3];
2156
- a[c + v] = _ < F ? _ : F, a[c + v + 3] = E > D ? E : D;
2157
- }
2158
- return R;
2159
- }
2160
- }
2161
- }
2162
- function Vs(n, t, i, e, s, r, o) {
2163
- I.setBuffer(n._roots[t]), Ge(0, n, i, e, s, r, o), I.clearBuffer();
2164
- }
2165
- function Ge(n, t, i, e, s, r, o) {
2166
- const { float32Array: d, uint16Array: a, uint32Array: f } = I, l = n * 2;
2167
- if (q(l, a)) {
2168
- const c = J(n, f), u = Z(l, a);
2169
- zs(t, i, e, c, u, s, r, o);
2170
- } else {
2171
- const c = V(n);
2172
- gt(c, d, e, r, o) && Ge(c, t, i, e, s, r, o);
2173
- const u = W(n, f);
2174
- gt(u, d, e, r, o) && Ge(u, t, i, e, s, r, o);
2175
- }
2176
- }
2177
- const Ws = ["x", "y", "z"];
2178
- function Gs(n, t, i, e, s, r) {
2179
- I.setBuffer(n._roots[t]);
2180
- const o = Xe(0, n, i, e, s, r);
2181
- return I.clearBuffer(), o;
2182
- }
2183
- function Xe(n, t, i, e, s, r) {
2184
- const { float32Array: o, uint16Array: d, uint32Array: a } = I;
2185
- let f = n * 2;
2186
- if (q(f, d)) {
2187
- const p = J(n, a), c = Z(f, d);
2188
- return Us(t, i, e, p, c, s, r);
2189
- } else {
2190
- const p = ii(n, a), c = Ws[p], y = e.direction[c] >= 0;
2191
- let g, x;
2192
- y ? (g = V(n), x = W(n, a)) : (g = W(n, a), x = V(n));
2193
- const m = gt(g, o, e, s, r) ? Xe(g, t, i, e, s, r) : null;
2194
- if (m) {
2195
- const A = m.point[c];
2196
- if (y ? A <= o[x + p] : (
2197
- // min bounding data
2198
- A >= o[x + p + 3]
2199
- ))
2200
- return m;
2201
- }
2202
- const b = gt(x, o, e, s, r) ? Xe(x, t, i, e, s, r) : null;
2203
- return m && b ? m.distance <= b.distance ? m : b : m || b || null;
2204
- }
2205
- }
2206
- const ue = /* @__PURE__ */ new it(), Ut = /* @__PURE__ */ new ot(), Ft = /* @__PURE__ */ new ot(), Wt = /* @__PURE__ */ new H(), Di = /* @__PURE__ */ new Q(), fe = /* @__PURE__ */ new Q();
2207
- function Xs(n, t, i, e) {
2208
- I.setBuffer(n._roots[t]);
2209
- const s = Je(0, n, i, e);
2210
- return I.clearBuffer(), s;
2211
- }
2212
- function Je(n, t, i, e, s = null) {
2213
- const { float32Array: r, uint16Array: o, uint32Array: d } = I;
2214
- let a = n * 2;
2215
- if (s === null && (i.boundingBox || i.computeBoundingBox(), Di.set(i.boundingBox.min, i.boundingBox.max, e), s = Di), q(a, o)) {
2216
- const l = t.geometry, p = l.index, c = l.attributes.position, u = i.index, y = i.attributes.position, g = J(n, d), x = Z(a, o);
2217
- if (Wt.copy(e).invert(), i.boundsTree)
2218
- return k(n, r, fe), fe.matrix.copy(Wt), fe.needsUpdate = !0, i.boundsTree.shapecast({
2219
- intersectsBounds: (m) => fe.intersectsBox(m),
2220
- intersectsTriangle: (m) => {
2221
- m.a.applyMatrix4(e), m.b.applyMatrix4(e), m.c.applyMatrix4(e), m.needsUpdate = !0;
2222
- for (let h = g, b = x + g; h < b; h++)
2223
- if (j(Ft, 3 * t.resolveTriangleIndex(h), p, c), Ft.needsUpdate = !0, m.intersectsTriangle(Ft))
2224
- return !0;
2225
- return !1;
2226
- }
2227
- });
2228
- {
2229
- const w = Me(i);
2230
- for (let m = g, h = x + g; m < h; m++) {
2231
- const b = t.resolveTriangleIndex(m);
2232
- j(Ut, 3 * b, p, c), Ut.a.applyMatrix4(Wt), Ut.b.applyMatrix4(Wt), Ut.c.applyMatrix4(Wt), Ut.needsUpdate = !0;
2233
- for (let A = 0, P = w * 3; A < P; A += 3)
2234
- if (j(Ft, A, u, y), Ft.needsUpdate = !0, Ut.intersectsTriangle(Ft))
2235
- return !0;
2236
- }
2237
- }
2238
- } else {
2239
- const l = V(n), p = W(n, d);
2240
- return k(l, r, ue), !!(s.intersectsBox(ue) && Je(l, t, i, e, s) || (k(p, r, ue), s.intersectsBox(ue) && Je(p, t, i, e, s)));
2241
- }
2242
- }
2243
- const de = /* @__PURE__ */ new H(), Fe = /* @__PURE__ */ new Q(), Gt = /* @__PURE__ */ new Q(), Js = /* @__PURE__ */ new z(), Hs = /* @__PURE__ */ new z(), Zs = /* @__PURE__ */ new z(), Qs = /* @__PURE__ */ new z();
2244
- function Ys(n, t, i, e = {}, s = {}, r = 0, o = 1 / 0) {
2245
- t.boundingBox || t.computeBoundingBox(), Fe.set(t.boundingBox.min, t.boundingBox.max, i), Fe.needsUpdate = !0;
2246
- const d = n.geometry, a = d.attributes.position, f = d.index, l = t.attributes.position, p = t.index, c = et.getPrimitive(), u = et.getPrimitive();
2247
- let y = Js, g = Hs, x = null, w = null;
2248
- s && (x = Zs, w = Qs);
2249
- let m = 1 / 0, h = null, b = null;
2250
- return de.copy(i).invert(), Gt.matrix.copy(de), n.shapecast(
2251
- {
2252
- boundsTraverseOrder: (A) => Fe.distanceToBox(A),
2253
- intersectsBounds: (A, P, M) => M < m && M < o ? (P && (Gt.min.copy(A.min), Gt.max.copy(A.max), Gt.needsUpdate = !0), !0) : !1,
2254
- intersectsRange: (A, P) => {
2255
- if (t.boundsTree) {
2256
- const M = t.boundsTree;
2257
- return M.shapecast({
2258
- boundsTraverseOrder: (T) => Gt.distanceToBox(T),
2259
- intersectsBounds: (T, R, v) => v < m && v < o,
2260
- intersectsRange: (T, R) => {
2261
- for (let v = T, B = T + R; v < B; v++) {
2262
- const S = M.resolveTriangleIndex(v);
2263
- j(u, 3 * S, p, l), u.a.applyMatrix4(i), u.b.applyMatrix4(i), u.c.applyMatrix4(i), u.needsUpdate = !0;
2264
- for (let _ = A, E = A + P; _ < E; _++) {
2265
- const F = n.resolveTriangleIndex(_);
2266
- j(c, 3 * F, f, a), c.needsUpdate = !0;
2267
- const D = c.distanceToTriangle(u, y, x);
2268
- if (D < m && (g.copy(y), w && w.copy(x), m = D, h = _, b = v), D < r)
2269
- return !0;
2270
- }
2271
- }
2272
- }
2273
- });
2274
- } else {
2275
- const M = Me(t);
2276
- for (let T = 0, R = M; T < R; T++) {
2277
- j(u, 3 * T, p, l), u.a.applyMatrix4(i), u.b.applyMatrix4(i), u.c.applyMatrix4(i), u.needsUpdate = !0;
2278
- for (let v = A, B = A + P; v < B; v++) {
2279
- const S = n.resolveTriangleIndex(v);
2280
- j(c, 3 * S, f, a), c.needsUpdate = !0;
2281
- const _ = c.distanceToTriangle(u, y, x);
2282
- if (_ < m && (g.copy(y), w && w.copy(x), m = _, h = v, b = T), _ < r)
2283
- return !0;
2284
- }
2285
- }
2286
- }
2287
- }
2288
- }
2289
- ), et.releasePrimitive(c), et.releasePrimitive(u), m === 1 / 0 ? null : (e.point ? e.point.copy(g) : e.point = g.clone(), e.distance = m, e.faceIndex = h, s && (s.point ? s.point.copy(w) : s.point = w.clone(), s.point.applyMatrix4(de), g.applyMatrix4(de), s.distance = g.sub(s.point).length(), s.faceIndex = b), e);
2290
- }
2291
- function Li(n, t, i) {
2292
- return n === null ? null : (n.point.applyMatrix4(t.matrixWorld), n.distance = n.point.distanceTo(i.ray.origin), n.object = t, n);
2293
- }
2294
- const pe = /* @__PURE__ */ new Q(), me = /* @__PURE__ */ new Bn(), ki = /* @__PURE__ */ new z(), ji = /* @__PURE__ */ new H(), Ni = /* @__PURE__ */ new z(), $e = ["getX", "getY", "getZ"];
2295
- class xe extends bs {
2296
- static serialize(t, i = {}) {
2297
- i = {
2298
- cloneBuffers: !0,
2299
- ...i
2300
- };
2301
- const e = t.geometry, s = t._roots, r = t._indirectBuffer, o = e.getIndex(), d = {
2302
- version: 1,
2303
- roots: null,
2304
- index: null,
2305
- indirectBuffer: null
2306
- };
2307
- return i.cloneBuffers ? (d.roots = s.map((a) => a.slice()), d.index = o ? o.array.slice() : null, d.indirectBuffer = r ? r.slice() : null) : (d.roots = s, d.index = o ? o.array : null, d.indirectBuffer = r), d;
2308
- }
2309
- static deserialize(t, i, e = {}) {
2310
- e = {
2311
- setIndex: !0,
2312
- indirect: !!t.indirectBuffer,
2313
- ...e
2314
- };
2315
- const { index: s, roots: r, indirectBuffer: o } = t;
2316
- t.version || (console.warn(
2317
- "MeshBVH.deserialize: Serialization format has been changed and will be fixed up. It is recommended to regenerate any stored serialized data."
2318
- ), a(r));
2319
- const d = new xe(i, { ...e, [ei]: !0 });
2320
- if (d._roots = r, d._indirectBuffer = o || null, e.setIndex) {
2321
- const f = i.getIndex();
2322
- if (f === null) {
2323
- const l = new Gi(t.index, 1, !1);
2324
- i.setIndex(l);
2325
- } else f.array !== s && (f.array.set(s), f.needsUpdate = !0);
2326
- }
2327
- return d;
2328
- function a(f) {
2329
- for (let l = 0; l < f.length; l++) {
2330
- const p = f[l], c = new Uint32Array(p), u = new Uint16Array(p);
2331
- for (let y = 0, g = p.byteLength / X; y < g; y++) {
2332
- const x = C * y, w = 2 * x;
2333
- q(w, u) || (c[x + 6] = c[x + 6] / C - y);
2334
- }
2335
- }
2336
- }
2337
- }
2338
- get primitiveStride() {
2339
- return 3;
2340
- }
2341
- get resolveTriangleIndex() {
2342
- return this.resolvePrimitiveIndex;
2343
- }
2344
- constructor(t, i = {}) {
2345
- i.maxLeafTris && (console.warn('MeshBVH: "maxLeafTris" option has been deprecated. Use maxLeafSize, instead.'), i = {
2346
- ...i,
2347
- maxLeafSize: i.maxLeafTris
2348
- }), super(t, i);
2349
- }
2350
- // implement abstract methods from BVH base class
2351
- shiftTriangleOffsets(t) {
2352
- return super.shiftPrimitiveOffsets(t);
2353
- }
2354
- // write primitive bounds to the buffer - used only for validateBounds at the moment
2355
- writePrimitiveBounds(t, i, e) {
2356
- const s = this.geometry, r = this._indirectBuffer, o = s.attributes.position, d = s.index ? s.index.array : null, f = (r ? r[t] : t) * 3;
2357
- let l = f + 0, p = f + 1, c = f + 2;
2358
- d && (l = d[l], p = d[p], c = d[c]);
2359
- for (let u = 0; u < 3; u++) {
2360
- const y = o[$e[u]](l), g = o[$e[u]](p), x = o[$e[u]](c);
2361
- let w = y;
2362
- g < w && (w = g), x < w && (w = x);
2363
- let m = y;
2364
- g > m && (m = g), x > m && (m = x), i[e + u] = w, i[e + u + 3] = m;
2365
- }
2366
- return i;
2367
- }
2368
- // precomputes the bounding box for each triangle; required for quickly calculating tree splits.
2369
- // result is an array of size count * 6 where triangle i maps to a
2370
- // [x_center, x_delta, y_center, y_delta, z_center, z_delta] tuple starting at index (i - offset) * 6,
2371
- // representing the center and half-extent in each dimension of triangle i
2372
- computePrimitiveBounds(t, i, e) {
2373
- const s = this.geometry, r = this._indirectBuffer, o = s.attributes.position, d = s.index ? s.index.array : null, a = o.normalized;
2374
- if (t < 0 || i + t - e.offset > e.length / 6)
2375
- throw new Error("MeshBVH: compute triangle bounds range is invalid.");
2376
- const f = o.array, l = o.offset || 0;
2377
- let p = 3;
2378
- o.isInterleavedBufferAttribute && (p = o.data.stride);
2379
- const c = ["getX", "getY", "getZ"], u = e.offset;
2380
- for (let y = t, g = t + i; y < g; y++) {
2381
- const w = (r ? r[y] : y) * 3, m = (y - u) * 6;
2382
- let h = w + 0, b = w + 1, A = w + 2;
2383
- d && (h = d[h], b = d[b], A = d[A]), a || (h = h * p + l, b = b * p + l, A = A * p + l);
2384
- for (let P = 0; P < 3; P++) {
2385
- let M, T, R;
2386
- a ? (M = o[c[P]](h), T = o[c[P]](b), R = o[c[P]](A)) : (M = f[h + P], T = f[b + P], R = f[A + P]);
2387
- let v = M;
2388
- T < v && (v = T), R < v && (v = R);
2389
- let B = M;
2390
- T > B && (B = T), R > B && (B = R);
2391
- const S = (B - v) / 2, _ = P * 2;
2392
- e[m + _ + 0] = v + S, e[m + _ + 1] = S + (Math.abs(v) + S) * ge;
2393
- }
2394
- }
2395
- return e;
2396
- }
2397
- raycastObject3D(t, i, e = []) {
2398
- const { material: s } = t;
2399
- if (s === void 0)
2400
- return;
2401
- ji.copy(t.matrixWorld).invert(), me.copy(i.ray).applyMatrix4(ji), Ni.setFromMatrixScale(t.matrixWorld), ki.copy(me.direction).multiply(Ni);
2402
- const r = ki.length(), o = i.near / r, d = i.far / r;
2403
- if (i.firstHitOnly === !0) {
2404
- let a = this.raycastFirst(me, s, o, d);
2405
- a = Li(a, t, i), a && e.push(a);
2406
- } else {
2407
- const a = this.raycast(me, s, o, d);
2408
- for (let f = 0, l = a.length; f < l; f++) {
2409
- const p = Li(a[f], t, i);
2410
- p && e.push(p);
2411
- }
2412
- }
2413
- return e;
2414
- }
2415
- refit(t = null) {
2416
- return (this.indirect ? Os : Es)(this, t);
2417
- }
2418
- /* Core Cast Functions */
2419
- raycast(t, i = pi, e = 0, s = 1 / 0) {
2420
- const r = this._roots, o = [], d = this.indirect ? Vs : $s;
2421
- for (let a = 0, f = r.length; a < f; a++)
2422
- d(this, a, i, t, o, e, s);
2423
- return o;
2424
- }
2425
- raycastFirst(t, i = pi, e = 0, s = 1 / 0) {
2426
- const r = this._roots;
2427
- let o = null;
2428
- const d = this.indirect ? Gs : Ds;
2429
- for (let a = 0, f = r.length; a < f; a++) {
2430
- const l = d(this, a, i, t, e, s);
2431
- l != null && (o == null || l.distance < o.distance) && (o = l);
2432
- }
2433
- return o;
2434
- }
2435
- intersectsGeometry(t, i) {
2436
- let e = !1;
2437
- const s = this._roots, r = this.indirect ? Xs : Ls;
2438
- for (let o = 0, d = s.length; o < d && (e = r(this, o, t, i), !e); o++)
2439
- ;
2440
- return e;
2441
- }
2442
- shapecast(t) {
2443
- const i = et.getPrimitive(), e = super.shapecast(
2444
- {
2445
- ...t,
2446
- intersectsPrimitive: t.intersectsTriangle,
2447
- scratchPrimitive: i,
2448
- // TODO: is the performance significant enough for the added complexity here?
2449
- // can we just use one function?
2450
- iterate: this.indirect ? Fs : _s
2451
- }
2452
- );
2453
- return et.releasePrimitive(i), e;
2454
- }
2455
- bvhcast(t, i, e) {
2456
- let {
2457
- intersectsRanges: s,
2458
- intersectsTriangles: r
2459
- } = e;
2460
- const o = et.getPrimitive(), d = this.geometry.index, a = this.geometry.attributes.position, f = this.indirect ? (y) => {
2461
- const g = this.resolveTriangleIndex(y);
2462
- j(o, g * 3, d, a);
2463
- } : (y) => {
2464
- j(o, y * 3, d, a);
2465
- }, l = et.getPrimitive(), p = t.geometry.index, c = t.geometry.attributes.position, u = t.indirect ? (y) => {
2466
- const g = t.resolveTriangleIndex(y);
2467
- j(l, g * 3, p, c);
2468
- } : (y) => {
2469
- j(l, y * 3, p, c);
2470
- };
2471
- if (r) {
2472
- if (!(t instanceof xe))
2473
- throw new Error('MeshBVH: "intersectsTriangles" callback can only be used with another MeshBVH.');
2474
- const y = (g, x, w, m, h, b, A, P) => {
2475
- for (let M = w, T = w + m; M < T; M++) {
2476
- u(M), l.a.applyMatrix4(i), l.b.applyMatrix4(i), l.c.applyMatrix4(i), l.needsUpdate = !0;
2477
- for (let R = g, v = g + x; R < v; R++)
2478
- if (f(R), o.needsUpdate = !0, r(o, l, R, M, h, b, A, P))
2479
- return !0;
2480
- }
2481
- return !1;
2482
- };
2483
- if (s) {
2484
- const g = s;
2485
- s = function(x, w, m, h, b, A, P, M) {
2486
- return g(x, w, m, h, b, A, P, M) ? !0 : y(x, w, m, h, b, A, P, M);
2487
- };
2488
- } else
2489
- s = y;
2490
- }
2491
- return super.bvhcast(t, i, { intersectsRanges: s });
2492
- }
2493
- /* Derived Cast Functions */
2494
- intersectsBox(t, i) {
2495
- return pe.set(t.min, t.max, i), pe.needsUpdate = !0, this.shapecast(
2496
- {
2497
- intersectsBounds: (e) => pe.intersectsBox(e),
2498
- intersectsTriangle: (e) => pe.intersectsTriangle(e)
2499
- }
2500
- );
2501
- }
2502
- intersectsSphere(t) {
2503
- return this.shapecast(
2504
- {
2505
- intersectsBounds: (i) => t.intersectsBox(i),
2506
- intersectsTriangle: (i) => i.intersectsSphere(t)
2507
- }
2508
- );
2509
- }
2510
- closestPointToGeometry(t, i, e = {}, s = {}, r = 0, o = 1 / 0) {
2511
- return (this.indirect ? Ys : qs)(
2512
- this,
2513
- t,
2514
- i,
2515
- e,
2516
- s,
2517
- r,
2518
- o
2519
- );
2520
- }
2521
- closestPointToPoint(t, i = {}, e = 0, s = 1 / 0) {
2522
- return Ps(
2523
- this,
2524
- t,
2525
- i,
2526
- e,
2527
- s
2528
- );
2529
- }
2530
- }
2531
- const It = {
2532
- Mesh: N.prototype.raycast,
2533
- Line: Ke.prototype.raycast,
2534
- LineSegments: Ye.prototype.raycast,
2535
- LineLoop: Hi.prototype.raycast,
2536
- Points: Zi.prototype.raycast,
2537
- BatchedMesh: En.prototype.raycast
2538
- }, G = /* @__PURE__ */ new N(), he = [];
2539
- function Ks(n, t) {
2540
- if (this.isBatchedMesh)
2541
- to.call(this, n, t);
2542
- else {
2543
- const { geometry: i } = this;
2544
- if (i.boundsTree)
2545
- i.boundsTree.raycastObject3D(this, n, t);
2546
- else {
2547
- let e;
2548
- if (this instanceof N)
2549
- e = It.Mesh;
2550
- else if (this instanceof Ye)
2551
- e = It.LineSegments;
2552
- else if (this instanceof Hi)
2553
- e = It.LineLoop;
2554
- else if (this instanceof Ke)
2555
- e = It.Line;
2556
- else if (this instanceof Zi)
2557
- e = It.Points;
2558
- else
2559
- throw new Error("BVH: Fallback raycast function not found.");
2560
- e.call(this, n, t);
2561
- }
2562
- }
2563
- }
2564
- function to(n, t) {
2565
- if (this.boundsTrees) {
2566
- const i = this.boundsTrees, e = this._drawInfo || this._instanceInfo, s = this._drawRanges || this._geometryInfo, r = this.matrixWorld;
2567
- G.material = this.material, G.geometry = this.geometry;
2568
- const o = G.geometry.boundsTree, d = G.geometry.drawRange;
2569
- G.geometry.boundingSphere === null && (G.geometry.boundingSphere = new _n());
2570
- for (let a = 0, f = e.length; a < f; a++) {
2571
- if (!this.getVisibleAt(a))
2572
- continue;
2573
- const l = e[a].geometryIndex;
2574
- if (G.geometry.boundsTree = i[l], this.getMatrixAt(a, G.matrixWorld).premultiply(r), !G.geometry.boundsTree) {
2575
- this.getBoundingBoxAt(l, G.geometry.boundingBox), this.getBoundingSphereAt(l, G.geometry.boundingSphere);
2576
- const p = s[l];
2577
- G.geometry.setDrawRange(p.start, p.count);
2578
- }
2579
- G.raycast(n, he);
2580
- for (let p = 0, c = he.length; p < c; p++) {
2581
- const u = he[p];
2582
- u.object = this, u.batchId = a, t.push(u);
2583
- }
2584
- he.length = 0;
2585
- }
2586
- G.geometry.boundsTree = o, G.geometry.drawRange = d, G.material = null, G.geometry = null;
2587
- } else
2588
- It.BatchedMesh.call(this, n, t);
2589
- }
2590
- function Ie(n) {
2591
- return n.type === "continuous" || !n.limits ? 0 : Math.max(n.limits.lower, Math.min(n.limits.upper, 0));
2592
- }
2593
- class eo {
2594
- constructor(t, i) {
2595
- $(this, "root");
2596
- $(this, "visual");
2597
- $(this, "joints", /* @__PURE__ */ new Map());
2598
- $(this, "values", /* @__PURE__ */ new Map());
2599
- $(this, "specs", /* @__PURE__ */ new Map());
2600
- $(this, "pickMeshes", []);
2601
- $(this, "disposed", !1);
2602
- $(this, "initialRoots", /* @__PURE__ */ new Map());
2603
- this.visual = i, this.root = i.root;
2604
- const e = new Set(t.links.map((u) => u.id)), s = t.joints.filter((u) => e.has(u.parent) && e.has(u.child) && u.parent !== u.child), r = new Set(s.map((u) => u.child)), o = t.links.filter((u) => !r.has(u.id)), d = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Set();
2605
- for (const u of s)
2606
- a.has(u.child) || (a.add(u.child), d.set(u.parent, [...d.get(u.parent) ?? [], u]));
2607
- const f = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Set(), p = (u) => {
2608
- if (l.has(u)) return;
2609
- l.add(u), f.add(u);
2610
- const y = i.bindings.get(u);
2611
- if (!y) {
2612
- l.delete(u);
2613
- return;
2614
- }
2615
- for (const g of d.get(u) ?? []) {
2616
- if (l.has(g.child) || f.has(g.child)) continue;
2617
- const x = i.bindings.get(g.child);
2618
- if (!x) continue;
2619
- y.add(x);
2620
- const w = {
2621
- joint: g,
2622
- child: x,
2623
- originPosition: new z().fromArray(g.origin.position),
2624
- originQuaternion: Qt(g.origin),
2625
- axis: new z().fromArray(g.axis).normalize()
2626
- };
2627
- this.joints.set(g.id, w), this.specs.set(g.id, g), this.values.set(g.id, Ie(g)), this.apply(w, this.values.get(g.id)), p(g.child);
2628
- }
2629
- l.delete(u);
2630
- }, c = o.length ? o : t.links;
2631
- for (const u of c) {
2632
- if (f.has(u.id)) continue;
2633
- const y = i.bindings.get(u.id);
2634
- y && (this.root.add(y), p(u.id));
2635
- }
2636
- for (const u of t.links) {
2637
- if (f.has(u.id)) continue;
2638
- const y = i.bindings.get(u.id);
2639
- y && (this.root.add(y), p(u.id));
2640
- }
2641
- if (o.length === 1) {
2642
- const u = Ki(t, o[0].id, d), y = i.bindings.get(o[0].id);
2643
- y && u > 0 && (y.position.z += u);
2644
- }
2645
- for (const u of i.bindings.values()) u.parent === this.root && this.initialRoots.set(u, { position: u.position.clone(), quaternion: u.quaternion.clone() });
2646
- this.applyAll(), this.preparePicking(), this.root.updateMatrixWorld(!0);
2647
- }
2648
- pickRoots() {
2649
- return this.pickMeshes;
2650
- }
2651
- value(t) {
2652
- return this.values.get(t);
2653
- }
2654
- valuesSnapshot() {
2655
- return Object.freeze(Object.fromEntries(this.values));
2656
- }
2657
- setJointValue(t, i) {
2658
- if (this.disposed) throw Xt("URDF kinematic binding has been disposed.", "E_URDF_INACTIVE");
2659
- const e = this.joints.get(t);
2660
- if (!e) throw Xt(`Unknown URDF joint: ${t}`, "E_URDF_JOINT_UNKNOWN");
2661
- if (!Number.isFinite(i)) throw Xt(`Joint value must be finite: ${t}`, "E_URDF_JOINT_VALUE");
2662
- let s = i;
2663
- if (e.joint.type !== "continuous" && e.joint.limits && (s = Math.max(e.joint.limits.lower, Math.min(e.joint.limits.upper, i))), ["revolute", "continuous", "prismatic"].includes(e.joint.type) || (s = 0), e.joint.mimic) throw Xt(`Mimic joint is controlled by ${e.joint.mimic.joint}: ${t}`, "E_URDF_MIMIC_READONLY");
2664
- const r = Ae([...this.specs.values()]).get(t);
2665
- return r && (s = Math.max(r.lower, Math.min(r.upper, s))), this.values.set(t, s), this.applyAll(), this.root.updateMatrixWorld(!0), s;
2666
- }
2667
- resetJoint(t) {
2668
- const i = this.joints.get(t);
2669
- if (!i) throw Xt(`Unknown URDF joint: ${t}`, "E_URDF_JOINT_UNKNOWN");
2670
- this.setJointValue(t, Ie(i.joint));
2671
- }
2672
- syncPhysicsPose(t, i) {
2673
- for (const [e, s] of Object.entries(t)) {
2674
- const r = this.joints.get(e);
2675
- r && Number.isFinite(s) && (this.values.set(e, s), this.apply(r, s));
2676
- }
2677
- for (const e of this.initialRoots.keys()) {
2678
- const s = i.get(e.name);
2679
- s && (e.position.copy(s.position), e.quaternion.copy(s.quaternion));
2680
- }
2681
- this.root.updateMatrixWorld(!0);
2682
- }
2683
- resetPose() {
2684
- for (const [t, i] of this.initialRoots)
2685
- t.position.copy(i.position), t.quaternion.copy(i.quaternion);
2686
- for (const t of this.joints.values()) {
2687
- const i = Ie(t.joint);
2688
- t.joint.mimic || this.values.set(t.joint.id, i);
2689
- }
2690
- this.applyAll(), this.root.updateMatrixWorld(!0);
2691
- }
2692
- objectForEntity(t) {
2693
- var i;
2694
- return t.startsWith("link:") ? this.visual.bindings.get(t.slice(5)) ?? null : t.startsWith("joint:") ? ((i = this.joints.get(t.slice(6))) == null ? void 0 : i.child) ?? null : t.startsWith("robot:") ? this.root : null;
2695
- }
2696
- jointWorldFrame(t) {
2697
- const i = this.joints.get(t);
2698
- if (!i) return null;
2699
- const e = i.child.parent;
2700
- if (!e) return null;
2701
- e.updateMatrixWorld(!0);
2702
- const s = i.originPosition.clone().applyMatrix4(e.matrixWorld), r = e.getWorldQuaternion(new K()).multiply(i.originQuaternion);
2703
- return { position: s, quaternion: r, axis: i.axis.clone().applyQuaternion(r).normalize() };
2704
- }
2705
- dispose() {
2706
- if (!this.disposed) {
2707
- this.disposed = !0, this.joints.clear(), this.values.clear(), this.specs.clear();
2708
- for (const t of this.pickMeshes) {
2709
- const i = t.geometry;
2710
- delete i.boundsTree;
2711
- }
2712
- this.pickMeshes.length = 0, ft(this.root);
2713
- }
2714
- }
2715
- applyAll() {
2716
- const t = ti([...this.specs.values()], Object.fromEntries(this.values));
2717
- for (const [i, e] of this.joints)
2718
- this.values.set(i, t[i]), this.apply(e, t[i]);
2719
- }
2720
- preparePicking() {
2721
- const t = /* @__PURE__ */ new Set();
2722
- this.root.traverse((i) => {
2723
- if (!(i instanceof N) || !i.geometry) return;
2724
- const e = i.geometry;
2725
- !e.boundsTree && !t.has(e) && (e.boundsTree = new xe(e, { maxLeafSize: 20, indirect: !0, verbose: !1 }), t.add(e)), i.raycast = Ks, this.pickMeshes.push(i);
2726
- });
2727
- }
2728
- apply(t, i) {
2729
- t.child.position.copy(t.originPosition), t.child.quaternion.copy(t.originQuaternion), t.joint.type === "revolute" || t.joint.type === "continuous" ? t.child.quaternion.multiply(new K().setFromAxisAngle(t.axis, i)) : t.joint.type === "prismatic" && t.child.position.addScaledVector(t.axis, i);
2730
- }
2731
- }
2732
- function Xt(n, t) {
2733
- return Object.assign(new Error(n), { code: t });
2734
- }
2735
- function io(n, t, i = n.source.uri) {
2736
- var m;
2737
- const e = ro(t, i), s = new Set(n.links.map((h) => h.id)), r = n.joints.filter((h) => s.has(h.parent) && s.has(h.child) && h.parent !== h.child), o = /* @__PURE__ */ new Map();
2738
- for (const h of r) o.has(h.child) || o.set(h.child, h);
2739
- const d = n.links.filter((h) => !o.has(h.id)), a = /* @__PURE__ */ new Map();
2740
- for (const h of o.values()) a.set(h.parent, [...a.get(h.parent) ?? [], h]);
2741
- const f = [], l = /* @__PURE__ */ new Set(), p = (h) => {
2742
- if (!l.has(h)) {
2743
- l.add(h);
2744
- for (const b of a.get(h) ?? []) p(b.child);
2745
- }
2746
- };
2747
- for (const h of d)
2748
- f.push(h), p(h.id);
2749
- for (const h of n.links) l.has(h.id) || (f.push(h), p(h.id));
2750
- const c = new Set(f.map((h) => h.id)), u = `robot:${n.name}`, y = /* @__PURE__ */ new Map();
2751
- for (const h of n.joints) y.set(h.parent, [...y.get(h.parent) ?? [], `joint:${h.id}`]);
2752
- const g = [], x = Object.freeze({
2753
- id: u,
2754
- kind: "robot",
2755
- name: n.name,
2756
- childIds: f.map((h) => `link:${h.id}`),
2757
- rootLinkIds: f.map((h) => h.id),
2758
- source: e.robot
2759
- });
2760
- g.push(x);
2761
- for (const h of n.links) g.push(no(h, n, e.links.get(h.id), y, c, o));
2762
- const w = Ae(n.joints);
2763
- for (const h of n.joints) g.push(so({ ...h, controlLimits: w.get(h.id) }, e.joints.get(h.id), ((m = o.get(h.child)) == null ? void 0 : m.id) === h.id));
2764
- return Object.freeze({
2765
- robotName: n.name,
2766
- sourcePath: i,
2767
- sourceText: t,
2768
- nodes: Object.freeze(g),
2769
- rootEntityId: u,
2770
- issues: Object.freeze(oo(n, e))
2771
- });
2772
- }
2773
- function no(n, t, i, e, s, r) {
2774
- var d, a, f, l;
2775
- const o = r.get(n.id);
2776
- return Object.freeze({
2777
- id: `link:${n.id}`,
2778
- kind: "link",
2779
- name: n.name,
2780
- parentId: o ? `joint:${o.id}` : `robot:${t.name}`,
2781
- childIds: Object.freeze(e.get(n.id) ?? []),
2782
- source: i,
2783
- mass: (d = n.inertial) == null ? void 0 : d.mass,
2784
- centerOfMass: (a = n.inertial) == null ? void 0 : a.origin.position,
2785
- inertiaTensor: (f = n.inertial) == null ? void 0 : f.tensor,
2786
- inertiaMass: (l = n.inertial) == null ? void 0 : l.mass,
2787
- inertialRotationRpy: n.inertial ? Object.freeze([...n.inertial.origin.rotationRpy]) : void 0,
2788
- visualCount: n.visuals.length,
2789
- collisionCount: n.collisions.length
2790
- });
2791
- }
2792
- function so(n, t, i) {
2793
- return Object.freeze({
2794
- id: `joint:${n.id}`,
2795
- kind: "joint",
2796
- name: n.id,
2797
- parentId: `link:${n.parent}`,
2798
- childIds: Object.freeze([`link:${n.child}`]),
2799
- source: t,
2800
- jointType: n.type,
2801
- parentLink: n.parent,
2802
- childLink: n.child,
2803
- axis: Object.freeze([...n.axis]),
2804
- originPosition: Object.freeze([...n.origin.position]),
2805
- originRotationRpy: Object.freeze([...n.origin.rotationRpy]),
2806
- limits: n.limits,
2807
- effort: n.effort,
2808
- velocity: n.velocity,
2809
- controlLimits: n.controlLimits,
2810
- movable: ["revolute", "continuous", "prismatic"].includes(n.type),
2811
- ...n.mimic ? { mimic: Object.freeze({ ...n.mimic }) } : {}
2812
- });
2813
- }
2814
- function oo(n, t) {
2815
- const i = [], e = (c, u, y, g, x, w) => {
2816
- i.push(Object.freeze({ id: `${c}:${g}`, ruleId: c, severity: "error", title: u, message: y, entityId: g, source: x, values: w ? Object.freeze(w) : void 0 }));
2817
- }, s = new Set(n.links.map((c) => c.id));
2818
- for (const c of n.joints)
2819
- s.has(c.parent) || e("joint.parent.missing", "父连杆不存在", `${c.id} 引用了不存在的 parent link ${c.parent}。`, `joint:${c.id}`, t.joints.get(c.id), { parent: c.parent }), s.has(c.child) || e("joint.child.missing", "子连杆不存在", `${c.id} 引用了不存在的 child link ${c.child}。`, `joint:${c.id}`, t.joints.get(c.id), { child: c.child });
2820
- const r = n.links.filter((c) => !n.joints.some((u) => s.has(u.parent) && u.child === c.id));
2821
- r.length !== 1 && e("robot.root.count", "根连杆数量无效", `URDF 必须恰好有一个根连杆,当前为 ${r.length} 个。`, `robot:${n.name}`, t.robot, { roots: r.map((c) => c.id) });
2822
- const o = /* @__PURE__ */ new Map();
2823
- for (const c of n.joints) s.has(c.parent) && s.has(c.child) && o.set(c.parent, [...o.get(c.parent) ?? [], c.child]);
2824
- const d = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set();
2825
- let f = null;
2826
- const l = (c, u) => {
2827
- if (!f) {
2828
- if (a.has(c)) {
2829
- const y = u.indexOf(c);
2830
- f = [...u.slice(Math.max(0, y)), c];
2831
- return;
2832
- }
2833
- if (!d.has(c)) {
2834
- a.add(c);
2835
- for (const y of o.get(c) ?? []) l(y, [...u, c]);
2836
- a.delete(c), d.add(c);
2837
- }
2838
- }
2839
- };
2840
- for (const c of n.links) l(c.id, []);
2841
- f && e("robot.graph.cycle", "运动学结构存在环", `运动学链形成循环:${f.join(" → ")}。`, `robot:${n.name}`, t.robot, { cycle: f });
2842
- const p = /* @__PURE__ */ new Map();
2843
- for (const c of n.joints) {
2844
- if (p.set(c.child, [...p.get(c.child) ?? [], c.id]), c.parent === c.child && e("joint.self-reference", "关节父子连杆相同", `${c.id} 的 parent 和 child 都是 ${c.parent}。`, `joint:${c.id}`, t.joints.get(c.id)), ["revolute", "continuous", "prismatic"].includes(c.type)) {
2845
- const u = c.axis.reduce((y, g) => y + g * g, 0);
2846
- (!Number.isFinite(u) || u <= Number.EPSILON) && e("joint.axis.zero", "关节轴无效", `${c.id} 的 axis 是零向量。`, `joint:${c.id}`, t.joints.get(c.id), { axis: c.axis });
2847
- }
2848
- c.type !== "continuous" && c.limits && c.limits.lower > c.limits.upper && e("joint.limit.order", "关节限位上下界无效", `${c.id} 的 lower ${c.limits.lower} 大于 upper ${c.limits.upper}。`, `joint:${c.id}`, t.joints.get(c.id), { ...c.limits });
2849
- }
2850
- for (const [c, u] of p) u.length > 1 && e("link.multiple-parents", "连杆存在多个父关节", `${c} 同时被 ${u.join("、")} 作为 child。`, `link:${c}`, t.links.get(c), { joints: u });
2851
- for (const c of n.links) {
2852
- const u = c.inertial;
2853
- u && ((!(u.mass > 0) || !Number.isFinite(u.mass)) && e("inertial.mass.invalid", "质量无效", `${c.id} 的质量必须是大于零的有限数。`, `link:${c.id}`, t.links.get(c.id), { mass: u.mass }), Yi(u.tensor) || e("inertial.tensor.invalid", "惯性张量无效", `${c.id} 的惯性张量不是正定的物理惯性张量。`, `link:${c.id}`, t.links.get(c.id), { tensor: u.tensor }));
2854
- }
2855
- return i;
2856
- }
2857
- function ro(n, t) {
2858
- const i = { robot: void 0, links: /* @__PURE__ */ new Map(), joints: /* @__PURE__ */ new Map() };
2859
- i.robot = Ci(n, "robot", void 0, t);
2860
- for (const e of ["link", "joint"]) {
2861
- const s = new RegExp(`<${e}\\b[^>]*\\bname\\s*=\\s*(["'])(.*?)\\1[^>]*>`, "gi");
2862
- for (const r of n.matchAll(s)) {
2863
- const o = r[2], d = Ci(n, e, o, t, r.index);
2864
- d && i[e === "link" ? "links" : "joints"].set(o, d);
2865
- }
2866
- }
2867
- return i;
2868
- }
2869
- function Ci(n, t, i, e, s) {
2870
- const r = s ?? n.search(new RegExp(`<${t}\\b`, "i"));
2871
- if (r < 0) return;
2872
- const o = n.indexOf(">", r);
2873
- if (o < 0) return;
2874
- const a = n.slice(r, o + 1).trimEnd().endsWith("/>") ? o + 1 : n.indexOf(`</${t}>`, o + 1), f = a < 0 ? o + 1 : a + t.length + 3, l = qi(n, r), p = qi(n, f);
2875
- return Object.freeze({ file: e, startOffset: r, endOffset: f, startLine: l.line, startColumn: l.column, endLine: p.line, endColumn: p.column });
2876
- }
2877
- function qi(n, t) {
2878
- const e = n.slice(0, t).split(/\r?\n/);
2879
- return { line: e.length, column: e.at(-1).length + 1 };
2880
- }
2881
- class ao {
2882
- constructor(t, i, e, s, r) {
2883
- $(this, "kind", "urdf");
2884
- $(this, "overlayRoot", new lt());
2885
- $(this, "listeners", /* @__PURE__ */ new Set());
2886
- $(this, "revision", 0);
2887
- $(this, "selectedEntityId");
2888
- $(this, "overlays", Object.freeze({ collision: !1, jointAxis: !1, centerOfMass: !1, inertia: !1 }));
2889
- $(this, "mode");
2890
- $(this, "dynamicsAvailable");
2891
- $(this, "dynamicsMessage");
2892
- $(this, "highlightedMaterials", /* @__PURE__ */ new Map());
2893
- $(this, "selectionMaterials", /* @__PURE__ */ new Map());
2894
- $(this, "guideBindings", []);
2895
- $(this, "guideMatrix", new H());
2896
- $(this, "overlayInverse", new H());
2897
- $(this, "guideSizes", /* @__PURE__ */ new Map());
2898
- $(this, "labelAnchors", /* @__PURE__ */ new Map());
2899
- $(this, "modelSize", 1);
2900
- $(this, "disposed", !1);
2901
- var o, d;
2902
- this.asset = t, this.document = i, this.binding = e, this.options = s, this.selectedEntityId = ((o = i.nodes.find((a) => a.kind === "joint")) == null ? void 0 : o.id) ?? ((d = i.nodes.find((a) => a.kind === "link")) == null ? void 0 : d.id) ?? i.rootEntityId, this.mode = "kinematic", this.dynamicsAvailable = r, this.overlayRoot.name = "URDF review overlays", this.measureGuideSizes(), this.rebuildOverlay();
2903
- }
2904
- getSnapshot() {
2905
- return Object.freeze({
2906
- revision: this.revision,
2907
- kind: "urdf",
2908
- robotName: this.document.robotName,
2909
- sourcePath: this.document.sourcePath,
2910
- sourceText: this.document.sourceText,
2911
- nodes: this.document.nodes,
2912
- rootEntityId: this.document.rootEntityId,
2913
- selectedEntityId: this.selectedEntityId,
2914
- jointValues: this.binding.valuesSnapshot(),
2915
- overlays: this.overlays,
2916
- issues: this.document.issues,
2917
- mode: this.mode,
2918
- dynamicsAvailable: this.dynamicsAvailable,
2919
- ...this.dynamicsMessage ? { dynamicsMessage: this.dynamicsMessage } : {}
2920
- });
2921
- }
2922
- subscribe(t) {
2923
- return this.listeners.add(t), () => this.listeners.delete(t);
2924
- }
2925
- select(t) {
2926
- if (!(this.mode === "dynamics-running" || this.mode === "dynamics-initializing")) {
2927
- if (t !== null && !this.document.nodes.some((i) => i.id === t)) throw co(`Unknown URDF entity: ${t}`, "E_URDF_ENTITY_UNKNOWN");
2928
- this.selectedEntityId !== t && (this.selectedEntityId = t, this.rebuildOverlay(), this.publish());
2929
- }
2930
- }
2931
- setJointValue(t, i) {
2932
- this.mode === "dynamics-running" || this.mode === "dynamics-initializing" || (this.options.enterKinematicPreview(), this.binding.setJointValue(t, i), this.mode = "kinematic", this.rebuildGuides(), this.publish());
2933
- }
2934
- resetJoint(t) {
2935
- this.mode === "dynamics-running" || this.mode === "dynamics-initializing" || (this.options.enterKinematicPreview(), this.binding.resetJoint(t), this.mode = "kinematic", this.rebuildOverlay(), this.publish());
2936
- }
2937
- resetPose() {
2938
- this.options.enterKinematicPreview(), this.binding.resetPose(), this.mode = "kinematic", this.rebuildOverlay(), this.publish();
2939
- }
2940
- setOverlay(t, i) {
2941
- this.overlays[t] !== i && (this.overlays = Object.freeze({ ...this.overlays, [t]: i }), t === "collision" && this.options.setCollisionVisible(i), this.rebuildOverlay(), this.publish());
2942
- }
2943
- enterKinematicPreview() {
2944
- this.options.enterKinematicPreview(), this.mode = "kinematic", this.overlayRoot.visible = !0, this.publish();
2945
- }
2946
- setMode(t, i) {
2947
- t === "dynamics-unavailable" ? (this.mode = "kinematic", this.dynamicsAvailable = !1) : this.mode = t, this.overlayRoot.visible = this.mode !== "dynamics-initializing", (t === "dynamics-running" || t === "dynamics-initializing") && (this.selectedEntityId = null), this.dynamicsMessage = i, this.rebuildOverlay(), this.publish();
2948
- }
2949
- jointWorldFrame(t) {
2950
- return this.binding.jointWorldFrame(t);
2951
- }
2952
- pickRoots() {
2953
- var t, i;
2954
- return ((i = (t = this.options).pickRoots) == null ? void 0 : i.call(t)) ?? this.binding.pickRoots();
2955
- }
2956
- selectionObject() {
2957
- return this.selectedEntityId ? this.binding.objectForEntity(this.selectedEntityId) : null;
2958
- }
2959
- dispose() {
2960
- if (!this.disposed) {
2961
- this.disposed = !0, this.listeners.clear(), this.restoreSelectionMaterials();
2962
- for (const t of this.selectionMaterials.values()) t.dispose();
2963
- this.selectionMaterials.clear(), ft(this.overlayRoot), this.overlayRoot.removeFromParent();
2964
- }
2965
- }
2966
- publish() {
2967
- if (this.disposed) return;
2968
- this.revision += 1;
2969
- const t = this.getSnapshot();
2970
- for (const i of this.listeners) i(t);
2971
- this.options.requestRender();
2972
- }
2973
- rebuildOverlay() {
2974
- if (this.overlayRoot.visible = this.mode !== "dynamics-initializing", this.restoreSelectionMaterials(), this.clearGuides(), this.buildMassGuides(), this.buildJointGuides(), this.syncGuides(), !this.selectedEntityId) return;
2975
- const t = this.document.nodes.find((e) => e.id === this.selectedEntityId);
2976
- if (!t) return;
2977
- const i = this.binding.objectForEntity(t.id);
2978
- i && t.kind !== "robot" && this.applySelectionMaterials(i);
2979
- }
2980
- rebuildGuides() {
2981
- this.overlayRoot.visible = this.mode !== "dynamics-initializing", this.clearGuides(), this.buildMassGuides(), this.buildJointGuides(), this.syncGuides(), this.selectedEntityId && this.document.nodes.find((t) => t.id === this.selectedEntityId);
2982
- }
2983
- /** Update transforms only: physics ticks must not rebuild geometry or publish UI state. */
2984
- syncGuides() {
2985
- if (!(this.disposed || !this.overlayRoot.visible || !this.guideBindings.length)) {
2986
- this.overlayRoot.updateWorldMatrix(!0, !1), this.overlayInverse.copy(this.overlayRoot.matrixWorld).invert();
2987
- for (const { guide: t, source: i, local: e } of this.guideBindings)
2988
- i.updateWorldMatrix(!0, !1), this.guideMatrix.multiplyMatrices(this.overlayInverse, i.matrixWorld).multiply(e), this.guideMatrix.decompose(t.position, t.quaternion, t.scale), t.updateMatrixWorld(!0);
2989
- }
2990
- }
2991
- linkObject(t) {
2992
- var i, e;
2993
- return ((e = (i = this.options).linkObject) == null ? void 0 : e.call(i, t)) ?? this.binding.objectForEntity(`link:${t}`);
2994
- }
2995
- trackGuide(t, i) {
2996
- i.updateWorldMatrix(!0, !1), this.overlayRoot.updateWorldMatrix(!0, !1), t.updateWorldMatrix(!0, !1);
2997
- const e = i.matrixWorld.clone().invert().multiply(t.matrixWorld);
2998
- this.guideBindings.push({ guide: t, source: i, local: e });
2999
- }
3000
- clearGuides() {
3001
- this.guideBindings.length = 0, ft(this.overlayRoot), this.overlayRoot.clear();
3002
- }
3003
- /** Cache own-link bounds once; exclude child links and never traverse meshes per physics tick. */
3004
- measureGuideSizes() {
3005
- const t = new it();
3006
- for (const e of this.document.nodes) {
3007
- if (e.kind !== "link") continue;
3008
- const s = this.binding.objectForEntity(e.id);
3009
- if (!s) continue;
3010
- s.updateWorldMatrix(!0, !0);
3011
- const r = new it(), o = (a) => {
3012
- if (!(a !== s && typeof a.userData.urdfEntityId == "string")) {
3013
- a instanceof N && (a.geometry.boundingBox || a.geometry.computeBoundingBox(), a.geometry.boundingBox && r.union(a.geometry.boundingBox.clone().applyMatrix4(a.matrixWorld)));
3014
- for (const f of a.children) o(f);
3015
- }
3016
- };
3017
- if (o(s), r.isEmpty()) continue;
3018
- this.labelAnchors.set(e.name, r.getCenter(new z()).applyMatrix4(s.matrixWorld.clone().invert()));
3019
- const d = r.getSize(new z()).length();
3020
- Number.isFinite(d) && d > 0 && (this.guideSizes.set(e.name, d), t.union(r));
3021
- }
3022
- const i = t.getSize(new z()).length();
3023
- Number.isFinite(i) && i > 0 && (this.modelSize = i);
3024
- }
3025
- linkWorldPosition(t, i) {
3026
- const e = this.linkObject(t);
3027
- return !e || this.disposed || this.mode === "dynamics-initializing" ? !1 : (e.updateWorldMatrix(!0, !1), i.copy(this.labelAnchors.get(t) ?? new z()).applyMatrix4(e.matrixWorld), !0);
3028
- }
3029
- guideSize(t) {
3030
- const i = this.guideSizes.get(t) ?? this.modelSize * 0.1;
3031
- return Math.max(this.modelSize * 3e-3, Math.min(i, this.modelSize * 0.35));
3032
- }
3033
- buildJointGuides() {
3034
- const t = this.document.nodes.find((e) => e.id === this.selectedEntityId), i = (t == null ? void 0 : t.kind) === "joint" ? t : (t == null ? void 0 : t.kind) === "link" ? this.document.nodes.find((e) => e.kind === "joint" && e.childLink === t.name) : void 0;
3035
- for (const e of this.document.nodes) {
3036
- if (e.kind !== "joint" || !this.overlays.jointAxis && e.id !== (i == null ? void 0 : i.id)) continue;
3037
- const s = this.linkObject(e.parentLink), r = s ? (() => {
3038
- s.updateWorldMatrix(!0, !1);
3039
- const f = s.getWorldQuaternion(new K()).multiply(new K().setFromEuler(new Zt(...e.originRotationRpy, "ZYX")));
3040
- return { position: new z().fromArray(e.originPosition).applyMatrix4(s.matrixWorld), quaternion: f, axis: new z().fromArray(e.axis).applyQuaternion(f).normalize() };
3041
- })() : this.binding.jointWorldFrame(e.name);
3042
- if (!r) continue;
3043
- const o = new lt();
3044
- o.name = `Joint guide:${e.name}`;
3045
- const d = this.guideSize(e.childLink), a = new zn(0.18 * d);
3046
- if (a.name = `Joint frame:${e.name}`, a.position.copy(r.position), a.quaternion.copy(r.quaternion), o.add(a), ["revolute", "continuous", "prismatic"].includes(e.jointType)) {
3047
- const f = r.axis.clone().normalize(), l = new mi(f, r.position, 0.28 * d, 5625523, 0.045 * d, 0.022 * d);
3048
- if (l.name = `Joint axis:${e.name}`, o.add(l), e.jointType !== "prismatic") {
3049
- const c = (Math.abs(f.z) < 0.9 ? new z(0, 0, 1) : new z(0, 1, 0)).cross(f).normalize(), u = f.clone().cross(c).normalize(), y = 0.09 * d, g = Math.PI * 1.65, x = r.position.clone().addScaledVector(f, 0.085 * d), w = Array.from({ length: 33 }, (A, P) => {
3050
- const M = g * P / 32;
3051
- return x.clone().addScaledVector(c, y * Math.cos(M)).addScaledVector(u, y * Math.sin(M));
3052
- }), m = new Ke(new Un().setFromPoints(w), new hi({ color: 16761963 }));
3053
- m.name = `Positive rotation:${e.name}`;
3054
- const h = c.clone().multiplyScalar(-Math.sin(g)).addScaledVector(u, Math.cos(g)), b = new mi(h, w[32], 0.03 * d, 16761963, 0.03 * d, 0.022 * d);
3055
- b.name = `Positive rotation arrow:${e.name}`, o.add(m, b);
3056
- }
3057
- }
3058
- o.traverse((f) => {
3059
- f.renderOrder = 1001;
3060
- const l = f.material;
3061
- for (const p of Array.isArray(l) ? l : l ? [l] : [])
3062
- p.depthTest = !1, p.depthWrite = !1, p.transparent = !0;
3063
- }), this.overlayRoot.add(o), s && this.trackGuide(o, s);
3064
- }
3065
- }
3066
- buildMassGuides() {
3067
- if (!this.overlays.centerOfMass && !this.overlays.inertia) return;
3068
- const t = this.document.nodes.find((e) => e.id === this.selectedEntityId), i = (t == null ? void 0 : t.kind) === "link" ? t.name : (t == null ? void 0 : t.kind) === "joint" ? t.childLink : void 0;
3069
- for (const e of this.asset.links) {
3070
- const s = e.inertial, r = this.linkObject(e.id);
3071
- if (!s || !r) continue;
3072
- r.updateWorldMatrix(!0, !1);
3073
- const o = new z().fromArray(s.origin.position).applyMatrix4(r.matrixWorld), d = e.id === i;
3074
- if (this.overlays.centerOfMass) {
3075
- const u = new Qe(0.025 * this.guideSize(e.id), 32, 16), y = u.toNonIndexed();
3076
- u.dispose();
3077
- const g = y.getAttribute("position"), x = new Float32Array(g.count * 3);
3078
- for (let m = 0; m < g.count; m += 3) {
3079
- let h = 0, b = 0, A = 0;
3080
- for (let M = 0; M < 3; M++)
3081
- h += g.getX(m + M), b += g.getY(m + M), A += g.getZ(m + M);
3082
- const P = h * b * A >= 0 ? 1 : 0.015;
3083
- for (let M = 0; M < 9; M++) x[m * 3 + M] = P;
3084
- }
3085
- y.setAttribute("color", new Fn(x, 3));
3086
- const w = new N(y, new yi({ color: d ? 16739210 : 16777215, vertexColors: !0, depthTest: !1, depthWrite: !1 }));
3087
- w.quaternion.copy(r.getWorldQuaternion(new K())), w.position.copy(o), w.renderOrder = 1002, w.name = `Center of mass:${e.id}`, this.overlayRoot.add(w), this.trackGuide(w, r);
3088
- }
3089
- const a = this.overlays.inertia ? qn(s.tensor, s.mass) : null;
3090
- if (!a) continue;
3091
- const f = new Ze(1, 1, 1), l = new Ye(new $n(f), new hi({ color: d ? 16736135 : 2211300, transparent: !0, opacity: d ? 0.98 : 0.82, depthTest: !0, depthWrite: !1 })), p = new N(f, new yi({ color: d ? 16739210 : 3262699, transparent: !0, opacity: d ? 0.28 : 0.16, depthTest: !0, depthWrite: !1 }));
3092
- p.name = `Inertia fill:${e.id}`;
3093
- const c = s.origin.rotationQuaternion ? new K().fromArray(s.origin.rotationQuaternion) : new K().setFromEuler(new Zt(...s.origin.rotationRpy, "ZYX"));
3094
- l.position.copy(o), l.scale.fromArray(a.size), l.quaternion.copy(r.getWorldQuaternion(new K())).multiply(c).multiply(a.rotation), l.renderOrder = 1001, l.name = `Inertia:${e.id}`, p.position.copy(l.position), p.quaternion.copy(l.quaternion), p.scale.copy(l.scale), p.renderOrder = 1e3, this.overlayRoot.add(p, l), this.trackGuide(p, r), this.trackGuide(l, r);
3095
- }
3096
- }
3097
- applySelectionMaterials(t) {
3098
- const i = (e, s) => {
3099
- if (!(!s && typeof e.userData.urdfEntityId == "string")) {
3100
- e instanceof N && e.material && (this.highlightedMaterials.set(e, e.material), e.material = Array.isArray(e.material) ? e.material.map((r) => this.createSelectionMaterial(r)) : this.createSelectionMaterial(e.material));
3101
- for (const r of e.children) i(r, !1);
3102
- }
3103
- };
3104
- i(t, !0);
3105
- }
3106
- createSelectionMaterial(t) {
3107
- var r;
3108
- const i = this.selectionMaterials.get(t);
3109
- if (i) return i;
3110
- const e = t.clone(), s = e;
3111
- return (r = s.color) == null || r.set(9425919), s.emissive ? (s.emissive.set(2793430), s.emissiveIntensity = Math.max(s.emissiveIntensity ?? 0, 0.34)) : e instanceof He && (e.emissive.set(2793430), e.emissiveIntensity = 0.34), e.transparent = t.transparent, e.opacity = t.opacity, e.depthWrite = t.depthWrite, e.needsUpdate = !0, this.selectionMaterials.set(t, e), e;
3112
- }
3113
- restoreSelectionMaterials() {
3114
- for (const [t, i] of this.highlightedMaterials) t.material = i;
3115
- this.highlightedMaterials.clear();
3116
- }
3117
- }
3118
- function co(n, t) {
3119
- return Object.assign(new Error(n), { code: t });
3120
- }
3121
- async function Ao(n, t, i) {
3122
- var _, E, F, D;
3123
- const e = n.entryPath ?? n.filename ?? "asset.urdf";
3124
- (_ = t.reportProgress) == null || _.call(t, "fetch", "正在读取模型描述与依赖资源...");
3125
- let s = await wn(n, i);
3126
- const r = t.format === "xacro";
3127
- if (r) {
3128
- (E = t.reportProgress) == null || E.call(t, "xacro", "正在展开 Xacro 宏与依赖文件...");
3129
- const { expandXacro: U } = await import("./xacro-BpICdIYd.js");
3130
- s = await U(s, e, i, t.options.xacro, t.signal);
3131
- }
3132
- if (t.signal.aborted) throw Jt();
3133
- (F = t.reportProgress) == null || F.call(t, "parse", "正在解析 URDF 结构与连杆...");
3134
- const o = On(s, e, i);
3135
- (D = t.reportProgress) == null || D.call(t, "visual", "正在构建运动学预览...");
3136
- const d = await xi(o), a = new eo(o, d), f = await Xn(o, d), l = o.diagnostics.errors.find((U) => U.code === "E_URDF_INERTIA"), p = o.links.some((U) => U.inertial), c = p && !l, u = new xn(
3137
- a.root,
3138
- t.options.simulationTheme,
3139
- void 0,
3140
- "simulation",
3141
- c ? "physics" : "visual"
3142
- );
3143
- f && (f.root.visible = !1, u.attachAuxiliary(f.root));
3144
- let y = null, g = null, x = null;
3145
- const w = o.diagnostics.warnings[0], m = w ? ai(w, "visual-material") : null;
3146
- let h = l ? {
3147
- code: l.code,
3148
- message: l.message,
3149
- stage: "physics-validation",
3150
- notice: ci(l.code, l.source)
3151
- } : m, b = l ? { status: "unavailable", issue: h } : p ? { status: "available" } : { status: "not-authored" }, A = !1, P = null, M = !1, T = !1;
3152
- const R = () => {
3153
- var L;
3154
- const U = A || !a.root.visible;
3155
- A = !1, u.setRuntime(void 0), g && (g.root.visible = !1), u.replaceModel(a.root, !1), a.root.visible = !0, f && f.sync(), f != null && f.root && (f.root.visible = v.getSnapshot().overlays.collision), x && (x.root.visible = !1), U && ((L = t.reportSessionState) == null || L.call(t)), t.requestRender();
3156
- }, v = new ao(o, io(o, s, r ? `${e} (expanded URDF)` : e), a, {
3157
- requestRender() {
3158
- a.root.visible && (f == null || f.sync()), t.requestRender();
3159
- },
3160
- setCollisionVisible(U) {
3161
- f && (f.root.visible = U && a.root.visible), x && (x.root.visible = U && (g == null ? void 0 : g.root.visible) === !0);
3162
- },
3163
- enterKinematicPreview: R,
3164
- linkObject: (U) => g != null && g.root.visible ? g.objectForLink(U) : a.objectForEntity(`link:${U}`),
3165
- pickRoots: () => A || b.status === "initializing" ? [] : a.pickRoots()
3166
- }, c);
3167
- l && v.setMode(
3168
- "dynamics-unavailable",
3169
- ci(l.code, l.source).summary
3170
- ), u.attachAuxiliary(v.overlayRoot);
3171
- const B = () => !y || !g ? !1 : (y.setJointValues(v.getSnapshot().jointValues), u.replaceModel(g.root, !1), u.setRuntime(y), g.sync(), x == null || x.sync(), a.root.visible = !1, g.root.visible = !0, f && (f.root.visible = !1), x && (x.root.visible = v.getSnapshot().overlays.collision), !0), S = async () => {
3172
- var Mt, vt, Pt, kt;
3173
- if (y || M || !c) return;
3174
- v.setMode("dynamics-initializing"), b = { status: "initializing" }, (Mt = t.reportSessionState) == null || Mt.call(t);
3175
- let U = null, L = null, Y = null;
3176
- try {
3177
- const wt = await Gn(o);
3178
- if (t.signal.aborted || M) throw Jt();
3179
- const nt = await import("./mujoco-runtime-ZuEJSHnD.js");
3180
- if (U = new nt.MujocoRuntime(), await U.initialize(wt, t.options.mujocoBaseUrl), U.setWorldRotation(ui(u.getUpAxis())), t.signal.aborted || M || (L = new li(U, await xi(o)), t.signal.aborted || M)) throw Jt();
3181
- const Yt = await tn(o);
3182
- if (Y = Yt ? new li(U, Yt) : null, t.signal.aborted || M) throw Jt();
3183
- y = U, U = null, g = L, L = null, g.root.visible = !1, x = Y, Y = null, x && (x.root.visible = !1, u.attachAuxiliary(x.root)), b = { status: "ready" }, h = m, T && (B(), A = !0, v.setMode("dynamics-running")), (vt = t.reportSessionState) == null || vt.call(t), t.requestRender();
3184
- } catch (wt) {
3185
- if (Y == null || Y.dispose(), L == null || L.dispose(), U == null || U.dispose(), M || t.signal.aborted || wt instanceof Error && wt.name === "AbortError") return;
3186
- const nt = bn(wt, t.signal, "urdf-physics");
3187
- u.setRuntime(void 0), u.setGroundPlacement("visual"), y == null || y.dispose(), y = null, g == null || g.dispose(), g = null, x && (u.detachAuxiliary(x.root), x.dispose(), x = null), R(), h = ai(nt, "physics-fallback"), b = { status: "unavailable", issue: h }, v.setMode("dynamics-unavailable", ((Pt = h.notice) == null ? void 0 : Pt.summary) ?? h.message), (kt = t.reportSessionState) == null || kt.call(t), t.reportError(wt, { fatal: !1, stage: "physics-start", code: nt.code });
3188
- }
3189
- };
3190
- return {
3191
- asset: o,
3192
- stage: u,
3193
- inspection: v,
3194
- autoPlay: !1,
3195
- root: u.root,
3196
- get capabilities() {
3197
- const U = b.status === "initializing", L = b.status === "unavailable" || b.status === "not-authored";
3198
- return { canPlay: !U && !L, canPause: y !== null, canReset: !0, hasPhysics: y !== null, canToggleGround: !0, canToggleCollision: f !== void 0 || x !== null, canReorientUpAxis: !0 };
3199
- },
3200
- get physics() {
3201
- return b;
3202
- },
3203
- get warning() {
3204
- return h;
3205
- },
3206
- isPlaying: () => A,
3207
- play() {
3208
- var U;
3209
- if (!(M || b.status === "unavailable" || b.status === "not-authored")) {
3210
- if (T = !0, v.select(null), !y) {
3211
- P ?? (P = S().finally(() => {
3212
- P = null;
3213
- }));
3214
- return;
3215
- }
3216
- B() && (A = !0, v.setMode("dynamics-running"), (U = t.reportSessionState) == null || U.call(t));
3217
- }
3218
- },
3219
- pause() {
3220
- var U;
3221
- T = !1, A = !1, u.endGrab(), y && (a.syncPhysicsPose(y.getJointValues(o.joints.map((L) => L.id)), lo(g.bodyPoses(), ui(u.getUpAxis()))), R(), v.setMode("dynamics-paused")), (U = t.reportSessionState) == null || U.call(t), t.requestRender();
3222
- },
3223
- reset() {
3224
- var U;
3225
- T = !1, u.endGrab(), y && (y.reset(), g == null || g.sync(), x == null || x.sync()), v.resetPose(), u.resetGround(), (U = t.reportSessionState) == null || U.call(t), t.requestRender();
3226
- },
3227
- step(U) {
3228
- if (!y || !A) return !1;
3229
- const L = y.step(U);
3230
- return L && (g == null || g.sync(), x == null || x.sync(), v.syncGuides(), u.updateForceLine()), L;
3231
- },
3232
- beginGrab(U, L, Y) {
3233
- return !!(A && y && u.beginGrab(U, L, Y));
3234
- },
3235
- moveGrab(U, L, Y) {
3236
- A && y && u.moveGrab(U, L, Y);
3237
- },
3238
- endGrab() {
3239
- u.endGrab();
3240
- },
3241
- setCollisionVisible(U) {
3242
- f && (f.root.visible = U && a.root.visible), x && (x.root.visible = U && (g == null ? void 0 : g.root.visible) === !0);
3243
- },
3244
- dispose() {
3245
- M || (M = !0, A = !1, v.dispose(), f && (u.detachAuxiliary(f.root), f.dispose()), x && (u.detachAuxiliary(x.root), x.dispose()), u.setRuntime(void 0), g == null || g.dispose(), a.dispose(), y == null || y.dispose(), u.dispose());
3246
- }
3247
- };
3248
- }
3249
- function Jt() {
3250
- return Object.assign(new Error("Simulation load aborted."), { name: "AbortError", code: "E_ABORTED" });
3251
- }
3252
- function lo(n, t) {
3253
- const i = t.clone().invert(), e = /* @__PURE__ */ new Map();
3254
- for (const [s, r] of n)
3255
- e.set(s, {
3256
- position: r.position.clone().applyQuaternion(i),
3257
- quaternion: i.clone().multiply(r.quaternion)
3258
- });
3259
- return e;
3260
- }
3261
- export {
3262
- Ao as loadUrdfSimulation
3263
- };