| @@ -18,28 +18,34 @@ private let angleSamples = 256 |
| 18 | 18 | /// on, with decision 0003's opening. `before` is frame n − 1; `after` is frame n as the step's axes |
| 19 | 19 | /// place it, carrying the step's record; `file` holds frames 0…n − 1. |
| 20 | 20 | /// |
| 21 | | /// The named crease is folded flat. The paper is cut along every edge on its line in `before`; |
| 22 | | /// the spine is the edges of that line whose faces then fall on different sides, each folded flat |
| 23 | | /// or flat. A flat one is a bend line: it bends while the flap is open and is never creased. The |
| 24 | | /// side nearer the viewpoint opens: the open axis turns it about the line, from ±180 to |
| 25 | | /// ±`openAngle`. The pop and close move on a tree cut along the spine and the step's creases, |
| 26 | | /// grown from the faces neither the opening nor the step moves, spine first. In `popped` the spine |
| 27 | | /// is held as the open phase leaves it, reversed where both its faces move in the step, and the |
| 28 | | /// other tree axes are solved to keep the paper whole, starting from `openAngle / 180` of their |
| 29 | | /// way. The close axes drive the named spine edge back to ±180 and solve the rest, as for a rigid |
| 30 | | /// step, and must arrive at `after`'s coordinates. |
| 21 | /// The flap opens on the opening crease: `openOn`, or the named spine crease. Both are folded |
| 22 | /// flat. The paper is cut along every edge on either crease's line in `before`, and, with |
| 23 | /// `openOn`, along the creases of the earlier reverse fold that opened on `openOn` and whose flap |
| 24 | /// holds this step's. Of each line, the edges whose faces then fall on different sides are kept, |
| 25 | /// each folded flat or flat; a flat one is a bend line, which bends while the flap is open and is |
| 26 | /// never creased. The side of the opening crease nearer the viewpoint opens, with the earlier |
| 27 | /// fold's flap: the open axes are a tree grown from the other faces across the kept edges, the |
| 28 | /// opening crease first and driven from ±180 to ±`openAngle`, the earlier fold's creases and the |
| 29 | /// spine solved to keep the paper whole. The pop and close move on a tree cut along those edges and |
| 30 | /// the step's creases, grown from the faces neither the opening nor the step moves, the opening |
| 31 | /// crease first. In `popped` the opening crease and the spine's edges in the flap are held as the |
| 32 | /// open phase leaves them, the latter reversed, and the other tree axes are solved, starting where |
| 33 | /// the open phase leaves them or `openAngle / 180` of their way. The close axes drive the opening |
| 34 | /// crease back to ±180 and solve the rest, as for a rigid step, and must arrive at `after`'s |
| 35 | /// coordinates. |
| 31 | 36 | /// |
| 32 | 37 | /// The starting guess can lead the solver to another branch of the vertex's motion. Closing the |
| 33 | 38 | /// loops does not catch that: every branch is closed. A branch is caught when the close does not |
| 34 | 39 | /// reach `after`'s coordinates, when it leaves a crease of the last frame assigned against the |
| 35 | | /// crease pattern, or when frame n breaks the validator's layer-order rules; the reverse fixture's |
| 36 | | /// test that the tip lands between the flap's layers covers that fixture. `layerMotionDefects` |
| 37 | | /// does not add a check here: frame n's orders for the faces the step's axes move are guessed |
| 38 | | /// from the same close, read near the same moment. |
| 40 | /// crease pattern, or when frame n breaks the validator's layer-order rules; the reverse fixtures' |
| 41 | /// tests that the tip lands between the flap's layers cover them. `layerMotionDefects` does not |
| 42 | /// add a check here: frame n's orders for the faces the step's axes move are guessed from the same |
| 43 | /// close, read near the same moment. |
| 39 | 44 | /// |
| 40 | | /// - Throws: `unresolvedCrease` when the spine names no edge, `invalidSpine` when it is not |
| 41 | | /// folded flat, `noRigidMotion` when the flap cannot open on its spine, `popped` does not close |
| 42 | | /// or the close does not reach `after`, and `popTooStrained` when the pop's strain exceeds 10%. |
| 45 | /// - Throws: `unresolvedCrease` when the spine or `openOn` names no edge, `invalidSpine` when one |
| 46 | /// of them is not folded flat or a kept edge is neither folded flat nor flat, `noRigidMotion` |
| 47 | /// when the flap cannot open, `popped` does not close or the close does not reach `after`, and |
| 48 | /// `popTooStrained` when the pop's strain exceeds 10%. |
| 43 | 49 | public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after: FoldFrame, |
| 44 | 50 | file: FoldFile) throws -> PopRecord { |
| 45 | 51 | guard let reference = script.spine, let record = after.step else { |
| @@ -50,12 +56,13 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 50 | 56 | let tolerance = sheetExtent(file) * relativeTolerance |
| 51 | 57 | let openAngle = script.openAngle ?? defaultOpenAngle |
| 52 | 58 | func foldedFlat(_ e: Int) -> Bool { abs(abs(before.edgesFoldAngle[e]) - 180) <= angleSnapTolerance } |
| 53 | | |
| 54 | | guard let named = resolve(reference, file, edgeFaces) else { |
| 55 | | throw CompileError.unresolvedCrease(step: n, crease: reference) |
| 59 | func edge(_ ref: CreaseRef) throws -> Int { |
| 60 | guard let e = resolve(ref, file, edgeFaces) else { throw CompileError.unresolvedCrease(step: n, crease: ref) } |
| 61 | guard foldedFlat(e) else { throw CompileError.invalidSpine(step: n) } |
| 62 | return e |
| 56 | 63 | } |
| 57 | | guard foldedFlat(named) else { throw CompileError.invalidSpine(step: n) } |
| 58 | | let line = onLine(named, before, file, edgeFaces, tolerance) |
| 64 | let named = try edge(reference) |
| 65 | let opener = try script.openOn.map(edge) ?? named |
| 59 | 66 | |
| 60 | 67 | var group = Array(repeating: -1, count: faceCount) |
| 61 | 68 | for (a, axis) in record.axes.enumerated() { |
| @@ -64,47 +71,63 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 64 | 71 | let moving = group.map { $0 >= 0 } |
| 65 | 72 | let creased = edgeFaces.indices.filter { edgeFaces[$0].count == 2 && group[edgeFaces[$0][0]] != group[edgeFaces[$0][1]] } |
| 66 | 73 | |
| 67 | | // edges on the line whose faces stay connected without it lie within one side |
| 68 | | let sides = regions(cut: Set(line), file, edgeFaces) |
| 69 | | let spine = line.filter { sides[edgeFaces[$0][0]] != sides[edgeFaces[$0][1]] } |
| 70 | | guard spine.allSatisfy({ foldedFlat($0) || abs(before.edgesFoldAngle[$0]) <= angleSnapTolerance }) else { |
| 74 | // the earlier reverse fold this step's flap is in, which opened on the opening crease |
| 75 | let tip = Set((0..<faceCount).filter { moving[$0] }) |
| 76 | let earlier = script.openOn == nil ? nil : (1..<n).reversed().lazy.compactMap { file.frames[$0].step }.first { s in |
| 77 | s.nonRigid && s.pop?.spine.contains(opener) == true && tip.isSubset(of: s.axes.flatMap(\.movingFaces)) |
| 78 | } |
| 79 | let earlierFlap = Set(earlier?.axes.flatMap(\.movingFaces) ?? []) |
| 80 | let earlierCreases = edgeFaces.indices.filter { e in |
| 81 | edgeFaces[e].count == 2 && earlierFlap.contains(edgeFaces[e][0]) != earlierFlap.contains(edgeFaces[e][1]) |
| 82 | } |
| 83 | |
| 84 | // edges on a line whose faces stay connected without it lie within one side |
| 85 | let openerLine = onLine(opener, before, file, edgeFaces, tolerance) |
| 86 | let spineLine = onLine(named, before, file, edgeFaces, tolerance) |
| 87 | let sides = regions(cut: Set(openerLine + earlierCreases + spineLine), file, edgeFaces) |
| 88 | func across(_ e: Int) -> Bool { sides[edgeFaces[e][0]] != sides[edgeFaces[e][1]] } |
| 89 | let openLine = openerLine.filter(across) |
| 90 | let spine = spineLine.filter(across) |
| 91 | guard (openLine + spine).allSatisfy({ foldedFlat($0) || abs(before.edgesFoldAngle[$0]) <= angleSnapTolerance }) else { |
| 71 | 92 | throw CompileError.invalidSpine(step: n) |
| 72 | 93 | } |
| 73 | | let tipSpine = Set(spine.filter { edgeFaces[$0].allSatisfy { moving[$0] } }) |
| 94 | let tipSpine = Set(spine.filter { edgeFaces[$0].allSatisfy(tip.contains) }) |
| 74 | 95 | |
| 75 | | // the layer nearer the viewer opens: of the named edge's faces, the one a valley puts on the |
| 76 | | // first face's normal side, or a mountain on the other |
| 77 | | let (f, g) = (edgeFaces[named][0], edgeFaces[named][1]) |
| 96 | // the layer nearer the viewer opens: of the opening crease's faces, the one a valley puts on |
| 97 | // the first face's normal side, or a mountain on the other |
| 98 | let (f, g) = (edgeFaces[opener][0], edgeFaces[opener][1]) |
| 78 | 99 | let normals = faceNormals(before, file) |
| 79 | | let gNearer = (before.edgesFoldAngle[named] > 0) == (dot(normals[f], record.viewpoint.eye) > 0) |
| 100 | let gNearer = (before.edgesFoldAngle[opener] > 0) == (dot(normals[f], record.viewpoint.eye) > 0) |
| 80 | 101 | let (near, far) = gNearer ? (g, f) : (f, g) |
| 81 | | guard sides[far] != sides[near] else { throw CompileError.noRigidMotion(step: n) } |
| 82 | | let opening = (0..<faceCount).filter { sides[$0] == sides[near] } |
| 83 | | let openSign = before.edgesFoldAngle[named] > 0 ? 1.0 : -1.0 |
| 84 | | let openAxes = [AxisRecord(edge: named, movingFaces: opening, startAngle: before.edgesFoldAngle[named], |
| 85 | | endAngle: openSign * openAngle, window: [0, phases[0]])] |
| 86 | | let opened = try openEndAngles(n, openAxes, edges: spine, before: before, record, file) |
| 102 | guard sides[far] != sides[near], !earlierFlap.contains(far) else { throw CompileError.noRigidMotion(step: n) } |
| 103 | let opening = Set((0..<faceCount).filter { sides[$0] == sides[near] || earlierFlap.contains($0) }) |
| 104 | let openEdges = unique(openLine + earlierCreases + spine) |
| 105 | let openTree = growTree(order: openEdges, fixed: Set((0..<faceCount).filter { !opening.contains($0) }), |
| 106 | file, edgeFaces) |
| 107 | guard openTree.axes.first?.edge == opener else { throw CompileError.noRigidMotion(step: n) } |
| 108 | let openSign = before.edgesFoldAngle[opener] > 0 ? 1.0 : -1.0 |
| 109 | let openAxes = try openingAxes(n, openTree, end: openSign * openAngle, before: before, record, file) |
| 110 | let opened = try openEndAngles(n, openAxes, edges: openEdges, before: before, record, file) |
| 87 | 111 | |
| 88 | 112 | // the tree the pop and the close move on |
| 89 | 113 | let fixed = Set((0..<faceCount).filter { !moving[$0] && !opening.contains($0) }) |
| 90 | 114 | guard !fixed.isEmpty else { throw CompileError.noRigidMotion(step: n) } |
| 91 | | let order = spine + record.axes.map(\.edge).filter { !spine.contains($0) } |
| 92 | | + creased.filter { e in !spine.contains(e) && !record.axes.contains { $0.edge == e } } |
| 115 | let order = unique(openEdges + record.axes.map(\.edge) + creased) |
| 93 | 116 | let tree = growTree(order: order, fixed: fixed, file, edgeFaces) |
| 94 | 117 | |
| 95 | | // popped: the spine held as the opening leaves it, reversed beyond the step's creases; the |
| 96 | | // rest solved. The close returns the spine to where it was, reversed beyond the step's creases. |
| 97 | | func held(_ e: Int) -> Double { tipSpine.contains(e) ? -opened[e]! : opened[e]! } |
| 118 | // popped: the open edges as the opening leaves them, the spine reversed in the flap; the rest |
| 119 | // solved. The close returns the open edges to where they were, the spine reversed in the flap. |
| 98 | 120 | func closed(_ e: Int) -> Double { |
| 99 | | spine.contains(e) ? (tipSpine.contains(e) ? -1 : 1) * before.edgesFoldAngle[e] : after.edgesFoldAngle[e] |
| 121 | opened[e] == nil ? after.edgesFoldAngle[e] : (tipSpine.contains(e) ? -1 : 1) * before.edgesFoldAngle[e] |
| 100 | 122 | } |
| 101 | 123 | var guess: [Int: Double] = [:] |
| 102 | 124 | for axis in tree.axes { |
| 103 | 125 | let start = before.edgesFoldAngle[axis.edge] |
| 104 | | guess[axis.edge] = spine.contains(axis.edge) ? held(axis.edge) |
| 105 | | : start + (closed(axis.edge) - start) * openAngle / 180 |
| 126 | guess[axis.edge] = opened[axis.edge].map { tipSpine.contains(axis.edge) ? -$0 : $0 } |
| 127 | ?? start + (closed(axis.edge) - start) * openAngle / 180 |
| 106 | 128 | } |
| 107 | | let popped = try solvedPose(n, tree, from: before, angles: guess, holding: Set(spine), record, file) |
| 129 | let popped = try solvedPose(n, tree, from: before, angles: guess, holding: Set(openLine).union(tipSpine), |
| 130 | record, file) |
| 108 | 131 | var poppedFrame = before |
| 109 | 132 | poppedFrame.verticesCoords = popped.positions |
| 110 | 133 | |
| @@ -126,6 +149,53 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 126 | 149 | return pop |
| 127 | 150 | } |
| 128 | 151 | |
| 152 | /// `edges` without repeats, in order. |
| 153 | private func unique(_ edges: [Int]) -> [Int] { |
| 154 | var seen = Set<Int>() |
| 155 | return edges.filter { seen.insert($0).inserted } |
| 156 | } |
| 157 | |
| 158 | /// The open axes on `[0, phases[0]]`: the tree's first axis, the opening crease, linear from its |
| 159 | /// angle in `before` to `end`; the axes on a loop's cycle solved so the loops close, continued |
| 160 | /// from `before` along the window to find where they end and then solved as for a rigid step; the |
| 161 | /// rest held. |
| 162 | private func openingAxes(_ n: Int, _ tree: RegionTree, end: Double, before: FoldFrame, _ record: StepRecord, |
| 163 | _ file: FoldFile) throws -> [AxisRecord] { |
| 164 | let opener = tree.axes[0].edge |
| 165 | func draft(_ ends: [Int: Double]) -> (StepRecord, FoldFile) { |
| 166 | let axes = records(tree, start: { before.edgesFoldAngle[$0] }, end: { ends[$0] ?? before.edgesFoldAngle[$0] }, |
| 167 | window: [0, phases[0]]) |
| 168 | let open = withAxes(record, axes) |
| 169 | var draft = file |
| 170 | draft.frames.append(FoldFrame(verticesCoords: before.verticesCoords, edgesAssignment: before.edgesAssignment, |
| 171 | edgesFoldAngle: before.edgesFoldAngle, faceOrders: [], step: open)) |
| 172 | return (open, draft) |
| 173 | } |
| 174 | let (held, heldFile) = draft([opener: end]) |
| 175 | guard !tree.loops.isEmpty else { return held.axes } |
| 176 | let step = try readStep(n, in: heldFile) |
| 177 | let driver = step.axes.firstIndex { $0.edge == opener }! |
| 178 | let free = axesOnCycles(step, loops: tree.loops, heldFile).subtracting([driver]).sorted() |
| 179 | guard !free.isEmpty else { return held.axes } |
| 180 | |
| 181 | let kinematics = LoopKinematics(step: step, file: heldFile) |
| 182 | let tolerance = sheetExtent(file) * relativeTolerance |
| 183 | var previous = step.axes.map(\.startAngle), angles = previous |
| 184 | for k in 1...angleSamples { |
| 185 | var guess = zip(angles, previous).map { 2 * $0 - $1 } |
| 186 | guess[driver] = step.axes[driver].startAngle + Double(k) / Double(angleSamples) * (end - step.axes[driver].startAngle) |
| 187 | guard kinematics.solve(&guess, free: free) <= tolerance else { throw CompileError.noRigidMotion(step: n) } |
| 188 | (previous, angles) = (angles, guess) |
| 189 | } |
| 190 | let (open, openFile) = draft(Dictionary(uniqueKeysWithValues: zip(step.axes.map(\.edge), angles))) |
| 191 | let solved = try solveLoops(n, open, try readStep(n, in: openFile), loops: tree.loops, openFile) |
| 192 | var solvedFile = openFile |
| 193 | solvedFile.frames[n].step = solved |
| 194 | try checkClosure(n, try readStep(n, in: solvedFile), driver: opener, solvedFile, |
| 195 | solved: solved.axes.contains { $0.angles != nil }) |
| 196 | return solved.axes |
| 197 | } |
| 198 | |
| 129 | 199 | /// `named` and every other interior edge on its line in `frame`. |
| 130 | 200 | private func onLine(_ named: Int, _ frame: FoldFrame, _ file: FoldFile, _ edgeFaces: [[Int]], |
| 131 | 201 | _ tolerance: Double) -> [Int] { |