Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

27.5 KB
Raw Blame
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.Numerics;
using Aetheris.Kernel.Core.Topology;
using Aetheris.Kernel.Core.Brep.Tessellation;

namespace Aetheris.Kernel.Core.Brep.Queries;

internal sealed class AnalyticPlanarFaceDomain
{
    private readonly PlaneSurface _plane;
    private readonly (Point3D Min, Point3D Max)? _axisBounds;
    private readonly IReadOnlyList<(double U, double V)> _outerPolygon;
    private readonly IReadOnlyList<IReadOnlyList<(double U, double V)>> _innerPolygons;
    private readonly (Point3D Center, double Radius)? _singleCircularBoundary;
    private readonly IReadOnlyList<(Point3D Center, double Radius)> _circularExclusionBoundaries;

    private AnalyticPlanarFaceDomain(
        PlaneSurface plane,
        (Point3D Min, Point3D Max)? axisBounds,
        IReadOnlyList<(double U, double V)> outerPolygon,
        IReadOnlyList<IReadOnlyList<(double U, double V)>> innerPolygons,
        (Point3D Center, double Radius)? singleCircularBoundary,
        IReadOnlyList<(Point3D Center, double Radius)> circularExclusionBoundaries)
    {
        _plane = plane;
        _axisBounds = axisBounds;
        _outerPolygon = outerPolygon;
        _innerPolygons = innerPolygons;
        _singleCircularBoundary = singleCircularBoundary;
        _circularExclusionBoundaries = circularExclusionBoundaries;
    }

    public static bool TryCreate(BrepBody body, FaceId faceId, PlaneSurface plane, out AnalyticPlanarFaceDomain domain)
    {
        domain = null!;
        if (!body.Topology.TryGetFace(faceId, out var face) || face is null)
        {
            return false;
        }

        if (TryBuildPolygonLoops(body, face, plane, out var outerPolygon, out var innerPolygons))
        {
            domain = new AnalyticPlanarFaceDomain(
                plane,
                axisBounds: null,
                outerPolygon,
                innerPolygons,
                singleCircularBoundary: null,
                circularExclusionBoundaries: []);
            return true;
        }

        var circularBoundaries = TryResolveCircularBoundaries(body, face);
        if (face.LoopIds.Count == 1 && circularBoundaries.Count == 1)
        {
            domain = new AnalyticPlanarFaceDomain(
                plane,
                axisBounds: null,
                outerPolygon: [],
                innerPolygons: [],
                singleCircularBoundary: circularBoundaries[0],
                circularExclusionBoundaries: []);
            return true;
        }

        if (IsAxisAlignedPlane(plane.Normal.ToVector()))
        {
            TryResolveAxisAlignedBoxBounds(body, out var min, out var max);
            domain = new AnalyticPlanarFaceDomain(
                plane,
                (min, max),
                outerPolygon: [],
                innerPolygons: [],
                singleCircularBoundary: null,
                circularExclusionBoundaries: circularBoundaries);
            return true;
        }

        return false;
    }

    internal static bool TryGetOuterBoundaryWorld(BrepBody body, FaceId faceId, PlaneSurface plane, out IReadOnlyList<Point3D> outerBoundary)
    {
        outerBoundary = [];
        if (!body.Topology.TryGetFace(faceId, out var face) || face is null || face.LoopIds.Count == 0)
        {
            return false;
        }

        var loops = new List<(IReadOnlyList<Point3D> Vertices, IReadOnlyList<(double U, double V)> Projected, double Area)>(face.LoopIds.Count);
        foreach (var loopId in face.LoopIds)
        {
            var loop = body.Topology.GetLoop(loopId);
            if (!TryBuildPolygonLoopDetailed(body, loop, plane, out var vertices, out var projected)
                && !TryBuildLoopPolylineDetailed(body, loop, plane, out vertices, out projected))
            {
                return false;
            }

            loops.Add((vertices, projected, double.Abs(SignedArea(projected))));
        }

        var ordered = loops.OrderByDescending(entry => entry.Area).ToArray();
        if (ordered.Length == 0 || ordered[0].Area <= 1e-9d)
        {
            return false;
        }

        var candidateOuter = ordered[0].Projected;
        for (var i = 1; i < ordered.Length; i++)
        {
            if (!ordered[i].Projected.All(vertex => ContainsPoint(candidateOuter, vertex, ToleranceContext.Default)))
            {
                return false;
            }
        }

        outerBoundary = ordered[0].Vertices;
        return true;
    }

    public bool Contains(Point3D point, ToleranceContext tolerance)
    {
        if (_outerPolygon.Count > 0)
        {
            var uv = ProjectToPlane(point, _plane);
            if (!ContainsPoint(_outerPolygon, uv, tolerance))
            {
                return false;
            }

            foreach (var hole in _innerPolygons)
            {
                if (ContainsPoint(hole, uv, tolerance))
                {
                    return false;
                }
            }

            return true;
        }

        if (_singleCircularBoundary.HasValue)
        {
            return IsPointInsideCircularBoundary(point, _plane, _singleCircularBoundary.Value.Center, _singleCircularBoundary.Value.Radius, tolerance);
        }

        if (_axisBounds.HasValue)
        {
            if (!IsPointInsideAxisBounds(point, _plane.Normal.ToVector(), _axisBounds.Value.Min, _axisBounds.Value.Max, tolerance))
            {
                return false;
            }
        }

        foreach (var boundary in _circularExclusionBoundaries)
        {
            if (IsPointInsideCircularBoundary(point, _plane, boundary.Center, boundary.Radius, tolerance))
            {
                return false;
            }
        }

        return true;
    }

    // Unlike the display domain, admission never falls back to whole-body bounds.
    // Arc chords preserve winding only when the probe is separated by more than
    // their certified sag plus the imported endpoint uncertainty.
    internal static bool TryContainsInPlanarFace(BrepBody body, FaceId faceId,
        Point3D point, ToleranceContext tolerance, LoopId removedInnerLoop, out bool inside)
    {
        inside = false;
        var face = body.Topology.GetFace(faceId);
        if (face.LoopIds.Any(id => !body.Bindings.TryGetFaceBoundaryRoleBinding(id, out _))) return false;
        var outers = face.LoopIds.Where(id => body.Bindings.GetFaceBoundaryRoleBinding(id).Role == FaceBoundaryRole.Outer).ToArray();
        if (outers.Length != 1 || body.GetFaceSurface(faceId).Plane is not { } plane) return false;
        if (!TryClassifyLoop(outers[0], out inside)) return false;
        if (!inside) return true;
        foreach (var loopId in face.LoopIds.Where(id => id != outers[0] && id != removedInnerLoop))
        {
            if (body.Bindings.GetFaceBoundaryRoleBinding(loopId).Role != FaceBoundaryRole.Inner
                || !TryClassifyLoop(loopId, out var inHole)) return false;
            if (inHole) { inside = false; return true; }
        }
        return true;

        bool TryClassifyLoop(LoopId loopId, out bool contains)
        {
            contains = false;
            var points = new List<(double U, double V)>();
            var deviation = tolerance.Linear;
            foreach (var coedgeId in body.Topology.GetLoop(loopId).CoedgeIds)
            {
                var coedge = body.Topology.GetCoedge(coedgeId);
                var edge = body.Topology.GetEdge(coedge.EdgeId);
                if (!body.TryGetVertexPoint(coedge.IsReversed ? edge.EndVertexId : edge.StartVertexId, out var start)
                    || !body.TryGetVertexPoint(coedge.IsReversed ? edge.StartVertexId : edge.EndVertexId, out var end)) return false;
                var curve = body.GetEdgeCurve(edge.Id);
                if (curve.Kind == CurveGeometryKind.Line3) points.Add(ProjectToPlane(start, plane));
                else if (curve.Circle3 is { } circle)
                {
                    var binding = body.Bindings.GetEdgeBinding(edge.Id);
                    if (binding.TrimInterval is not { } interval || System.Math.Abs(interval.End - interval.Start) > 2 * double.Pi + tolerance.Angular) return false;
                    var samples = CurveSampler.SampleCircleArc(circle, interval.Start, interval.End - interval.Start).ToArray();
                    if (coedge.IsReversed == binding.OrientedEdgeSense) Array.Reverse(samples);
                    if ((samples[0] - start).Length > tolerance.Linear || (samples[^1] - end).Length > tolerance.Linear) return false;
                    var step = System.Math.Abs(interval.End - interval.Start) / (samples.Length - 1);
                    if (step > double.Pi) return false;
                    deviation = System.Math.Max(deviation, circle.Radius * (1 - double.Cos(step / 2)) + tolerance.Linear);
                    for (var i = 0; i < samples.Length - 1; i++) points.Add(ProjectToPlane(samples[i], plane));
                }
                else return false;
            }
            if (points.Count < 3) return false;
            var uv = ProjectToPlane(point, plane);
            if (Enumerable.Range(0, points.Count).Any(i => DistancePointToSegmentSquared(uv, points[i], points[(i + 1) % points.Count]) <= deviation * deviation)) return false;
            contains = ContainsPoint(points, uv, tolerance);
            return true;
        }
    }


    public bool IsNearBoundary(Point3D point, ToleranceContext tolerance, out double boundaryDistance, out bool nearVertex)
    {
        boundaryDistance = double.PositiveInfinity;
        nearVertex = false;
        var sqTol = tolerance.Linear * tolerance.Linear;

        if (_outerPolygon.Count > 0)
        {
            var uv = ProjectToPlane(point, _plane);
            foreach (var polygon in EnumerateAllPolygons())
            {
                for (var i = 0; i < polygon.Count; i++)
                {
                    var a = polygon[i];
                    var b = polygon[(i + 1) % polygon.Count];
                    var d2 = DistancePointToSegmentSquared(uv, a, b);
                    if (d2 < boundaryDistance * boundaryDistance)
                    {
                        boundaryDistance = double.Sqrt(d2);
                    }

                    if (d2 <= sqTol)
                    {
                        nearVertex = nearVertex || DistancePointToPointSquared(uv, a) <= sqTol || DistancePointToPointSquared(uv, b) <= sqTol;
                        return true;
                    }
                }
            }

            if (!double.IsFinite(boundaryDistance))
            {
                boundaryDistance = double.PositiveInfinity;
            }

            return false;
        }

        if (_singleCircularBoundary.HasValue)
        {
            var radial = point - _singleCircularBoundary.Value.Center;
            var height = radial.Dot(_plane.Normal.ToVector());
            var inPlaneRadial = radial - (_plane.Normal.ToVector() * height);
            var distance = double.Abs(double.Sqrt(inPlaneRadial.Dot(inPlaneRadial)) - _singleCircularBoundary.Value.Radius);
            boundaryDistance = distance;
            return distance <= tolerance.Linear;
        }

        return false;
    }

    private IEnumerable<IReadOnlyList<(double U, double V)>> EnumerateAllPolygons()
    {
        if (_outerPolygon.Count > 0)
        {
            yield return _outerPolygon;
        }

        foreach (var hole in _innerPolygons)
        {
            yield return hole;
        }
    }

    private static bool TryBuildPolygonLoops(
        BrepBody body,
        Face face,
        PlaneSurface plane,
        out IReadOnlyList<(double U, double V)> outerPolygon,
        out IReadOnlyList<IReadOnlyList<(double U, double V)>> innerPolygons)
    {
        outerPolygon = [];
        innerPolygons = [];
        if (face.LoopIds.Count == 0)
        {
            return false;
        }

        var loopPolygons = new List<IReadOnlyList<(double U, double V)>>(face.LoopIds.Count);
        foreach (var loopId in face.LoopIds)
        {
            var loop = body.Topology.GetLoop(loopId);
            if (!TryBuildPolygonLoop(body, loop, plane, out var polygon))
            {
                return false;
            }

            loopPolygons.Add(polygon);
        }

        var sorted = loopPolygons
            .Select(loop => (Loop: loop, Area: double.Abs(SignedArea(loop))))
            .OrderByDescending(entry => entry.Area)
            .ToArray();

        if (sorted.Length == 0 || sorted[0].Area <= 1e-9d)
        {
            return false;
        }

        var outer = sorted[0].Loop;
        var holes = new List<IReadOnlyList<(double U, double V)>>(sorted.Length - 1);
        for (var i = 1; i < sorted.Length; i++)
        {
            var candidate = sorted[i].Loop;
            if (!candidate.All(vertex => ContainsPoint(outer, vertex, ToleranceContext.Default)))
            {
                return false;
            }

            holes.Add(candidate);
        }

        outerPolygon = outer;
        innerPolygons = holes;
        return true;
    }

    private static bool TryBuildPolygonLoop(BrepBody body, Loop loop, PlaneSurface plane, out IReadOnlyList<(double U, double V)> polygon)
    {
        polygon = [];
        if (!TryBuildPolygonLoopDetailed(body, loop, plane, out _, out var projected))
        {
            return false;
        }

        polygon = projected;
        return true;
    }

    private static bool TryBuildPolygonLoopDetailed(
        BrepBody body,
        Loop loop,
        PlaneSurface plane,
        out IReadOnlyList<Point3D> vertices3d,
        out IReadOnlyList<(double U, double V)> projected)
    {
        vertices3d = [];
        projected = [];
        if (loop.CoedgeIds.Count < 3)
        {
            return false;
        }

        var lines = new List<(Point3D Origin, Vector3D Direction)>(loop.CoedgeIds.Count);
        foreach (var coedgeId in loop.CoedgeIds)
        {
            var coedge = body.Topology.GetCoedge(coedgeId);
            if (!body.TryGetEdgeCurveGeometry(coedge.EdgeId, out var curve)
                || curve is null
                || curve.Kind != CurveGeometryKind.Line3)
            {
                return false;
            }

            var line = curve.Line3!.Value;
            var direction = coedge.IsReversed ? line.Direction.ToVector() * -1d : line.Direction.ToVector();
            if (direction.Length <= 1e-12d)
            {
                return false;
            }

            lines.Add((line.Origin, direction));
        }

        var vertices = new List<Point3D>(lines.Count);
        for (var i = 0; i < lines.Count; i++)
        {
            var current = lines[i];
            var next = lines[(i + 1) % lines.Count];
            var current2d = ProjectToPlane(current.Origin, plane);
            var next2d = ProjectToPlane(next.Origin, plane);
            var currentDir2d = (
                current.Direction.Dot(plane.UAxis.ToVector()),
                current.Direction.Dot(plane.VAxis.ToVector()));
            var nextDir2d = (
                next.Direction.Dot(plane.UAxis.ToVector()),
                next.Direction.Dot(plane.VAxis.ToVector()));
            if (!TryIntersectLines2D(current2d, currentDir2d, next2d, nextDir2d, out var intersection2d))
            {
                return false;
            }

            var point = plane.Origin + (plane.UAxis.ToVector() * intersection2d.U) + (plane.VAxis.ToVector() * intersection2d.V);
            vertices.Add(point);
        }

        if (vertices.Count < 3)
        {
            return false;
        }

        var uvVertices = new List<(double U, double V)>(vertices.Count);
        foreach (var vertex in vertices)
        {
            uvVertices.Add(ProjectToPlane(vertex, plane));
        }

        if (double.Abs(SignedArea(uvVertices)) <= 1e-9d)
        {
            return false;
        }

        vertices3d = vertices;
        projected = uvVertices;
        return true;
    }

    /// <summary>
    /// Display-oriented loop flattening: walks the loop's coedges in order and emits their actual
    /// vertices, sampling circular arcs. Unlike <see cref="TryBuildPolygonLoopDetailed"/>, which only
    /// accepts all-line loops and reconstructs corners by intersecting consecutive lines, this accepts
    /// loops containing Circle3 edges (fillets, rounded corners) and collinear consecutive lines.
    /// </summary>
    private static bool TryBuildLoopPolylineDetailed(
        BrepBody body,
        Loop loop,
        PlaneSurface plane,
        out IReadOnlyList<Point3D> vertices3d,
        out IReadOnlyList<(double U, double V)> projected)
    {
        vertices3d = [];
        projected = [];
        if (loop.CoedgeIds.Count == 0)
        {
            return false;
        }

        var points = new List<Point3D>(loop.CoedgeIds.Count * 4);
        foreach (var coedgeId in loop.CoedgeIds)
        {
            var coedge = body.Topology.GetCoedge(coedgeId);
            var edge = body.Topology.GetEdge(coedge.EdgeId);
            if (!body.TryGetEdgeCurveGeometry(coedge.EdgeId, out var curve) || curve is null)
            {
                return false;
            }

            Point3D edgeStart;
            Point3D edgeEnd;
            if (body.TryGetVertexPoint(edge.StartVertexId, out edgeStart)
                && body.TryGetVertexPoint(edge.EndVertexId, out edgeEnd))
            {
            }
            else if (edge.StartVertexId == edge.EndVertexId && curve.Circle3 is { } selfLoopCircle)
            {
                // Revolved primitives carry no vertex points; a closed circle self-loop starts at its X axis.
                edgeStart = selfLoopCircle.Center + (selfLoopCircle.XAxis.ToVector() * selfLoopCircle.Radius);
                edgeEnd = edgeStart;
            }
            else
            {
                return false;
            }

            var traversalStart = coedge.IsReversed ? edgeEnd : edgeStart;
            var traversalEnd = coedge.IsReversed ? edgeStart : edgeEnd;

            switch (curve.Kind)
            {
                case CurveGeometryKind.Line3:
                    AppendDistinct(points, traversalStart);
                    break;

                case CurveGeometryKind.Circle3 when curve.Circle3 is { } circle:
                    if (!body.Bindings.TryGetEdgeBinding(coedge.EdgeId, out var binding)
                        || !CurveSampler.TrySampleTrimmedCircleArc(
                            circle,
                            traversalStart,
                            traversalEnd,
                            coedge.IsReversed ? !binding.OrientedEdgeSense : binding.OrientedEdgeSense,
                            out var arcPoints,
                            out _,
                            out _))
                    {
                        return false;
                    }

                    // The last sample coincides with the next coedge's start, so it is emitted there.
                    for (var i = 0; i < arcPoints.Count - 1; i++)
                    {
                        AppendDistinct(points, arcPoints[i]);
                    }

                    break;

                default:
                    return false;
            }
        }

        if (points.Count > 1 && PointsAlmostEqual(points[0], points[^1]))
        {
            points.RemoveAt(points.Count - 1);
        }

        if (points.Count < 3)
        {
            return false;
        }

        var uv = new List<(double U, double V)>(points.Count);
        foreach (var point in points)
        {
            uv.Add(ProjectToPlane(point, plane));
        }

        if (double.Abs(SignedArea(uv)) <= 1e-9d)
        {
            return false;
        }

        vertices3d = points;
        projected = uv;
        return true;
    }

    private static void AppendDistinct(List<Point3D> points, Point3D point)
    {
        if (points.Count == 0 || !PointsAlmostEqual(points[^1], point))
        {
            points.Add(point);
        }
    }

    private static bool TryIntersectLines2D(
        (double U, double V) originA,
        (double U, double V) directionA,
        (double U, double V) originB,
        (double U, double V) directionB,
        out (double U, double V) intersection)
    {
        intersection = default;
        var determinant = (directionA.U * directionB.V) - (directionA.V * directionB.U);
        if (double.Abs(determinant) <= 1e-12d)
        {
            return false;
        }

        var delta = (U: originB.U - originA.U, V: originB.V - originA.V);
        var t = ((delta.U * directionB.V) - (delta.V * directionB.U)) / determinant;
        intersection = (originA.U + (directionA.U * t), originA.V + (directionA.V * t));
        return true;
    }

    private static bool PointsAlmostEqual(Point3D a, Point3D b)
        => (a - b).Length <= 1e-7d;

    private static List<(Point3D Center, double Radius)> TryResolveCircularBoundaries(BrepBody body, Face face)
    {
        var boundaries = new List<(Point3D Center, double Radius)>();
        foreach (var loopId in face.LoopIds)
        {
            var loop = body.Topology.GetLoop(loopId);
            var hasOnlyCircles = loop.CoedgeIds.Count > 0;
            Point3D? center = null;
            double? radius = null;
            foreach (var coedgeId in loop.CoedgeIds)
            {
                var edgeId = body.Topology.GetCoedge(coedgeId).EdgeId;
                if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding)
                    || !body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
                    || curve is not { Kind: CurveGeometryKind.Circle3, Circle3: { } circle })
                {
                    hasOnlyCircles = false;
                    break;
                }

                center ??= circle.Center;
                radius ??= circle.Radius;
            }

            if (hasOnlyCircles && center.HasValue && radius.HasValue)
            {
                boundaries.Add((center.Value, radius.Value));
            }
        }

        return boundaries;
    }

    private static (double U, double V) ProjectToPlane(Point3D point, PlaneSurface plane)
    {
        var local = point - plane.Origin;
        return (local.Dot(plane.UAxis.ToVector()), local.Dot(plane.VAxis.ToVector()));
    }

    private static bool ContainsPoint(IReadOnlyList<(double U, double V)> polygon, (double U, double V) point, ToleranceContext tolerance)
    {
        var inside = false;
        for (var i = 0; i < polygon.Count; i++)
        {
            var a = polygon[i];
            var b = polygon[(i + 1) % polygon.Count];
            if (DistancePointToSegmentSquared(point, a, b) <= tolerance.Linear * tolerance.Linear)
            {
                return true;
            }

            var intersects = ((a.V > point.V) != (b.V > point.V))
                && (point.U < ((b.U - a.U) * (point.V - a.V) / (b.V - a.V + 1e-20d)) + a.U);
            if (intersects)
            {
                inside = !inside;
            }
        }

        return inside;
    }

    private static double DistancePointToSegmentSquared((double U, double V) point, (double U, double V) a, (double U, double V) b)
    {
        var abU = b.U - a.U;
        var abV = b.V - a.V;
        var lengthSquared = (abU * abU) + (abV * abV);
        if (lengthSquared <= 1e-20d)
        {
            var du = point.U - a.U;
            var dv = point.V - a.V;
            return (du * du) + (dv * dv);
        }

        var apU = point.U - a.U;
        var apV = point.V - a.V;
        var t = double.Clamp(((apU * abU) + (apV * abV)) / lengthSquared, 0d, 1d);
        var closestU = a.U + (abU * t);
        var closestV = a.V + (abV * t);
        var dU = point.U - closestU;
        var dV = point.V - closestV;
        return (dU * dU) + (dV * dV);
    }

    private static double DistancePointToPointSquared((double U, double V) a, (double U, double V) b)
    {
        var du = a.U - b.U;
        var dv = a.V - b.V;
        return (du * du) + (dv * dv);
    }

    private static double SignedArea(IReadOnlyList<(double U, double V)> polygon)
    {
        var area = 0d;
        for (var i = 0; i < polygon.Count; i++)
        {
            var current = polygon[i];
            var next = polygon[(i + 1) % polygon.Count];
            area += (current.U * next.V) - (next.U * current.V);
        }

        return area * 0.5d;
    }

    private static bool IsAxisAlignedPlane(Vector3D normal)
        => double.Abs(normal.X) > 0.9d || double.Abs(normal.Y) > 0.9d || double.Abs(normal.Z) > 0.9d;

    private static bool IsPointInsideAxisBounds(Point3D point, Vector3D normal, Point3D min, Point3D max, ToleranceContext tolerance)
    {
        if (double.Abs(normal.X) > 0.9d)
        {
            return point.Y >= min.Y - tolerance.Linear && point.Y <= max.Y + tolerance.Linear
                && point.Z >= min.Z - tolerance.Linear && point.Z <= max.Z + tolerance.Linear;
        }

        if (double.Abs(normal.Y) > 0.9d)
        {
            return point.X >= min.X - tolerance.Linear && point.X <= max.X + tolerance.Linear
                && point.Z >= min.Z - tolerance.Linear && point.Z <= max.Z + tolerance.Linear;
        }

        return point.X >= min.X - tolerance.Linear && point.X <= max.X + tolerance.Linear
            && point.Y >= min.Y - tolerance.Linear && point.Y <= max.Y + tolerance.Linear;
    }

    private static bool IsPointInsideCircularBoundary(Point3D point, PlaneSurface plane, Point3D center, double radius, ToleranceContext tolerance)
    {
        var radial = point - center;
        var height = radial.Dot(plane.Normal.ToVector());
        var inPlaneRadial = radial - (plane.Normal.ToVector() * height);
        return inPlaneRadial.Dot(inPlaneRadial) <= (radius + tolerance.Linear) * (radius + tolerance.Linear);
    }

    private static bool TryResolveAxisAlignedBoxBounds(BrepBody body, out Point3D min, out Point3D max)
    {
        min = default;
        max = default;
        var hasXMin = false;
        var hasXMax = false;
        var hasYMin = false;
        var hasYMax = false;
        var hasZMin = false;
        var hasZMax = false;

        foreach (var faceBinding in body.Bindings.FaceBindings)
        {
            var surface = body.Geometry.GetSurface(faceBinding.SurfaceGeometryId);
            if (surface.Kind != SurfaceGeometryKind.Plane || surface.Plane is not PlaneSurface plane)
            {
                continue;
            }

            var n = plane.Normal;
            if (ToleranceMath.AlmostEqual(n.X, -1d, ToleranceContext.Default))
            {
                min = min with { X = plane.Origin.X };
                hasXMin = true;
            }
            else if (ToleranceMath.AlmostEqual(n.X, 1d, ToleranceContext.Default))
            {
                max = max with { X = plane.Origin.X };
                hasXMax = true;
            }
            else if (ToleranceMath.AlmostEqual(n.Y, -1d, ToleranceContext.Default))
            {
                min = min with { Y = plane.Origin.Y };
                hasYMin = true;
            }
            else if (ToleranceMath.AlmostEqual(n.Y, 1d, ToleranceContext.Default))
            {
                max = max with { Y = plane.Origin.Y };
                hasYMax = true;
            }
            else if (ToleranceMath.AlmostEqual(n.Z, -1d, ToleranceContext.Default))
            {
                min = min with { Z = plane.Origin.Z };
                hasZMin = true;
            }
            else if (ToleranceMath.AlmostEqual(n.Z, 1d, ToleranceContext.Default))
            {
                max = max with { Z = plane.Origin.Z };
                hasZMax = true;
            }
        }

        return hasXMin && hasXMax && hasYMin && hasYMax && hasZMin && hasZMax;
    }
}