Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

4.2 KB
Raw Blame
// SPDX-License-Identifier: MIT
// Writes src/sim/tables.ts: the fixed inverse-CDF lookup tables the twin draws its random variates from
// (sim-spec 8: ECMAScript leaves Math.log and Math.exp implementation-approximated, so the sim never calls
// them; these tables are computed once here and stored as literals). Both tables are normalised to a mean
// of exactly 1 over their 1024 midpoint quantiles:
//   EXPONENTIAL[i] = -ln(1 - u_i) / mean,            u_i = (i + 0.5) / 1024
//   LOGNORMAL_05[i] = exp(0.5 z_i - 0.125) / mean,    z_i = Phi^-1(u_i) (shape 0.5, mean 1)
// Run from the scenes root: ./scripts/toolchain-run.ps1 packages/foundry-floor/scripts/build-tables.ts [--check]
import { readFileSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const N = 1024;

/** Wichura's AS241 (PPND16): the standard normal quantile to about 1e-16. */
function normalQuantile(p: number): number {
  const q = p - 0.5;
  if (Math.abs(q) <= 0.425) {
    const r = 0.180625 - q * q;
    return q * (((((((2509.0809287301226727 * r + 33430.575583588128105) * r + 67265.770927008700853) * r + 45921.953931549871457) * r + 13731.693765509461125) * r + 1971.5909503065514427) * r + 133.14166789178437745) * r + 3.387132872796366608)
      / (((((((5226.495278852545925 * r + 28729.085735721942674) * r + 39307.89580009271061) * r + 21213.794301586595867) * r + 5394.1960214247511077) * r + 687.1870074920579083) * r + 42.313330701600911252) * r + 1);
  }
  let r = q < 0 ? p : 1 - p;
  r = Math.sqrt(-Math.log(r));
  let v: number;
  if (r <= 5) {
    r -= 1.6;
    v = (((((((7.7454501427834140764e-4 * r + 0.0227238449892691845833) * r + 0.24178072517745061177) * r + 1.27045825245236838258) * r + 3.64784832476320460504) * r + 5.7694972214606914055) * r + 4.6303378461565452959) * r + 1.42343711074968357734)
      / (((((((1.05075007164441684324e-9 * r + 5.475938084995344946e-4) * r + 0.0151986665636164571966) * r + 0.14810397642748007459) * r + 0.68976733498510000455) * r + 1.6763848301838038494) * r + 2.05319162663775882187) * r + 1);
  } else {
    r -= 5;
    v = (((((((2.01033439929228813265e-7 * r + 2.71155556874348757815e-5) * r + 0.0012426609473880784386) * r + 0.026532189526576123093) * r + 0.29656057182850489123) * r + 1.7848265399172913358) * r + 5.4637849111641143699) * r + 6.6579046435011037772)
      / (((((((2.04426310338993978564e-15 * r + 1.4215117583164458887e-7) * r + 1.8463183175100546818e-5) * r + 7.868691311456132591e-4) * r + 0.0148753612908506148525) * r + 0.13692988092273580531) * r + 0.59983220655588793769) * r + 1);
  }
  return q < 0 ? -v : v;
}
function table(f: (u: number) => number): number[] {
  const raw = Array.from({ length: N }, (_, i) => f((i + 0.5) / N)), mean = raw.reduce((s, x) => s + x, 0) / N;
  return raw.map(x => Number((x / mean).toPrecision(12)));
}
const exponential = table(u => -Math.log(1 - u));
const lognormal = table(u => Math.exp(0.5 * normalQuantile(u) - 0.125));
const format = (name: string, values: number[], doc: string) => `/** ${doc} */\nexport const ${name}: readonly number[] = [\n${Array.from({ length: N / 8 }, (_, r) => '  ' + values.slice(r * 8, r * 8 + 8).join(', ') + ',').join('\n')}\n];\n`;
const text = `// SPDX-License-Identifier: MIT
// Generated by scripts/build-tables.ts. Do not edit: fixed inverse-CDF lookup tables (sim-spec 8), so the
// twin never calls Math.log or Math.exp and every engine draws identical variates.
export const TABLE_SIZE = ${N};
${format('EXPONENTIAL', exponential, 'Standard exponential quantiles at the 1024 midpoints, normalised to mean 1.')}
${format('LOGNORMAL_05', lognormal, 'Lognormal (shape 0.5) quantiles at the 1024 midpoints, normalised to mean 1.')}`;
if (import.meta.main) {
  const path = resolve(ROOT, 'src/sim/tables.ts');
  if (process.argv.includes('--check')) { let old = ''; try { old = readFileSync(path, 'utf8'); } catch { /* missing */ } if (old !== text) { console.log('stale: src/sim/tables.ts'); process.exit(1); } console.log('tables: current'); }
  else { writeFileSync(path, text); console.log(JSON.stringify({ size: N, exponentialMax: exponential[N - 1], lognormalRange: [lognormal[0], lognormal[N - 1]] })); }
}