import simd /// Pairs of mesh triangles in different FOLD faces that pass through each other: the segment where /// their planes meet crosses the inside of both, its midpoint deeper than `depth` inside each. /// Coplanar pairs (normals within 1e-6 rad, planes within 1e-6 of the sheet's size) are the layer /// order's and skipped, as FoldKit's check does. Triangles sharing a vertex are skipped. func meshCollisions(_ x: [V3], _ mesh: Mesh, size: Double, depth: Double, among subset: Set? = nil) -> [(Int, Int)] { thickness = thicknessFraction * size let tris = mesh.triangles let candidates = subset.map { Array($0).sorted() } ?? Array(tris.indices) let normals = tris.map { t in normalOf(x[t.x], x[t.y], x[t.z]) } let boxes = tris.map { t -> (V3, V3) in let p = [x[t.x], x[t.y], x[t.z]] return (simd_min(simd_min(p[0], p[1]), p[2]), simd_max(simd_max(p[0], p[1]), p[2])) } let order = candidates.filter { normals[$0] != .zero }.sorted { boxes[$0].0.x < boxes[$1].0.x } var found: [(Int, Int)] = [] let eps = size * 1e-9 for (n, a) in order.enumerated() { let ta = tris[a] for b in order[(n + 1)...] { if boxes[b].0.x > boxes[a].1.x + eps { break } guard mesh.faceOf[a] != mesh.faceOf[b], all(boxes[a].0 .<= boxes[b].1 + eps), all(boxes[b].0 .<= boxes[a].1 + eps) else { continue } let tb = tris[b] let va = [ta.x, ta.y, ta.z], vb = [tb.x, tb.y, tb.z] if va.contains(where: vb.contains) { continue } let pa = va.map { x[$0] }, pb = vb.map { x[$0] } if length(cross(normals[a], normals[b])) <= 1e-6, abs(dot(normals[a], pb[0] - pa[0])) <= 1e-6 * size { continue } if passThrough(pa, normals[a], pb, normals[b], depth: depth) { found.append((a, b)) } } } return found } func normalOf(_ a: V3, _ b: V3, _ c: V3) -> V3 { let n = cross(b - a, c - a) let l = length(n) return l > 0 ? n / l : .zero } private func passThrough(_ a: [V3], _ na: V3, _ b: [V3], _ nb: V3, depth: Double) -> Bool { let along = cross(na, nb) let squared = dot(along, along) guard squared > 1e-24 else { return false } let d = along / sqrt(squared) let p = (dot(na, a[0]) * cross(nb, along) + dot(nb, b[0]) * cross(along, na)) / squared func inward(_ poly: [V3], _ n: V3) -> [(V3, V3)] { (0..<3).map { i in let c = poly[i], e = poly[(i + 1) % 3] - c return (c, cross(n, e) / length(e)) } } let ea = inward(a, na), eb = inward(b, nb) func clip(_ edges: [(V3, V3)]) -> (Double, Double)? { var lo = -Double.infinity, hi = Double.infinity for (c, m) in edges { let dist = dot(m, p - c), rate = dot(m, d) if abs(rate) < 1e-15 { if dist < 0 { return nil }; continue } let s = -dist / rate if rate > 0 { lo = max(lo, s) } else { hi = min(hi, s) } } return lo <= hi ? (lo, hi) : nil } guard let ia = clip(ea), let ib = clip(eb) else { return false } let lo = max(ia.0, ib.0), hi = min(ia.1, ib.1) guard hi - lo > 2 * depth else { return false } let mid = p + d * ((lo + hi) / 2) guard [ea, eb].allSatisfy({ edges in edges.allSatisfy { dot($0.1, mid - $0.0) > depth } }) else { return false } // how far each pokes through the other's plane: layers of one stack that cross by less than // the paper's thickness are contact, not interpenetration func poke(_ t: [V3], _ n: V3, _ o: V3) -> Double { let s = t.map { dot($0 - o, n) } return min(max(0, s.max()!), max(0, -s.min()!)) } return min(poke(a, nb, b[0]), poke(b, na, a[0])) > thickness } nonisolated(unsafe) var thicknessFraction = 1e-3 /// Paper thickness for the interpenetration test, as a fraction of the sheet's size (0.1 mm on a /// 15 cm square is 6.7e-4). nonisolated(unsafe) var thickness = 0.0