code wiki / _hdl_build / nx_camera_anim_gate.nx
nx_camera_anim_gate.nx source
↩ module page · 207 lines · 9816 B
1// nx_camera_anim_gate.nx -- ★R4 of the Infinigen ladder: CAMERA RIG + ANIMATION + EXACT OPTICAL-FLOW GT (fills
2// the animation GAP and CLOSES the R2 flow residual). A positionable/aimable camera (cam=[dx,dy,dz,yaw]) renders
3// distinct frames = a trajectory; because our renderer KNOWS every pixel's 3D world point (ray*depth), reprojecting
4// it through the next camera yields DENSE, EXACT optical flow -- the label real footage can't give and Infinigen's
5// data is prized for. Two world renders (survives the box).
6// T1 CAMERA RIG: two cam poses render substantially different frames (a real move, not a terrain reseed)
7// T2 ★EXACT FLOW + PARALLAX: reproject A's world points through cam B -> dense flow; high coverage; COHERENT
8// (neighbour flow similar); ★NEAR terrain flows MORE than FAR (parallax = the flow encodes 3D, the killer check)
9// T3 WARP PROOF: A sampled at the flowed location matches real B far better than the no-flow baseline (the flow
10// lands on the same surface) + panel knowledge/nx_flow_panel.png (frameA | flow-vis | warped | realB)
11// license_tier: ORIGINAL expect_exit: 0
12import "nx_syscalls.nx"
13import "nx_png.nx"
14import "nx_worldgen.nx"
15
16func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
17func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 }
18func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
19func cL1(a: i64, b: i64) -> i64 { var s: i64=(a&255)-(b&255); if s<0{s=0-s} var g: i64=((a>>8)&255)-((b>>8)&255); if g<0{g=0-g} var c: i64=((a>>16)&255)-((b>>16)&255); if c<0{c=0-c} return s+g+c }
20
21func main() -> i64 {
22 hw("=== nx_camera_anim_gate -- R4: camera rig + exact optical-flow GT ===\n" as *u8)
23 var fails: i64 = 0
24 let W: i64 = wg_w(); let H: i64 = wg_h(); let npx: i64 = W*H
25 let seed: i64 = 314
26 let base: i64 = wg_cam_base_y(seed)
27
28 // camera A (base view) and camera B (dolly forward + strafe + rise + pan) = a trajectory keyframe pair
29 let camA: *i64 = sys_mmap(4*8) as *i64 // [0,0,0,0]
30 let camB: *i64 = sys_mmap(4*8) as *i64
31 camB[0] = 360; camB[1] = 90; camB[2] = 1150; camB[3] = 460 // +x strafe, +y rise, +z dolly, +yaw pan (it4096)
32
33 let fbA: *i64 = sys_mmap(npx*8) as *i64
34 let dA: *i64 = sys_mmap(npx*8) as *i64
35 let sA: *i64 = sys_mmap(npx*8) as *i64
36 let nA: *i64 = sys_mmap(npx*8) as *i64
37 let fbB: *i64 = sys_mmap(npx*8) as *i64
38 let dB: *i64 = sys_mmap(npx*8) as *i64
39 let sB: *i64 = sys_mmap(npx*8) as *i64
40 let nB: *i64 = sys_mmap(npx*8) as *i64
41 worldgen_render_full(seed, 0, 0, camA, fbA, dA, sA, nA)
42 worldgen_render_full(seed, 0, 0, camB, fbB, dB, sB, nB)
43
44 // ---- T1 camera rig moved the view ----
45 var diff: i64 = 0
46 var i: i64 = 0
47 while i < npx { diff = diff + cL1(fbA[i], fbB[i]); i = i + 1 }
48 hw(" rig: sum colour L1(A,B) = "); pn(diff); hw("\n" as *u8)
49 var t1: i64 = 0
50 if diff > 20000000 { t1 = 1 }
51 if t1 == 1 { hw("T1 PASS the camera rig produced a distinct frame (real trajectory keyframe)\n" as *u8) }
52 else { fails=fails+1; hw("T1 FAIL frames too similar\n" as *u8) }
53
54 // ---- T2 exact flow by reprojection + parallax ----
55 let flowx: *i64 = sys_mmap(npx*8) as *i64
56 let flowy: *i64 = sys_mmap(npx*8) as *i64
57 let valid: *i64 = sys_mmap(npx*8) as *i64
58 let ray: *i64 = sys_mmap(4*8) as *i64
59 let prj: *i64 = sys_mmap(4*8) as *i64
60 var nvalid: i64 = 0
61 var nsurf: i64 = 0
62 var nearmag: i64 = 0; var nearn: i64 = 0
63 var farmag: i64 = 0; var farn: i64 = 0
64 // mean depth over surface pixels (split point for parallax)
65 var dsum: i64 = 0
66 var py: i64 = 0
67 while py < H {
68 var px: i64 = 0
69 while px < W {
70 let pi: i64 = py*W+px
71 let d: i64 = dA[pi]
72 if d < 1000000000 { dsum = dsum + d; nsurf = nsurf + 1 }
73 px = px + 1
74 }
75 py = py + 1
76 }
77 var dmean: i64 = 1
78 if nsurf > 0 { dmean = dsum / nsurf }
79 py = 0
80 while py < H {
81 var px: i64 = 0
82 while px < W {
83 let pi: i64 = py*W+px
84 valid[pi] = 0
85 let d: i64 = dA[pi]
86 if d < 1000000000 {
87 wg_ray(px, py, 0, ray) // camA yaw = 0
88 let Px: i64 = 0 + ray[0]*d/1024 // camA position = (0, base, 0)
89 let Py: i64 = base + ray[1]*d/1024
90 let Pz: i64 = 0 + ray[2]*d/1024
91 wg_project(seed, camB, Px, Py, Pz, prj)
92 if prj[2] > 0 {
93 let fx: i64 = prj[0] - px
94 let fy: i64 = prj[1] - py
95 flowx[pi] = fx; flowy[pi] = fy; valid[pi] = 1
96 nvalid = nvalid + 1
97 let mag: i64 = iabs(fx) + iabs(fy)
98 if d < dmean { nearmag = nearmag + mag; nearn = nearn + 1 }
99 else { farmag = farmag + mag; farn = farn + 1 }
100 }
101 }
102 px = px + 1
103 }
104 py = py + 1
105 }
106 // coherence: mean |flow - right-neighbour flow| over valid adjacent pairs
107 var coh: i64 = 0; var cohn: i64 = 0
108 py = 0
109 while py < H {
110 var px: i64 = 0
111 while px < W-1 {
112 let pi: i64 = py*W+px
113 if valid[pi] == 1 { if valid[pi+1] == 1 {
114 coh = coh + iabs(flowx[pi]-flowx[pi+1]) + iabs(flowy[pi]-flowy[pi+1])
115 cohn = cohn + 1
116 } }
117 px = px + 1
118 }
119 py = py + 1
120 }
121 let cohmean: i64 = coh*100/(cohn+1) // x100 (sub-unit)
122 let nearavg: i64 = nearmag/(nearn+1)
123 let faravg: i64 = farmag/(farn+1)
124 hw(" flow: valid="); pn(nvalid); hw("/"); pn(nsurf); hw(" surf coherence(x100/pair)="); pn(cohmean); hw(" parallax near-avg="); pn(nearavg); hw(" far-avg="); pn(faravg); hw("\n" as *u8)
125 var t2: i64 = 0
126 if nvalid*100 > nsurf*75 { // most surface pixels reproject in-frame
127 if cohmean < 400 { // < ~4 px avg neighbour flow jump = smooth field
128 if nearavg > faravg*3/2 { t2 = 1 } // ★near flows >=1.5x far = parallax = correct 3D flow
129 }
130 }
131 if t2 == 1 { hw("T2 PASS EXACT dense flow: high coverage, coherent, and PARALLAX holds (near>far) = 3D-correct\n" as *u8) }
132 else { fails=fails+1; hw("T2 FAIL flow\n" as *u8) }
133
134 // ---- T3 warp proof: A[pix] vs realB[flowed pix] << no-flow baseline A[pix] vs realB[pix] ----
135 var warped: i64 = 0; var noflow: i64 = 0; var wn: i64 = 0
136 py = 0
137 while py < H {
138 var px: i64 = 0
139 while px < W {
140 let pi: i64 = py*W+px
141 if valid[pi] == 1 {
142 let bx: i64 = px + flowx[pi]
143 let by: i64 = py + flowy[pi]
144 if bx >= 0 { if bx < W { if by >= 0 { if by < H {
145 warped = warped + cL1(fbA[pi], fbB[by*W+bx])
146 noflow = noflow + cL1(fbA[pi], fbB[pi])
147 wn = wn + 1
148 } } } }
149 }
150 px = px + 1
151 }
152 py = py + 1
153 }
154 let warpavg: i64 = warped/(wn+1)
155 let noflowavg: i64 = noflow/(wn+1)
156 hw(" warp: A-vs-B@flow avg L1="); pn(warpavg); hw(" vs no-flow baseline="); pn(noflowavg); hw("\n" as *u8)
157 var t3: i64 = 0
158 if warpavg*2 < noflowavg { t3 = 1 } // following the flow more than halves the mismatch
159 if t3 == 1 { hw("T3 PASS warp proof: A lands on the same surface in B when it follows the flow\n" as *u8) }
160 else { fails=fails+1; hw("T3 FAIL warp\n" as *u8) }
161
162 // ---- panel: A | flow-vis | warped-A(->B) | realB ----
163 let GW: i64 = W*2
164 let gal: *i64 = sys_mmap(GW*H*2*8) as *i64
165 // warped-A: backward-sample A at (px - flow) so it looks like the view slid toward B (no ghosting)
166 let warp: *i64 = sys_mmap(npx*8) as *i64
167 py = 0
168 while py < H {
169 var px: i64 = 0
170 while px < W {
171 let pi: i64 = py*W+px
172 var sx: i64 = px; var sy: i64 = py
173 if valid[pi] == 1 { sx = px + flowx[pi]; sy = py + flowy[pi] }
174 if sx < 0 { sx = 0 } if sx >= W { sx = W-1 }
175 if sy < 0 { sy = 0 } if sy >= H { sy = H-1 }
176 warp[pi] = fbA[sy*W+sx] // A content pulled to where B sees it
177 px = px + 1
178 }
179 py = py + 1
180 }
181 py = 0
182 while py < H {
183 var px: i64 = 0
184 while px < W {
185 let pi: i64 = py*W+px
186 gal[py*GW+px] = fbA[pi]
187 // flow-vis: R = +x flow, G = +y flow, around 128
188 var fvr: i64 = 128; var fvg: i64 = 128
189 if valid[pi] == 1 {
190 fvr = 128 + flowx[pi]*3; if fvr<0{fvr=0} if fvr>255{fvr=255}
191 fvg = 128 + flowy[pi]*3; if fvg<0{fvg=0} if fvg>255{fvg=255}
192 }
193 gal[py*GW+W+px] = fvr + fvg*256 + 128*65536
194 gal[(H+py)*GW+px] = warp[pi]
195 gal[(H+py)*GW+W+px] = fbB[pi]
196 px = px + 1
197 }
198 py = py + 1
199 }
200 write_png(gal, GW, H*2, "knowledge/nx_flow_panel.png" as *u8)
201 hw("T4 panel -> knowledge/nx_flow_panel.png (frameA | flow-vis | flow-warped | realB)\n" as *u8)
202
203 if fails == 0 { hw("CAMERA-ANIM-GATE GREEN -- R4: a positionable camera rig produces a trajectory, and EXACT dense optical flow (parallax-verified, warp-verified) is emitted between frames -- the animation GAP + the R2 flow residual both closed\n" as *u8); sys_exit(0); return 0 }
204 hw("CAMERA-ANIM-GATE RED fails="); pn(fails); hw("\n" as *u8)
205 sys_exit(1)
206 return 1
207}