code wiki / (root) / nx_sdfprim_lib.nx

nx_sdfprim_lib.nx

buildroot/runtime/nx_sdfprim_lib.nx

11530 B216 linesdepth 2pulls 2 transitivereach 12 importersview sourcekind library
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_sdfprim_lib.nx nx_fieldfit_gate.nx nx_fieldfit_verified_canonical_gat nx_fieldfit_verified_gate_t333.nx nx_fieldfit_verified_r2_gate_t333. nx_fieldfit_verified_r3_gate_t333. nx_fieldfit_verified_r4_gate_t333. nx_sdfprim_gate.nx nx_skullsdf.nx nx_skullsdf_region_gate_t334.nx nx_skullsdf_verified_candidate_t33

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

33const SP_Q: i64 = 1024 // fixed point for the dimensionless ratios inside the estimators
34const SP_FQ: i64 = 256 // output units per millimetre: the sub-millimetre lane
35const SP_PERMILLE: i64 = 1000 // the sine tables' unit, and the unit of a per-mille policy knob
36const SP_LERP_ONE: i64 = 1024 // a capsule's parameter t, Q10
37const SP_INF_MM: i64 = 300 // farther than any point in a 240 mm box: where an element-min starts
38const SP_ARCH_SEG: i64 = 12 // capsule segments a swept arch is polygonised into
41const SP_BHASKARA_NUM_PERMILLE: i64 = 4000
42const SP_BHASKARA_DEN: i64 = 40500
44const SP_RING_N_MIN: i64 = 4
45const SP_RING_N_MAX: i64 = 20
46const SP_RING_TH_MIN: i64 = 20
47const SP_RING_TH_MAX: i64 = 88
48const SP_ARCH_TH_MIN: i64 = 10
49const SP_ARCH_TH_MAX: i64 = 90

functions

51func 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 }
52func sp_abs(v: i64) -> i64 { if v<0 { return 0-v } return v }
called by 1: sp_smin
53func sp_min(a: i64, b: i64) -> i64 { if a<b { return a } return b }
54func sp_max(a: i64, b: i64) -> i64 { if a>b { return a } return b }
called by 2: sp_subsp_cylz_q
57func sp_isin(a: i64) -> i64
67func sp_icos(a: i64) -> i64
74func sp_ellipsoid_q(px: i64, py: i64, pz: i64, cx: i64, cy: i64, cz: i64, rx: i64, ry: i64, rz: i64) -> i64
97func sp_capsule_q(px: i64, py: i64, pz: i64, ax: i64, ay: i64, az: i64, bx: i64, by: i64, bz: i64, r: i64) -> i64
113func sp_smin(a: i64, b: i64, k: i64) -> i64
120func sp_sub(a: i64, b: i64) -> i64 { return sp_max(a, 0-b) }
126func 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
153func sp_arch_q(px: i64, py: i64, pz: i64, cy: i64, cz: i64, ax: i64, az: i64, thmax: i64, ylift: i64, r: i64) -> i64
184func 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
196func sp_cylz_q(px: i64, py: i64, pz: i64, cx: i64, cy: i64, rx: i64, ry: i64, zback: i64) -> i64