import assert from 'node:assert/strict'; import * as R from './mesh-refraction-reference.mjs'; const {v3,makeDepthTexture,depthAt,photoSurface,triangleOracle,tracePhotoDDA}=R; const F=Math.fround,A=(a,b)=>F(a+b),S=(a,b)=>F(a-b),M=(a,b)=>F(a*b),D=(a,b)=>F(a/b),V=a=>a.map(F),add=(a,b)=>a.map((x,i)=>A(x,b[i])),sub=(a,b)=>a.map((x,i)=>S(x,b[i])),mul=(a,b)=>a.map(x=>M(x,b)),dot=(a,b)=>A(A(M(a[0],b[0]),M(a[1],b[1])),M(a[2],b[2])),cross=(a,b)=>[S(M(a[1],b[2]),M(a[2],b[1])),S(M(a[2],b[0]),M(a[0],b[2])),S(M(a[0],b[1]),M(a[1],b[0]))],INF=F(1e20); function triangle(o,r,a,b,c){let e1=sub(b,a),e2=sub(c,a),p=cross(r,e2),det=dot(e1,p);if(det<=F(1e-9))return INF;let s=sub(o,a),u=D(dot(s,p),det);if(uF(1.00001))return INF;let q=cross(s,e1),v=D(dot(r,q),det);if(vF(1.00001))return INF;let t=D(dot(e2,q),det);return t>=0?t:INF;} function shaderDDA(surface,origin,ray){const o=V(origin.toArray()),r=V(ray.toArray());let near=0,far=INF;const bounds=[[-.5,.5],[-.5,.5],[0,.25]];for(let i=0;i<3;i++){let [lo,hi]=bounds[i];if(Math.abs(r[i])=lo&&o[i]<=hi))return null;}else{let a=D(S(lo,o[i]),r[i]),b=D(S(hi,o[i]),r[i]);near=Math.max(near,Math.min(a,b));far=Math.min(far,Math.max(a,b));if(farMath.min(127,Math.max(0,Math.floor(A(M(A(v,.5),128),M(direction[i],.00001)))))),next=[INF,INF],delta=[INF,INF];for(let i=0;i<2;i++)if(Math.abs(r[i])>=F(1e-9)){next[i]=D(S(S(D(A(cell[i],r[i]>=0?1:0),128),.5),o[i]),r[i]);delta[i]=D(1,M(128,Math.abs(r[i])));} const vert=(x,y)=>[S(D(x,128),.5),S(D(y,128),.5),M(F(depthAt(surface.texture,D(x,128),D(y,128))),.25)]; for(let i=0;i<257;i++){let ll=vert(cell[0],cell[1]),lr=vert(A(cell[0],1),cell[1]),ul=vert(cell[0],A(cell[1],1)),ur=vert(A(cell[0],1),A(cell[1],1));let t=Math.min(triangle(o,r,ul,ll,ur),triangle(o,r,ll,lr,ur)),end=Math.min(far,...next);if(t>=S(near,.00001)&&t<=A(end,.00001)&&t=S(far,.000001))return null;let cx=next[0]<=next[1],cy=next[1]<=next[0];near=end;if(cx){cell[0]=A(cell[0],direction[0]);next[0]=A(next[0],delta[0]);}if(cy){cell[1]=A(cell[1],direction[1]);next[1]=A(next[1],delta[1]);}if(cell.some(v=>v<0||v>=128))return null;}throw Error('257 cells exhausted');} const surface=photoSurface(makeDepthTexture(67,53,(x,y)=>.5+.21*Math.sin(x*1.2)+.25*Math.cos(y*.9+x*.4))); let seed=0x5eed,random=()=>{seed=(Math.imul(seed,1664525)+1013904223)>>>0;return seed/2**32;},cases=[]; for(let i=0;i<1500;i++){const o=v3((random()-.5)*1.8,(random()-.5)*1.8,.4+random()),target=v3((random()-.5)*1.2,(random()-.5)*1.2,random()*.2);cases.push([o,target.sub(o).normalize()]);} for(const x of [-.5,-.25,0,.25,.5])for(const y of [-.5,-.25,0,.25,.5])for(const dx of [-.3,0,.3])for(const dy of [-.3,0,.3])cases.push([v3(x,y,1),v3(dx,dy,-1).normalize()]); for(let i=0;i<20;i++)cases.push([v3(-.6,-.45+i*.02,.249),v3(1,.85,-.0001).normalize()]); let misses=0,worst=0,maxcells=0; for(let i=0;ix*y));let longWorst=0,longMax=0,longCases=0; for(let i=0;i<80;i++){const o=v3(-.6,-.45+i*.005,.249),r=v3(1,.85,-.0001).normalize(),a=shaderDDA(longSurface,o,r),e=tracePhotoDDA(longSurface,o,r).hit;assert.equal(Boolean(a),Boolean(e),`long ray ${i}`);if(a){longMax=Math.max(longMax,a.cells);longWorst=Math.max(longWorst,v3(...a.point).distanceTo(e.point));assert.ok(v3(...a.point).distanceTo(e.point)<.0001);}longCases++;} console.log(JSON.stringify({kind:'CPU Float32 long XY traversal',rays:longCases,worstHitPointError:longWorst,maxcells:longMax}));