Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add torus support to Firmament

Yuechen Li254b3e651451
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs
index e601c6a5..6f9f853d 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs
@@ -41,6 +41,18 @@ public sealed class FirmamentBuildAndExportTests
"sphere");
}
+ [Fact]
+ public void Run_TorusBasicExample_Writes_Default_Export_Artifact()
+ {
+ AssertExampleBuildAndExport(
+ "testdata/firmament/examples/torus_basic.firmament",
+ "torus_basic.step",
+ "donut1",
+ 0,
+ "primitive",
+ "torus");
+ }
+
[Fact]
public void Run_ConeFrustumBasicExample_Writes_Default_Export_Artifact()
{
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentExamplePackSmokeTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentExamplePackSmokeTests.cs
index 5ca920c7..5e4153ea 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentExamplePackSmokeTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentExamplePackSmokeTests.cs
@@ -11,6 +11,7 @@ public sealed class FirmamentExamplePackSmokeTests
"testdata/firmament/examples/cone_frustum_basic.firmament",
"testdata/firmament/examples/cone_pointed_top_zero.firmament",
"testdata/firmament/examples/sphere_basic.firmament",
+ "testdata/firmament/examples/torus_basic.firmament",
"testdata/firmament/examples/box_with_hole.firmament",
"testdata/firmament/examples/placed_primitive.firmament",
"testdata/firmament/examples/cnc_min_tool_radius_demo.firmament"
@@ -36,6 +37,7 @@ public sealed class FirmamentExamplePackSmokeTests
[InlineData("testdata/firmament/examples/cone_frustum_basic.firmament", "frustum1", 0, "primitive", "cone")]
[InlineData("testdata/firmament/examples/cone_pointed_top_zero.firmament", "pointed1", 0, "primitive", "cone")]
[InlineData("testdata/firmament/examples/sphere_basic.firmament", "ball", 0, "primitive", "sphere")]
+ [InlineData("testdata/firmament/examples/torus_basic.firmament", "donut1", 0, "primitive", "torus")]
[InlineData("testdata/firmament/examples/box_with_hole.firmament", "base", 0, "primitive", "box")]
[InlineData("testdata/firmament/examples/placed_primitive.firmament", "post", 1, "primitive", "cylinder")]
public void ExamplePack_GoldenPath_Examples_Export(string fixturePath, string expectedFeatureId, int expectedOpIndex, string expectedBodyCategory, string expectedFeatureKind)
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs
index d1a70aa6..141d9b43 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs
@@ -48,6 +48,20 @@ public sealed class FirmamentPrimitiveExecutionTests
Assert.NotEmpty(executed.Body.Topology.Faces);
}
+ [Fact]
+ public void Compile_Executes_Torus_Primitive_Into_Real_Body_With_Truthful_Topology()
+ {
+ var result = CompileFixture("testdata/firmament/examples/torus_basic.firmament");
+
+ Assert.True(result.Compilation.IsSuccess);
+ var executed = Assert.Single(result.Compilation.Value.PrimitiveExecutionResult!.ExecutedPrimitives);
+ Assert.Equal("donut1", executed.FeatureId);
+ Assert.Equal(FirmamentLoweredPrimitiveKind.Torus, executed.Kind);
+ Assert.Single(executed.Body.Topology.Faces);
+ Assert.Equal(2, executed.Body.Topology.Edges.Count());
+ Assert.Single(executed.Body.Topology.Vertices);
+ }
+
[Fact]
public void Compile_Executes_Cone_Primitive_Into_Real_Body_With_Truthful_Frustum_Topology()
{
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveLoweringTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveLoweringTests.cs
index 1ddfab75..96be3947 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveLoweringTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveLoweringTests.cs
@@ -154,6 +154,35 @@ public sealed class FirmamentPrimitiveLoweringTests
Assert.Equal(2, parameters.Radius);
}
+ [Fact]
+ public void Compile_Lowers_Torus_Primitive()
+ {
+ var result = Compile(
+ """
+ firmament:
+ version: 1
+
+ model:
+ name: demo
+ units: mm
+
+ ops[1]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
+ """);
+
+ Assert.True(result.Compilation.IsSuccess);
+ var primitive = Assert.Single(result.Compilation.Value.PrimitiveLoweringPlan!.Primitives);
+ var parameters = Assert.IsType<FirmamentLoweredTorusParameters>(primitive.Parameters);
+ Assert.Equal("donut1", primitive.FeatureId);
+ Assert.Equal(FirmamentLoweredPrimitiveKind.Torus, primitive.Kind);
+ Assert.Equal(10, parameters.MajorRadius);
+ Assert.Equal(3, parameters.MinorRadius);
+ }
+
[Fact]
public void Compile_Lowers_Add_Boolean_With_Primary_Reference_And_Coarse_Tool()
{
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
index 99fc41f5..a337ab75 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
@@ -143,6 +143,10 @@ public sealed class FirmamentScaffoldTests
public void Compiler_Classifies_Cone_As_KnownPrimitiveOpFamily() =>
AssertClassifiedFamily("cone", FirmamentOpFamily.Primitive);
+ [Fact]
+ public void Compiler_Classifies_Torus_As_KnownPrimitiveOpFamily() =>
+ AssertClassifiedFamily("torus", FirmamentOpFamily.Primitive);
+
[Fact]
public void Compiler_Classifies_KnownBooleanOpFamily() =>
AssertClassifiedFamily("intersect", FirmamentOpFamily.Boolean);
@@ -316,10 +320,10 @@ public sealed class FirmamentScaffoldTests
ops[1]:
-
- op: torus
+ op: pyramid
""",
FirmamentDiagnosticCodes.StructureUnknownOpKind,
- "Operation entry at index 0 has unknown op kind 'torus'.");
+ "Operation entry at index 0 has unknown op kind 'pyramid'.");
[Fact]
public void Compiler_Rejects_UnknownOpKind_Deterministically()
@@ -335,7 +339,7 @@ public sealed class FirmamentScaffoldTests
ops[1]:
-
- op: torus
+ op: pyramid
""";
var first = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
@@ -522,6 +526,26 @@ public sealed class FirmamentScaffoldTests
""",
1);
+ [Fact]
+ public void Compiler_Accepts_ValidTorusPrimitiveOp() =>
+ AssertValidOpsCount(
+ """
+ firmament:
+ version: 1
+
+ model:
+ name: demo
+ units: mm
+
+ ops[1]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
+ """,
+ 1);
+
[Fact]
public void Compiler_Rejects_Box_MissingId() =>
AssertSingleValidationError(
@@ -743,6 +767,48 @@ public sealed class FirmamentScaffoldTests
FirmamentDiagnosticCodes.PrimitiveInvalidFieldValue,
"Primitive op 'sphere' at index 0 has invalid field 'radius' value; expected a numeric value greater than 0.");
+ [Fact]
+ public void Compiler_Rejects_Torus_NonPositiveMajorRadius() =>
+ AssertSingleValidationError(
+ """
+ firmament:
+ version: 1
+
+ model:
+ name: demo
+ units: mm
+
+ ops[1]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 0
+ minor_radius: 3
+ """,
+ FirmamentDiagnosticCodes.PrimitiveInvalidFieldValue,
+ "Primitive op 'torus' at index 0 has invalid field 'major_radius' value; expected a numeric value greater than 0.");
+
+ [Fact]
+ public void Compiler_Rejects_Torus_MajorRadius_NotGreaterThan_MinorRadius() =>
+ AssertSingleValidationError(
+ """
+ firmament:
+ version: 1
+
+ model:
+ name: demo
+ units: mm
+
+ ops[1]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 3
+ minor_radius: 3
+ """,
+ FirmamentDiagnosticCodes.PrimitiveInvalidFieldValue,
+ "Primitive op 'torus' at index 0 has invalid field 'major_radius' value; expected a numeric value greater than 'minor_radius'.");
+
[Fact]
public void PrimitiveValidation_Diagnostics_AreDeterministic()
{
@@ -854,6 +920,13 @@ public sealed class FirmamentScaffoldTests
bottom_radius: 3
top_radius: 1
height: 5
+""",
+ "torus" => """
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
""",
"sphere" => """
-
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs
index fa8b8580..71c22d0a 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs
@@ -126,6 +126,27 @@ public sealed class FirmamentStepExporterTests
Assert.Equal(first.Value.StepText, File.ReadAllText(artifactPath));
}
+ [Fact]
+ public void Export_TorusExample_Exports_Successfully_With_Metadata_Deterministic_Text_And_Persisted_Artifact()
+ {
+ var first = ExportFixture("testdata/firmament/examples/torus_basic.firmament");
+ var second = ExportFixture("testdata/firmament/examples/torus_basic.firmament");
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.Equal("donut1", first.Value.ExportedFeatureId);
+ Assert.Equal(0, first.Value.ExportedOpIndex);
+ Assert.Equal("primitive", first.Value.ExportedBodyCategory);
+ Assert.Equal("torus", first.Value.ExportedFeatureKind);
+ Assert.NotEmpty(first.Value.StepText);
+ Assert.Equal(first.Value.StepText, second.Value.StepText);
+ Assert.Contains("TOROIDAL_SURFACE", first.Value.StepText, StringComparison.Ordinal);
+
+ var artifactPath = WriteExportArtifact("m10g2-torus.step", first.Value.StepText);
+ Assert.True(File.Exists(artifactPath));
+ Assert.Equal(first.Value.StepText, File.ReadAllText(artifactPath));
+ }
+
[Fact]
public void Export_ConeExample_Exports_Successfully_With_Metadata_Deterministic_Text_And_Persisted_Artifact()
{
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentTorusContractTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentTorusContractTests.cs
new file mode 100644
index 00000000..7a58016a
--- /dev/null
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentTorusContractTests.cs
@@ -0,0 +1,134 @@
+using System.Linq;
+using Aetheris.Kernel.Core.Geometry;
+using Aetheris.Kernel.Firmament.Lowering;
+
+namespace Aetheris.Kernel.Firmament.Tests;
+
+public sealed class FirmamentTorusContractTests
+{
+ [Fact]
+ public void Compile_Attaches_Torus_Selector_Contract_Metadata_Truthfully()
+ {
+ const string source = """
+firmament:
+ version: 1
+
+model:
+ name: torus_selector_contract
+ units: mm
+
+ops[4]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
+ -
+ op: expect_exists
+ target: donut1.surface
+ -
+ op: expect_exists
+ target: donut1.edges
+ -
+ op: expect_exists
+ target: donut1.vertices
+""";
+
+ var result = FirmamentCorpusHarness.Compile(source);
+
+ Assert.True(result.Compilation.IsSuccess);
+ var parsedOps = result.Compilation.Value.ParsedDocument!.Ops.Entries;
+ Assert.NotNull(parsedOps[1].ClassifiedFields);
+ Assert.NotNull(parsedOps[2].ClassifiedFields);
+ Assert.NotNull(parsedOps[3].ClassifiedFields);
+ Assert.Equal("Face", parsedOps[1].ClassifiedFields!["selectorResultKind"]);
+ Assert.Equal("One", parsedOps[1].ClassifiedFields!["selectorCardinality"]);
+ Assert.Equal("EdgeSet", parsedOps[2].ClassifiedFields!["selectorResultKind"]);
+ Assert.Equal("Many", parsedOps[2].ClassifiedFields!["selectorCardinality"]);
+ Assert.Equal("VertexSet", parsedOps[3].ClassifiedFields!["selectorResultKind"]);
+ Assert.Equal("Many", parsedOps[3].ClassifiedFields!["selectorCardinality"]);
+ }
+
+ [Fact]
+ public void Compile_Executes_Torus_Selector_Contract_With_Truthful_Runtime_Topology()
+ {
+ const string source = """
+firmament:
+ version: 1
+
+model:
+ name: torus_selector_truth
+ units: mm
+
+ops[4]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
+ -
+ op: expect_selectable
+ target: donut1.surface
+ count: 1
+ -
+ op: expect_selectable
+ target: donut1.edges
+ count: 2
+ -
+ op: expect_selectable
+ target: donut1.vertices
+ count: 1
+""";
+
+ var result = FirmamentCorpusHarness.Compile(source);
+
+ Assert.True(result.Compilation.IsSuccess);
+ var executed = Assert.Single(result.Compilation.Value.PrimitiveExecutionResult!.ExecutedPrimitives);
+ Assert.Equal(FirmamentLoweredPrimitiveKind.Torus, executed.Kind);
+ Assert.Single(executed.Body.Topology.Faces);
+ Assert.Equal(2, executed.Body.Topology.Edges.Count());
+ Assert.Single(executed.Body.Topology.Vertices);
+ var face = Assert.Single(executed.Body.Topology.Faces);
+ Assert.True(executed.Body.TryGetFaceSurfaceGeometry(face.Id, out var surface));
+ Assert.Equal(SurfaceGeometryKind.Torus, surface!.Kind);
+
+ Assert.Equal(3, result.Compilation.Value.ValidationExecutionResult!.Validations.Count);
+ Assert.All(result.Compilation.Value.ValidationExecutionResult.Validations, validation =>
+ {
+ Assert.True(validation.IsExecuted);
+ Assert.True(validation.IsSuccess);
+ Assert.Null(validation.Reason);
+ });
+ }
+
+ [Fact]
+ public void Compile_Rejects_False_Torus_Selector_Families_Not_In_Truthful_Contract()
+ {
+ const string source = """
+firmament:
+ version: 1
+
+model:
+ name: torus_false_selector_family
+ units: mm
+
+ops[2]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
+ -
+ op: expect_exists
+ target: donut1.outer_surface
+""";
+
+ var result = FirmamentCorpusHarness.Compile(source);
+
+ Assert.False(result.Compilation.IsSuccess);
+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);
+ Assert.Equal(
+ "[FIRM-REF-0006] Validation op 'expect_exists' at index 1 has selector port 'outer_surface' not allowed for feature kind 'torus' on feature id 'donut1' via field 'target'.",
+ diagnostic.Message);
+ }
+}
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetFeatureExistenceTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetFeatureExistenceTests.cs
index a4192568..bd948e90 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetFeatureExistenceTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetFeatureExistenceTests.cs
@@ -173,6 +173,9 @@ public sealed partial class FirmamentValidationTargetFeatureExistenceTests
[InlineData("frustum.side_face", "Face", "One")]
[InlineData("frustum.circular_edges", "EdgeSet", "Many")]
[InlineData("sphere.surface", "Face", "One")]
+ [InlineData("donut.surface", "Face", "One")]
+ [InlineData("donut.edges", "EdgeSet", "Many")]
+ [InlineData("donut.vertices", "VertexSet", "Many")]
public void Compiler_Attaches_SelectorContractMetadata_For_Legal_SelectorTargets(string selectorTarget, string expectedResultKind, string expectedCardinality)
{
var source = $$"""
@@ -183,7 +186,7 @@ model:
name: demo
units: mm
-ops[5]:
+ops[6]:
-
op: box
id: base
@@ -206,6 +209,11 @@ ops[5]:
bottom_radius: 5
top_radius: 2
height: 9
+ -
+ op: torus
+ id: donut
+ major_radius: 10
+ minor_radius: 3
-
op: expect_exists
target: {{selectorTarget}}
@@ -214,7 +222,7 @@ ops[5]:
var result = new FirmamentCompiler().Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
Assert.True(result.Compilation.IsSuccess);
- var validationOp = result.Compilation.Value.ParsedDocument!.Ops.Entries[4];
+ var validationOp = result.Compilation.Value.ParsedDocument!.Ops.Entries[5];
Assert.NotNull(validationOp.ClassifiedFields);
Assert.Equal("SelectorShaped", validationOp.ClassifiedFields!["targetShape"]);
Assert.Equal(expectedResultKind, validationOp.ClassifiedFields["selectorResultKind"]);
diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentPlacementResolver.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentPlacementResolver.cs
index 12752342..8fc16dd6 100644
--- a/Aetheris.Kernel.Firmament/Execution/FirmamentPlacementResolver.cs
+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentPlacementResolver.cs
@@ -87,7 +87,7 @@ internal static class FirmamentPlacementResolver
.Where(x => x.Surface.Kind == SurfaceGeometryKind.Plane && Math.Abs(x.Surface.Plane!.Value.Normal.ToVector().Z) <= 0.5d)
.Select(x => x.Point),
"surface" => facePoints
- .Where(x => x.Surface.Kind == SurfaceGeometryKind.Sphere)
+ .Where(x => x.Surface.Kind is SurfaceGeometryKind.Sphere or SurfaceGeometryKind.Torus)
.Select(x => x.Point),
_ => facePoints.Select(x => x.Point)
};
@@ -105,6 +105,7 @@ internal static class FirmamentPlacementResolver
SurfaceGeometryKind.Plane => s.Plane!.Value.Origin,
SurfaceGeometryKind.Cylinder => s.Cylinder!.Value.Origin,
SurfaceGeometryKind.Cone => GetConicalFaceRepresentativePoint(body, faceId, s.Cone!.Value),
+ SurfaceGeometryKind.Torus => s.Torus!.Value.Center,
SurfaceGeometryKind.Sphere => s.Sphere!.Value.Center,
_ => null
};
@@ -216,6 +217,7 @@ internal static class FirmamentPlacementResolver
}
}
+ if (f == 1 && e == 2) return FirmamentKnownOpKind.Torus;
if (f == 1) return FirmamentKnownOpKind.Sphere;
return FirmamentKnownOpKind.Add;
}
diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs
index a79bbf7b..fc483c50 100644
--- a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs
+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs
@@ -113,6 +113,7 @@ internal static class FirmamentPrimitiveExecutor
FirmamentLoweredPrimitiveKind.Box => BrepPrimitives.CreateBox(((FirmamentLoweredBoxParameters)primitive.Parameters).SizeX, ((FirmamentLoweredBoxParameters)primitive.Parameters).SizeY, ((FirmamentLoweredBoxParameters)primitive.Parameters).SizeZ),
FirmamentLoweredPrimitiveKind.Cylinder => BrepPrimitives.CreateCylinder(((FirmamentLoweredCylinderParameters)primitive.Parameters).Radius, ((FirmamentLoweredCylinderParameters)primitive.Parameters).Height),
FirmamentLoweredPrimitiveKind.Cone => ExecuteCone((FirmamentLoweredConeParameters)primitive.Parameters),
+ FirmamentLoweredPrimitiveKind.Torus => BrepPrimitives.CreateTorus(((FirmamentLoweredTorusParameters)primitive.Parameters).MajorRadius, ((FirmamentLoweredTorusParameters)primitive.Parameters).MinorRadius),
FirmamentLoweredPrimitiveKind.Sphere => BrepPrimitives.CreateSphere(((FirmamentLoweredSphereParameters)primitive.Parameters).Radius),
_ => KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, $"Primitive execution for kind '{primitive.Kind}' is not implemented.")])
};
@@ -203,6 +204,7 @@ internal static class FirmamentPrimitiveExecutor
SurfaceGeometryKind.Plane => SurfaceGeometry.FromPlane(new PlaneSurface(surfaceEntry.Value.Plane!.Value.Origin + translation, surfaceEntry.Value.Plane.Value.Normal, surfaceEntry.Value.Plane.Value.UAxis)),
SurfaceGeometryKind.Cylinder => SurfaceGeometry.FromCylinder(new CylinderSurface(surfaceEntry.Value.Cylinder!.Value.Origin + translation, surfaceEntry.Value.Cylinder.Value.Axis, surfaceEntry.Value.Cylinder.Value.Radius, surfaceEntry.Value.Cylinder.Value.XAxis)),
SurfaceGeometryKind.Cone => SurfaceGeometry.FromCone(new ConeSurface(surfaceEntry.Value.Cone!.Value.PlacementOrigin + translation, surfaceEntry.Value.Cone.Value.Axis, surfaceEntry.Value.Cone.Value.PlacementRadius, surfaceEntry.Value.Cone.Value.SemiAngleRadians, surfaceEntry.Value.Cone.Value.ReferenceAxis)),
+ SurfaceGeometryKind.Torus => SurfaceGeometry.FromTorus(new TorusSurface(surfaceEntry.Value.Torus!.Value.Center + translation, surfaceEntry.Value.Torus.Value.Axis, surfaceEntry.Value.Torus.Value.MajorRadius, surfaceEntry.Value.Torus.Value.MinorRadius, surfaceEntry.Value.Torus.Value.XAxis)),
SurfaceGeometryKind.Sphere => SurfaceGeometry.FromSphere(new SphereSurface(surfaceEntry.Value.Sphere!.Value.Center + translation, surfaceEntry.Value.Sphere.Value.Axis, surfaceEntry.Value.Sphere.Value.Radius, surfaceEntry.Value.Sphere.Value.XAxis)),
_ => surfaceEntry.Value
});
diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentSelectorResolver.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentSelectorResolver.cs
index f44c88ee..cb9adc30 100644
--- a/Aetheris.Kernel.Firmament/Execution/FirmamentSelectorResolver.cs
+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentSelectorResolver.cs
@@ -34,6 +34,8 @@ internal static class FirmamentSelectorResolver
? cylinderCount
: TryResolveConeCount(body, port, resultKind, out var coneCount)
? coneCount
+ : TryResolveTorusCount(body, port, resultKind, out var torusCount)
+ ? torusCount
: resultKind switch
{
FirmamentSelectorResultKind.Face or FirmamentSelectorResultKind.FaceSet => body.Topology.Faces.Count(),
@@ -92,6 +94,26 @@ internal static class FirmamentSelectorResolver
return true;
}
+ private static bool TryResolveTorusCount(BrepBody body, string port, FirmamentSelectorResultKind resultKind, out int count)
+ {
+ count = 0;
+
+ if (!LooksLikeTorus(body))
+ {
+ return false;
+ }
+
+ count = port switch
+ {
+ "surface" => CountFaces(body, surface => surface.Kind == SurfaceGeometryKind.Torus),
+ "edges" when resultKind == FirmamentSelectorResultKind.EdgeSet => body.Topology.Edges.Count(),
+ "vertices" when resultKind == FirmamentSelectorResultKind.VertexSet => body.Topology.Vertices.Count(),
+ _ => 0
+ };
+
+ return true;
+ }
+
private static bool LooksLikeCylinder(BrepBody body)
{
if (body.Topology.Faces.Count() != 3 || body.Topology.Edges.Count() != 3)
@@ -160,6 +182,19 @@ internal static class FirmamentSelectorResolver
return conicalFaces == 1 && (planarFaces == 1 || planarFaces == 2);
}
+ private static bool LooksLikeTorus(BrepBody body)
+ {
+ if (body.Topology.Faces.Count() != 1 || body.Topology.Edges.Count() != 2 || body.Topology.Vertices.Count() != 1)
+ {
+ return false;
+ }
+
+ var face = body.Topology.Faces.Single();
+ return body.TryGetFaceSurfaceGeometry(face.Id, out var surface)
+ && surface is not null
+ && surface.Kind == SurfaceGeometryKind.Torus;
+ }
+
private static int CountFaces(BrepBody body, Func<SurfaceGeometry, bool> predicate)
{
var count = 0;
diff --git a/Aetheris.Kernel.Firmament/Formatting/FirmamentCanonicalFormatter.cs b/Aetheris.Kernel.Firmament/Formatting/FirmamentCanonicalFormatter.cs
index 5f34e282..869e2c32 100644
--- a/Aetheris.Kernel.Firmament/Formatting/FirmamentCanonicalFormatter.cs
+++ b/Aetheris.Kernel.Firmament/Formatting/FirmamentCanonicalFormatter.cs
@@ -124,6 +124,10 @@ internal static class FirmamentCanonicalFormatter
EmitIfPresent(builder, entry.RawFields, emitted, indentLevel, "top_radius");
EmitIfPresent(builder, entry.RawFields, emitted, indentLevel, "height");
break;
+ case FirmamentKnownOpKind.Torus:
+ EmitIfPresent(builder, entry.RawFields, emitted, indentLevel, "major_radius");
+ EmitIfPresent(builder, entry.RawFields, emitted, indentLevel, "minor_radius");
+ break;
case FirmamentKnownOpKind.Sphere:
EmitIfPresent(builder, entry.RawFields, emitted, indentLevel, "radius");
break;
diff --git a/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLowerer.cs b/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLowerer.cs
index e97cce39..8ada404f 100644
--- a/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLowerer.cs
+++ b/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLowerer.cs
@@ -55,6 +55,17 @@ internal static class FirmamentPrimitiveLowerer
Placement: LowerPlacement(entry.Placement)));
break;
+ case FirmamentKnownOpKind.Torus:
+ loweredPrimitives.Add(new FirmamentLoweredPrimitive(
+ OpIndex: index,
+ FeatureId: entry.RawFields["id"],
+ Kind: FirmamentLoweredPrimitiveKind.Torus,
+ Parameters: new FirmamentLoweredTorusParameters(
+ MajorRadius: ParseScalar(entry.RawFields["major_radius"]),
+ MinorRadius: ParseScalar(entry.RawFields["minor_radius"])),
+ Placement: LowerPlacement(entry.Placement)));
+ break;
+
case FirmamentKnownOpKind.Sphere:
loweredPrimitives.Add(new FirmamentLoweredPrimitive(
OpIndex: index,
diff --git a/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLoweringPlan.cs b/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLoweringPlan.cs
index 30249f3b..87335a66 100644
--- a/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLoweringPlan.cs
+++ b/Aetheris.Kernel.Firmament/Lowering/FirmamentPrimitiveLoweringPlan.cs
@@ -45,6 +45,7 @@ public enum FirmamentLoweredPrimitiveKind
Box,
Cylinder,
Cone,
+ Torus,
Sphere
}
@@ -59,6 +60,9 @@ public sealed record FirmamentLoweredCylinderParameters(double Radius, double He
public sealed record FirmamentLoweredConeParameters(double BottomRadius, double TopRadius, double Height)
: FirmamentLoweredPrimitiveParameters;
+public sealed record FirmamentLoweredTorusParameters(double MajorRadius, double MinorRadius)
+ : FirmamentLoweredPrimitiveParameters;
+
public sealed record FirmamentLoweredSphereParameters(double Radius)
: FirmamentLoweredPrimitiveParameters;
diff --git a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs
index 294e9666..0fb4b6f4 100644
--- a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs
+++ b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs
@@ -5,6 +5,7 @@ public enum FirmamentKnownOpKind
Box,
Cylinder,
Cone,
+ Torus,
Sphere,
Add,
Subtract,
@@ -22,6 +23,7 @@ internal static class FirmamentKnownOpKinds
["box"] = FirmamentKnownOpKind.Box,
["cylinder"] = FirmamentKnownOpKind.Cylinder,
["cone"] = FirmamentKnownOpKind.Cone,
+ ["torus"] = FirmamentKnownOpKind.Torus,
["sphere"] = FirmamentKnownOpKind.Sphere,
["add"] = FirmamentKnownOpKind.Add,
["subtract"] = FirmamentKnownOpKind.Subtract,
@@ -40,6 +42,7 @@ internal static class FirmamentKnownOpKinds
FirmamentKnownOpKind.Box => FirmamentOpFamily.Primitive,
FirmamentKnownOpKind.Cylinder => FirmamentOpFamily.Primitive,
FirmamentKnownOpKind.Cone => FirmamentOpFamily.Primitive,
+ FirmamentKnownOpKind.Torus => FirmamentOpFamily.Primitive,
FirmamentKnownOpKind.Sphere => FirmamentOpFamily.Primitive,
FirmamentKnownOpKind.Add => FirmamentOpFamily.Boolean,
FirmamentKnownOpKind.Subtract => FirmamentOpFamily.Boolean,
diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentPrimitiveRequiredFieldValidator.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentPrimitiveRequiredFieldValidator.cs
index 90fcc208..07c8e091 100644
--- a/Aetheris.Kernel.Firmament/Validation/FirmamentPrimitiveRequiredFieldValidator.cs
+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentPrimitiveRequiredFieldValidator.cs
@@ -27,6 +27,7 @@ internal static class FirmamentPrimitiveRequiredFieldValidator
FirmamentKnownOpKind.Box => ValidateBox(entry, index),
FirmamentKnownOpKind.Cylinder => ValidateCylinder(entry, index),
FirmamentKnownOpKind.Cone => ValidateCone(entry, index),
+ FirmamentKnownOpKind.Torus => ValidateTorus(entry, index),
FirmamentKnownOpKind.Sphere => ValidateSphere(entry, index),
_ => KernelResult<bool>.Success(true)
};
@@ -129,6 +130,41 @@ internal static class FirmamentPrimitiveRequiredFieldValidator
return KernelResult<bool>.Success(true);
}
+ private static KernelResult<bool> ValidateTorus(FirmamentParsedOpEntry entry, int opIndex)
+ {
+ if (!TryGetRequiredNonEmptyScalar(entry, "id", opIndex, out var missingOrTypeDiagnostic))
+ {
+ return KernelResult<bool>.Failure([missingOrTypeDiagnostic!]);
+ }
+
+ var majorRadiusResult = ValidatePositiveNumericField(entry, "major_radius", opIndex);
+ if (!majorRadiusResult.IsSuccess)
+ {
+ return majorRadiusResult;
+ }
+
+ var minorRadiusResult = ValidatePositiveNumericField(entry, "minor_radius", opIndex);
+ if (!minorRadiusResult.IsSuccess)
+ {
+ return minorRadiusResult;
+ }
+
+ var majorRadius = ParseRequiredNumeric(entry.RawFields["major_radius"]);
+ var minorRadius = ParseRequiredNumeric(entry.RawFields["minor_radius"]);
+ if (majorRadius <= minorRadius)
+ {
+ return InvalidFieldValue("major_radius", opIndex, entry.OpName, "expected a numeric value greater than 'minor_radius'");
+ }
+
+ var placementResult = ValidatePlacement(entry, opIndex);
+ if (!placementResult.IsSuccess)
+ {
+ return placementResult;
+ }
+
+ return KernelResult<bool>.Success(true);
+ }
+
private static KernelResult<bool> ValidateCone(FirmamentParsedOpEntry entry, int opIndex)
{
if (!TryGetRequiredNonEmptyScalar(entry, "id", opIndex, out var missingOrTypeDiagnostic))
diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentSelectorContracts.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentSelectorContracts.cs
index 3390819c..436a235a 100644
--- a/Aetheris.Kernel.Firmament/Validation/FirmamentSelectorContracts.cs
+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentSelectorContracts.cs
@@ -42,6 +42,14 @@ internal static class FirmamentSelectorContracts
["surface"] = new(FirmamentSelectorResultKind.Face, FirmamentSelectorCardinality.One)
};
+ private static readonly IReadOnlyDictionary<string, FirmamentSelectorPortContract> TorusPorts =
+ new Dictionary<string, FirmamentSelectorPortContract>(StringComparer.Ordinal)
+ {
+ ["surface"] = new(FirmamentSelectorResultKind.Face, FirmamentSelectorCardinality.One),
+ ["edges"] = new(FirmamentSelectorResultKind.EdgeSet, FirmamentSelectorCardinality.Many),
+ ["vertices"] = new(FirmamentSelectorResultKind.VertexSet, FirmamentSelectorCardinality.Many)
+ };
+
private static readonly IReadOnlyDictionary<string, FirmamentSelectorPortContract> BooleanPorts =
new Dictionary<string, FirmamentSelectorPortContract>(StringComparer.Ordinal)
{
@@ -65,6 +73,9 @@ internal static class FirmamentSelectorContracts
case FirmamentKnownOpKind.Cone:
allowedPorts = ConeAllowedPorts;
return true;
+ case FirmamentKnownOpKind.Torus:
+ allowedPorts = TorusAllowedPorts;
+ return true;
case FirmamentKnownOpKind.Sphere:
allowedPorts = SphereAllowedPorts;
return true;
@@ -104,6 +115,9 @@ internal static class FirmamentSelectorContracts
case FirmamentKnownOpKind.Cone:
contracts = ConePorts;
return true;
+ case FirmamentKnownOpKind.Torus:
+ contracts = TorusPorts;
+ return true;
case FirmamentKnownOpKind.Sphere:
contracts = SpherePorts;
return true;
@@ -124,6 +138,7 @@ internal static class FirmamentSelectorContracts
private static readonly IReadOnlySet<string> BoxAllowedPorts = new HashSet<string>(BoxPorts.Keys, StringComparer.Ordinal);
private static readonly IReadOnlySet<string> CylinderAllowedPorts = new HashSet<string>(CylinderPorts.Keys, StringComparer.Ordinal);
private static readonly IReadOnlySet<string> ConeAllowedPorts = new HashSet<string>(ConePorts.Keys, StringComparer.Ordinal);
+ private static readonly IReadOnlySet<string> TorusAllowedPorts = new HashSet<string>(TorusPorts.Keys, StringComparer.Ordinal);
private static readonly IReadOnlySet<string> SphereAllowedPorts = new HashSet<string>(SpherePorts.Keys, StringComparer.Ordinal);
private static readonly IReadOnlySet<string> BooleanAllowedPorts = new HashSet<string>(BooleanPorts.Keys, StringComparer.Ordinal);
private static readonly IReadOnlySet<string> EmptyPorts = new HashSet<string>(StringComparer.Ordinal);
diff --git a/testdata/firmament/examples/README.md b/testdata/firmament/examples/README.md
index 1b820316..4e971151 100644
--- a/testdata/firmament/examples/README.md
+++ b/testdata/firmament/examples/README.md
@@ -7,6 +7,7 @@ This directory contains small canonical Firmament examples for the current v1 su
- `cone_frustum_basic.firmament` — minimal single frustum-cone export example using the v1 `cone` primitive; helper export path: `testdata/firmament/exports/cone_frustum_basic.step`.
- `cone_pointed_top_zero.firmament` — minimal single pointed-cone export example using the same v1 `cone` primitive with `top_radius: 0`; helper export path: `testdata/firmament/exports/cone_pointed_top_zero.step`.
- `sphere_basic.firmament` — minimal single-sphere export example; helper export path: `testdata/firmament/exports/sphere_basic.step`.
+- `torus_basic.firmament` — minimal single-torus export example using the canonical `major_radius` / `minor_radius` surface; helper export paths: `testdata/firmament/exports/torus_basic.step` and the milestone proof artifact `testdata/firmament/exports/m10g2-torus.step`.
- `box_with_hole.firmament` — box plus subtract-with-cylinder boolean syntax example; helper export path: `testdata/firmament/exports/box_with_hole.step`.
- `placed_primitive.firmament` — selector-based placement using `place.on` and `offset[3]`; helper export path: `testdata/firmament/exports/placed_primitive.step`.
- `cnc_min_tool_radius_demo.firmament` — schema-aware CNC example using `minimum_tool_radius` with a subtract cylinder sized to pass the current rule.
diff --git a/testdata/firmament/examples/torus_basic.firmament b/testdata/firmament/examples/torus_basic.firmament
new file mode 100644
index 00000000..5103af11
--- /dev/null
+++ b/testdata/firmament/examples/torus_basic.firmament
@@ -0,0 +1,13 @@
+firmament:
+ version: 1
+
+model:
+ name: torus_basic
+ units: mm
+
+ops[1]:
+ -
+ op: torus
+ id: donut1
+ major_radius: 10
+ minor_radius: 3
diff --git a/testdata/firmament/exports/m10g2-torus.step b/testdata/firmament/exports/m10g2-torus.step
new file mode 100644
index 00000000..33a5bafd
--- /dev/null
+++ b/testdata/firmament/exports/m10g2-torus.step
@@ -0,0 +1,50 @@
+ISO-10303-21;
+HEADER;
+FILE_DESCRIPTION(('Aetheris AP242 subset export'),'2;1');
+FILE_NAME('aetheris_export.step','1970-01-01T00:00:00',('Aetheris'),('Aetheris'),'Aetheris.Kernel','Aetheris.Kernel','');
+FILE_SCHEMA(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF'));
+ENDSEC;
+DATA;
+#1=CARTESIAN_POINT($,(0,0,0));
+#2=DIRECTION($,(0,1,0));
+#3=DIRECTION($,(-1,0,0));
+#4=AXIS2_PLACEMENT_3D($,#1,#2,#3);
+#5=TOROIDAL_SURFACE($,#4,10,3);
+#6=CARTESIAN_POINT($,(-7,0,0));
+#7=VERTEX_POINT($,#6);
+#8=CARTESIAN_POINT($,(0,0,0));
+#9=DIRECTION($,(0,1,0));
+#10=DIRECTION($,(-1,0,0));
+#11=AXIS2_PLACEMENT_3D($,#8,#9,#10);
+#12=CIRCLE($,#11,7);
+#13=EDGE_CURVE($,#7,#7,#12,.T.);
+#14=ORIENTED_EDGE($,$,$,#13,.T.);
+#15=CARTESIAN_POINT($,(-10,0,0));
+#16=DIRECTION($,(0,0,1));
+#17=DIRECTION($,(1,0,0));
+#18=AXIS2_PLACEMENT_3D($,#15,#16,#17);
+#19=CIRCLE($,#18,3);
+#20=EDGE_CURVE($,#7,#7,#19,.T.);
+#21=ORIENTED_EDGE($,$,$,#20,.F.);
+#22=ORIENTED_EDGE($,$,$,#13,.F.);
+#23=ORIENTED_EDGE($,$,$,#20,.T.);
+#24=EDGE_LOOP($,(#14,#21,#22,#23));
+#25=FACE_OUTER_BOUND('',#24,.T.);
+#26=ADVANCED_FACE('',(#25),#5,.T.);
+#27=CLOSED_SHELL($,(#26));
+#28=MANIFOLD_SOLID_BREP('AetherisBody',#27);
+#29=APPLICATION_CONTEXT('mechanical design');
+#30=PRODUCT_CONTEXT('',#29,'mechanical');
+#31=PRODUCT('AETHERIS','AetherisBody','',(#30));
+#32=PRODUCT_DEFINITION_FORMATION('','',#31);
+#33=PRODUCT_DEFINITION_CONTEXT('design',#29,'design');
+#34=PRODUCT_DEFINITION('','',#32,#33);
+#35=PRODUCT_DEFINITION_SHAPE('','',#34);
+#36=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
+#37=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));
+#38=(NAMED_UNIT(*)SI_UNIT($,.STERADIAN.)SOLID_ANGLE_UNIT());
+#39=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#36,#37,#38))REPRESENTATION_CONTEXT('3','3D'));
+#40=SHAPE_REPRESENTATION('AetherisBody',(#28),#39);
+#41=SHAPE_DEFINITION_REPRESENTATION(#35,#40);
+ENDSEC;
+END-ISO-10303-21;
diff --git a/testdata/firmament/exports/torus_basic.step b/testdata/firmament/exports/torus_basic.step
new file mode 100644
index 00000000..33a5bafd
--- /dev/null
+++ b/testdata/firmament/exports/torus_basic.step
@@ -0,0 +1,50 @@
+ISO-10303-21;
+HEADER;
+FILE_DESCRIPTION(('Aetheris AP242 subset export'),'2;1');
+FILE_NAME('aetheris_export.step','1970-01-01T00:00:00',('Aetheris'),('Aetheris'),'Aetheris.Kernel','Aetheris.Kernel','');
+FILE_SCHEMA(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF'));
+ENDSEC;
+DATA;
+#1=CARTESIAN_POINT($,(0,0,0));
+#2=DIRECTION($,(0,1,0));
+#3=DIRECTION($,(-1,0,0));
+#4=AXIS2_PLACEMENT_3D($,#1,#2,#3);
+#5=TOROIDAL_SURFACE($,#4,10,3);
+#6=CARTESIAN_POINT($,(-7,0,0));
+#7=VERTEX_POINT($,#6);
+#8=CARTESIAN_POINT($,(0,0,0));
+#9=DIRECTION($,(0,1,0));
+#10=DIRECTION($,(-1,0,0));
+#11=AXIS2_PLACEMENT_3D($,#8,#9,#10);
+#12=CIRCLE($,#11,7);
+#13=EDGE_CURVE($,#7,#7,#12,.T.);
+#14=ORIENTED_EDGE($,$,$,#13,.T.);
+#15=CARTESIAN_POINT($,(-10,0,0));
+#16=DIRECTION($,(0,0,1));
+#17=DIRECTION($,(1,0,0));
+#18=AXIS2_PLACEMENT_3D($,#15,#16,#17);
+#19=CIRCLE($,#18,3);
+#20=EDGE_CURVE($,#7,#7,#19,.T.);
+#21=ORIENTED_EDGE($,$,$,#20,.F.);
+#22=ORIENTED_EDGE($,$,$,#13,.F.);
+#23=ORIENTED_EDGE($,$,$,#20,.T.);
+#24=EDGE_LOOP($,(#14,#21,#22,#23));
+#25=FACE_OUTER_BOUND('',#24,.T.);
+#26=ADVANCED_FACE('',(#25),#5,.T.);
+#27=CLOSED_SHELL($,(#26));
+#28=MANIFOLD_SOLID_BREP('AetherisBody',#27);
+#29=APPLICATION_CONTEXT('mechanical design');
+#30=PRODUCT_CONTEXT('',#29,'mechanical');
+#31=PRODUCT('AETHERIS','AetherisBody','',(#30));
+#32=PRODUCT_DEFINITION_FORMATION('','',#31);
+#33=PRODUCT_DEFINITION_CONTEXT('design',#29,'design');
+#34=PRODUCT_DEFINITION('','',#32,#33);
+#35=PRODUCT_DEFINITION_SHAPE('','',#34);
+#36=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));
+#37=(NAMED_UNIT(*)PLANE_ANGLE_UNIT()SI_UNIT($,.RADIAN.));
+#38=(NAMED_UNIT(*)SI_UNIT($,.STERADIAN.)SOLID_ANGLE_UNIT());
+#39=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNIT_ASSIGNED_CONTEXT((#36,#37,#38))REPRESENTATION_CONTEXT('3','3D'));
+#40=SHAPE_REPRESENTATION('AetherisBody',(#28),#39);
+#41=SHAPE_DEFINITION_REPRESENTATION(#35,#40);
+ENDSEC;
+END-ISO-10303-21;
diff --git a/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament b/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament
index 160eae74..b6cdd6ce 100644
--- a/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament
+++ b/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament
@@ -7,4 +7,4 @@ model:
ops[1]:
-
- op: torus
+ op: pyramid