Commit 9d5135d823
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/FoldCompiler/FrameCompiler.swift +3
| @@ -43,6 +43,9 @@ public enum CompileError: Error, Equatable, Sendable { | ||
| 43 | 43 | /// An axis's angle leaves the range between its start angle and its end angle, or its peak |
| 44 | 44 | /// angle for a fold-and-unfold. |
| 45 | 45 | case angleOutsideTravel(step: Int, edge: Int) |
| 46 | /// A step's `openOn` names a crease that no earlier reverse fold holding the step's flap | |
| 47 | /// opened on. | |
| 48 | case noEarlierOpening(step: Int) | |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | /// Samples in a solved curve, evenly spaced over its window. |
Packages/FoldKit/Sources/FoldCompiler/PopCompiler.swift +5 −3
| @@ -32,7 +32,7 @@ private let angleSamples = 256 | ||
| 32 | 32 | /// open phase leaves them, the latter reversed, and the other tree axes are solved, starting where |
| 33 | 33 | /// the open phase leaves them or `openAngle / 180` of their way. The close axes drive the opening |
| 34 | 34 | /// crease back to ±180 and solve the rest, as for a rigid step, and must arrive at `after`'s |
| 35 | /// coordinates. | |
| 35 | /// coordinates. The record's `spine` holds the kept edges of both lines. | |
| 36 | 36 | /// |
| 37 | 37 | /// The starting guess can lead the solver to another branch of the vertex's motion. Closing the |
| 38 | 38 | /// loops does not catch that: every branch is closed. A branch is caught when the close does not |
| @@ -45,7 +45,8 @@ private let angleSamples = 256 | ||
| 45 | 45 | /// - Throws: `unresolvedCrease` when the spine or `openOn` names no edge, `invalidSpine` when one |
| 46 | 46 | /// of them is not folded flat or a kept edge is neither folded flat nor flat, `noRigidMotion` |
| 47 | 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%. | |
| 48 | /// `popTooStrained` when the pop's strain exceeds 10%, `noEarlierOpening` when no earlier reverse | |
| 49 | /// fold holding the flap opened on `openOn`. | |
| 49 | 50 | public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after: FoldFrame, |
| 50 | 51 | file: FoldFile) throws -> PopRecord { |
| 51 | 52 | guard let reference = script.spine, let record = after.step else { |
| @@ -76,6 +77,7 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after | ||
| 76 | 77 | let earlier = script.openOn == nil ? nil : (1..<n).reversed().lazy.compactMap { file.frames[$0].step }.first { s in |
| 77 | 78 | s.nonRigid && s.pop?.spine.contains(opener) == true && tip.isSubset(of: s.axes.flatMap(\.movingFaces)) |
| 78 | 79 | } |
| 80 | if script.openOn != nil && earlier == nil { throw CompileError.noEarlierOpening(step: n) } | |
| 79 | 81 | let earlierFlap = Set(earlier?.axes.flatMap(\.movingFaces) ?? []) |
| 80 | 82 | let earlierCreases = edgeFaces.indices.filter { e in |
| 81 | 83 | edgeFaces[e].count == 2 && earlierFlap.contains(edgeFaces[e][0]) != earlierFlap.contains(edgeFaces[e][1]) |
| @@ -132,7 +134,7 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after | ||
| 132 | 134 | poppedFrame.verticesCoords = popped.positions |
| 133 | 135 | |
| 134 | 136 | let closeAxes = try closing(n, tree, from: poppedFrame, start: popped.angles, end: closed, record, file) |
| 135 | let pop = PopRecord(spine: spine, openAngle: openAngle, phases: phases, openAxes: openAxes, | |
| 137 | let pop = PopRecord(spine: unique(spine + openLine), openAngle: openAngle, phases: phases, openAxes: openAxes, | |
| 136 | 138 | popped: popped.positions, closeAxes: closeAxes) |
| 137 | 139 | var draft = file |
| 138 | 140 | draft.frames.append(FoldFrame(verticesCoords: after.verticesCoords, edgesAssignment: after.edgesAssignment, |
Packages/FoldKit/Sources/FoldFormat/StepRecord.swift +3 −1
| @@ -51,7 +51,9 @@ public struct Viewpoint: Codable, Equatable, Sendable { | ||
| 51 | 51 | /// The open, pop and close of a non-rigid step: `openAxes` open the flap on `[0, phases[0]]`, the |
| 52 | 52 | /// first turning the opening crease to `openAngle` (the `spine`, or for a nested opening an |
| 53 | 53 | /// earlier fold's spine), every vertex reaches `popped` by `phases[1]`, and `closeAxes`, relative |
| 54 | /// to `popped`, close the flap on `[phases[1], 1]`. | |
| 54 | /// to `popped`, close the flap on `[phases[1], 1]`. `spine` lists the kept edges of the spine's | |
| 55 | /// line and, for a nested opening, of the opening crease's line: the edges the open and close turn | |
| 56 | /// and return, which the pop does not crease. | |
| 55 | 57 | public struct PopRecord: Codable, Equatable, Sendable { |
| 56 | 58 | public let spine: [Int] |
| 57 | 59 | public let openAngle: Double |
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/reverse-nested-bend-cp.fold added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | { | |
| 2 | "file_spec": 1.2, | |
| 3 | "file_title": "Nested reverse folds with a bend line", | |
| 4 | "file_author": "Traditional", | |
| 5 | "file_classes": ["creasePattern"], | |
| 6 | "vertices_coords": [[0, 0], [3, 0], [4, 0], [4, 1], [4, 2], [3, 2], [1, 2], [0, 2], [0, 1], [2, 1], | |
| 7 | [3.5, 1], [4, 0.5], [4, 1.5], [0, 3], [1, 3], [0, 4], [1, 4]], | |
| 8 | "edges_vertices": [[0, 1], [1, 2], [2, 11], [11, 3], [3, 12], [12, 4], [4, 5], [5, 6], [6, 14], [14, 16], | |
| 9 | [16, 15], [15, 13], [13, 7], [7, 8], [8, 0], | |
| 10 | [8, 9], [9, 10], [10, 3], [9, 1], [9, 5], [10, 11], [10, 12], [7, 6], [13, 14]], | |
| 11 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", | |
| 12 | "B", "B", "B", "B", "B", | |
| 13 | "V", "M", "V", "V", "V", "M", "M", "M", "F"], | |
| 14 | "faces_vertices": [[0, 1, 9, 8], [1, 2, 11, 10, 9], [11, 3, 10], [10, 3, 12], [9, 10, 12, 4, 5], | |
| 15 | [8, 9, 5, 6, 7], [7, 6, 14, 13], [13, 14, 16, 15]] | |
| 16 | } | |
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/reverse-nested-bend.foldscript added +37
| @@ -0,0 +1,37 @@ | ||
| 1 | { | |
| 2 | "creasePattern": "reverse-nested-bend-cp.fold", | |
| 3 | "title": "Nested reverse folds with a bend line", | |
| 4 | "steps": [ | |
| 5 | { | |
| 6 | "kind": "fold", | |
| 7 | "instruction": "Valley fold the top half down onto the bottom half.", | |
| 8 | "creases": [[[0, 1], [2, 1]]], | |
| 9 | "movingSide": [[1, 1.5]] | |
| 10 | }, | |
| 11 | { | |
| 12 | "kind": "fold", | |
| 13 | "instruction": "Fold the tab up in front, across the top edge.", | |
| 14 | "creases": [[[0, 2], [1, 2]]], | |
| 15 | "movingSide": [[0.5, 3]] | |
| 16 | }, | |
| 17 | { | |
| 18 | "kind": "fold", | |
| 19 | "technique": "insideReverse", | |
| 20 | "instruction": "Inside reverse fold the right end down between the layers.", | |
| 21 | "creases": [[[2, 1], [3, 0]], [[2, 1], [3, 2]]], | |
| 22 | "movingSide": [[3.2, 0.5]], | |
| 23 | "spine": [[0, 1], [2, 1]], | |
| 24 | "faceOrders": [{"above": [3.2, 1.5], "below": [3.2, 0.5]}, {"above": [3.9, 1.1], "below": [3.9, 0.9]}] | |
| 25 | }, | |
| 26 | { | |
| 27 | "kind": "fold", | |
| 28 | "technique": "insideReverse", | |
| 29 | "instruction": "Inside reverse fold the end of the flap up between its layers.", | |
| 30 | "creases": [[[3.5, 1], [4, 0.5]], [[3.5, 1], [4, 1.5]]], | |
| 31 | "movingSide": [[3.9, 0.9]], | |
| 32 | "spine": [[3.5, 1], [4, 1]], | |
| 33 | "openOn": [[0, 1], [2, 1]], | |
| 34 | "faceOrders": [{"above": [3.9, 1.1], "below": [3.9, 0.9]}] | |
| 35 | } | |
| 36 | ] | |
| 37 | } | |
Packages/FoldKit/Tests/FoldCompilerTests/NonRigidTests.swift +21
| @@ -144,6 +144,27 @@ import FoldKitTestSupport | ||
| 144 | 144 | #expect(layerMotionDefects(file).isEmpty) |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | @Test func aNestedOpeningLeavesABendLineOnItsOpeningCreaseUncreased() throws { | |
| 148 | // the head reopens on the body's spine, whose line holds the tab's bend line (edge 23) | |
| 149 | let file = try compiled("reverse-nested-bend") | |
| 150 | let bend = 23 | |
| 151 | #expect(file.edgesFirstCreasedAtStep[bend] == nil) | |
| 152 | #expect(file.frames.allSatisfy { $0.edgesFoldAngle[bend] == 0 && $0.edgesAssignment[bend] == .flat }) | |
| 153 | #expect(try readSteps(file)[3].pop?.spine.contains(bend) == true) | |
| 154 | #expect(validate(file).isEmpty) | |
| 155 | } | |
| 156 | ||
| 157 | @Test func anOpeningCreaseNoEarlierFoldOpenedIsRejected() throws { | |
| 158 | let script = try FoldScript(contentsOf: fixture("reverse.foldscript")) | |
| 159 | let s = script.steps[1] | |
| 160 | let reopened = ScriptStep(kind: s.kind, technique: s.technique, instruction: s.instruction, creases: s.creases, | |
| 161 | movingSide: s.movingSide, faceOrders: s.faceOrders, spine: s.spine, openOn: s.spine) | |
| 162 | #expect(throws: CompileError.noEarlierOpening(step: 2)) { | |
| 163 | try compile(FoldScript(creasePattern: script.creasePattern, title: script.title, steps: [script.steps[0], reopened]), | |
| 164 | pattern: FoldFile(contentsOf: fixture("reverse-cp.fold"))) | |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 147 | 168 | @Test func theReverseFoldMovesThroughNoStillFace() throws { |
| 148 | 169 | // the open phase departs from frame 1 and the close phase arrives at frame 2 |
| 149 | 170 | #expect(layerMotionDefects(try compiled("reverse")).isEmpty) |