import FoldFormat import Foundation import simd typealias V2 = SIMD2 typealias V3 = SIMD3 /// The directory holding this package's `Data`. let dataDirectory = URL(fileURLWithPath: #filePath) .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() .appendingPathComponent("Data") /// A compiled FOLD file: the crease pattern (frame 0, flat) and each frame's positions. struct Model { var file: FoldFile let flat: [V2] let edgeFaces: [[Int]] init(_ file: FoldFile) { self.file = file flat = file.frames[0].verticesCoords.map { V2($0.x, $0.y) } edgeFaces = edgesFaces(file) } init(named name: String) throws { self.init(try FoldFile(contentsOf: dataDirectory.appendingPathComponent(name))) } var faces: [[Int]] { file.facesVertices } var edges: [[Int]] { file.edgesVertices } func positions(_ k: Int) -> [V3] { file.frames[k].verticesCoords } var size: Double { let lo = flat.reduce(flat[0], simd_min), hi = flat.reduce(flat[0], simd_max) return (hi - lo).max() } /// The face whose crease-pattern polygon contains `p`. func face(containing p: V2) -> Int? { faces.indices.first { f in contains(faces[f].map { flat[$0] }, p, tolerance: 1e-12) } } func centroid(_ f: Int) -> V2 { faces[f].map { flat[$0] }.reduce(.zero, +) / Double(faces[f].count) } /// The image of crease-pattern point `p` of face `f` when the FOLD vertices are at `x`. func place(_ p: V2, face f: Int, _ x: [V3]) -> V3 { let c = widest(faces[f].map { flat[$0] }).map { faces[f][$0] } let u = flat[c[1]] - flat[c[0]], v = flat[c[2]] - flat[c[0]], w = p - flat[c[0]] let det = u.x * v.y - u.y * v.x let a = (w.x * v.y - w.y * v.x) / det, b = (u.x * w.y - u.y * w.x) / det return x[c[0]] + a * (x[c[1]] - x[c[0]]) + b * (x[c[2]] - x[c[0]]) } func normals(_ x: [V3]) -> [V3] { faces.map { polygonNormal($0.map { x[$0] }) } } /// The faces connected to `seed` across interior edges not in `cut`. func component(from seeds: [Int], cut: Set, within allowed: Set? = nil) -> Set { var seen = Set(seeds) var stack = seeds while let f = stack.popLast() { for v in faces[f].indices { let a = faces[f][v], b = faces[f][(v + 1) % faces[f].count] guard let e = edgeIndex[SIMD2(min(a, b), max(a, b))], !cut.contains(e), edgeFaces[e].count == 2 else { continue } let g = edgeFaces[e][0] == f ? edgeFaces[e][1] : edgeFaces[e][0] if let allowed, !allowed.contains(g) { continue } if seen.insert(g).inserted { stack.append(g) } } } return seen } var edgeIndex: [SIMD2: Int] { Dictionary(uniqueKeysWithValues: edges.enumerated().map { (SIMD2(min($1[0], $1[1]), max($1[0], $1[1])), $0) }) } } func polygonNormal(_ p: [V3]) -> V3 { var n = V3.zero for k in p.indices { n += cross(p[k], p[(k + 1) % p.count]) } let l = length(n) return l > 0 ? n / l : .zero } func contains(_ polygon: [V2], _ p: V2, tolerance: Double) -> Bool { let n = polygon.count let s = (0.. Double in let a = polygon[k], b = polygon[(k + 1) % n] return (b - a).x * (p - a).y - (b - a).y * (p - a).x } return s.allSatisfy { $0 >= -tolerance } || s.allSatisfy { $0 <= tolerance } } /// The three corners spanning the largest triangle. func widest(_ corners: [V2]) -> [Int] { var best = [0, 1, 2], largest = -1.0 for i in corners.indices { for j in corners.indices where j > i { for k in corners.indices where k > j { let u = corners[j] - corners[i], v = corners[k] - corners[i] let area = abs(u.x * v.y - u.y * v.x) if area > largest { (best, largest) = ([i, j, k], area) } } } } return best } func cross2(_ a: V2, _ b: V2) -> Double { a.x * b.y - a.y * b.x } /// Rotation by `degrees` about the line through `origin` along `axis`. func rotate(_ p: V3, about origin: V3, axis: V3, degrees: Double) -> V3 { origin + simd_quatd(angle: degrees * .pi / 180, axis: normalize(axis)).act(p - origin) } /// Reflection of `p` in the plane through `origin` with unit `normal`. func reflect(_ p: V3, origin: V3, normal: V3) -> V3 { p - 2 * dot(p - origin, normal) * normal } func pct(_ x: Double) -> String { String(format: "%.2f%%", 100 * x) } func sci(_ x: Double) -> String { String(format: "%.2e", x) } func f3(_ x: Double) -> String { String(format: "%.3f", x) } func f1(_ x: Double) -> String { String(format: "%.1f", x) }