import simd func rect(_ x0: Double, _ y0: Double, _ x1: Double, _ y1: Double) -> [V2] { [V2(x0, y0), V2(x1, y0), V2(x1, y1), V2(x0, y1)] } func line(_ s: String) { print(s) } let only = CommandLine.arguments.dropFirst().first // MARK: 1 and 2 — letter fold if only == nil || only == "letter" { let sheet = Sheet(boundary: rect(0, 0, 3, 3), creases: [(V2(1, 0), V2(1, 3)), (V2(2, 0), V2(2, 3))]) let k = Kinematics(sheet, root: sheet.face(containing: V2(1.5, 1.5))) let left = sheet.edges(on: V2(1, 0), V2(1, 3)), right = sheet.edges(on: V2(2, 0), V2(2, 3)) func set(_ l: Double, _ r: Double) -> [Double] { var a = [Double](repeating: 0, count: sheet.edges.count) for e in left { a[e] = l } for e in right { a[e] = r } return a } let bothValley = prescribed(k) { t in set(180 * t, 180 * t) } let staggered = prescribed(k) { t in set(180 * min(1, 2 * t), 180 * max(0, 2 * t - 1)) } let valleyMountain = prescribed(k) { t in set(180 * t, -180 * t) } line("## Case 1 — letter fold, both flaps valley") line("- M1, one linear parameter: max gap \(sci(bothValley.maxGap)); first collision at t = \(bothValley.firstCollision.map { fmt($0, 3) } ?? "none") (\(bothValley.firstCollision.map { fmt($0 * 180, 1) } ?? "-")°)") line("- M1 staggered (left on t 0–0.5, right on 0.5–1): max gap \(sci(staggered.maxGap)); first collision \(staggered.firstCollision.map { fmt($0, 3) } ?? "none")") line("## Case 2 — letter fold, left valley, right mountain") line("- M1: max gap \(sci(valleyMountain.maxGap)); first collision \(valleyMountain.firstCollision.map { fmt($0, 3) } ?? "none")") line("") } // MARK: 3 — generic flat-foldable degree-4 vertex if only == nil || only == "degree4" { let rays = [0.0, 60, 140, 260] // sectors 60, 80, 120, 100 let ends = rays.map { deg -> V2 in let d = V2(cos(deg * .pi / 180), sin(deg * .pi / 180)) return d / max(abs(d.x), abs(d.y)) } let sheet = Sheet(boundary: rect(-1, -1, 1, 1), creases: ends.map { (V2.zero, $0) }) let e = ends.map { sheet.edges(on: .zero, $0)[0] } // The 60° crease is the one left off the tree: its angle is measured. let k = Kinematics(sheet, root: sheet.face(containing: V2(cos(200 * .pi / 180), sin(200 * .pi / 180)) * 0.5), prefer: [e[2], e[0], e[3]]) line("## Case 3 — flat-foldable degree-4 vertex, sectors 60°, 80°, 120°, 100°") // Maekawa and big-little-big: the odd crease bounds the 60° sector, so it is the 0° or the 60° crease. func lin(_ signs: [Double]) -> (Double) -> [Double] { { t in var a = [Double](repeating: 0, count: sheet.edges.count) for i in 0..<4 { a[e[i]] = 180 * t * signs[i] } return a } } for (name, signs) in [("odd crease at 0°", [-1.0, 1, 1, 1]), ("odd crease at 60°", [1.0, -1, 1, 1])] { let m1 = prescribed(k, collisions: false, lin(signs)) line("- M1, all four angles linear to the assignment with the \(name): max gap \(sci(m1.maxGap)) on a sheet of side 2") } for s0 in [-1.0, 1.0] { let m2 = continuation(k, start: lin([0, 0, 0, 0])(0), drivers: [e[2]: { 180 * $0 }], free: [e[0], e[3]], seed: [e[0]: 180 * s0, e[3]: 180], collisions: false) let at = { (t: Double) in [0, 1, 2, 3].map { fmt(m2.angle(e[$0], at: t)) }.joined(separator: ", ") } line("- M2, 140° crease driven 0 → 180, 0° and 260° creases solved, 60° crease measured (seed for 0°: \(s0 > 0 ? "+" : "−")): max gap \(sci(m2.maxGap)); angles (0°, 60°, 140°, 260°) at t = 0.25: \(at(0.25)); 0.5: \(at(0.5)); 0.75: \(at(0.75)); 1: \(at(1))") } // The closed form for a flat-foldable degree-4 vertex with consecutive sectors α, β. let k4 = cos((60.0 + 80) / 2 * .pi / 180) / cos((60.0 - 80) / 2 * .pi / 180) line("- closed form tan(ρ₁/2) = k·tan(ρ₂/2), k = cos((α+β)/2) / cos((α−β)/2) = \(fmt(k4, 4)) for α = 60°, β = 80°") // How densely must foldc sample a solved curve? Sample the solved angles at // N + 1 points, rebuild them by linear or cubic interpolation, and measure the tear. let reference = continuation(k, start: lin([0, 0, 0, 0])(0), drivers: [e[2]: { 180 * $0 }], free: [e[0], e[3]], seed: [e[0]: 180, e[3]: 180], steps: 1024, collisions: false) func sampled(_ n: Int) -> [[Double]] { (0...n).map { i in [e[0], e[3]].map { reference.angle($0, at: Double(i) / Double(n)) } } } func cubic(_ ys: [Double], _ t: Double) -> Double { let n = ys.count - 1 let x = t * Double(n) let i = min(Int(x), n - 1), u = x - Double(i) let m = { (j: Int) -> Double in j == 0 ? ys[1] - ys[0] : j == n ? ys[n] - ys[n - 1] : (ys[j + 1] - ys[j - 1]) / 2 } let (p0, p1, m0, m1) = (ys[i], ys[i + 1], m(i), m(i + 1)) let u2 = u * u, u3 = u2 * u return (2 * u3 - 3 * u2 + 1) * p0 + (u3 - 2 * u2 + u) * m0 + (-2 * u3 + 3 * u2) * p1 + (u3 - u2) * m1 } var rows: [String] = [] for n in [16, 32, 64, 128, 256] { let samples = sampled(n) func angles(_ interpolate: @escaping ([Double], Double) -> Double) -> (Double) -> [Double] { { t in var a = lin([0, 0, 0, 0])(0) a[e[2]] = 180 * t for (j, edge) in [e[0], e[3]].enumerated() { a[edge] = interpolate(samples.map { $0[j] }, t) } return a } } let linear = prescribed(k, steps: 2048, collisions: false, angles { ys, t in let x = t * Double(ys.count - 1), i = min(Int(x), ys.count - 2), u = x - Double(i) return ys[i] + u * (ys[i + 1] - ys[i]) }) let smooth = prescribed(k, steps: 2048, collisions: false, angles(cubic)) rows.append("N = \(n): linear \(sci(linear.maxGap)), cubic \(sci(smooth.maxGap))") } line("- solved curves sampled at N + 1 points and interpolated, max gap on a sheet of side 2: " + rows.joined(separator: "; ")) line("") } // MARK: 4 — inside reverse fold of a two-layer flap if only == nil || only == "reverse" { let sheet = Sheet(boundary: rect(0, 0, 4, 2), creases: [ (V2(0, 1), V2(2, 1)), (V2(2, 1), V2(4, 1)), (V2(2, 1), V2(3, 2)), (V2(2, 1), V2(3, 0)), ]) let root = sheet.face(containing: V2(0.5, 0.5)) let body = sheet.edges(on: V2(0, 1), V2(2, 1))[0], tip = sheet.edges(on: V2(2, 1), V2(4, 1))[0] let a = sheet.edges(on: V2(2, 1), V2(3, 2))[0], b = sheet.edges(on: V2(2, 1), V2(3, 0))[0] var start = [Double](repeating: 0, count: sheet.edges.count) start[body] = 180; start[tip] = 180 // Tree without the tip spine (driving the reverse creases), or without A (driving the tip spine). let byCreases = Kinematics(sheet, root: root, prefer: [body, b, a]) let bySpine = Kinematics(sheet, root: root, prefer: [body, tip, b]) line("## Case 4 — inside reverse fold, 4×2 sheet folded on its spine y = 1") line("- start gap \(sci(byCreases.gap(start))). Body spine and tip spine start at 180, reverse creases A (upper layer) and B (lower layer) at 0. Angles below are measured from the geometry and unwrapped, so a value past ±180 means a face went through its neighbour at the hinge.") func row(_ s: Sweep) -> String { let r = [body, tip, a, b].map { e in let (lo, hi) = s.range(e); return "\(fmt(lo))…\(fmt(hi))" } return "max gap \(sci(s.maxGap)); ranges body \(r[0]), tip \(r[1]), A \(r[2]), B \(r[3]); end body \(fmt(s.end[body])), tip \(fmt(s.end[tip])), A \(fmt(s.end[a])), B \(fmt(s.end[b]))" } for sb in [1.0, -1.0] { for sa in [1.0, -1.0] { let s = continuation(byCreases, start: start, drivers: [b: { 180 * $0 * sb }], free: [a], seed: [a: 180 * sa], collisions: false) line("- (a) body held at 180, B → \(fmt(180 * sb)), A solved (seed \(sa > 0 ? "+" : "−")): \(row(s))") } let s = continuation(byCreases, start: start, drivers: [b: { 180 * $0 * sb }], free: [body, a], seed: [a: -180 * sb], collisions: false) line("- (b) body free, B → \(fmt(180 * sb)), body and A solved: \(row(s))") } for st in [1.0, -1.0] { let s = continuation(bySpine, start: start, drivers: [tip: { 180 - 360 * $0 }], free: [body, b], seed: [b: 180 * st], collisions: false) line("- (c) body free, tip spine driven 180 → −180, body and B solved (seed B \(st > 0 ? "+" : "−")): \(row(s))") } let phase1 = continuation(bySpine, start: start, drivers: [tip: { 180 * (1 - $0) }], free: [body, b], collisions: false) line("- (c′) phase 1, tip spine 180 → 0: \(row(phase1))") for st in [1.0, -1.0] { let s = continuation(bySpine, start: phase1.end, drivers: [tip: { -180 * $0 }], free: [body, b], seed: [body: 180, b: 180 * st], collisions: false) line("- (c′) phase 2, tip spine 0 → −180 (seed B \(st > 0 ? "+" : "−")): \(row(s))") } let forced = continuation(bySpine, start: start, drivers: [tip: { 180 - 360 * $0 }], free: [b], seed: [b: 180], collisions: false) line("- (d) M4, body held at 180, tip spine forced 180 → −180, B least-squares: \(row(forced)) (flap height 1)") // (e) How much must the flap open? The body spine opens to φ at mid-fold and // closes again, the tip spine is driven through, B is least-squares. var curve: [String] = [] for phi in [180.0, 150, 120, 90, 60, 30, 10, 0] { let s = continuation(bySpine, start: start, drivers: [tip: { 180 - 360 * $0 }, body: { 180 - (180 - phi) * sin(.pi * $0) }], free: [b], seed: [b: 180], collisions: false) curve.append("φ \(fmt(phi, 0))° → gap \(sci(s.maxGap))") } line("- (e) body spine opened to φ at mid-fold, tip spine driven through, B least-squares: " + curve.joined(separator: "; ")) line("") } // MARK: 5 — petal fold of the front flap of a preliminary base if only == nil || only == "petal" { // The bird base's crease pattern on the unit square (after Origami Simulator's // birdBase.svg, made exact): midlines, the diagonal through the two quadrants // the preliminary base folds in half, the eight 22.5° kite creases, and the // square joining their ends on the midlines. let h = 0.5 * (2.0.squareRoot() - 1) // 0.5·tan 22.5° let L = V2(h, 0.5), B = V2(0.5, h), R = V2(1 - h, 0.5), T = V2(0.5, 1 - h) let c00 = V2(0, 0), c10 = V2(1, 0), c11 = V2(1, 1), c01 = V2(0, 1) let sheet = Sheet(boundary: rect(0, 0, 1, 1), creases: [ (V2(0.5, 0), V2(0.5, 1)), (V2(0, 0.5), V2(1, 0.5)), (c10, c01), (c00, B), (c00, L), (c10, B), (c10, R), (c11, R), (c11, T), (c01, T), (c01, L), (L, B), (B, R), (R, T), (T, L), ]) let midlines = sheet.edges(on: V2(0.5, 0), V2(0.5, 1)) + sheet.edges(on: V2(0, 0.5), V2(1, 0.5)) let diagonal = sheet.edges(on: c10, c01) var start = [Double](repeating: 0, count: sheet.edges.count) for e in midlines { start[e] = -180 } for e in diagonal { start[e] = 180 } let hinge = sheet.edges(on: L, B)[0] // Everything outside the back quadrant may move; the back quadrant holds still. let back: (Int) -> Bool = { e in let (u, v) = sheet.edges[e] let m = (sheet.verts[u] + sheet.verts[v]) / 2 return m.x > 0.5 - 1e-9 && m.y > 0.5 - 1e-9 } let movable = sheet.interiorEdges.filter { !back($0) && $0 != hinge } let k = Kinematics(sheet, root: sheet.face(containing: V2(0.55, 0.55)), prefer: [hinge] + movable) let free = movable.filter(k.treeEdges.contains) let kites = [sheet.edges(on: c00, B)[0], sheet.edges(on: c00, L)[0]] let innerKites = [sheet.edges(on: c10, B)[0], sheet.edges(on: c01, L)[0]] let outerMidlines = [sheet.edges(on: V2(0.5, 0), B)[0], sheet.edges(on: V2(0, 0.5), L)[0]] let innerTilted = sheet.edges(on: B, R) + sheet.edges(on: T, L) line("## Case 5 — petal fold of a preliminary base's front flap") line("- \(sheet.faces.count) faces, \(sheet.interiorEdges.count) creases, \(free.count) free on the spanning tree, \(movable.count + 1 - free.count - 1) measured off it; preliminary base start gap \(sci(k.gap(start)))") func row(_ s: Sweep) -> String { func r(_ es: [Int]) -> String { es.map { let (lo, hi) = s.range($0); return "\(fmt(lo))…\(fmt(hi))" }.joined(separator: ", ") } func end(_ es: [Int]) -> String { es.map { fmt(s.end[$0]) }.joined(separator: ", ") } let moved = sheet.interiorEdges.filter { abs(s.end[$0] - start[$0]) > 1 }.count return "max gap \(sci(s.maxGap)); collision \(s.firstCollision.map { "at t = \(fmt($0, 3))" } ?? "none"); hinge \(r([hinge])); front kites \(r(kites)) → \(end(kites)); inner kites \(r(innerKites)) → \(end(innerKites)); outer midline segments \(r(outerMidlines)) → \(end(outerMidlines)); inner square sides \(r(innerTilted)) → \(end(innerTilted)); creases changed: \(moved)" } // Intended as the petal fold: the front point lifts on the hinge, and each // side — the front layer's side and the inner layer behind it, still joined // along the midline — swings in on its two kite creases. The traced points // show the inner layers' corners lifting too, which a petal fold does not // do, so this crease pattern is not the bird base; see the decision record. let petalKinematics = Kinematics(sheet, root: sheet.face(containing: V2(0.55, 0.55)), prefer: [hinge] + kites + innerKites) let petalFree = (kites + innerKites).filter(petalKinematics.treeEdges.contains) line("- the petal fold's own creases only (hinge driven; \(petalFree.count) of the 4 kite creases on the tree, the rest measured):") for sk in [1.0, -1.0] { var seed: [Int: Double] = [:] for e in kites where petalFree.contains(e) { seed[e] = 180 * sk } for e in innerKites where petalFree.contains(e) { seed[e] = -180 * sk } let s = continuation(petalKinematics, start: start, drivers: [hinge: { 180 * $0 }], free: petalFree, seed: seed, steps: 200) line(" - kite seed \(sk > 0 ? "+" : "−"): \(row(s)); front kite at t = 0.25, 0.5, 0.75: \([0.25, 0.5, 0.75].map { fmt(s.angle(kites[0], at: $0)) }.joined(separator: ", "))") let linear = prescribed(petalKinematics, steps: 200, collisions: false) { t in var a = start for e in petalKinematics.treeEdges { a[e] = start[e] + t * (s.end[e] - start[e]) } return a } line(" - M1, the same end state with every angle linear: max gap \(sci(linear.maxGap))") if sk < 0 { // Where the front point, the front layer's side corner and the inner layer's corner go. for (name, p) in [("front point (0,0)", c00), ("side corner (0.5,0)", V2(0.5, 0)), ("inner corner (1,0)", c10), ("hinge end B", B)] { let f = sheet.faces.indices.first { sheet.faces[$0].contains { simd_distance(sheet.verts[$0], p) < 1e-9 } }! let v = sheet.faces[f].first { simd_distance(sheet.verts[$0], p) < 1e-9 }! let i = sheet.faces[f].firstIndex(of: v)! let path = [0.0, 0.5, 1.0].map { t -> String in let a = s.samples.min { abs($0.t - t) < abs($1.t - t) }! var angles = start for e in petalKinematics.treeEdges { angles[e] = a.angles[e] } let q = petalKinematics.positions(angles)[f][i] return "(\(fmt(q.x, 3)), \(fmt(q.y, 3)), \(fmt(q.z, 3)))" } line(" - \(name): \(path.joined(separator: " → "))") } } } // With every crease outside the back quadrant free, the solver is // underdetermined and finds other mechanisms; the step has to say which // creases move, which is what krz:step's axes are for. let loose = continuation(k, start: start, drivers: [hinge: { 180 * $0 }], free: free, steps: 200, collisions: false) line("- control, every crease outside the back quadrant free (\(free.count) unknowns): \(row(loose))") line("") } // MARK: 7 — petal fold on the bird base read from a crane if only == nil || only == "petal2" { // The bird base inside Origami Simulator's assets/Origami/flat_crane.svg, made // exact: the midline cross from the centre to the four kite ends, the // diagonal the preliminary base folds, the eight 22.5° kite creases, and the // two petal hinges. The outer midline segments and the other diagonal are // not creases of the bird base; the preliminary base folds the outer // segments, so the petal fold unfolds them. let h = 0.5 * (2.0.squareRoot() - 1) let L = V2(h, 0.5), B = V2(0.5, h), R = V2(1 - h, 0.5), T = V2(0.5, 1 - h), O = V2(0.5, 0.5) let c00 = V2(0, 0), c10 = V2(1, 0), c11 = V2(1, 1), c01 = V2(0, 1) let mL = V2(0, 0.5), mB = V2(0.5, 0), mR = V2(1, 0.5), mT = V2(0.5, 1) let sheet = Sheet(boundary: rect(0, 0, 1, 1), creases: [ (mL, L), (L, O), (O, R), (R, mR), (mB, B), (B, O), (O, T), (T, mT), (c10, c01), (c00, B), (c00, L), (c10, B), (c10, R), (c11, R), (c11, T), (c01, T), (c01, L), (L, B), (R, T), ]) func e(_ p: V2, _ q: V2) -> Int { sheet.edges(on: p, q)[0] } let outer = [e(mL, L), e(mB, B)], backOuter = [e(R, mR), e(T, mT)] let cross = [e(L, O), e(O, R), e(B, O), e(O, T)] let diagonal = sheet.edges(on: c10, c01) let hinge = e(L, B) let kites = [e(c00, B), e(c00, L)], innerKites = [e(c10, B), e(c01, L)] var start = [Double](repeating: 0, count: sheet.edges.count) for x in outer + backOuter + cross { start[x] = -180 } // mountain for x in diagonal { start[x] = 180 } // valley var end = start end[hinge] = 180 for x in kites + innerKites { end[x] = -180 } for x in outer { end[x] = 0 } let root = sheet.face(containing: V2(0.6, 0.6)) // Static creases go on the tree first, so the regions they join are held // still by the tree, not only by closure; the moving creases fill the rest. let moving = Set([hinge] + kites + innerKites + outer) let fixed = sheet.interiorEdges.filter { !moving.contains($0) } let k = Kinematics(sheet, root: root, prefer: fixed + [hinge] + kites + innerKites + outer) precondition(k.treeEdges.contains(hinge)) line("## Case 7 — petal fold on the bird base from Origami Simulator's flat crane") line("- tree: \(fixed.filter(k.treeEdges.contains).count) of \(fixed.count) static creases, \((kites + innerKites + outer).filter(k.treeEdges.contains).count) of 6 moving creases solved, the rest measured") // Flat-foldability of both end states, vertex by vertex. func check(_ a: [Double]) -> String { var bad: [String] = [] for v in sheet.verts.indices { let es = sheet.interiorEdges.filter { [sheet.edges[$0].0, sheet.edges[$0].1].contains(v) } let onBoundary = sheet.edges.indices.contains { sheet.edgeFaces[$0].count == 1 && [sheet.edges[$0].0, sheet.edges[$0].1].contains(v) } if onBoundary { continue } let folded = es.filter { abs(abs(a[$0]) - 180) < 1e-9 } guard !folded.isEmpty else { continue } let dirs = folded.map { x -> Double in let w = sheet.edges[x].0 == v ? sheet.edges[x].1 : sheet.edges[x].0 let d = sheet.verts[w] - sheet.verts[v] return atan2(d.y, d.x) * 180 / .pi }.sorted() let sectors = dirs.indices.map { i in i + 1 < dirs.count ? dirs[i + 1] - dirs[i] : dirs[0] + 360 - dirs[i] } let alt = stride(from: 0, to: sectors.count, by: 2).map { sectors[$0] }.reduce(0, +) let m = folded.filter { a[$0] < 0 }.count, vv = folded.count - m if folded.count % 2 != 0 || abs(alt - 180) > 1e-9 || abs(m - vv) != 2 { bad.append("(\(fmt(sheet.verts[v].x, 3)), \(fmt(sheet.verts[v].y, 3))) degree \(folded.count), M \(m) V \(vv), alternate sum \(fmt(alt, 3))") } } return bad.isEmpty ? "Kawasaki and Maekawa hold at every interior vertex" : bad.joined(separator: "; ") } line("- preliminary base: gap \(sci(k.gap(start))); \(check(start))") line("- after the front petal fold: gap \(sci(k.gap(end))); \(check(end))") let free = (kites + innerKites + outer).filter(k.treeEdges.contains) for (name, seedSign) in [("kites seeded mountain", -1.0), ("kites seeded valley", 1.0)] { var seed: [Int: Double] = [:] for x in kites + innerKites where free.contains(x) { seed[x] = 180 * seedSign } for x in outer where free.contains(x) { seed[x] = 180 } let s = continuation(k, start: start, drivers: [hinge: { 180 * $0 }], free: free, seed: seed, steps: 400) func r(_ es: [Int]) -> String { es.map { let (lo, hi) = s.range($0); return "\(fmt(lo))…\(fmt(hi))" }.joined(separator: ", ") } var details: [String] = [] let reached = sheet.interiorEdges.allSatisfy { x in let d = (s.end[x] - end[x]).truncatingRemainder(dividingBy: 360) return min(abs(d), 360 - abs(d)) < 1e-3 } let away = s.samples.filter { $0.t >= 0.05 && $0.t <= 0.95 }.first { sample in var a = start for x in k.treeEdges { a[x] = sample.angles[x] } return !intersectingPairs(k.positions(a)).isEmpty } details.append(" - collisions with t in 0.05…0.95, away from the coincident start and end: \(away.map { "first at t = \(fmt($0.t, 3))" } ?? "none")") if let t0 = s.firstCollision { var a = start let sample = s.samples.min { abs($0.t - t0) < abs($1.t - t0) }! for x in k.treeEdges { a[x] = sample.angles[x] } let centroid = { (f: Int) -> String in let c = sheet.faces[f].map { sheet.verts[$0] }.reduce(V2.zero, +) / Double(sheet.faces[f].count) return "(\(fmt(c.x, 2)), \(fmt(c.y, 2)))" } let pairs = intersectingPairs(k.positions(a)).map { "\(centroid($0.0))×\(centroid($0.1))" } details.append(" - first colliding faces at t = \(fmt(t0, 3)), by centroid on the sheet: \(pairs.joined(separator: ", "))") } line("- M2, hinge 0 → 180 driven, \(free.count) creases solved (\(name)): max gap \(sci(s.maxGap)); collision \(s.firstCollision.map { "at t = \(fmt($0, 3))" } ?? "none"); front kites \(r(kites)); inner kites \(r(innerKites)); outer midline segments \(r(outer)); reaches the bird base: \(reached)") details.forEach(line) let at = { (t: Double) in [kites[0], innerKites[0], outer[0]].map { fmt(s.angle($0, at: t)) }.joined(separator: ", ") } line(" - front kite, inner kite, outer segment at t = 0.25: \(at(0.25)); 0.5: \(at(0.5)); 0.75: \(at(0.75))") let linear = prescribed(k, steps: 400, collisions: false) { t in var a = start for x in k.treeEdges { a[x] = start[x] + t * (s.end[x] - start[x]) } return a } line(" - M1, every angle linear to the same end: max gap \(sci(linear.maxGap))") if reached { for (pname, p) in [("front point (0,0)", c00), ("side corner (0.5,0)", mB), ("leg corner (1,0)", c10)] { let f = sheet.faces.indices.first { sheet.faces[$0].contains { simd_distance(sheet.verts[$0], p) < 1e-9 } }! let i = sheet.faces[f].firstIndex { simd_distance(sheet.verts[$0], p) < 1e-9 }! let path = [0.0, 0.5, 1.0].map { t -> String in var a = start let sample = s.samples.min { abs($0.t - t) < abs($1.t - t) }! for x in k.treeEdges { a[x] = sample.angles[x] } let q = k.positions(a)[f][i] return "(\(fmt(q.x, 3)), \(fmt(q.y, 3)), \(fmt(q.z, 3)))" } line(" - \(pname): \(path.joined(separator: " → "))") } } } line("") } // MARK: 6 — inside reverse fold with bending if only == nil || only == "bending" { let creases = [(V2(0, 1), V2(2, 1)), (V2(2, 1), V2(4, 1)), (V2(2, 1), V2(3, 2)), (V2(2, 1), V2(3, 0))] let sheet = Sheet(boundary: rect(0, 0, 4, 2), creases: creases) let root = sheet.face(containing: V2(0.5, 0.5)), bodyTop = sheet.face(containing: V2(0.5, 1.5)) let body = sheet.edges(on: V2(0, 1), V2(2, 1))[0], tip = sheet.edges(on: V2(2, 1), V2(4, 1))[0] let b = sheet.edges(on: V2(2, 1), V2(3, 0))[0] let k = Kinematics(sheet, root: root, prefer: [body, tip, b]) func pose(_ bodyAngle: Double, _ tipAngle: Double, _ bAngle: Double) -> [Double] { var a = [Double](repeating: 0, count: sheet.edges.count) a[body] = bodyAngle; a[tip] = tipAngle; a[b] = bAngle return a } let start = pose(180, 180, 0), end = pose(180, -180, 180) line("## Case 6 — inside reverse fold with bending (as-rigid-as-possible interpolation)") line("- end pose gap \(sci(k.gap(end))): the reverse fold's coordinates; inside, outside and folded-over share them, so the path decides which") for spacing in [0.25, 0.125] { let mesh = Bending(sheet, k, spacing: spacing, creases: creases, pinFace: root) line("- mesh spacing \(spacing): \(mesh.fine.verts.count) vertices, \(mesh.triangles.count) triangles") // Which layer is the tip between, just before it lands? Compare a tip point's // height with the body's two layers at the same place. func side(_ x: [V3]) -> String { let probe = mesh.fine.verts.indices.min { simd_distance(mesh.fine.verts[$0], V2(2.75, 0.75)) < simd_distance(mesh.fine.verts[$1], V2(2.75, 0.75)) }! let p = x[probe] var zTop: Double? = nil var t = 0 for (f, face) in mesh.fine.faces.enumerated() { for kk in 1..<(face.count - 1) { defer { t += 1 } guard mesh.parent[f] == bodyTop else { continue } let (a0, b0, c0) = (x[face[0]], x[face[kk]], x[face[kk + 1]]) let v0 = V2(b0.x - a0.x, b0.y - a0.y), v1 = V2(c0.x - a0.x, c0.y - a0.y), v2 = V2(p.x - a0.x, p.y - a0.y) let d = v0.x * v1.y - v0.y * v1.x guard abs(d) > 1e-12 else { continue } let u = (v2.x * v1.y - v2.y * v1.x) / d, w = (v0.x * v2.y - v0.y * v2.x) / d if u >= 0, w >= 0, u + w <= 1 { zTop = a0.z + u * (b0.z - a0.z) + w * (c0.z - a0.z) } } } guard let top = zTop else { return "tip not over the body yet" } if p.z > min(0, top) && p.z < max(0, top) { return "between the body's layers (inside)" } return "outside the body's layers (z \(fmt(p.z, 3)), layers at 0 and \(fmt(top, 3)))" } func run(_ name: String, from a: [Double], to bPose: [Double]) { var worst = 0.0 var collision: Double? = nil for i in 0...60 { let s = Double(i) / 60 let x = mesh.interpolate(from: a, to: bPose, at: s) worst = max(worst, mesh.strain(x)) if collision == nil, s > 0.02, s < 0.98, !intersectingPairs(mesh.triangleSoup(x)).isEmpty { collision = s } } let near = mesh.interpolate(from: a, to: bPose, at: 0.97) line(" - \(name): max strain \(fmt(100 * worst, 2))%; collision \(collision.map { "at s = \(fmt($0, 3))" } ?? "none"); at s = 0.97 the tip is \(side(near))") } run("direct, closed flap → reverse fold", from: start, to: end) // Open, pop, close: open the flap rigidly to φ with the tip still pointing // out; pop the tip inside with the flap held at φ (the only bending); close // rigidly along the reverse-fold branch. At φ = 0 the pop is free: that is // the fully unfolded rigid path. for phi in [170.0, 150, 120, 90, 60, 30, 10] { var popped = pose(phi, -phi, phi) k.solve(&popped, free: [b]) let closing = continuation(k, start: popped, drivers: [body: { phi + (180 - phi) * $0 }, tip: { -(phi + (180 - phi) * $0) }], free: [b], collisions: false) var worst = 0.0, beyondQuarter = 0.0, beyondHalf = 0.0 var collision: Double? = nil for i in 0...60 { let s = Double(i) / 60 let x = mesh.interpolate(from: pose(phi, phi, 0), to: popped, at: s) worst = max(worst, mesh.strain(x)) beyondQuarter = max(beyondQuarter, mesh.strain(x, outside: 0.25, center: V2(2, 1))) beyondHalf = max(beyondHalf, mesh.strain(x, outside: 0.5, center: V2(2, 1))) if collision == nil, s > 0.02, s < 0.98, !intersectingPairs(mesh.triangleSoup(x)).isEmpty { collision = s } } let closeCollision = closing.samples.dropLast(3).first { sample in var a = sample.angles a[body] = sample.angles[body] return !intersectingPairs(mesh.triangleSoup(mesh.pose(a))).isEmpty } // The same pop with iterative ARAP: the body held at φ, only the tip's // far corners moved along the pop, everything else left to the solver. var bent = 0.0 if spacing == 0.25 { let bodyFaces = Set([root, bodyTop]) let held = Set(mesh.fine.faces.indices.filter { bodyFaces.contains(mesh.parent[$0]) }.flatMap { mesh.fine.faces[$0] }) let corners = mesh.fine.verts.indices.filter { mesh.fine.verts[$0].x > 4 - 1e-9 } var x = mesh.pose(pose(phi, phi, 0)) for i in 1...12 { let s = Double(i) / 12 let guide = mesh.interpolate(from: pose(phi, phi, 0), to: popped, at: s) var handles: [Int: V3] = [:] for h in held { handles[h] = guide[h] } for c in corners { handles[c] = guide[c] } x = mesh.deform(start: x, handles: handles) bent = max(bent, mesh.strain(x)) } } let nearEnd = closing.samples.min { abs($0.t - 0.97) < abs($1.t - 0.97) }!.angles line(" - open to \(fmt(phi, 0))°, pop, close: \(spacing == 0.25 ? "iterative ARAP pop strain \(fmt(100 * bent, 2))%; " : "")pop strain \(fmt(100 * worst, 2))% (beyond 0.25 of the vertex \(fmt(100 * beyondQuarter, 2))%, beyond 0.5 \(fmt(100 * beyondHalf, 2))%), pop collision \(collision.map { "at s = \(fmt($0, 3))" } ?? "none"); closing gap \(sci(closing.maxGap)), collision \(closeCollision.map { "at t = \(fmt($0.t, 3))" } ?? "none"), reaches the end pose \(abs(closing.end[b] - 180) < 1e-3); just before landing the tip is \(side(mesh.pose(nearEnd)))") } } line("") }