code wiki / _hdl_build / nx_geo_join_gate.nx

nx_geo_join_gate.nx source

↩ module page · 67 lines · 2927 B

1// nx_geo_join_gate.nx -- GATE for GEO-013 spatial join. Pool of 2 disjoint squares (A near origin, 2// B far away) + 4 query points. Proves each point joins to the correct polygon (exact, deterministic): 3// Q0 center of A -> 0 4// Q1 center of B -> 1 5// Q2 between, outside -> -1 6// Q3 inside A by edge -> 0 7// 8// Evidence -> knowledge/status/geo_join.log (GEOJOINGATE authored=organ ... verdict=GREEN). 9// license_tier: ORIGINAL 10import "nx_geo_join.nx" 11import "nx_syscalls.nx" 12 13const GJN_LOG: *u8 = "knowledge/status/geo_join.log" 14 15func gjn_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 16func gjn_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 } 17 18func gjn_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 } 19 20func gjn_emit(fd: i64, j0: i64, j1: i64, j2: i64, j3: i64, ok: i64) -> i64 { 21 gjn_w(fd, "GEOJOINGATE authored=organ method=integer-raycast-join q0=" as *u8); gjn_wn(fd, j0) 22 gjn_w(fd, " q1=" as *u8); gjn_wn(fd, j1) 23 gjn_w(fd, " q2=" as *u8); gjn_wn(fd, j2) 24 gjn_w(fd, " q3=" as *u8); gjn_wn(fd, j3) 25 if ok == 1 { gjn_w(fd, " verdict=GREEN\n" as *u8) } else { gjn_w(fd, " verdict=RED\n" as *u8) } 26 return 0 27} 28 29func main() -> i64 { 30 // pool: polygon A (idx 0..3) = square lon/lat 0..2e6; polygon B (idx 4..7) = square lon/lat 10e6..12e6. 31 let pool: *i64 = sys_mmap(8 * 16) as *i64 32 gjn_set(pool, 0, 0, 0) 33 gjn_set(pool, 1, 0, 2000000) 34 gjn_set(pool, 2, 2000000, 2000000) 35 gjn_set(pool, 3, 2000000, 0) 36 gjn_set(pool, 4, 10000000, 10000000) 37 gjn_set(pool, 5, 10000000, 12000000) 38 gjn_set(pool, 6, 12000000, 12000000) 39 gjn_set(pool, 7, 12000000, 10000000) 40 41 let off: *i64 = sys_mmap(8 * 2) as *i64 42 off[0] = 0; off[1] = 4 43 let cnt: *i64 = sys_mmap(8 * 2) as *i64 44 cnt[0] = 4; cnt[1] = 4 45 46 let pts: *i64 = sys_mmap(8 * 8) as *i64 47 gjn_set(pts, 0, 1000000, 1000000) // center of A -> 0 48 gjn_set(pts, 1, 11000000, 11000000) // center of B -> 1 49 gjn_set(pts, 2, 5000000, 5000000) // outside both -> -1 50 gjn_set(pts, 3, 1000000, 1900000) // inside A by edge -> 0 51 52 let out: *i64 = sys_mmap(8 * 4) as *i64 53 geo_spatial_join(pts, 4, pool, off, cnt, 2, out) 54 55 var ok: i64 = 1 56 if out[0] != 0 { ok = 0 } 57 if out[1] != 1 { ok = 0 } 58 if out[2] != (0 - 1) { ok = 0 } 59 if out[3] != 0 { ok = 0 } 60 61 gjn_emit(1, out[0], out[1], out[2], out[3], ok) 62 let lf: i64 = sys_openat_append(GJN_LOG, 420) 63 if lf >= 0 { gjn_emit(lf, out[0], out[1], out[2], out[3], ok); sys_close(lf) } 64 65 if ok == 1 { return 0 } 66 return 1 67}