import Foundation import Testing import FoldFormat import FoldKitTestSupport @Test(arguments: ["square.fold", "half.fold", "mountain.fold", "letter.fold", "letter-sequential.fold", "letter-staggered.fold", "quarters.fold", "occlusion.fold"]) func acceptsAValidModel(_ name: String) throws { #expect(validate(try FoldFile(contentsOf: fixture(name))).isEmpty) } @Test func oppositeNormalsMakeAMutualAboveValid() { // [f, g, +1] and [g, f, +1] agree when the faces face opposite ways let pairs = normalisedFaceOrders([[0, 1, 1], [1, 0, 1]], normals: [SIMD3(0, 0, 1), SIMD3(0, 0, -1)]) #expect(Set(pairs.map { [$0.below, $0.above] }) == [[0, 1]]) } @Test func rejectsAnOrderTheCreaseMakesImpossible() throws { #expect(validate(try FoldFile(contentsOf: fixture("order-against-crease.fold"))) .contains(.orderDisagreesWithCrease(frame: 1, edge: 6))) } @Test func rejectsInterleavedFolds() throws { let defects = validate(try FoldFile(contentsOf: fixture("interleaved-folds.fold"))) #expect(defects.contains { if case .interleavedFolds(frame: 3, _) = $0 { return true }; return false }) } @Test func rejectsAFaceInsideAFoldItCrosses() throws { let defects = validate(try FoldFile(contentsOf: fixture("face-inside-fold.fold"))) #expect(defects.contains(.faceInsideFold(frame: 2, face: 2, edge: 8))) } @Test func rejectsAFoldAndUnfoldWithoutItsPeakOrder() throws { var file = try FoldFile(contentsOf: fixture("quarters.fold")) file.frames[1].faceOrdersMidFold = nil #expect(validate(file).contains(.foldAndUnfoldWithoutPeakOrder(step: 1))) } @Test func rejectsANonConvexFace() throws { #expect(validate(try FoldFile(contentsOf: fixture("nonconvex.fold"))) == [.nonConvexFace(face: 0)]) } @Test func rejectsAFrameThatChangesTheVertexCount() throws { let defects = validate(try FoldFile(contentsOf: fixture("retopologised.fold"))) #expect(defects.contains(.vertexCountChanged(frame: 2))) } @Test func rejectsOverlappingMovingSetsWithNoMidFoldOrder() throws { let defects = validate(try FoldFile(contentsOf: fixture("letter-no-midfold.fold"))) #expect(defects == [.overlappingMovingSetsWithoutMidFoldOrder(step: 1)]) } @Test func rejectsStartAndEndOrderDisagreementOnStillFaces() throws { let defects = validate(try FoldFile(contentsOf: fixture("still-face-reorder.fold"))) #expect(defects.contains { if case .startAndEndOrderDisagreeOnStillFaces(step: 2, _, _) = $0 { return true }; return false }) } @Test func rejectsCyclicFaceOrder() throws { let defects = validate(try FoldFile(contentsOf: fixture("cyclic-order.fold"))) #expect(defects.contains { if case .cyclicFaceOrder(frame: 2, _) = $0 { return true }; return false }) } @Test func rejectsNonIsometricTransition() throws { let defects = validate(try FoldFile(contentsOf: fixture("stretched-step.fold"))) #expect(defects.contains { if case .nonIsometricTransition(step: 1, _, _, _) = $0 { return true }; return false }) } @Test func rejectsFoldAngleOutsideRange() throws { #expect(validate(try FoldFile(contentsOf: fixture("angle-200.fold"))) == [.foldAngleOutOfRange(frame: 1, edge: 6, degrees: 200)]) } @Test func rejectsAFoldAngleWhoseSignDisagreesWithItsAssignment() throws { #expect(validate(try FoldFile(contentsOf: fixture("angle-sign.fold"))) == [.foldAngleSignDisagreesWithAssignment(frame: 1, edge: 6)]) } @Test func rejectsAFoldedBoundaryEdge() throws { let defects = validate(try FoldFile(contentsOf: fixture("folded-boundary.fold"))) #expect(defects.contains(.boundaryEdgeFolded(frame: 1, edge: 0))) } @Test func rejectsAStepWithoutAnInstruction() throws { #expect(validate(try FoldFile(contentsOf: fixture("no-instruction.fold"))) == [.missingInstruction(step: 1)]) } @Test func rejectsANonTraditionalAuthor() throws { #expect(validate(try FoldFile(contentsOf: fixture("designed-model.fold"))) == [.authorNotTraditional("A. Designer")]) } @Test func rejectsEdgeReferencingMissingVertex() throws { #expect(validate(try FoldFile(contentsOf: fixture("bad-index.fold"))) == [.vertexIndexOutOfRange(edge: 0)]) } @Test func rejectsAFoldAndUnfoldCreaseWithoutFirstCreasedAtStep() throws { // without step 3, x = 1 is only folded and unfolded in step 1, so e8 is F and flat throughout var file = try FoldFile(contentsOf: fixture("quarters.fold")) file.frames.removeLast() file.edgesFirstCreasedAtStep[8] = nil file.edgesFirstCreasedAtStep[9] = nil // step 1 moves f1 and f2 and holds f0 and f3, so it creases e9 (f2 | f3) as well as e8 (f0 | f1) #expect(validate(file) == [.missingFirstCreasedAtStep(edge: 8), .missingFirstCreasedAtStep(edge: 9)]) } @Test func rejectsAShearedFaceWhoseEdgesKeepTheirLengths() throws { // f1 = [v1, v2, v3, v4] in half.fold's frame 1, sheared into a parallelogram on e6 = v1–v4 var file = try FoldFile(contentsOf: fixture("half.fold")) let slant = SIMD3(-0.6, 0.8, 0) file.frames[1].verticesCoords[2] = file.frames[1].verticesCoords[1] + slant file.frames[1].verticesCoords[3] = file.frames[1].verticesCoords[4] + slant let defects = validate(file) #expect(defects.contains(.faceDistorted(frame: 1, face: 1))) #expect(!defects.contains { if case .nonIsometricTransition = $0 { return true }; return false }) } @Test func aReportedCycleListsEachFaceAboveTheNext() throws { // frame 2 puts f0 above f1, f1 above f2 and f2 above f0 let defects = validate(try FoldFile(contentsOf: fixture("cyclic-order.fold"))) let cycle = try #require(defects.lazy.compactMap { d -> [Int]? in if case .cyclicFaceOrder(frame: 2, let cycle) = d { return cycle }; return nil }.first) let rotations = [[0, 1, 2], [1, 2, 0], [2, 0, 1]] #expect(rotations.contains(cycle)) } @Test func normalsAtGivenPositionsAndTheFacesOfEachEdge() throws { let file = try FoldFile(contentsOf: fixture("half.fold")) #expect(faceNormals(positions: file.frames[1].verticesCoords, faces: file.facesVertices) == [SIMD3(0, 0, 1), SIMD3(0, 0, -1)]) #expect(edgesFaces(file) == [[0], [1], [1], [1], [0], [0], [0, 1]]) } @Test func rejectsAMalformedPop() throws { #expect(validate(try FoldFile(contentsOf: fixture("nonrigid-no-pop.fold"))).contains(.popMalformed(step: 1))) var file = try FoldFile(contentsOf: fixture("nonrigid-no-pop.fold")) let record = file.frames[1].step! func withPop(popped: Int, phases: [Double]) -> StepRecord { StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, nonRigid: true, pop: PopRecord(spine: [6], openAngle: 30, phases: phases, openAxes: [], popped: Array(file.frames[0].verticesCoords.prefix(popped)), closeAxes: []), viewpoint: record.viewpoint) } file.frames[1].step = withPop(popped: 5, phases: [1.0 / 3, 2.0 / 3]) #expect(validate(file).contains(.popMalformed(step: 1))) file.frames[1].step = withPop(popped: 6, phases: [2.0 / 3, 1.0 / 3]) #expect(validate(file).contains(.popMalformed(step: 1))) file.frames[1].step = withPop(popped: 6, phases: [1.0 / 3, 2.0 / 3]) #expect(!validate(file).contains(.popMalformed(step: 1))) } @Test func rejectsAViewpointWhoseUpIsAlongItsEye() throws { var file = try FoldFile(contentsOf: fixture("half.fold")) let record = file.frames[1].step! func viewing(eye: SIMD3, up: SIMD3) -> StepRecord { StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, viewpoint: Viewpoint(eye: eye, up: up)) } file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(0, 0, -2)) #expect(validate(file).contains(.viewpointDegenerate(step: 1))) file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(1e-6, 0, 1)) #expect(validate(file).contains(.viewpointDegenerate(step: 1))) file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: .zero) #expect(validate(file).contains(.viewpointDegenerate(step: 1))) file.frames[1].step = viewing(eye: .zero, up: SIMD3(0, 1, 0)) #expect(validate(file).contains(.viewpointDegenerate(step: 1))) file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 1)) #expect(!validate(file).contains(.viewpointDegenerate(step: 1))) }