code wiki / (root) / nx_fieldfit_verified_r3_t333.nx

nx_fieldfit_verified_r3_t333.nx

buildroot/runtime/nx_fieldfit_verified_r3_t333.nx

21323 B503 linesdepth 3pulls 3 transitivereach 3 importersview sourcekind librarytopic fieldfit
docsdependenciesstructsconstsfunctions

about

nx_fieldfit_lib.nx -- FIT A PARAMETRIC IMPLICIT FIELD TO A SCANNED ORACLE. The machinery nx_skullsdf's `tune` grew (anatomy AN17, 2026-09-18/19), extracted so any anatomy field -- a skull, an arm, a foot, a muscle belly, a tissue layer -- is fitted by ONE optimiser with ONE objective and ONE fit-file contract, and a defect fixed here is fixed for every field at once. The consumer hands over three functions as VALUES (NishiLang func-pointer locals and fields, indirect calls up to six arguments): field(px, py, pz, a, b, P) -> signed distance at `fq` units per millimetre (negative inside) for parameter vector P, with a and b the consumer's two policy knobs (the skull passes sex and robusticity) region(x, y, z) -> 0 .. nreg-1, the named anatomical region a point belongs to live(k) -> 1 for a station the field reads, 0 for a retired one (the descent skips it: two evaluations spent on a dead station are a pass that cannot move anything) THE OBJECTIVE IS TWO-SIDED, AND IT HAS TO BE. A one-directional score -- how far is each ORACLE point from my surface -- is trivially gamed by DELETING GEOMETRY: the skull's first tuning run shrank its orbits from 22 mm to 8 and its maxilla from 30 mm to 13, improving the number 40 percent by filling in the eye sockets, because surface that does not exist cannot be wrong. The REVERSE term charges every lattice point on OUR zero level its distance to the nearest oracle sample, so a deleted feature is EXPENSIVE by construction. (The reverse term uses the nearest oracle VERTEX from a spatial hash: an upper bound on the true point-to-surface distance -- it can overstate, never understate, the safe direction for a penalty.) THE OBJECTIVE IS REGION-BALANCED. A plain mean is dominated by whatever region has the most samples (the skull's vault and base held 9,335 + 8,672 of ~31,000, so a 49 mm muzzle and 4 mm drilled orbits read as a 4 mm skull and the surface objective fell 629 -> 531 while the chin moved +4 -> +19.6 mm). Each side is the mean of the PER-REGION means (a region with no samples abstains), so the smallest region weighs what the largest does. No weight is typed: the population decides. THE DESCENT is coordinate descent: per live station try +step then -step, keep a move only if the MEASURED objective falls; the step halves per pass, floor 1. No change is ever kept on the strength of looking better. THE FIT FILE is newline-separated integers, loaded as a PREFIX: a file shorter than the vector applies to its first entries and the rest keep their defaults, so growing a parametrisation never discards banked convergence; a file shorter than `minlen` changes nothing (parsed into a temp and applied only when long enough, so a truncated write cannot half-poison a field). Written whole by ff_save. UNITS: objectives are reported in centi-millimetres; `lat` (the reverse lattice spacing), `ext` (the half-extent the hash covers) and `dim` (cells per axis) are the consumer's, taken from the resource it fits. LIB, no main. license_tier: ORIGINAL. No hw writes (Rule 26).

dependencies 2 imports · 3 importers

nx_syscalls.nx nx_atomic_rewrite.nx nx_fieldfit_verified_r3_t333.n nx_fieldfit_verified_r3_gate_t333. nx_skullsdf_region_gate_t334.nx nx_skullsdf_verified_r4_t333.nx

imports: nx_syscalls.nxnx_atomic_rewrite.nx

imported by: nx_fieldfit_verified_r3_gate_t333.nxnx_skullsdf_region_gate_t334.nxnx_skullsdf_verified_r4_t333.nx

structs

51struct FfCtx { field: func(i64,i64,i64,i64,i64,*i64) -> i64, region: func(i64,i64,i64) -> i64, live: func(i64) -> i64, RX: *i64, RY: *i64, RZ: *i64, RG: *i64, n: i64, head: *i64, next: *i64, ext: i64, dim: i64, lat: i64, a: i64, b: i64, fq: i64, nreg: i64, sum: *i64, cnt: *i64, np: i64, cap: i64, extra: func(*i64) -> i64, has_extra: i64 }
371struct FfFitContract { header: *u8, header_bytes: i64, np: i64, valid: func(*i64,i64) -> i64 }

consts

36const FF_I64: i64 = 8
37const FF_CENTI: i64 = 100 // objectives are centi-millimetres
38const FF_CTX_BYTES: i64 = 256 // FfCtx: 21 fields of 8 bytes, one page-rounded map
39const FF_LOAD_HDR: i64 = 16 // the two-word length record sys_read_file fills
40const FF_SAVE_DIGITS: i64 = 16 // bytes reserved per integer on save (sign + 19 digits fits a page-rounded map)
41const FF_SAVE_SLACK: i64 = 64
42const FF_MODE644: i64 = 420
362const FF_FIT_LEGACY_UNVERIFIED: i64 = 0 - 2
363const FF_FIT_CONTRACT_MISMATCH: i64 = 0 - 3
364const FF_FIT_MALFORMED: i64 = 0 - 4
365const FF_FIT_INVALID: i64 = 0 - 5
366const FF_FIT_IO: i64 = 0 - 6
367const FF_FIT_MAGIC_BYTES: i64 = 7
368const FF_FIT_I64_DIGITS: i64 = 21
369const FF_FIT_ATOMIC_PATH_CAP: i64 = 512 // nx_atomic_rewrite ar_syncdir buffer contract
370const FF_FIT_I64_POS_LIMIT: i64 = 9223372036854775807

functions

54func ff_ctx_new(np: i64, nreg: i64, cap: i64, ext: i64, dim: i64, lat: i64, fq: i64) -> *FfCtx
called by 3: mainss_tuness_tune calls 1: sys_mmap
70func ff_cell(c: *FfCtx, x: i64, y: i64, z: i64) -> i64
called by 1: ff_bin
85func ff_bin(c: *FfCtx) -> i64
called by 3: mainss_tuness_tune calls 1: ff_cell
101func ff_near(c: *FfCtx, x: i64, y: i64, z: i64) -> i64
called by 2: mainff_score_rev calls 1: ff_isqrt
146func ff_isqrt(v: i64) -> i64 { if v<=0 { return 0 } var x: i64=v; var y: i64=(x+1)/2; while y<x { x=y; y=(x+v/x)/2 } return x }
called by 1: ff_near
150func ff_regions(c: *FfCtx) -> i64
called by 3: mainss_tuness_tune
157func ff_rtab_reset(c: *FfCtx) -> i64
163func ff_rtab_mean(c: *FfCtx, fq: i64) -> i64
175func ff_score_fwd(c: *FfCtx, P: *i64) -> i64
190func ff_score_rev(c: *FfCtx, P: *i64) -> i64
220func ff_score2(c: *FfCtx, P: *i64) -> i64
229func ff_live_count(c: *FfCtx) -> i64
called by 3: mainss_tuness_tune
235func ff_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
236func ff_pn(v: i64) -> i64
244func ff_descend(c: *FfCtx, P: *i64, passes: i64, step0: i64, verbose: i64) -> i64
called by 1: main calls 3: ff_score2ff_putsff_pn
279func ff_wint(b: *u8, o: i64, v: i64) -> i64
298func ff_load_prefix(path: *u8, P: *i64, np: i64, minlen: i64) -> i64
343func ff_save(path: *u8, P: *i64, np: i64) -> i64
374func ff_contract_ok(C: *FfFitContract) -> i64
386func ff_load_verified(path: *u8, P: *i64, C: *FfFitContract) -> i64
446func ff_save_verified(path: *u8, P: *i64, C: *FfFitContract) -> i64
464func ff_descend_checked(c: *FfCtx, P: *i64, passes: i64, step0: i64, verbose: i64, valid: func(*i64,i64) -> i64) -> i64