public/math/reference.mjs
与本次数学页面一起发布的源码快照。SHA-256: b50c72b5e2812604e48f9bd565d51a490db2598a792fa9d7519895df76b83baf
1// Teaching examples for OTHER representations. Not called by the STG-Lite pass.
2// Inputs use ordinary arrays and double precision; no model/checkpoint loader.
3function normalize(v) {
4 const length = Math.hypot(...v);
5 if (!(length > 0)) throw new Error('Cannot normalize a zero vector');
6 return v.map(x => x / length);
7}
8
9export function decodeDeformation(base, deltaPosition, deltaLogScale, deltaQuaternion) {
10 // Example choice of heads; not a claim about every deformation implementation.
11 return {
12 position: base.position.map((v, i) => v + deltaPosition[i]),
13 scale: base.logScale.map((v, i) => Math.exp(v + deltaLogScale[i])),
14 quaternion: normalize(base.quaternion.map((v, i) => v + deltaQuaternion[i])),
15 };
16}
17
18export function condition4D(meanSpace, meanTime, A, b, c, time) {
19 if (!(c > 0)) throw new Error('Time variance must be positive');
20 // Caller supplies a positive-definite joint covariance.
21 const dt = time - meanTime;
22 return {
23 mean: meanSpace.map((v, i) => v + b[i] * dt / c),
24 covariance: A.map((row, i) => row.map((v, j) => v - b[i] * b[j] / c)),
25 timeWeight: Math.exp(-0.5 * dt * dt / c),
26 };
27}
28
29export function evaluateSH1(coefficients, direction) {
30 // Four RGB coefficients, Graphdeco real-SH signs/order. Raw sum, no +0.5/clamp.
31 const [x, y, z] = normalize(direction);
32 const c0 = 1 / Math.sqrt(4 * Math.PI);
33 const c1 = Math.sqrt(3 / (4 * Math.PI));
34 const basis = [c0, -c1 * y, c1 * z, -c1 * x];
35 return [0, 1, 2].map(channel =>
36 basis.reduce((sum, value, index) => sum + value * coefficients[index][channel], 0));
37}
38
39export function compositeFeatures(alphas, features, background) {
40 // Near to far; any feature dimension, including ordinary RGB.
41 const result = background.map(() => 0);
42 let transmittance = 1;
43 for (let i = 0; i < alphas.length; i++) {
44 for (let d = 0; d < result.length; d++) {
45 result[d] += transmittance * alphas[i] * features[i][d];
46 }
47 transmittance *= 1 - alphas[i];
48 }
49 return result.map((v, d) => v + transmittance * background[d]);
50}
51