diff --git a/Sources/Manifold3D/MeshGL.swift b/Sources/Manifold3D/MeshGL.swift index 41cfe00..358f350 100644 --- a/Sources/Manifold3D/MeshGL.swift +++ b/Sources/Manifold3D/MeshGL.swift @@ -26,6 +26,27 @@ public struct MeshGL { } } +public extension MeshGL { + /// Returns a copy whose merge vectors are recomputed so that topologically identical but + /// separated vertices are reconnected. + /// + /// This is a best-effort helper for the case where a manifold `MeshGL` was produced but its + /// merge vectors were lost through a round-trip via a file format (e.g. STL) that does not + /// preserve them. Manifoldness is judged by shared vertex *indices*, not by position, so a + /// surface that is closed geometrically but carries split coincident vertices is reported as + /// non-manifold — constructing a ``Manifold`` from it throws. Merging snaps those coincident + /// vertices back together so the mesh is accepted. + /// + /// When nothing needs merging, the result is equivalent to the original mesh. + /// + /// - Returns: A merged copy of the mesh. + func merged() -> MeshGL { + var meshGL = self.meshGL + _ = meshGL.Merge() + return MeshGL(meshGL: meshGL) + } +} + public extension MeshGL { /// The triangles of this mesh. var triangles: [Triangle] { diff --git a/Tests/Manifold3DTests/MeshGLMergeTests.swift b/Tests/Manifold3DTests/MeshGLMergeTests.swift new file mode 100644 index 0000000..de32a57 --- /dev/null +++ b/Tests/Manifold3DTests/MeshGLMergeTests.swift @@ -0,0 +1,39 @@ +import Testing +@testable import Manifold3D + +// A closed tetrahedron whose corners are NOT shared by index: every face carries its own three +// vertices (12 positions, 4 distinct corners). This is the shape a file-format round-trip (e.g. STL) +// produces — geometrically closed, but non-manifold by index because no edge has a twin. +private func splitTetrahedron() -> MeshGL { + let a = TestVec3(x: 0, y: 0, z: 0) + let b = TestVec3(x: 1, y: 0, z: 0) + let c = TestVec3(x: 0, y: 1, z: 0) + let d = TestVec3(x: 0, y: 0, z: 1) + return MeshGL( + vertices: [a, c, b, a, b, d, a, d, c, b, c, d], + triangles: [Triangle(0, 1, 2), Triangle(3, 4, 5), Triangle(6, 7, 8), Triangle(9, 10, 11)] + ) +} + +@Test func `merged reconnects split vertices so a closed mesh is accepted`() throws { + let split = splitTetrahedron() + + // Without merging the surface is closed but non-manifold by index, so construction fails. + let unmerged: TestManifold? = try? Manifold(split) + #expect(unmerged == nil) + + // After merging, the coincident corners are reconnected and the mesh is accepted. + let solid: TestManifold = try Manifold(split.merged()) + TestSupport.expectNoStatusError(solid) + #expect(!solid.isEmpty) + #expect(abs(solid.volume - 1.0 / 6.0) <= 1e-9) +} + +@Test func `merged is a no-op for an already-connected mesh`() throws { + let cube: TestManifold = .cube(size: TestVec3(x: 1, y: 1, z: 1), center: true) + let mesh = cube.meshGL() + + let remerged: TestManifold = try Manifold(mesh.merged()) + TestSupport.expectNoStatusError(remerged) + #expect(abs(remerged.volume - cube.volume) <= 1e-9) +}