Commit b1e606f57c

b1e606f57c24fc650add3d81763c82359340051c

parent: ae2d67750c

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 02:05 UTC

Folding: read one step at a time

Ref #4

Layout: unified · split

Packages/FoldKit/Sources/Folding/StepReader.swift +11 −5
@@ -6,11 +6,17 @@ import simd
66/// - Precondition: `validate(file)` is empty. On a file it rejects, this may trap on an
77/// out-of-range index.
88public func readSteps(_ file: FoldFile) throws -> [FoldStep] {
9 let edgeFaces = edgesFaces(file)
10 return try file.frames.indices.dropFirst().map { n in
11 guard let record = file.frames[n].step else { throw FoldError.missingStepRecord(step: n) }
12 return try readStep(n, record, file, edgeFaces)
13 }
9 try file.frames.indices.dropFirst().map { n in try readStep(n, in: file) }
10}
11
12/// Reads step `n` from frame n's `krz:step`, directing its axes against frame n − 1's
13/// coordinates. Only frames 0…n are read, so frames after n need not exist.
14///
15/// - Precondition: frames 0…n are well formed: frame n − 1 has a coordinate for every vertex
16/// and the topology's indices are in range.
17public func readStep(_ n: Int, in file: FoldFile) throws -> FoldStep {
18 guard let record = file.frames[n].step else { throw FoldError.missingStepRecord(step: n) }
19 return try readStep(n, record, file, edgesFaces(file))
1420}
1521
1622private func readStep(_ n: Int, _ record: StepRecord, _ file: FoldFile, _ edgeFaces: [[Int]]) throws -> FoldStep {
Packages/FoldKit/Tests/FoldingTests/StepReadingTests.swift +7
@@ -137,3 +137,10 @@ func movingFaceSetsWithinOneStepAreDisjoint(_ name: String) throws {
137137 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)))
138138 #expect(step.movingFaceUnion == [1])
139139}
140
141@Test func aStepReadsWithoutTheFramesAfterIt() throws {
142 var file = try FoldFile(contentsOf: fixture("quarters.fold"))
143 let whole = try readSteps(file)
144 file.frames.removeLast()
145 #expect(try readStep(2, in: file) == whole[1])
146}