Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sources/Manifold3D/MeshGL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ public struct MeshGL<Vector: Vector3> {
}
}

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] {
Expand Down
39 changes: 39 additions & 0 deletions Tests/Manifold3DTests/MeshGLMergeTests.swift
Original file line number Diff line number Diff line change
@@ -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<TestVec3> {
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<TestVec3>(
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)
}
Loading