using XFEEDToolBox.Core.Models; namespace XFEEDToolBox.Core.Galaxy; /// Bounded perspective grid and clipped route geometry in the actual Galactic frame. public static class GalaxyMapGeometry { /// A star's vertical connector to the camera's Galactic grid plane, without per-frame coordinate allocations. public static bool TryProjectStalk(GalaxyCamera camera,Coordinates star,out MapSegment segment) { segment=default;if(!star.IsValid||Math.Abs(star.Y-camera.Focus.Y)<1e-8)return false; var dx=star.X-camera.Focus.X;var dz=star.Z-camera.Focus.Z;var height=star.Y-camera.Focus.Y; var planeDepth=camera.Distance+dx*camera.Forward.X+dz*camera.Forward.Z; var starDepth=planeDepth+height*camera.Forward.Y; if(starDepth1e-12) { var near=(camera.Near-starDepth)/depthChange; if(depthChange>0)low=Math.Max(0,near);else high=Math.Min(1,near); } if(low>high)return false; (double X,double Y) At(double t) { var dy=height*(1-t);var scale=camera.FocalLength/Math.Max(camera.Near,starDepth+depthChange*t); return(camera.Width/2+(dx*camera.Right.X+dy*camera.Right.Y+dz*camera.Right.Z)*scale, camera.Height/2-(dx*camera.Up.X+dy*camera.Up.Y+dz*camera.Up.Z)*scale); } var a=At(low);var b=At(high);var lineX=b.X-a.X;var lineY=b.Y-a.Y;low=0;high=1; bool Bound(double p,double q) { if(Math.Abs(p)<1e-12)return q>=0; var t=q/p;if(p<0){if(t>high)return false;low=Math.Max(low,t);}else{if(t=0; var t=q/p; if(p<0){if(t>high)return false;low=Math.Max(low,t);}else{if(t output,GalaxyCamera camera,out double step) { step=0;if(output.Length<8||Math.Abs(Math.Sin(camera.Pitch))<.015)return 0; // Four orbit distances reach the far clip plane. Grid lines are snapped // to Galactic coordinates so panning never resets the lattice origin. var extent=camera.Distance*4; var target=Math.Max(20/camera.Scale,extent/70); var decade=Math.Pow(10,Math.Floor(Math.Log10(target)));var fraction=target/decade; step=decade*(fraction<=1?1:fraction<=2?2:fraction<=5?5:10); var budget=Math.Min(output.Length,InfiniteGalaxyGrid.MaximumLines);var count=0; var focus=camera.Focus;var minX=focus.X-extent;var maxX=focus.X+extent;var minZ=focus.Z-extent;var maxZ=focus.Z+extent; for(var axis=0;axis<2;axis++) { var first=Math.Ceiling((axis==0?minX:minZ)/step);var last=Math.Floor((axis==0?maxX:maxZ)/step); for(var i=first;i<=last&&countProjects the original198 world lines through the shared camera. public static int WriteOriginalGrid(Span output,GalaxyCamera camera,out double step) { step=GalaxyOriginalGrid.Step(camera.Distance); if(output.Length source=stackalloc GalaxyOriginalGridLine[GalaxyOriginalGrid.MaximumLines]; var generated=GalaxyOriginalGrid.Write(source,camera.Focus,camera.Distance);var count=0; foreach(var line in source[..generated]) if(TryProjectSegment(camera,new(line.FromX,line.FromY,line.FromZ),new(line.ToX,line.ToY,line.ToZ),out var segment))output[count++]=segment; return count; } }