Packages/FoldKit/Tests/ModelsTests/FourModelTests.swift
125 lines · 5884 bytes
11 symbols in this file
theCatalogueContainsFourModelstheCraneContainsAMultiAxisSteptheCraneStacksAtLeastEightLayersSomewhereeveryModelWithOverlappingMovingSetsCarriesAMidFoldOrderonlyTheCranesReverseFoldsAreFlaggedtheCranesReverseFoldsOpenAndCloseRigidlytheCranesPopsStayWithinTheBoundAndPassThroughNothingtheCranesTipsLandBetweenTheLayerstheCranesHeadReversesOnlyTheEdgesJoiningThePapersHalveshalftheCranesPopsAreDraggedAsRecorded
1import Foundation
2import Testing
3import simd
4import FoldFormat
5import Folding
6import FoldKitTestSupport
7import Models
8
9@Test func theCatalogueContainsFourModels() throws {
10 #expect(try Set(ModelLibrary.names()) == ["cup", "crane", "sampan", "box"])
11}
12
13@Test func theCraneContainsAMultiAxisStep() throws {
14 #expect(try readSteps(ModelLibrary.model(named: "crane")).contains { $0.axes.count > 1 })
15}
16
17@Test func theCraneStacksAtLeastEightLayersSomewhere() throws {
18 let crane = try ModelLibrary.model(named: "crane")
19 let deepest = (0...crane.stepCount).map {
20 stackDepth(positions: crane.frames[$0].verticesCoords, faces: crane.facesVertices)
21 }.max() ?? 0
22 #expect(deepest >= 8)
23}
24
25@Test func everyModelWithOverlappingMovingSetsCarriesAMidFoldOrder() throws {
26 for file in try ModelLibrary.all() {
27 for n in 1...file.stepCount where movingSetsOverlapInProjection(step: n, file) {
28 #expect(file.frames[n].faceOrdersMidFold != nil, "\(file.fileTitle ?? "?") step \(n)")
29 }
30 }
31}
32
33@Test func onlyTheCranesReverseFoldsAreFlagged() throws {
34 // decision 0002: the neck, tail and head reverse folds; nothing else
35 let crane = try readSteps(ModelLibrary.model(named: "crane"))
36 #expect(crane.filter(\.nonRigid).count == 3)
37 #expect(crane.filter(\.nonRigid).allSatisfy { $0.technique == .insideReverse })
38 for name in ["cup", "box", "sampan"] {
39 #expect(try readSteps(ModelLibrary.model(named: name)).allSatisfy { !$0.nonRigid }, "\(name)")
40 }
41}
42
43/// The crane's reverse folds: the step, a sheet point of its tip, and sheet points of the faces
44/// the tip lands between, the upper one in the half of the square nearer the eye.
45private let craneReverseFolds: [(step: Int, tip: SIMD2<Double>, lower: SIMD2<Double>, upper: SIMD2<Double>)] = [
46 (13, SIMD2(0.01295, 0.64699), SIMD2(0.42511, 0.6469), SIMD2(0.3531, 0.57489)),
47 (14, SIMD2(0.64666, 0.02868), SIMD2(0.6469, 0.42511), SIMD2(0.57489, 0.3531)),
48 (15, SIMD2(0.00626, 0.88539), SIMD2(0.25767, 0.86361), SIMD2(0.13639, 0.74233)),
49]
50
51@Test func theCranesReverseFoldsOpenAndCloseRigidly() throws {
52 // decision 0003: only the pop bends
53 let crane = try ModelLibrary.model(named: "crane")
54 for fold in craneReverseFolds {
55 let step = try readStep(fold.step, in: crane)
56 let pop = try #require(step.pop)
57 let i = FoldInterpolator(file: crane, step: step)
58 for k in 0...256 {
59 let t = Double(k) / 256
60 guard t <= pop.phases.lowerBound || t >= pop.phases.upperBound else { continue }
61 #expect(maxEdgeLengthDrift(i.positions(at: t), crane) < 1e-6 * sheetSize(crane), "step \(fold.step) t \(t)")
62 }
63 }
64}
65
66@Test func theCranesPopsStayWithinTheBoundAndPassThroughNothing() throws {
67 // decision 0003 and R78: under 10% edge strain; no mesh interpenetration in any phase
68 let crane = try ModelLibrary.model(named: "crane")
69 for fold in craneReverseFolds {
70 let step = try readStep(fold.step, in: crane)
71 let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound)
72 let i = FoldInterpolator(file: crane, step: step)
73 let worst = (0...64).map { strain(i.meshPositions(at: lo + Double($0) / 64 * (hi - lo)), mesh: i.mesh) }.max()!
74 #expect(worst <= 0.10, "step \(fold.step)")
75 #expect(meshInterpenetrations(i, samples: 64).isEmpty, "step \(fold.step)")
76 }
77}
78
79@Test func theCranesTipsLandBetweenTheLayers() throws {
80 let crane = try ModelLibrary.model(named: "crane")
81 for fold in craneReverseFolds {
82 let step = try readStep(fold.step, in: crane)
83 let hi = step.pop!.phases.upperBound
84 let positions = FoldInterpolator(file: crane, step: step).positions(at: hi + 0.97 * (1 - hi))
85 #expect(pointIsBetweenLayers(positions, crane, point: fold.tip, lower: fold.lower, upper: fold.upper),
86 "step \(fold.step)")
87 }
88}
89
90@Test func theCranesHeadReversesOnlyTheEdgesJoiningThePapersHalves() throws {
91 // R77: the legs' former edge folds lie on the neck's spine line but join layers of one half
92 let crane = try ModelLibrary.model(named: "crane")
93 let step = try readStep(15, in: crane)
94 let pop = try #require(step.pop)
95 let flat = crane.frames[0].verticesCoords
96 let edgeFaces = edgesFaces(crane)
97 // the halves of the square either side of its diagonal x + y = 1
98 func half(_ f: Int) -> Bool {
99 let sum = crane.facesVertices[f].reduce(SIMD3<Double>.zero) { $0 + flat[$1] }
100 return sum.x + sum.y < Double(crane.facesVertices[f].count)
101 }
102 let tipSpine = pop.spine.filter { edgeFaces[$0].allSatisfy(step.movingFaceUnion.contains) }
103 #expect(!tipSpine.isEmpty)
104 #expect(tipSpine.allSatisfy { half(edgeFaces[$0][0]) != half(edgeFaces[$0][1]) })
105}
106
107@Test func theCranesPopsAreDraggedAsRecorded() throws {
108 // R75: whether, at each reverse fold's authored viewpoint, the drag follows the pop or falls
109 // back to a straight line down (docs/authoring/crane.md)
110 let crane = try ModelLibrary.model(named: "crane")
111 var fallbacks: [Int: Bool] = [:]
112 for fold in craneReverseFolds {
113 let step = try readStep(fold.step, in: crane)
114 let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound)
115 let m = FoldParameterMapping(file: crane, step: step, camera: sessionCamera(crane, step, aspect: 400.0 / 600),
116 viewportSize: SIMD2(400, 600))
117 let start = m.point(at: lo)
118 fallbacks[fold.step] = (1...16).allSatisfy { k in
119 let t = lo + Double(k) / 16 * (hi - lo)
120 return simd_distance(m.point(at: t) - start, SIMD2(0, (t - lo) * 160)) < 1e-6
121 }
122 print("crane step \(fold.step): pop \(fallbacks[fold.step]! ? "falls back" : "followed")")
123 }
124 #expect(fallbacks == [13: false, 14: false, 15: true])
125}