namespace XFEEDToolBox.Core.Navigation; public readonly record struct Vector3d(double X,double Y,double Z) { public static Vector3d Zero=>new(0,0,0); public bool Finite=>double.IsFinite(X)&&double.IsFinite(Y)&&double.IsFinite(Z); public double Length=>Math.Sqrt(Dot(this,this)); public Vector3d Unit()=>Length is >1e-10 and var length?this/length:throw new InvalidOperationException("方向向量无效。"); public static double Dot(Vector3d a,Vector3d b)=>a.X*b.X+a.Y*b.Y+a.Z*b.Z; public static Vector3d Cross(Vector3d a,Vector3d b)=>new(a.Y*b.Z-a.Z*b.Y,a.Z*b.X-a.X*b.Z,a.X*b.Y-a.Y*b.X); public static Vector3d operator+(Vector3d a,Vector3d b)=>new(a.X+b.X,a.Y+b.Y,a.Z+b.Z); public static Vector3d operator-(Vector3d a,Vector3d b)=>new(a.X-b.X,a.Y-b.Y,a.Z-b.Z); public static Vector3d operator*(Vector3d a,double b)=>new(a.X*b,a.Y*b,a.Z*b); public static Vector3d operator/(Vector3d a,double b)=>new(a.X/b,a.Y/b,a.Z/b); public static double AngleDegrees(Vector3d a,Vector3d b)=>Math.Acos(Math.Clamp(Dot(a.Unit(),b.Unit()),-1,1))*180/Math.PI; } public sealed record StarEnvelope(long BodyId,string StarClass,Vector3d Centre,double ExclusionRadius,double? ScoopInnerRadius,double? ScoopOuterRadius) { public double SafeRadius=>ExclusionRadius*1.05; public bool CanScoop=>ScoopRules.CanRefuel(StarClass)&&ScoopInnerRadius is >0&&ScoopOuterRadius>ScoopInnerRadius; } public static class FlightGeometry { public static double ClosestSegmentDistance(Vector3d point,Vector3d from,Vector3d to) { var delta=to-from;var square=Vector3d.Dot(delta,delta); if(square<1e-12)return(point-from).Length; return(point-(from+delta*Math.Clamp(Vector3d.Dot(point-from,delta)/square,0,1))).Length; } public static bool Obscures(StarEnvelope star,Vector3d ship,Vector3d destination)=>ClosestSegmentDistance(star.Centre,ship,destination)<=star.SafeRadius; public static Vector3d Perpendicular(Vector3d direction,Vector3d preferred) { var axis=direction.Unit();var projected=preferred-axis*Vector3d.Dot(preferred,axis); if(projected.Length<1e-8){var fallback=Math.Abs(axis.Y)<.9?new Vector3d(0,1,0):new Vector3d(1,0,0);projected=fallback-axis*Vector3d.Dot(fallback,axis);} return projected.Unit(); } /// Normal to the target ray; pure radial escape cannot clear a diametrically hidden target. public static Vector3d FastClearDirection(StarEnvelope star,Vector3d ship,Vector3d destination,Vector3d up) { var toward=(destination-ship).Unit();var radial=ship-star.Centre; var sideways=radial-toward*Vector3d.Dot(radial,toward); return sideways.Length>1e-8?sideways.Unit():Perpendicular(toward,up); } public static Vector3d? ClearPerpendicularDirection(IReadOnlyList bodies,Vector3d ship,Vector3d destination,Vector3d preferred) { if(!ship.Finite||!destination.Finite||!preferred.Finite||(destination-ship).Length<1 ||bodies.Any(x=>!x.Centre.Finite||!double.IsFinite(x.ExclusionRadius)||x.ExclusionRadius<=0))return null; var toward=(destination-ship).Unit();var first=Perpendicular(toward,preferred);var second=Vector3d.Cross(toward,first).Unit(); // Check the entire forward ray against EVERY observed body. A clear // tangent to one planet can still point directly into another moon. for(var step=0;step<16;step++) { var angle=(step==0?0:(step%2==1?(step+1)/2:-(step/2)))*Math.PI/8; var candidate=first*Math.Cos(angle)+second*Math.Sin(angle); if(bodies.All(body=> { var delta=body.Centre-ship;var along=Math.Max(0,Vector3d.Dot(delta,candidate)); return(delta-candidate*along).Length>body.SafeRadius; }))return candidate; } return null; } public static Vector3d OrbitDirection(StarEnvelope star,Vector3d ship,Vector3d destination,Vector3d up) { if(!star.CanScoop)throw new InvalidOperationException("该恒星没有已确认的可采集包络。"); var r=(ship-star.Centre).Length; var inner=Math.Max(star.SafeRadius*1.02,star.ScoopInnerRadius!.Value); var outer=star.ScoopOuterRadius!.Value;if(outer<=inner)throw new InvalidOperationException("可采集区域与安全排除区不相容。"); var orbit=Math.Clamp(r,inner,outer); return OrbitAtRadius(star,ship,destination,up,orbit); } public static Vector3d OrbitPlane(StarEnvelope star,Vector3d ship,Vector3d destination,Vector3d up,Vector3d? forward=null) { var radial=(ship-star.Centre).Unit();var toward=(destination-ship).Unit(); var normal=Vector3d.Cross(radial,toward); // Native destination rays are floats; near-parallel subtraction must // not choose a new random orbital azimuth from their rounding noise. if(normal.Length<.01) { var candidate=forward??up; var projection=candidate-radial*Vector3d.Dot(candidate,radial); normal=Vector3d.Cross(radial,projection.Length>.1?projection.Unit():Perpendicular(radial,up)); } return normal.Unit(); } public static Vector3d OrbitAtRadius(StarEnvelope star,Vector3d ship,Vector3d destination,Vector3d up,double radius,Vector3d? planeNormal=null,double? speed=null) { if(!double.IsFinite(radius)||!double.IsFinite(star.ExclusionRadius)||star.ExclusionRadius<=0||radius