| @@ -6,6 +6,7 @@ import FoldFormat |
| 6 | 6 | import Folding |
| 7 | 7 | import FoldKitTestSupport |
| 8 | 8 | import Models |
| 9 | import Notation |
| 9 | 10 | |
| 10 | 11 | @Test func everyShippedStepEndsOnItsEndFrame() throws { |
| 11 | 12 | // the step records and the frames must agree: interpolating a step to t = 1 reproduces frame n |
| @@ -81,6 +82,43 @@ import Models |
| 81 | 82 | } |
| 82 | 83 | } |
| 83 | 84 | |
| 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. |
| 108 | private 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 | |
| 84 | 122 | @Test func theCatalogueContainsTheCup() throws { |
| 85 | 123 | #expect(try ModelLibrary.names() == ["cup"]) |
| 86 | 124 | } |