Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
namespace Aetheris.Kernel.Firmament.Materializer;
/// <summary>Immutable, resolved material boundary. It is neither a sketch nor a constraint solver.</summary>
public sealed record ProfileSegmentProvenance(
string StableId,
string ConceptStableId,
string SourceSpan,
string Derivation,
string SourceFrame,
string? TracedFrom = null,
bool Reversed = false,
int? PipelineIndex = null,
string? InvocationSourceRange = null);
public sealed record ResolvedProfileSegment2D(string Name, LineArcProfileCurve2D Geometry, ProfileSegmentProvenance Provenance);
public sealed record ResolvedProfileLoop2D(string Name, bool IsOuter, IReadOnlyList<ResolvedProfileSegment2D> Segments);
public sealed record ResolvedProfile2D(string Name, string PlaneFrame, IReadOnlyList<ResolvedProfileLoop2D> Loops, ConstructionPlane? ConstructionPlane = null, double? LocalStartDepth = null, double? LocalEndDepth = null,
IReadOnlyList<FirmamentV2.ProfileBoundaryEditEvidence>? BoundaryEdits = null)
{
public ConstructionPlane EffectiveConstructionPlane => ConstructionPlane ?? Materializer.ConstructionPlane.WorldXY;
}
public sealed record ResolvedProfileValidationResult(bool IsValid, double SignedArea, IReadOnlyList<string> Diagnostics);
public static class ResolvedProfile2DValidator
{
private const double Tol = 1e-7;
public static ResolvedProfileValidationResult Validate(ResolvedProfile2D profile)
{
var d = new List<string>();
if (profile.Loops.Count == 0 || profile.Loops.Count(x => x.IsOuter) != 1) d.Add($"profile:{profile.Name}: exactly one declared outer loop is required");
var outer = profile.Loops.SingleOrDefault(x => x.IsOuter);
if (outer is null) return new(false, 0, d.Append($"profile:{profile.Name}: outer loop is required").ToArray());
var names = new HashSet<string>(StringComparer.Ordinal);
foreach (var loop in profile.Loops)
{
if (loop.Segments.Count == 0 || (loop.Segments.Count < 2 && !(loop.Segments.Count == 1 && loop.Segments[0].Geometry is LineArcFullCircle2D or LineArcFullEllipse2D))) d.Add($"profile:{profile.Name}: loop '{loop.Name}' requires a closed boundary");
for (var i=0;i<loop.Segments.Count;i++)
{
var s=loop.Segments[i]; if(!names.Add(s.Name))d.Add($"profile:{profile.Name}: duplicate segment '{s.Name}'");
if (s.Geometry is LineArcFullCircle2D or LineArcFullEllipse2D && loop.Segments.Count == 1) continue;
if(!Endpoints(s.Geometry,out var start,out var end)){d.Add($"profile:{profile.Name}: unsupported unbounded segment '{s.Name}'");continue;}
if(Distance(start,end)<=Tol)d.Add($"profile:{profile.Name}: zero-length segment '{s.Name}'");
var next=loop.Segments[(i+1)%loop.Segments.Count]; if(Endpoints(next.Geometry,out var ns,out _)&&Distance(end,ns)>Tol)d.Add($"profile:{profile.Name}: endpoint mismatch '{s.Name}' -> '{next.Name}'");
}
for (var i=0;i<loop.Segments.Count;i++) for (var j=i+1;j<loop.Segments.Count;j++)
{
var a=loop.Segments[i]; var b=loop.Segments[j];
if (SameLine(a.Geometry,b.Geometry)) d.Add($"profile:{profile.Name}: duplicate coincident segment '{a.Name}' / '{b.Name}'");
if (!Adjacent(i,j,loop.Segments.Count) && a.Geometry is LineArcLineSegment2D && b.Geometry is LineArcLineSegment2D && Endpoints(a.Geometry,out var as_,out var ae) && Endpoints(b.Geometry,out var bs,out var be) && ProperIntersection(as_,ae,bs,be)) d.Add($"profile:{profile.Name}: self-intersection '{a.Name}' / '{b.Name}'");
}
}
var area=outer.Segments.Count == 1 && outer.Segments[0].Geometry is LineArcFullCircle2D circle ? Math.PI * circle.Radius * circle.Radius
: outer.Segments.Count == 1 && outer.Segments[0].Geometry is LineArcFullEllipse2D ellipse ? Math.PI * ellipse.MajorRadius * ellipse.MinorRadius
: outer.Segments.Sum(s=>SignedAreaContribution(s.Geometry));
if(area<=Tol)d.Add($"profile:{profile.Name}: outer winding must be CounterClockwise with nonzero area");
return new(d.Count==0,area,d);
}
public static LineArcProfileExtrudeResult Extrude(ResolvedProfile2D profile,double height)
{
var validation=Validate(profile); if(!validation.IsValid)return new(LineArcProfileExtrudeStatus.Rejected,null,validation.Diagnostics);
return LineArcProfileExtrudeEmitter.TryEmit(profile, height);
}
private static bool Endpoints(LineArcProfileCurve2D c,out (double X,double Y) a,out (double X,double Y) b){switch(c){case LineArcCubicBezier2D z:a=z.Start;b=z.End;return true;case LineArcLineSegment2D l:a=l.Start;b=l.End;return true;case LineArcCircularArc2D x:a=(x.Center.X+x.Radius*Math.Cos(x.StartAngleRadians),x.Center.Y+x.Radius*Math.Sin(x.StartAngleRadians));b=(x.Center.X+x.Radius*Math.Cos(x.StartAngleRadians+x.SweepAngleRadians),x.Center.Y+x.Radius*Math.Sin(x.StartAngleRadians+x.SweepAngleRadians));return true;default:a=default;b=default;return false;}}
private static double Distance((double X,double Y)a,(double X,double Y)b)=>Math.Sqrt((a.X-b.X)*(a.X-b.X)+(a.Y-b.Y)*(a.Y-b.Y));
internal static double SignedAreaContribution(LineArcProfileCurve2D curve)
{
if (curve is LineArcCubicBezier2D cubic)
{
// Three-point Gauss integrates x*y' - y*x' (degree <= 5) exactly.
double Integrand(double t)
{
var u = 1 - t;
var x = u*u*u*cubic.Start.X + 3*u*u*t*cubic.Control1.X + 3*u*t*t*cubic.Control2.X + t*t*t*cubic.End.X;
var y = u*u*u*cubic.Start.Y + 3*u*u*t*cubic.Control1.Y + 3*u*t*t*cubic.Control2.Y + t*t*t*cubic.End.Y;
var dx = 3*(u*u*(cubic.Control1.X-cubic.Start.X)+2*u*t*(cubic.Control2.X-cubic.Control1.X)+t*t*(cubic.End.X-cubic.Control2.X));
var dy = 3*(u*u*(cubic.Control1.Y-cubic.Start.Y)+2*u*t*(cubic.Control2.Y-cubic.Control1.Y)+t*t*(cubic.End.Y-cubic.Control2.Y));
return x*dy-y*dx;
}
var d = Math.Sqrt(3d/5)/2;
return (5*Integrand(0.5-d)+8*Integrand(0.5)+5*Integrand(0.5+d))/36;
}
if (curve is LineArcCircularArc2D arc)
{
var a=(X:arc.Center.X+arc.Radius*Math.Cos(arc.StartAngleRadians),Y:arc.Center.Y+arc.Radius*Math.Sin(arc.StartAngleRadians));
var b=(X:arc.Center.X+arc.Radius*Math.Cos(arc.StartAngleRadians+arc.SweepAngleRadians),Y:arc.Center.Y+arc.Radius*Math.Sin(arc.StartAngleRadians+arc.SweepAngleRadians));
return 0.5d*(arc.Center.X*(b.Y-a.Y)-arc.Center.Y*(b.X-a.X)+arc.Radius*arc.Radius*arc.SweepAngleRadians);
}
return Endpoints(curve,out var start,out var end)?0.5d*(start.X*end.Y-end.X*start.Y):0d;
}
private static bool Adjacent(int i,int j,int count)=>j==i+1 || (i==0 && j==count-1);
private static bool SameLine(LineArcProfileCurve2D a,LineArcProfileCurve2D b)=>a is LineArcLineSegment2D x&&b is LineArcLineSegment2D y&&((Distance(x.Start,y.Start)<=Tol&&Distance(x.End,y.End)<=Tol)||(Distance(x.Start,y.End)<=Tol&&Distance(x.End,y.Start)<=Tol));
private static bool ProperIntersection((double X,double Y)a,(double X,double Y)b,(double X,double Y)c,(double X,double Y)d)
{ var ab1=Orientation(a,b,c);var ab2=Orientation(a,b,d);var cd1=Orientation(c,d,a);var cd2=Orientation(c,d,b);return ab1*ab2 < -Tol && cd1*cd2 < -Tol; }
private static double Orientation((double X,double Y)a,(double X,double Y)b,(double X,double Y)c)=>(b.X-a.X)*(c.Y-a.Y)-(b.Y-a.Y)*(c.X-a.X);
}