Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add headless analytic ray-query kernel for native surfaces
Yuechen Lid3e8cfb4cb28
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.csnew file mode 100644index 00000000..3c06fbf2--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.cs@@ -0,0 +1,155 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Brep.Features;+using Aetheris.Kernel.Core.Brep.Queries;+using Aetheris.Kernel.Core.Math;++namespace Aetheris.Kernel.Core.Tests.Brep.Queries;++public sealed class AnalyticDisplayQueryTests+{+ [Fact]+ public void PlanarFace_BoxFace_HitMissAndNormal()+ {+ var box = BrepPrimitives.CreateBox(2d, 2d, 2d).Value;+ var faceId = box.Topology.Faces.OrderBy(f => f.Id.Value).First().Id;++ var hitRay = new Ray3D(new Point3D(0d, 0d, -3d), Direction3D.Create(new Vector3D(0d, 0d, 1d)));+ var missRay = new Ray3D(new Point3D(3d, 3d, -3d), Direction3D.Create(new Vector3D(0d, 0d, 1d)));++ var hit = AnalyticDisplayQuery.TryIntersectFace(box, faceId, hitRay, out var faceHit);+ var miss = AnalyticDisplayQuery.TryIntersectFace(box, faceId, missRay, out _);++ Assert.True(hit);+ Assert.False(miss);+ Assert.Equal(2d, faceHit.Distance, 12);+ Assert.Equal(new Point3D(0d, 0d, -1d), faceHit.Position);+ Assert.Equal(-1d, faceHit.Normal.Z, 12);+ }++ [Fact]+ public void Sphere_HitMissAndNormal()+ {+ var sphere = BrepPrimitives.CreateSphere(2d).Value;+ var faceId = sphere.Topology.Faces.Single().Id;++ var hitRay = new Ray3D(new Point3D(-5d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));+ var missRay = new Ray3D(new Point3D(-5d, 0d, 0d), Direction3D.Create(new Vector3D(0d, 1d, 0d)));++ Assert.True(AnalyticDisplayQuery.TryIntersectFace(sphere, faceId, hitRay, out var hit));+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(sphere, faceId, missRay, out _));++ Assert.Equal(3d, hit.Distance, 12);+ Assert.Equal(new Point3D(-2d, 0d, 0d), hit.Position);+ Assert.Equal(-1d, hit.Normal.X, 12);+ }++ [Fact]+ public void Cylinder_Side_HitMissAndNormal()+ {+ var cylinder = BrepPrimitives.CreateCylinder(2d, 6d).Value;+ var sideFaceId = FindFaceByKind(cylinder, Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Cylinder);++ var hitRay = new Ray3D(new Point3D(3d, 0d, 0d), Direction3D.Create(new Vector3D(-1d, 0d, 0d)));+ var missRay = new Ray3D(new Point3D(3d, 0d, 4d), Direction3D.Create(new Vector3D(-1d, 0d, 0d)));++ Assert.True(AnalyticDisplayQuery.TryIntersectFace(cylinder, sideFaceId, hitRay, out var hit));+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(cylinder, sideFaceId, missRay, out _));++ Assert.Equal(1d, hit.Distance, 12);+ Assert.Equal(new Point3D(2d, 0d, 0d), hit.Position);+ Assert.Equal(1d, hit.Normal.X, 12);+ }++ [Fact]+ public void Cone_Side_HitMissAndNormal()+ {+ var cone = BrepRevolve.Create(+ [new ProfilePoint2D(1d, 0d), new ProfilePoint2D(3d, 4d)],+ new ExtrudeFrame3D(Point3D.Origin, Direction3D.Create(new Vector3D(0d, 0d, 1d)), Direction3D.Create(new Vector3D(1d, 0d, 0d))),+ new RevolveAxis3D(Point3D.Origin, new Vector3D(0d, 0d, 1d))).Value;++ var sideFaceId = FindFaceByKind(cone, Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Cone);++ var hitRay = new Ray3D(new Point3D(5d, 0d, 2d), Direction3D.Create(new Vector3D(-1d, 0d, 0d)));+ var missRay = new Ray3D(new Point3D(5d, 0d, -1d), Direction3D.Create(new Vector3D(-1d, 0d, 0d)));++ Assert.True(AnalyticDisplayQuery.TryIntersectFace(cone, sideFaceId, hitRay, out var hit));+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(cone, sideFaceId, missRay, out _));++ Assert.True(hit.Normal.X > 0d);+ Assert.True(hit.Normal.Z < 0d);+ }++ [Fact]+ public void Torus_HitAndMiss()+ {+ var torus = BrepPrimitives.CreateTorus(4d, 1d).Value;+ var torusFace = torus.Topology.Faces.Single().Id;++ var hitRay = new Ray3D(new Point3D(-8d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));+ var missRay = new Ray3D(new Point3D(0d, 0d, 0d), Direction3D.Create(new Vector3D(0d, 1d, 0d)));++ Assert.True(AnalyticDisplayQuery.TryIntersectFace(torus, torusFace, hitRay, out var hit));+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(torus, torusFace, missRay, out _));++ Assert.True(hit.Distance > 0d);+ Assert.Equal(Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Torus, hit.SurfaceKind);+ }++ [Fact]+ public void BodyLevelNearestHit_BoxAndDeterminism()+ {+ var box = BrepPrimitives.CreateBox(2d, 2d, 2d).Value;+ var ray = new Ray3D(new Point3D(-5d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));++ Assert.True(AnalyticDisplayQuery.TryIntersectBody(box, ray, out var first));+ Assert.True(AnalyticDisplayQuery.TryIntersectBody(box, ray, out var second));++ Assert.Equal(first.Distance, second.Distance, 12);+ Assert.Equal(first.Position, second.Position);+ Assert.Equal(first.FaceId, second.FaceId);+ Assert.Equal(4d, first.Distance, 12);+ Assert.Equal(new Point3D(-1d, 0d, 0d), first.Position);+ }++ [Fact]+ public void BodyLevelNearestHit_Sphere_PreservesIdentity()+ {+ var sphere = BrepPrimitives.CreateSphere(2d).Value;+ var ray = new Ray3D(new Point3D(-5d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));++ Assert.True(AnalyticDisplayQuery.TryIntersectBody(sphere, ray, out var hit));++ Assert.Equal(3d, hit.Distance, 12);+ Assert.True(hit.BodyId.IsValid);+ Assert.True(hit.FaceId.IsValid);+ }++ [Fact]+ public void AnalyticPath_SupportsTorusWhereSpatialPrimitiveRaycastDoesNot()+ {+ var torus = BrepPrimitives.CreateTorus(4d, 1d).Value;+ var ray = new Ray3D(new Point3D(-8d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));++ var spatial = BrepSpatialQueries.Raycast(torus, ray);+ var analytic = AnalyticDisplayQuery.TryIntersectBody(torus, ray, out var hit);++ Assert.False(spatial.IsSuccess);+ Assert.True(analytic);+ Assert.Equal(Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Torus, hit.SurfaceKind);+ }++ private static Aetheris.Kernel.Core.Topology.FaceId FindFaceByKind(BrepBody body, Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind kind)+ {+ foreach (var face in body.Topology.Faces.OrderBy(f => f.Id.Value))+ {+ var binding = body.Bindings.GetFaceBinding(face.Id);+ if (body.Geometry.GetSurface(binding.SurfaceGeometryId).Kind == kind)+ {+ return face.Id;+ }+ }++ throw new InvalidOperationException($"Face kind {kind} not found.");+ }+}diff --git a/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.cs b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.csnew file mode 100644index 00000000..d4e7bc16--- /dev/null+++ b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.cs@@ -0,0 +1,603 @@+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Geometry.Surfaces;+using Aetheris.Kernel.Core.Math;+using Aetheris.Kernel.Core.Numerics;+using Aetheris.Kernel.Core.Topology;++namespace Aetheris.Kernel.Core.Brep.Queries;++public readonly record struct AnalyticRayHit(+ bool Hit,+ double Distance,+ Point3D Position,+ Direction3D Normal,+ BodyId BodyId,+ FaceId FaceId,+ SurfaceGeometryKind SurfaceKind,+ double? U = null,+ double? V = null);++public static class AnalyticDisplayQuery+{+ public static bool TryIntersectBody(BrepBody body, Ray3D ray, out AnalyticRayHit hit, double maxDistance = 1e6d, ToleranceContext? tolerance = null)+ {+ var context = tolerance ?? ToleranceContext.Default;+ hit = default;+ var bodyEntity = body.Topology.Bodies.OrderBy(b => b.Id.Value).FirstOrDefault();+ if (bodyEntity is not Body resolvedBody)+ {+ return false;+ }++ var faceIds = resolvedBody.ShellIds+ .SelectMany(shellId => body.Topology.GetShell(shellId).FaceIds)+ .Distinct()+ .OrderBy(id => id.Value)+ .ToArray();++ var found = false;+ AnalyticRayHit best = default;++ foreach (var faceId in faceIds)+ {+ if (TryIntersectFace(body, faceId, ray, out var candidate, maxDistance, context)+ && (!found || candidate.Distance < best.Distance - context.Linear || (ToleranceMath.AlmostEqual(candidate.Distance, best.Distance, context) && candidate.FaceId.Value < best.FaceId.Value)))+ {+ found = true;+ best = candidate;+ }+ }++ if (!found)+ {+ return false;+ }++ hit = best;+ return true;+ }++ public static bool TryIntersectFace(BrepBody body, FaceId faceId, Ray3D ray, out AnalyticRayHit hit, double maxDistance = 1e6d, ToleranceContext? tolerance = null)+ {+ var context = tolerance ?? ToleranceContext.Default;+ hit = default;++ if (!body.Bindings.TryGetFaceBinding(faceId, out var binding)+ || !body.Geometry.TryGetSurface(binding.SurfaceGeometryId, out var surface)+ || surface is null)+ {+ return false;+ }++ var bodyId = body.Topology.Bodies.OrderBy(b => b.Id.Value).FirstOrDefault()?.Id ?? BodyId.Invalid;++ return surface.Kind switch+ {+ SurfaceGeometryKind.Plane when surface.Plane is PlaneSurface plane => TryIntersectPlane(body, bodyId, faceId, plane, ray, maxDistance, context, out hit),+ SurfaceGeometryKind.Sphere when surface.Sphere is SphereSurface sphere => TryIntersectSphere(bodyId, faceId, sphere, ray, maxDistance, context, out hit),+ SurfaceGeometryKind.Cylinder when surface.Cylinder is CylinderSurface cylinder => TryIntersectCylinder(body, bodyId, faceId, cylinder, ray, maxDistance, context, out hit),+ SurfaceGeometryKind.Cone when surface.Cone is ConeSurface cone => TryIntersectCone(body, bodyId, faceId, cone, ray, maxDistance, context, out hit),+ SurfaceGeometryKind.Torus when surface.Torus is TorusSurface torus => TryIntersectTorus(bodyId, faceId, torus, ray, maxDistance, context, out hit),+ _ => false,+ };+ }++ private static bool TryIntersectPlane(BrepBody body, BodyId bodyId, FaceId faceId, PlaneSurface plane, Ray3D ray, double maxDistance, ToleranceContext tolerance, out AnalyticRayHit hit)+ {+ hit = default;+ var denom = plane.Normal.ToVector().Dot(ray.Direction.ToVector());+ if (ToleranceMath.AlmostZero(denom, tolerance))+ {+ return false;+ }++ var t = (plane.Origin - ray.Origin).Dot(plane.Normal.ToVector()) / denom;+ if (t < -tolerance.Linear)+ {+ return false;+ }++ t = ToleranceMath.ClampToZero(t, tolerance.Linear);+ if (t > maxDistance + tolerance.Linear)+ {+ return false;+ }++ var point = ray.PointAt(t);+ if (!IsPointOnPlaneFaceDomain(body, faceId, plane, point, tolerance))+ {+ return false;+ }++ var local = point - plane.Origin;+ var u = local.Dot(plane.UAxis.ToVector());+ var v = local.Dot(plane.VAxis.ToVector());+ hit = new AnalyticRayHit(true, t, point, plane.Normal, bodyId, faceId, SurfaceGeometryKind.Plane, u, v);+ return true;+ }++ private static bool TryIntersectSphere(BodyId bodyId, FaceId faceId, SphereSurface sphere, Ray3D ray, double maxDistance, ToleranceContext tolerance, out AnalyticRayHit hit)+ {+ hit = default;+ var oc = ray.Origin - sphere.Center;+ var d = ray.Direction.ToVector();+ var b = 2d * oc.Dot(d);+ var c = oc.Dot(oc) - (sphere.Radius * sphere.Radius);+ var discriminant = (b * b) - (4d * c);+ if (discriminant < -tolerance.Linear)+ {+ return false;+ }++ var root = double.Sqrt(double.Max(discriminant, 0d));+ var t1 = (-b - root) / 2d;+ var t2 = (-b + root) / 2d;+ var t = PickNearestPositive(t1, t2, tolerance, maxDistance);+ if (double.IsNaN(t))+ {+ return false;+ }++ var point = ray.PointAt(t);+ var normal = Direction3D.Create(point - sphere.Center);+ var local = point - sphere.Center;+ var u = double.Atan2(local.Dot(sphere.YAxis.ToVector()), local.Dot(sphere.XAxis.ToVector()));+ var v = double.Asin(local.Dot(sphere.Axis.ToVector()) / sphere.Radius);+ hit = new AnalyticRayHit(true, t, point, normal, bodyId, faceId, SurfaceGeometryKind.Sphere, u, v);+ return true;+ }++ private static bool TryIntersectCylinder(BrepBody body, BodyId bodyId, FaceId faceId, CylinderSurface cylinder, Ray3D ray, double maxDistance, ToleranceContext tolerance, out AnalyticRayHit hit)+ {+ hit = default;+ var axis = cylinder.Axis.ToVector();+ var delta = ray.Origin - cylinder.Origin;+ var direction = ray.Direction.ToVector();++ var oAxial = delta.Dot(axis);+ var dAxial = direction.Dot(axis);+ var oRadial = delta - (axis * oAxial);+ var dRadial = direction - (axis * dAxial);++ var a = dRadial.Dot(dRadial);+ var b = 2d * oRadial.Dot(dRadial);+ var c = oRadial.Dot(oRadial) - (cylinder.Radius * cylinder.Radius);++ if (ToleranceMath.AlmostZero(a, tolerance))+ {+ return false;+ }++ var disc = (b * b) - (4d * a * c);+ if (disc < -tolerance.Linear)+ {+ return false;+ }++ var root = double.Sqrt(double.Max(disc, 0d));+ var candidates = new[] { (-b - root) / (2d * a), (-b + root) / (2d * a) }+ .Where(t => t >= -tolerance.Linear && t <= maxDistance + tolerance.Linear)+ .Select(t => ToleranceMath.ClampToZero(t, tolerance.Linear))+ .OrderBy(t => t)+ .ToArray();++ if (candidates.Length == 0)+ {+ return false;+ }++ ResolveAxialBounds(body, faceId, cylinder.Axis, cylinder.Origin, out var minV, out var maxV);++ foreach (var t in candidates)+ {+ var point = ray.PointAt(t);+ var axial = (point - cylinder.Origin).Dot(axis);+ if (minV.HasValue && axial < minV.Value - tolerance.Linear)+ {+ continue;+ }++ if (maxV.HasValue && axial > maxV.Value + tolerance.Linear)+ {+ continue;+ }++ var radial = (point - cylinder.Origin) - (axis * axial);+ if (!Direction3D.TryCreate(radial, out var normal))+ {+ continue;+ }++ var u = double.Atan2(radial.Dot(cylinder.YAxis.ToVector()), radial.Dot(cylinder.XAxis.ToVector()));+ hit = new AnalyticRayHit(true, t, point, normal, bodyId, faceId, SurfaceGeometryKind.Cylinder, u, axial);+ return true;+ }++ return false;+ }++ private static bool TryIntersectCone(BrepBody body, BodyId bodyId, FaceId faceId, ConeSurface cone, Ray3D ray, double maxDistance, ToleranceContext tolerance, out AnalyticRayHit hit)+ {+ hit = default;+ var axis = cone.Axis.ToVector();+ var tan = double.Tan(cone.SemiAngleRadians);+ var tanSquared = tan * tan;++ var delta = ray.Origin - cone.Apex;+ var direction = ray.Direction.ToVector();++ var dAxial = direction.Dot(axis);+ var oAxial = delta.Dot(axis);+ var dPerp = direction - (axis * dAxial);+ var oPerp = delta - (axis * oAxial);++ var a = dPerp.Dot(dPerp) - (tanSquared * dAxial * dAxial);+ var b = 2d * (oPerp.Dot(dPerp) - (tanSquared * oAxial * dAxial));+ var c = oPerp.Dot(oPerp) - (tanSquared * oAxial * oAxial);++ if (ToleranceMath.AlmostZero(a, tolerance))+ {+ return false;+ }++ var disc = (b * b) - (4d * a * c);+ if (disc < -tolerance.Linear)+ {+ return false;+ }++ var root = double.Sqrt(double.Max(disc, 0d));+ var candidates = new[] { (-b - root) / (2d * a), (-b + root) / (2d * a) }+ .Where(t => t >= -tolerance.Linear && t <= maxDistance + tolerance.Linear)+ .Select(t => ToleranceMath.ClampToZero(t, tolerance.Linear))+ .OrderBy(t => t)+ .ToArray();++ if (candidates.Length == 0)+ {+ return false;+ }++ ResolveAxialBounds(body, faceId, cone.Axis, cone.Apex, out var minV, out var maxV);++ foreach (var t in candidates)+ {+ var point = ray.PointAt(t);+ var v = (point - cone.Apex).Dot(axis);+ if (v < -tolerance.Linear)+ {+ continue;+ }++ if (minV.HasValue && v < minV.Value - tolerance.Linear)+ {+ continue;+ }++ if (maxV.HasValue && v > maxV.Value + tolerance.Linear)+ {+ continue;+ }++ var radial = (point - cone.Apex) - (axis * v);+ var radialNormal = Direction3D.TryCreate(radial, out var radialDirection)+ ? radialDirection.ToVector()+ : Vector3D.Zero;+ var normalVector = radialNormal - (axis * tan);+ if (!Direction3D.TryCreate(normalVector, out var normal))+ {+ continue;+ }++ var u = double.Atan2(radial.Dot(cone.ReferenceAxis.ToVector().Cross(axis)), radial.Dot(cone.ReferenceAxis.ToVector()));+ hit = new AnalyticRayHit(true, t, point, normal, bodyId, faceId, SurfaceGeometryKind.Cone, u, v);+ return true;+ }++ return false;+ }++ private static bool TryIntersectTorus(BodyId bodyId, FaceId faceId, TorusSurface torus, Ray3D ray, double maxDistance, ToleranceContext tolerance, out AnalyticRayHit hit)+ {+ hit = default;++ if (!TryIntersectBoundingSphere(ray, torus.Center, torus.MajorRadius + torus.MinorRadius, out var near, out var far, tolerance))+ {+ return false;+ }++ near = double.Max(0d, near);+ far = double.Min(maxDistance, far);+ if (far < near)+ {+ return false;+ }++ const int samples = 512;+ var dt = (far - near) / samples;+ if (dt <= tolerance.Linear)+ {+ dt = tolerance.Linear;+ }++ double? bestT = null;+ var prevT = near;+ var prevF = TorusImplicit(ray.PointAt(prevT), torus);++ for (var i = 1; i <= samples; i++)+ {+ var currentT = i == samples ? far : near + (i * dt);+ var currentF = TorusImplicit(ray.PointAt(currentT), torus);++ if (double.Abs(currentF) <= 1e-10d)+ {+ bestT = currentT;+ break;+ }++ if ((prevF < 0d && currentF > 0d) || (prevF > 0d && currentF < 0d))+ {+ var root = BisectRoot(prevT, currentT, torus, ray);+ bestT = root;+ break;+ }++ prevT = currentT;+ prevF = currentF;+ }++ if (!bestT.HasValue)+ {+ return false;+ }++ var t = ToleranceMath.ClampToZero(bestT.Value, tolerance.Linear);+ var point = ray.PointAt(t);+ var normal = TorusNormal(point, torus);++ var local = point - torus.Center;+ var x = local.Dot(torus.XAxis.ToVector());+ var y = local.Dot(torus.YAxis.ToVector());+ var z = local.Dot(torus.Axis.ToVector());+ var u = double.Atan2(y, x);+ var radial = double.Sqrt((x * x) + (y * y));+ var v = double.Atan2(z, radial - torus.MajorRadius);++ hit = new AnalyticRayHit(true, t, point, normal, bodyId, faceId, SurfaceGeometryKind.Torus, u, v);+ return true;+ }++ private static bool IsPointOnPlaneFaceDomain(BrepBody body, FaceId faceId, PlaneSurface plane, Point3D point, ToleranceContext tolerance)+ {+ if (!body.Topology.TryGetFace(faceId, out var face) || face is null)+ {+ return true;+ }++ var circularBoundary = false;+ var circleCenter = Point3D.Origin;+ var circleRadius = 0d;++ foreach (var loopId in face.LoopIds)+ {+ var loop = body.Topology.GetLoop(loopId);+ 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))+ {+ continue;+ }++ if (curve.Kind == CurveGeometryKind.Circle3 && curve.Circle3 is { } circle)+ {+ circularBoundary = true;+ circleCenter = circle.Center;+ circleRadius = circle.Radius;+ }+ }+ }++ if (circularBoundary)+ {+ var radial = point - circleCenter;+ var height = radial.Dot(plane.Normal.ToVector());+ var inPlaneRadial = radial - (plane.Normal.ToVector() * height);+ return inPlaneRadial.Dot(inPlaneRadial) <= (circleRadius + tolerance.Linear) * (circleRadius + tolerance.Linear);+ }++ if (TryResolveAxisAlignedBoxBounds(body, out var min, out var max))+ {+ var normal = plane.Normal.ToVector();+ 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;+ }++ if (double.Abs(normal.Z) > 0.9d)+ {+ 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;+ }+ }++ return true;+ }++ 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;+ }++ private static void ResolveAxialBounds(BrepBody body, FaceId sideFaceId, Direction3D axis, Point3D axisOrigin, out double? minV, out double? maxV)+ {+ minV = null;+ maxV = null;+ var axisVector = axis.ToVector();++ foreach (var faceBinding in body.Bindings.FaceBindings)+ {+ if (faceBinding.FaceId == sideFaceId)+ {+ continue;+ }++ var surface = body.Geometry.GetSurface(faceBinding.SurfaceGeometryId);+ if (surface.Kind != SurfaceGeometryKind.Plane || surface.Plane is not PlaneSurface plane)+ {+ continue;+ }++ var dot = plane.Normal.ToVector().Dot(axisVector);+ if (double.Abs(double.Abs(dot) - 1d) > 1e-6d)+ {+ continue;+ }++ var v = (plane.Origin - axisOrigin).Dot(axisVector);+ minV = !minV.HasValue ? v : double.Min(minV.Value, v);+ maxV = !maxV.HasValue ? v : double.Max(maxV.Value, v);+ }+ }++ private static bool TryIntersectBoundingSphere(Ray3D ray, Point3D center, double radius, out double near, out double far, ToleranceContext tolerance)+ {+ near = 0d;+ far = 0d;++ var oc = ray.Origin - center;+ var d = ray.Direction.ToVector();+ var b = 2d * oc.Dot(d);+ var c = oc.Dot(oc) - (radius * radius);+ var discriminant = (b * b) - (4d * c);+ if (discriminant < -tolerance.Linear)+ {+ return false;+ }++ var root = double.Sqrt(double.Max(0d, discriminant));+ near = (-b - root) / 2d;+ far = (-b + root) / 2d;+ return far >= -tolerance.Linear;+ }++ private static double TorusImplicit(Point3D p, TorusSurface torus)+ {+ var delta = p - torus.Center;+ var x = delta.Dot(torus.XAxis.ToVector());+ var y = delta.Dot(torus.YAxis.ToVector());+ var z = delta.Dot(torus.Axis.ToVector());+ var sum = (x * x) + (y * y) + (z * z) + (torus.MajorRadius * torus.MajorRadius) - (torus.MinorRadius * torus.MinorRadius);+ return (sum * sum) - (4d * torus.MajorRadius * torus.MajorRadius * ((x * x) + (y * y)));+ }++ private static Direction3D TorusNormal(Point3D point, TorusSurface torus)+ {+ var delta = point - torus.Center;+ var x = delta.Dot(torus.XAxis.ToVector());+ var y = delta.Dot(torus.YAxis.ToVector());+ var z = delta.Dot(torus.Axis.ToVector());+ var common = (x * x) + (y * y) + (z * z) + (torus.MajorRadius * torus.MajorRadius) - (torus.MinorRadius * torus.MinorRadius);+ var nx = 4d * x * (common - (2d * torus.MajorRadius * torus.MajorRadius));+ var ny = 4d * y * (common - (2d * torus.MajorRadius * torus.MajorRadius));+ var nz = 4d * z * common;++ var vector = (torus.XAxis.ToVector() * nx) + (torus.YAxis.ToVector() * ny) + (torus.Axis.ToVector() * nz);+ return Direction3D.Create(vector);+ }++ private static double BisectRoot(double a, double b, TorusSurface torus, Ray3D ray)+ {+ var fa = TorusImplicit(ray.PointAt(a), torus);+ for (var i = 0; i < 60; i++)+ {+ var mid = 0.5d * (a + b);+ var fm = TorusImplicit(ray.PointAt(mid), torus);+ if (double.Abs(fm) < 1e-12d)+ {+ return mid;+ }++ if ((fa < 0d && fm > 0d) || (fa > 0d && fm < 0d))+ {+ b = mid;+ }+ else+ {+ a = mid;+ fa = fm;+ }+ }++ return 0.5d * (a + b);+ }++ private static double PickNearestPositive(double t1, double t2, ToleranceContext tolerance, double maxDistance)+ {+ var candidates = new[] { t1, t2 }+ .Where(t => t >= -tolerance.Linear && t <= maxDistance + tolerance.Linear)+ .Select(t => ToleranceMath.ClampToZero(t, tolerance.Linear))+ .OrderBy(t => t)+ .ToArray();++ return candidates.Length == 0 ? double.NaN : candidates[0];+ }+}