code wiki / _hdl_build / nx_geo_area_gate.nx
nx_geo_area_gate.nx source
↩ module page · 100 lines · 5203 B
1// nx_geo_area_gate.nx -- GATE for GEO-010 polygon area + orientation (integer-exact shoelace). Proves:
2// square CCW : 1deg x 1deg square (side 1e6) -> signed doubled area = +2e12, orientation = +1
3// square CW : same square reversed -> signed doubled area = -2e12, orientation = -1
4// triangle : legs 2e6, area 2e12 -> signed doubled area = +4e12
5// degenerate : 3 collinear points -> doubled area EXACTLY 0, orientation = 0 (the exceed)
6// micro-apex : base 2e6, apex 1 microdeg up -> doubled area EXACTLY 2e6 (1-microdeg resolution)
7// All comparisons are EXACT equality (no tolerance): the shoelace is pure-integer, so the answers are
8// reproducible to the bit -- the property float libs cannot promise on these edge cases.
9//
10// Evidence -> knowledge/status/geo_area.log (GEOAREAGATE authored=organ ... verdict=GREEN).
11// license_tier: ORIGINAL
12import "nx_geo_area.nx"
13import "nx_syscalls.nx"
14import "nx_gate_verdict.nx"
15
16const GA_LOG: *u8 = "knowledge/status/geo_area.log"
17
18func ga_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 }
19func ga_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 }
20
21func ga_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 }
22
23func ga_emit(fd: i64, sq: i64, sqo: i64, sqcw: i64, sqcwo: i64, tri: i64, deg: i64, dego: i64, mic: i64, ok: i64) -> i64 {
24 ga_w(fd, "GEOAREAGATE authored=organ exact=integer-shoelace sq_ccw_area2=" as *u8); ga_wn(fd, sq)
25 ga_w(fd, " sq_ccw_orient=" as *u8); ga_wn(fd, sqo)
26 ga_w(fd, " sq_cw_area2=" as *u8); ga_wn(fd, sqcw)
27 ga_w(fd, " sq_cw_orient=" as *u8); ga_wn(fd, sqcwo)
28 ga_w(fd, " tri_area2=" as *u8); ga_wn(fd, tri)
29 ga_w(fd, " degenerate_area2=" as *u8); ga_wn(fd, deg)
30 ga_w(fd, " degenerate_orient=" as *u8); ga_wn(fd, dego)
31 ga_w(fd, " microapex_area2=" as *u8); ga_wn(fd, mic)
32 if ok == 1 { ga_w(fd, " verdict=GREEN\n" as *u8) } else { ga_w(fd, " verdict=RED\n" as *u8) }
33 return 0
34}
35
36func main() -> i64 {
37 // CCW square: (0,0)->(0,1e6)->(1e6,1e6)->(1e6,0) [lat,lon]; in (x=lon,y=lat) = right,up,left,down = CCW.
38 let sq: *i64 = sys_mmap(8 * 8) as *i64
39 ga_set(sq, 0, 0, 0)
40 ga_set(sq, 1, 0, 1000000)
41 ga_set(sq, 2, 1000000, 1000000)
42 ga_set(sq, 3, 1000000, 0)
43 let sq_area: i64 = geo_signed_area2(sq, 4) // expect +2e12
44 let sq_orient: i64 = geo_orientation(sq, 4) // expect +1
45
46 // same square, vertex order reversed -> clockwise.
47 let cw: *i64 = sys_mmap(8 * 8) as *i64
48 ga_set(cw, 0, 1000000, 0)
49 ga_set(cw, 1, 1000000, 1000000)
50 ga_set(cw, 2, 0, 1000000)
51 ga_set(cw, 3, 0, 0)
52 let cw_area: i64 = geo_signed_area2(cw, 4) // expect -2e12
53 let cw_orient: i64 = geo_orientation(cw, 4) // expect -1
54
55 // right triangle legs 2e6 -> area 2e12, doubled 4e12. CCW.
56 let tri: *i64 = sys_mmap(8 * 6) as *i64
57 ga_set(tri, 0, 0, 0)
58 ga_set(tri, 1, 0, 2000000)
59 ga_set(tri, 2, 2000000, 0)
60 let tri_area: i64 = geo_signed_area2(tri, 3) // expect +4e12
61
62 // degenerate: 3 collinear points (same lat). Float libs can emit tiny noise; we are EXACTLY 0.
63 let deg: *i64 = sys_mmap(8 * 6) as *i64
64 ga_set(deg, 0, 0, 0)
65 ga_set(deg, 1, 0, 1000000)
66 ga_set(deg, 2, 0, 2000000)
67 let deg_area: i64 = geo_signed_area2(deg, 3) // expect 0
68 let deg_orient: i64 = geo_orientation(deg, 3) // expect 0
69
70 // micro-apex: base lon 0..2e6 at lat 0, apex 1 microdeg up -> exact doubled area 2e6.
71 let mic: *i64 = sys_mmap(8 * 6) as *i64
72 ga_set(mic, 0, 0, 0)
73 ga_set(mic, 1, 0, 2000000)
74 ga_set(mic, 2, 1, 1000000)
75 let mic_area: i64 = geo_signed_area2(mic, 3) // expect +2e6
76
77 var ok: i64 = 1
78 if sq_area != 2000000000000 { ok = 0 }
79 if sq_orient != 1 { ok = 0 }
80 if cw_area != (0 - 2000000000000) { ok = 0 }
81 if cw_orient != (0 - 1) { ok = 0 }
82 if tri_area != 4000000000000 { ok = 0 }
83 if deg_area != 0 { ok = 0 }
84 if deg_orient != 0 { ok = 0 }
85 if mic_area != 2000000 { ok = 0 }
86
87 ga_emit(1, sq_area, sq_orient, cw_area, cw_orient, tri_area, deg_area, deg_orient, mic_area, ok)
88 let lf: i64 = sys_openat_append(GA_LOG, 420)
89 if lf >= 0 { ga_emit(lf, sq_area, sq_orient, cw_area, cw_orient, tri_area, deg_area, deg_orient, mic_area, ok); sys_close(lf) }
90
91 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
92 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
93 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
94 let ctr__dry: *i64 = gv_ctr()
95 ctr__dry[0] = ok
96 ctr__dry[1] = 1
97 let rc__dry: i64 = gv_verdict("GEO-AREA-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
98 sys_exit(rc__dry)
99 return rc__dry
100}