Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

22.8 KB
Raw Blame
using System.Security.Cryptography;
using System.Text;
using Aetheris.Kernel.Core.Brep;
using Aetheris.Kernel.Core.Geometry;
using Aetheris.Kernel.Core.Geometry.Curves;
using Aetheris.Kernel.Core.Geometry.Surfaces;
using Aetheris.Kernel.Core.Math;
using Aetheris.Kernel.Core.Topology;

namespace Aetheris.Kernel.Core.Visualization;

public enum WireframeView { Isometric, Front, Top, Right, IsometricZUp }

public sealed record BrepWireframeOptions(
    WireframeView View = WireframeView.Isometric,
    int Density = 8,
    int CurveSamples = 48,
    int Width = 1200,
    int Height = 700,
    string Background = "#10151d",
    string IsoLineColor = "#79b8ff",
    string BoundaryColor = "#dbeafe",
    double BoundaryWidth = 1.35,
    double IsoLineWidth = 1,
    bool ShowLabel = true,
    bool Center = false);

public sealed record BrepWireframeEvidence(
    WireframeView View,
    int Density,
    int FaceCount,
    int FacesWithTrimmedIsolines,
    int EdgeCount,
    int IsoPolylineCount,
    int BoundaryPolylineCount,
    IReadOnlyDictionary<string, int> SurfaceFamilies,
    IReadOnlyList<string> UnsupportedSurfaceFamilies,
    IReadOnlyList<string> UnsupportedCurveFamilies,
    string Sha256);

public sealed record BrepWireframeResult(string Svg, BrepWireframeEvidence Evidence);

/// <summary>World-space presentation samples; exact topology remains authority.</summary>
public sealed record BrepWireframePolyline(string Identity, IReadOnlyList<Point3D> Points, int? EdgeId = null, int? FaceId = null);
public sealed record BrepWireframeGeometry(IReadOnlyList<BrepWireframePolyline> Boundaries,
    IReadOnlyList<BrepWireframePolyline> Isolines, BrepWireframeEvidence Evidence);

/// <summary>
/// Deterministic diagnostic SVG renderer for exact BRep models. Topology edges are sampled from
/// their authoritative 3D curves. Face isolines are sampled from exact supports and clipped in UV
/// with the face's pcurve loops using the even/odd trim rule. The SVG is evidence/inspection output,
/// not a tessellated product representation or a hidden-line engineering drawing.
/// </summary>
public static class BrepWireframeSvgRenderer
{
    /// <summary>Project already sampled topology edges with the same SVG view/framing owner.
    /// Callers retain responsibility for world placement and edge provenance; no triangle edges,
    /// surface isolines or hidden-line removal are inferred from these polylines.</summary>
    public static string RenderEdges(IReadOnlyList<IReadOnlyList<Point3D>> edges, BrepWireframeOptions? options = null)
    {
        ArgumentNullException.ThrowIfNull(edges);
        options ??= new();
        if (options.Width < 320 || options.Height < 240 || !double.IsFinite(options.BoundaryWidth) || options.BoundaryWidth <= 0)
            throw new ArgumentOutOfRangeException(nameof(options));
        if (edges.Any(edge => edge.Count < 2 || edge.Any(p => !double.IsFinite(p.X) || !double.IsFinite(p.Y) || !double.IsFinite(p.Z))))
            throw new ArgumentException("Topology polylines require at least two finite points.", nameof(edges));
        var all = edges.SelectMany(edge => edge).ToArray();
        if (all.Length == 0) throw new ArgumentException("No topology edges were supplied.", nameof(edges));
        return ComposeSvg(all, [], edges, options);
    }

    public static BrepWireframeResult Render(BrepBody body, BrepWireframeOptions? options = null)
    {
        options ??= new();
        var geometry = SampleGeometry(body, options);
        var boundaries = geometry.Boundaries.Select(line => line.Points).ToArray();
        var iso = geometry.Isolines.Select(line => line.Points).ToArray();
        var svg = ComposeSvg(boundaries.Concat(iso).SelectMany(points => points).ToArray(), iso, boundaries, options);
        var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(svg))).ToLowerInvariant();
        return new(svg, geometry.Evidence with { Sha256 = hash });
    }

    public static BrepWireframeGeometry SampleGeometry(BrepBody body, BrepWireframeOptions? options = null)
    {
        ArgumentNullException.ThrowIfNull(body);
        options ??= new();
        if (options.Density is < 2 or > 32) throw new ArgumentOutOfRangeException(nameof(options), "Wireframe density must be between 2 and 32.");
        if (options.CurveSamples is < 8 or > 256) throw new ArgumentOutOfRangeException(nameof(options), "CurveSamples must be between 8 and 256.");
        if (options.Width < 320 || options.Height < 240) throw new ArgumentOutOfRangeException(nameof(options), "Wireframe dimensions are too small.");
        if (!double.IsFinite(options.BoundaryWidth) || options.BoundaryWidth <= 0 || !double.IsFinite(options.IsoLineWidth) || options.IsoLineWidth <= 0)
            throw new ArgumentOutOfRangeException(nameof(options), "Wireframe stroke widths must be finite and positive.");

        var iso = new List<IReadOnlyList<Point3D>>();
        var boundaries = new List<IReadOnlyList<Point3D>>();
        var unsupported = new SortedSet<string>(StringComparer.Ordinal);
        var unsupportedCurves = new SortedSet<string>(StringComparer.Ordinal);
        var families = body.Topology.Faces.Select(face => body.TryGetFaceSurfaceGeometry(face.Id, out var surface) ? surface?.Kind.ToString() ?? "Unbound" : "Unbound")
            .GroupBy(name => name, StringComparer.Ordinal).ToDictionary(group => group.Key, group => group.Count(), StringComparer.Ordinal);
        var trimmedFaces = 0;
        var boundaryOwners = new List<int>();
        var isoOwners = new List<int>();

        foreach (var edge in body.Topology.Edges.OrderBy(edge => edge.Id.Value))
        {
            if (!body.Bindings.TryGetEdgeBinding(edge.Id, out var binding)
                || !body.Geometry.TryGetCurve(binding.CurveGeometryId, out var curve) || curve is null) continue;
            if (!CanEvaluate(curve)) { unsupportedCurves.Add(curve.UnsupportedKind ?? curve.Kind.ToString()); continue; }
            var interval = binding.TrimInterval ?? CurveDomain(curve);
            if (interval is null) continue;
            var points = SampleCurve(curve, interval.Value, options.CurveSamples).ToArray();
            if (points.Length > 1) { boundaries.Add(points); boundaryOwners.Add(edge.Id.Value); }
        }

        foreach (var face in body.Topology.Faces.OrderBy(face => face.Id.Value))
        {
            if (!body.Bindings.TryGetFaceBinding(face.Id, out var faceBinding)
                || !body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var surface) || surface is null) continue;
            var raw = TrimLoops(body, face).ToArray();
            if (raw.Length == 0 || raw.All(loop => loop.Count < 3)) continue;
            var chart = TrimChart.Of(raw, SurfacePeriodicity.Of(surface));
            if (chart is null) continue;
            var loops = chart.Loops;
            var (u0, u1, v0, v1) = (chart.U0, chart.U1, chart.V0, chart.V1);
            if (!double.IsFinite(u0) || !double.IsFinite(u1) || !double.IsFinite(v0) || !double.IsFinite(v1)
                || u1 - u0 <= 1e-12d || v1 - v0 <= 1e-12d) continue;
            if (!CanEvaluate(surface)) { unsupported.Add(surface.Kind.ToString()); continue; }
            var before = iso.Count;
            for (var line = 1; line < options.Density; line++)
            {
                var u = u0 + (u1 - u0) * line / options.Density;
                AddTrimmedIso(iso, chart, options.CurveSamples, t => new(u, v0 + (v1 - v0) * t), point => Evaluate(surface, point.U, point.V));
                var v = v0 + (v1 - v0) * line / options.Density;
                AddTrimmedIso(iso, chart, options.CurveSamples, t => new(u0 + (u1 - u0) * t, v), point => Evaluate(surface, point.U, point.V));
            }
            if (iso.Count > before) { trimmedFaces++; isoOwners.AddRange(Enumerable.Repeat(face.Id.Value, iso.Count - before)); }
        }

        var all = boundaries.Concat(iso).SelectMany(points => points).ToArray();
        if (all.Length == 0) throw new InvalidOperationException("The BRep contains no renderable bound curves or trimmed surface supports.");
        return new(boundaries.Select((points, i) => new BrepWireframePolyline($"edge:{boundaryOwners[i]}", points, boundaryOwners[i])).ToArray(),
            iso.Select((points, i) => new BrepWireframePolyline($"face:{isoOwners[i]}:iso:{i}", points, FaceId: isoOwners[i])).ToArray(),
            new(options.View, options.Density, body.Topology.Faces.Count(), trimmedFaces,
                body.Topology.Edges.Count(), iso.Count, boundaries.Count, families, unsupported.ToArray(), unsupportedCurves.ToArray(), ""));
    }

    private static IEnumerable<IReadOnlyList<SurfaceParameterPoint>> TrimLoops(BrepBody body, Face face)
    {
        foreach (var loopId in face.LoopIds)
        {
            var loop = body.Topology.GetLoop(loopId); var points = new List<SurfaceParameterPoint>();
            foreach (var coedgeId in loop.CoedgeIds)
            {
                var coedge = body.Topology.GetCoedge(coedgeId);
                if (!body.Bindings.TryGetPcurveBinding(coedgeId, out var binding)) continue;
                var reverse = coedge.IsReversed == binding.SameSense;
                for (var sample = 0; sample <= 16; sample++)
                {
                    var fraction = sample / 16d;
                    var parameter = reverse
                        ? binding.Pcurve.Domain.End - (binding.Pcurve.Domain.End - binding.Pcurve.Domain.Start) * fraction
                        : binding.Pcurve.Domain.Start + (binding.Pcurve.Domain.End - binding.Pcurve.Domain.Start) * fraction;
                    var point = binding.Pcurve.Evaluate(parameter);
                    if (points.Count == 0 || DistanceSquared(points[^1], point) > 1e-20d) points.Add(point);
                }
            }
            if (points.Count >= 3) yield return points;
        }
    }

    private static void AddTrimmedIso(List<IReadOnlyList<Point3D>> output, TrimChart loops,
        int samples, Func<double, SurfaceParameterPoint> parameter, Func<SurfaceParameterPoint, Point3D> evaluate)
    {
        List<Point3D>? run = null;
        for (var index = 0; index < samples; index++)
        {
            var a = parameter(index / (double)samples); var b = parameter((index + 1d) / samples);
            var midpoint = new SurfaceParameterPoint((a.U + b.U) / 2d, (a.V + b.V) / 2d);
            if (loops.Inside(midpoint))
            {
                run ??= [];
                if (run.Count == 0) run.Add(evaluate(a));
                run.Add(evaluate(b));
            }
            else if (run is { Count: > 1 }) { output.Add(run); run = null; }
            else run = null;
        }
        if (run is { Count: > 1 }) output.Add(run);
    }

    /// <summary>
    /// A face's trim loops in its own parameter window. Each periodic parameter is windowed around the face's
    /// circular mean. Where a loop runs once round a period (a cylinder's rims, a torus band's rings) the face is a
    /// band in that parameter: its loops do not close as polygons in the plane, so points are classified by a ray
    /// along the other parameter, counting the loop segments it crosses with their spans taken modulo the period
    /// (seams run along the ray and never count). Otherwise the even/odd polygon rule applies in the window.
    /// </summary>
    private sealed class TrimChart
    {
        public required IReadOnlyList<IReadOnlyList<SurfaceParameterPoint>> Loops { get; init; }
        public double? UPeriod { get; init; }
        public double? VPeriod { get; init; }
        public bool WrapU { get; init; }
        public bool WrapV { get; init; }
        public double U0 { get; init; }
        public double U1 { get; init; }
        public double V0 { get; init; }
        public double V1 { get; init; }

        public static TrimChart? Of(IReadOnlyList<IReadOnlyList<SurfaceParameterPoint>> raw, SurfacePeriodicity periodicity)
        {
            var all = raw.SelectMany(loop => loop).ToArray();
            static double Mean(IEnumerable<double> values, double period)
            {
                var (c, s) = values.Aggregate((0d, 0d), (sum, x) => (sum.Item1 + double.Cos(x * double.Tau / period), sum.Item2 + double.Sin(x * double.Tau / period)));
                return double.Atan2(s, c) * period / double.Tau;
            }
            var cu = periodicity.UPeriod is { } pu ? Mean(all.Select(p => p.U), pu) : 0d;
            var cv = periodicity.VPeriod is { } pv ? Mean(all.Select(p => p.V), pv) : 0d;
            double Window(double x, double centre, double? period) => period is { } t ? centre + Wrap(x - centre, t) : x;
            // A band: walked continuously, some loop covers a whole period (a rim goes once round; the loop may come
            // back round the other rim, so its net turn can be zero).
            bool Wraps(Func<SurfaceParameterPoint, double> axis, double? period) => period is { } t && raw.Any(loop =>
            {
                var (x, low, high) = (0d, 0d, 0d);
                for (var i = 1; i < loop.Count; i++) { x += Wrap(axis(loop[i]) - axis(loop[i - 1]), t); low = double.Min(low, x); high = double.Max(high, x); }
                return high - low > t * (1 - 1e-6);
            });
            var (wrapU, wrapV) = (Wraps(p => p.U, periodicity.UPeriod), Wraps(p => p.V, periodicity.VPeriod));
            if (wrapU && wrapV) return null; // a whole torus: nothing to trim against in either direction
            var loops = raw.Select(loop => (IReadOnlyList<SurfaceParameterPoint>)loop.Select(p => new SurfaceParameterPoint(Window(p.U, cu, periodicity.UPeriod), Window(p.V, cv, periodicity.VPeriod))).ToArray()).ToArray();
            var flat = loops.SelectMany(loop => loop).ToArray();
            return new TrimChart
            {
                Loops = loops, UPeriod = periodicity.UPeriod, VPeriod = periodicity.VPeriod, WrapU = wrapU, WrapV = wrapV,
                U0 = wrapU ? cu - periodicity.UPeriod!.Value / 2 : flat.Min(p => p.U), U1 = wrapU ? cu + periodicity.UPeriod!.Value / 2 : flat.Max(p => p.U),
                V0 = wrapV ? cv - periodicity.VPeriod!.Value / 2 : flat.Min(p => p.V), V1 = wrapV ? cv + periodicity.VPeriod!.Value / 2 : flat.Max(p => p.V),
            };
        }

        public bool Inside(SurfaceParameterPoint point)
        {
            if (WrapU) return Band(point.U, point.V, p => p.U, p => p.V, UPeriod!.Value);
            if (WrapV) return Band(point.V, point.U, p => p.V, p => p.U, VPeriod!.Value);
            return InsideTrim(point, Loops);
        }

        /// <summary>Parity of loop segments crossed by the ray from the point along increasing <paramref name="across"/>.</summary>
        private bool Band(double along, double across, Func<SurfaceParameterPoint, double> wrapped, Func<SurfaceParameterPoint, double> other, double period)
        {
            var crossings = 0;
            foreach (var loop in Loops)
                for (var i = 0; i < loop.Count; i++)
                {
                    var (a, b) = (loop[i], loop[(i + 1) % loop.Count]);
                    var delta = Wrap(wrapped(b) - wrapped(a), period);
                    if (double.Abs(delta) <= 1e-12) continue; // along the ray: a seam or a generator
                    var (start, span, from, to) = delta > 0 ? (wrapped(a), delta, other(a), other(b)) : (wrapped(b), -delta, other(b), other(a));
                    var offset = Modulo(along - start, period);
                    if (offset >= span) continue;
                    if (from + (to - from) * (offset / span) > across) crossings++;
                }
            return crossings % 2 == 1;
        }

        private static double Wrap(double x, double period) { var y = Modulo(x + period / 2, period) - period / 2; return y; }

        private static double Modulo(double x, double period) { var y = x % period; return y < 0 ? y + period : y; }
    }

    private static bool InsideTrim(SurfaceParameterPoint point, IReadOnlyList<IReadOnlyList<SurfaceParameterPoint>> loops)
    {
        var crossings = 0;
        foreach (var polygon in loops)
        {
            var inside = false;
            for (var i = 0; i < polygon.Count; i++)
            {
                var a = polygon[i]; var b = polygon[(i + 1) % polygon.Count];
                if ((a.V > point.V) == (b.V > point.V)) continue;
                var intersection = (b.U - a.U) * (point.V - a.V) / (b.V - a.V) + a.U;
                if (point.U < intersection) inside = !inside;
            }
            if (inside) crossings++;
        }
        return crossings % 2 == 1;
    }

    private static string ComposeSvg(IReadOnlyList<Point3D> all, IReadOnlyList<IReadOnlyList<Point3D>> iso,
        IReadOnlyList<IReadOnlyList<Point3D>> boundaries, BrepWireframeOptions options)
    {
        var projected = all.Select(point => Project(point, options.View)).ToArray();
        var minX = projected.Min(p => p.X); var maxX = projected.Max(p => p.X);
        var minY = projected.Min(p => p.Y); var maxY = projected.Max(p => p.Y);
        const double margin = 42d;
        var scale = System.Math.Min((options.Width - 2d * margin) / System.Math.Max(maxX - minX, 1e-9d),
            (options.Height - 2d * margin) / System.Math.Max(maxY - minY, 1e-9d));
        var left = options.Center ? (options.Width - (maxX - minX) * scale) / 2 : margin;
        var top = options.Center ? (options.Height - (maxY - minY) * scale) / 2 : margin;
        string P(Point3D point)
        {
            var p = Project(point, options.View);
            return FormattableString.Invariant($"{left + (p.X - minX) * scale:F2},{top + (p.Y - minY) * scale:F2}");
        }
        var svg = new StringBuilder();
        svg.Append("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"").Append(options.Width).Append("\" height=\"").Append(options.Height)
            .Append("\" viewBox=\"0 0 ").Append(options.Width).Append(' ').Append(options.Height).Append("\"><rect width=\"100%\" height=\"100%\" fill=\"")
            .Append(options.Background).Append("\"/><g fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\">");
        foreach (var line in iso) svg.Append("<polyline points=\"").Append(string.Join(' ', line.Select(P))).Append("\" stroke=\"")
            .Append(options.IsoLineColor).Append("\" stroke-width=\"").Append(options.IsoLineWidth.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append("\" opacity=\".43\"/>");
        foreach (var line in boundaries) svg.Append("<polyline points=\"").Append(string.Join(' ', line.Select(P))).Append("\" stroke=\"")
            .Append(options.BoundaryColor).Append("\" stroke-width=\"").Append(options.BoundaryWidth.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append("\" opacity=\".88\"/>");
        svg.Append("</g>");
        if (options.ShowLabel) svg.Append("<text x=\"24\" y=\"").Append(options.Height - 20).Append("\" fill=\"").Append(options.BoundaryColor)
            .Append("\" opacity=\".78\" font-family=\"ui-monospace,monospace\" font-size=\"14\">Aetheris exact BRep wireframe · ")
            .Append(options.View).Append(" · density ").Append(options.Density).Append("</text>");
        svg.Append("</svg>");
        return svg.ToString();
    }

    private static (double X, double Y) Project(Point3D p, WireframeView view) => view switch
    {
        WireframeView.Front => (p.X, -p.Z),
        WireframeView.Top => (p.X, -p.Y),
        WireframeView.Right => (p.Y, -p.Z),
        WireframeView.IsometricZUp => ((p.X - p.Y) / System.Math.Sqrt(2), (p.X + p.Y) / System.Math.Sqrt(6) - p.Z * System.Math.Sqrt(2d / 3)),
        _ => (p.Z + .45d * p.X, -p.Y + .28d * p.X - .18d * p.Z)
    };

    private static bool CanEvaluate(SurfaceGeometry surface) => surface.Kind switch
    {
        SurfaceGeometryKind.LinearExtrusion => CanEvaluate(surface.LinearExtrusion!.Value.Directrix),
        SurfaceGeometryKind.SurfaceOfRevolution => CanEvaluate(surface.SurfaceOfRevolution!.Value.Directrix),
        _ => surface.Kind is SurfaceGeometryKind.Plane or SurfaceGeometryKind.Cylinder or SurfaceGeometryKind.Cone
            or SurfaceGeometryKind.Sphere or SurfaceGeometryKind.Torus or SurfaceGeometryKind.BSplineSurfaceWithKnots
    };

    private static bool CanEvaluate(CurveGeometry curve) => curve.Kind is CurveGeometryKind.Line3 or CurveGeometryKind.Circle3
        or CurveGeometryKind.BSpline3 or CurveGeometryKind.Ellipse3 or CurveGeometryKind.Hyperbola3;

    private static Point3D Evaluate(SurfaceGeometry surface, double u, double v) => surface.Kind switch
    {
        SurfaceGeometryKind.Plane => surface.Plane!.Value.Evaluate(u, v),
        SurfaceGeometryKind.Cylinder => surface.Cylinder!.Value.Evaluate(u, v),
        SurfaceGeometryKind.Cone => surface.Cone!.Value.Evaluate(u, v),
        SurfaceGeometryKind.Sphere => surface.Sphere!.Value.Evaluate(u, v),
        SurfaceGeometryKind.Torus => surface.Torus!.Value.Evaluate(u, v),
        SurfaceGeometryKind.BSplineSurfaceWithKnots => surface.BSplineSurfaceWithKnots!.Evaluate(u, v),
        SurfaceGeometryKind.LinearExtrusion => EvaluateLinearExtrusion(surface.LinearExtrusion!.Value, u, v),
        SurfaceGeometryKind.SurfaceOfRevolution => EvaluateRevolution(surface.SurfaceOfRevolution!.Value, u, v),
        _ => throw new InvalidOperationException($"Unsupported surface family {surface.Kind}.")
    };

    private static Point3D EvaluateLinearExtrusion(LinearExtrusionSurface surface, double u, double v) =>
        EvaluateCurve(surface.Directrix, u) + surface.ExtrusionVector * v;

    private static Point3D EvaluateRevolution(SurfaceOfRevolutionSurface surface, double u, double v)
    {
        var point = EvaluateCurve(surface.Directrix, u); var axis = surface.AxisDirection.ToVector();
        var offset = point - surface.AxisOrigin; var axial = axis * offset.Dot(axis); var radial = offset - axial;
        return surface.AxisOrigin + axial + radial * System.Math.Cos(v) + axis.Cross(radial) * System.Math.Sin(v);
    }

    private static IEnumerable<Point3D> SampleCurve(CurveGeometry curve, ParameterInterval interval, int samples)
    {
        for (var i = 0; i <= samples; i++) yield return EvaluateCurve(curve, interval.Start + (interval.End - interval.Start) * i / samples);
    }

    private static Point3D EvaluateCurve(CurveGeometry curve, double parameter) => curve.Kind switch
    {
        CurveGeometryKind.Line3 => curve.Line3!.Value.Evaluate(parameter),
        CurveGeometryKind.Circle3 => curve.Circle3!.Value.Evaluate(parameter),
        CurveGeometryKind.BSpline3 => curve.BSpline3!.Value.Evaluate(parameter),
        CurveGeometryKind.Ellipse3 => curve.Ellipse3!.Value.Evaluate(parameter),
        CurveGeometryKind.Hyperbola3 => curve.Hyperbola3!.Value.Evaluate(parameter),
        _ => throw new InvalidOperationException($"Unsupported curve family {curve.Kind}.")
    };

    private static ParameterInterval? CurveDomain(CurveGeometry curve) => curve.Kind switch
    {
        CurveGeometryKind.BSpline3 => new(curve.BSpline3!.Value.DomainStart, curve.BSpline3.Value.DomainEnd),
        _ => null
    };

    private static double DistanceSquared(SurfaceParameterPoint a, SurfaceParameterPoint b)
    {
        var u = a.U - b.U; var v = a.V - b.V; return u * u + v * v;
    }
}