Commit 13a69d14b0

13a69d14b0f3953d8fa6007be1f5ab16e70a1162

parent: aa644a399c

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 20:58 UTC

Open the flap on the spine's side of its whole line; bend lines

Ref #7

Layout: unified · split

Packages/FoldKit/Sources/FoldCompiler/FrameCompiler.swift +2 −1
@@ -152,10 +152,11 @@ private func appendStep(_ n: Int, _ script: ScriptStep, viewpoint: Viewpoint, to
152152 interpolator = FoldInterpolator(file: draft, step: step)
153153 frame.verticesCoords = interpolator.positions(at: 1)
154154 frame.step = record
155 // a bend line on the spine bends and flattens again, and is not creased
155156 for (e, angle) in popEndAngles(step, interpolator, previous: previous, file: file) {
156157 frame.edgesFoldAngle[e] = angle
157158 frame.edgesAssignment[e] = angle > 0 ? .valley : angle < 0 ? .mountain : .flat
158 if file.edgesFirstCreasedAtStep[e] == nil { file.edgesFirstCreasedAtStep[e] = n }
159 if file.edgesFirstCreasedAtStep[e] == nil && angle != 0 { file.edgesFirstCreasedAtStep[e] = n }
159160 }
160161 }
161162 let guess = guessFaceOrders(previous: previous, next: frame, step: record, file: file)
Packages/FoldKit/Sources/FoldCompiler/PopCompiler.swift +62 −26
@@ -15,17 +15,19 @@ private let popSamples = 64
1515private let angleSamples = 256
1616
1717/// 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.
2020///
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.
2931///
3032/// The starting guess can lead the solver to another branch of the vertex's motion. Closing the
3133/// 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
4749 let faceCount = file.facesVertices.count
4850 let tolerance = sheetExtent(file) * relativeTolerance
4951 let openAngle = script.openAngle ?? defaultOpenAngle
52 func foldedFlat(_ e: Int) -> Bool { abs(abs(before.edgesFoldAngle[e]) - 180) <= angleSnapTolerance }
5053
5154 guard let named = resolve(reference, file, edgeFaces) else {
5255 throw CompileError.unresolvedCrease(step: n, crease: reference)
5356 }
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)
6259
6360 var group = Array(repeating: -1, count: faceCount)
6461 for (a, axis) in record.axes.enumerated() {
@@ -67,18 +64,26 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after
6764 let moving = group.map { $0 >= 0 }
6865 let creased = edgeFaces.indices.filter { edgeFaces[$0].count == 2 && group[edgeFaces[$0][0]] != group[edgeFaces[$0][1]] }
6966
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
7075 // the layer nearer the viewer opens: of the named edge's faces, the one a valley puts on the
7176 // first face's normal side, or a mountain on the other
7277 let (f, g) = (edgeFaces[named][0], edgeFaces[named][1])
7378 let normals = faceNormals(before, file)
7479 let gNearer = (before.edgesFoldAngle[named] > 0) == (dot(normals[f], record.viewpoint.eye) > 0)
7580 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] }
7881 guard sides[far] != sides[near] else { throw CompileError.noRigidMotion(step: n) }
82 let opening = (0..<faceCount).filter { sides[$0] == sides[near] }
7983 let openSign = before.edgesFoldAngle[named] > 0 ? 1.0 : -1.0
8084 let openAxes = [AxisRecord(edge: named, movingFaces: opening, startAngle: before.edgesFoldAngle[named],
8185 endAngle: openSign * openAngle, window: [0, phases[0]])]
86 let opened = try openEndAngles(n, openAxes, edges: spine, before: before, record, file)
8287
8388 // the tree the pop and the close move on
8489 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
8792 + creased.filter { e in !spine.contains(e) && !record.axes.contains { $0.edge == e } }
8893 let tree = growTree(order: order, fixed: fixed, file, edgeFaces)
8994
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]! }
9598 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]
97100 }
98101 var guess: [Int: Double] = [:]
99102 for axis in tree.axes {
@@ -123,6 +126,39 @@ public func compilePop(step n: Int, script: ScriptStep, before: FoldFrame, after
123126 return pop
124127}
125128
129/// `named` and every other interior edge on its line in `frame`.
130private 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.
141private 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
126162/// `record`, flagged non-rigid, with `pop`.
127163func withPop(_ record: StepRecord, _ pop: PopRecord) -> StepRecord {
128164 StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes,
Packages/FoldKit/Sources/FoldFormat/FoldValidator.swift +7 −5
@@ -269,15 +269,17 @@ private func missingFirstCreasedAtStep(_ file: FoldFile, edgeFaces: [[Int]]) ->
269269
270270/// The edges some step creases: those whose two faces the step moves differently. In each of a
271271/// step's axis lists (`axes`, and a pop's `openAxes` and `closeAxes`), a face moves with the
272/// deepest axis whose `movingFaces` contains it, or stays fixed when none does.
272/// deepest axis whose `movingFaces` contains it, or stays fixed when none does. A pop does not
273/// crease its spine's edges: each was folded flat before the step or is a bend line, which bends
274/// while the flap is open and flattens again (decision 0003).
273275private func creasedEdges(_ file: FoldFile, edgeFaces: [[Int]]) -> Set<Int> {
274 let lists = file.frames.compactMap(\.step).flatMap { step in
275 [step.axes] + (step.pop.map { [$0.openAxes, $0.closeAxes] } ?? [])
276 let lists: [(axes: [AxisRecord], spine: Set<Int>)] = file.frames.compactMap(\.step).flatMap { step in
277 [(step.axes, [])] + (step.pop.map { [($0.openAxes, Set($0.spine)), ($0.closeAxes, Set($0.spine))] } ?? [])
276278 }
277 let groupings = lists.map(movementGroups)
279 let groupings = lists.map { (groups: movementGroups($0.axes), spine: $0.spine) }
278280 return Set(edgeFaces.indices.filter { e in
279281 let faces = edgeFaces[e]
280 return faces.count == 2 && groupings.contains { $0[faces[0]] != $0[faces[1]] }
282 return faces.count == 2 && groupings.contains { !$0.spine.contains(e) && $0.groups[faces[0]] != $0.groups[faces[1]] }
281283 })
282284}
283285
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/reverse-bend-cp.fold added +14
@@ -0,0 +1,14 @@
1{
2 "file_spec": 1.2,
3 "file_title": "Reverse fold 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 [0, 3], [1, 3], [0, 4], [1, 4]],
8 "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 11], [11, 13], [13, 12], [12, 10],
9 [10, 7], [7, 8], [8, 0],
10 [8, 9], [9, 3], [9, 1], [9, 5], [7, 6], [10, 11]],
11 "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
12 "V", "M", "V", "V", "M", "F"],
13 "faces_vertices": [[0, 1, 9, 8], [1, 2, 3, 9], [9, 3, 4, 5], [8, 9, 5, 6, 7], [7, 6, 11, 10], [10, 11, 13, 12]]
14}
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/reverse-bend.foldscript added +27
@@ -0,0 +1,27 @@
1{
2 "creasePattern": "reverse-bend-cp.fold",
3 "title": "Reverse fold 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.5, 0.5]],
23 "spine": [[0, 1], [2, 1]],
24 "faceOrders": [{"above": [3.5, 1.5], "below": [3.5, 0.5]}]
25 }
26 ]
27}
Packages/FoldKit/Tests/FoldCompilerTests/NonRigidTests.swift +16
@@ -103,6 +103,22 @@ import FoldKitTestSupport
103103 #expect(elapsed / 100 < .milliseconds(4))
104104}
105105
106@Test func aBendLineOnTheSpineFoldsWhileTheFlapIsOpenAndFlattensAgain() throws {
107 // decision 0003: the tab's bend line (1 of its 2 faces beyond the spine's line) is cut with the
108 // spine, so the tab's near half opens with the flap; the line is never creased
109 let file = try compiled("reverse-bend")
110 let bend = 18
111 #expect(file.edgesFirstCreasedAtStep[bend] == nil)
112 #expect(file.frames.allSatisfy { $0.edgesFoldAngle[bend] == 0 && $0.edgesAssignment[bend] == .flat })
113 let step = try readSteps(file)[2]
114 let i = FoldInterpolator(file: file, step: step)
115 let open = i.positions(at: step.pop!.phases.lowerBound)
116 let normals = faceNormals(positions: open, faces: file.facesVertices)
117 let between = acos(simd_dot(normals[4], normals[5])) * 180 / .pi
118 #expect(abs(between - 160) < 1e-6)
119 #expect(meshInterpenetrations(i, samples: 64).isEmpty)
120}
121
106122@Test func theReverseFoldMovesThroughNoStillFace() throws {
107123 // the open phase departs from frame 1 and the close phase arrives at frame 2
108124 #expect(layerMotionDefects(try compiled("reverse")).isEmpty)