Commit 7f3b2232c5

7f3b2232c56405f317100b42e3a1870f2566afc4

parent: ae72845afe

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-19 23:13 UTC

Read moving/still face pairs against the still face's normal

LayerOrder.active reverses each triple whose g moves in the step and whose
f does not, using its source's normals. Adds tests for a page turning over
a still layer and for the start-frame fallback as a flap leaves a stack.
Fixes the linking tolerance wording in the clusters doc comment.

Ref #3

Layout: unified · split

Packages/FoldKit/Sources/Folding/LayerOrder.swift +14 −2
@@ -74,6 +74,10 @@ public struct LayerOrder: Sendable {
74 /// normals at the peak for a `foldAndUnfold`, else frame n's), or without one frame n's 74 /// normals at the peak for a `foldAndUnfold`, else frame n's), or without one frame n's
75 /// `faceOrders`; then frame n − 1's `faceOrders` for the pairs the first lacks, such as a flap 75 /// `faceOrders`; then frame n − 1's `faceOrders` for the pairs the first lacks, such as a flap
76 /// leaving a stack. 76 /// leaving a stack.
77 ///
78 /// A triple whose `g` moves in the step and whose `f` does not is replaced, with its source's
79 /// normals, by its reverse, so that the pair is read against the still face's normal, which
80 /// does not turn during the step.
77 public static func active(for step: FoldStep, in file: FoldFile) throws -> LayerOrder { 81 public static func active(for step: FoldStep, in file: FoldFile) throws -> LayerOrder {
78 let start = file.frames[step.index - 1], end = file.frames[step.index] 82 let start = file.frames[step.index - 1], end = file.frames[step.index]
79 let first: (faceOrders: [[Int]], normals: [SIMD3<Double>]) 83 let first: (faceOrders: [[Int]], normals: [SIMD3<Double>])
@@ -86,14 +90,22 @@ public struct LayerOrder: Sendable {
86 } else { 90 } else {
87 first = (end.faceOrders, faceNormals(end, file)) 91 first = (end.faceOrders, faceNormals(end, file))
88 } 92 }
89 return try LayerOrder(sources: [first, (start.faceOrders, faceNormals(start, file))]) 93 let moving = step.movingFaceUnion
94 let sources = [first, (start.faceOrders, faceNormals(start, file))].map { source in
95 (faceOrders: source.faceOrders.map { t -> [Int] in
96 guard t.count == 3, source.normals.indices.contains(t[0]), source.normals.indices.contains(t[1]),
97 moving.contains(t[1]), !moving.contains(t[0]) else { return t }
98 return [t[1], t[0], dot(source.normals[t[0]], source.normals[t[1]]) > 0 ? -t[2] : t[2]]
99 }, normals: source.normals)
100 }
101 return try LayerOrder(sources: sources)
90 } 102 }
91 103
92 /// The faces grouped into clusters, each sorted back to front as seen looking along 104 /// The faces grouped into clusters, each sorted back to front as seen looking along
93 /// `viewDirection`, ordered by their smallest face index. Every face is in exactly one. 105 /// `viewDirection`, ordered by their smallest face index. Every face is in exactly one.
94 /// 106 ///
95 /// Two faces are linked when, projected onto the first's plane, their interiors overlap, their 107 /// Two faces are linked when, projected onto the first's plane, their interiors overlap, their
96 /// normals are within 2° of one line, and each one's vertices lie within sin 2° of the 108 /// normals are within 2° of one line, and each one's vertices lie within sin 2° × the
97 /// bounding-box diagonal of `positions` from the other's plane. A cluster is a connected group 109 /// bounding-box diagonal of `positions` from the other's plane. A cluster is a connected group
98 /// of linked faces. Within a cluster each triple is read against its `g`'s normal at 110 /// of linked faces. Within a cluster each triple is read against its `g`'s normal at
99 /// `positions`; faces the order leaves unrelated keep index order. 111 /// `positions`; faces the order leaves unrelated keep index order.
Packages/FoldKit/Tests/FoldingTests/LayerOrderTests.swift +103
@@ -63,6 +63,30 @@ let up3 = up2 + [SIMD3(0, 0, 1)]
63 #expect(left.faces.firstIndex(of: 2)! > left.faces.firstIndex(of: 3)!) 63 #expect(left.faces.firstIndex(of: 2)! > left.faces.firstIndex(of: 3)!)
64} 64}
65 65
66@Test func aPageTurningOverAStillLayerStaysInFrontOfItThroughoutTheFold() throws {
67 // S under the hinge, M turning over onto N; the end frame's [0, 2, 1] is written against M's
68 // normal at the end, opposite to its normal at the start
69 let (file, step) = pageTurnOverStillLayer()
70 let order = try LayerOrder.active(for: step, in: file)
71 let interpolator = FoldInterpolator(file: file, step: step)
72 for degrees in [0.5, 179.5] {
73 let c = order.clusters(positions: interpolator.positions(at: degrees / 180), faces: file.facesVertices,
74 viewDirection: SIMD3(0, 0, -1))
75 let stack = try #require(c.first { $0.faces.contains(0) })
76 #expect(stack.faces.contains(2) && stack.faces.firstIndex(of: 2)! > stack.faces.firstIndex(of: 0)!,
77 "\(degrees)°: \(c.map(\.faces))")
78 }
79}
80
81@Test func aFlapLeavingAStackIsOrderedByTheStartFrame() throws {
82 // frame 2 is flat and orders nothing, so frame 1's order must apply as f0 lifts off f1
83 let (file, step) = halfUnfolded()
84 let c = try LayerOrder.active(for: step, in: file)
85 .clusters(positions: FoldInterpolator(file: file, step: step).positions(at: 0.01),
86 faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1))
87 #expect(c.map(\.faces) == [[1, 0]])
88}
89
66@Test func cyclicOrderThrows() { 90@Test func cyclicOrderThrows() {
67 #expect(throws: LayerError.cyclic(cycle: [0, 1, 2])) { 91 #expect(throws: LayerError.cyclic(cycle: [0, 1, 2])) {
68 try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: up3) 92 try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: up3)
@@ -98,3 +122,82 @@ let up3 = up2 + [SIMD3(0, 0, 1)]
98 let file = try FoldFile(contentsOf: fixture("quarters.fold")) 122 let file = try FoldFile(contentsOf: fixture("quarters.fold"))
99 #expect(stackDepth(positions: file.frames[0].verticesCoords, faces: file.facesVertices) == 1) 123 #expect(stackDepth(positions: file.frames[0].verticesCoords, faces: file.facesVertices) == 1)
100} 124}
125
126/// S, f0, a 2 × 1 face at z = 0, under a 2 × 1 top layer of N, f1, on x ∈ [0, 1] and M, f2, on
127/// x ∈ [1, 2], hinged at x = 1. Step 1 valley folds M over onto N.
128private func pageTurnOverStillLayer() -> (FoldFile, FoldStep) {
129 let start: [SIMD3<Double>] = [
130 SIMD3(0, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 1, 0), SIMD3(0, 1, 0),
131 SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 1, 0), SIMD3(1, 1, 0), SIMD3(0, 1, 0),
132 ]
133 var end = start
134 end[6] = SIMD3(0, 0, 0)
135 end[7] = SIMD3(0, 1, 0)
136 let boundary = Array(repeating: EdgeAssignment.boundary, count: 10)
137 let file = FoldFile(
138 fileSpec: 1.2,
139 fileAuthor: "Traditional",
140 edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 0],
141 [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 4], [5, 8]],
142 facesVertices: [[0, 1, 2, 3], [4, 5, 8, 9], [5, 6, 7, 8]],
143 edgesFirstCreasedAtStep: Array(repeating: nil, count: 10) + [1],
144 frames: [
145 FoldFrame(verticesCoords: start, edgesAssignment: boundary + [.flat],
146 edgesFoldAngle: Array(repeating: 0, count: 11),
147 faceOrders: [[0, 1, -1], [0, 2, -1]]),
148 // normals +z, +z, −z: S below N, S below M, N below M
149 FoldFrame(verticesCoords: end, edgesAssignment: boundary + [.valley],
150 edgesFoldAngle: Array(repeating: 0, count: 10) + [180],
151 faceOrders: [[0, 1, -1], [0, 2, 1], [1, 2, 1]]),
152 ]
153 )
154 let step = FoldStep(
155 index: 1,
156 kind: .fold,
157 instruction: "Turn the page over.",
158 // M traverses the hinge from v8 down to v5
159 axes: [FoldAxis(edge: 10, origin: SIMD3(1, 0, 0), direction: SIMD3(0, -1, 0), movingFaces: [2],
160 startAngle: 0, endAngle: 180)],
161 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))
162 )
163 return (file, step)
164}
165
166/// `half.fold`'s sheet with its faces listed right half first, so the flap is f0. Step 1 valley
167/// folds f0 over f1; step 2 unfolds it flat again.
168private func halfUnfolded() -> (FoldFile, FoldStep) {
169 let flat: [SIMD3<Double>] = [
170 SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 2, 0), SIMD3(1, 2, 0), SIMD3(0, 2, 0),
171 ]
172 var folded = flat
173 folded[2] = SIMD3(0, 0, 0)
174 folded[3] = SIMD3(0, 2, 0)
175 let boundary = Array(repeating: EdgeAssignment.boundary, count: 6)
176 func frame(_ points: [SIMD3<Double>], angle: Double, faceOrders: [[Int]]) -> FoldFrame {
177 FoldFrame(verticesCoords: points, edgesAssignment: boundary + [angle == 0 ? .flat : .valley],
178 edgesFoldAngle: Array(repeating: 0, count: 6) + [angle], faceOrders: faceOrders)
179 }
180 let file = FoldFile(
181 fileSpec: 1.2,
182 fileAuthor: "Traditional",
183 edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]],
184 facesVertices: [[1, 2, 3, 4], [0, 1, 4, 5]],
185 edgesFirstCreasedAtStep: Array(repeating: nil, count: 6) + [1],
186 frames: [
187 frame(flat, angle: 0, faceOrders: []),
188 // f0 faces −z, f1 faces +z: f0 on top
189 frame(folded, angle: 180, faceOrders: [[0, 1, 1]]),
190 frame(flat, angle: 0, faceOrders: []),
191 ]
192 )
193 let step = FoldStep(
194 index: 2,
195 kind: .fold,
196 instruction: "Unfold.",
197 // f0 traverses the crease from v4 down to v1
198 axes: [FoldAxis(edge: 6, origin: SIMD3(1, 0, 0), direction: SIMD3(0, -1, 0), movingFaces: [0],
199 startAngle: 180, endAngle: 0)],
200 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))
201 )
202 return (file, step)
203}