import Testing import simd import FoldFormat @testable import Folding import FoldKitTestSupport let up2 = [SIMD3(0, 0, 1), SIMD3(0, 0, 1)] let up3 = up2 + [SIMD3(0, 0, 1)] @Test func twoStackedCoplanarFacesFormOneClusterOrderedFarthestFirst() throws { let order = try LayerOrder(faceOrders: [[0, 1, 1]], normals: up2) // 0 above 1 let c = order.clusters(positions: stackedSquarePositions(), faces: twoSquares(), viewDirection: SIMD3(0, 0, -1)) #expect(c.count == 1) #expect(c[0].faces == [1, 0]) } @Test func orbitingUnderneathReversesTheClusterOrder() throws { let order = try LayerOrder(faceOrders: [[0, 1, 1]], normals: up2) let above = order.clusters(positions: stackedSquarePositions(), faces: twoSquares(), viewDirection: SIMD3(0, 0, -1)) let below = order.clusters(positions: stackedSquarePositions(), faces: twoSquares(), viewDirection: SIMD3(0, 0, 1)) #expect(above[0].faces == below[0].faces.reversed()) } @Test func facesSeparatedInSpaceFormSeparateClusters() throws { let order = try LayerOrder(faceOrders: [], normals: up2) #expect(order.clusters(positions: separatedSquarePositions(), faces: twoSquares(), viewDirection: SIMD3(0, 0, -1)).count == 2) } @Test func unorderedCoplanarFacesKeepIndexOrder() throws { let order = try LayerOrder(faceOrders: [], normals: up3) #expect(order.clusters(positions: threeStackedPositions(), faces: threeSquares(), viewDirection: SIMD3(0, 0, -1))[0].faces == [0, 1, 2]) } @Test func anArrivingFlapJoinsTheStackItLandsOn() throws { // letter-sequential step 2 at 179.5°: f2 is half a degree above f0 and f1 let (file, step, i) = try interpolatorWithStep("letter-sequential.fold", step: 2) let c = try LayerOrder.active(for: step, in: file) .clusters(positions: i.positions(at: 179.5 / 180), faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1)) #expect(c.contains { Set($0.faces) == [0, 1, 2] && $0.faces.last == 2 }) } @Test func aFlapAtThirtyDegreesIsItsOwnCluster() throws { let (file, step, i) = try interpolatorWithStep("letter-sequential.fold", step: 2) let c = try LayerOrder.active(for: step, in: file) .clusters(positions: i.positions(at: 150.0 / 180), faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1)) #expect(c.contains { $0.faces == [2] }) } @Test func aFoldAndUnfoldPeakIsOrderedByItsMidFoldOrder() throws { // quarters step 1 at the peak: f2 lies on f3 and f1 on f0, the moving faces on top let (file, step, i) = try interpolatorWithStep("quarters.fold", step: 1) let c = try LayerOrder.active(for: step, in: file) .clusters(positions: i.positions(at: 0.5), faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1)) let left = try #require(c.first { $0.faces.contains(3) }) #expect(left.faces.firstIndex(of: 2)! > left.faces.firstIndex(of: 3)!) } @Test func aPageTurningOverAStillLayerStaysInFrontOfItThroughoutTheFold() throws { // S under the hinge, M turning over onto N; the end frame's [0, 2, 1] is written against M's // normal at the end, opposite to its normal at the start let (file, step) = pageTurnOverStillLayer() let order = try LayerOrder.active(for: step, in: file) let interpolator = FoldInterpolator(file: file, step: step) for degrees in [0.5, 179.5] { let c = order.clusters(positions: interpolator.positions(at: degrees / 180), faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1)) let stack = try #require(c.first { $0.faces.contains(0) }) #expect(stack.faces.contains(2) && stack.faces.firstIndex(of: 2)! > stack.faces.firstIndex(of: 0)!, "\(degrees)°: \(c.map(\.faces))") } } @Test func aFlapLeavingAStackIsOrderedByTheStartFrame() throws { // frame 2 is flat and orders nothing, so frame 1's order must apply as f0 lifts off f1 let (file, step) = halfUnfolded() let c = try LayerOrder.active(for: step, in: file) .clusters(positions: FoldInterpolator(file: file, step: step).positions(at: 0.01), faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1)) #expect(c.map(\.faces) == [[1, 0]]) } @Test func aFaceOpeningOnTheSpineIsReadAgainstTheStillFace() throws { // reverse step 2: f3 opens on the spine and closes again while f0 holds; both frames give [0, 3, 1] let file = try compiled("reverse") let step = try readSteps(file)[1] #expect(!step.movingFaceUnion.contains(3)) let pairs = try LayerOrder.active(for: step, in: file).triples.map { [$0.f, $0.g] } #expect(pairs.contains([3, 0]) && !pairs.contains([0, 3])) } @Test func cyclicOrderThrows() { #expect(throws: LayerError.cyclic(cycle: [0, 1, 2])) { try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: up3) } } @Test func aRawCycleThatNormalisesAwayIsAccepted() { // f0 and f2 face down: these triples say f1 < f0, f1 < f2, f2 < f0 #expect(throws: Never.self) { try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: [SIMD3(0, 0, -1), SIMD3(0, 0, 1), SIMD3(0, 0, -1)]) } } @Test func consecutiveOrderingsAgreeAwayFromTheMovingFaces() throws { let file = try FoldFile(contentsOf: fixture("letter-sequential.fold")) for step in try readSteps(file) { let start = try LayerOrder(faceOrders: file.frames[step.index - 1].faceOrders, normals: faceNormals(file.frames[step.index - 1], file)) let end = try LayerOrder(faceOrders: file.frames[step.index].faceOrders, normals: faceNormals(file.frames[step.index], file)) #expect(start.agreesWith(end, ignoring: step.movingFaceUnion), "step \(step.index)") } } @Test func stackDepthCountsTheLayersOverOnePoint() throws { let file = try FoldFile(contentsOf: fixture("letter-sequential.fold")) #expect(stackDepth(positions: file.frames[2].verticesCoords, faces: file.facesVertices) == 3) } @Test func stackDepthOfAFlatSheetIsOne() throws { // four faces meet at the centre vertex let file = try FoldFile(contentsOf: fixture("quarters.fold")) #expect(stackDepth(positions: file.frames[0].verticesCoords, faces: file.facesVertices) == 1) } /// S, f0, a 2 × 1 face at z = 0, under a 2 × 1 top layer of N, f1, on x ∈ [0, 1] and M, f2, on /// x ∈ [1, 2], hinged at x = 1. Step 1 valley folds M over onto N. private func pageTurnOverStillLayer() -> (FoldFile, FoldStep) { let start: [SIMD3] = [ SIMD3(0, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 1, 0), SIMD3(0, 1, 0), SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 1, 0), SIMD3(1, 1, 0), SIMD3(0, 1, 0), ] var end = start end[6] = SIMD3(0, 0, 0) end[7] = SIMD3(0, 1, 0) let boundary = Array(repeating: EdgeAssignment.boundary, count: 10) let file = FoldFile( fileSpec: 1.2, fileAuthor: "Traditional", edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 4], [5, 8]], facesVertices: [[0, 1, 2, 3], [4, 5, 8, 9], [5, 6, 7, 8]], edgesFirstCreasedAtStep: Array(repeating: nil, count: 10) + [1], frames: [ FoldFrame(verticesCoords: start, edgesAssignment: boundary + [.flat], edgesFoldAngle: Array(repeating: 0, count: 11), faceOrders: [[0, 1, -1], [0, 2, -1]]), // normals +z, +z, −z: S below N, S below M, N below M FoldFrame(verticesCoords: end, edgesAssignment: boundary + [.valley], edgesFoldAngle: Array(repeating: 0, count: 10) + [180], faceOrders: [[0, 1, -1], [0, 2, 1], [1, 2, 1]]), ] ) let step = FoldStep( index: 1, kind: .fold, instruction: "Turn the page over.", // M traverses the hinge from v8 down to v5 axes: [FoldAxis(edge: 10, origin: SIMD3(1, 0, 0), direction: SIMD3(0, -1, 0), movingFaces: [2], startAngle: 0, endAngle: 180)], viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)) ) return (file, step) } /// `half.fold`'s sheet with its faces listed right half first, so the flap is f0. Step 1 valley /// folds f0 over f1; step 2 unfolds it flat again. private func halfUnfolded() -> (FoldFile, FoldStep) { let flat: [SIMD3] = [ SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 2, 0), SIMD3(1, 2, 0), SIMD3(0, 2, 0), ] var folded = flat folded[2] = SIMD3(0, 0, 0) folded[3] = SIMD3(0, 2, 0) let boundary = Array(repeating: EdgeAssignment.boundary, count: 6) func frame(_ points: [SIMD3], angle: Double, faceOrders: [[Int]]) -> FoldFrame { FoldFrame(verticesCoords: points, edgesAssignment: boundary + [angle == 0 ? .flat : .valley], edgesFoldAngle: Array(repeating: 0, count: 6) + [angle], faceOrders: faceOrders) } let file = FoldFile( fileSpec: 1.2, fileAuthor: "Traditional", edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], facesVertices: [[1, 2, 3, 4], [0, 1, 4, 5]], edgesFirstCreasedAtStep: Array(repeating: nil, count: 6) + [1], frames: [ frame(flat, angle: 0, faceOrders: []), // f0 faces −z, f1 faces +z: f0 on top frame(folded, angle: 180, faceOrders: [[0, 1, 1]]), frame(flat, angle: 0, faceOrders: []), ] ) let step = FoldStep( index: 2, kind: .fold, instruction: "Unfold.", // f0 traverses the crease from v4 down to v1 axes: [FoldAxis(edge: 6, origin: SIMD3(1, 0, 0), direction: SIMD3(0, -1, 0), movingFaces: [0], startAngle: 180, endAngle: 0)], viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)) ) return (file, step) }