code wiki / _hdl_build / nx_geo_clip_gate.nx
nx_geo_clip_gate.nx source
↩ module page · 77 lines · 3278 B
1// nx_geo_clip_gate.nx -- GATE for GEO-015 polygon intersection (Sutherland-Hodgman). Subject = the
2// 4e6-microdeg square [0,4e6]^2. Three clips prove the three code paths, correctness checked by
3// REUSING GEO-010 geo_area2 on the clipped result (no hard-coded vertex order):
4// overlap : clip by [2e6,6e6]^2 -> overlap square [2e6,4e6]^2, doubled area = 8e12 (4 verts)
5// disjoint : clip by [10e6,12e6]^2 -> no overlap -> 0 verts
6// contains : clip by [-10e6,10e6]^2 -> subject unchanged, doubled area = 3.2e13
7//
8// Evidence -> knowledge/status/geo_clip.log (GEOCLIPGATE authored=organ ... verdict=GREEN).
9// license_tier: ORIGINAL
10import "nx_geo_clip.nx"
11import "nx_geo_area.nx"
12import "nx_syscalls.nx"
13
14const GC_LOG: *u8 = "knowledge/status/geo_clip.log"
15
16func gc_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 }
17func gc_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 }
18
19func gc_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 }
20
21// fill a CCW axis-aligned square [lo,hi]^2 (in both lat and lon) into p (4 verts).
22func gc_square(p: *i64, lo: i64, hi: i64) -> i64 {
23 gc_set(p, 0, lo, lo)
24 gc_set(p, 1, lo, hi)
25 gc_set(p, 2, hi, hi)
26 gc_set(p, 3, hi, lo)
27 return 0
28}
29
30func gc_emit(fd: i64, no: i64, ao: i64, nd: i64, ncn: i64, ac: i64, ok: i64) -> i64 {
31 gc_w(fd, "GEOCLIPGATE authored=organ method=sutherland-hodgman overlap_nverts=" as *u8); gc_wn(fd, no)
32 gc_w(fd, " overlap_area2=" as *u8); gc_wn(fd, ao)
33 gc_w(fd, " disjoint_nverts=" as *u8); gc_wn(fd, nd)
34 gc_w(fd, " contains_nverts=" as *u8); gc_wn(fd, ncn)
35 gc_w(fd, " contains_area2=" as *u8); gc_wn(fd, ac)
36 if ok == 1 { gc_w(fd, " verdict=GREEN\n" as *u8) } else { gc_w(fd, " verdict=RED\n" as *u8) }
37 return 0
38}
39
40func main() -> i64 {
41 let subj: *i64 = sys_mmap(8 * 8) as *i64
42 gc_square(subj, 0, 4000000)
43
44 // overlap
45 let clipO: *i64 = sys_mmap(8 * 8) as *i64
46 gc_square(clipO, 2000000, 6000000)
47 let outO: *i64 = sys_mmap(8 * 40) as *i64
48 let no: i64 = geo_poly_clip(subj, 4, clipO, 4, outO)
49 let ao: i64 = geo_area2(outO, no)
50
51 // disjoint
52 let clipD: *i64 = sys_mmap(8 * 8) as *i64
53 gc_square(clipD, 10000000, 12000000)
54 let outD: *i64 = sys_mmap(8 * 40) as *i64
55 let nd: i64 = geo_poly_clip(subj, 4, clipD, 4, outD)
56
57 // contains
58 let clipC: *i64 = sys_mmap(8 * 8) as *i64
59 gc_square(clipC, 0 - 10000000, 10000000)
60 let outC: *i64 = sys_mmap(8 * 40) as *i64
61 let ncn: i64 = geo_poly_clip(subj, 4, clipC, 4, outC)
62 let ac: i64 = geo_area2(outC, ncn)
63
64 var ok: i64 = 1
65 if no != 4 { ok = 0 }
66 if ao != 8000000000000 { ok = 0 }
67 if nd != 0 { ok = 0 }
68 if ncn != 4 { ok = 0 }
69 if ac != 32000000000000 { ok = 0 }
70
71 gc_emit(1, no, ao, nd, ncn, ac, ok)
72 let lf: i64 = sys_openat_append(GC_LOG, 420)
73 if lf >= 0 { gc_emit(lf, no, ao, nd, ncn, ac, ok); sys_close(lf) }
74
75 if ok == 1 { return 0 }
76 return 1
77}