nx_skinfit_lib.nx
buildroot/runtime/nx_skinfit_lib.nx
about
nx_skinfit_lib.nx -- RECOVER MELANIN AND HAEMOGLOBIN FROM A MEASURED REFLECTANCE (2026-09-03)
WHY THIS SHAPE, AND WHY NOT A DIFFERENTIABLE RENDERER. Inverse rendering in general is a 10^5..10^6
parameter problem behind a renderer, and the estate's Gauss-Newton solvers (nx_pnprefine, nx_multical) are
built for 6-7 well-conditioned DOF with analytic residuals -- they do not transfer, and pointing them at a
renderer is the measured-a-different-subject defect wearing a solver's clothes. So the problem is
DECOMPOSED until no general solver is needed: the illuminant is closed form (cs_cat_*), and the chromophore
fit -- here -- is TWO parameters against THREE channels, which a deterministic coarse-to-fine grid search
inverts exactly and repeatably, with no gradients, no autograd, and no float.
WHY IT IS IDENTIFIABLE. One reflectance and two unknowns is underdetermined. Three channels give three
equations for two unknowns, and the fit is well posed precisely BECAUSE the two absorbers have different
spectral shapes -- melanin rising toward blue, haemoglobin peaking in green. If a caller supplies
proportional coefficient vectors the problem degenerates, and the RESIDUAL is what says so. The gate
carries that degenerate case as a NEG-CONTROL and measures the collapse rather than asserting it cannot
happen: with proportional coefficients the fit puts everything on one axis and misses by 0.30 and 0.60.
THE RESIDUAL IS PART OF THE ANSWER, NEVER AN INTERNAL. A fitter that returns its best guess without saying
how badly it missed will happily report chromophores for a measurement no skin could produce. sf_fit writes
the residual beside the parameters so a caller can refuse; a fit is a measurement with an error bar or it
is a fabrication.
PROVEN 8/8 GREEN 2026-09-03 by round trip: worst recovery error 2.2e-4 over a spread of true pairs with a
zero residual on every one, the degenerate case correctly missing, and an unreachable measurement coming
back with a residual of 0.5 against a floor of 0.002.
license_tier: ORIGINAL No hw writes (Rule 26). LIB (no main).
dependencies 3 imports · 1 importers
imports: nx_syscalls.nxnx_fixq30_lib.nxnx_kubelka_lib.nx
imported by: nx_skinfit_gate.nx
structs
| none |
consts
| 31 | const SF_REFUSED: i64 = 0 - 1 |
| 32 | const SF_CH: i64 = 3 |
| 33 | const SF_SLOT: i64 = 8 |
| 36 | const SF_GRID: i64 = 16 |
| 37 | const SF_PASSES: i64 = 4 |
| 38 | const SF_O_MEL: i64 = 0 |
| 39 | const SF_O_HEM: i64 = 1 |
| 40 | const SF_O_RESID: i64 = 2 |
| 41 | const SF_O_SLOTS: i64 = 3 |
functions
| 45 | func sf_forward(kbase: *i64, kmel: *i64, khem: *i64, s: *i64, cmel: i64, chem: i64, out3: *i64) -> i64 |
| 57 | func sf_sse(a: *i64, b: *i64) -> i64 |
| 70 | func sf_fit(kbase: *i64, kmel: *i64, khem: *i64, s: *i64, meas3: *i64, out: *i64) -> i64 |