using XFEEDToolBox.Core.Navigation;
namespace XFEEDToolBox.Core.Rendering;
/// <summary>One uniform scale per parent family. Ellipses and body positions use the same affine mapping.</summary>
public sealed class LocalSystemOrbitLayout
{
private sealed record Focus(ulong Id,Vector3d Actual);
private readonly Dictionary<ulong,SystemOrbit> parents;
private readonly Dictionary<ulong,Focus> foci=[];
private readonly Dictionary<ulong,double> scales=[];
private readonly Dictionary<ulong,Vector3d> centres=[];
private readonly LocalSystemOrientation orientation;
private readonly Vector3d origin;
private readonly double globalScale;
public LocalSystemOrbitLayout(CurrentSystemSnapshot snapshot,IReadOnlyList<SystemOrbit> verified,LocalSystemOrientation orientation)
{
this.orientation=orientation;
parents=verified.GroupBy(orbit=>orbit.ChildWorldAddress).Where(group=>group.Count()==1).ToDictionary(group=>group.Key,group=>group.Single());
foreach(var body in snapshot.Locations.Where(location=>location.Category is SystemLocationCategory.Star or SystemLocationCategory.Planet or SystemLocationCategory.LandablePlanet))
if(body.Position is {Finite:true} point)foci.TryAdd(body.WorldAddress,new(body.WorldAddress,point));
foreach(var orbit in verified)if(orbit.ParentPosition is {Finite:true} point)foci.TryAdd(orbit.ParentWorldAddress!.Value,new(orbit.ParentWorldAddress.Value,point));
var roots=foci.Values.Where(focus=>!parents.ContainsKey(focus.Id)).OrderBy(focus=>focus.Id).ToArray();
origin=roots.FirstOrDefault()?.Actual??new(0,0,0);
var extent=verified.Select(orbit=>orbit.SemiMajorAxisMetres!.Value*(1+orbit.Eccentricity!.Value))
.Concat(foci.Values.Select(focus=>(focus.Actual-origin).Length)).Where(double.IsFinite).DefaultIfEmpty(1).Max();
globalScale=4.4/Math.Max(1,extent);
foreach(var root in roots){centres[root.Id]=orientation.Rotate(root.Actual-origin)*globalScale;scales[root.Id]=globalScale;}
foreach(var focus in foci.Values)Centre(focus.Id,[]);
}
private Vector3d Centre(ulong id,HashSet<ulong> path)
{
if(centres.TryGetValue(id,out var existing))return existing;
if(!foci.TryGetValue(id,out var focus)||!path.Add(id))throw new InvalidDataException("轨道父子关系存在循环或缺失焦点。");
if(parents.TryGetValue(id,out var orbit))
{
var parent=orbit.ParentWorldAddress!.Value;var centre=Centre(parent,path);
centres[id]=centre+orientation.Rotate(focus.Actual-foci[parent].Actual)*scales[parent];
// Leave room around sibling systems while making each star's
// planetary family legible. This is explicit display scaling.
var siblings=parents.Values.Where(value=>value.ParentWorldAddress==parent&&value.ChildWorldAddress!=id)
.Where(value=>foci.ContainsKey(value.ChildWorldAddress))
.Select(value=>(foci[value.ChildWorldAddress].Actual-focus.Actual).Length*scales[parent]).Where(distance=>distance>0).ToArray();
var budget=Math.Min(1.35,Math.Max(.08,(focus.Actual-foci[parent].Actual).Length*scales[parent]*.32));
if(siblings.Length>0)budget=Math.Min(budget,siblings.Min()*.3);
var children=parents.Values.Where(value=>value.ParentWorldAddress==id).ToArray();
var extent=children.Select(value=>value.SemiMajorAxisMetres!.Value*(1+value.Eccentricity!.Value)).DefaultIfEmpty(0).Max();
scales[id]=extent>0?budget/extent:scales[parent];
}
else{centres[id]=orientation.Rotate(focus.Actual-origin)*globalScale;scales[id]=globalScale;}
path.Remove(id);return centres[id];
}
public Vector3d Location(SystemLocation location)
{
var point=location.Position!.Value;
if(location.Category is SystemLocationCategory.Star or SystemLocationCategory.Planet or SystemLocationCategory.LandablePlanet&¢res.TryGetValue(location.WorldAddress,out var centre))return centre;
if(foci.TryGetValue(location.WorldAddress,out var focus)&¢res.TryGetValue(location.WorldAddress,out var reference))
{
var offset=orientation.Rotate(point-focus.Actual)*scales[location.WorldAddress];var length=offset.Length;
// Distant transient signals must not shrink an entire stellar
// family. Compress marker-only offsets; celestial paths stay affine.
if(length>1.5)offset*= (1.5+.6*(1-Math.Exp(-(length-1.5)/.6)))/length;
return reference+offset;
}
return Relative(location.WorldAddress,point);
}
public Vector3d Relative(ulong reference,Vector3d actual)
=>foci.TryGetValue(reference,out var focus)&¢res.TryGetValue(reference,out var centre)
?centre+orientation.Rotate(actual-focus.Actual)*scales[reference]:orientation.Rotate(actual-origin)*globalScale;
public Vector3d OrbitPoint(SystemOrbit orbit,Vector3d actual)=>Relative(orbit.ParentWorldAddress!.Value,actual);
public Vector3d ClosestReference(Vector3d actual)
{
var nearest=foci.Values.OrderBy(value=>(actual-value.Actual).Length).FirstOrDefault();
return nearest is null?orientation.Rotate(actual-origin)*globalScale:Relative(nearest.Id,actual);
}
}
using XFEEDToolBox.Core.Navigation;
namespace XFEEDToolBox.Core.Rendering;
/// <summary>One uniform scale per parent family. Ellipses and body positions use the same affine mapping.</summary>
public sealed class LocalSystemOrbitLayout
{
private sealed record Focus(ulong Id,Vector3d Actual);
private readonly Dictionary<ulong,SystemOrbit> parents;
private readonly Dictionary<ulong,Focus> foci=[];
private readonly Dictionary<ulong,double> scales=[];
private readonly Dictionary<ulong,Vector3d> centres=[];
private readonly LocalSystemOrientation orientation;
private readonly Vector3d origin;
private readonly double globalScale;
public LocalSystemOrbitLayout(CurrentSystemSnapshot snapshot,IReadOnlyList<SystemOrbit> verified,LocalSystemOrientation orientation)
{
this.orientation=orientation;
parents=verified.GroupBy(orbit=>orbit.ChildWorldAddress).Where(group=>group.Count()==1).ToDictionary(group=>group.Key,group=>group.Single());
foreach(var body in snapshot.Locations.Where(location=>location.Category is SystemLocationCategory.Star or SystemLocationCategory.Planet or SystemLocationCategory.LandablePlanet))
if(body.Position is {Finite:true} point)foci.TryAdd(body.WorldAddress,new(body.WorldAddress,point));
foreach(var orbit in verified)if(orbit.ParentPosition is {Finite:true} point)foci.TryAdd(orbit.ParentWorldAddress!.Value,new(orbit.ParentWorldAddress.Value,point));
var roots=foci.Values.Where(focus=>!parents.ContainsKey(focus.Id)).OrderBy(focus=>focus.Id).ToArray();
origin=roots.FirstOrDefault()?.Actual??new(0,0,0);
var extent=verified.Select(orbit=>orbit.SemiMajorAxisMetres!.Value*(1+orbit.Eccentricity!.Value))
.Concat(foci.Values.Select(focus=>(focus.Actual-origin).Length)).Where(double.IsFinite).DefaultIfEmpty(1).Max();
globalScale=4.4/Math.Max(1,extent);
foreach(var root in roots){centres[root.Id]=orientation.Rotate(root.Actual-origin)*globalScale;scales[root.Id]=globalScale;}
foreach(var focus in foci.Values)Centre(focus.Id,[]);
}
private Vector3d Centre(ulong id,HashSet<ulong> path)
{
if(centres.TryGetValue(id,out var existing))return existing;
if(!foci.TryGetValue(id,out var focus)||!path.Add(id))throw new InvalidDataException("轨道父子关系存在循环或缺失焦点。");
if(parents.TryGetValue(id,out var orbit))
{
var parent=orbit.ParentWorldAddress!.Value;var centre=Centre(parent,path);
centres[id]=centre+orientation.Rotate(focus.Actual-foci[parent].Actual)*scales[parent];
// Leave room around sibling systems while making each star's
// planetary family legible. This is explicit display scaling.
var siblings=parents.Values.Where(value=>value.ParentWorldAddress==parent&&value.ChildWorldAddress!=id)
.Where(value=>foci.ContainsKey(value.ChildWorldAddress))
.Select(value=>(foci[value.ChildWorldAddress].Actual-focus.Actual).Length*scales[parent]).Where(distance=>distance>0).ToArray();
var budget=Math.Min(1.35,Math.Max(.08,(focus.Actual-foci[parent].Actual).Length*scales[parent]*.32));
if(siblings.Length>0)budget=Math.Min(budget,siblings.Min()*.3);
var children=parents.Values.Where(value=>value.ParentWorldAddress==id).ToArray();
var extent=children.Select(value=>value.SemiMajorAxisMetres!.Value*(1+value.Eccentricity!.Value)).DefaultIfEmpty(0).Max();
scales[id]=extent>0?budget/extent:scales[parent];
}
else{centres[id]=orientation.Rotate(focus.Actual-origin)*globalScale;scales[id]=globalScale;}
path.Remove(id);return centres[id];
}
public Vector3d Location(SystemLocation location)
{
var point=location.Position!.Value;
if(location.Category is SystemLocationCategory.Star or SystemLocationCategory.Planet or SystemLocationCategory.LandablePlanet&¢res.TryGetValue(location.WorldAddress,out var centre))return centre;
if(foci.TryGetValue(location.WorldAddress,out var focus)&¢res.TryGetValue(location.WorldAddress,out var reference))
{
var offset=orientation.Rotate(point-focus.Actual)*scales[location.WorldAddress];var length=offset.Length;
// Distant transient signals must not shrink an entire stellar
// family. Compress marker-only offsets; celestial paths stay affine.
if(length>1.5)offset*= (1.5+.6*(1-Math.Exp(-(length-1.5)/.6)))/length;
return reference+offset;
}
return Relative(location.WorldAddress,point);
}
public Vector3d Relative(ulong reference,Vector3d actual)
=>foci.TryGetValue(reference,out var focus)&¢res.TryGetValue(reference,out var centre)
?centre+orientation.Rotate(actual-focus.Actual)*scales[reference]:orientation.Rotate(actual-origin)*globalScale;
public Vector3d OrbitPoint(SystemOrbit orbit,Vector3d actual)=>Relative(orbit.ParentWorldAddress!.Value,actual);
public Vector3d ClosestReference(Vector3d actual)
{
var nearest=foci.Values.OrderBy(value=>(actual-value.Actual).Length).FirstOrDefault();
return nearest is null?orientation.Rotate(actual-origin)*globalScale:Relative(nearest.Id,actual);
}
}