import FoldFormat import Folding import simd /// A reverse fold whose near layers turn rigidly about the spine through `vertex`, the tip /// following decision 0001's vertex on either branch. func reverseStage(name: String, model: Model, frame: Int, vertex v: V3, spine s: V3, crease c: V3, eye: V3, nearBody: Set, tipNear: Set, tipFar: Set, lower: Set) -> Stage { // the turn's sign that lifts the near layers toward the eye let x = model.positions(frame) let f = nearBody.first ?? tipNear.first! let centre = model.faces[f].map { x[$0] }.reduce(.zero, +) / Double(model.faces[f].count) var d = centre - v d -= dot(d, s) * s let sign = dot(simd_quatd(angle: 0.5, axis: s).act(d), eye) > 0 ? 1.0 : -1.0 func open(_ a: Double) -> simd_quatd { simd_quatd(angle: sign * a * .pi / 180, axis: s) } var stage = Stage(name: name, model: model, frame: frame, units: [ (nearBody, { a, _ in Rigid(open(a), about: v) }), (tipNear, { a, r in guard r else { return Rigid(open(a), about: v) } return Rigid(reverseBranch(w: -s, cNear: open(a).act(c), cFar: c).near * open(a), about: v) }), (tipFar, { a, r in guard r else { return .identity } return Rigid(reverseBranch(w: -s, cNear: open(a).act(c), cFar: c).far, about: v) }), ], tip: tipNear.union(tipFar), upper: nearBody, lower: lower, normal: eye) stage.nearTip = tipNear stage.popAxes = { a in (v, -s, open(a).act(c), c) } return stage } /// Decision 0002's fixture as a stage. func fixtureStage() throws -> (Stage, V3) { let file = try compiledReverseFixture() let model = Model(file) let step = try readStep(2, in: file) let pop = step.pop! let x = model.positions(1) func at(_ p: V2) -> V3 { x[model.flat.firstIndex { simd_distance($0, p) < 1e-9 }!] } let v = at(V2(2, 1)), s = normalize(at(V2(0, 1)) - v), c = normalize(at(V2(3, 0)) - v) let opening = Set(pop.openAxes[0].movingFaces) let tip = step.movingFaceUnion let stage = reverseStage(name: "fixture", model: model, frame: 1, vertex: v, spine: s, crease: c, eye: step.viewpoint.eye, nearBody: opening.subtracting(tip), tipNear: opening.intersection(tip), tipFar: tip.subtracting(opening), lower: Set(model.faces.indices).subtracting(opening).subtracting(tip)) return (stage, v) } func popCase() throws { print("## The pop: per-face slerp (0002) against as-rigid-as-possible paths") print("") print("Delaunay mesh, h = 1/32 of the longer side. The opening and closing are rigid in every row; strain is the pop's.") print("") 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 |") print("|---|---|---|---|---|---|") let (fixture, fixtureVertex) = try fixtureStage() let crane = try craneStages() let v = craneFrame(V2(0, 0.47)) let headVertex = v + 0.33 * simd_quatd(angle: .pi, axis: (try neckFold(.raised, bendLine: true)).crease).act(-normalize(V3(1, 1, 0))) for (stage, vertex) in [(fixture, fixtureVertex), (crane.neck, v), (crane.tail, v), (crane.head, headVertex)] { let mesh = buildMesh(.delaunay, stage.model, spacing: stage.model.size / 32) for alpha in [170.0, 160.0, 150.0, 120.0] { var cells: [String] = [] for pop in [PopPath.slerp, .guided(fraction: 0.9), .spine, .banded(band: 1)] { let run = runStage(stage, mesh: mesh, alphaMax: alpha, collisionDepth: 1e-3 * stage.model.size / 32, pop: pop, vertex: vertex) cells.append("\(pct(run.popStrain)), \(pct(run.popPrincipal)), \(run.collisions.pop)" + (pop.isGuided ? "" : ", \(sci(run.popEndGap))")) } print("| \(stage.name) | \(Int(alpha))° | \(cells.joined(separator: " | ")) |") } } print("") } extension PopPath { var isGuided: Bool { if case .guided = self { true } else { false } } }