@nebula-spatial/viewer 0.4.4 → 0.4.5

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