import Foundation import simd import Testing import FoldCompiler import FoldFormat import Folding import FoldKitTestSupport @Test(arguments: ["half", "mountain", "letter", "letter-sequential", "letter-staggered", "quarters", "occlusion"]) func compilingASyntheticScriptReproducesItsHandWrittenFixture(_ name: String) throws { let out = try compile(FoldScript(contentsOf: fixture("\(name).foldscript")), pattern: FoldFile(contentsOf: fixture("\(name)-cp.fold"))) #expect(structurallyEqual(out, try FoldFile(contentsOf: fixture("\(name).fold")), tolerance: 1e-12)) #expect(validate(out).isEmpty) } @Test func aCreaseThroughTwoLayersChangesBothEdges() throws { let out = try compiled("quarters") #expect(out.frames[3].edgesFoldAngle[8] == 180) #expect(out.frames[3].edgesFoldAngle[9] == -180) #expect(out.frames[3].edgesAssignment[9] == .mountain) } @Test func firstCreasedAtStepIsTheFirstStepThatMakesTheCrease() throws { // x = 1 is made by the fold-and-unfold in step 1 and refolded in step 3 #expect(try compiled("quarters").edgesFirstCreasedAtStep == [nil, nil, nil, nil, nil, nil, nil, nil, 1, 1, 2, 2]) } @Test func movingSideIsResolvedFromAContainedPoint() throws { #expect(try compiled("half").frames[1].step?.axes[0].movingFaces == [1]) } @Test func creaseGivenByEndpointsResolvesToAnEdgeIndex() throws { #expect(try compiled("half").frames[1].step?.axes[0].edge == 6) } @Test func anOverrideReplacesTheGuessForThatPair() throws { let file = try FoldFile(contentsOf: fixture("letter-sequential.fold")) let frame = file.frames[2] // the guess puts f2 (folded last) above f0; the override puts f0 above f2 let swapped = try applyOrderOverrides([OrderOverride(above: SIMD2(0.5, 1.5), below: SIMD2(2.5, 1.5))], to: frame.faceOrders, frame: frame, viewpoint: frame.step!.viewpoint, file: file) #expect(swapped != frame.faceOrders) #expect(throws: Never.self) { try LayerOrder(faceOrders: swapped, normals: faceNormals(frame, file)) } } @Test func aCoupledStepIsSolvedIntoCurves() throws { // decision 0001's case 3 as a script: the 140° crease drives, the 260° crease // gets a curve, the 0° crease rides on it, the 60° crease closes the loop let out = try compile(degreeFourScript(), pattern: degreeFourPattern()) let axes = try #require(out.frames[1].step?.axes) #expect(axes.contains { $0.angles != nil }) #expect(axes.contains { $0.parent != nil }) for axis in axes where axis.angles != nil { #expect(abs(axis.angles![128] - degreeFourExactAngle(axis.edge, at: 0.5)) < 1e-6) } } @Test func aDegreeSixVertexCollapsesToAPreliminaryBase() throws { // four midlines and two diagonal halves: two degrees of freedom beyond the driver, held near // their linear angles; the three moving corners land on the still one, flat let out = try compiled("preliminary") let last = out.frames[1].verticesCoords for v in [2, 4, 6] { #expect(simd_distance(last[v], last[0]) < 1e-9, "corner \(v)") } #expect(last.allSatisfy { abs($0.z) < 1e-9 }) } @Test func anUnresolvedCreaseNamesItsStepAndReference() { #expect(throws: CompileError.unresolvedCrease(step: 2, crease: .betweenVertices(SIMD2(0, 1), SIMD2(1, 1)))) { try compile(FoldScript(contentsOf: fixture("unresolved-crease.foldscript")), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aStepThatFailsValidationNamesItsStep() { #expect { try compile(FoldScript(contentsOf: fixture("angle-out-of-range.foldscript")), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } throws: { if case CompileError.stepFailed(step: 2, _) = $0 { true } else { false } } } @Test func guessedFaceOrdersAreAcyclic() throws { let out = try compiled("occlusion") for frame in out.frames { #expect(throws: Never.self) { try LayerOrder(faceOrders: frame.faceOrders, normals: faceNormals(frame, out)) } } } @Test func aClockwiseFaceIsRejected() throws { var pattern = try FoldFile(contentsOf: fixture("half-cp.fold")) pattern.facesVertices[1].reverse() #expect(throws: CompileError.clockwiseFace(face: 1)) { try compile(FoldScript(contentsOf: fixture("half.foldscript")), pattern: pattern) } } @Test func aCoupledStepReproducesTheClosedForm() throws { #expect(structurallyEqual(try compile(degreeFourScript(), pattern: degreeFourPattern()), degreeFourVertex(), tolerance: 1e-9)) } private func halfFold(angles: [Double]? = nil, peakAngles: [Double]? = nil, kind: StepKind = .fold, movingSide: SIMD2 = SIMD2(1.5, 1), faceOrders: [OrderOverride]? = nil, spine: CreaseRef? = nil, openOn: CreaseRef? = nil) -> FoldScript { FoldScript(creasePattern: "half-cp.fold", title: "Half", steps: [ ScriptStep(kind: kind, instruction: "Fold the right half.", creases: [.betweenVertices(SIMD2(1, 0), SIMD2(1, 2))], movingSide: [movingSide], angles: angles, peakAngles: peakAngles, faceOrders: faceOrders, spine: spine, openOn: openOn), ]) } @Test func aCreaseFoldedAgainstThePatternIsRejected() throws { #expect(throws: CompileError.assignmentDisagreesWithPattern(edge: 6)) { try compile(halfFold(angles: [-180]), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aMovingSideOffTheSheetIsRejected() throws { #expect(throws: CompileError.unresolvedMovingSide(step: 1)) { try compile(halfFold(movingSide: SIMD2(5, 5)), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func anOverrideOffTheSheetNamesItsStep() throws { let override = OrderOverride(above: SIMD2(0.5, 1), below: SIMD2(5, 5)) #expect(throws: CompileError.unresolvedFace(step: 1, point: SIMD2(5, 5))) { try compile(halfFold(faceOrders: [override]), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func anUnassignedCreaseNeedsAnAngle() throws { var pattern = try FoldFile(contentsOf: fixture("half-cp.fold")) pattern.frames[0].edgesAssignment[6] = .flat #expect(throws: CompileError.missingAngle(step: 1, crease: .betweenVertices(SIMD2(1, 0), SIMD2(1, 2)))) { try compile(halfFold(kind: .foldAndUnfold), pattern: pattern) } } @Test func anglesPairWithCreases() throws { #expect(throws: CompileError.creaseListMismatch(step: 1)) { try compile(halfFold(angles: [180, 180]), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aSpineThatIsNotFoldedIsRejected() throws { // a flap opens on a spine folded flat in the frame the step starts from #expect(throws: CompileError.invalidSpine(step: 1)) { try compile(halfFold(spine: .edgeIndex(6)), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func anOpeningCreaseWithoutASpineIsRejected() throws { #expect(throws: CompileError.missingSpine(step: 1)) { try compile(halfFold(openOn: .edgeIndex(6)), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aLoopCreaseScriptedAtTheWrongAngleHasNoRigidMotion() throws { let step = degreeFourScript().steps[0] let script = FoldScript(creasePattern: "", title: "Degree-4 vertex", steps: [ ScriptStep(kind: .fold, instruction: step.instruction, creases: step.creases, movingSide: step.movingSide, angles: [180, 180, 180, 180]), ]) #expect(throws: CompileError.noRigidMotion(step: 1)) { try compile(script, pattern: degreeFourPattern()) } } @Test func twoFlapsFoldedIntoEachOtherAreRejected() throws { let script = FoldScript(creasePattern: "letter-sequential-cp.fold", title: "Letter", steps: [ ScriptStep(kind: .fold, instruction: "Valley fold both thirds over at once.", creases: [.betweenVertices(SIMD2(1, 0), SIMD2(1, 3)), .betweenVertices(SIMD2(2, 0), SIMD2(2, 3))], movingSide: [SIMD2(0.5, 1.5), SIMD2(2.5, 1.5)]), ]) #expect { try compile(script, pattern: FoldFile(contentsOf: fixture("letter-sequential-cp.fold"))) } throws: { if case CompileError.interpenetration(step: 1, _) = $0 { true } else { false } } } @Test func aCreaseNamedTwiceIsUnresolved() throws { let script = FoldScript(creasePattern: "half-cp.fold", title: "Half", steps: [ ScriptStep(kind: .fold, instruction: "Fold the right half.", creases: [.edgeIndex(6), .edgeIndex(6)], movingSide: [SIMD2(1.5, 1)]), ]) #expect(throws: CompileError.unresolvedCrease(step: 1, crease: .edgeIndex(6))) { try compile(script, pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aSpineOnAStepThatIsNotAFoldIsRejected() throws { #expect(throws: CompileError.invalidSpine(step: 1)) { try compile(halfFold(kind: .foldAndUnfold, spine: .edgeIndex(6)), pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aDriverThatIsNotAStepCreaseIsUnresolved() throws { let script = FoldScript(creasePattern: "half-cp.fold", title: "Half", steps: [ ScriptStep(kind: .fold, instruction: "Fold the right half.", creases: [.edgeIndex(6)], movingSide: [SIMD2(1.5, 1)], driver: .edgeIndex(0)), ]) #expect(throws: CompileError.unresolvedCrease(step: 1, crease: .edgeIndex(0))) { try compile(script, pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } @Test func aFoldWithNoCreaseIsRejected() throws { let script = FoldScript(creasePattern: "half-cp.fold", title: "Half", steps: [ ScriptStep(kind: .fold, instruction: "Fold the right half.", movingSide: [SIMD2(1.5, 1)]), ]) #expect(throws: CompileError.noCreases(step: 1)) { try compile(script, pattern: FoldFile(contentsOf: fixture("half-cp.fold"))) } } /// `letter-sequential-cp.fold`'s sheet with the x = 1 crease split at (1, 1.5) into e8 (lower) /// and e10 (upper), so folding along it closes a loop of two coincident edges. private func splitLetterPattern() -> FoldFile { let points: [SIMD3] = [ SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(3, 0, 0), SIMD3(3, 3, 0), SIMD3(2, 3, 0), SIMD3(1, 3, 0), SIMD3(0, 3, 0), SIMD3(1, 1.5, 0), ] let assignment = Array(repeating: EdgeAssignment.boundary, count: 8) + [.valley, .valley, .valley] return FoldFile(fileSpec: 1.2, fileTitle: "Split letter", fileAuthor: "Traditional", fileClasses: ["creasePattern"], edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 8], [2, 5], [8, 6]], facesVertices: [[0, 1, 8, 6, 7], [1, 2, 5, 6, 8], [2, 3, 4, 5]], edgesFirstCreasedAtStep: Array(repeating: nil, count: 11), frames: [FoldFrame(verticesCoords: points, edgesAssignment: assignment, edgesFoldAngle: Array(repeating: 0, count: 11), faceOrders: [])]) } @Test func anAxisOffEveryLoopKeepsItsWindowAndStaysLinear() throws { let script = FoldScript(creasePattern: "", title: "Split letter", steps: [ ScriptStep(kind: .fold, instruction: "Valley fold the left third over, then the right third over the top.", creases: [.betweenVertices(SIMD2(1, 0), SIMD2(1, 1.5)), .betweenVertices(SIMD2(2, 0), SIMD2(2, 3))], movingSide: [SIMD2(0.5, 1.5), SIMD2(2.5, 1.5)], windows: [[0, 0.5], [0.5, 1]]), ]) let axes = try #require(try compile(script, pattern: splitLetterPattern()).frames[1].step?.axes) let flap = try #require(axes.first { $0.edge == 9 }) #expect(flap.window == [0.5, 1]) #expect(flap.angles == nil) } @Test func aSolvedAxisScriptedOnAnotherWindowIsRejected() throws { let step = degreeFourScript().steps[0] let script = FoldScript(creasePattern: "", title: "Degree-4 vertex", steps: [ ScriptStep(kind: .fold, instruction: step.instruction, creases: step.creases, movingSide: step.movingSide, windows: [[0, 1], [0, 0.5], [0, 1], [0, 1]]), ]) #expect(throws: CompileError.solvedAxisWindowConflict(step: 1, edge: 11)) { try compile(script, pattern: degreeFourPattern()) } } /// A 4 × 1 strip with valley creases at x = 1 (e10) and x = 2 (e11) and a mountain at x = 3 (e12). /// Folded in half at x = 2, e12 lies on e10's line in a separate layer. private func stripPattern() -> FoldFile { let points: [SIMD3] = [ SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(3, 0, 0), SIMD3(4, 0, 0), SIMD3(4, 1, 0), SIMD3(3, 1, 0), SIMD3(2, 1, 0), SIMD3(1, 1, 0), SIMD3(0, 1, 0), ] let assignment = Array(repeating: EdgeAssignment.boundary, count: 10) + [.valley, .valley, .mountain] return FoldFile(fileSpec: 1.2, fileTitle: "Strip", fileAuthor: "Traditional", fileClasses: ["creasePattern"], edgesVertices: (0..<10).map { [$0, ($0 + 1) % 10] } + [[1, 8], [2, 7], [3, 6]], facesVertices: [[0, 1, 8, 9], [1, 2, 7, 8], [2, 3, 6, 7], [3, 4, 5, 6]], edgesFirstCreasedAtStep: Array(repeating: nil, count: 13), frames: [FoldFrame(verticesCoords: points, edgesAssignment: assignment, edgesFoldAngle: Array(repeating: 0, count: 13), faceOrders: [])]) } @Test func aCoincidentAxisUnderAScriptedAngleIsRejected() throws { // step 2's crease at x = 1 cuts both layers; the upper layer's edge, e12, is an axis of its own let script = FoldScript(creasePattern: "", title: "Strip", steps: [ ScriptStep(kind: .fold, instruction: "Valley fold in half.", creases: [.betweenVertices(SIMD2(2, 0), SIMD2(2, 1))], movingSide: [SIMD2(3, 0.5)]), ScriptStep(kind: .fold, instruction: "Fold the end up to stand.", creases: [.betweenVertices(SIMD2(1, 0), SIMD2(1, 1))], movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)], angles: [90]), ]) #expect(throws: CompileError.coincidentAxisAngle(step: 2, edge: 12)) { try compile(script, pattern: stripPattern()) } } @Test func aCoincidentAxisFoldedFlatCompiles() throws { let script = FoldScript(creasePattern: "", title: "Strip", steps: [ ScriptStep(kind: .fold, instruction: "Valley fold in half.", creases: [.betweenVertices(SIMD2(2, 0), SIMD2(2, 1))], movingSide: [SIMD2(3, 0.5)]), ScriptStep(kind: .fold, instruction: "Fold the end over.", creases: [.betweenVertices(SIMD2(1, 0), SIMD2(1, 1))], movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)], angles: [180]), ]) let out = try compile(script, pattern: stripPattern()) #expect(out.frames[2].edgesFoldAngle[10] == 180) #expect(out.frames[2].edgesFoldAngle[12] == -180) } @Test func anOverrideThatPutsAFlapUnderTheLayerItArrivesOnIsRejected() throws { // step 2's flap folds over the top, so it cannot end under f0 var script = try FoldScript(contentsOf: fixture("letter-sequential.foldscript")) let step = script.steps[1] script = FoldScript(creasePattern: script.creasePattern, title: script.title, steps: [ script.steps[0], ScriptStep(kind: step.kind, instruction: step.instruction, creases: step.creases, movingSide: step.movingSide, repeatOf: step.repeatOf, faceOrders: [OrderOverride(above: SIMD2(0.5, 1.5), below: SIMD2(2.5, 1.5))]), ]) #expect(throws: CompileError.stepFailed(step: 2, defects: [.movesThroughStillFace(step: 2, moving: 2, still: 0)])) { try compile(script, pattern: FoldFile(contentsOf: fixture("letter-sequential-cp.fold"))) } } @Test func aFlapThroughAStillFaceFailsItsStepBeforeLaterSteps() throws { // step 2 moves through a still face; step 3 has no moving side on the sheet let script = try FoldScript(contentsOf: fixture("letter-sequential.foldscript")) let (first, second) = (script.steps[0], script.steps[1]) let failing = FoldScript(creasePattern: script.creasePattern, title: script.title, steps: [ first, ScriptStep(kind: second.kind, instruction: second.instruction, creases: second.creases, movingSide: second.movingSide, repeatOf: second.repeatOf, faceOrders: [OrderOverride(above: SIMD2(0.5, 1.5), below: SIMD2(2.5, 1.5))]), ScriptStep(kind: .fold, instruction: "Fold off the sheet.", creases: first.creases, movingSide: [SIMD2(5, 5)]), ]) #expect(throws: CompileError.stepFailed(step: 2, defects: [.movesThroughStillFace(step: 2, moving: 2, still: 0)])) { try compile(failing, pattern: FoldFile(contentsOf: fixture("letter-sequential-cp.fold"))) } }