Commit e7ef09b655

e7ef09b655274fac52bdfe65f91394a52df01e45

parent: a2c75668f8

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 08:43 UTC

Gate every shipped step's session inputs

Ref #6

Layout: unified · split

App/Sources/FoldSession.swift +11 −4
@@ -70,11 +70,18 @@ final class FoldSession {
7070 }
7171
7272 /// Moves to the next step at t = 0, seen from its viewpoint, once the current step is complete.
73 /// After the last step the finished model stays at t = 1.
73 /// After the last step the finished model stays at t = 1, as it does when the next step cannot
74 /// be read, which stops a Debug build.
7475 func completeStep() {
75 guard t == 1, step.index + 1 < model.frames.count,
76 let next = try? readStep(step.index + 1, in: model),
77 let nextOrder = try? LayerOrder.active(for: next, in: model) else { return }
76 guard t == 1, step.index + 1 < model.frames.count else { return }
77 let next: FoldStep, nextOrder: LayerOrder
78 do {
79 next = try readStep(step.index + 1, in: model)
80 nextOrder = try LayerOrder.active(for: next, in: model)
81 } catch {
82 assertionFailure("step \(step.index + 1) of \(model.fileTitle ?? "?"): \(error)")
83 return
84 }
7885 step = next
7986 interpolator = FoldInterpolator(file: model, step: next)
8087 order = nextOrder
Packages/FoldKit/Package.swift +2 −1
@@ -33,7 +33,8 @@ let package = Package(
3333 .testTarget(name: "FoldCompilerTests",
3434 dependencies: ["FoldCompiler", "Folding", "FoldFormat", "FoldKitTestSupport"]),
3535 .testTarget(name: "ModelsTests",
36 dependencies: ["Models", "FoldCompiler", "Folding", "FoldFormat", "FoldKitTestSupport", "foldlint"]),
36 dependencies: ["Models", "FoldCompiler", "Folding", "FoldFormat", "FoldKitTestSupport", "Notation",
37 "foldlint"]),
3738 .testTarget(name: "NotationTests",
3839 dependencies: ["Notation", "Models", "Folding", "FoldFormat", "FoldKitTestSupport"],
3940 resources: [.copy("Snapshots")]),
Packages/FoldKit/Tests/ModelsTests/CatalogueTests.swift +38
@@ -6,6 +6,7 @@ import FoldFormat
66import Folding
77import FoldKitTestSupport
88import Models
9import Notation
910
1011@Test func everyShippedStepEndsOnItsEndFrame() throws {
1112 // the step records and the frames must agree: interpolating a step to t = 1 reproduces frame n
@@ -81,6 +82,43 @@ import Models
8182 }
8283}
8384
85@Test func everyShippedStepBuildsWhatTheAppShowsOfIt() throws {
86 // per step, what the app's session builds, with its camera at the step's authored viewpoint
87 for file in try ModelLibrary.all() {
88 for n in 1..<file.frames.count {
89 let label = "\(file.fileTitle ?? "?") step \(n)"
90 let step = try readStep(n, in: file)
91 let order = try LayerOrder.active(for: step, in: file)
92 _ = buildDiagram(file: file, step: step)
93 let interpolator = FoldInterpolator(file: file, step: step)
94 let camera = sessionCamera(file, step, aspect: 400.0 / 600)
95 for t in [0, 0.5, 1.0] {
96 let clusters = order.clusters(positions: interpolator.positions(at: t), faces: file.facesVertices,
97 viewDirection: camera.target - camera.eye)
98 #expect(clusters.reduce(0) { $0 + $1.faces.count } == file.facesVertices.count, "\(label) t \(t)")
99 }
100 let mapping = FoldParameterMapping(file: file, step: step, camera: camera, viewportSize: SIMD2(400, 600))
101 #expect(mapping.path.allSatisfy { $0.x.isFinite && $0.y.isFinite }, "\(label)")
102 }
103 }
104}
105
106/// The camera FoldSession starts a step with: from the step's viewpoint, at the default field of
107/// view, far enough that the flat sheet's half-diagonal fits about the start frame's centre.
108private func sessionCamera(_ file: FoldFile, _ step: FoldStep, aspect: Double) -> Camera {
109 func bounds(_ p: [SIMD3<Double>]) -> (SIMD3<Double>, SIMD3<Double>) {
110 (p.dropFirst().reduce(p[0], simd_min), p.dropFirst().reduce(p[0], simd_max))
111 }
112 let (low, high) = bounds(file.frames[0].verticesCoords)
113 let (startLow, startHigh) = bounds(file.frames[step.index - 1].verticesCoords)
114 let target = (startLow + startHigh) / 2
115 let fieldOfView = Double.pi / 6
116 let distance = simd_length(high - low) / 2 / sin(min(fieldOfView / 2, atan(tan(fieldOfView / 2) * aspect)))
117 let eye = simd_normalize(step.viewpoint.eye)
118 let up = simd_normalize(step.viewpoint.up - simd_dot(step.viewpoint.up, eye) * eye)
119 return Camera(eye: target + distance * eye, target: target, up: up, fieldOfView: fieldOfView)
120}
121
84122@Test func theCatalogueContainsTheCup() throws {
85123 #expect(try ModelLibrary.names() == ["cup"])
86124}