| @@ -15,17 +15,19 @@ private let popSamples = 64 |
| 15 | 15 | private let angleSamples = 256 |
| 16 | 16 | |
| 17 | 17 | /// Decision 0002's open, pop and close for step `n`, whose script names the `spine` its flap opens |
| 18 | | /// on. `before` is frame n − 1; `after` is frame n as the step's axes place it, carrying the step's |
| 19 | | /// record; `file` holds frames 0…n − 1. |
| 18 | /// on, with decision 0003's opening. `before` is frame n − 1; `after` is frame n as the step's axes |
| 19 | /// place it, carrying the step's record; `file` holds frames 0…n − 1. |
| 20 | 20 | /// |
| 21 | | /// The spine is the named crease and every edge on its line in `before`, each folded flat. The |
| 22 | | /// flap's layer nearer the viewpoint opens: the open axis turns the faces on that side of the |
| 23 | | /// spine, from ±180 to ±`openAngle`. The pop and close move on a tree cut along the spine and the |
| 24 | | /// step's creases, grown from the faces neither the opening nor the step moves, spine first. In |
| 25 | | /// `popped` the spine is held at ±`openAngle`, reversed where both its faces move in the step, and |
| 26 | | /// the other tree axes are solved to keep the paper whole, starting from `openAngle / 180` of |
| 27 | | /// their way. The close axes drive the named spine edge back to ±180 and solve the rest, as for a |
| 28 | | /// rigid step, and must arrive at `after`'s coordinates. |
| 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. |
| 29 | 31 | /// |
| 30 | 32 | /// The starting guess can lead the solver to another branch of the vertex's motion. Closing the |
| 31 | 33 | /// loops does not catch that: every branch is closed. A branch is caught when the close does not |
| @@ -47,18 +49,13 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 47 | 49 | let faceCount = file.facesVertices.count |
| 48 | 50 | let tolerance = sheetExtent(file) * relativeTolerance |
| 49 | 51 | let openAngle = script.openAngle ?? defaultOpenAngle |
| 52 | func foldedFlat(_ e: Int) -> Bool { abs(abs(before.edgesFoldAngle[e]) - 180) <= angleSnapTolerance } |
| 50 | 53 | |
| 51 | 54 | guard let named = resolve(reference, file, edgeFaces) else { |
| 52 | 55 | throw CompileError.unresolvedCrease(step: n, crease: reference) |
| 53 | 56 | } |
| 54 | | let ends = file.edgesVertices[named].map { before.verticesCoords[$0] } |
| 55 | | let spine = [named] + edgeFaces.indices.filter { e in |
| 56 | | e != named && edgeFaces[e].count == 2 |
| 57 | | && file.edgesVertices[e].allSatisfy { onLine(before.verticesCoords[$0], ends[0], ends[1], tolerance) } |
| 58 | | } |
| 59 | | guard spine.allSatisfy({ abs(abs(before.edgesFoldAngle[$0]) - 180) <= angleSnapTolerance }) else { |
| 60 | | throw CompileError.invalidSpine(step: n) |
| 61 | | } |
| 57 | guard foldedFlat(named) else { throw CompileError.invalidSpine(step: n) } |
| 58 | let line = onLine(named, before, file, edgeFaces, tolerance) |
| 62 | 59 | |
| 63 | 60 | var group = Array(repeating: -1, count: faceCount) |
| 64 | 61 | for (a, axis) in record.axes.enumerated() { |
| @@ -67,18 +64,26 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 67 | 64 | let moving = group.map { $0 >= 0 } |
| 68 | 65 | let creased = edgeFaces.indices.filter { edgeFaces[$0].count == 2 && group[edgeFaces[$0][0]] != group[edgeFaces[$0][1]] } |
| 69 | 66 | |
| 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 { |
| 71 | throw CompileError.invalidSpine(step: n) |
| 72 | } |
| 73 | let tipSpine = Set(spine.filter { edgeFaces[$0].allSatisfy { moving[$0] } }) |
| 74 | |
| 70 | 75 | // the layer nearer the viewer opens: of the named edge's faces, the one a valley puts on the |
| 71 | 76 | // first face's normal side, or a mountain on the other |
| 72 | 77 | let (f, g) = (edgeFaces[named][0], edgeFaces[named][1]) |
| 73 | 78 | let normals = faceNormals(before, file) |
| 74 | 79 | let gNearer = (before.edgesFoldAngle[named] > 0) == (dot(normals[f], record.viewpoint.eye) > 0) |
| 75 | 80 | let (near, far) = gNearer ? (g, f) : (f, g) |
| 76 | | let sides = regions(cut: Set(spine), file, edgeFaces) |
| 77 | | let opening = (0..<faceCount).filter { sides[$0] == sides[near] } |
| 78 | 81 | guard sides[far] != sides[near] else { throw CompileError.noRigidMotion(step: n) } |
| 82 | let opening = (0..<faceCount).filter { sides[$0] == sides[near] } |
| 79 | 83 | let openSign = before.edgesFoldAngle[named] > 0 ? 1.0 : -1.0 |
| 80 | 84 | let openAxes = [AxisRecord(edge: named, movingFaces: opening, startAngle: before.edgesFoldAngle[named], |
| 81 | 85 | endAngle: openSign * openAngle, window: [0, phases[0]])] |
| 86 | let opened = try openEndAngles(n, openAxes, edges: spine, before: before, record, file) |
| 82 | 87 | |
| 83 | 88 | // the tree the pop and the close move on |
| 84 | 89 | let fixed = Set((0..<faceCount).filter { !moving[$0] && !opening.contains($0) }) |
| @@ -87,13 +92,11 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 87 | 92 | + creased.filter { e in !spine.contains(e) && !record.axes.contains { $0.edge == e } } |
| 88 | 93 | let tree = growTree(order: order, fixed: fixed, file, edgeFaces) |
| 89 | 94 | |
| 90 | | // popped: the spine held open, reversed beyond the step's creases; the rest solved |
| 91 | | func held(_ e: Int) -> Double { |
| 92 | | let sign = before.edgesFoldAngle[e] > 0 ? 1.0 : -1.0 |
| 93 | | return (edgeFaces[e].allSatisfy { moving[$0] } ? -sign : sign) * openAngle |
| 94 | | } |
| 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]! } |
| 95 | 98 | func closed(_ e: Int) -> Double { |
| 96 | | spine.contains(e) ? (held(e) > 0 ? 180 : -180) : after.edgesFoldAngle[e] |
| 99 | spine.contains(e) ? (tipSpine.contains(e) ? -1 : 1) * before.edgesFoldAngle[e] : after.edgesFoldAngle[e] |
| 97 | 100 | } |
| 98 | 101 | var guess: [Int: Double] = [:] |
| 99 | 102 | for axis in tree.axes { |
| @@ -123,6 +126,39 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 123 | 126 | return pop |
| 124 | 127 | } |
| 125 | 128 | |
| 129 | /// `named` and every other interior edge on its line in `frame`. |
| 130 | private func onLine(_ named: Int, _ frame: FoldFrame, _ file: FoldFile, _ edgeFaces: [[Int]], |
| 131 | _ tolerance: Double) -> [Int] { |
| 132 | let ends = file.edgesVertices[named].map { frame.verticesCoords[$0] } |
| 133 | return [named] + edgeFaces.indices.filter { e in |
| 134 | e != named && edgeFaces[e].count == 2 |
| 135 | && file.edgesVertices[e].allSatisfy { onLine(frame.verticesCoords[$0], ends[0], ends[1], tolerance) } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// The fold angle of each of `edges` where `openAxes` leave the paper at the end of the open |
| 140 | /// phase, measured from the faces' normals along it and unwrapped from `before`'s angle. |
| 141 | private func openEndAngles(_ n: Int, _ openAxes: [AxisRecord], edges: [Int], before: FoldFrame, |
| 142 | _ record: StepRecord, _ file: FoldFile) throws -> [Int: Double] { |
| 143 | var draft = file |
| 144 | draft.frames.append(FoldFrame(verticesCoords: before.verticesCoords, edgesAssignment: before.edgesAssignment, |
| 145 | edgesFoldAngle: before.edgesFoldAngle, faceOrders: [], |
| 146 | step: withAxes(record, openAxes))) |
| 147 | let interpolator = FoldInterpolator(file: draft, step: try readStep(n, in: draft)) |
| 148 | let edgeFaces = edgesFaces(file) |
| 149 | var current = Dictionary(uniqueKeysWithValues: edges.map { ($0, before.edgesFoldAngle[$0]) }) |
| 150 | for k in 1...angleSamples { |
| 151 | let positions = interpolator.positions(at: phases[0] * Double(k) / Double(angleSamples)) |
| 152 | let normals = faceNormals(positions: positions, faces: file.facesVertices) |
| 153 | for e in edges { |
| 154 | let measured = dihedral(e, edgeFaces[e], positions, normals, file) |
| 155 | current[e] = measured + 360 * ((current[e]! - measured) / 360).rounded() |
| 156 | } |
| 157 | } |
| 158 | for axis in openAxes { current[axis.edge] = axis.endAngle } |
| 159 | return current |
| 160 | } |
| 161 | |
| 126 | 162 | /// `record`, flagged non-rigid, with `pop`. |
| 127 | 163 | func withPop(_ record: StepRecord, _ pop: PopRecord) -> StepRecord { |
| 128 | 164 | StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, |