import {readFile,writeFile} from 'node:fs/promises'; import {measure} from './measure.mjs'; const base=new URL('./',import.meta.url); const vertices=(await readFile(new URL('canonical-face.obj',base),'utf8')).split(/\r?\n/).filter(x=>x.startsWith('v ')).map(x=>x.split(/\s+/).slice(1,4).map(Number)); const rad=a=>a*Math.PI/180; function run({yaw=0,pitch=0,roll=0,perspective=false,distance=30,brow=false}={}){ const original=vertices.map(v=>[...v]); const eyeCenter=ids=>ids.map(i=>original[i]).reduce((a,b)=>a.map((x,j)=>x+b[j]),[0,0,0]).map(x=>x/2); const a=eyeCenter([33,133]),b=eyeCenter([263,362]),eyeSpace=Math.hypot(a[0]-b[0],a[1]-b[1]); if(brow)for(const i of [70,105,107])original[i][1]+=.1*eyeSpace; const points=original.map(([x,y,z])=>{ const yy=y*Math.cos(rad(pitch))-z*Math.sin(rad(pitch)),zz=y*Math.sin(rad(pitch))+z*Math.cos(rad(pitch)); let xx=x*Math.cos(rad(yaw))+zz*Math.sin(rad(yaw));z=-x*Math.sin(rad(yaw))+zz*Math.cos(rad(yaw));y=yy; if(perspective){const scale=distance/(distance-z);xx*=scale;y*=scale;} x=xx*Math.cos(rad(roll))-y*Math.sin(rad(roll));y=xx*Math.sin(rad(roll))+y*Math.cos(rad(roll)); return {x:(500+20*x)/1000,y:(500-20*y)/1000}; }); const result=measure(points,1000,1000); return {yaw,pitch,roll,perspective,distance:perspective?distance:null,brow,score:result.score,mismatchPercent:result.mismatch*100,regions:result.groups.map(g=>({name:g.name,mismatchPercent:g.mismatch*100}))}; } const rows=[run(),...[3,4,8,12].map(yaw=>run({yaw})),run({pitch:22}),run({roll:20}),run({brow:true}),run({perspective:true,yaw:12,distance:30})]; const report={modelVertices:vertices.length,projection:'Orthographic except explicitly labeled pinhole example. Pinhole camera on positive z axis at 30 canonical units from canonical origin; no inference/model noise. Coordinates mapped with uniform scale into a 1000 by 1000 working image.',rows}; await writeFile(new URL('pose-sensitivity.json',base),JSON.stringify(report,null,2)); console.log(JSON.stringify(rows.map(({regions,...r})=>r),null,2));