import Foundation import Testing import simd import FoldCompiler import FoldFormat import Folding import FoldKitTestSupport @Test func aReverseFoldCompilesToANonRigidStep() throws { let out = try compiled("reverse") let step = try #require(out.frames[2].step) #expect(step.nonRigid) #expect(step.pop?.openAngle == 20) #expect(step.technique == .insideReverse) } @Test func aReverseFoldWithoutASpineIsRejected() { #expect(throws: CompileError.missingSpine(step: 2)) { try compile(FoldScript(contentsOf: fixture("reverse.foldscript")).withoutSpine(step: 2), pattern: FoldFile(contentsOf: fixture("reverse-cp.fold"))) } } @Test func theOpenAndCloseLegsAreRigid() throws { // checked on the FOLD edges, as rigid steps are; mesh slivers at creases make relative mesh strain noisy let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) for n in 0...256 { let t = Double(n) / 256 guard t <= step.pop!.phases.lowerBound || t >= step.pop!.phases.upperBound else { continue } #expect(maxEdgeLengthDrift(i.positions(at: t), file) < 1e-6 * sheetSize(file), "t \(t)") } } @Test func thePopStaysWithinTheBound() throws { let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let worst = (0...64).map { strain(i.meshPositions(at: lo + Double($0) / 64 * (hi - lo)), mesh: i.mesh) }.max()! #expect(worst <= 0.10) } @Test func theBandedPopStrainsTheFixtureLessThanPerFaceTurns() throws { // decision 0003 at 20° from flat: 3.08% banded, 4.36% with each face turned on its own let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let worst = (0...64).map { strain(i.meshPositions(at: lo + Double($0) / 64 * (hi - lo)), mesh: i.mesh) }.max()! #expect(worst < 0.035) } @Test func thePopStartsAndEndsOnTheRigidPoses() throws { let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) // at the phases' ends the mesh is placed rigidly; just inside them it is bent for (bent, rigid) in [(lo + 1e-12, lo), (hi - 1e-12, hi)] { #expect(zip(i.meshPositions(at: bent), i.meshPositions(at: rigid)).allSatisfy { simd_distance($0, $1) < 1e-9 }, "t \(rigid)") } } @Test func theStepEndsOnItsEndFrame() throws { let file = try compiled("reverse") let i = FoldInterpolator(file: file, step: try readSteps(file)[1]) #expect(zip(i.positions(at: 1), file.frames[2].verticesCoords).allSatisfy { simd_distance($0, $1) < 1e-9 }) } @Test func nothingPassesThroughAnything() throws { // in all three phases, on the render mesh let file = try compiled("reverse") let i = FoldInterpolator(file: file, step: try readSteps(file)[1]) #expect(meshInterpenetrations(i, samples: 64).isEmpty) } @Test func theTipLandsBetweenTheFlapsLayers() throws { // 97% of the way through the closing phase, a point of the tip lies between the body's two layers let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) let upper = step.pop!.phases.upperBound #expect(tipIsBetweenBodyLayers(i.positions(at: upper + 0.97 * (1 - upper)), file)) } @Test func aFlapOpenedOnlyTo60DegreesIsRejected() { #expect { try compile(FoldScript(contentsOf: fixture("reverse.foldscript")).withOpenAngle(60, step: 2), pattern: FoldFile(contentsOf: fixture("reverse-cp.fold"))) } throws: { if case CompileError.popTooStrained(step: 2, _) = $0 { true } else { false } } } @Test func thePopFitsInAFrameBudget() throws { let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let elapsed = ContinuousClock().measure { for n in 0..<100 { _ = i.meshPositions(at: lo + Double(n) / 100 * (hi - lo)) } } #expect(elapsed / 100 < .milliseconds(4)) } @Test func aBendLineOnTheSpineFoldsWhileTheFlapIsOpenAndFlattensAgain() throws { // decision 0003: the tab's bend line (1 of its 2 faces beyond the spine's line) is cut with the // spine, so the tab's near half opens with the flap; the line is never creased let file = try compiled("reverse-bend") let bend = 18 #expect(file.edgesFirstCreasedAtStep[bend] == nil) #expect(file.frames.allSatisfy { $0.edgesFoldAngle[bend] == 0 && $0.edgesAssignment[bend] == .flat }) let step = try readSteps(file)[2] let i = FoldInterpolator(file: file, step: step) let open = i.positions(at: step.pop!.phases.lowerBound) let normals = faceNormals(positions: open, faces: file.facesVertices) let between = acos(simd_dot(normals[4], normals[5])) * 180 / .pi #expect(abs(between - 160) < 1e-6) #expect(meshInterpenetrations(i, samples: 64).isEmpty) } @Test func aNestedReverseFoldOpensOnTheOuterSpine() throws { // decision 0003: the head reopens the neck by turning the body's near side again, the neck's // creases solved on their reversed branch, then pops and closes between the neck's layers let file = try compiled("reverse-nested") let step = try readSteps(file)[2] let pop = try #require(step.pop) #expect(pop.openAxes.first?.edge == 10) #expect(Set(pop.openAxes.map(\.edge)).isSuperset(of: [13, 14])) #expect(pop.openAxes.contains { $0.curve != nil }) let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (pop.phases.lowerBound, pop.phases.upperBound) for n in 0...256 { let t = Double(n) / 256 guard t <= lo || t >= hi else { continue } #expect(maxEdgeLengthDrift(i.positions(at: t), file) < 1e-6 * sheetSize(file), "t \(t)") } let worst = (0...64).map { strain(i.meshPositions(at: lo + Double($0) / 64 * (hi - lo)), mesh: i.mesh) }.max()! #expect(worst <= 0.10) #expect(meshInterpenetrations(i, samples: 64).isEmpty) #expect(pointIsBetweenLayers(i.positions(at: hi + 0.97 * (1 - hi)), file, point: SIMD2(3.9, 0.9), lower: SIMD2(3.2, 0.5), upper: SIMD2(3.2, 1.5))) #expect(layerMotionDefects(file).isEmpty) } @Test func aNestedOpeningLeavesABendLineOnItsOpeningCreaseUncreased() throws { // the head reopens on the body's spine, whose line holds the tab's bend line (edge 23) let file = try compiled("reverse-nested-bend") let bend = 23 #expect(file.edgesFirstCreasedAtStep[bend] == nil) #expect(file.frames.allSatisfy { $0.edgesFoldAngle[bend] == 0 && $0.edgesAssignment[bend] == .flat }) #expect(try readSteps(file)[3].pop?.spine.contains(bend) == true) #expect(validate(file).isEmpty) } @Test func anOpeningCreaseNoEarlierFoldOpenedIsRejected() throws { let script = try FoldScript(contentsOf: fixture("reverse.foldscript")) let s = script.steps[1] let reopened = ScriptStep(kind: s.kind, technique: s.technique, instruction: s.instruction, creases: s.creases, movingSide: s.movingSide, faceOrders: s.faceOrders, spine: s.spine, openOn: s.spine) #expect(throws: CompileError.noEarlierOpening(step: 2)) { try compile(FoldScript(creasePattern: script.creasePattern, title: script.title, steps: [script.steps[0], reopened]), pattern: FoldFile(contentsOf: fixture("reverse-cp.fold"))) } } @Test func theReverseFoldMovesThroughNoStillFace() throws { // the open phase departs from frame 1 and the close phase arrives at frame 2 #expect(layerMotionDefects(try compiled("reverse")).isEmpty) } @Test func popsCompareWithinTheTolerance() throws { let file = try compiled("reverse") func nudged(by d: Double) -> FoldFile { let record = file.frames[2].step!, p = record.pop! let close = p.closeAxes.map { AxisRecord(edge: $0.edge, movingFaces: $0.movingFaces, startAngle: $0.startAngle + d, endAngle: $0.endAngle, peakAngle: $0.peakAngle, parent: $0.parent, window: $0.window, angles: $0.angles?.map { $0 + d }) } let pop = PopRecord(spine: p.spine, openAngle: p.openAngle + d, phases: p.phases.map { $0 + d }, openAxes: p.openAxes, popped: p.popped.map { $0 + d }, closeAxes: close) var copy = file copy.frames[2].step = StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, nonRigid: record.nonRigid, pop: pop, viewpoint: record.viewpoint) return copy } #expect(structurallyEqual(nudged(by: 1e-13), file, tolerance: 1e-12)) #expect(!structurallyEqual(nudged(by: 1e-9), file, tolerance: 1e-12)) } @Test func concurrentPopSolvesAgreeWithSerialOnes() async throws { // one interpolator, so one factor, solved from eight tasks at once let file = try compiled("reverse") let step = try readSteps(file)[1] let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let times = (1..<64).map { lo + Double($0) / 64 * (hi - lo) } let serial = times.map { i.meshPositions(at: $0) } let mismatches = await withTaskGroup(of: Int.self) { group in for _ in 0..<8 { group.addTask { var count = 0 for _ in 0..<20 { for (t, expected) in zip(times, serial) where i.meshPositions(at: t) != expected { count += 1 } } return count } } return await group.reduce(0, +) } #expect(mismatches == 0) }