diff --git a/tests/GameCult.Geometry.Tests/QuadNormalsTests.cs b/tests/GameCult.Geometry.Tests/QuadNormalsTests.cs index e7c2489b..448c699d 100644 --- a/tests/GameCult.Geometry.Tests/QuadNormalsTests.cs +++ b/tests/GameCult.Geometry.Tests/QuadNormalsTests.cs @@ -11,20 +11,22 @@ namespace GameCult.Geometry.Tests public void A_sliver_sharing_a_vertex_does_not_move_the_large_quads_normal() { // A 10x10 quad in the XY plane (facing +Z) sharing vertex 0 with a near-degenerate - // sliver tilted into the XZ plane. The sliver's area is ~1e6 times smaller. + // sliver tilted into the XZ plane. The sliver's area is ~1e6 times smaller. Offset away + // from the origin so a diagonal computed as (p2 + p0) instead of (p2 - p0) is not + // coincidentally the same value (p0 = 0 would erase that distinction). var mesh = new CultGeometryQuadMesh { Positions = new[] { // Large quad: 0,1,2,3 - 0f, 0f, 0f, - 10f, 0f, 0f, - 10f, 10f, 0f, - 0f, 10f, 0f, + 5f, 7f, 9f, + 15f, 7f, 9f, + 15f, 17f, 9f, + 5f, 17f, 9f, // Sliver quad: 0 (shared), 4, 5, 6 - 0.001f, 0f, 0f, - 0.001f, 0f, 0.001f, - 0f, 0f, 0.001f, + 5.001f, 7f, 9f, + 5.001f, 7f, 9.001f, + 5f, 7f, 9.001f, }, Quads = new[] { @@ -46,22 +48,33 @@ namespace GameCult.Geometry.Tests } [Test] - public void A_closed_cube_gets_unit_length_normals() + public void A_closed_cube_gets_unit_length_normals_pointing_away_from_center() { var mesh = Cube(); var normals = CultGeometryQuadNormals.FaceWeighted(mesh); + // Each vertex's normal must point away from the cube's center (0.5,0.5,0.5) along every + // axis, i.e. its sign must match the vertex's own offset from the center. Checking the + // exact direction (not just the length) catches a mutant that flips the sign of one of a + // vertex's three contributing faces: three orthogonal +-2-length vectors summed have the + // same length no matter which one is negated, so a length-only check cannot tell them apart. + var oneOverRoot3 = 1f / MathF.Sqrt(3f); for (var vertex = 0; vertex < mesh.VertexCount; vertex++) { - var x = normals[vertex * 3]; - var y = normals[(vertex * 3) + 1]; - var z = normals[(vertex * 3) + 2]; - var length = MathF.Sqrt((x * x) + (y * y) + (z * z)); - length.Should().BeApproximately(1f, 1e-5f); - float.IsFinite(x).Should().BeTrue(); - float.IsFinite(y).Should().BeTrue(); - float.IsFinite(z).Should().BeTrue(); + var px = mesh.Positions[vertex * 3]; + var py = mesh.Positions[(vertex * 3) + 1]; + var pz = mesh.Positions[(vertex * 3) + 2]; + var expected = new[] + { + px > 0.5f ? oneOverRoot3 : -oneOverRoot3, + py > 0.5f ? oneOverRoot3 : -oneOverRoot3, + pz > 0.5f ? oneOverRoot3 : -oneOverRoot3, + }; + + normals[vertex * 3].Should().BeApproximately(expected[0], 1e-5f); + normals[(vertex * 3) + 1].Should().BeApproximately(expected[1], 1e-5f); + normals[(vertex * 3) + 2].Should().BeApproximately(expected[2], 1e-5f); } } diff --git a/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs b/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs index fe550146..770a19be 100644 --- a/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs +++ b/tests/GameCult.Geometry.Tests/SurfaceNetsTests.cs @@ -139,22 +139,23 @@ namespace GameCult.Geometry.Tests [Test] public void Vertex_placement_applies_origin_and_cell_size() { + // The inside corner is (1,1,1), not (0,0,0): every crossing edge's start sample has at + // least one non-zero coordinate, so a cellSize applied by division instead of + // multiplication (or dropped altogether) changes the result instead of coincidentally + // matching it (a zero component erases that distinction either way). var samples = new float[2, 2, 2]; - samples[0, 0, 0] = -1f; - samples[1, 0, 0] = 1f; - samples[0, 1, 0] = 1f; - samples[0, 0, 1] = 1f; - samples[1, 1, 0] = 5f; - samples[1, 0, 1] = 5f; - samples[0, 1, 1] = 5f; - samples[1, 1, 1] = 5f; + for (var x = 0; x < 2; x++) + for (var y = 0; y < 2; y++) + for (var z = 0; z < 2; z++) samples[x, y, z] = 9f; + + samples[1, 1, 1] = -1f; var mesh = CultGeometrySurfaceNets.Extract(samples, origin: new CultVec3(10f, 20f, 30f), cellSize: 2f); mesh.VertexCount.Should().Be(1); - mesh.Positions[0].Should().BeApproximately(10f + (1f / 6f * 2f), 1e-5f); - mesh.Positions[1].Should().BeApproximately(20f + (1f / 6f * 2f), 1e-5f); - mesh.Positions[2].Should().BeApproximately(30f + (1f / 6f * 2f), 1e-5f); + mesh.Positions[0].Should().BeApproximately(10f + (2.9f / 3f * 2f), 1e-5f); + mesh.Positions[1].Should().BeApproximately(20f + (2.9f / 3f * 2f), 1e-5f); + mesh.Positions[2].Should().BeApproximately(30f + (2.9f / 3f * 2f), 1e-5f); } [TestCase(6)] @@ -216,6 +217,67 @@ namespace GameCult.Geometry.Tests (edge.Z == 1 || edge.Z == 2)); } + [Test] + public void An_open_plane_along_y_emits_quads_only_where_all_four_surrounding_cells_exist() + { + var samples = new float[4, 4, 4]; + for (var x = 0; x < 4; x++) + for (var y = 0; y < 4; y++) + for (var z = 0; z < 4; z++) + { + samples[x, y, z] = y - 1.5f; + } + + var mesh = CultGeometrySurfaceNets.Extract(samples); + + mesh.QuadCount.Should().Be(4); + mesh.QuadEdges.Should().OnlyContain(edge => + edge.Y == 1 && edge.Axis == 1 && + (edge.X == 1 || edge.X == 2) && + (edge.Z == 1 || edge.Z == 2)); + } + + [Test] + public void An_open_plane_along_z_emits_quads_only_where_all_four_surrounding_cells_exist() + { + var samples = new float[4, 4, 4]; + for (var x = 0; x < 4; x++) + for (var y = 0; y < 4; y++) + for (var z = 0; z < 4; z++) + { + samples[x, y, z] = z - 1.5f; + } + + var mesh = CultGeometrySurfaceNets.Extract(samples); + + mesh.QuadCount.Should().Be(4); + mesh.QuadEdges.Should().OnlyContain(edge => + edge.Z == 1 && edge.Axis == 2 && + (edge.X == 1 || edge.X == 2) && + (edge.Y == 1 || edge.Y == 2)); + } + + [Test] + public void An_extremely_small_edge_delta_falls_back_to_the_edge_midpoint() + { + // Every crossing edge from the inside corner has a delta of ~3e-21, under the 1e-20 + // threshold that falls back to the 0.5 midpoint; computed directly, (0 - -1e-30) / 3e-21 + // would land near the inside endpoint instead (amt ~ 3.3e-10), not the midpoint. + var samples = new float[2, 2, 2]; + for (var x = 0; x < 2; x++) + for (var y = 0; y < 2; y++) + for (var z = 0; z < 2; z++) samples[x, y, z] = 3e-21f; + + samples[0, 0, 0] = -1e-30f; + + var mesh = CultGeometrySurfaceNets.Extract(samples); + + mesh.VertexCount.Should().Be(1); + mesh.Positions[0].Should().BeApproximately(1f / 6f, 1e-4f); + mesh.Positions[1].Should().BeApproximately(1f / 6f, 1e-4f); + mesh.Positions[2].Should().BeApproximately(1f / 6f, 1e-4f); + } + [Test] public void Extraction_is_deterministic_and_quad_edges_are_strictly_ascending() { @@ -234,6 +296,26 @@ namespace GameCult.Geometry.Tests } } + [Test] + public void Grid_edge_equality_requires_every_component_to_match() + { + var edge = new CultGeometryGridEdge(1, 2, 3, 0); + + (edge == new CultGeometryGridEdge(1, 2, 3, 0)).Should().BeTrue(); + (edge == new CultGeometryGridEdge(9, 2, 3, 0)).Should().BeFalse(); + (edge == new CultGeometryGridEdge(1, 9, 3, 0)).Should().BeFalse(); + (edge == new CultGeometryGridEdge(1, 2, 9, 0)).Should().BeFalse(); + (edge == new CultGeometryGridEdge(1, 2, 3, 1)).Should().BeFalse(); + } + + [Test] + public void Grid_edge_to_string_names_its_coordinates_and_axis() + { + var edge = new CultGeometryGridEdge(1, 2, 3, 2); + + edge.ToString().Should().Be("(1, 2, 3)+e2"); + } + [Test] public void Null_field_is_rejected() {