gaussian-splat-lite 1.3.2 → 1.4.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.
@@ -0,0 +1,166 @@
1
+ // LOD math adapted from MIT-licensed code. See THIRD_PARTY_LICENSES.md.
2
+ const float = new Float32Array(1);
3
+ const bits = new Uint32Array(float.buffer);
4
+ export const HALF = new Float32Array(65536);
5
+ export const SCALE = new Float32Array(65536);
6
+ for (let i = 0; i < 65536; i++) {
7
+ const exponent = (i >> 10) & 31;
8
+ const fraction = i & 1023;
9
+ const sign = i & 32768 ? -1 : 1;
10
+ HALF[i] =
11
+ exponent === 31
12
+ ? fraction
13
+ ? Number.NaN
14
+ : sign * Number.POSITIVE_INFINITY
15
+ : sign *
16
+ (exponent ? 1 + fraction / 1024 : fraction / 1024) *
17
+ 2 ** (exponent ? exponent - 15 : -14);
18
+ SCALE[i] = Math.exp(HALF[i]);
19
+ }
20
+
21
+ export function halfBits(value) {
22
+ float[0] = value;
23
+ const word = bits[0];
24
+ const sign = (word >>> 16) & 32768;
25
+ const exponent = (word >>> 23) & 255;
26
+ let mantissa = word & 0x7fffff;
27
+ if (exponent === 255) return sign | (mantissa ? 0x7e00 : 0x7c00);
28
+ const e = exponent - 112;
29
+ if (e >= 31) return sign | 0x7c00;
30
+ if (e <= 0) {
31
+ if (e < -10) return sign;
32
+ mantissa |= 0x800000;
33
+ const shift = 14 - e;
34
+ return (
35
+ sign |
36
+ ((mantissa + (2 ** (shift - 1) - 1) + ((mantissa >>> shift) & 1)) >>>
37
+ shift)
38
+ );
39
+ }
40
+ return (
41
+ sign | ((e << 10) + ((mantissa + 4095 + ((mantissa >>> 13) & 1)) >>> 13))
42
+ );
43
+ }
44
+ export function half(value) {
45
+ return HALF[halfBits(value)];
46
+ }
47
+ export function area(x, y, z) {
48
+ return (
49
+ 4 *
50
+ Math.PI *
51
+ (((x * y) ** 1.6075 + (x * z) ** 1.6075 + (y * z) ** 1.6075) / 3) **
52
+ (1 / 1.6075)
53
+ );
54
+ }
55
+ export function opacityRadius(opacity) {
56
+ return opacity > 1 ? Math.sqrt(1 + Math.E * Math.log(opacity)) : 1;
57
+ }
58
+
59
+ export function rotation(record, output) {
60
+ const x = record[10];
61
+ const y = record[11];
62
+ const z = record[12];
63
+ const w = record[13];
64
+ output[0] = 1 - 2 * (y * y + z * z);
65
+ output[1] = 2 * (x * y + z * w);
66
+ output[2] = 2 * (x * z - y * w);
67
+ output[3] = 2 * (x * y - z * w);
68
+ output[4] = 1 - 2 * (x * x + z * z);
69
+ output[5] = 2 * (y * z + x * w);
70
+ output[6] = 2 * (x * z + y * w);
71
+ output[7] = 2 * (y * z - x * w);
72
+ output[8] = 1 - 2 * (x * x + y * y);
73
+ }
74
+
75
+ export function covariance(record, output, r) {
76
+ rotation(record, r);
77
+ const x = SCALE[halfBits(record[7])] ** 2;
78
+ const y = SCALE[halfBits(record[8])] ** 2;
79
+ const z = SCALE[halfBits(record[9])] ** 2;
80
+ output[0] = r[0] * r[0] * x + r[3] * r[3] * y + r[6] * r[6] * z;
81
+ output[1] = r[1] * r[1] * x + r[4] * r[4] * y + r[7] * r[7] * z;
82
+ output[2] = r[2] * r[2] * x + r[5] * r[5] * y + r[8] * r[8] * z;
83
+ output[3] = r[0] * r[1] * x + r[3] * r[4] * y + r[6] * r[7] * z;
84
+ output[4] = r[0] * r[2] * x + r[3] * r[5] * y + r[6] * r[8] * z;
85
+ output[5] = r[1] * r[2] * x + r[4] * r[5] * y + r[7] * r[8] * z;
86
+ }
87
+
88
+ export function determinant(m) {
89
+ return (
90
+ m[0] * (m[1] * m[2] - m[5] * m[5]) -
91
+ m[3] * (m[3] * m[2] - m[5] * m[4]) +
92
+ m[4] * (m[3] * m[5] - m[1] * m[4])
93
+ );
94
+ }
95
+
96
+ // Jacobi diagonalization of a symmetric covariance. All scratch arrays are
97
+ // reused by the caller; merging millions of pairs does not allocate JS objects.
98
+ export function eigens(m, matrix, vectors, order) {
99
+ matrix[0] = m[0];
100
+ matrix[4] = m[1];
101
+ matrix[8] = m[2];
102
+ matrix[1] = matrix[3] = m[3];
103
+ matrix[2] = matrix[6] = m[4];
104
+ matrix[5] = matrix[7] = m[5];
105
+ vectors.fill(0);
106
+ vectors[0] = vectors[4] = vectors[8] = 1;
107
+ // A relative tolerance keeps merged orientations independent of scene units.
108
+ const eps = 1e-6 * (Math.abs(m[0]) + Math.abs(m[1]) + Math.abs(m[2]));
109
+ for (let k = 0; k < 32; k++) {
110
+ if (matrix[1] ** 2 + matrix[2] ** 2 + matrix[5] ** 2 <= eps ** 2) break;
111
+ let p = 0;
112
+ let q = 1;
113
+ if (Math.abs(matrix[2]) > Math.abs(matrix[1])) q = 2;
114
+ if (Math.abs(matrix[5]) > Math.abs(matrix[p * 3 + q])) {
115
+ p = 1;
116
+ q = 2;
117
+ }
118
+ const apq = matrix[p * 3 + q];
119
+ if (Math.abs(apq) <= eps) break;
120
+ const app = matrix[p * 3 + p];
121
+ const aqq = matrix[q * 3 + q];
122
+ const tau = (aqq - app) / (2 * apq);
123
+ const t = (tau < 0 ? -1 : 1) / (Math.abs(tau) + Math.sqrt(1 + tau * tau));
124
+ const c = 1 / Math.sqrt(1 + t * t);
125
+ const s = t * c;
126
+ const r = 3 - p - q;
127
+ const arp = matrix[r * 3 + p];
128
+ const arq = matrix[r * 3 + q];
129
+ matrix[p * 3 + p] = app - t * apq;
130
+ matrix[q * 3 + q] = aqq + t * apq;
131
+ matrix[r * 3 + p] = matrix[p * 3 + r] = c * arp - s * arq;
132
+ matrix[r * 3 + q] = matrix[q * 3 + r] = s * arp + c * arq;
133
+ matrix[p * 3 + q] = matrix[q * 3 + p] = 0;
134
+ for (let row = 0; row < 3; row++) {
135
+ const a = vectors[row * 3 + p];
136
+ const b = vectors[row * 3 + q];
137
+ vectors[row * 3 + p] = c * a - s * b;
138
+ vectors[row * 3 + q] = s * a + c * b;
139
+ }
140
+ }
141
+ order[0] = 0;
142
+ order[1] = 1;
143
+ order[2] = 2;
144
+ order.sort((a, b) => matrix[b * 3 + b] - matrix[a * 3 + a]);
145
+ }
146
+
147
+ export function quaternion(r, out) {
148
+ const trace = r[0] + r[4] + r[8];
149
+ if (trace > 0) {
150
+ const s = Math.sqrt(trace + 1) * 2;
151
+ out[10] = (r[5] - r[7]) / s;
152
+ out[11] = (r[6] - r[2]) / s;
153
+ out[12] = (r[1] - r[3]) / s;
154
+ out[13] = s * 0.25;
155
+ } else {
156
+ let i = r[0] > r[4] ? 0 : 1;
157
+ if (r[8] > r[i * 3 + i]) i = 2;
158
+ const j = (i + 1) % 3;
159
+ const k = (i + 2) % 3;
160
+ const s = Math.sqrt(1 + r[i * 3 + i] - r[j * 3 + j] - r[k * 3 + k]) * 2;
161
+ out[10 + i] = s * 0.25;
162
+ out[10 + j] = (r[i * 3 + j] + r[j * 3 + i]) / s;
163
+ out[10 + k] = (r[i * 3 + k] + r[k * 3 + i]) / s;
164
+ out[13] = (r[j * 3 + k] - r[k * 3 + j]) / s;
165
+ }
166
+ }