Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add M03 core math primitives and affine transforms
Yuechen Li0ec4c361689e
diff --git a/Aetheris.Kernel.Core.Tests/Math/Primitives3DTests.cs b/Aetheris.Kernel.Core.Tests/Math/Primitives3DTests.csnew file mode 100644index 00000000..daf753ed--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Math/Primitives3DTests.cs@@ -0,0 +1,86 @@+using Aetheris.Kernel.Core.Math;+using Aetheris.Kernel.Core.Numerics;++namespace Aetheris.Kernel.Core.Tests.Math;++public sealed class Primitives3DTests+{+ [Fact]+ public void VectorArithmetic_WorksAsExpected()+ {+ var a = new Vector3D(1, 2, 3);+ var b = new Vector3D(4, 5, 6);++ Assert.Equal(new Vector3D(5, 7, 9), a + b);+ Assert.Equal(new Vector3D(-3, -3, -3), a - b);+ Assert.Equal(new Vector3D(2, 4, 6), a * 2);+ Assert.Equal(new Vector3D(0.5, 1, 1.5), a / 2);+ }++ [Fact]+ public void DotAndCross_BasicsAreCorrect()+ {+ var x = new Vector3D(1, 0, 0);+ var y = new Vector3D(0, 1, 0);++ Assert.Equal(0, x.Dot(y));+ Assert.Equal(new Vector3D(0, 0, 1), x.Cross(y));+ }++ [Fact]+ public void LengthAndLengthSquared_AreConsistent()+ {+ var vector = new Vector3D(2, 3, 6);++ Assert.Equal(49, vector.LengthSquared);+ Assert.Equal(7, vector.Length, 10);+ }++ [Fact]+ public void Normalize_NonZeroVector_Succeeds()+ {+ var vector = new Vector3D(10, 0, 0);++ var success = vector.TryNormalize(out var normalized);++ Assert.True(success);+ Assert.Equal(new Vector3D(1, 0, 0), normalized);+ }++ [Fact]+ public void Normalize_ZeroVector_Fails()+ {+ var success = Vector3D.Zero.TryNormalize(out var normalized);++ Assert.False(success);+ Assert.Equal(Vector3D.Zero, normalized);+ }++ [Fact]+ public void PointAndVectorOperators_HaveExpectedSemantics()+ {+ var point = new Point3D(10, 20, 30);+ var vector = new Vector3D(1, 2, 3);++ Assert.Equal(new Point3D(11, 22, 33), point + vector);+ Assert.Equal(new Point3D(9, 18, 27), point - vector);+ Assert.Equal(vector, (point + vector) - point);+ }++ [Fact]+ public void DirectionCreate_NormalizesInput()+ {+ var direction = Direction3D.Create(new Vector3D(0, 3, 0));++ Assert.Equal(new Vector3D(0, 1, 0), direction.ToVector());+ }++ [Fact]+ public void DirectionCreate_RejectsNearZeroInput()+ {+ var context = new ToleranceContext(linear: 1e-6, angular: 1e-9);++ Assert.False(Direction3D.TryCreate(new Vector3D(1e-8, 0, 0), out _, context));+ Assert.Throws<ArgumentOutOfRangeException>(() => Direction3D.Create(new Vector3D(1e-8, 0, 0), context));+ }+}diff --git a/Aetheris.Kernel.Core.Tests/Math/SpatialPrimitivesTests.cs b/Aetheris.Kernel.Core.Tests/Math/SpatialPrimitivesTests.csnew file mode 100644index 00000000..3f1f205a--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Math/SpatialPrimitivesTests.cs@@ -0,0 +1,54 @@+using Aetheris.Kernel.Core.Math;++namespace Aetheris.Kernel.Core.Tests.Math;++public sealed class SpatialPrimitivesTests+{+ [Fact]+ public void BoundingBox_Construction_ValidatesMinLessThanOrEqualToMax()+ {+ Assert.Throws<ArgumentOutOfRangeException>(() =>+ new BoundingBox3D(new Point3D(2, 0, 0), new Point3D(1, 0, 0)));+ }++ [Fact]+ public void BoundingBox_Contains_UsesInclusiveBoundaries()+ {+ var box = new BoundingBox3D(new Point3D(0, 0, 0), new Point3D(1, 1, 1));++ Assert.True(box.Contains(new Point3D(0, 0, 0)));+ Assert.True(box.Contains(new Point3D(1, 1, 1)));+ Assert.False(box.Contains(new Point3D(1.1, 1, 1)));+ }++ [Fact]+ public void BoundingBox_Expand_UpdatesBounds()+ {+ var box = new BoundingBox3D(new Point3D(0, 0, 0), new Point3D(1, 1, 1));++ var expanded = box.Expand(new Point3D(-1, 2, 0.5));++ Assert.Equal(new Point3D(-1, 0, 0), expanded.Min);+ Assert.Equal(new Point3D(1, 2, 1), expanded.Max);+ }++ [Fact]+ public void BoundingBox_Union_CombinesBothBoxes()+ {+ var a = new BoundingBox3D(new Point3D(0, 0, 0), new Point3D(1, 1, 1));+ var b = new BoundingBox3D(new Point3D(-2, 0.5, 0), new Point3D(0.5, 3, 2));++ var union = a.Union(b);++ Assert.Equal(new Point3D(-2, 0, 0), union.Min);+ Assert.Equal(new Point3D(1, 3, 2), union.Max);+ }++ [Fact]+ public void Ray_PointAt_ReturnsPointAlongDirection()+ {+ var ray = new Ray3D(new Point3D(1, 1, 1), Direction3D.Create(new Vector3D(2, 0, 0)));++ Assert.Equal(new Point3D(4, 1, 1), ray.PointAt(3));+ }+}diff --git a/Aetheris.Kernel.Core.Tests/Math/Transform3DTests.cs b/Aetheris.Kernel.Core.Tests/Math/Transform3DTests.csnew file mode 100644index 00000000..dd8905ff--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Math/Transform3DTests.cs@@ -0,0 +1,92 @@+using Aetheris.Kernel.Core.Math;++namespace Aetheris.Kernel.Core.Tests.Math;++public sealed class Transform3DTests+{+ [Fact]+ public void Identity_LeavesPointAndVectorUnchanged()+ {+ var point = new Point3D(1, 2, 3);+ var vector = new Vector3D(4, 5, 6);++ Assert.Equal(point, Transform3D.Identity.Apply(point));+ Assert.Equal(vector, Transform3D.Identity.Apply(vector));+ }++ [Fact]+ public void Translation_AffectsPointsButNotVectors()+ {+ var translation = Transform3D.CreateTranslation(new Vector3D(10, 0, 0));++ Assert.Equal(new Point3D(11, 2, 3), translation.Apply(new Point3D(1, 2, 3)));+ Assert.Equal(new Vector3D(1, 2, 3), translation.Apply(new Vector3D(1, 2, 3)));+ }++ [Fact]+ public void Scale_AffectsPointAndVector()+ {+ var scale = Transform3D.CreateScale(new Vector3D(2, 3, 4));++ Assert.Equal(new Point3D(2, 6, 12), scale.Apply(new Point3D(1, 2, 3)));+ Assert.Equal(new Vector3D(2, 6, 12), scale.Apply(new Vector3D(1, 2, 3)));+ }++ [Fact]+ public void RotationZ_RotatesKnownCase()+ {+ var rotation = Transform3D.CreateRotationZ(double.Pi / 2);++ var rotated = rotation.Apply(new Vector3D(1, 0, 0));++ Assert.True(double.Abs(rotated.X) < 1e-6);+ Assert.True(double.Abs(rotated.Y - 1) < 1e-6);+ Assert.True(double.Abs(rotated.Z) < 1e-6);+ }++ [Fact]+ public void Composition_OrderIsApplyFirstThenSecond()+ {+ var translation = Transform3D.CreateTranslation(new Vector3D(1, 0, 0));+ var scale = Transform3D.CreateScale(2);++ var composed = Transform3D.Compose(translation, scale);+ var transformed = composed.Apply(new Point3D(1, 0, 0));++ Assert.Equal(new Point3D(4, 0, 0), transformed);+ }++ [Fact]+ public void Inverse_NonSingular_RoundTripsPoint()+ {+ var transform = Transform3D.CreateTranslation(new Vector3D(1, 2, 3)) * Transform3D.CreateRotationZ(0.1);+ var point = new Point3D(5, -1, 2);++ var transformed = transform.Apply(point);+ var roundTripped = transform.Inverse().Apply(transformed);++ Assert.True(double.Abs(roundTripped.X - point.X) < 1e-5);+ Assert.True(double.Abs(roundTripped.Y - point.Y) < 1e-5);+ Assert.True(double.Abs(roundTripped.Z - point.Z) < 1e-5);+ }++ [Fact]+ public void Inverse_SingularTransform_FailsExplicitly()+ {+ var singular = Transform3D.CreateScale(0);++ Assert.False(singular.TryInverse(out _));+ Assert.Throws<InvalidOperationException>(() => singular.Inverse());+ }++ [Fact]+ public void ApplyDirection_ReNormalizesDirection()+ {+ var scale = Transform3D.CreateScale(new Vector3D(2, 1, 1));+ var direction = Direction3D.Create(new Vector3D(1, 1, 0));++ var transformed = scale.Apply(direction);++ Assert.True(double.Abs(transformed.ToVector().Length - 1) < 1e-6);+ }+}diff --git a/Aetheris.Kernel.Core/Math/BoundingBox3D.cs b/Aetheris.Kernel.Core/Math/BoundingBox3D.csnew file mode 100644index 00000000..3ebdbb9a--- /dev/null+++ b/Aetheris.Kernel.Core/Math/BoundingBox3D.cs@@ -0,0 +1,65 @@+using Aetheris.Kernel.Core.Numerics;++namespace Aetheris.Kernel.Core.Math;++public readonly record struct BoundingBox3D+{+ public BoundingBox3D(Point3D min, Point3D max)+ {+ var context = ToleranceContext.Default;+ if (!ToleranceMath.LessThanOrAlmostEqual(min.X, max.X, context) ||+ !ToleranceMath.LessThanOrAlmostEqual(min.Y, max.Y, context) ||+ !ToleranceMath.LessThanOrAlmostEqual(min.Z, max.Z, context))+ {+ throw new ArgumentOutOfRangeException(nameof(min), "Min point must be less than or equal to max point on each axis.");+ }++ Min = min;+ Max = max;+ }++ public Point3D Min { get; }++ public Point3D Max { get; }++ public bool Contains(Point3D point)+ {+ var context = ToleranceContext.Default;+ return ToleranceMath.GreaterThanOrAlmostEqual(point.X, Min.X, context)+ && ToleranceMath.LessThanOrAlmostEqual(point.X, Max.X, context)+ && ToleranceMath.GreaterThanOrAlmostEqual(point.Y, Min.Y, context)+ && ToleranceMath.LessThanOrAlmostEqual(point.Y, Max.Y, context)+ && ToleranceMath.GreaterThanOrAlmostEqual(point.Z, Min.Z, context)+ && ToleranceMath.LessThanOrAlmostEqual(point.Z, Max.Z, context);+ }++ public BoundingBox3D Expand(Point3D point)+ {+ var min = new Point3D(+ double.Min(Min.X, point.X),+ double.Min(Min.Y, point.Y),+ double.Min(Min.Z, point.Z));++ var max = new Point3D(+ double.Max(Max.X, point.X),+ double.Max(Max.Y, point.Y),+ double.Max(Max.Z, point.Z));++ return new BoundingBox3D(min, max);+ }++ public BoundingBox3D Union(BoundingBox3D other)+ {+ var min = new Point3D(+ double.Min(Min.X, other.Min.X),+ double.Min(Min.Y, other.Min.Y),+ double.Min(Min.Z, other.Min.Z));++ var max = new Point3D(+ double.Max(Max.X, other.Max.X),+ double.Max(Max.Y, other.Max.Y),+ double.Max(Max.Z, other.Max.Z));++ return new BoundingBox3D(min, max);+ }+}diff --git a/Aetheris.Kernel.Core/Math/Direction3D.cs b/Aetheris.Kernel.Core/Math/Direction3D.csnew file mode 100644index 00000000..fbfddb65--- /dev/null+++ b/Aetheris.Kernel.Core/Math/Direction3D.cs@@ -0,0 +1,43 @@+using Aetheris.Kernel.Core.Numerics;++namespace Aetheris.Kernel.Core.Math;++public readonly record struct Direction3D+{+ private Direction3D(double x, double y, double z)+ {+ X = x;+ Y = y;+ Z = z;+ }++ public double X { get; }++ public double Y { get; }++ public double Z { get; }++ public Vector3D ToVector() => new(X, Y, Z);++ public static Direction3D Create(Vector3D vector, ToleranceContext? toleranceContext = null)+ {+ if (!TryCreate(vector, out var direction, toleranceContext))+ {+ throw new ArgumentOutOfRangeException(nameof(vector), "Direction requires a non-zero vector within tolerance.");+ }++ return direction;+ }++ public static bool TryCreate(Vector3D vector, out Direction3D direction, ToleranceContext? toleranceContext = null)+ {+ if (!vector.TryNormalize(out var normalized, toleranceContext))+ {+ direction = default;+ return false;+ }++ direction = new Direction3D(normalized.X, normalized.Y, normalized.Z);+ return true;+ }+}diff --git a/Aetheris.Kernel.Core/Math/Point3D.cs b/Aetheris.Kernel.Core/Math/Point3D.csnew file mode 100644index 00000000..848d5783--- /dev/null+++ b/Aetheris.Kernel.Core/Math/Point3D.cs@@ -0,0 +1,15 @@+namespace Aetheris.Kernel.Core.Math;++public readonly record struct Point3D(double X, double Y, double Z)+{+ public static Point3D Origin => new(0d, 0d, 0d);++ public static Point3D operator +(Point3D point, Vector3D vector) =>+ new(point.X + vector.X, point.Y + vector.Y, point.Z + vector.Z);++ public static Point3D operator -(Point3D point, Vector3D vector) =>+ new(point.X - vector.X, point.Y - vector.Y, point.Z - vector.Z);++ public static Vector3D operator -(Point3D left, Point3D right) =>+ new(left.X - right.X, left.Y - right.Y, left.Z - right.Z);+}diff --git a/Aetheris.Kernel.Core/Math/Ray3D.cs b/Aetheris.Kernel.Core/Math/Ray3D.csnew file mode 100644index 00000000..ae938017--- /dev/null+++ b/Aetheris.Kernel.Core/Math/Ray3D.cs@@ -0,0 +1,6 @@+namespace Aetheris.Kernel.Core.Math;++public readonly record struct Ray3D(Point3D Origin, Direction3D Direction)+{+ public Point3D PointAt(double parameter) => Origin + (Direction.ToVector() * parameter);+}diff --git a/Aetheris.Kernel.Core/Math/Transform3D.cs b/Aetheris.Kernel.Core/Math/Transform3D.csnew file mode 100644index 00000000..2f22f474--- /dev/null+++ b/Aetheris.Kernel.Core/Math/Transform3D.cs@@ -0,0 +1,80 @@+using System.Numerics;+using Aetheris.Kernel.Core.Numerics;++namespace Aetheris.Kernel.Core.Math;++public readonly struct Transform3D+{+ private readonly Matrix4x4 _matrix;++ private Transform3D(Matrix4x4 matrix)+ {+ _matrix = matrix;+ }++ public static Transform3D Identity { get; } = new(Matrix4x4.Identity);++ public static Transform3D CreateTranslation(Vector3D translation) =>+ new(Matrix4x4.CreateTranslation((float)translation.X, (float)translation.Y, (float)translation.Z));++ public static Transform3D CreateScale(double uniformScale) =>+ new(Matrix4x4.CreateScale((float)uniformScale));++ public static Transform3D CreateScale(Vector3D scale) =>+ new(Matrix4x4.CreateScale((float)scale.X, (float)scale.Y, (float)scale.Z));++ public static Transform3D CreateRotationX(double radians) => new(Matrix4x4.CreateRotationX((float)radians));++ public static Transform3D CreateRotationY(double radians) => new(Matrix4x4.CreateRotationY((float)radians));++ public static Transform3D CreateRotationZ(double radians) => new(Matrix4x4.CreateRotationZ((float)radians));++ public static Transform3D operator *(Transform3D left, Transform3D right) =>+ new(Matrix4x4.Multiply(left._matrix, right._matrix));++ public static Transform3D Compose(Transform3D first, Transform3D second) => first * second;++ public bool TryInverse(out Transform3D inverse)+ {+ var success = Matrix4x4.Invert(_matrix, out var inverseMatrix);+ inverse = success ? new Transform3D(inverseMatrix) : default;+ return success;+ }++ public Transform3D Inverse()+ {+ if (!TryInverse(out var inverse))+ {+ throw new InvalidOperationException("Transform is singular and cannot be inverted.");+ }++ return inverse;+ }++ public Point3D Apply(Point3D point)+ {+ var transformed = Vector3.Transform(ToNumerics(point), _matrix);+ return new Point3D(transformed.X, transformed.Y, transformed.Z);+ }++ public Vector3D Apply(Vector3D vector)+ {+ var transformed = Vector3.TransformNormal(ToNumerics(vector), _matrix);+ return new Vector3D(transformed.X, transformed.Y, transformed.Z);+ }++ public Direction3D Apply(Direction3D direction, ToleranceContext? toleranceContext = null)+ {+ var transformed = Apply(direction.ToVector());+ if (!Direction3D.TryCreate(transformed, out var transformedDirection, toleranceContext))+ {+ throw new InvalidOperationException("Transform collapses direction to near-zero length.");+ }++ return transformedDirection;+ }++ private static Vector3 ToNumerics(Point3D point) => new((float)point.X, (float)point.Y, (float)point.Z);++ private static Vector3 ToNumerics(Vector3D vector) => new((float)vector.X, (float)vector.Y, (float)vector.Z);+}diff --git a/Aetheris.Kernel.Core/Math/Vector3D.cs b/Aetheris.Kernel.Core/Math/Vector3D.csnew file mode 100644index 00000000..3a96157c--- /dev/null+++ b/Aetheris.Kernel.Core/Math/Vector3D.cs@@ -0,0 +1,58 @@+using Aetheris.Kernel.Core.Numerics;++namespace Aetheris.Kernel.Core.Math;++public readonly record struct Vector3D(double X, double Y, double Z)+{+ public static Vector3D Zero => new(0d, 0d, 0d);++ public double LengthSquared => (X * X) + (Y * Y) + (Z * Z);++ public double Length => double.Sqrt(LengthSquared);++ public static Vector3D operator +(Vector3D left, Vector3D right) =>+ new(left.X + right.X, left.Y + right.Y, left.Z + right.Z);++ public static Vector3D operator -(Vector3D left, Vector3D right) =>+ new(left.X - right.X, left.Y - right.Y, left.Z - right.Z);++ public static Vector3D operator -(Vector3D vector) =>+ new(-vector.X, -vector.Y, -vector.Z);++ public static Vector3D operator *(Vector3D vector, double scalar) =>+ new(vector.X * scalar, vector.Y * scalar, vector.Z * scalar);++ public static Vector3D operator *(double scalar, Vector3D vector) => vector * scalar;++ public static Vector3D operator /(Vector3D vector, double scalar)+ {+ if (ToleranceMath.AlmostZero(scalar, ToleranceContext.Default))+ {+ throw new DivideByZeroException("Cannot divide vector by a value that is approximately zero.");+ }++ return new Vector3D(vector.X / scalar, vector.Y / scalar, vector.Z / scalar);+ }++ public double Dot(Vector3D other) => (X * other.X) + (Y * other.Y) + (Z * other.Z);++ public Vector3D Cross(Vector3D other) =>+ new(+ (Y * other.Z) - (Z * other.Y),+ (Z * other.X) - (X * other.Z),+ (X * other.Y) - (Y * other.X));++ public bool TryNormalize(out Vector3D normalized, ToleranceContext? toleranceContext = null)+ {+ var context = toleranceContext ?? ToleranceContext.Default;+ var length = Length;+ if (ToleranceMath.AlmostZero(length, context))+ {+ normalized = Zero;+ return false;+ }++ normalized = this / length;+ return true;+ }+}diff --git a/docs/architecture-overview.md b/docs/architecture-overview.mdindex 503ea36d..fd85aed1 100644--- a/docs/architecture-overview.md+++ b/docs/architecture-overview.md@@ -4,7 +4,7 @@ This package map is directional only and may evolve with milestone outcomes.## Proposed package map-- `Aetheris.Kernel.Core` — shared kernel primitives, result/diagnostic contracts, and numeric policy entry points.+- `Aetheris.Kernel.Core` — shared kernel primitives, result/diagnostic contracts, numeric policy entry points, and kernel-internal math/transform substrate (not a public CAD math API promise).- `Aetheris.Kernel.Modeling` — solid/B-rep modeling operations and topology-aware workflows.- `Aetheris.Kernel.Tessellation` — mesh generation derived from authoritative B-rep.- `Aetheris.Kernel.Step242` — AP242 mapping and translation at persistence/interchange boundaries.diff --git a/docs/milestones.md b/docs/milestones.mdindex 36b12e26..e56aaaa3 100644--- a/docs/milestones.md+++ b/docs/milestones.md@@ -19,3 +19,11 @@- [ ] Introduce centralized tolerance primitives and policy-backed defaults.- [ ] Add foundational numeric utility types required for later geometric operations.- [ ] Add focused tests that enforce tolerance policy behavior.+++## M03 — Core Math Primitives + Affine Transforms++- [x] Add immutable 3D primitives (`Point3D`, `Vector3D`, `Direction3D`) with explicit semantics and normalization checks.+- [x] Add minimal affine `Transform3D` wrapper with point/vector/direction application, composition, and inverse behavior.+- [x] Add minimal spatial substrate (`BoundingBox3D`, `Ray3D`) for later query/intersection work.+- [x] Add focused unit tests for primitive math invariants and transform behavior.