code wiki / (root) / nx_garment_strap_gate_t138.nx

nx_garment_strap_gate_t138.nx source

↩ module page · 32 lines · 5091 B

1import "nx_garment_boundary_t138.nx" 2func gs_check(ok:i64,name:*u8,counts:*i64)->i64{counts[0]=counts[0]+1;if ok==1{gmg_puts("PASS ");gmg_puts(name);gmg_puts("\n");return 0};counts[1]=counts[1]+1;gmg_puts("FAIL ");gmg_puts(name);gmg_puts("\n");return 1} 3func gs_area(v:*i64,t:*i64,i:i64)->i64{let a:i64=t[i*3]*5;let b:i64=t[i*3+1]*5;let c:i64=t[i*3+2]*5;let x:i64=v[b]-v[a];let y:i64=v[b+1]-v[a+1];let z:i64=v[b+2]-v[a+2];let u:i64=v[c]-v[a];let w:i64=v[c+1]-v[a+1];let q:i64=v[c+2]-v[a+2];if y*q-z*w==0&&z*u-x*q==0&&x*w-y*u==0{return 0};return 1} 4func main(argc:i64,argv:*i64)->i64{ 5 if argc!=2&&argc!=3{gmg_puts("usage: gate <existing exact served NXA> [exclusive private output]\n");return 2} 6 let fd:i64=sys_openat_rd(argv[1] as *u8);if fd<0{return 3};let bytes:i64=sys_lseek(fd,0,2);if bytes<=0{sys_close(fd);return 3};if sys_lseek(fd,0,0)<0{sys_close(fd);return 3} 7 let b:*u8=sys_mmap(bytes) as *u8;if (b as i64)<=0{sys_close(fd);return 3};var got:i64=0;while got<bytes{let r:i64=sys_read(fd,((b as i64)+got) as *u8,bytes-got);if r<=0{sys_close(fd);return 3};got=got+r};if sys_close(fd)<0{return 3} 8 let counts:*i64=sys_mmap(16) as *i64;if (counts as i64)<=0{return 3};counts[0]=0;counts[1]=0 9 let G:*i64=gmg_load(b,bytes);if (G as i64)<=0{return 4} 10 let sec:i64=nxa_find(b,bytes,nxa_tag4("GARM"));if sec<0{return 4};let old:*i64=((b as i64)+sec*8) as *i64;var directory:i64=-1;var k:i64=0;while k<old[1]{let d:i64=8+k*old[5];if old[d]==3{directory=d};k=k+1};if directory<0{return 4} 11 let ov:*i64=((old as i64)+old[directory+3]*8) as *i64;let ot:*i64=((old as i64)+old[directory+4]*8) as *i64;let onv:i64=old[directory+1];let ont:i64=old[directory+2] 12 var oldzero:i64=0;var olddeg:i64=0;var i:i64=0;while i<onv{if ov[i*5+4]==GMG_F_STRAP{if i+1>=onv{return 4};let dx:i64=ov[i*5]-ov[(i+1)*5];let dy:i64=ov[i*5+1]-ov[(i+1)*5+1];let dz:i64=ov[i*5+2]-ov[(i+1)*5+2];if dx==0&&dy==0&&dz==0{oldzero=oldzero+1};i=i+1};i=i+1} 13 i=0;while i<ont{if ov[ot[i*3]*5+4]==GMG_F_STRAP&&ov[ot[i*3+1]*5+4]==GMG_F_STRAP&&ov[ot[i*3+2]*5+4]==GMG_F_STRAP{if gs_area(ov,ot,i)==0{olddeg=olddeg+1}};i=i+1} 14 gs_check(oldzero==2,"served fixture reproduces two zero-width shoulder stations",counts);gs_check(olddeg>0,"served fixture reproduces degenerate strap triangles",counts) 15 let stats:*i64=sys_mmap(GMG_STAT_N*8) as *i64;if (stats as i64)<=0{return 3};let result:i64=gmg_build(G,3,stats);gs_check(result>0,"kind3 regenerates successfully",counts);if result<=0{return 5} 16 let v:*i64=G[GMG_C_OV] as *i64;let t:*i64=G[GMG_C_OT] as *i64;let nv:i64=G[GMG_C_ONV];let nt:i64=G[GMG_C_ONT];gs_check(nv==onv&&nt==ont,"vertex and triangle contract unchanged",counts);if nv!=onv||nt!=ont{return 5} 17 var unchanged:i64=1;var source:i64=1;var centers:i64=1;var width:i64=1;var zero:i64=0;var deg:i64=0;var direction:i64=1;var previous0:i64=0;var previous1:i64=0;var previous2:i64=0;var station:i64=0 18 i=0;while i<nv{if v[i*5+4]!=GMG_F_STRAP{k=0;while k<5{if v[i*5+k]!=ov[i*5+k]{unchanged=0};k=k+1}}else{ 19 if i+1>=nv{return 5};if v[i*5+3]!=ov[i*5+3]||v[(i+1)*5+3]!=ov[(i+1)*5+3]{source=0} 20 k=0;while k<3{if v[i*5+k]+v[(i+1)*5+k]!=ov[i*5+k]+ov[(i+1)*5+k]{centers=0};k=k+1} 21 let dx:i64=v[i*5]-v[(i+1)*5];let dy:i64=v[i*5+1]-v[(i+1)*5+1];let dz:i64=v[i*5+2]-v[(i+1)*5+2];let length:i64=gmg_isqrt(dx*dx+dy*dy+dz*dz);if length==0{zero=zero+1} 22 if gmg_abs(length-2*gmg_um2u(GMG_STRAP_HALFW_DRESS_UM))>2*3{width=0} 23 if station%(GMG_STRAP_K*2+1)!=0{if dx*previous0+dy*previous1+dz*previous2<0{direction=0}};previous0=dx;previous1=dy;previous2=dz;station=station+1;i=i+1 24 };i=i+1} 25 gs_check(unchanged,"all non-strap vertex records byte identical",counts);gs_check(source,"strap source-body influences preserved",counts);gs_check(centers,"strap centerline and endpoint anchors preserved",counts);gs_check(zero==0,"no zero-width strap stations",counts);gs_check(width,"all strap widths respect fabric width within integer rounding",counts);gs_check(direction,"continuous ribbon edge orientation",counts) 26 i=0;var indices:i64=1;while i<nt*3{if t[i]!=ot[i]{indices=0};i=i+1};gs_check(indices,"all triangle indices unchanged",counts) 27 i=0;while i<nt{if v[t[i*3]*5+4]==GMG_F_STRAP&&v[t[i*3+1]*5+4]==GMG_F_STRAP&&v[t[i*3+2]*5+4]==GMG_F_STRAP{if gs_area(v,t,i)==0{deg=deg+1}};i=i+1};gs_check(deg==0,"no degenerate strap triangles",counts) 28 gmg_puts("STRAP old_zero=");gmg_pn(oldzero);gmg_puts(" new_zero=");gmg_pn(zero);gmg_puts(" old_degenerate=");gmg_pn(olddeg);gmg_puts(" new_degenerate=");gmg_pn(deg);gmg_puts("\nGATE total=");gmg_pn(counts[0]);gmg_puts(" fail=");gmg_pn(counts[1]);gmg_puts("\n");if counts[1]!=0{return 1};if argc==3{ 29 let nw:i64=old[6];if nw<GMG_HDR||nw>(bytes-sec*8)/8{return 6};let copy:*i64=sys_mmap(nw*8) as *i64;if (copy as i64)<=0{return 6};i=0;while i<nw{copy[i]=old[i];i=i+1};i=0;while i<nv*5{copy[old[directory+3]+i]=v[i];i=i+1} 30 let emit:i64=gmg_write_nxa_checked_v1(b,bytes,copy,nw,argv[2] as *u8);sys_munmap(copy as *u8,nw*8);if emit<0{gmg_puts("PRIVATE-ASSET REFUSED rc=");gmg_pn(emit);gmg_puts("\n");return 6};gmg_puts("PRIVATE-ASSET WRITTEN; mandatory independent readback before use\n") 31 };return 0 32}