import Foundation import simd /// The crane's view in frames 12–14: eye (0, 0, 1), up (1, 1, 0). let craneUp = normalize(V3(1, 1, 0)), craneEye = V3(0, 0, 1), craneRight = cross(normalize(V3(1, 1, 0)), V3(0, 0, 1)) func craneView(_ p: V3) -> V2 { V2(dot(p, craneRight), dot(p, craneUp)) } func craneFrame(_ v: V2) -> V3 { v.x * craneRight + v.y * craneUp } /// Which way the neck's reverse crease runs, from the spine at view (0, 0.47). enum NeckCrease: String { /// The staged script's (`crane-1-13.foldscript`): to the leg's edge at (−0.057, 0.288). The neck /// ends pointing down and out, 35° below horizontal. case staged /// 22.5° below horizontal: the neck ends 45° above horizontal, rising through the body between /// its layers, as the traditional crane's does. case raised } /// The neck's reverse fold on frame 12 of `crane-1-12.fold`. func neckFold(_ crease: NeckCrease, bendLine: Bool) throws -> ReverseFold { var model = try Model(named: "crane-1-12.fold") let k = 12 let vertex = craneFrame(V2(0, 0.47)) // the spine: from the vertex toward the centre of the square (vertex 14 in the file) let centre = model.positions(k)[model.flat.firstIndex { simd_distance($0, V2(0.5, 0.5)) < 1e-9 }!] let spine = normalize(centre - vertex) let direction: V3 switch crease { case .staged: let edgePoint = model.positions(k)[model.flat.firstIndex { simd_distance($0, V2(0.16324643087570403, 0.7556844508167454)) < 1e-9 }!] direction = normalize(edgePoint - vertex) case .raised: let a = 22.5 * Double.pi / 180 direction = normalize(craneFrame(V2(-cos(a), -sin(a)))) model = refine(model, frame: k, from: vertex, to: vertex + 0.3 * direction, faces: Set(model.faces.indices)).model } var fold = classify(model, frame: k, vertex: vertex, spine: spine, crease: direction) if bendLine { // a render-mesh line along the spine's line through the near faces that cross it model = refine(model, frame: k, from: craneFrame(V2(0, 0.3)), to: craneFrame(V2(0, 1.1)), faces: fold.across).model fold = classify(model, frame: k, vertex: vertex, spine: spine, crease: direction) } return fold } /// Splits the faces of a crane frame for a reverse fold of the left leg (view x < 0), whose near /// half is the crease pattern's x + y < 1. func classify(_ model: Model, frame k: Int, vertex: V3, spine: V3, crease: V3, side: Double = -1) -> ReverseFold { let x = model.positions(k) let eps = 1e-9 let v = craneView(vertex), d = craneView(vertex + crease) - v var tipNear = Set(), tipFar = Set(), nearSide = Set(), across = Set(), rest = Set() for f in model.faces.indices { let view = model.faces[f].map { craneView(x[$0]) } let c = view.reduce(.zero, +) / Double(view.count) let near = model.centroid(f).x + model.centroid(f).y < 1 // the leg's tip, view (0, 0), is on the tip side let sign = cross2(d, V2(0, 0) - v) > 0 ? 1.0 : -1.0 let tipSide = sign * cross2(d, c - v) > 0 && side * c.x > 0 && c.y < v.y && view.allSatisfy { sign * cross2(d, $0 - v) >= -eps } let minX = view.map(\.x).min()!, maxX = view.map(\.x).max()! if tipSide { if near { tipNear.insert(f) } else { tipFar.insert(f) } } else if near && (side < 0 ? maxX <= eps : minX >= -eps) { nearSide.insert(f) } else if near && minX < -eps && maxX > eps { across.insert(f) } else { rest.insert(f) } } return ReverseFold(model: model, frame: k, vertex: vertex, spine: spine, crease: crease, normal: craneEye, tipNear: tipNear, tipFar: tipFar, nearSide: nearSide, across: across, rest: rest) } func craneCase() throws { print("## Crane neck: opening by bending") print("") print("Delaunay mesh. Strain is the largest over every sample of the phase, of the whole mesh. Collisions count mesh triangle pairs over the samples, ignoring crossings shallower than 1e-3 of the sheet (paper thickness).") print("") for crease in [NeckCrease.staged, .raised] { let plain = try neckFold(crease, bendLine: false) let lined = try neckFold(crease, bendLine: true) print("### Neck crease \(crease.rawValue): tip \(plain.tipNear.count) near + \(plain.tipFar.count) far faces; near side \(plain.nearSide.count); across the spine's line \(plain.across.sorted())") print("") print("| what bends | mesh | α | open strain | pop strain | close strain | principal (open, pop, close) | collisions (open, pop, close) | first collision | worst edge (strain, rest, where) | tip between layers |") print("|---|---|---|---|---|---|---|---|---|---|---|") func row(_ name: String, _ fold: ReverseFold, _ free: Set, divisions: Double, alpha: Double) { let model = fold.model let h = model.size / divisions let mesh = buildMesh(.delaunay, model, spacing: h) let run = runReverse(fold, mesh: mesh, alphaMax: alpha, free: free, samples: 16, collisionDepth: 1e-3 * h) let first = run.firstCollision.map { "\($0.phase) s=\(f3($0.s)) faces \($0.faces.0)×\($0.faces.1)" } ?? "none" let w = run.worst print("| \(name) | 1/\(Int(divisions)) | \(Int(alpha))° | \(pct(run.openStrain)) | \(pct(run.popStrain)) | \(pct(run.closeStrain)) | \(pct(run.openPrincipal)), \(pct(run.popPrincipal)), \(pct(run.closePrincipal)) | \(run.openCollisions), \(run.popCollisions), \(run.closeCollisions) | \(first) | \(pct(w.strain)), \(sci(w.rest)), (\(f3(w.at.x)), \(f3(w.at.y))) | \(run.tipInside) |") } let x = plain.model.positions(plain.frame) let legNear = plain.nearSide.filter { f in plain.model.faces[f].contains { craneView(x[$0]).y < 0.47 + 1e-9 } } row("A: the leg's near layers; body held", plain, Set(legNear), divisions: 32, alpha: 150) for alpha in [170.0, 150.0, 120.0] { row("B: the near side, and the faces across the line", plain, plain.nearSide.union(plain.across), divisions: 32, alpha: alpha) } row("B: the near side, and the faces across the line", plain, plain.nearSide.union(plain.across), divisions: 64, alpha: 150) for alpha in [170.0, 160.0, 150.0, 120.0] { row("C: the near side, with a bend line", lined, lined.nearSide, divisions: 32, alpha: alpha) } print("") } }