import Testing @testable import FoldCompiler import FoldFormat import Folding import Foundation @Test func aSecondFoldAndUnfoldAxisWithCollinearEdgesTurnsBackAtItsPeak() throws { let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern()) let step = try readStep(1, in: file) for axis in step.axes { for k in 0...256 { let angle = axis.angle(at: Double(k) / 256, kind: step.kind) #expect(angle >= -1e-9 && angle <= 180 + 1e-9, "edge \(axis.edge) t \(Double(k) / 256)") } #expect(abs(axis.angle(at: 0.5, kind: step.kind) - 180) < 1e-9) #expect(abs(axis.angle(at: 1, kind: step.kind)) < 1e-9) } } @Test func anAxisWithCollinearEdgesTakesItsOwnWindow() throws { let file = try compile(strips(.fold, windows: [[0, 0.5], [0.5, 1]]), pattern: stripsPattern()) #expect(try readStep(1, in: file).axes.map(\.window) == [0...0.5, 0.5...1]) } @Test func anAxisAngleOutsideItsTravelIsRejected() throws { // the second strip's angle carried on past its peak to a full turn let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern()) let step = try readStep(1, in: file) let a = step.axes[1] let runaway = FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces, startAngle: a.startAngle, endAngle: a.endAngle, peakAngle: a.peakAngle, parent: a.parent, window: a.window, curve: (0...256).map { 360 * Double($0) / 256 }) let broken = FoldStep(index: 1, kind: step.kind, instruction: step.instruction, axes: [step.axes[0], runaway], viewpoint: step.viewpoint) #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: a.edge)) { try checkTravel(1, broken) } } @Test func aSolvedCurveMayBendPastItsEndAnglesWithinAHalfTurn() throws { // a coupled crease flat at both ends that bends mid-step, and a hold-then-rise curve whose // interpolation undershoots its start let file = try compile(strips(.fold, windows: nil), pattern: stripsPattern()) let a = try readStep(1, in: file).axes[1] let bend = (0...256).map { 120 * sin(.pi * Double($0) / 256) } let rise = (0...256).map { $0 < 200 ? 0 : 90 * Double($0 - 199) / 57 } for (end, curve) in [(0.0, bend), (90.0, rise)] { let axis = FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces, startAngle: 0, endAngle: end, window: a.window, curve: curve) #expect(axis.angle(at: 0.775, kind: .fold) < 0 || end == 0) try checkTravel(1, FoldStep(index: 1, kind: .fold, instruction: "", axes: [axis], viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)))) } } @Test func compilingRejectsAnAngleBeyondAHalfTurn() throws { let script = FoldScript(creasePattern: "", title: "Strips", steps: [ ScriptStep(kind: .foldAndUnfold, instruction: "Fold both ends in and unfold.", creases: [.edgeIndex(10), .edgeIndex(12)], movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)], peakAngles: [180, 200]), ]) #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: 12)) { try compile(script, pattern: stripsPattern()) } } /// A 4 × 2 sheet with creases x = 1 and x = 3, each split by y = 1 into two collinear edges. private func stripsPattern() -> FoldFile { let points: [SIMD3] = [[0, 0, 0], [1, 0, 0], [3, 0, 0], [4, 0, 0], [4, 1, 0], [4, 2, 0], [3, 2, 0], [1, 2, 0], [0, 2, 0], [0, 1, 0], [1, 1, 0], [3, 1, 0]] let edges = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 0], [1, 10], [10, 7], [2, 11], [11, 6], [9, 10], [10, 11], [11, 4]] let assignment: [EdgeAssignment] = Array(repeating: .boundary, count: 10) + [.valley, .valley, .valley, .valley, .flat, .flat, .flat] return FoldFile(fileSpec: 1.2, fileTitle: "Strips", fileAuthor: "Traditional", fileClasses: ["creasePattern"], edgesVertices: edges, facesVertices: [[0, 1, 10, 9], [9, 10, 7, 8], [1, 2, 11, 10], [10, 11, 6, 7], [2, 3, 4, 11], [11, 4, 5, 6]], edgesFirstCreasedAtStep: edges.map { _ in nil }, frames: [FoldFrame(verticesCoords: points, edgesAssignment: assignment, edgesFoldAngle: edges.map { _ in 0 }, faceOrders: [])]) } /// One step folding both end strips in over the middle, about the lower edges of x = 1 and x = 3. private func strips(_ kind: StepKind, windows: [[Double]]? = nil) -> FoldScript { FoldScript(creasePattern: "", title: "Strips", steps: [ ScriptStep(kind: kind, instruction: "Fold both ends in.", creases: [.edgeIndex(10), .edgeIndex(12)], movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)], angles: kind == .fold ? [180, 180] : nil, peakAngles: kind == .foldAndUnfold ? [180, 180] : nil, windows: windows), ]) }