| @@ -15,17 +15,19 @@ private let popSamples = 64 |
| 15 | private let angleSamples = 256 |
15 | private let angleSamples = 256 |
| 16 | |
16 | |
| 17 | /// Decision 0002's open, pop and close for step `n`, whose script names the `spine` its flap opens |
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 |
18 | /// on, with decision 0003's opening. `before` is frame n − 1; `after` is frame n as the step's axes |
| 19 | /// record; `file` holds frames 0…n − 1. |
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 |
21 | /// The named crease is folded flat. The paper is cut along every edge on its line in `before`; |
| 22 | /// flap's layer nearer the viewpoint opens: the open axis turns the faces on that side of the |
22 | /// the spine is the edges of that line whose faces then fall on different sides, each folded flat |
| 23 | /// spine, from ±180 to ±`openAngle`. The pop and close move on a tree cut along the spine and the |
23 | /// or flat. A flat one is a bend line: it bends while the flap is open and is never creased. The |
| 24 | /// step's creases, grown from the faces neither the opening nor the step moves, spine first. In |
24 | /// side nearer the viewpoint opens: the open axis turns it about the line, from ±180 to |
| 25 | /// `popped` the spine is held at ±`openAngle`, reversed where both its faces move in the step, and |
25 | /// ±`openAngle`. The pop and close move on a tree cut along the spine and the step's creases, |
| 26 | /// the other tree axes are solved to keep the paper whole, starting from `openAngle / 180` of |
26 | /// grown from the faces neither the opening nor the step moves, spine first. In `popped` the spine |
| 27 | /// their way. The close axes drive the named spine edge back to ±180 and solve the rest, as for a |
27 | /// is held as the open phase leaves it, reversed where both its faces move in the step, and the |
| 28 | /// rigid step, and must arrive at `after`'s coordinates. |
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 | /// The starting guess can lead the solver to another branch of the vertex's motion. Closing the |
32 | /// The starting guess can lead the solver to another branch of the vertex's motion. Closing the |
| 31 | /// loops does not catch that: every branch is closed. A branch is caught when the close does not |
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 | let faceCount = file.facesVertices.count |
49 | let faceCount = file.facesVertices.count |
| 48 | let tolerance = sheetExtent(file) * relativeTolerance |
50 | let tolerance = sheetExtent(file) * relativeTolerance |
| 49 | let openAngle = script.openAngle ?? defaultOpenAngle |
51 | let openAngle = script.openAngle ?? defaultOpenAngle |
| |
52 | func foldedFlat(_ e: Int) -> Bool { abs(abs(before.edgesFoldAngle[e]) - 180) <= angleSnapTolerance } |
| 50 | |
53 | |
| 51 | guard let named = resolve(reference, file, edgeFaces) else { |
54 | guard let named = resolve(reference, file, edgeFaces) else { |
| 52 | throw CompileError.unresolvedCrease(step: n, crease: reference) |
55 | throw CompileError.unresolvedCrease(step: n, crease: reference) |
| 53 | } |
56 | } |
| 54 | let ends = file.edgesVertices[named].map { before.verticesCoords[$0] } |
57 | guard foldedFlat(named) else { throw CompileError.invalidSpine(step: n) } |
| 55 | let spine = [named] + edgeFaces.indices.filter { e in |
58 | let line = onLine(named, before, file, edgeFaces, tolerance) |
| 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 | } |
| |
| 62 | |
59 | |
| 63 | var group = Array(repeating: -1, count: faceCount) |
60 | var group = Array(repeating: -1, count: faceCount) |
| 64 | for (a, axis) in record.axes.enumerated() { |
61 | for (a, axis) in record.axes.enumerated() { |
| @@ -67,18 +64,26 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 67 | let moving = group.map { $0 >= 0 } |
64 | let moving = group.map { $0 >= 0 } |
| 68 | let creased = edgeFaces.indices.filter { edgeFaces[$0].count == 2 && group[edgeFaces[$0][0]] != group[edgeFaces[$0][1]] } |
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 | // the layer nearer the viewer opens: of the named edge's faces, the one a valley puts on the |
75 | // the layer nearer the viewer opens: of the named edge's faces, the one a valley puts on the |
| 71 | // first face's normal side, or a mountain on the other |
76 | // first face's normal side, or a mountain on the other |
| 72 | let (f, g) = (edgeFaces[named][0], edgeFaces[named][1]) |
77 | let (f, g) = (edgeFaces[named][0], edgeFaces[named][1]) |
| 73 | let normals = faceNormals(before, file) |
78 | let normals = faceNormals(before, file) |
| 74 | let gNearer = (before.edgesFoldAngle[named] > 0) == (dot(normals[f], record.viewpoint.eye) > 0) |
79 | let gNearer = (before.edgesFoldAngle[named] > 0) == (dot(normals[f], record.viewpoint.eye) > 0) |
| 75 | let (near, far) = gNearer ? (g, f) : (f, g) |
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 | guard sides[far] != sides[near] else { throw CompileError.noRigidMotion(step: n) } |
81 | guard sides[far] != sides[near] else { throw CompileError.noRigidMotion(step: n) } |
| |
82 | let opening = (0..<faceCount).filter { sides[$0] == sides[near] } |
| 79 | let openSign = before.edgesFoldAngle[named] > 0 ? 1.0 : -1.0 |
83 | let openSign = before.edgesFoldAngle[named] > 0 ? 1.0 : -1.0 |
| 80 | let openAxes = [AxisRecord(edge: named, movingFaces: opening, startAngle: before.edgesFoldAngle[named], |
84 | let openAxes = [AxisRecord(edge: named, movingFaces: opening, startAngle: before.edgesFoldAngle[named], |
| 81 | endAngle: openSign * openAngle, window: [0, phases[0]])] |
85 | endAngle: openSign * openAngle, window: [0, phases[0]])] |
| |
86 | let opened = try openEndAngles(n, openAxes, edges: spine, before: before, record, file) |
| 82 | |
87 | |
| 83 | // the tree the pop and the close move on |
88 | // the tree the pop and the close move on |
| 84 | let fixed = Set((0..<faceCount).filter { !moving[$0] && !opening.contains($0) }) |
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 | + creased.filter { e in !spine.contains(e) && !record.axes.contains { $0.edge == e } } |
92 | + creased.filter { e in !spine.contains(e) && !record.axes.contains { $0.edge == e } } |
| 88 | let tree = growTree(order: order, fixed: fixed, file, edgeFaces) |
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 |
95 | // popped: the spine held as the opening leaves it, reversed beyond the step's creases; the |
| 91 | func held(_ e: Int) -> Double { |
96 | // rest solved. The close returns the spine to where it was, reversed beyond the step's creases. |
| 92 | let sign = before.edgesFoldAngle[e] > 0 ? 1.0 : -1.0 |
97 | func held(_ e: Int) -> Double { tipSpine.contains(e) ? -opened[e]! : opened[e]! } |
| 93 | return (edgeFaces[e].allSatisfy { moving[$0] } ? -sign : sign) * openAngle |
| |
| 94 | } |
| |
| 95 | func closed(_ e: Int) -> Double { |
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 | var guess: [Int: Double] = [:] |
101 | var guess: [Int: Double] = [:] |
| 99 | for axis in tree.axes { |
102 | for axis in tree.axes { |
| @@ -123,6 +126,39 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after |
| 123 | return pop |
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 | /// `record`, flagged non-rigid, with `pop`. |
162 | /// `record`, flagged non-rigid, with `pop`. |
| 127 | func withPop(_ record: StepRecord, _ pop: PopRecord) -> StepRecord { |
163 | func withPop(_ record: StepRecord, _ pop: PopRecord) -> StepRecord { |
| 128 | StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, |
164 | StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, |