@vib3code/sdk 2.0.3-canary.183c93e → 2.0.3-canary.19bcbe1

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.183c93e",
3
+ "version": "2.0.3-canary.19bcbe1",
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",
@@ -16,13 +16,14 @@ import { Vec4 } from '../../math/Vec4.js';
16
16
  * @param {number} radius - Hypersphere radius
17
17
  * @returns {Vec4} Point on hypersphere
18
18
  */
19
- export function projectToHypersphere(point, radius = 1) {
19
+ export function projectToHypersphere(point, radius = 1, target = null) {
20
20
  const len = point.length();
21
21
  if (len < 0.0001) {
22
22
  // Handle origin - project to north pole
23
+ if (target) return target.set(0, 0, 0, radius);
23
24
  return new Vec4(0, 0, 0, radius);
24
25
  }
25
- return point.scale(radius / len);
26
+ return point.scale(radius / len, target);
26
27
  }
27
28
 
28
29
  /**
@@ -30,9 +31,10 @@ export function projectToHypersphere(point, radius = 1) {
30
31
  * Maps all of 3D space onto the 4D hypersphere
31
32
  * @param {Vec4} point - Input point (uses x, y, z)
32
33
  * @param {number} radius - Hypersphere radius
34
+ * @param {Vec4} [target=null] - Optional target vector
33
35
  * @returns {Vec4} Point on hypersphere
34
36
  */
35
- export function stereographicToHypersphere(point, radius = 1) {
37
+ export function stereographicToHypersphere(point, radius = 1, target = null) {
36
38
  const x = point.x;
37
39
  const y = point.y;
38
40
  const z = point.z;
@@ -40,6 +42,15 @@ export function stereographicToHypersphere(point, radius = 1) {
40
42
  const sumSq = x * x + y * y + z * z;
41
43
  const denom = sumSq + 1;
42
44
 
45
+ if (target) {
46
+ return target.set(
47
+ (2 * x) / denom * radius,
48
+ (2 * y) / denom * radius,
49
+ (2 * z) / denom * radius,
50
+ (sumSq - 1) / denom * radius
51
+ );
52
+ }
53
+
43
54
  return new Vec4(
44
55
  (2 * x) / denom * radius,
45
56
  (2 * y) / denom * radius,
@@ -56,12 +67,22 @@ export function stereographicToHypersphere(point, radius = 1) {
56
67
  * @param {number} phi - Azimuth on base S² (0 to 2π)
57
68
  * @param {number} psi - Fiber angle (0 to 2π)
58
69
  * @param {number} radius - Hypersphere radius
70
+ * @param {Vec4} [target=null] - Optional target vector
59
71
  * @returns {Vec4} Point on hypersphere
60
72
  */
61
- export function hopfFibration(theta, phi, psi, radius = 1) {
73
+ export function hopfFibration(theta, phi, psi, radius = 1, target = null) {
62
74
  const cosTheta2 = Math.cos(theta / 2);
63
75
  const sinTheta2 = Math.sin(theta / 2);
64
76
 
77
+ if (target) {
78
+ return target.set(
79
+ cosTheta2 * Math.cos((phi + psi) / 2) * radius,
80
+ cosTheta2 * Math.sin((phi + psi) / 2) * radius,
81
+ sinTheta2 * Math.cos((phi - psi) / 2) * radius,
82
+ sinTheta2 * Math.sin((phi - psi) / 2) * radius
83
+ );
84
+ }
85
+
65
86
  return new Vec4(
66
87
  cosTheta2 * Math.cos((phi + psi) / 2) * radius,
67
88
  cosTheta2 * Math.sin((phi + psi) / 2) * radius,
@@ -78,8 +99,9 @@ export function hopfFibration(theta, phi, psi, radius = 1) {
78
99
  * @returns {Vec4[]} Warped vertices
79
100
  */
80
101
  export function warpRadial(vertices, radius = 1, blendFactor = 1) {
102
+ const onSphere = new Vec4();
81
103
  return vertices.map(v => {
82
- const onSphere = projectToHypersphere(v, radius);
104
+ projectToHypersphere(v, radius, onSphere);
83
105
  return v.lerp(onSphere, blendFactor);
84
106
  });
85
107
  }
@@ -93,8 +115,9 @@ export function warpRadial(vertices, radius = 1, blendFactor = 1) {
93
115
  * @returns {Vec4[]} Warped vertices
94
116
  */
95
117
  export function warpStereographic(vertices, radius = 1, scale = 1) {
118
+ const scaled = new Vec4();
96
119
  return vertices.map(v => {
97
- const scaled = v.scale(scale);
120
+ v.scale(scale, scaled);
98
121
  return stereographicToHypersphere(scaled, radius);
99
122
  });
100
123
  }
@@ -146,25 +169,31 @@ export function warpHypersphereCore(geometry, options = {}) {
146
169
  twist = 1
147
170
  } = options;
148
171
 
149
- let warpedVertices;
150
-
151
- // Pre-scale vertices
152
- const scaledVertices = geometry.vertices.map(v => v.scale(scale));
153
-
154
- switch (method) {
155
- case 'stereographic':
156
- warpedVertices = warpStereographic(scaledVertices, radius, 1);
157
- break;
158
-
159
- case 'hopf':
160
- warpedVertices = warpHopf(scaledVertices, radius, twist);
161
- break;
172
+ const temp = new Vec4();
173
+ const warpedVertices = geometry.vertices.map(v => {
174
+ // Combined scaling and warping to minimize allocations
175
+ const result = v.scale(scale);
176
+
177
+ if (method === 'stereographic') {
178
+ stereographicToHypersphere(result, radius, result);
179
+ } else if (method === 'hopf') {
180
+ const r = result.length();
181
+ if (r < 0.0001) {
182
+ result.set(0, 0, 0, radius);
183
+ } else {
184
+ const theta = Math.acos(result.z / r);
185
+ const phi = Math.atan2(result.y, result.x);
186
+ const psi = result.w * twist + phi * 0.5;
187
+ hopfFibration(theta, phi, psi, radius, result);
188
+ }
189
+ } else {
190
+ // Radial (default)
191
+ projectToHypersphere(result, radius, temp);
192
+ result.lerp(temp, blend, result);
193
+ }
162
194
 
163
- case 'radial':
164
- default:
165
- warpedVertices = warpRadial(scaledVertices, radius, blend);
166
- break;
167
- }
195
+ return result;
196
+ });
168
197
 
169
198
  return {
170
199
  ...geometry,
@@ -18,6 +18,12 @@
18
18
  import { Vec4 } from './Vec4.js';
19
19
 
20
20
  export class Mat4x4 {
21
+ /**
22
+ * Internal token to skip initialization during construction
23
+ * @private
24
+ */
25
+ static UNINITIALIZED = {};
26
+
21
27
  /**
22
28
  * Create a new 4x4 matrix
23
29
  * Default is identity matrix
@@ -26,6 +32,8 @@ export class Mat4x4 {
26
32
  constructor(elements = null) {
27
33
  this.data = new Float32Array(16);
28
34
 
35
+ if (elements === Mat4x4.UNINITIALIZED) return;
36
+
29
37
  if (elements) {
30
38
  if (elements.length !== 16) {
31
39
  throw new Error('Mat4x4 requires exactly 16 elements');
@@ -45,12 +53,7 @@ export class Mat4x4 {
45
53
  * @returns {Mat4x4}
46
54
  */
47
55
  static identity() {
48
- return new Mat4x4([
49
- 1, 0, 0, 0,
50
- 0, 1, 0, 0,
51
- 0, 0, 1, 0,
52
- 0, 0, 0, 1
53
- ]);
56
+ return new Mat4x4();
54
57
  }
55
58
 
56
59
  /**
@@ -58,7 +61,7 @@ export class Mat4x4 {
58
61
  * @returns {Mat4x4}
59
62
  */
60
63
  static zero() {
61
- return new Mat4x4(new Float32Array(16));
64
+ return new Mat4x4(Mat4x4.UNINITIALIZED);
62
65
  }
63
66
 
64
67
  /**
@@ -161,7 +164,7 @@ export class Mat4x4 {
161
164
  * @returns {Mat4x4} New matrix = this * m
162
165
  */
163
166
  multiply(m, target = null) {
164
- const out = target || new Mat4x4();
167
+ const out = target || new Mat4x4(Mat4x4.UNINITIALIZED);
165
168
  const r = out.data;
166
169
  const a = this.data;
167
170
  const b = m.data;
@@ -321,7 +324,7 @@ export class Mat4x4 {
321
324
  * @returns {Mat4x4} New matrix
322
325
  */
323
326
  add(m, target = null) {
324
- const out = target || new Mat4x4();
327
+ const out = target || new Mat4x4(Mat4x4.UNINITIALIZED);
325
328
  const r = out.data;
326
329
  const a = this.data;
327
330
  const b = m.data;
@@ -339,7 +342,7 @@ export class Mat4x4 {
339
342
  * @returns {Mat4x4} New matrix
340
343
  */
341
344
  scale(s, target = null) {
342
- const out = target || new Mat4x4();
345
+ const out = target || new Mat4x4(Mat4x4.UNINITIALIZED);
343
346
  const r = out.data;
344
347
  const a = this.data;
345
348
 
@@ -355,12 +358,13 @@ export class Mat4x4 {
355
358
  */
356
359
  transpose() {
357
360
  const m = this.data;
358
- return new Mat4x4([
359
- m[0], m[4], m[8], m[12],
360
- m[1], m[5], m[9], m[13],
361
- m[2], m[6], m[10], m[14],
362
- m[3], m[7], m[11], m[15]
363
- ]);
361
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
362
+ const r = out.data;
363
+ r[0] = m[0]; r[4] = m[1]; r[8] = m[2]; r[12] = m[3];
364
+ r[1] = m[4]; r[5] = m[5]; r[9] = m[6]; r[13] = m[7];
365
+ r[2] = m[8]; r[6] = m[9]; r[10] = m[10]; r[14] = m[11];
366
+ r[3] = m[12]; r[7] = m[13]; r[11] = m[14]; r[15] = m[15];
367
+ return out;
364
368
  }
365
369
 
366
370
  /**
@@ -415,7 +419,8 @@ export class Mat4x4 {
415
419
  */
416
420
  inverse() {
417
421
  const m = this.data;
418
- const inv = new Float32Array(16);
422
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
423
+ const inv = out.data;
419
424
 
420
425
  inv[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] +
421
426
  m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
@@ -476,7 +481,7 @@ export class Mat4x4 {
476
481
  inv[i] *= invDet;
477
482
  }
478
483
 
479
- return new Mat4x4(inv);
484
+ return out;
480
485
  }
481
486
 
482
487
  /**
@@ -689,12 +694,13 @@ export class Mat4x4 {
689
694
  static rotationXY(angle) {
690
695
  const c = Math.cos(angle);
691
696
  const s = Math.sin(angle);
692
- return new Mat4x4([
693
- c, s, 0, 0,
694
- -s, c, 0, 0,
695
- 0, 0, 1, 0,
696
- 0, 0, 0, 1
697
- ]);
697
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
698
+ const r = out.data;
699
+ r[0] = c; r[1] = s;
700
+ r[4] = -s; r[5] = c;
701
+ r[10] = 1;
702
+ r[15] = 1;
703
+ return out;
698
704
  }
699
705
 
700
706
  /**
@@ -705,12 +711,13 @@ export class Mat4x4 {
705
711
  static rotationXZ(angle) {
706
712
  const c = Math.cos(angle);
707
713
  const s = Math.sin(angle);
708
- return new Mat4x4([
709
- c, 0, -s, 0,
710
- 0, 1, 0, 0,
711
- s, 0, c, 0,
712
- 0, 0, 0, 1
713
- ]);
714
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
715
+ const r = out.data;
716
+ r[0] = c; r[2] = -s;
717
+ r[5] = 1;
718
+ r[8] = s; r[10] = c;
719
+ r[15] = 1;
720
+ return out;
714
721
  }
715
722
 
716
723
  /**
@@ -721,12 +728,13 @@ export class Mat4x4 {
721
728
  static rotationYZ(angle) {
722
729
  const c = Math.cos(angle);
723
730
  const s = Math.sin(angle);
724
- return new Mat4x4([
725
- 1, 0, 0, 0,
726
- 0, c, s, 0,
727
- 0, -s, c, 0,
728
- 0, 0, 0, 1
729
- ]);
731
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
732
+ const r = out.data;
733
+ r[0] = 1;
734
+ r[5] = c; r[6] = s;
735
+ r[9] = -s; r[10] = c;
736
+ r[15] = 1;
737
+ return out;
730
738
  }
731
739
 
732
740
  /**
@@ -738,12 +746,13 @@ export class Mat4x4 {
738
746
  static rotationXW(angle) {
739
747
  const c = Math.cos(angle);
740
748
  const s = Math.sin(angle);
741
- return new Mat4x4([
742
- c, 0, 0, s,
743
- 0, 1, 0, 0,
744
- 0, 0, 1, 0,
745
- -s, 0, 0, c
746
- ]);
749
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
750
+ const r = out.data;
751
+ r[0] = c; r[3] = s;
752
+ r[5] = 1;
753
+ r[10] = 1;
754
+ r[12] = -s; r[15] = c;
755
+ return out;
747
756
  }
748
757
 
749
758
  /**
@@ -754,12 +763,13 @@ export class Mat4x4 {
754
763
  static rotationYW(angle) {
755
764
  const c = Math.cos(angle);
756
765
  const s = Math.sin(angle);
757
- return new Mat4x4([
758
- 1, 0, 0, 0,
759
- 0, c, 0, s,
760
- 0, 0, 1, 0,
761
- 0, -s, 0, c
762
- ]);
766
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
767
+ const r = out.data;
768
+ r[0] = 1;
769
+ r[5] = c; r[7] = s;
770
+ r[10] = 1;
771
+ r[13] = -s; r[15] = c;
772
+ return out;
763
773
  }
764
774
 
765
775
  /**
@@ -770,12 +780,13 @@ export class Mat4x4 {
770
780
  static rotationZW(angle) {
771
781
  const c = Math.cos(angle);
772
782
  const s = Math.sin(angle);
773
- return new Mat4x4([
774
- 1, 0, 0, 0,
775
- 0, 1, 0, 0,
776
- 0, 0, c, s,
777
- 0, 0, -s, c
778
- ]);
783
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
784
+ const r = out.data;
785
+ r[0] = 1;
786
+ r[5] = 1;
787
+ r[10] = c; r[11] = s;
788
+ r[14] = -s; r[15] = c;
789
+ return out;
779
790
  }
780
791
 
781
792
  /**
@@ -847,12 +858,13 @@ export class Mat4x4 {
847
858
  * @returns {Mat4x4}
848
859
  */
849
860
  static uniformScale(s) {
850
- return new Mat4x4([
851
- s, 0, 0, 0,
852
- 0, s, 0, 0,
853
- 0, 0, s, 0,
854
- 0, 0, 0, s
855
- ]);
861
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
862
+ const r = out.data;
863
+ r[0] = s;
864
+ r[5] = s;
865
+ r[10] = s;
866
+ r[15] = s;
867
+ return out;
856
868
  }
857
869
 
858
870
  /**
@@ -864,12 +876,13 @@ export class Mat4x4 {
864
876
  * @returns {Mat4x4}
865
877
  */
866
878
  static scale(sx, sy, sz, sw = 1) {
867
- return new Mat4x4([
868
- sx, 0, 0, 0,
869
- 0, sy, 0, 0,
870
- 0, 0, sz, 0,
871
- 0, 0, 0, sw
872
- ]);
879
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
880
+ const r = out.data;
881
+ r[0] = sx;
882
+ r[5] = sy;
883
+ r[10] = sz;
884
+ r[15] = sw;
885
+ return out;
873
886
  }
874
887
 
875
888
  /**
@@ -885,12 +898,13 @@ export class Mat4x4 {
885
898
  static translation(tx, ty, tz, tw = 0) {
886
899
  // For true 4D translation, you need 5D homogeneous coordinates
887
900
  // This is a placeholder that adds the translation to the W column
888
- return new Mat4x4([
889
- 1, 0, 0, 0,
890
- 0, 1, 0, 0,
891
- 0, 0, 1, 0,
892
- tx, ty, tz, 1 + tw
893
- ]);
901
+ const out = new Mat4x4(Mat4x4.UNINITIALIZED);
902
+ const r = out.data;
903
+ r[0] = 1;
904
+ r[5] = 1;
905
+ r[10] = 1;
906
+ r[12] = tx; r[13] = ty; r[14] = tz; r[15] = 1 + tw;
907
+ return out;
894
908
  }
895
909
  }
896
910