Commit 9d5135d823

9d5135d8237d87f9e0471ff92ff4ff1d4b2a3f4b

parent: 59dbd0686d

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 21:31 UTC

Record a nested opening's crease line in the pop's spine

A bend line on the opening crease's line is turned and returned by the
open and close, so the validator no longer counts it as creased. An
openOn that no earlier reverse fold opened on is rejected with
noEarlierOpening.

Ref #7

Layout: unified · split

Packages/FoldKit/Sources/FoldCompiler/FrameCompiler.swift +3
@@ -43,6 +43,9 @@ public enum CompileError: Error, Equatable, Sendable {
4343 /// An axis's angle leaves the range between its start angle and its end angle, or its peak
4444 /// angle for a fold-and-unfold.
4545 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)
4649}
4750
4851/// 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
3232/// open phase leaves them, the latter reversed, and the other tree axes are solved, starting where
3333/// the open phase leaves them or `openAngle / 180` of their way. The close axes drive the opening
3434/// 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.
3636///
3737/// The starting guess can lead the solver to another branch of the vertex's motion. Closing the
3838/// 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
4545/// - Throws: `unresolvedCrease` when the spine or `openOn` names no edge, `invalidSpine` when one
4646/// of them is not folded flat or a kept edge is neither folded flat nor flat, `noRigidMotion`
4747/// 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`.
4950public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after: FoldFrame,
5051 file: FoldFile) throws -> PopRecord {
5152 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
7677 let earlier = script.openOn == nil ? nil : (1..<n).reversed().lazy.compactMap { file.frames[$0].step }.first { s in
7778 s.nonRigid && s.pop?.spine.contains(opener) == true && tip.isSubset(of: s.axes.flatMap(\.movingFaces))
7879 }
80 if script.openOn != nil && earlier == nil { throw CompileError.noEarlierOpening(step: n) }
7981 let earlierFlap = Set(earlier?.axes.flatMap(\.movingFaces) ?? [])
8082 let earlierCreases = edgeFaces.indices.filter { e in
8183 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
132134 poppedFrame.verticesCoords = popped.positions
133135
134136 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,
136138 popped: popped.positions, closeAxes: closeAxes)
137139 var draft = file
138140 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 {
5151/// The open, pop and close of a non-rigid step: `openAxes` open the flap on `[0, phases[0]]`, the
5252/// first turning the opening crease to `openAngle` (the `spine`, or for a nested opening an
5353/// 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.
5557public struct PopRecord: Codable, Equatable, Sendable {
5658 public let spine: [Int]
5759 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
144144 #expect(layerMotionDefects(file).isEmpty)
145145}
146146
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
147168@Test func theReverseFoldMovesThroughNoStillFace() throws {
148169 // the open phase departs from frame 1 and the close phase arrives at frame 2
149170 #expect(layerMotionDefects(try compiled("reverse")).isEmpty)