namespace AstroFrame.Engine; public sealed record Transform(double A,double B,double Tx,double Ty) { public (double X,double Y) Apply(double x,double y)=>(A*x-B*y+Tx,B*x+A*y+Ty); } public static class Matching { const double Cell=.005; sealed class Grid { readonly Dictionary<(int,int),List> cells=[]; readonly double aspect; public Grid(IEnumerable stars,double aspect) {this.aspect=aspect;foreach(var s in stars){var k=((int)(s.X/Cell),(int)(s.Y*aspect/Cell));if(!cells.TryGetValue(k,out var a))cells[k]=a=[];a.Add(s);}} public Star? Nearest(double x,double y) { int cx=(int)(x/Cell),cy=(int)(y/Cell);Star? best=null;double dist=Cell*Cell; for(int dx=-1;dx<=1;dx++)for(int dy=-1;dy<=1;dy++)if(cells.TryGetValue((cx+dx,cy+dy),out var a))foreach(var s in a){double d=(s.X-x)*(s.X-x)+(s.Y*aspect-y)*(s.Y*aspect-y);if(d.025)continue; double a=(rx*ox+ry*oy)/rd,b=(rx*oy-ry*ox)/rd; var tr=new Transform(a,b,observed[k].X-a*refs[i].X+b*refs[i].Y*aspect,observed[k].Y*aspect-b*refs[i].X-a*refs[i].Y*aspect); int n=0;foreach(var star in refs){var(x,y)=tr.Apply(star.X,star.Y*aspect);if(grid.Nearest(x,y)!=null)n++;} if(n>bestN){bestN=n;best=tr;} if(n>=refs.Length-1)return Refine(reference,frame,tr); } } return bestN>=Math.Max(7,refs.Length*.5)&&best!=null?Refine(reference,frame,best):null; } static Transform Refine(Frame reference,Frame frame,Transform tr) { double aspect=(double)reference.Height/reference.Width;var grid=new Grid(frame.Stars,aspect); for(int iteration=0;iteration<4;iteration++) { List<(double X,double Y,double U,double V)> pairs=[];HashSet used=[]; foreach(var s in reference.Stars.Take(400)) { var(x,y)=tr.Apply(s.X,s.Y*aspect);var other=grid.Nearest(x,y); if(other!=null&&used.Add(other))pairs.Add((s.X,s.Y*aspect,other.X,other.Y*aspect)); } if(pairs.Count<12)break; double x0=pairs.Average(p=>p.X),y0=pairs.Average(p=>p.Y),u0=pairs.Average(p=>p.U),v0=pairs.Average(p=>p.V); double d=pairs.Sum(p=>(p.X-x0)*(p.X-x0)+(p.Y-y0)*(p.Y-y0));if(d<=0)break; double a=pairs.Sum(p=>(p.X-x0)*(p.U-u0)+(p.Y-y0)*(p.V-v0))/d,b=pairs.Sum(p=>(p.X-x0)*(p.V-v0)-(p.Y-y0)*(p.U-u0))/d; tr=new(a,b,u0-a*x0+b*y0,v0-b*x0-a*y0); } return tr; } public static double[] ForcedFlux(Frame reference,Frame frame,ImagePlane image) { var stars=reference.Stars.Take(600).ToArray();double[] flux=Enumerable.Repeat(double.NaN,stars.Length).ToArray(); if(frame.Alignment.Length!=4)return flux; double aspect=(double)reference.Height/reference.Width;var a=frame.Alignment;var p=image.Pixels;int w=image.Width,h=image.Height; for(int j=0;j=w-10||y>=h-10)continue; List annulus=[];for(int dy=-8;dy<=8;dy++)for(int dx=-8;dx<=8;dx++)if(dx*dx+dy*dy>=36&&dx*dx+dy*dy<=64)annulus.Add(p[(y+dy)*w+x+dx]); double bg=Robust.Median(annulus),sum=0;bool saturated=false; for(int dy=-4;dy<=4;dy++)for(int dx=-4;dx<=4;dx++)if(dx*dx+dy*dy<=16){double value=p[(y+dy)*w+x+dx];sum+=value-bg;saturated|=value>=64000;} if(!saturated)flux[j]=sum/Math.Max(1,frame.Exposure); } return flux; } public static void Measure(Frame reference,Frame frame) { frame.Transparency=0;frame.MatchFraction=0;frame.RegionFlux=new double[12];frame.RegionLoss=new double[12];frame.RegionMatches=new int[12]; frame.Alignment=[];frame.ReferenceId=reference.Id; var tr=Align(reference,frame);if(tr==null)return; frame.Alignment=[tr.A,tr.B,tr.Tx,tr.Ty]; double aspect=(double)reference.Height/reference.Width;var grid=new Grid(frame.Stars,aspect); List ratios=[];List[] regional=Enumerable.Range(0,12).Select(_=>new List()).ToArray();int[] expected=new int[12],found=new int[12];HashSet used=[]; foreach(var s in reference.Stars.Take(600)) { var(x,y)=tr.Apply(s.X,s.Y*aspect);y/=aspect; if(x<.025||x>.975||y<.025||y>.975)continue; int region=Math.Clamp((int)(s.Y*4),0,3)*3+Math.Clamp((int)(s.X*3),0,2);expected[region]++; var other=grid.Nearest(x,y*aspect);if(other==null||!used.Add(other))continue;found[region]++; double ratio=other.Flux/Math.Max(s.Flux,.001);if(double.IsFinite(ratio)&&ratio>0){ratios.Add(ratio);regional[region].Add(ratio);} } frame.MatchFraction=(double)found.Sum()/Math.Max(expected.Sum(),1); if(ratios.Count>=15)frame.Transparency=Robust.Median(ratios); for(int i=0;i<12;i++){frame.RegionMatches[i]=regional[i].Count;frame.RegionLoss[i]=expected[i]>=8?1-(double)found[i]/expected[i]:0;frame.RegionFlux[i]=regional[i].Count>=5?Robust.Median(regional[i]):0;} frame.SpatialLoss=frame.RegionLoss.Max(); } }