Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add kernel torus brep primitive

Yuechen Lib0eed5bcaf5f
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Primitives/BrepPrimitivesTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Primitives/BrepPrimitivesTests.cs
index 31651421..67a7d628 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Primitives/BrepPrimitivesTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Primitives/BrepPrimitivesTests.cs
@@ -108,6 +108,58 @@ public sealed class BrepPrimitivesTests
Assert.Contains(result.Diagnostics, d => d.Code == KernelDiagnosticCode.InvalidArgument);
}
+
+ [Fact]
+ public void CreateTorus_ProducesExpectedTopologyAndBindings_WithCircularSelfLoopSeams()
+ {
+ var result = BrepPrimitives.CreateTorus(5d, 2d);
+
+ Assert.True(result.IsSuccess);
+ var body = result.Value;
+
+ Assert.Single(body.Topology.Bodies);
+ Assert.Single(body.Topology.Shells);
+ Assert.Single(body.Topology.Faces);
+ Assert.Single(body.Topology.Loops);
+ Assert.Equal(4, body.Topology.Coedges.Count());
+ Assert.Equal(2, body.Topology.Edges.Count());
+ Assert.Single(body.Topology.Vertices);
+
+ var torusFace = Assert.Single(body.Topology.Faces);
+ Assert.Equal(SurfaceGeometryKind.Torus, body.GetFaceSurface(torusFace.Id).Kind);
+
+ var loopId = Assert.Single(body.GetLoopIds(torusFace.Id));
+ var coedges = body.GetCoedgeIds(loopId)
+ .Select(body.Topology.GetCoedge)
+ .ToArray();
+
+ Assert.Equal(4, coedges.Length);
+ Assert.All(coedges, coedge => Assert.Equal(CurveGeometryKind.Circle3, body.GetEdgeCurve(coedge.EdgeId).Kind));
+ Assert.Equal(2, coedges.Select(coedge => coedge.EdgeId).Distinct().Count());
+ Assert.All(body.Topology.Edges, edge => Assert.Equal(edge.StartVertexId, edge.EndVertexId));
+ Assert.Equal(4, coedges.Count(coedge => body.Topology.GetEdge(coedge.EdgeId).StartVertexId == body.Topology.GetEdge(coedge.EdgeId).EndVertexId));
+
+ var validation = BrepBindingValidator.Validate(body, requireAllEdgeAndFaceBindings: true);
+ Assert.True(validation.IsSuccess);
+ }
+
+ [Theory]
+ [InlineData(0d, 1d)]
+ [InlineData(1d, 0d)]
+ [InlineData(-1d, 1d)]
+ [InlineData(1d, -1d)]
+ [InlineData(1d, 1d)]
+ [InlineData(1d, 2d)]
+ [InlineData(double.NaN, 1d)]
+ [InlineData(2d, double.PositiveInfinity)]
+ public void CreateTorus_InvalidInputs_Fails(double majorRadius, double minorRadius)
+ {
+ var result = BrepPrimitives.CreateTorus(majorRadius, minorRadius);
+
+ Assert.False(result.IsSuccess);
+ Assert.Contains(result.Diagnostics, d => d.Code == KernelDiagnosticCode.InvalidArgument);
+ }
+
[Fact]
public void CreateSphere_ProducesSingleClosedFaceWithSphereBinding()
{
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
index a1b7165d..b6e2cb82 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
@@ -115,6 +115,34 @@ END-ISO-10303-21;";
Assert.Contains("TOROIDAL_SURFACE", export.Value, StringComparison.Ordinal);
}
+
+ [Fact]
+ public void ExportBody_ProgrammaticTorus_IsDeterministic_AndEmitsToroidalSurface()
+ {
+ var torus = BrepPrimitives.CreateTorus(5d, 2d);
+ Assert.True(torus.IsSuccess);
+
+ var first = Step242Exporter.ExportBody(torus.Value);
+ var second = Step242Exporter.ExportBody(torus.Value);
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.Equal(first.Value, second.Value);
+ Assert.Contains("TOROIDAL_SURFACE", first.Value, StringComparison.Ordinal);
+ Assert.Contains("CIRCLE", first.Value, StringComparison.Ordinal);
+
+ var import = Step242Importer.ImportBody(first.Value);
+ Assert.True(import.IsSuccess);
+
+ Assert.Equal(torus.Value.Topology.Faces.Count(), import.Value.Topology.Faces.Count());
+ Assert.Equal(torus.Value.Topology.Loops.Count(), import.Value.Topology.Loops.Count());
+ Assert.Equal(torus.Value.Topology.Edges.Count(), import.Value.Topology.Edges.Count());
+ Assert.Equal(torus.Value.Topology.Vertices.Count(), import.Value.Topology.Vertices.Count());
+
+ var torusFace = Assert.Single(import.Value.Topology.Faces);
+ Assert.Equal(Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Torus, import.Value.GetFaceSurface(torusFace.Id).Kind);
+ }
+
[Fact]
public void ExportBody_ImportedConicalFace_EmitsConicalSurfaceWithCanonicalRadianSemiAngle()
{
diff --git a/Aetheris.Kernel.Core/Brep/BrepPrimitives.cs b/Aetheris.Kernel.Core/Brep/BrepPrimitives.cs
index 8ded9fef..f35f7ea9 100644
--- a/Aetheris.Kernel.Core/Brep/BrepPrimitives.cs
+++ b/Aetheris.Kernel.Core/Brep/BrepPrimitives.cs
@@ -186,6 +186,76 @@ public static class BrepPrimitives
return ValidateAndReturn(new BrepBody(builder.Model, geometry, bindings));
}
+
+ public static KernelResult<BrepBody> CreateTorus(double majorRadius, double minorRadius)
+ {
+ var diagnostics = ValidatePositiveFinite((majorRadius, nameof(majorRadius)), (minorRadius, nameof(minorRadius)));
+ if (majorRadius <= minorRadius)
+ {
+ diagnostics.Add(new KernelDiagnostic(
+ KernelDiagnosticCode.InvalidArgument,
+ KernelDiagnosticSeverity.Error,
+ $"{nameof(majorRadius)} must be greater than {nameof(minorRadius)} for a non-self-intersecting torus."));
+ }
+
+ if (diagnostics.Count > 0)
+ {
+ return KernelResult<BrepBody>.Failure(diagnostics);
+ }
+
+ var builder = new TopologyBuilder();
+
+ // Narrow M10g1 torus convention: one periodic toroidal face with one loop,
+ // represented by two circular self-loop seam edges that are each used twice.
+ var seamVertex = builder.AddVertex();
+ var majorSeamEdge = builder.AddEdge(seamVertex, seamVertex);
+ var minorSeamEdge = builder.AddEdge(seamVertex, seamVertex);
+
+ var torusFace = AddFaceWithLoop(
+ builder,
+ [
+ EdgeUse.Forward(majorSeamEdge),
+ EdgeUse.Reversed(minorSeamEdge),
+ EdgeUse.Reversed(majorSeamEdge),
+ EdgeUse.Forward(minorSeamEdge),
+ ]);
+
+ var shell = builder.AddShell([torusFace]);
+ builder.AddBody([shell]);
+
+ var geometry = new BrepGeometryStore();
+ var yAxis = Direction3D.Create(new Vector3D(0d, 1d, 0d));
+ var zAxis = Direction3D.Create(new Vector3D(0d, 0d, 1d));
+ var negativeXAxis = Direction3D.Create(new Vector3D(-1d, 0d, 0d));
+ var positiveXAxis = Direction3D.Create(new Vector3D(1d, 0d, 0d));
+
+ var sharedVertexPoint = new Point3D(-(majorRadius - minorRadius), 0d, 0d);
+
+ geometry.AddCurve(
+ new CurveGeometryId(1),
+ CurveGeometry.FromCircle(new Circle3Curve(Point3D.Origin, yAxis, majorRadius - minorRadius, negativeXAxis)));
+ geometry.AddCurve(
+ new CurveGeometryId(2),
+ CurveGeometry.FromCircle(new Circle3Curve(new Point3D(-majorRadius, 0d, 0d), zAxis, minorRadius, positiveXAxis)));
+ geometry.AddSurface(
+ new SurfaceGeometryId(1),
+ SurfaceGeometry.FromTorus(new TorusSurface(Point3D.Origin, yAxis, majorRadius, minorRadius, negativeXAxis)));
+
+ var bindings = new BrepBindingModel();
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(majorSeamEdge, new CurveGeometryId(1), new ParameterInterval(0d, 2d * double.Pi)));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(minorSeamEdge, new CurveGeometryId(2), new ParameterInterval(0d, 2d * double.Pi)));
+ bindings.AddFaceBinding(new FaceGeometryBinding(torusFace, new SurfaceGeometryId(1)));
+
+ return ValidateAndReturn(new BrepBody(
+ builder.Model,
+ geometry,
+ bindings,
+ new Dictionary<VertexId, Point3D>
+ {
+ [seamVertex] = sharedVertexPoint,
+ }));
+ }
+
public static KernelResult<BrepBody> CreateSphere(double radius)
{
var diagnostics = ValidatePositiveFinite((radius, nameof(radius)));