Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

M19 extract reusable UV trim mask for B-spline scaffold lane

Yuechen Li59899b4a2578
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242BsplineUvGridScaffoldBuilderTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242BsplineUvGridScaffoldBuilderTests.cs
index 0d75b8bd..c44031ac 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242BsplineUvGridScaffoldBuilderTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242BsplineUvGridScaffoldBuilderTests.cs
@@ -71,7 +71,7 @@ public sealed class Step242BsplineUvGridScaffoldBuilderTests
var surface = GetBsplineSurface(body, face.Id);
var referencePatch = GetReferencePatch(body, face.Id);
- var trimMask = new BsplineUvGridTrimMask(outerLoopUv, [innerHoleUv]);
+ var trimMask = new UvTrimMask(outerLoopUv, [innerHoleUv]);
var result = Builder.Build(
surface,
diff --git a/Aetheris.Kernel.Core.Tests/Step242/UvTrimMaskTests.cs b/Aetheris.Kernel.Core.Tests/Step242/UvTrimMaskTests.cs
new file mode 100644
index 00000000..2f41ca2a
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Step242/UvTrimMaskTests.cs
@@ -0,0 +1,93 @@
+using Aetheris.Kernel.Core.Brep.Tessellation;
+
+namespace Aetheris.Kernel.Core.Tests.Step242;
+
+public sealed class UvTrimMaskTests
+{
+ [Fact]
+ public void Contains_OuterLoopOnly_AcceptsInsideAndRejectsOutside()
+ {
+ var mask = new UvTrimMask(
+ outerLoop:
+ [
+ new UvPoint(0d, 0d),
+ new UvPoint(1d, 0d),
+ new UvPoint(1d, 1d),
+ new UvPoint(0d, 1d),
+ ],
+ innerLoops: []);
+
+ Assert.True(mask.Contains(new UvPoint(0.5d, 0.5d)));
+ Assert.False(mask.Contains(new UvPoint(1.2d, 0.5d)));
+ }
+
+ [Fact]
+ public void Contains_OuterPlusHole_RejectsHoleAndAcceptsRing()
+ {
+ var mask = new UvTrimMask(
+ outerLoop:
+ [
+ new UvPoint(0d, 0d),
+ new UvPoint(1d, 0d),
+ new UvPoint(1d, 1d),
+ new UvPoint(0d, 1d),
+ ],
+ innerLoops:
+ [
+ [
+ new UvPoint(0.3d, 0.3d),
+ new UvPoint(0.7d, 0.3d),
+ new UvPoint(0.7d, 0.7d),
+ new UvPoint(0.3d, 0.7d),
+ ],
+ ]);
+
+ Assert.False(mask.Contains(new UvPoint(0.5d, 0.5d)));
+ Assert.True(mask.Contains(new UvPoint(0.2d, 0.5d)));
+ }
+
+ [Fact]
+ public void Determinism_RepeatedConstructionAndQueries_AreEquivalent()
+ {
+ IReadOnlyList<UvPoint> outer =
+ [
+ new(0d, 0d),
+ new(1d, 0d),
+ new(1d, 1d),
+ new(0d, 1d),
+ ];
+ IReadOnlyList<UvPoint> hole =
+ [
+ new(0.35d, 0.35d),
+ new(0.65d, 0.35d),
+ new(0.65d, 0.65d),
+ new(0.35d, 0.65d),
+ ];
+
+ var first = new UvTrimMask(outer, [hole]);
+ var second = new UvTrimMask(outer, [hole]);
+ UvPoint[] probes =
+ [
+ new(0.1d, 0.1d),
+ new(0.5d, 0.5d),
+ new(0.9d, 0.9d),
+ new(1.2d, 0.5d),
+ new(0.35d, 0.5d),
+ ];
+
+ var firstResults = probes.Select(first.Contains).ToArray();
+ var secondResults = probes.Select(second.Contains).ToArray();
+
+ Assert.Equal(firstResults, secondResults);
+ Assert.True(first.KeepCellByCorners(
+ new UvPoint(0d, 0d),
+ new UvPoint(0.2d, 0d),
+ new UvPoint(0d, 0.2d),
+ new UvPoint(0.2d, 0.2d)));
+ Assert.False(first.KeepCellByCorners(
+ new UvPoint(0.4d, 0.4d),
+ new UvPoint(0.6d, 0.4d),
+ new UvPoint(0.4d, 0.6d),
+ new UvPoint(0.6d, 0.6d)));
+ }
+}
diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/BsplineUvGridScaffoldBuilder.cs b/Aetheris.Kernel.Core/Brep/Tessellation/BsplineUvGridScaffoldBuilder.cs
index 5d0a9969..861d9c38 100644
--- a/Aetheris.Kernel.Core/Brep/Tessellation/BsplineUvGridScaffoldBuilder.cs
+++ b/Aetheris.Kernel.Core/Brep/Tessellation/BsplineUvGridScaffoldBuilder.cs
@@ -112,10 +112,11 @@ internal sealed class BsplineUvGridScaffoldBuilder
var topRight = GridIndex(uIndex + 1, vIndex + 1, columns);
if (request.TrimMask is not null
- && (!request.TrimMask.Contains(uvPoints[bottomLeft])
- || !request.TrimMask.Contains(uvPoints[bottomRight])
- || !request.TrimMask.Contains(uvPoints[topLeft])
- || !request.TrimMask.Contains(uvPoints[topRight])))
+ && !request.TrimMask.KeepCellByCorners(
+ uvPoints[bottomLeft],
+ uvPoints[bottomRight],
+ uvPoints[topLeft],
+ uvPoints[topRight]))
{
continue;
}
@@ -164,7 +165,7 @@ internal sealed class BsplineUvGridScaffoldBuilder
return (double)degenerate / triangles;
}
- private static int ComputeTrimLeakageCount(BsplineUvGridScaffoldMesh mesh, BsplineUvGridTrimMask? trimMask)
+ private static int ComputeTrimLeakageCount(BsplineUvGridScaffoldMesh mesh, UvTrimMask? trimMask)
{
if (trimMask is null)
{
@@ -187,7 +188,7 @@ internal sealed class BsplineUvGridScaffoldBuilder
return leakage;
}
- private static double ComputeBoundaryDeviation(BsplineUvGridScaffoldMesh mesh, BsplineUvGridTrimMask? trimMask)
+ private static double ComputeBoundaryDeviation(BsplineUvGridScaffoldMesh mesh, UvTrimMask? trimMask)
{
if (trimMask is null)
{
@@ -302,7 +303,7 @@ internal sealed class BsplineUvGridScaffoldBuilder
internal sealed record BsplineUvGridScaffoldBuildRequest(
int USegments,
int VSegments,
- BsplineUvGridTrimMask? TrimMask = null,
+ UvTrimMask? TrimMask = null,
IReadOnlyList<Point3D>? ReferencePositions = null,
int? ReferenceTriangleCount = null,
double MaxDegenerateTriangleRatio = 0.15d,
@@ -358,67 +359,4 @@ internal sealed record BsplineUvGridScaffoldResult(
=> new(BsplineUvGridScaffoldAcceptance.Rejected, mesh, metrics, reason);
}
-internal sealed record BsplineUvGridTrimMask(IReadOnlyList<UvPoint> OuterLoop, IReadOnlyList<IReadOnlyList<UvPoint>> InnerLoops)
-{
- internal bool Contains(UvPoint point)
- {
- if (!PointInLoop(OuterLoop, point))
- {
- return false;
- }
-
- foreach (var hole in InnerLoops)
- {
- if (PointInLoop(hole, point))
- {
- return false;
- }
- }
-
- return true;
- }
-
- private static bool PointInLoop(IReadOnlyList<UvPoint> loop, UvPoint point)
- {
- var inside = false;
- for (var i = 0; i < loop.Count; i++)
- {
- var a = loop[i];
- var b = loop[(i + 1) % loop.Count];
- if (IsPointOnSegment(point, a, b))
- {
- return true;
- }
-
- var crosses = ((a.V > point.V) != (b.V > point.V))
- && (point.U < (((b.U - a.U) * (point.V - a.V)) / (b.V - a.V)) + a.U);
- if (crosses)
- {
- inside = !inside;
- }
- }
-
- return inside;
- }
-
- private static bool IsPointOnSegment(UvPoint point, UvPoint a, UvPoint b)
- {
- const double tolerance = 1e-9d;
- var cross = ((point.U - a.U) * (b.V - a.V)) - ((point.V - a.V) * (b.U - a.U));
- if (double.Abs(cross) > tolerance)
- {
- return false;
- }
-
- var dot = ((point.U - a.U) * (b.U - a.U)) + ((point.V - a.V) * (b.V - a.V));
- if (dot < -tolerance)
- {
- return false;
- }
-
- var lengthSquared = ((b.U - a.U) * (b.U - a.U)) + ((b.V - a.V) * (b.V - a.V));
- return dot <= lengthSquared + tolerance;
- }
-}
-
internal readonly record struct UvPoint(double U, double V);
diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/UvTrimMask.cs b/Aetheris.Kernel.Core/Brep/Tessellation/UvTrimMask.cs
new file mode 100644
index 00000000..49a1228e
--- /dev/null
+++ b/Aetheris.Kernel.Core/Brep/Tessellation/UvTrimMask.cs
@@ -0,0 +1,108 @@
+namespace Aetheris.Kernel.Core.Brep.Tessellation;
+
+internal sealed class UvTrimMask
+{
+ private readonly UvTrimLoop _outerLoop;
+ private readonly UvTrimLoop[] _innerLoops;
+ private readonly IReadOnlyList<IReadOnlyList<UvPoint>> _innerLoopVertices;
+
+ internal UvTrimMask(IReadOnlyList<UvPoint> outerLoop, IReadOnlyList<IReadOnlyList<UvPoint>> innerLoops)
+ {
+ _outerLoop = new UvTrimLoop(outerLoop);
+ _innerLoops = innerLoops.Select(loop => new UvTrimLoop(loop)).ToArray();
+ _innerLoopVertices = _innerLoops.Select(loop => (IReadOnlyList<UvPoint>)loop.Vertices).ToArray();
+ }
+
+ internal IReadOnlyList<UvPoint> OuterLoop => _outerLoop.Vertices;
+
+ internal IReadOnlyList<IReadOnlyList<UvPoint>> InnerLoops => _innerLoopVertices;
+
+ internal bool Contains(UvPoint point)
+ {
+ if (!_outerLoop.Contains(point))
+ {
+ return false;
+ }
+
+ for (var i = 0; i < _innerLoops.Length; i++)
+ {
+ if (_innerLoops[i].Contains(point))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ internal bool KeepCellByCorners(UvPoint bottomLeft, UvPoint bottomRight, UvPoint topLeft, UvPoint topRight)
+ => Contains(bottomLeft)
+ && Contains(bottomRight)
+ && Contains(topLeft)
+ && Contains(topRight);
+}
+
+internal sealed class UvTrimLoop
+{
+ private const double SegmentTolerance = 1e-9d;
+
+ private readonly UvPoint[] _vertices;
+
+ internal UvTrimLoop(IReadOnlyList<UvPoint> vertices)
+ {
+ if (vertices is null)
+ {
+ throw new ArgumentNullException(nameof(vertices));
+ }
+
+ if (vertices.Count < 3)
+ {
+ throw new ArgumentException("UV trim loops require at least three vertices.", nameof(vertices));
+ }
+
+ _vertices = vertices.ToArray();
+ }
+
+ internal IReadOnlyList<UvPoint> Vertices => _vertices;
+
+ internal bool Contains(UvPoint point)
+ {
+ var inside = false;
+ for (var i = 0; i < _vertices.Length; i++)
+ {
+ var a = _vertices[i];
+ var b = _vertices[(i + 1) % _vertices.Length];
+ if (IsPointOnSegment(point, a, b))
+ {
+ return true;
+ }
+
+ var crosses = ((a.V > point.V) != (b.V > point.V))
+ && (point.U < (((b.U - a.U) * (point.V - a.V)) / (b.V - a.V)) + a.U);
+ if (crosses)
+ {
+ inside = !inside;
+ }
+ }
+
+ return inside;
+ }
+
+ private static bool IsPointOnSegment(UvPoint point, UvPoint a, UvPoint b)
+ {
+ var cross = ((point.U - a.U) * (b.V - a.V)) - ((point.V - a.V) * (b.U - a.U));
+ if (double.Abs(cross) > SegmentTolerance)
+ {
+ return false;
+ }
+
+ var dot = ((point.U - a.U) * (b.U - a.U)) + ((point.V - a.V) * (b.V - a.V));
+ if (dot < -SegmentTolerance)
+ {
+ return false;
+ }
+
+ var lengthSquared = ((b.U - a.U) * (b.U - a.U)) + ((b.V - a.V) * (b.V - a.V));
+ return dot <= lengthSquared + SegmentTolerance;
+ }
+}