spikes/bending/Sources/Bending/PopCase.swift

78 lines · 4185 bytes

5 symbols in this file
 1import FoldFormat
 2import Folding
 3import simd
 4
 5/// A reverse fold whose near layers turn rigidly about the spine through `vertex`, the tip
 6/// following decision 0001's vertex on either branch.
 7func reverseStage(name: String, model: Model, frame: Int, vertex v: V3, spine s: V3, crease c: V3, eye: V3,
 8                  nearBody: Set<Int>, tipNear: Set<Int>, tipFar: Set<Int>, lower: Set<Int>) -> Stage {
 9    // the turn's sign that lifts the near layers toward the eye
10    let x = model.positions(frame)
11    let f = nearBody.first ?? tipNear.first!
12    let centre = model.faces[f].map { x[$0] }.reduce(.zero, +) / Double(model.faces[f].count)
13    var d = centre - v
14    d -= dot(d, s) * s
15    let sign = dot(simd_quatd(angle: 0.5, axis: s).act(d), eye) > 0 ? 1.0 : -1.0
16    func open(_ a: Double) -> simd_quatd { simd_quatd(angle: sign * a * .pi / 180, axis: s) }
17    var stage = Stage(name: name, model: model, frame: frame, units: [
18        (nearBody, { a, _ in Rigid(open(a), about: v) }),
19        (tipNear, { a, r in
20            guard r else { return Rigid(open(a), about: v) }
21            return Rigid(reverseBranch(w: -s, cNear: open(a).act(c), cFar: c).near * open(a), about: v)
22        }),
23        (tipFar, { a, r in
24            guard r else { return .identity }
25            return Rigid(reverseBranch(w: -s, cNear: open(a).act(c), cFar: c).far, about: v)
26        }),
27    ], tip: tipNear.union(tipFar), upper: nearBody, lower: lower, normal: eye)
28    stage.nearTip = tipNear
29    stage.popAxes = { a in (v, -s, open(a).act(c), c) }
30    return stage
31}
32
33/// Decision 0002's fixture as a stage.
34func fixtureStage() throws -> (Stage, V3) {
35    let file = try compiledReverseFixture()
36    let model = Model(file)
37    let step = try readStep(2, in: file)
38    let pop = step.pop!
39    let x = model.positions(1)
40    func at(_ p: V2) -> V3 { x[model.flat.firstIndex { simd_distance($0, p) < 1e-9 }!] }
41    let v = at(V2(2, 1)), s = normalize(at(V2(0, 1)) - v), c = normalize(at(V2(3, 0)) - v)
42    let opening = Set(pop.openAxes[0].movingFaces)
43    let tip = step.movingFaceUnion
44    let stage = reverseStage(name: "fixture", model: model, frame: 1, vertex: v, spine: s, crease: c,
45                             eye: step.viewpoint.eye, nearBody: opening.subtracting(tip), tipNear: opening.intersection(tip),
46                             tipFar: tip.subtracting(opening), lower: Set(model.faces.indices).subtracting(opening).subtracting(tip))
47    return (stage, v)
48}
49
50func popCase() throws {
51    print("## The pop: per-face slerp (0002) against as-rigid-as-possible paths")
52    print("")
53    print("Delaunay mesh, h = 1/32 of the longer side. The opening and closing are rigid in every row; strain is the pop's.")
54    print("")
55    print("| step | α | slerp: edge, principal, collisions | guided (end 10%): edge, principal, collisions | spine guided: edge, principal, collisions, end gap | banded: edge, principal, collisions, end gap |")
56    print("|---|---|---|---|---|---|")
57    let (fixture, fixtureVertex) = try fixtureStage()
58    let crane = try craneStages()
59    let v = craneFrame(V2(0, 0.47))
60    let headVertex = v + 0.33 * simd_quatd(angle: .pi, axis: (try neckFold(.raised, bendLine: true)).crease).act(-normalize(V3(1, 1, 0)))
61    for (stage, vertex) in [(fixture, fixtureVertex), (crane.neck, v), (crane.tail, v), (crane.head, headVertex)] {
62        let mesh = buildMesh(.delaunay, stage.model, spacing: stage.model.size / 32)
63        for alpha in [170.0, 160.0, 150.0, 120.0] {
64            var cells: [String] = []
65            for pop in [PopPath.slerp, .guided(fraction: 0.9), .spine, .banded(band: 1)] {
66                let run = runStage(stage, mesh: mesh, alphaMax: alpha, collisionDepth: 1e-3 * stage.model.size / 32,
67                                   pop: pop, vertex: vertex)
68                cells.append("\(pct(run.popStrain)), \(pct(run.popPrincipal)), \(run.collisions.pop)" + (pop.isGuided ? "" : ", \(sci(run.popEndGap))"))
69            }
70            print("| \(stage.name) | \(Int(alpha))° | \(cells.joined(separator: " | ")) |")
71        }
72    }
73    print("")
74}
75
76extension PopPath {
77    var isGuided: Bool { if case .guided = self { true } else { false } }
78}