spikes/bending/Sources/Bending/CraneCase.swift
108 lines · 6538 bytes
7 symbols in this file
1import Foundation
2import simd
3
4/// The crane's view in frames 12–14: eye (0, 0, 1), up (1, 1, 0).
5let craneUp = normalize(V3(1, 1, 0)), craneEye = V3(0, 0, 1), craneRight = cross(normalize(V3(1, 1, 0)), V3(0, 0, 1))
6func craneView(_ p: V3) -> V2 { V2(dot(p, craneRight), dot(p, craneUp)) }
7func craneFrame(_ v: V2) -> V3 { v.x * craneRight + v.y * craneUp }
8
9/// Which way the neck's reverse crease runs, from the spine at view (0, 0.47).
10enum NeckCrease: String {
11 /// The staged script's (`crane-1-13.foldscript`): to the leg's edge at (−0.057, 0.288). The neck
12 /// ends pointing down and out, 35° below horizontal.
13 case staged
14 /// 22.5° below horizontal: the neck ends 45° above horizontal, rising through the body between
15 /// its layers, as the traditional crane's does.
16 case raised
17}
18
19/// The neck's reverse fold on frame 12 of `crane-1-12.fold`.
20func neckFold(_ crease: NeckCrease, bendLine: Bool) throws -> ReverseFold {
21 var model = try Model(named: "crane-1-12.fold")
22 let k = 12
23 let vertex = craneFrame(V2(0, 0.47))
24 // the spine: from the vertex toward the centre of the square (vertex 14 in the file)
25 let centre = model.positions(k)[model.flat.firstIndex { simd_distance($0, V2(0.5, 0.5)) < 1e-9 }!]
26 let spine = normalize(centre - vertex)
27 let direction: V3
28 switch crease {
29 case .staged:
30 let edgePoint = model.positions(k)[model.flat.firstIndex { simd_distance($0, V2(0.16324643087570403, 0.7556844508167454)) < 1e-9 }!]
31 direction = normalize(edgePoint - vertex)
32 case .raised:
33 let a = 22.5 * Double.pi / 180
34 direction = normalize(craneFrame(V2(-cos(a), -sin(a))))
35 model = refine(model, frame: k, from: vertex, to: vertex + 0.3 * direction, faces: Set(model.faces.indices)).model
36 }
37 var fold = classify(model, frame: k, vertex: vertex, spine: spine, crease: direction)
38 if bendLine {
39 // a render-mesh line along the spine's line through the near faces that cross it
40 model = refine(model, frame: k, from: craneFrame(V2(0, 0.3)), to: craneFrame(V2(0, 1.1)), faces: fold.across).model
41 fold = classify(model, frame: k, vertex: vertex, spine: spine, crease: direction)
42 }
43 return fold
44}
45
46/// Splits the faces of a crane frame for a reverse fold of the left leg (view x < 0), whose near
47/// half is the crease pattern's x + y < 1.
48func classify(_ model: Model, frame k: Int, vertex: V3, spine: V3, crease: V3, side: Double = -1) -> ReverseFold {
49 let x = model.positions(k)
50 let eps = 1e-9
51 let v = craneView(vertex), d = craneView(vertex + crease) - v
52 var tipNear = Set<Int>(), tipFar = Set<Int>(), nearSide = Set<Int>(), across = Set<Int>(), rest = Set<Int>()
53 for f in model.faces.indices {
54 let view = model.faces[f].map { craneView(x[$0]) }
55 let c = view.reduce(.zero, +) / Double(view.count)
56 let near = model.centroid(f).x + model.centroid(f).y < 1
57 // the leg's tip, view (0, 0), is on the tip side
58 let sign = cross2(d, V2(0, 0) - v) > 0 ? 1.0 : -1.0
59 let tipSide = sign * cross2(d, c - v) > 0 && side * c.x > 0 && c.y < v.y && view.allSatisfy { sign * cross2(d, $0 - v) >= -eps }
60 let minX = view.map(\.x).min()!, maxX = view.map(\.x).max()!
61 if tipSide {
62 if near { tipNear.insert(f) } else { tipFar.insert(f) }
63 } else if near && (side < 0 ? maxX <= eps : minX >= -eps) {
64 nearSide.insert(f)
65 } else if near && minX < -eps && maxX > eps {
66 across.insert(f)
67 } else {
68 rest.insert(f)
69 }
70 }
71 return ReverseFold(model: model, frame: k, vertex: vertex, spine: spine, crease: crease, normal: craneEye,
72 tipNear: tipNear, tipFar: tipFar, nearSide: nearSide, across: across, rest: rest)
73}
74
75func craneCase() throws {
76 print("## Crane neck: opening by bending")
77 print("")
78 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).")
79 print("")
80 for crease in [NeckCrease.staged, .raised] {
81 let plain = try neckFold(crease, bendLine: false)
82 let lined = try neckFold(crease, bendLine: true)
83 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())")
84 print("")
85 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 |")
86 print("|---|---|---|---|---|---|---|---|---|---|---|")
87 func row(_ name: String, _ fold: ReverseFold, _ free: Set<Int>, divisions: Double, alpha: Double) {
88 let model = fold.model
89 let h = model.size / divisions
90 let mesh = buildMesh(.delaunay, model, spacing: h)
91 let run = runReverse(fold, mesh: mesh, alphaMax: alpha, free: free, samples: 16, collisionDepth: 1e-3 * h)
92 let first = run.firstCollision.map { "\($0.phase) s=\(f3($0.s)) faces \($0.faces.0)×\($0.faces.1)" } ?? "none"
93 let w = run.worst
94 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) |")
95 }
96 let x = plain.model.positions(plain.frame)
97 let legNear = plain.nearSide.filter { f in plain.model.faces[f].contains { craneView(x[$0]).y < 0.47 + 1e-9 } }
98 row("A: the leg's near layers; body held", plain, Set(legNear), divisions: 32, alpha: 150)
99 for alpha in [170.0, 150.0, 120.0] {
100 row("B: the near side, and the faces across the line", plain, plain.nearSide.union(plain.across), divisions: 32, alpha: alpha)
101 }
102 row("B: the near side, and the faces across the line", plain, plain.nearSide.union(plain.across), divisions: 64, alpha: 150)
103 for alpha in [170.0, 160.0, 150.0, 120.0] {
104 row("C: the near side, with a bend line", lined, lined.nearSide, divisions: 32, alpha: alpha)
105 }
106 print("")
107 }
108}