Commit b1e606f57c
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/Folding/StepReader.swift +11 −5
| @@ -6,11 +6,17 @@ import simd | ||
| 6 | 6 | /// - Precondition: `validate(file)` is empty. On a file it rejects, this may trap on an |
| 7 | 7 | /// out-of-range index. |
| 8 | 8 | public 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. | |
| 17 | public 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)) | |
| 14 | 20 | } |
| 15 | 21 | |
| 16 | 22 | private 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 { | ||
| 137 | 137 | viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))) |
| 138 | 138 | #expect(step.movingFaceUnion == [1]) |
| 139 | 139 | } |
| 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 | } | |