@vib3code/sdk 2.0.3-canary.8d2fdcd → 2.0.3-canary.98b84da

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vib3code/sdk",
3
- "version": "2.0.3-canary.8d2fdcd",
3
+ "version": "2.0.3-canary.98b84da",
4
4
  "description": "VIB3+ 4D Visualization SDK - Unified engine with 6D rotation, MCP agentic integration, and cross-platform support",
5
5
  "type": "module",
6
6
  "main": "src/core/VIB3Engine.js",
@@ -354,12 +354,18 @@ export class Mat4x4 {
354
354
 
355
355
  /**
356
356
  * Transpose matrix
357
+ * @param {Mat4x4} [target=null] - Optional target matrix
357
358
  * @returns {Mat4x4} New transposed matrix
358
359
  */
359
- transpose() {
360
+ transpose(target = null) {
360
361
  const m = this.data;
361
- const out = new Mat4x4(Mat4x4.UNINITIALIZED);
362
+ const out = target || new Mat4x4(Mat4x4.UNINITIALIZED);
362
363
  const r = out.data;
364
+ // If target is same as source, use intermediate or careful swap
365
+ if (target === this) {
366
+ return this.transposeInPlace();
367
+ }
368
+
363
369
  r[0] = m[0]; r[4] = m[1]; r[8] = m[2]; r[12] = m[3];
364
370
  r[1] = m[4]; r[5] = m[5]; r[9] = m[6]; r[13] = m[7];
365
371
  r[2] = m[8]; r[6] = m[9]; r[10] = m[10]; r[14] = m[11];
@@ -415,62 +421,85 @@ export class Mat4x4 {
415
421
 
416
422
  /**
417
423
  * Calculate inverse matrix
424
+ * @param {Mat4x4} [target=null] - Optional target matrix
418
425
  * @returns {Mat4x4|null} Inverse matrix or null if singular
419
426
  */
420
- inverse() {
427
+ inverse(target = null) {
421
428
  const m = this.data;
422
- const out = new Mat4x4(Mat4x4.UNINITIALIZED);
429
+ const out = target || new Mat4x4(Mat4x4.UNINITIALIZED);
423
430
  const inv = out.data;
424
431
 
425
- inv[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] +
426
- m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
432
+ // Note: For in-place inversion (target === this), we need to be careful.
433
+ // The standard algorithm uses input values for every output cell.
434
+ // We can check for aliasing or use local variables if we wanted full safety,
435
+ // but simplest is to compute to temp if aliased, or just computing to the array directly works
436
+ // IF we cache everything first. But here we are writing to `inv` index by index.
437
+ // If inv === m, writing inv[0] destroys m[0] which is needed for inv[5] etc.
438
+ // So aliasing is NOT safe with this direct write approach.
439
+
440
+ // Handle aliasing by cloning first if needed, or using temp array.
441
+ // Since we want performance, let's detect aliasing.
442
+ let sourceData = m;
443
+ if (target === this) {
444
+ // Copy source data to temp array so we can write to 'this.data' safely
445
+ // We can't avoid allocation entirely in this specific edge case easily without unrolling everything into locals,
446
+ // which is huge for 4x4 inverse.
447
+ // Using a static temp buffer would be unsafe for threading/recursion (not an issue in JS single thread usually but still).
448
+ // Let's just clone the source data for the calculation.
449
+ sourceData = new Float32Array(m);
450
+ }
451
+
452
+ const s = sourceData;
453
+
454
+ inv[0] = s[5] * s[10] * s[15] - s[5] * s[11] * s[14] - s[9] * s[6] * s[15] +
455
+ s[9] * s[7] * s[14] + s[13] * s[6] * s[11] - s[13] * s[7] * s[10];
427
456
 
428
- inv[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] -
429
- m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
457
+ inv[4] = -s[4] * s[10] * s[15] + s[4] * s[11] * s[14] + s[8] * s[6] * s[15] -
458
+ s[8] * s[7] * s[14] - s[12] * s[6] * s[11] + s[12] * s[7] * s[10];
430
459
 
431
- inv[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] +
432
- m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
460
+ inv[8] = s[4] * s[9] * s[15] - s[4] * s[11] * s[13] - s[8] * s[5] * s[15] +
461
+ s[8] * s[7] * s[13] + s[12] * s[5] * s[11] - s[12] * s[7] * s[9];
433
462
 
434
- inv[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] -
435
- m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
463
+ inv[12] = -s[4] * s[9] * s[14] + s[4] * s[10] * s[13] + s[8] * s[5] * s[14] -
464
+ s[8] * s[6] * s[13] - s[12] * s[5] * s[10] + s[12] * s[6] * s[9];
436
465
 
437
- inv[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] -
438
- m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
466
+ inv[1] = -s[1] * s[10] * s[15] + s[1] * s[11] * s[14] + s[9] * s[2] * s[15] -
467
+ s[9] * s[3] * s[14] - s[13] * s[2] * s[11] + s[13] * s[3] * s[10];
439
468
 
440
- inv[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] +
441
- m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
469
+ inv[5] = s[0] * s[10] * s[15] - s[0] * s[11] * s[14] - s[8] * s[2] * s[15] +
470
+ s[8] * s[3] * s[14] + s[12] * s[2] * s[11] - s[12] * s[3] * s[10];
442
471
 
443
- inv[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] -
444
- m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
472
+ inv[9] = -s[0] * s[9] * s[15] + s[0] * s[11] * s[13] + s[8] * s[1] * s[15] -
473
+ s[8] * s[3] * s[13] - s[12] * s[1] * s[11] + s[12] * s[3] * s[9];
445
474
 
446
- inv[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] +
447
- m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
475
+ inv[13] = s[0] * s[9] * s[14] - s[0] * s[10] * s[13] - s[8] * s[1] * s[14] +
476
+ s[8] * s[2] * s[13] + s[12] * s[1] * s[10] - s[12] * s[2] * s[9];
448
477
 
449
- inv[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] +
450
- m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
478
+ inv[2] = s[1] * s[6] * s[15] - s[1] * s[7] * s[14] - s[5] * s[2] * s[15] +
479
+ s[5] * s[3] * s[14] + s[13] * s[2] * s[7] - s[13] * s[3] * s[6];
451
480
 
452
- inv[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] -
453
- m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
481
+ inv[6] = -s[0] * s[6] * s[15] + s[0] * s[7] * s[14] + s[4] * s[2] * s[15] -
482
+ s[4] * s[3] * s[14] - s[12] * s[2] * s[7] + s[12] * s[3] * s[6];
454
483
 
455
- inv[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] +
456
- m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
484
+ inv[10] = s[0] * s[5] * s[15] - s[0] * s[7] * s[13] - s[4] * s[1] * s[15] +
485
+ s[4] * s[3] * s[13] + s[12] * s[1] * s[7] - s[12] * s[3] * s[5];
457
486
 
458
- inv[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] -
459
- m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
487
+ inv[14] = -s[0] * s[5] * s[14] + s[0] * s[6] * s[13] + s[4] * s[1] * s[14] -
488
+ s[4] * s[2] * s[13] - s[12] * s[1] * s[6] + s[12] * s[2] * s[5];
460
489
 
461
- inv[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] -
462
- m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
490
+ inv[3] = -s[1] * s[6] * s[11] + s[1] * s[7] * s[10] + s[5] * s[2] * s[11] -
491
+ s[5] * s[3] * s[10] - s[9] * s[2] * s[7] + s[9] * s[3] * s[6];
463
492
 
464
- inv[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] +
465
- m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
493
+ inv[7] = s[0] * s[6] * s[11] - s[0] * s[7] * s[10] - s[4] * s[2] * s[11] +
494
+ s[4] * s[3] * s[10] + s[8] * s[2] * s[7] - s[8] * s[3] * s[6];
466
495
 
467
- inv[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] -
468
- m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
496
+ inv[11] = -s[0] * s[5] * s[11] + s[0] * s[7] * s[9] + s[4] * s[1] * s[11] -
497
+ s[4] * s[3] * s[9] - s[8] * s[1] * s[7] + s[8] * s[3] * s[5];
469
498
 
470
- inv[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] +
471
- m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5];
499
+ inv[15] = s[0] * s[5] * s[10] - s[0] * s[6] * s[9] - s[4] * s[1] * s[10] +
500
+ s[4] * s[2] * s[9] + s[8] * s[1] * s[6] - s[8] * s[2] * s[5];
472
501
 
473
- const det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
502
+ const det = s[0] * inv[0] + s[1] * inv[4] + s[2] * inv[8] + s[3] * inv[12];
474
503
 
475
504
  if (Math.abs(det) < 1e-10) {
476
505
  return null; // Singular matrix
@@ -276,48 +276,54 @@ export class Rotor4D {
276
276
  * The result applies this rotation, then r's rotation
277
277
  *
278
278
  * @param {Rotor4D} r - Right operand
279
+ * @param {Rotor4D} [target=null] - Optional target rotor to write result into
279
280
  * @returns {Rotor4D} Composed rotor
280
281
  */
281
- multiply(r) {
282
+ multiply(r, target = null) {
282
283
  // Full geometric product of two rotors in 4D
283
284
  // This is derived from the geometric algebra product rules
284
285
 
285
286
  const a = this;
286
287
  const b = r;
287
288
 
288
- return new Rotor4D(
289
- // Scalar component
290
- a.s * b.s - a.xy * b.xy - a.xz * b.xz - a.yz * b.yz -
291
- a.xw * b.xw - a.yw * b.yw - a.zw * b.zw - a.xyzw * b.xyzw,
289
+ // Compute all components first to ensure safety if target aliases a or b
290
+ const s = a.s * b.s - a.xy * b.xy - a.xz * b.xz - a.yz * b.yz -
291
+ a.xw * b.xw - a.yw * b.yw - a.zw * b.zw - a.xyzw * b.xyzw;
292
292
 
293
- // XY bivector
294
- a.s * b.xy + a.xy * b.s + a.xz * b.yz - a.yz * b.xz +
295
- a.xw * b.yw - a.yw * b.xw - a.zw * b.xyzw - a.xyzw * b.zw,
293
+ const xy = a.s * b.xy + a.xy * b.s + a.xz * b.yz - a.yz * b.xz +
294
+ a.xw * b.yw - a.yw * b.xw - a.zw * b.xyzw - a.xyzw * b.zw;
296
295
 
297
- // XZ bivector
298
- a.s * b.xz + a.xz * b.s - a.xy * b.yz + a.yz * b.xy +
299
- a.xw * b.zw + a.yw * b.xyzw - a.zw * b.xw + a.xyzw * b.yw,
296
+ const xz = a.s * b.xz + a.xz * b.s - a.xy * b.yz + a.yz * b.xy +
297
+ a.xw * b.zw + a.yw * b.xyzw - a.zw * b.xw + a.xyzw * b.yw;
300
298
 
301
- // YZ bivector
302
- a.s * b.yz + a.yz * b.s + a.xy * b.xz - a.xz * b.xy -
303
- a.xw * b.xyzw + a.yw * b.zw - a.zw * b.yw - a.xyzw * b.xw,
299
+ const yz = a.s * b.yz + a.yz * b.s + a.xy * b.xz - a.xz * b.xy -
300
+ a.xw * b.xyzw + a.yw * b.zw - a.zw * b.yw - a.xyzw * b.xw;
304
301
 
305
- // XW bivector
306
- a.s * b.xw + a.xw * b.s - a.xy * b.yw + a.xz * b.zw +
307
- a.yz * b.xyzw + a.yw * b.xy - a.zw * b.xz + a.xyzw * b.yz,
302
+ const xw = a.s * b.xw + a.xw * b.s - a.xy * b.yw + a.xz * b.zw +
303
+ a.yz * b.xyzw + a.yw * b.xy - a.zw * b.xz + a.xyzw * b.yz;
308
304
 
309
- // YW bivector
310
- a.s * b.yw + a.yw * b.s + a.xy * b.xw - a.xz * b.xyzw -
311
- a.yz * b.zw - a.xw * b.xy + a.zw * b.yz - a.xyzw * b.xz,
305
+ const yw = a.s * b.yw + a.yw * b.s + a.xy * b.xw - a.xz * b.xyzw -
306
+ a.yz * b.zw - a.xw * b.xy + a.zw * b.yz - a.xyzw * b.xz;
312
307
 
313
- // ZW bivector
314
- a.s * b.zw + a.zw * b.s + a.xy * b.xyzw + a.xz * b.xw +
315
- a.yz * b.yw - a.xw * b.xz - a.yw * b.yz + a.xyzw * b.xy,
308
+ const zw = a.s * b.zw + a.zw * b.s + a.xy * b.xyzw + a.xz * b.xw +
309
+ a.yz * b.yw - a.xw * b.xz - a.yw * b.yz + a.xyzw * b.xy;
316
310
 
317
- // Pseudoscalar XYZW
318
- a.s * b.xyzw + a.xyzw * b.s + a.xy * b.zw - a.xz * b.yw +
319
- a.yz * b.xw + a.xw * b.yz - a.yw * b.xz + a.zw * b.xy
320
- );
311
+ const xyzw = a.s * b.xyzw + a.xyzw * b.s + a.xy * b.zw - a.xz * b.yw +
312
+ a.yz * b.xw + a.xw * b.yz - a.yw * b.xz + a.zw * b.xy;
313
+
314
+ if (target) {
315
+ target.s = s;
316
+ target.xy = xy;
317
+ target.xz = xz;
318
+ target.yz = yz;
319
+ target.xw = xw;
320
+ target.yw = yw;
321
+ target.zw = zw;
322
+ target.xyzw = xyzw;
323
+ return target;
324
+ }
325
+
326
+ return new Rotor4D(s, xy, xz, yz, xw, yw, zw, xyzw);
321
327
  }
322
328
 
323
329
  /**
package/src/math/Vec4.js CHANGED
@@ -313,7 +313,11 @@ export class Vec4 {
313
313
  * @returns {number}
314
314
  */
315
315
  distanceTo(v) {
316
- return this.sub(v).length();
316
+ const dx = this._x - v._x;
317
+ const dy = this._y - v._y;
318
+ const dz = this._z - v._z;
319
+ const dw = this._w - v._w;
320
+ return Math.sqrt(dx * dx + dy * dy + dz * dz + dw * dw);
317
321
  }
318
322
 
319
323
  /**
@@ -322,7 +326,11 @@ export class Vec4 {
322
326
  * @returns {number}
323
327
  */
324
328
  distanceToSquared(v) {
325
- return this.sub(v).lengthSquared();
329
+ const dx = this._x - v._x;
330
+ const dy = this._y - v._y;
331
+ const dz = this._z - v._z;
332
+ const dw = this._w - v._w;
333
+ return dx * dx + dy * dy + dz * dz + dw * dw;
326
334
  }
327
335
 
328
336
  /**
@@ -482,9 +490,17 @@ export class Vec4 {
482
490
  /**
483
491
  * Project 4D point to 3D using orthographic projection
484
492
  * Simply drops the W component
493
+ * @param {Vec4} [target=null] - Optional target vector
485
494
  * @returns {Vec4} Projected point (w component is 0)
486
495
  */
487
- projectOrthographic() {
496
+ projectOrthographic(target = null) {
497
+ if (target) {
498
+ target._x = this._x;
499
+ target._y = this._y;
500
+ target._z = this._z;
501
+ target._w = 0;
502
+ return target;
503
+ }
488
504
  return new Vec4(this._x, this._y, this._z, 0);
489
505
  }
490
506