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 {
1717 public let window: ClosedRange<Double>
1818 public let curve: [Double]?
1919
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
2035 /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the
2136 /// start and after it holds at the end.
2237 public func angle(at t: Double, kind: StepKind) -> Double {
2338 let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1)
2439 if let curve { return hermite(curve, at: u) }
2540 if kind == .foldAndUnfold {
26 return startAngle + (peakAngle! - startAngle) * (1 - abs(2 * u - 1))
41 return startAngle + ((peakAngle ?? endAngle) - startAngle) * (1 - abs(2 * u - 1))
2742 }
2843 return startAngle + u * (endAngle - startAngle)
2944 }
@@ -38,6 +53,16 @@ public struct Pop: Equatable, Sendable {
3853 public let openAxes: [FoldAxis]
3954 public let popped: [SIMD3<Double>]
4055 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 }
4166}
4267
4368/// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before
@@ -55,6 +80,22 @@ public struct FoldStep: Equatable, Sendable {
5580 public let pop: Pop?
5681 public let viewpoint: Viewpoint
5782
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
5899 public var movingFaceUnion: Set<Int> {
59100 axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
60101 }
@@ -68,6 +109,7 @@ public enum StepError: Error, Equatable, Sendable {
68109 case rotateWithoutDegrees(step: Int)
69110 case turnOverWithoutAxis(step: Int)
70111 case foldAndUnfoldWithoutPeak(step: Int)
112 case foldAndUnfoldDoesNotReturn(step: Int)
71113 case missingInstruction(step: Int)
72114 case axisParentCycle(step: Int)
73115 case axisWindowOutOfRange(step: Int)
Packages/FoldKit/Sources/Folding/StepReader.swift +6 −15
@@ -2,6 +2,9 @@ import FoldFormat
22import simd
33
44/// 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.
58public func readSteps(_ file: FoldFile) throws -> [FoldStep] {
69 let edgeFaces = edgesFaces(file)
710 return try file.frames.indices.dropFirst().map { n in
@@ -23,6 +26,9 @@ private func readStep(_ n: Int, _ record: StepRecord, _ file: FoldFile, _ edgeFa
2326 if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.peakAngle == nil }) {
2427 throw StepError.foldAndUnfoldWithoutPeak(step: n)
2528 }
29 if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.endAngle != $0.startAngle }) {
30 throw StepError.foldAndUnfoldDoesNotReturn(step: n)
31 }
2632 if record.nonRigid && record.pop == nil {
2733 throw StepError.nonRigidWithoutPop(step: n)
2834 }
@@ -117,18 +123,3 @@ private func traversal(of edge: [Int], by face: [Int], _ points: [SIMD3<Double>]
117123 let origin = points[face[i]]
118124 return (origin, normalize(points[face[(i + 1) % face.count]] - origin))
119125}
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 {
114114 #expect(validate(degreeFourVertex()) == [])
115115 #expect(validate(degreeFourVertex(linear: true)) == [])
116116}
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}