Packages/FoldKit/Tests/FoldingTests/InterpolationTests.swift

main
fold/Packages/FoldKit/Tests/FoldingTests/InterpolationTests.swift history · blame · raw

119 lines · 5043 bytes

14 symbols in this file
  1import Foundation
  2import Testing
  3import simd
  4import FoldFormat
  5import Folding
  6import FoldKitTestSupport
  7
  8let modelFixtures = ["half.fold", "mountain.fold", "letter.fold", "letter-sequential.fold",
  9                     "letter-staggered.fold", "quarters.fold", "occlusion.fold"]
 10
 11@Test func tZeroReproducesTheStartFrame() throws {
 12    let (file, i) = try interpolator("half.fold", step: 1)
 13    #expect(zip(i.positions(at: 0), file.frames[0].verticesCoords)
 14        .allSatisfy { simd_distance($0, $1) < 1e-12 })
 15}
 16
 17@Test(arguments: modelFixtures)
 18func tOneReproducesTheEndFrameForEveryStep(_ name: String) throws {
 19    let file = try FoldFile(contentsOf: fixture(name))
 20    for step in try readSteps(file) {
 21        let i = FoldInterpolator(file: file, step: step)
 22        #expect(zip(i.positions(at: 1), file.frames[step.index].verticesCoords)
 23            .allSatisfy { simd_distance($0, $1) < 1e-9 }, "\(name) step \(step.index)")
 24    }
 25}
 26
 27@Test(arguments: modelFixtures)
 28func everyEdgeLengthIsPreservedThroughoutEveryStep(_ name: String) throws {
 29    let file = try FoldFile(contentsOf: fixture(name))
 30    for step in try readSteps(file) {
 31        let i = FoldInterpolator(file: file, step: step)
 32        for t in stride(from: 0.0, through: 1.0, by: 0.05) {
 33            #expect(maxEdgeLengthDrift(i.positions(at: t), file) < 1e-9,
 34                    "\(name) step \(step.index) t \(t)")
 35        }
 36    }
 37}
 38
 39@Test func verticesOnTheAxisDoNotMove() throws {
 40    let (file, i) = try interpolator("half.fold", step: 1)
 41    for v in [1, 4] {
 42        #expect(simd_distance(i.positions(at: 0.37)[v], file.frames[0].verticesCoords[v]) < 1e-12)
 43    }
 44}
 45
 46@Test func aValleyFoldRisesTowardTheViewerAndAMountainFoldFallsAway() throws {
 47    let (_, valley) = try interpolator("half.fold", step: 1)
 48    let (_, mountain) = try interpolator("mountain.fold", step: 1)
 49    #expect(valley.positions(at: 0.5)[2].z > 0)
 50    #expect(mountain.positions(at: 0.5)[2].z < 0)
 51}
 52
 53@Test func multiAxisStepMovesEverySetOnTheSharedParameter() throws {
 54    let (file, i) = try interpolator("letter.fold", step: 1)
 55    let start = file.frames[0].verticesCoords, mid = i.positions(at: 0.5)
 56    #expect(simd_distance(mid[0], start[0]) > 0.1)   // left flap
 57    #expect(simd_distance(mid[3], start[3]) > 0.1)   // right flap
 58}
 59
 60@Test func foldAndUnfoldPassesThroughThePeakAndReturns() throws {
 61    let (file, i) = try interpolator("quarters.fold", step: 1)
 62    #expect(simd_distance(i.positions(at: 0.5)[2], SIMD3(0, 0, 0)) < 1e-9)
 63    #expect(zip(i.positions(at: 1), file.frames[0].verticesCoords)
 64        .allSatisfy { simd_distance($0, $1) < 1e-9 })
 65}
 66
 67@Test func aCoupledVertexStaysWholeThroughout() throws {
 68    // decision 0001, case 3: curves from the closed form, sampled at 257 points
 69    let file = degreeFourVertex()
 70    let i = FoldInterpolator(file: file, step: try readSteps(file)[0])
 71    for n in 0...1024 {
 72        #expect(maxEdgeLengthDrift(i.positions(at: Double(n) / 1024), file) < 1e-6)
 73    }
 74    #expect(zip(i.positions(at: 1), file.frames[1].verticesCoords)
 75        .allSatisfy { simd_distance($0, $1) < 1e-9 })
 76}
 77
 78@Test func theSameVertexWithLinearAnglesTears() throws {
 79    // the control: drop the curve and the paper does not hold together
 80    let file = degreeFourVertex(linear: true)
 81    let i = FoldInterpolator(file: file, step: try readSteps(file)[0])
 82    #expect(maxEdgeLengthDrift(i.positions(at: 0.5), file) > 0.1)
 83}
 84
 85@Test func aStaggeredAxisMovesOnlyInItsWindow() throws {
 86    let (file, i) = try interpolator("letter-staggered.fold", step: 1)
 87    let start = file.frames[0].verticesCoords
 88    #expect(simd_distance(i.positions(at: 0.25)[3], start[3]) < 1e-12)   // right flap waits
 89    #expect(simd_distance(i.positions(at: 0.5)[0], SIMD3(2, 0, 0)) < 1e-9) // left flap down
 90}
 91
 92@Test func turnOverRotatesTheModelRigidly() throws {
 93    let (_, i) = try interpolator("half.fold", step: 2)
 94    #expect(pairwiseDistancesAgree(i.positions(at: 0.5), i.positions(at: 0)))
 95    #expect(i.positions(at: 0.5).contains { $0.z != 0 })
 96}
 97
 98@Test func interpolationFitsInAFrameBudget() throws {
 99    let file = gridSheet(cells: 12)   // built in code: a 12×12 grid, one fold down the middle
100    let i = FoldInterpolator(file: file, step: try readSteps(file)[0])
101    let elapsed = ContinuousClock().measure {
102        for n in 0..<1000 { _ = i.positions(at: Double(n) / 1000) }
103    }
104    #expect(elapsed / 1000 < .milliseconds(1))
105}
106
107@Test func theGridSheetIsAValidFile() {
108    #expect(validate(gridSheet(cells: 12)).isEmpty)
109}
110
111@Test func aSolvedCurveIsRebuiltToSecondOrderAtItsEnds() {
112    // 180 u² at 257 samples: the rebuilt curve is exact for a quadratic in its first and last intervals
113    let samples = (0...256).map { 180 * pow(Double($0) / 256, 2) }
114    let axis = FoldAxis(edge: 0, origin: .zero, direction: SIMD3(1, 0, 0), movingFaces: [], startAngle: 0,
115                        endAngle: 180, curve: samples)
116    for u in [0.5 / 256, 1 - 0.5 / 256] {
117        #expect(abs(axis.angle(at: u, kind: .fold) - 180 * u * u) < 1e-9, "u \(u)")
118    }
119}