Commit 5e1d0001ad

5e1d0001addd08c60703a1efcc72282e037b4a74

parent: ae41d0e459

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-19 22:00 UTC

Steps: public initialisers, fold-and-unfold must return

FoldAxis, FoldStep and Pop get public memberwise initialisers.
angle(at:kind:) falls back to endAngle without a peak, and readSteps
rejects a fold-and-unfold axis whose end angle differs from its start
(foldAndUnfoldDoesNotReturn). readSteps documents that it expects a
validated file and uses FoldFormat's edgesFaces.

Ref #2

Layout: unified · split

Packages/FoldKit/Sources/Folding/FoldStep.swift +43 −1
@@ -17,13 +17,28 @@ public struct FoldAxis: Equatable, Sendable {
17 public let window: ClosedRange<Double> 17 public let window: ClosedRange<Double>
18 public let curve: [Double]? 18 public let curve: [Double]?
19 19
20 public init(edge: Int, origin: SIMD3<Double>, direction: SIMD3<Double>, movingFaces: Set<Int>,
21 startAngle: Double, endAngle: Double, peakAngle: Double? = nil, parent: Int? = nil,
22 window: ClosedRange<Double> = 0...1, curve: [Double]? = nil) {
23 self.edge = edge
24 self.origin = origin
25 self.direction = direction
26 self.movingFaces = movingFaces
27 self.startAngle = startAngle
28 self.endAngle = endAngle
29 self.peakAngle = peakAngle
30 self.parent = parent
31 self.window = window
32 self.curve = curve
33 }
34
20 /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the 35 /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the
21 /// start and after it holds at the end. 36 /// start and after it holds at the end.
22 public func angle(at t: Double, kind: StepKind) -> Double { 37 public func angle(at t: Double, kind: StepKind) -> Double {
23 let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1) 38 let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1)
24 if let curve { return hermite(curve, at: u) } 39 if let curve { return hermite(curve, at: u) }
25 if kind == .foldAndUnfold { 40 if kind == .foldAndUnfold {
26 return startAngle + (peakAngle! - startAngle) * (1 - abs(2 * u - 1)) 41 return startAngle + ((peakAngle ?? endAngle) - startAngle) * (1 - abs(2 * u - 1))
27 } 42 }
28 return startAngle + u * (endAngle - startAngle) 43 return startAngle + u * (endAngle - startAngle)
29 } 44 }
@@ -38,6 +53,16 @@ public struct Pop: Equatable, Sendable {
38 public let openAxes: [FoldAxis] 53 public let openAxes: [FoldAxis]
39 public let popped: [SIMD3<Double>] 54 public let popped: [SIMD3<Double>]
40 public let closeAxes: [FoldAxis] 55 public let closeAxes: [FoldAxis]
56
57 public init(spine: [Int], openAngle: Double, phases: ClosedRange<Double>, openAxes: [FoldAxis],
58 popped: [SIMD3<Double>], closeAxes: [FoldAxis]) {
59 self.spine = spine
60 self.openAngle = openAngle
61 self.phases = phases
62 self.openAxes = openAxes
63 self.popped = popped
64 self.closeAxes = closeAxes
65 }
41} 66}
42 67
43/// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before 68/// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before
@@ -55,6 +80,22 @@ public struct FoldStep: Equatable, Sendable {
55 public let pop: Pop? 80 public let pop: Pop?
56 public let viewpoint: Viewpoint 81 public let viewpoint: Viewpoint
57 82
83 public init(index: Int, kind: StepKind, technique: StepTechnique = .plain, instruction: String,
84 axes: [FoldAxis], rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil,
85 repeatOf: Int? = nil, nonRigid: Bool = false, pop: Pop? = nil, viewpoint: Viewpoint) {
86 self.index = index
87 self.kind = kind
88 self.technique = technique
89 self.instruction = instruction
90 self.axes = axes
91 self.rotateDegrees = rotateDegrees
92 self.turnOverAxis = turnOverAxis
93 self.repeatOf = repeatOf
94 self.nonRigid = nonRigid
95 self.pop = pop
96 self.viewpoint = viewpoint
97 }
98
58 public var movingFaceUnion: Set<Int> { 99 public var movingFaceUnion: Set<Int> {
59 axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) } 100 axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
60 } 101 }
@@ -68,6 +109,7 @@ public enum StepError: Error, Equatable, Sendable {
68 case rotateWithoutDegrees(step: Int) 109 case rotateWithoutDegrees(step: Int)
69 case turnOverWithoutAxis(step: Int) 110 case turnOverWithoutAxis(step: Int)
70 case foldAndUnfoldWithoutPeak(step: Int) 111 case foldAndUnfoldWithoutPeak(step: Int)
112 case foldAndUnfoldDoesNotReturn(step: Int)
71 case missingInstruction(step: Int) 113 case missingInstruction(step: Int)
72 case axisParentCycle(step: Int) 114 case axisParentCycle(step: Int)
73 case axisWindowOutOfRange(step: Int) 115 case axisWindowOutOfRange(step: Int)
Packages/FoldKit/Sources/Folding/StepReader.swift +6 −15
@@ -2,6 +2,9 @@ import FoldFormat
2import simd 2import simd
3 3
4/// Reads every frame's authored `krz:step`. `result[k]` is step `k + 1`. 4/// Reads every frame's authored `krz:step`. `result[k]` is step `k + 1`.
5///
6/// - Precondition: `validate(file)` is empty. On a file it rejects, this may trap on an
7/// out-of-range index.
5public func readSteps(_ file: FoldFile) throws -> [FoldStep] { 8public func readSteps(_ file: FoldFile) throws -> [FoldStep] {
6 let edgeFaces = edgesFaces(file) 9 let edgeFaces = edgesFaces(file)
7 return try file.frames.indices.dropFirst().map { n in 10 return try file.frames.indices.dropFirst().map { n in
@@ -23,6 +26,9 @@ private func readStep(_ n: Int, _ record: StepRecord, _ file: FoldFile, _ edgeFa
23 if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.peakAngle == nil }) { 26 if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.peakAngle == nil }) {
24 throw StepError.foldAndUnfoldWithoutPeak(step: n) 27 throw StepError.foldAndUnfoldWithoutPeak(step: n)
25 } 28 }
29 if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.endAngle != $0.startAngle }) {
30 throw StepError.foldAndUnfoldDoesNotReturn(step: n)
31 }
26 if record.nonRigid && record.pop == nil { 32 if record.nonRigid && record.pop == nil {
27 throw StepError.nonRigidWithoutPop(step: n) 33 throw StepError.nonRigidWithoutPop(step: n)
28 } 34 }
@@ -117,18 +123,3 @@ private func traversal(of edge: [Int], by face: [Int], _ points: [SIMD3<Double>]
117 let origin = points[face[i]] 123 let origin = points[face[i]]
118 return (origin, normalize(points[face[(i + 1) % face.count]] - origin)) 124 return (origin, normalize(points[face[(i + 1) % face.count]] - origin))
119} 125}
120
121/// The faces on each side of each edge, from consecutive corners of `faces_vertices`.
122private func edgesFaces(_ file: FoldFile) -> [[Int]] {
123 var byEnds: [SIMD2<Int>: [Int]] = [:]
124 for (f, corners) in file.facesVertices.enumerated() {
125 for i in corners.indices {
126 let u = corners[i], v = corners[(i + 1) % corners.count]
127 byEnds[SIMD2(min(u, v), max(u, v)), default: []].append(f)
128 }
129 }
130 return file.edgesVertices.map { ends in
131 guard ends.count == 2 else { return [] }
132 return byEnds[SIMD2(min(ends[0], ends[1]), max(ends[0], ends[1]))] ?? []
133 }
134}
Packages/FoldKit/Tests/FoldingTests/StepReadingTests.swift +23
@@ -114,3 +114,26 @@ func movingFaceSetsWithinOneStepAreDisjoint(_ name: String) throws {
114 #expect(validate(degreeFourVertex()) == []) 114 #expect(validate(degreeFourVertex()) == [])
115 #expect(validate(degreeFourVertex(linear: true)) == []) 115 #expect(validate(degreeFourVertex(linear: true)) == [])
116} 116}
117
118@Test func aFoldAndUnfoldThatDoesNotReturnThrows() throws {
119 var file = try FoldFile(contentsOf: fixture("quarters.fold"))
120 let record = file.frames[1].step!
121 let a = record.axes[0]
122 file.frames[1].step = StepRecord(kind: record.kind, instruction: record.instruction,
123 axes: [AxisRecord(edge: a.edge, movingFaces: a.movingFaces,
124 startAngle: a.startAngle, endAngle: 180,
125 peakAngle: a.peakAngle)],
126 viewpoint: record.viewpoint)
127 #expect(throws: StepError.foldAndUnfoldDoesNotReturn(step: 1)) { try readSteps(file) }
128}
129
130@Test func aFoldAndUnfoldAxisWithoutAPeakPeaksAtItsEndAngle() {
131 let axis = FoldAxis(edge: 0, origin: .zero, direction: SIMD3(0, 1, 0), movingFaces: [1],
132 startAngle: 0, endAngle: 90)
133 #expect(axis.angle(at: 0.5, kind: .foldAndUnfold) == 90)
134 let step = FoldStep(index: 1, kind: .foldAndUnfold, instruction: "Fold and unfold.", axes: [axis],
135 pop: Pop(spine: [], openAngle: 30, phases: 0.25...0.75, openAxes: [],
136 popped: [], closeAxes: []),
137 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)))
138 #expect(step.movingFaceUnion == [1])
139}