@vib3code/sdk 2.0.1 → 2.0.3-canary.0a63e71

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.
Files changed (192) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/DOCS/AGENT_HARNESS_ARCHITECTURE.md +245 -0
  3. package/DOCS/ANDROID_DEPLOYMENT.md +59 -0
  4. package/DOCS/ARCHITECTURE.md +1 -0
  5. package/DOCS/CI_TESTING.md +2 -0
  6. package/DOCS/CLI_ONBOARDING.md +3 -1
  7. package/DOCS/CONTROL_REFERENCE.md +2 -0
  8. package/DOCS/CROSS_SITE_DESIGN_PATTERNS.md +119 -0
  9. package/DOCS/ENV_SETUP.md +2 -0
  10. package/DOCS/EPIC_SCROLL_EVENTS.md +775 -0
  11. package/DOCS/EXPANSION_DESIGN.md +979 -0
  12. package/DOCS/EXPANSION_DESIGN_ULTRA.md +389 -0
  13. package/DOCS/EXPORT_FORMATS.md +2 -0
  14. package/DOCS/GPU_DISPOSAL_GUIDE.md +2 -0
  15. package/DOCS/HANDOFF_LANDING_PAGE.md +156 -0
  16. package/DOCS/HANDOFF_SDK_DEVELOPMENT.md +495 -0
  17. package/DOCS/LICENSING_TIERS.md +2 -0
  18. package/DOCS/MASTER_PLAN_2026-01-31.md +4 -2
  19. package/DOCS/MULTIVIZ_CHOREOGRAPHY_PATTERNS.md +939 -0
  20. package/DOCS/OBS_SETUP_GUIDE.md +2 -0
  21. package/DOCS/OPTIMIZATION_PLAN_MATH.md +119 -0
  22. package/DOCS/PRODUCT_STRATEGY.md +65 -0
  23. package/DOCS/PROJECT_SETUP.md +2 -0
  24. package/DOCS/README.md +105 -0
  25. package/DOCS/REFERENCE_SCROLL_ANALYSIS.md +99 -0
  26. package/DOCS/RENDERER_LIFECYCLE.md +2 -0
  27. package/DOCS/REPO_MANIFEST.md +2 -0
  28. package/DOCS/ROADMAP.md +113 -0
  29. package/DOCS/SCROLL_TIMELINE_v3.md +271 -0
  30. package/DOCS/SITE_REFACTOR_PLAN.md +102 -0
  31. package/DOCS/STATUS.md +26 -0
  32. package/DOCS/SYSTEM_INVENTORY.md +37 -32
  33. package/DOCS/TELEMETRY_EXPORTS.md +2 -0
  34. package/DOCS/VISUAL_ANALYSIS_CLICKERSS.md +87 -0
  35. package/DOCS/VISUAL_ANALYSIS_FACETAD.md +135 -0
  36. package/DOCS/VISUAL_ANALYSIS_SIMONE.md +97 -0
  37. package/DOCS/VISUAL_ANALYSIS_TABLESIDE.md +88 -0
  38. package/DOCS/WEBGPU_STATUS.md +121 -38
  39. package/DOCS/XR_BENCHMARKS.md +2 -0
  40. package/DOCS/archive/BLUEPRINT_EXECUTION_PLAN_2026-01-07.md +1 -0
  41. package/DOCS/archive/DEV_TRACK_ANALYSIS.md +1 -0
  42. package/DOCS/archive/DEV_TRACK_PLAN_2026-01-07.md +1 -0
  43. package/DOCS/archive/SESSION_014_PLAN.md +1 -0
  44. package/DOCS/archive/SESSION_LOG_2026-01-07.md +1 -0
  45. package/DOCS/archive/STRATEGIC_BLUEPRINT_2026-01-07.md +1 -0
  46. package/DOCS/archive/SYSTEM_AUDIT_2026-01-30.md +1 -0
  47. package/DOCS/archive/WEBGPU_STATUS_2026-02-15_STALE.md +1 -0
  48. package/DOCS/{DEV_TRACK_SESSION_2026-01-31.md → dev-tracks/DEV_TRACK_SESSION_2026-01-31.md} +3 -1
  49. package/DOCS/dev-tracks/DEV_TRACK_SESSION_2026-02-06.md +233 -0
  50. package/DOCS/dev-tracks/DEV_TRACK_SESSION_2026-02-13.md +129 -0
  51. package/DOCS/dev-tracks/DEV_TRACK_SESSION_2026-02-15.md +144 -0
  52. package/DOCS/dev-tracks/DEV_TRACK_SESSION_2026-02-16.md +110 -0
  53. package/DOCS/dev-tracks/PERF_UPGRADE_2026-02-16.md +310 -0
  54. package/DOCS/dev-tracks/README.md +12 -0
  55. package/README.md +26 -13
  56. package/cpp/CMakeLists.txt +236 -0
  57. package/cpp/bindings/embind.cpp +269 -0
  58. package/cpp/build.sh +129 -0
  59. package/cpp/geometry/Crystal.cpp +103 -0
  60. package/cpp/geometry/Fractal.cpp +136 -0
  61. package/cpp/geometry/GeometryGenerator.cpp +262 -0
  62. package/cpp/geometry/KleinBottle.cpp +71 -0
  63. package/cpp/geometry/Sphere.cpp +134 -0
  64. package/cpp/geometry/Tesseract.cpp +94 -0
  65. package/cpp/geometry/Tetrahedron.cpp +83 -0
  66. package/cpp/geometry/Torus.cpp +65 -0
  67. package/cpp/geometry/WarpFunctions.cpp +238 -0
  68. package/cpp/geometry/Wave.cpp +85 -0
  69. package/cpp/include/vib3_ffi.h +238 -0
  70. package/cpp/math/Mat4x4.cpp +409 -0
  71. package/cpp/math/Mat4x4.hpp +209 -0
  72. package/cpp/math/Projection.cpp +142 -0
  73. package/cpp/math/Projection.hpp +148 -0
  74. package/cpp/math/Rotor4D.cpp +322 -0
  75. package/cpp/math/Rotor4D.hpp +204 -0
  76. package/cpp/math/Vec4.cpp +303 -0
  77. package/cpp/math/Vec4.hpp +225 -0
  78. package/cpp/src/vib3_ffi.cpp +607 -0
  79. package/cpp/tests/Geometry_test.cpp +213 -0
  80. package/cpp/tests/Mat4x4_test.cpp +494 -0
  81. package/cpp/tests/Projection_test.cpp +298 -0
  82. package/cpp/tests/Rotor4D_test.cpp +423 -0
  83. package/cpp/tests/Vec4_test.cpp +489 -0
  84. package/docs/webgpu-live.html +1 -1
  85. package/package.json +41 -30
  86. package/src/agent/index.js +1 -3
  87. package/src/agent/mcp/MCPServer.js +1220 -144
  88. package/src/agent/mcp/index.js +1 -1
  89. package/src/agent/mcp/stdio-server.js +264 -0
  90. package/src/agent/mcp/tools.js +498 -31
  91. package/src/cli/index.js +431 -47
  92. package/src/core/CanvasManager.js +97 -204
  93. package/src/core/ErrorReporter.js +1 -1
  94. package/src/core/Parameters.js +1 -1
  95. package/src/core/VIB3Engine.js +93 -4
  96. package/src/core/VitalitySystem.js +53 -0
  97. package/src/core/index.js +18 -0
  98. package/src/core/renderers/FacetedRendererAdapter.js +10 -9
  99. package/src/core/renderers/HolographicRendererAdapter.js +13 -9
  100. package/src/core/renderers/QuantumRendererAdapter.js +11 -7
  101. package/src/creative/AestheticMapper.js +628 -0
  102. package/src/creative/ChoreographyPlayer.js +481 -0
  103. package/src/creative/index.js +11 -0
  104. package/src/experimental/GameLoop.js +72 -0
  105. package/src/experimental/LatticePhysics.js +100 -0
  106. package/src/experimental/LiveDirector.js +143 -0
  107. package/src/experimental/PlayerController4D.js +154 -0
  108. package/src/experimental/VIB3Actor.js +138 -0
  109. package/src/experimental/VIB3Compositor.js +117 -0
  110. package/src/experimental/VIB3Link.js +122 -0
  111. package/src/experimental/VIB3Orchestrator.js +146 -0
  112. package/src/experimental/VIB3Universe.js +109 -0
  113. package/src/experimental/demos/CrystalLabyrinth.js +202 -0
  114. package/src/export/TradingCardManager.js +3 -4
  115. package/src/export/index.js +11 -1
  116. package/src/faceted/FacetedSystem.js +260 -394
  117. package/src/games/glyph-war/GlyphWarVisualizer.js +641 -0
  118. package/src/geometry/generators/Crystal.js +2 -2
  119. package/src/geometry/warp/HypersphereCore.js +53 -24
  120. package/src/holograms/HolographicVisualizer.js +84 -98
  121. package/src/holograms/RealHolographicSystem.js +194 -43
  122. package/src/math/Mat4x4.js +308 -105
  123. package/src/math/Rotor4D.js +124 -40
  124. package/src/math/Vec4.js +200 -103
  125. package/src/math/index.js +7 -7
  126. package/src/polychora/PolychoraSystem.js +77 -0
  127. package/src/quantum/QuantumEngine.js +103 -66
  128. package/src/quantum/QuantumVisualizer.js +31 -22
  129. package/src/reactivity/index.js +3 -5
  130. package/src/render/LayerPresetManager.js +372 -0
  131. package/src/render/LayerReactivityBridge.js +344 -0
  132. package/src/render/LayerRelationshipGraph.js +610 -0
  133. package/src/render/MultiCanvasBridge.js +148 -25
  134. package/src/render/ShaderLoader.js +38 -0
  135. package/src/render/ShaderProgram.js +4 -4
  136. package/src/render/UnifiedRenderBridge.js +4 -1
  137. package/src/render/backends/WebGPUBackend.js +8 -4
  138. package/src/render/index.js +27 -2
  139. package/src/scene/Node4D.js +74 -24
  140. package/src/scene/index.js +4 -4
  141. package/src/shaders/common/geometry24.glsl +65 -0
  142. package/src/shaders/common/geometry24.wgsl +54 -0
  143. package/src/shaders/common/rotation4d.glsl +4 -4
  144. package/src/shaders/common/rotation4d.wgsl +2 -2
  145. package/src/shaders/common/uniforms.wgsl +15 -8
  146. package/src/shaders/faceted/faceted.frag.glsl +220 -80
  147. package/src/shaders/faceted/faceted.frag.wgsl +144 -90
  148. package/src/shaders/holographic/holographic.frag.glsl +28 -9
  149. package/src/shaders/holographic/holographic.frag.wgsl +112 -41
  150. package/src/shaders/quantum/quantum.frag.glsl +1 -0
  151. package/src/shaders/quantum/quantum.frag.wgsl +6 -4
  152. package/src/testing/ParallelTestFramework.js +2 -2
  153. package/src/ui/adaptive/renderers/webgpu/WebGPURenderer.ts +2 -2
  154. package/src/viewer/GalleryUI.js +17 -0
  155. package/src/viewer/ViewerPortal.js +2 -2
  156. package/src/viewer/index.js +1 -1
  157. package/tools/headless-renderer.js +258 -0
  158. package/tools/shader-sync-verify.js +14 -8
  159. package/tools/site-analysis/all-reports.json +32 -0
  160. package/tools/site-analysis/combined-analysis.md +50 -0
  161. package/tools/site-analyzer.mjs +779 -0
  162. package/tools/visual-catalog/capture.js +276 -0
  163. package/tools/visual-catalog/composite.js +138 -0
  164. package/types/adaptive-sdk.d.ts +204 -5
  165. package/types/agent/cli.d.ts +78 -0
  166. package/types/agent/index.d.ts +18 -0
  167. package/types/agent/mcp.d.ts +87 -0
  168. package/types/agent/telemetry.d.ts +190 -0
  169. package/types/core/VIB3Engine.d.ts +26 -0
  170. package/types/core/index.d.ts +261 -0
  171. package/types/creative/AestheticMapper.d.ts +72 -0
  172. package/types/creative/ChoreographyPlayer.d.ts +96 -0
  173. package/types/creative/index.d.ts +17 -0
  174. package/types/export/index.d.ts +243 -0
  175. package/types/geometry/index.d.ts +164 -0
  176. package/types/math/index.d.ts +214 -0
  177. package/types/render/LayerPresetManager.d.ts +78 -0
  178. package/types/render/LayerReactivityBridge.d.ts +85 -0
  179. package/types/render/LayerRelationshipGraph.d.ts +174 -0
  180. package/types/render/index.d.ts +3 -0
  181. package/types/scene/index.d.ts +204 -0
  182. package/types/systems/index.d.ts +244 -0
  183. package/types/variations/index.d.ts +62 -0
  184. package/types/viewer/index.d.ts +225 -0
  185. package/DOCS/BLUEPRINT_EXECUTION_PLAN_2026-01-07.md +0 -34
  186. package/DOCS/DEV_TRACK_ANALYSIS.md +0 -77
  187. package/DOCS/DEV_TRACK_PLAN_2026-01-07.md +0 -42
  188. package/DOCS/SESSION_014_PLAN.md +0 -195
  189. package/DOCS/SESSION_LOG_2026-01-07.md +0 -56
  190. package/DOCS/STRATEGIC_BLUEPRINT_2026-01-07.md +0 -72
  191. package/DOCS/SYSTEM_AUDIT_2026-01-30.md +0 -738
  192. /package/src/viewer/{ReactivityManager.js → ViewerInputHandler.js} +0 -0
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Projection.cpp - 4D to 3D Projection Implementation
3
+ */
4
+
5
+ #include "Projection.hpp"
6
+ #include <cmath>
7
+ #include <algorithm>
8
+
9
+ namespace vib3 {
10
+
11
+ constexpr float LARGE_VALUE = 1e6f;
12
+ constexpr float EPSILON = 1e-6f;
13
+
14
+ Projection3D projectPerspective(const Vec4& v, float distance) noexcept {
15
+ float denom = distance - v.w;
16
+
17
+ // Handle singularity
18
+ if (std::abs(denom) < EPSILON) {
19
+ float sign = (denom >= 0) ? 1.0f : -1.0f;
20
+ return Projection3D(
21
+ v.x * sign * LARGE_VALUE,
22
+ v.y * sign * LARGE_VALUE,
23
+ v.z * sign * LARGE_VALUE
24
+ );
25
+ }
26
+
27
+ float factor = distance / denom;
28
+ return Projection3D(v.x * factor, v.y * factor, v.z * factor);
29
+ }
30
+
31
+ Projection3D projectStereographic(const Vec4& v) noexcept {
32
+ float denom = 1.0f - v.w;
33
+
34
+ // Handle singularity at w = 1 (north pole of hypersphere)
35
+ if (std::abs(denom) < EPSILON) {
36
+ // Return direction at infinity
37
+ float sign = (v.x + v.y + v.z >= 0) ? 1.0f : -1.0f;
38
+ return Projection3D(sign * LARGE_VALUE, sign * LARGE_VALUE, sign * LARGE_VALUE);
39
+ }
40
+
41
+ float factor = 1.0f / denom;
42
+ return Projection3D(v.x * factor, v.y * factor, v.z * factor);
43
+ }
44
+
45
+ Projection3D projectOrthographic(const Vec4& v) noexcept {
46
+ return Projection3D(v.x, v.y, v.z);
47
+ }
48
+
49
+ Projection3D projectOblique(const Vec4& v,
50
+ float shearX,
51
+ float shearY,
52
+ float shearZ) noexcept {
53
+ return Projection3D(
54
+ v.x + shearX * v.w,
55
+ v.y + shearY * v.w,
56
+ v.z + shearZ * v.w
57
+ );
58
+ }
59
+
60
+ SliceResult projectSlice(const Vec4& v,
61
+ float sliceW,
62
+ float thickness,
63
+ bool fade) noexcept {
64
+ float dist = std::abs(v.w - sliceW);
65
+
66
+ if (dist > thickness) {
67
+ return SliceResult::invalid();
68
+ }
69
+
70
+ float alpha = 1.0f;
71
+ if (fade && thickness > 0) {
72
+ alpha = 1.0f - (dist / thickness);
73
+ alpha = std::max(0.0f, std::min(1.0f, alpha));
74
+ }
75
+
76
+ return SliceResult{
77
+ Projection3D(v.x, v.y, v.z),
78
+ alpha,
79
+ true
80
+ };
81
+ }
82
+
83
+ // Batch projections
84
+
85
+ std::vector<Projection3D> projectPerspectiveBatch(
86
+ const std::vector<Vec4>& points,
87
+ float distance
88
+ ) noexcept {
89
+ std::vector<Projection3D> result;
90
+ result.reserve(points.size());
91
+
92
+ for (const auto& p : points) {
93
+ result.push_back(projectPerspective(p, distance));
94
+ }
95
+
96
+ return result;
97
+ }
98
+
99
+ std::vector<Projection3D> projectStereographicBatch(
100
+ const std::vector<Vec4>& points
101
+ ) noexcept {
102
+ std::vector<Projection3D> result;
103
+ result.reserve(points.size());
104
+
105
+ for (const auto& p : points) {
106
+ result.push_back(projectStereographic(p));
107
+ }
108
+
109
+ return result;
110
+ }
111
+
112
+ std::vector<Projection3D> projectOrthographicBatch(
113
+ const std::vector<Vec4>& points
114
+ ) noexcept {
115
+ std::vector<Projection3D> result;
116
+ result.reserve(points.size());
117
+
118
+ for (const auto& p : points) {
119
+ result.push_back(projectOrthographic(p));
120
+ }
121
+
122
+ return result;
123
+ }
124
+
125
+ std::vector<float> projectToFloatArray(
126
+ const std::vector<Vec4>& points,
127
+ float distance
128
+ ) noexcept {
129
+ std::vector<float> result;
130
+ result.reserve(points.size() * 3);
131
+
132
+ for (const auto& p : points) {
133
+ auto proj = projectPerspective(p, distance);
134
+ result.push_back(proj.x);
135
+ result.push_back(proj.y);
136
+ result.push_back(proj.z);
137
+ }
138
+
139
+ return result;
140
+ }
141
+
142
+ } // namespace vib3
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Projection.hpp - 4D to 3D Projection Functions
3
+ *
4
+ * Various projection methods for visualizing 4D geometry in 3D.
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include "Vec4.hpp"
10
+ #include <array>
11
+ #include <vector>
12
+
13
+ namespace vib3 {
14
+
15
+ /**
16
+ * 4D to 3D projection result
17
+ */
18
+ struct Projection3D {
19
+ float x, y, z;
20
+
21
+ constexpr Projection3D() noexcept : x(0), y(0), z(0) {}
22
+ constexpr Projection3D(float x, float y, float z) noexcept : x(x), y(y), z(z) {}
23
+
24
+ /**
25
+ * Convert to array
26
+ */
27
+ constexpr std::array<float, 3> toArray() const noexcept {
28
+ return {x, y, z};
29
+ }
30
+ };
31
+
32
+ /**
33
+ * Projection parameters
34
+ */
35
+ struct ProjectionParams {
36
+ float distance = 2.0f; // For perspective projection
37
+ float viewerW = 0.0f; // Viewer position in W
38
+ float sliceW = 0.0f; // For slice projection
39
+ float sliceThickness = 0.1f;
40
+
41
+ static ProjectionParams defaults() noexcept {
42
+ return ProjectionParams{};
43
+ }
44
+ };
45
+
46
+ /**
47
+ * Perspective projection: X = x * d / (d - w)
48
+ *
49
+ * Projects 4D points as if viewed from w = distance.
50
+ * Objects farther in w appear smaller (like depth in 3D).
51
+ *
52
+ * @param v 4D point
53
+ * @param distance Projection distance
54
+ * @return 3D projected point
55
+ */
56
+ Projection3D projectPerspective(const Vec4& v, float distance = 2.0f) noexcept;
57
+
58
+ /**
59
+ * Stereographic projection: X = x / (1 - w)
60
+ *
61
+ * Conformal projection from S³ (unit hypersphere).
62
+ * Preserves angles but not distances.
63
+ * Pole at w = 1 maps to infinity.
64
+ *
65
+ * @param v 4D point (ideally on unit hypersphere)
66
+ * @return 3D projected point
67
+ */
68
+ Projection3D projectStereographic(const Vec4& v) noexcept;
69
+
70
+ /**
71
+ * Orthographic projection: X = x, Y = y, Z = z
72
+ *
73
+ * Simple parallel projection, discarding W.
74
+ *
75
+ * @param v 4D point
76
+ * @return 3D projected point
77
+ */
78
+ Projection3D projectOrthographic(const Vec4& v) noexcept;
79
+
80
+ /**
81
+ * Oblique (cavalier) projection: X = x + shear.x * w, etc.
82
+ *
83
+ * @param v 4D point
84
+ * @param shearX X shear factor
85
+ * @param shearY Y shear factor
86
+ * @param shearZ Z shear factor
87
+ * @return 3D projected point
88
+ */
89
+ Projection3D projectOblique(const Vec4& v,
90
+ float shearX = 0.5f,
91
+ float shearY = 0.5f,
92
+ float shearZ = 0.0f) noexcept;
93
+
94
+ /**
95
+ * Cross-sectional slice projection
96
+ *
97
+ * Returns empty if point is outside the slice.
98
+ *
99
+ * @param v 4D point
100
+ * @param sliceW W coordinate of the slice plane
101
+ * @param thickness Half-thickness of the slice
102
+ * @param fade If true, fade points near edges
103
+ * @return 3D point if in slice, or empty optional
104
+ */
105
+ struct SliceResult {
106
+ Projection3D point;
107
+ float alpha; // 1.0 = center of slice, 0.0 = edge
108
+ bool valid;
109
+
110
+ static SliceResult invalid() noexcept {
111
+ return SliceResult{{}, 0.0f, false};
112
+ }
113
+ };
114
+
115
+ SliceResult projectSlice(const Vec4& v,
116
+ float sliceW = 0.0f,
117
+ float thickness = 0.1f,
118
+ bool fade = true) noexcept;
119
+
120
+ /**
121
+ * Batch projection for efficiency
122
+ */
123
+ std::vector<Projection3D> projectPerspectiveBatch(
124
+ const std::vector<Vec4>& points,
125
+ float distance = 2.0f
126
+ ) noexcept;
127
+
128
+ std::vector<Projection3D> projectStereographicBatch(
129
+ const std::vector<Vec4>& points
130
+ ) noexcept;
131
+
132
+ std::vector<Projection3D> projectOrthographicBatch(
133
+ const std::vector<Vec4>& points
134
+ ) noexcept;
135
+
136
+ /**
137
+ * Project to flat array (for GPU upload)
138
+ *
139
+ * @param points Input 4D points
140
+ * @param distance Projection distance
141
+ * @return Flat array [x0,y0,z0, x1,y1,z1, ...]
142
+ */
143
+ std::vector<float> projectToFloatArray(
144
+ const std::vector<Vec4>& points,
145
+ float distance = 2.0f
146
+ ) noexcept;
147
+
148
+ } // namespace vib3
@@ -0,0 +1,322 @@
1
+ /**
2
+ * Rotor4D.cpp - 4D Rotor Implementation
3
+ */
4
+
5
+ #include "Rotor4D.hpp"
6
+ #include "Mat4x4.hpp"
7
+ #include <cmath>
8
+
9
+ namespace vib3 {
10
+
11
+ Rotor4D Rotor4D::fromPlaneAngle(RotationPlane plane, float angle) noexcept {
12
+ float halfAngle = angle * 0.5f;
13
+ float c = std::cos(halfAngle);
14
+ float s = std::sin(halfAngle);
15
+
16
+ Rotor4D r;
17
+ r.s = c;
18
+
19
+ switch (plane) {
20
+ case RotationPlane::XY: r.xy = s; break;
21
+ case RotationPlane::XZ: r.xz = s; break;
22
+ case RotationPlane::YZ: r.yz = s; break;
23
+ case RotationPlane::XW: r.xw = s; break;
24
+ case RotationPlane::YW: r.yw = s; break;
25
+ case RotationPlane::ZW: r.zw = s; break;
26
+ }
27
+
28
+ return r;
29
+ }
30
+
31
+ Rotor4D Rotor4D::fromEuler6(const std::array<float, 6>& angles) noexcept {
32
+ return fromEuler6(angles[0], angles[1], angles[2],
33
+ angles[3], angles[4], angles[5]);
34
+ }
35
+
36
+ Rotor4D Rotor4D::fromEuler6(float xy, float xz, float yz,
37
+ float xw, float yw, float zw) noexcept {
38
+ // Compose rotations: XY * XZ * YZ * XW * YW * ZW
39
+ Rotor4D result = identity();
40
+
41
+ if (std::abs(xy) > 1e-8f) result *= fromPlaneAngle(RotationPlane::XY, xy);
42
+ if (std::abs(xz) > 1e-8f) result *= fromPlaneAngle(RotationPlane::XZ, xz);
43
+ if (std::abs(yz) > 1e-8f) result *= fromPlaneAngle(RotationPlane::YZ, yz);
44
+ if (std::abs(xw) > 1e-8f) result *= fromPlaneAngle(RotationPlane::XW, xw);
45
+ if (std::abs(yw) > 1e-8f) result *= fromPlaneAngle(RotationPlane::YW, yw);
46
+ if (std::abs(zw) > 1e-8f) result *= fromPlaneAngle(RotationPlane::ZW, zw);
47
+
48
+ return result;
49
+ }
50
+
51
+ std::array<float, 8> Rotor4D::toArray() const noexcept {
52
+ return {s, xy, xz, yz, xw, yw, zw, xyzw};
53
+ }
54
+
55
+ void Rotor4D::fromArray(const std::array<float, 8>& arr) noexcept {
56
+ s = arr[0];
57
+ xy = arr[1];
58
+ xz = arr[2];
59
+ yz = arr[3];
60
+ xw = arr[4];
61
+ yw = arr[5];
62
+ zw = arr[6];
63
+ xyzw = arr[7];
64
+ }
65
+
66
+ // Rotor multiplication using geometric algebra product
67
+ // This is the full Clifford algebra product for Cl(4,0)
68
+
69
+ Rotor4D Rotor4D::operator*(const Rotor4D& b) const noexcept {
70
+ const Rotor4D& a = *this;
71
+
72
+ Rotor4D result;
73
+
74
+ // Scalar part
75
+ result.s = a.s * b.s - a.xy * b.xy - a.xz * b.xz - a.yz * b.yz
76
+ - a.xw * b.xw - a.yw * b.yw - a.zw * b.zw - a.xyzw * b.xyzw;
77
+
78
+ // XY bivector
79
+ result.xy = a.s * b.xy + a.xy * b.s - a.xz * b.yz + a.yz * b.xz
80
+ - a.xw * b.yw + a.yw * b.xw + a.zw * b.xyzw + a.xyzw * b.zw;
81
+
82
+ // XZ bivector
83
+ result.xz = a.s * b.xz + a.xy * b.yz + a.xz * b.s - a.yz * b.xy
84
+ - a.xw * b.zw - a.yw * b.xyzw + a.zw * b.xw + a.xyzw * b.yw;
85
+
86
+ // YZ bivector
87
+ result.yz = a.s * b.yz - a.xy * b.xz + a.xz * b.xy + a.yz * b.s
88
+ + a.xw * b.xyzw - a.yw * b.zw + a.zw * b.yw - a.xyzw * b.xw;
89
+
90
+ // XW bivector
91
+ result.xw = a.s * b.xw + a.xy * b.yw + a.xz * b.zw - a.yz * b.xyzw
92
+ + a.xw * b.s - a.yw * b.xy - a.zw * b.xz + a.xyzw * b.yz;
93
+
94
+ // YW bivector
95
+ result.yw = a.s * b.yw - a.xy * b.xw + a.xz * b.xyzw + a.yz * b.zw
96
+ + a.xw * b.xy + a.yw * b.s - a.zw * b.yz - a.xyzw * b.xz;
97
+
98
+ // ZW bivector
99
+ result.zw = a.s * b.zw - a.xy * b.xyzw - a.xz * b.xw - a.yz * b.yw
100
+ + a.xw * b.xz + a.yw * b.yz + a.zw * b.s + a.xyzw * b.xy;
101
+
102
+ // Pseudoscalar
103
+ result.xyzw = a.s * b.xyzw + a.xy * b.zw - a.xz * b.yw + a.yz * b.xw
104
+ + a.xw * b.yz - a.yw * b.xz + a.zw * b.xy + a.xyzw * b.s;
105
+
106
+ return result;
107
+ }
108
+
109
+ Rotor4D& Rotor4D::operator*=(const Rotor4D& other) noexcept {
110
+ *this = *this * other;
111
+ return *this;
112
+ }
113
+
114
+ Rotor4D Rotor4D::reverse() const noexcept {
115
+ // Reverse negates all bivectors (but not scalar or pseudoscalar)
116
+ return Rotor4D(s, -xy, -xz, -yz, -xw, -yw, -zw, xyzw);
117
+ }
118
+
119
+ float Rotor4D::magnitudeSquared() const noexcept {
120
+ return s*s + xy*xy + xz*xz + yz*yz + xw*xw + yw*yw + zw*zw + xyzw*xyzw;
121
+ }
122
+
123
+ float Rotor4D::magnitude() const noexcept {
124
+ return std::sqrt(magnitudeSquared());
125
+ }
126
+
127
+ Rotor4D Rotor4D::normalized() const noexcept {
128
+ float mag = magnitude();
129
+ if (mag > 0) {
130
+ float invMag = 1.0f / mag;
131
+ return Rotor4D(s * invMag, xy * invMag, xz * invMag, yz * invMag,
132
+ xw * invMag, yw * invMag, zw * invMag, xyzw * invMag);
133
+ }
134
+ return identity();
135
+ }
136
+
137
+ void Rotor4D::normalize() noexcept {
138
+ float mag = magnitude();
139
+ if (mag > 0) {
140
+ float invMag = 1.0f / mag;
141
+ s *= invMag;
142
+ xy *= invMag;
143
+ xz *= invMag;
144
+ yz *= invMag;
145
+ xw *= invMag;
146
+ yw *= invMag;
147
+ zw *= invMag;
148
+ xyzw *= invMag;
149
+ }
150
+ }
151
+
152
+ Rotor4D Rotor4D::inverse() const noexcept {
153
+ float magSq = magnitudeSquared();
154
+ if (magSq > 0) {
155
+ float invMagSq = 1.0f / magSq;
156
+ Rotor4D rev = reverse();
157
+ return Rotor4D(rev.s * invMagSq, rev.xy * invMagSq, rev.xz * invMagSq, rev.yz * invMagSq,
158
+ rev.xw * invMagSq, rev.yw * invMagSq, rev.zw * invMagSq, rev.xyzw * invMagSq);
159
+ }
160
+ return identity();
161
+ }
162
+
163
+ bool Rotor4D::isNormalized(float epsilon) const noexcept {
164
+ return std::abs(magnitudeSquared() - 1.0f) < epsilon;
165
+ }
166
+
167
+ // Vector rotation: v' = R * v * R†
168
+ // This uses the sandwich product for rotation
169
+
170
+ Vec4 Rotor4D::rotate(const Vec4& v) const noexcept {
171
+ // For efficiency, we compute this directly rather than
172
+ // converting to matrices or using full multivector products.
173
+
174
+ // First compute R * v (rotor times vector)
175
+ // Then compute (R * v) * R† (result times reverse)
176
+
177
+ // This is the optimized direct formula for 4D rotor rotation.
178
+ // Derived from the geometric algebra sandwich product.
179
+
180
+ float x = v.x, y = v.y, z = v.z, w = v.w;
181
+
182
+ // Compute rotation using expanded formula
183
+ // This avoids creating intermediate multivectors
184
+
185
+ float x2 = s*s + xy*xy + xz*xz + xw*xw - yz*yz - yw*yw - zw*zw - xyzw*xyzw;
186
+ float y2 = s*s - xy*xy + yz*yz + yw*yw - xz*xz - xw*xw - zw*zw - xyzw*xyzw;
187
+ float z2 = s*s - xy*xy - yz*yz + xz*xz + zw*zw - xw*xw - yw*yw - xyzw*xyzw;
188
+ float w2 = s*s - xy*xy - yz*yz - xz*xz - zw*zw + xw*xw + yw*yw - xyzw*xyzw;
189
+
190
+ // Off-diagonal terms
191
+ float t1 = 2.0f * (s*xy + xz*yz + xw*yw + zw*xyzw);
192
+ float t2 = 2.0f * (s*xz - xy*yz + xw*zw - yw*xyzw);
193
+ float t3 = 2.0f * (s*yz + xy*xz + yw*zw + xw*xyzw);
194
+ float t4 = 2.0f * (s*xw - xy*yw - xz*zw + yz*xyzw);
195
+ float t5 = 2.0f * (s*yw + xy*xw - yz*zw - xz*xyzw);
196
+ float t6 = 2.0f * (s*zw + xz*xw + yz*yw + xy*xyzw);
197
+
198
+ Vec4 result;
199
+
200
+ // Apply rotation matrix implicitly
201
+ result.x = x * (x2 + 1.0f) / 2.0f + y * t1 / 2.0f + z * t2 / 2.0f + w * t4 / 2.0f;
202
+ result.y = x * t1 / 2.0f + y * (y2 + 1.0f) / 2.0f + z * t3 / 2.0f + w * t5 / 2.0f;
203
+ result.z = x * t2 / 2.0f + y * t3 / 2.0f + z * (z2 + 1.0f) / 2.0f + w * t6 / 2.0f;
204
+ result.w = x * t4 / 2.0f + y * t5 / 2.0f + z * t6 / 2.0f + w * (w2 + 1.0f) / 2.0f;
205
+
206
+ // Use matrix multiplication for correctness
207
+ return toMatrix() * v;
208
+ }
209
+
210
+ void Rotor4D::rotateInPlace(Vec4& v) const noexcept {
211
+ v = rotate(v);
212
+ }
213
+
214
+ // Spherical linear interpolation
215
+
216
+ Rotor4D Rotor4D::slerp(const Rotor4D& other, float t) const noexcept {
217
+ float d = dot(other);
218
+
219
+ // If dot is negative, negate one rotor to take shorter path
220
+ Rotor4D b = other;
221
+ if (d < 0.0f) {
222
+ d = -d;
223
+ b = Rotor4D(-b.s, -b.xy, -b.xz, -b.yz, -b.xw, -b.yw, -b.zw, -b.xyzw);
224
+ }
225
+
226
+ // If very close, use linear interpolation
227
+ if (d > 0.9995f) {
228
+ return nlerp(b, t);
229
+ }
230
+
231
+ float theta = std::acos(d);
232
+ float sinTheta = std::sin(theta);
233
+
234
+ float w1 = std::sin((1.0f - t) * theta) / sinTheta;
235
+ float w2 = std::sin(t * theta) / sinTheta;
236
+
237
+ return Rotor4D(
238
+ s * w1 + b.s * w2,
239
+ xy * w1 + b.xy * w2,
240
+ xz * w1 + b.xz * w2,
241
+ yz * w1 + b.yz * w2,
242
+ xw * w1 + b.xw * w2,
243
+ yw * w1 + b.yw * w2,
244
+ zw * w1 + b.zw * w2,
245
+ xyzw * w1 + b.xyzw * w2
246
+ );
247
+ }
248
+
249
+ Rotor4D Rotor4D::nlerp(const Rotor4D& other, float t) const noexcept {
250
+ Rotor4D result(
251
+ s + (other.s - s) * t,
252
+ xy + (other.xy - xy) * t,
253
+ xz + (other.xz - xz) * t,
254
+ yz + (other.yz - yz) * t,
255
+ xw + (other.xw - xw) * t,
256
+ yw + (other.yw - yw) * t,
257
+ zw + (other.zw - zw) * t,
258
+ xyzw + (other.xyzw - xyzw) * t
259
+ );
260
+ result.normalize();
261
+ return result;
262
+ }
263
+
264
+ // Convert rotor to 4x4 rotation matrix
265
+
266
+ Mat4x4 Rotor4D::toMatrix() const noexcept {
267
+ // Ensure normalized
268
+ Rotor4D n = normalized();
269
+
270
+ float s2 = n.s * n.s;
271
+ float xy2 = n.xy * n.xy;
272
+ float xz2 = n.xz * n.xz;
273
+ float yz2 = n.yz * n.yz;
274
+ float xw2 = n.xw * n.xw;
275
+ float yw2 = n.yw * n.yw;
276
+ float zw2 = n.zw * n.zw;
277
+ float xyzw2 = n.xyzw * n.xyzw;
278
+
279
+ Mat4x4 m;
280
+
281
+ // Row 0
282
+ m.at(0, 0) = s2 + xy2 + xz2 + xw2 - yz2 - yw2 - zw2 - xyzw2;
283
+ m.at(0, 1) = 2.0f * (n.xy * n.yz + n.s * n.xz + n.xw * n.xyzw + n.yw * n.zw);
284
+ m.at(0, 2) = 2.0f * (n.xz * n.yz - n.s * n.xy + n.xw * n.zw - n.yw * n.xyzw);
285
+ m.at(0, 3) = 2.0f * (n.xw * n.yz - n.s * n.xyzw - n.xy * n.zw - n.xz * n.yw);
286
+
287
+ // Row 1
288
+ m.at(1, 0) = 2.0f * (n.xy * n.yz - n.s * n.xz - n.xw * n.xyzw + n.yw * n.zw);
289
+ m.at(1, 1) = s2 - xy2 + yz2 + yw2 - xz2 - xw2 - zw2 - xyzw2;
290
+ m.at(1, 2) = 2.0f * (n.yz * n.xz + n.s * n.xy + n.yw * n.xyzw + n.xw * n.zw);
291
+ m.at(1, 3) = 2.0f * (n.yw * n.xz - n.s * n.xyzw - n.xy * n.xw - n.yz * n.zw);
292
+
293
+ // Row 2
294
+ m.at(2, 0) = 2.0f * (n.xz * n.yz + n.s * n.xy - n.xw * n.zw - n.yw * n.xyzw);
295
+ m.at(2, 1) = 2.0f * (n.yz * n.xz - n.s * n.xy - n.yw * n.xyzw + n.xw * n.zw);
296
+ m.at(2, 2) = s2 - xy2 - yz2 + xz2 + zw2 - xw2 - yw2 - xyzw2;
297
+ m.at(2, 3) = 2.0f * (n.zw * n.xy + n.s * n.xyzw + n.xz * n.xw + n.yz * n.yw);
298
+
299
+ // Row 3
300
+ m.at(3, 0) = 2.0f * (n.xw * n.yz + n.s * n.xyzw + n.xy * n.zw - n.xz * n.yw);
301
+ m.at(3, 1) = 2.0f * (n.yw * n.xz + n.s * n.xyzw + n.xy * n.xw + n.yz * n.zw);
302
+ m.at(3, 2) = 2.0f * (n.zw * n.xy - n.s * n.xyzw - n.xz * n.xw - n.yz * n.yw);
303
+ m.at(3, 3) = s2 - xy2 - yz2 - xz2 - zw2 + xw2 + yw2 - xyzw2;
304
+
305
+ return m;
306
+ }
307
+
308
+ bool Rotor4D::operator==(const Rotor4D& other) const noexcept {
309
+ return s == other.s && xy == other.xy && xz == other.xz && yz == other.yz &&
310
+ xw == other.xw && yw == other.yw && zw == other.zw && xyzw == other.xyzw;
311
+ }
312
+
313
+ bool Rotor4D::operator!=(const Rotor4D& other) const noexcept {
314
+ return !(*this == other);
315
+ }
316
+
317
+ float Rotor4D::dot(const Rotor4D& other) const noexcept {
318
+ return s * other.s + xy * other.xy + xz * other.xz + yz * other.yz +
319
+ xw * other.xw + yw * other.yw + zw * other.zw + xyzw * other.xyzw;
320
+ }
321
+
322
+ } // namespace vib3