three-usd-robot 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,381 +1,6 @@
1
+ import { Quat, multiply, makeTranslation, makeRotationFromQuat, identity4, invert, makeEuler, DEG2RAD, makeRotationZ, makeRotationY, makeRotationX, fromUsdMatrix, makeScale, AssetPath, RAD2DEG, UsdMatrix } from './chunk-IYKPVUZ2.js';
1
2
  import { unzipSync } from 'fflate';
2
3
 
3
- // src/parser/ast.ts
4
- var Quat = class _Quat {
5
- constructor(real, imaginary) {
6
- this.real = real;
7
- this.imaginary = imaginary;
8
- }
9
- real;
10
- imaginary;
11
- /** Components in Three.js order `[x, y, z, w]`. */
12
- toXYZW() {
13
- return [this.imaginary[0], this.imaginary[1], this.imaginary[2], this.real];
14
- }
15
- static identity() {
16
- return new _Quat(1, [0, 0, 0]);
17
- }
18
- };
19
- var UsdMatrix = class _UsdMatrix {
20
- constructor(values, dim) {
21
- this.values = values;
22
- this.dim = dim;
23
- }
24
- values;
25
- dim;
26
- static identity4() {
27
- return new _UsdMatrix([
28
- 1,
29
- 0,
30
- 0,
31
- 0,
32
- 0,
33
- 1,
34
- 0,
35
- 0,
36
- 0,
37
- 0,
38
- 1,
39
- 0,
40
- 0,
41
- 0,
42
- 0,
43
- 1
44
- ], 4);
45
- }
46
- };
47
- var AssetPath = class {
48
- constructor(path) {
49
- this.path = path;
50
- }
51
- path;
52
- };
53
-
54
- // src/kinematics/transforms.ts
55
- var DEG2RAD = Math.PI / 180;
56
- var RAD2DEG = 180 / Math.PI;
57
- function identity4() {
58
- return [
59
- 1,
60
- 0,
61
- 0,
62
- 0,
63
- 0,
64
- 1,
65
- 0,
66
- 0,
67
- 0,
68
- 0,
69
- 1,
70
- 0,
71
- 0,
72
- 0,
73
- 0,
74
- 1
75
- ];
76
- }
77
- function multiply(a, b) {
78
- const a11 = a[0], a21 = a[1], a31 = a[2], a41 = a[3];
79
- const a12 = a[4], a22 = a[5], a32 = a[6], a42 = a[7];
80
- const a13 = a[8], a23 = a[9], a33 = a[10], a43 = a[11];
81
- const a14 = a[12], a24 = a[13], a34 = a[14], a44 = a[15];
82
- const b11 = b[0], b21 = b[1], b31 = b[2], b41 = b[3];
83
- const b12 = b[4], b22 = b[5], b32 = b[6], b42 = b[7];
84
- const b13 = b[8], b23 = b[9], b33 = b[10], b43 = b[11];
85
- const b14 = b[12], b24 = b[13], b34 = b[14], b44 = b[15];
86
- return [
87
- a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41,
88
- a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41,
89
- a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41,
90
- a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41,
91
- a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42,
92
- a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42,
93
- a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42,
94
- a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42,
95
- a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43,
96
- a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43,
97
- a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43,
98
- a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43,
99
- a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44,
100
- a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44,
101
- a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44,
102
- a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44
103
- ];
104
- }
105
- function multiplyAll(matrices) {
106
- let m = identity4();
107
- for (const next of matrices) m = multiply(m, next);
108
- return m;
109
- }
110
- function invert(m) {
111
- const n11 = m[0], n21 = m[1], n31 = m[2], n41 = m[3];
112
- const n12 = m[4], n22 = m[5], n32 = m[6], n42 = m[7];
113
- const n13 = m[8], n23 = m[9], n33 = m[10], n43 = m[11];
114
- const n14 = m[12], n24 = m[13], n34 = m[14], n44 = m[15];
115
- const t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44;
116
- const t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44;
117
- const t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44;
118
- const t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
119
- const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
120
- if (det === 0) throw new Error("cannot invert a singular matrix");
121
- const idet = 1 / det;
122
- return [
123
- t11 * idet,
124
- (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * idet,
125
- (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * idet,
126
- (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * idet,
127
- t12 * idet,
128
- (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * idet,
129
- (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * idet,
130
- (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * idet,
131
- t13 * idet,
132
- (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * idet,
133
- (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * idet,
134
- (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * idet,
135
- t14 * idet,
136
- (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * idet,
137
- (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * idet,
138
- (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * idet
139
- ];
140
- }
141
- function makeTranslation([x, y, z]) {
142
- return [
143
- 1,
144
- 0,
145
- 0,
146
- 0,
147
- 0,
148
- 1,
149
- 0,
150
- 0,
151
- 0,
152
- 0,
153
- 1,
154
- 0,
155
- x,
156
- y,
157
- z,
158
- 1
159
- ];
160
- }
161
- function makeScale([x, y, z]) {
162
- return [
163
- x,
164
- 0,
165
- 0,
166
- 0,
167
- 0,
168
- y,
169
- 0,
170
- 0,
171
- 0,
172
- 0,
173
- z,
174
- 0,
175
- 0,
176
- 0,
177
- 0,
178
- 1
179
- ];
180
- }
181
- function makeRotationX(rad) {
182
- const c = Math.cos(rad);
183
- const s = Math.sin(rad);
184
- return [
185
- 1,
186
- 0,
187
- 0,
188
- 0,
189
- 0,
190
- c,
191
- s,
192
- 0,
193
- 0,
194
- -s,
195
- c,
196
- 0,
197
- 0,
198
- 0,
199
- 0,
200
- 1
201
- ];
202
- }
203
- function makeRotationY(rad) {
204
- const c = Math.cos(rad);
205
- const s = Math.sin(rad);
206
- return [
207
- c,
208
- 0,
209
- -s,
210
- 0,
211
- 0,
212
- 1,
213
- 0,
214
- 0,
215
- s,
216
- 0,
217
- c,
218
- 0,
219
- 0,
220
- 0,
221
- 0,
222
- 1
223
- ];
224
- }
225
- function makeRotationZ(rad) {
226
- const c = Math.cos(rad);
227
- const s = Math.sin(rad);
228
- return [
229
- c,
230
- s,
231
- 0,
232
- 0,
233
- -s,
234
- c,
235
- 0,
236
- 0,
237
- 0,
238
- 0,
239
- 1,
240
- 0,
241
- 0,
242
- 0,
243
- 0,
244
- 1
245
- ];
246
- }
247
- function makeRotationFromQuat(q) {
248
- let x = q.imaginary[0];
249
- let y = q.imaginary[1];
250
- let z = q.imaginary[2];
251
- let w = q.real;
252
- const len = Math.hypot(x, y, z, w);
253
- if (len > 0 && Math.abs(len - 1) > 1e-9) {
254
- x /= len;
255
- y /= len;
256
- z /= len;
257
- w /= len;
258
- }
259
- const x2 = x + x, y2 = y + y, z2 = z + z;
260
- const xx = x * x2, xy = x * y2, xz = x * z2;
261
- const yy = y * y2, yz = y * z2, zz = z * z2;
262
- const wx = w * x2, wy = w * y2, wz = w * z2;
263
- return [
264
- 1 - (yy + zz),
265
- xy + wz,
266
- xz - wy,
267
- 0,
268
- xy - wz,
269
- 1 - (xx + zz),
270
- yz + wx,
271
- 0,
272
- xz + wy,
273
- yz - wx,
274
- 1 - (xx + yy),
275
- 0,
276
- 0,
277
- 0,
278
- 0,
279
- 1
280
- ];
281
- }
282
- function makeEuler(angles, order) {
283
- const perAxis = {
284
- X: makeRotationX(angles[0]),
285
- Y: makeRotationY(angles[1]),
286
- Z: makeRotationZ(angles[2])
287
- };
288
- let m = identity4();
289
- for (let i = order.length - 1; i >= 0; i--) {
290
- const axis = perAxis[order[i]];
291
- if (!axis)
292
- throw new Error(
293
- `invalid euler axis ${JSON.stringify(order[i])} in order ${JSON.stringify(order)}`
294
- );
295
- m = multiply(m, axis);
296
- }
297
- return m;
298
- }
299
- function fromUsdMatrix(m) {
300
- if (m.dim !== 4 || m.values.length !== 16) {
301
- throw new Error(`expected a 4x4 matrix but got dim=${m.dim}, length=${m.values.length}`);
302
- }
303
- return [...m.values];
304
- }
305
- function getTranslation(m) {
306
- return [m[12], m[13], m[14]];
307
- }
308
- function toUsdMatrix(m) {
309
- if (m.length !== 16) {
310
- throw new Error(`expected 16 matrix elements but got ${m.length}`);
311
- }
312
- return new UsdMatrix([...m], 4);
313
- }
314
- function decomposeRigid(m) {
315
- const position = [m[12], m[13], m[14]];
316
- const b0 = [m[0], m[1], m[2]];
317
- const b1 = [m[4], m[5], m[6]];
318
- const b2 = [m[8], m[9], m[10]];
319
- const EPS = 1e-6;
320
- const rigid = Math.abs(norm(b0) - 1) < EPS && Math.abs(norm(b1) - 1) < EPS && Math.abs(norm(b2) - 1) < EPS && Math.abs(dot(b0, b1)) < EPS && Math.abs(dot(b1, b2)) < EPS && Math.abs(dot(b0, b2)) < EPS && dot(cross(b0, b1), b2) > 0;
321
- const c0 = normalizeVec(b0);
322
- const c1 = normalizeVec(subVec(b1, scaleVec(c0, dot(c0, b1))));
323
- const c2 = cross(c0, c1);
324
- const r11 = c0[0], r12 = c1[0], r13 = c2[0];
325
- const r21 = c0[1], r22 = c1[1], r23 = c2[1];
326
- const r31 = c0[2], r32 = c1[2], r33 = c2[2];
327
- const trace = r11 + r22 + r33;
328
- let w;
329
- let x;
330
- let y;
331
- let z;
332
- if (trace > 0) {
333
- const s = 0.5 / Math.sqrt(trace + 1);
334
- w = 0.25 / s;
335
- x = (r32 - r23) * s;
336
- y = (r13 - r31) * s;
337
- z = (r21 - r12) * s;
338
- } else if (r11 > r22 && r11 > r33) {
339
- const s = 2 * Math.sqrt(1 + r11 - r22 - r33);
340
- w = (r32 - r23) / s;
341
- x = 0.25 * s;
342
- y = (r12 + r21) / s;
343
- z = (r13 + r31) / s;
344
- } else if (r22 > r33) {
345
- const s = 2 * Math.sqrt(1 + r22 - r11 - r33);
346
- w = (r13 - r31) / s;
347
- x = (r12 + r21) / s;
348
- y = 0.25 * s;
349
- z = (r23 + r32) / s;
350
- } else {
351
- const s = 2 * Math.sqrt(1 + r33 - r11 - r22);
352
- w = (r21 - r12) / s;
353
- x = (r13 + r31) / s;
354
- y = (r23 + r32) / s;
355
- z = 0.25 * s;
356
- }
357
- return { position, orientation: new Quat(w, [x, y, z]), rigid };
358
- }
359
- function dot(a, b) {
360
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
361
- }
362
- function norm(v) {
363
- return Math.hypot(v[0], v[1], v[2]);
364
- }
365
- function subVec(a, b) {
366
- return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
367
- }
368
- function scaleVec(v, s) {
369
- return [v[0] * s, v[1] * s, v[2] * s];
370
- }
371
- function normalizeVec(v) {
372
- const n = norm(v);
373
- return n > 0 ? scaleVec(v, 1 / n) : [1, 0, 0];
374
- }
375
- function cross(a, b) {
376
- return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
377
- }
378
-
379
4
  // src/schemas/usdGeom.ts
380
5
  function isXform(prim) {
381
6
  return prim.GetTypeName() === "Xform";
@@ -386,6 +11,19 @@ function isScope(prim) {
386
11
  function isMesh(prim) {
387
12
  return prim.GetTypeName() === "Mesh";
388
13
  }
14
+ var SOLID_GPRIM_TYPES = /* @__PURE__ */ new Set([
15
+ "Cube",
16
+ "Sphere",
17
+ "Cylinder",
18
+ "Capsule",
19
+ "Cone"
20
+ ]);
21
+ function isSolidGprim(prim) {
22
+ return SOLID_GPRIM_TYPES.has(prim.GetTypeName());
23
+ }
24
+ function isRenderableGprim(prim) {
25
+ return isMesh(prim) || isSolidGprim(prim);
26
+ }
389
27
  function getPurpose(prim) {
390
28
  const v = prim.GetAttribute("purpose").Get();
391
29
  return typeof v === "string" ? v : "default";
@@ -425,6 +63,13 @@ function gatherMeshDescendants(prim) {
425
63
  }
426
64
  return paths;
427
65
  }
66
+ function gatherGprimDescendants(prim) {
67
+ const paths = [];
68
+ for (const d of iterDescendants(prim)) {
69
+ if (isRenderableGprim(d)) paths.push(d.GetPath());
70
+ }
71
+ return paths;
72
+ }
428
73
 
429
74
  // src/schemas/usdPhysics.ts
430
75
  var JOINT_TYPE_BY_SCHEMA = {
@@ -795,33 +440,6 @@ function firstNumber(shader, names) {
795
440
  return void 0;
796
441
  }
797
442
 
798
- // src/kinematics/sampling.ts
799
- function interpolate(channel, t) {
800
- const { times, values } = channel;
801
- const n = times.length;
802
- if (n === 0) return 0;
803
- if (t <= times[0]) return values[0];
804
- if (t >= times[n - 1]) return values[n - 1];
805
- let lo = 0;
806
- let hi = n - 1;
807
- while (lo < hi) {
808
- const mid = lo + hi >> 1;
809
- if (times[mid] <= t) lo = mid + 1;
810
- else hi = mid;
811
- }
812
- const i = lo - 1;
813
- const t0 = times[i];
814
- const t1 = times[i + 1];
815
- const span = t1 - t0;
816
- const f = span > 0 ? (t - t0) / span : 0;
817
- return values[i] + (values[i + 1] - values[i]) * f;
818
- }
819
- function channelFromSamples(samples) {
820
- const times = [...samples.keys()].sort((a, b) => a - b);
821
- const values = times.map((t) => samples.get(t) ?? 0);
822
- return { times, values };
823
- }
824
-
825
443
  // src/robot/buildKinematicTree.ts
826
444
  var WORLD = "";
827
445
  function buildKinematicTree(robot, options = {}) {
@@ -1008,7 +626,7 @@ function buildLink(path, prim) {
1008
626
  if (!prim) return { name, primPath: path, visualPrims: [] };
1009
627
  const visualPrims = [];
1010
628
  const collisionPrims = [];
1011
- for (const meshPath of gatherMeshDescendants(prim)) {
629
+ for (const meshPath of gatherGprimDescendants(prim)) {
1012
630
  const mp = prim.GetStage().GetPrimAtPath(meshPath);
1013
631
  if (!mp) continue;
1014
632
  const nonVisual = isNonVisualPurpose(mp);
@@ -2413,6 +2031,17 @@ var Stage = class _Stage {
2413
2031
  }
2414
2032
  };
2415
2033
 
2034
+ // src/usd/bytes.ts
2035
+ async function toBytes(data) {
2036
+ if (data instanceof Uint8Array) return data;
2037
+ if (ArrayBuffer.isView(data))
2038
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
2039
+ if (typeof data.arrayBuffer === "function") {
2040
+ return new Uint8Array(await data.arrayBuffer());
2041
+ }
2042
+ return new Uint8Array(data);
2043
+ }
2044
+
2416
2045
  // src/usd/crate/lz4.ts
2417
2046
  var MAX_CHUNK_INPUT = 2113929216;
2418
2047
  function lz4DecompressBlock(src, dst) {
@@ -3234,10 +2863,10 @@ function asNumber2(crate, rep) {
3234
2863
  }
3235
2864
  function splitProperty(path) {
3236
2865
  const slash = path.lastIndexOf("/");
3237
- const dot2 = path.indexOf(".", slash < 0 ? 0 : slash);
3238
- if (dot2 === -1) return null;
2866
+ const dot = path.indexOf(".", slash < 0 ? 0 : slash);
2867
+ if (dot === -1) return null;
3239
2868
  if (path.includes("[")) return null;
3240
- return { primPath: path.slice(0, dot2), propName: path.slice(dot2 + 1) };
2869
+ return { primPath: path.slice(0, dot), propName: path.slice(dot + 1) };
3241
2870
  }
3242
2871
  function parentOf(path) {
3243
2872
  const i = path.lastIndexOf("/");
@@ -3545,6 +3174,12 @@ function stripKeys(meta, keys) {
3545
3174
  return out;
3546
3175
  }
3547
3176
  var USD_ENTRY = /\.(usda|usdc|usd)$/i;
3177
+ function isZip(bytes) {
3178
+ if (bytes.length < 4 || bytes[0] !== 80 || bytes[1] !== 75) return false;
3179
+ const a = bytes[2];
3180
+ const b = bytes[3];
3181
+ return a === 3 && b === 4 || a === 5 && b === 6 || a === 7 && b === 8;
3182
+ }
3548
3183
  function openUsdz(bytes) {
3549
3184
  const entries = unzipSync(bytes);
3550
3185
  const names = Object.keys(entries);
@@ -3574,6 +3209,6 @@ function openUsdz(bytes) {
3574
3209
  return { rootEntry, resolver };
3575
3210
  }
3576
3211
 
3577
- export { ARTICULATION_ROOT_API, AssetPath, Attribute, COLLISION_API, CrateReader, DEFAULT_METERS_PER_UNIT, DEG2RAD, DefaultAssetResolver, Layer, MASS_API, MESH_COLLISION_API, PHYSICS_MATERIAL_API, ParseError, Prim, Quat, RAD2DEG, RIGID_BODY_API, Relationship, Stage, TokenizeError, UsdMatrix, buildKinematicTree, channelFromSamples, composeFile, composeLayer, computeLocalTransform, computeWorldTransform, crateToUsdaFile, createMemoryResolver, decomposeRigid, driveKindFor, extractRobotDescription, fromUsdMatrix, gatherMeshDescendants, getJointAxis, getJointBodies, getJointDrive, getJointLimits, getJointLocalFrame, getJointStatePosition, getJointType, getMassProperties, getMaterialSubsets, getTranslation, hasArticulationRootAPI, hasCollisionAPI, hasRigidBodyAPI, identity4, interpolate, invert, isMesh, isNonVisualPurpose, isScope, isXform, iterDescendants, joinPosix, jointValueFromSI, jointValueToSI, makeEuler, makeRotationFromQuat, makeRotationX, makeRotationY, makeRotationZ, makeScale, makeTranslation, multiply, multiplyAll, normalizeJointLimits, openUsdz, parseOpType, parseUsda, refineJointType, resolveBoundMaterial, serializeUsda, toUsdMatrix, tokenize };
3578
- //# sourceMappingURL=chunk-4GPCBXUS.js.map
3579
- //# sourceMappingURL=chunk-4GPCBXUS.js.map
3212
+ export { ARTICULATION_ROOT_API, Attribute, COLLISION_API, CrateReader, DEFAULT_METERS_PER_UNIT, DefaultAssetResolver, Layer, MASS_API, MESH_COLLISION_API, PHYSICS_MATERIAL_API, ParseError, Prim, RIGID_BODY_API, Relationship, Stage, TokenizeError, buildKinematicTree, composeFile, composeLayer, computeLocalTransform, computeWorldTransform, crateToUsdaFile, createMemoryResolver, driveKindFor, extractRobotDescription, gatherGprimDescendants, gatherMeshDescendants, getJointAxis, getJointBodies, getJointDrive, getJointLimits, getJointLocalFrame, getJointStatePosition, getJointType, getMassProperties, getMaterialSubsets, hasArticulationRootAPI, hasCollisionAPI, hasRigidBodyAPI, isMesh, isNonVisualPurpose, isRenderableGprim, isScope, isSolidGprim, isXform, isZip, iterDescendants, joinPosix, jointValueFromSI, jointValueToSI, normalizeJointLimits, openUsdz, parseOpType, parseUsda, refineJointType, resolveBoundMaterial, serializeUsda, toBytes, tokenize };
3213
+ //# sourceMappingURL=chunk-PHHMWMCA.js.map
3214
+ //# sourceMappingURL=chunk-PHHMWMCA.js.map