nx_sdfprim_lib.nx
buildroot/runtime/nx_sdfprim_lib.nx
about
nx_sdfprim_lib.nx -- IMPLICIT PRIMITIVES FOR PARAMETRIC ANATOMY FIELDS: integer-millimetre geometry in, a signed
distance at SP_FQ units per millimetre out (negative = inside), composed by smooth union and subtraction.
Extracted VERBATIM from nx_skullsdf (anatomy AN17, 2026-09-19; the emitted skull mesh is byte-identical before and
after the move, proven by sha256) so an arm, a foot, a muscle belly or a soft-tissue layer composes the SAME
ellipsoid, capsule, swept arch, pitched plate, walled cylinder and element ring the skull is built from -- and a
primitive fixed here is fixed for every field at once instead of in one organ.
SIBLING FAMILIES, DECLARED (nx_spendgate listed them; this is the adjudication, not a silent fourth copy):
nx_sdf.nx the Q14-millimetre CSG algebra (sphere, box, cylinder, torus, rounded box, smooth booleans),
nx_sdfrender.nx the renderer's op-table evaluator (sdf_ellip, sdf_ellip_rot, sdf_lipsweep ...) shaded per pixel.
They differ in UNITS (Q14 mm vs integer mm in / SP_FQ out) and in EVALUATION MODEL (op tables vs a composed function
the polygonizer and the tuner call millions of times). Consolidating the three under one evaluator is a declared
ecosystem rung; until it lands, this file is the family a POLYGONIZED, TUNED field uses.
UNITS. Points and geometry are integer millimetres. Every *_q primitive returns distance * SP_FQ; sp_smin's k is in
the same output units (a caller passes k_mm * SP_FQ). The dimensionless ratios inside the ellipsoid estimator are
carried at SP_Q, and its k1 term at SP_Q squared: at plain SP_Q that term truncates to ZERO for a small offset on a
large axis (10*1024/100^2 = 1), which quantised the distance into concentric steps and left the polygonized surface
ringed and mottled (measured in the skull lane before the extraction).
THE ELLIPSOID IS A FIRST-ORDER ESTIMATOR, ON PURPOSE -- MEASURED, NOT PREFERRED. d = k0 (k0 - 1) / k1 with
k0 = |q/r| and k1 = |q/r^2| is exact on a sphere and accurate near an ellipsoid's surface. A Newton refinement
(walk d0 along the unit normal g/|g|, re-estimate, add) was tried in the skull lane and LOST at every damping:
field fidelity by the `grad` verb, total deviation outside +/-20 percent of a true SDF, FIRST-ORDER 402 permil,
undamped Newton 550, half-damped 425; undamped also pushed the mean |grad| from 7 percent LOW to 13 percent HIGH
(927 -> 1126) and the max from 1870 to 2121. The reason is worth more than the attempt: the refinement re-evaluates
the SAME first-order formula at the stepped point, so it inherits that formula's bias -- ITERATING AN APPROXIMATION
WITH ITSELF CONVERGES TO THE APPROXIMATION'S FIXED POINT, NOT TO THE TRUTH. A real fix needs an exact ellipsoid
residual (bisection on the Lagrange multiplier), never another pass of the same estimator.
LIB, no main. license_tier: ORIGINAL. No hw writes (Rule 26).
dependencies 1 imports · 12 importers
diagram shows first 10 each side; +0 more imports, +2 more importers in the complete lists below.
imports: nx_syscalls.nx
imported by: nx_fieldfit_gate.nxnx_fieldfit_verified_canonical_gate_t333.nxnx_fieldfit_verified_gate_t333.nxnx_fieldfit_verified_r2_gate_t333.nxnx_fieldfit_verified_r3_gate_t333.nxnx_fieldfit_verified_r4_gate_t333.nxnx_sdfprim_gate.nxnx_skullsdf.nxnx_skullsdf_region_gate_t334.nxnx_skullsdf_verified_candidate_t333.nxnx_skullsdf_verified_r3_t333.nxnx_skullsdf_verified_r4_t333.nx
structs
| none |
consts
| 33 | const SP_Q: i64 = 1024 // fixed point for the dimensionless ratios inside the estimators |
| 34 | const SP_FQ: i64 = 256 // output units per millimetre: the sub-millimetre lane |
| 35 | const SP_PERMILLE: i64 = 1000 // the sine tables' unit, and the unit of a per-mille policy knob |
| 36 | const SP_LERP_ONE: i64 = 1024 // a capsule's parameter t, Q10 |
| 37 | const SP_INF_MM: i64 = 300 // farther than any point in a 240 mm box: where an element-min starts |
| 38 | const SP_ARCH_SEG: i64 = 12 // capsule segments a swept arch is polygonised into |
| 41 | const SP_BHASKARA_NUM_PERMILLE: i64 = 4000 |
| 42 | const SP_BHASKARA_DEN: i64 = 40500 |
| 44 | const SP_RING_N_MIN: i64 = 4 |
| 45 | const SP_RING_N_MAX: i64 = 20 |
| 46 | const SP_RING_TH_MIN: i64 = 20 |
| 47 | const SP_RING_TH_MAX: i64 = 88 |
| 48 | const SP_ARCH_TH_MIN: i64 = 10 |
| 49 | const SP_ARCH_TH_MAX: i64 = 90 |
functions
| 51 | func sp_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 } |
| 52 | func sp_abs(v: i64) -> i64 { if v<0 { return 0-v } return v } called by 1: sp_smin |
| 53 | func sp_min(a: i64, b: i64) -> i64 { if a<b { return a } return b } |
| 54 | func sp_max(a: i64, b: i64) -> i64 { if a>b { return a } return b } |
| 57 | func sp_isin(a: i64) -> i64 |
| 67 | func sp_icos(a: i64) -> i64 |
| 74 | func sp_ellipsoid_q(px: i64, py: i64, pz: i64, cx: i64, cy: i64, cz: i64, rx: i64, ry: i64, rz: i64) -> i64 called by 8: mainsp_ring_qsp_ellipsoid_tiltx_qss_field_p_qss_field_p_qss_field_p_q+2 calls 2: sp_isqrtsp_min |
| 97 | func sp_capsule_q(px: i64, py: i64, pz: i64, ax: i64, ay: i64, az: i64, bx: i64, by: i64, bz: i64, r: i64) -> i64 |
| 113 | func sp_smin(a: i64, b: i64, k: i64) -> i64 |
| 120 | func sp_sub(a: i64, b: i64) -> i64 { return sp_max(a, 0-b) } |
| 126 | func sp_ring_q(px: i64, py: i64, pz: i64, ty: i64, tz0: i64, ax: i64, az: i64, thmax: i64, n: i64, tw: i64, tht: i64, tprof: i64) -> i64 called by 6: mainss_field_p_qss_field_p_qss_field_p_qss_field_p_qss_field_p_q calls 3: sp_isinsp_icossp_ellipsoid_q |
| 153 | func sp_arch_q(px: i64, py: i64, pz: i64, cy: i64, cz: i64, ax: i64, az: i64, thmax: i64, ylift: i64, r: i64) -> i64 called by 6: mainss_field_p_qss_field_p_qss_field_p_qss_field_p_qss_field_p_q calls 3: sp_isinsp_icossp_capsule_q |
| 184 | func sp_ellipsoid_tiltx_q(px: i64, py: i64, pz: i64, cx: i64, cy: i64, cz: i64, rx: i64, ry: i64, rz: i64, tilt: i64) -> i64 called by 6: mainss_field_p_qss_field_p_qss_field_p_qss_field_p_qss_field_p_q calls 3: sp_isinsp_icossp_ellipsoid_q |
| 196 | func sp_cylz_q(px: i64, py: i64, pz: i64, cx: i64, cy: i64, rx: i64, ry: i64, zback: i64) -> i64 called by 6: mainss_field_p_qss_field_p_qss_field_p_qss_field_p_qss_field_p_q calls 3: sp_isqrtsp_minsp_max |