code wiki / _hdl_build / nx_geo_simplify_gate.nx

nx_geo_simplify_gate.nx source

↩ module page · 66 lines · 3113 B

1// nx_geo_simplify_gate.nx -- GATE for GEO-014 line simplify (Douglas-Peucker, integer). A 5-vertex 2// polyline along y=0 with a single 1e6-microdeg bump at the middle vertex. Three tolerances prove the 3// keep/drop decision tracks eps deterministically: 4// eps = 5e5 -> keep {P0,P2,P4} (count 3): the bump (perp 1e6) survives, the two flat vertices (perp 5// ~447213 < eps) are dropped 6// eps = 2e6 -> keep {P0,P4} (count 2): even the bump is below tolerance -> straight line 7// eps = 1e5 -> keep all 5 (count 5): every vertex exceeds the tiny tolerance 8// Output order/indices are checked exactly for the eps=5e5 case ([0,2,4]). 9// 10// Evidence -> knowledge/status/geo_simplify.log (GEOSIMPGATE authored=organ ... verdict=GREEN). 11// license_tier: ORIGINAL 12import "nx_geo_simplify.nx" 13import "nx_syscalls.nx" 14 15const GS_LOG: *u8 = "knowledge/status/geo_simplify.log" 16 17func gs_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 } 18func gs_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 } 19 20func gs_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 } 21 22func gs_emit(fd: i64, c1: i64, o0: i64, o1: i64, o2: i64, c2: i64, c3: i64, ok: i64) -> i64 { 23 gs_w(fd, "GEOSIMPGATE authored=organ method=integer-douglas-peucker eps5e5_count=" as *u8); gs_wn(fd, c1) 24 gs_w(fd, " kept=[" as *u8); gs_wn(fd, o0); gs_w(fd, "," as *u8); gs_wn(fd, o1); gs_w(fd, "," as *u8); gs_wn(fd, o2) 25 gs_w(fd, "] eps2e6_count=" as *u8); gs_wn(fd, c2) 26 gs_w(fd, " eps1e5_count=" as *u8); gs_wn(fd, c3) 27 if ok == 1 { gs_w(fd, " verdict=GREEN\n" as *u8) } else { gs_w(fd, " verdict=RED\n" as *u8) } 28 return 0 29} 30 31func main() -> i64 { 32 // polyline (x=lon,y=lat): (0,0),(1e6,0),(2e6,1e6 bump),(3e6,0),(4e6,0). 33 let pts: *i64 = sys_mmap(8 * 10) as *i64 34 gs_set(pts, 0, 0, 0) 35 gs_set(pts, 1, 0, 1000000) 36 gs_set(pts, 2, 1000000, 2000000) 37 gs_set(pts, 3, 0, 3000000) 38 gs_set(pts, 4, 0, 4000000) 39 40 let out: *i64 = sys_mmap(8 * 5) as *i64 41 let c1: i64 = geo_simplify(pts, 5, 500000, out) // expect 3, [0,2,4] 42 let o0: i64 = out[0] 43 let o1: i64 = out[1] 44 let o2: i64 = out[2] 45 46 let out2: *i64 = sys_mmap(8 * 5) as *i64 47 let c2: i64 = geo_simplify(pts, 5, 2000000, out2) // expect 2 48 49 let out3: *i64 = sys_mmap(8 * 5) as *i64 50 let c3: i64 = geo_simplify(pts, 5, 100000, out3) // expect 5 51 52 var ok: i64 = 1 53 if c1 != 3 { ok = 0 } 54 if o0 != 0 { ok = 0 } 55 if o1 != 2 { ok = 0 } 56 if o2 != 4 { ok = 0 } 57 if c2 != 2 { ok = 0 } 58 if c3 != 5 { ok = 0 } 59 60 gs_emit(1, c1, o0, o1, o2, c2, c3, ok) 61 let lf: i64 = sys_openat_append(GS_LOG, 420) 62 if lf >= 0 { gs_emit(lf, c1, o0, o1, o2, c2, c3, ok); sys_close(lf) } 63 64 if ok == 1 { return 0 } 65 return 1 66}