import Foundation import Testing import simd import FoldFormat import Folding import FoldKitTestSupport import Models @Test func theCatalogueContainsFourModels() throws { #expect(try Set(ModelLibrary.names()) == ["cup", "crane", "sampan", "box"]) } @Test func theCraneContainsAMultiAxisStep() throws { #expect(try readSteps(ModelLibrary.model(named: "crane")).contains { $0.axes.count > 1 }) } @Test func theCraneStacksAtLeastEightLayersSomewhere() throws { let crane = try ModelLibrary.model(named: "crane") let deepest = (0...crane.stepCount).map { stackDepth(positions: crane.frames[$0].verticesCoords, faces: crane.facesVertices) }.max() ?? 0 #expect(deepest >= 8) } @Test func everyModelWithOverlappingMovingSetsCarriesAMidFoldOrder() throws { for file in try ModelLibrary.all() { for n in 1...file.stepCount where movingSetsOverlapInProjection(step: n, file) { #expect(file.frames[n].faceOrdersMidFold != nil, "\(file.fileTitle ?? "?") step \(n)") } } } @Test func onlyTheCranesReverseFoldsAreFlagged() throws { // decision 0002: the neck, tail and head reverse folds; nothing else let crane = try readSteps(ModelLibrary.model(named: "crane")) #expect(crane.filter(\.nonRigid).count == 3) #expect(crane.filter(\.nonRigid).allSatisfy { $0.technique == .insideReverse }) for name in ["cup", "box", "sampan"] { #expect(try readSteps(ModelLibrary.model(named: name)).allSatisfy { !$0.nonRigid }, "\(name)") } } /// The crane's reverse folds: the step, a sheet point of its tip, and sheet points of the faces /// the tip lands between, the upper one in the half of the square nearer the eye. private let craneReverseFolds: [(step: Int, tip: SIMD2, lower: SIMD2, upper: SIMD2)] = [ (13, SIMD2(0.01295, 0.64699), SIMD2(0.42511, 0.6469), SIMD2(0.3531, 0.57489)), (14, SIMD2(0.64666, 0.02868), SIMD2(0.6469, 0.42511), SIMD2(0.57489, 0.3531)), (15, SIMD2(0.00626, 0.88539), SIMD2(0.25767, 0.86361), SIMD2(0.13639, 0.74233)), ] @Test func theCranesReverseFoldsOpenAndCloseRigidly() throws { // decision 0003: only the pop bends let crane = try ModelLibrary.model(named: "crane") for fold in craneReverseFolds { let step = try readStep(fold.step, in: crane) let pop = try #require(step.pop) let i = FoldInterpolator(file: crane, step: step) for k in 0...256 { let t = Double(k) / 256 guard t <= pop.phases.lowerBound || t >= pop.phases.upperBound else { continue } #expect(maxEdgeLengthDrift(i.positions(at: t), crane) < 1e-6 * sheetSize(crane), "step \(fold.step) t \(t)") } } } @Test func theCranesPopsStayWithinTheBoundAndPassThroughNothing() throws { // decision 0003 and R78: under 10% edge strain; no mesh interpenetration in any phase let crane = try ModelLibrary.model(named: "crane") for fold in craneReverseFolds { let step = try readStep(fold.step, in: crane) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let i = FoldInterpolator(file: crane, step: step) let worst = (0...64).map { strain(i.meshPositions(at: lo + Double($0) / 64 * (hi - lo)), mesh: i.mesh) }.max()! #expect(worst <= 0.10, "step \(fold.step)") #expect(meshInterpenetrations(i, samples: 64).isEmpty, "step \(fold.step)") } } @Test func theCranesTipsLandBetweenTheLayers() throws { let crane = try ModelLibrary.model(named: "crane") for fold in craneReverseFolds { let step = try readStep(fold.step, in: crane) let hi = step.pop!.phases.upperBound let positions = FoldInterpolator(file: crane, step: step).positions(at: hi + 0.97 * (1 - hi)) #expect(pointIsBetweenLayers(positions, crane, point: fold.tip, lower: fold.lower, upper: fold.upper), "step \(fold.step)") } } @Test func theCranesHeadReversesOnlyTheEdgesJoiningThePapersHalves() throws { // R77: the legs' former edge folds lie on the neck's spine line but join layers of one half let crane = try ModelLibrary.model(named: "crane") let step = try readStep(15, in: crane) let pop = try #require(step.pop) let flat = crane.frames[0].verticesCoords let edgeFaces = edgesFaces(crane) // the halves of the square either side of its diagonal x + y = 1 func half(_ f: Int) -> Bool { let sum = crane.facesVertices[f].reduce(SIMD3.zero) { $0 + flat[$1] } return sum.x + sum.y < Double(crane.facesVertices[f].count) } let tipSpine = pop.spine.filter { edgeFaces[$0].allSatisfy(step.movingFaceUnion.contains) } #expect(!tipSpine.isEmpty) #expect(tipSpine.allSatisfy { half(edgeFaces[$0][0]) != half(edgeFaces[$0][1]) }) } @Test func theCranesPopsAreDraggedAsRecorded() throws { // R75: whether, at each reverse fold's authored viewpoint, the drag follows the pop or falls // back to a straight line down (docs/authoring/crane.md) let crane = try ModelLibrary.model(named: "crane") var fallbacks: [Int: Bool] = [:] for fold in craneReverseFolds { let step = try readStep(fold.step, in: crane) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let m = FoldParameterMapping(file: crane, step: step, camera: sessionCamera(crane, step, aspect: 400.0 / 600), viewportSize: SIMD2(400, 600)) let start = m.point(at: lo) fallbacks[fold.step] = (1...16).allSatisfy { k in let t = lo + Double(k) / 16 * (hi - lo) return simd_distance(m.point(at: t) - start, SIMD2(0, (t - lo) * 160)) < 1e-6 } print("crane step \(fold.step): pop \(fallbacks[fold.step]! ? "falls back" : "followed")") } #expect(fallbacks == [13: false, 14: false, 15: true]) }