diff --git a/src/GameCult.Geometry/CultGeometrySurfaceNets.cs b/src/GameCult.Geometry/CultGeometrySurfaceNets.cs index 22c47df7..45c3f1ec 100644 --- a/src/GameCult.Geometry/CultGeometrySurfaceNets.cs +++ b/src/GameCult.Geometry/CultGeometrySurfaceNets.cs @@ -29,6 +29,18 @@ namespace GameCult.Geometry /// Extracts the surface with surface nets. /// Values less than or equal to the isovalue are treated as inside. /// + /// is null. + /// + /// is too small or has a non-finite sample, or + /// or a component of is non-finite. + /// + /// is non-positive or infinite. + /// + /// Non-finite results from otherwise-finite inputs (for example an extreme + /// or samples near float's magnitude limit producing an + /// infinite vertex position) are a float-domain overflow, not a validated input error; + /// this method does not range-check magnitudes for that case. + /// public static CultGeometryQuadMesh Extract( float[,,] samples, float isoValue = 0f, @@ -49,6 +61,16 @@ namespace GameCult.Geometry } } + if (!float.IsFinite(isoValue)) + { + throw new ArgumentException("The isovalue must be finite.", nameof(isoValue)); + } + + if (!float.IsFinite(origin.X) || !float.IsFinite(origin.Y) || !float.IsFinite(origin.Z)) + { + throw new ArgumentException("The origin must be finite.", nameof(origin)); + } + if (!(cellSize > 0f) || float.IsInfinity(cellSize)) { throw new ArgumentOutOfRangeException(nameof(cellSize), "Cell size must be finite and positive."); diff --git a/src/GameCult.Geometry/SURFACE-NETS-PROVENANCE.md b/src/GameCult.Geometry/SURFACE-NETS-PROVENANCE.md index 3f80cf7c..d161ede3 100644 --- a/src/GameCult.Geometry/SURFACE-NETS-PROVENANCE.md +++ b/src/GameCult.Geometry/SURFACE-NETS-PROVENANCE.md @@ -37,6 +37,10 @@ Deliberate behavior: rule never looks at its geometry; - non-finite `isoValue` or `origin` components are rejected, matching the existing rejection of non-finite samples and cell size; +- an extreme but finite input (a `cellSize` near float's magnitude limit, or + finite samples whose interpolated position overflows to infinity) is a + float-domain overflow, not a validated input error, and is deliberately not + range-checked; - vertices are welded: one vertex per active cell, shared by every quad touching that cell; - face-weighted normals sum each quad's diagonal cross product (unnormalized, diff --git a/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs b/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs index feb250ba..3e3b3892 100644 --- a/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs +++ b/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs @@ -626,6 +626,36 @@ namespace GameCult.Geometry.Tests act.Should().Throw(); } + [TestCase(float.NegativeInfinity)] + [TestCase(float.PositiveInfinity)] + [TestCase(float.NaN)] + public void A_non_finite_isoValue_is_rejected(float nonFiniteValue) + { + var samples = new float[2, 2, 2]; + + Action act = () => CultGeometrySurfaceNets.Extract(samples, isoValue: nonFiniteValue); + + act.Should().Throw(); + } + + [TestCase(float.NegativeInfinity, 0f, 0f)] + [TestCase(float.PositiveInfinity, 0f, 0f)] + [TestCase(float.NaN, 0f, 0f)] + [TestCase(0f, float.NegativeInfinity, 0f)] + [TestCase(0f, float.PositiveInfinity, 0f)] + [TestCase(0f, float.NaN, 0f)] + [TestCase(0f, 0f, float.NegativeInfinity)] + [TestCase(0f, 0f, float.PositiveInfinity)] + [TestCase(0f, 0f, float.NaN)] + public void A_non_finite_origin_component_is_rejected(float x, float y, float z) + { + var samples = new float[2, 2, 2]; + + Action act = () => CultGeometrySurfaceNets.Extract(samples, origin: new CultVec3(x, y, z)); + + act.Should().Throw(); + } + [TestCase(float.NegativeInfinity)] [TestCase(float.PositiveInfinity)] [TestCase(float.NaN)]