Packages/FoldKit/Tests/FoldingTests/LayerOrderTests.swift
212 lines · 10152 bytes
18 symbols in this file
twoStackedCoplanarFacesFormOneClusterOrderedFarthestFirstorbitingUnderneathReversesTheClusterOrderfacesSeparatedInSpaceFormSeparateClustersunorderedCoplanarFacesKeepIndexOrderanArrivingFlapJoinsTheStackItLandsOnaFlapAtThirtyDegreesIsItsOwnClusteraFoldAndUnfoldPeakIsOrderedByItsMidFoldOrderaPageTurningOverAStillLayerStaysInFrontOfItThroughoutTheFoldaFlapLeavingAStackIsOrderedByTheStartFrameaFaceOpeningOnTheSpineIsReadAgainstTheStillFacecyclicOrderThrowsaRawCycleThatNormalisesAwayIsAcceptedconsecutiveOrderingsAgreeAwayFromTheMovingFacesstackDepthCountsTheLayersOverOnePointstackDepthOfAFlatSheetIsOnepageTurnOverStillLayerhalfUnfoldedframe
1import Testing
2import simd
3import FoldFormat
4@testable import Folding
5import FoldKitTestSupport
6
7let up2 = [SIMD3<Double>(0, 0, 1), SIMD3(0, 0, 1)]
8let up3 = up2 + [SIMD3(0, 0, 1)]
9
10@Test func twoStackedCoplanarFacesFormOneClusterOrderedFarthestFirst() throws {
11 let order = try LayerOrder(faceOrders: [[0, 1, 1]], normals: up2) // 0 above 1
12 let c = order.clusters(positions: stackedSquarePositions(), faces: twoSquares(),
13 viewDirection: SIMD3(0, 0, -1))
14 #expect(c.count == 1)
15 #expect(c[0].faces == [1, 0])
16}
17
18@Test func orbitingUnderneathReversesTheClusterOrder() throws {
19 let order = try LayerOrder(faceOrders: [[0, 1, 1]], normals: up2)
20 let above = order.clusters(positions: stackedSquarePositions(), faces: twoSquares(),
21 viewDirection: SIMD3(0, 0, -1))
22 let below = order.clusters(positions: stackedSquarePositions(), faces: twoSquares(),
23 viewDirection: SIMD3(0, 0, 1))
24 #expect(above[0].faces == below[0].faces.reversed())
25}
26
27@Test func facesSeparatedInSpaceFormSeparateClusters() throws {
28 let order = try LayerOrder(faceOrders: [], normals: up2)
29 #expect(order.clusters(positions: separatedSquarePositions(), faces: twoSquares(),
30 viewDirection: SIMD3(0, 0, -1)).count == 2)
31}
32
33@Test func unorderedCoplanarFacesKeepIndexOrder() throws {
34 let order = try LayerOrder(faceOrders: [], normals: up3)
35 #expect(order.clusters(positions: threeStackedPositions(), faces: threeSquares(),
36 viewDirection: SIMD3(0, 0, -1))[0].faces == [0, 1, 2])
37}
38
39@Test func anArrivingFlapJoinsTheStackItLandsOn() throws {
40 // letter-sequential step 2 at 179.5°: f2 is half a degree above f0 and f1
41 let (file, step, i) = try interpolatorWithStep("letter-sequential.fold", step: 2)
42 let c = try LayerOrder.active(for: step, in: file)
43 .clusters(positions: i.positions(at: 179.5 / 180), faces: file.facesVertices,
44 viewDirection: SIMD3(0, 0, -1))
45 #expect(c.contains { Set($0.faces) == [0, 1, 2] && $0.faces.last == 2 })
46}
47
48@Test func aFlapAtThirtyDegreesIsItsOwnCluster() throws {
49 let (file, step, i) = try interpolatorWithStep("letter-sequential.fold", step: 2)
50 let c = try LayerOrder.active(for: step, in: file)
51 .clusters(positions: i.positions(at: 150.0 / 180), faces: file.facesVertices,
52 viewDirection: SIMD3(0, 0, -1))
53 #expect(c.contains { $0.faces == [2] })
54}
55
56@Test func aFoldAndUnfoldPeakIsOrderedByItsMidFoldOrder() throws {
57 // quarters step 1 at the peak: f2 lies on f3 and f1 on f0, the moving faces on top
58 let (file, step, i) = try interpolatorWithStep("quarters.fold", step: 1)
59 let c = try LayerOrder.active(for: step, in: file)
60 .clusters(positions: i.positions(at: 0.5), faces: file.facesVertices,
61 viewDirection: SIMD3(0, 0, -1))
62 let left = try #require(c.first { $0.faces.contains(3) })
63 #expect(left.faces.firstIndex(of: 2)! > left.faces.firstIndex(of: 3)!)
64}
65
66@Test func aPageTurningOverAStillLayerStaysInFrontOfItThroughoutTheFold() throws {
67 // S under the hinge, M turning over onto N; the end frame's [0, 2, 1] is written against M's
68 // normal at the end, opposite to its normal at the start
69 let (file, step) = pageTurnOverStillLayer()
70 let order = try LayerOrder.active(for: step, in: file)
71 let interpolator = FoldInterpolator(file: file, step: step)
72 for degrees in [0.5, 179.5] {
73 let c = order.clusters(positions: interpolator.positions(at: degrees / 180), faces: file.facesVertices,
74 viewDirection: SIMD3(0, 0, -1))
75 let stack = try #require(c.first { $0.faces.contains(0) })
76 #expect(stack.faces.contains(2) && stack.faces.firstIndex(of: 2)! > stack.faces.firstIndex(of: 0)!,
77 "\(degrees)°: \(c.map(\.faces))")
78 }
79}
80
81@Test func aFlapLeavingAStackIsOrderedByTheStartFrame() throws {
82 // frame 2 is flat and orders nothing, so frame 1's order must apply as f0 lifts off f1
83 let (file, step) = halfUnfolded()
84 let c = try LayerOrder.active(for: step, in: file)
85 .clusters(positions: FoldInterpolator(file: file, step: step).positions(at: 0.01),
86 faces: file.facesVertices, viewDirection: SIMD3(0, 0, -1))
87 #expect(c.map(\.faces) == [[1, 0]])
88}
89
90@Test func aFaceOpeningOnTheSpineIsReadAgainstTheStillFace() throws {
91 // reverse step 2: f3 opens on the spine and closes again while f0 holds; both frames give [0, 3, 1]
92 let file = try compiled("reverse")
93 let step = try readSteps(file)[1]
94 #expect(!step.movingFaceUnion.contains(3))
95 let pairs = try LayerOrder.active(for: step, in: file).triples.map { [$0.f, $0.g] }
96 #expect(pairs.contains([3, 0]) && !pairs.contains([0, 3]))
97}
98
99@Test func cyclicOrderThrows() {
100 #expect(throws: LayerError.cyclic(cycle: [0, 1, 2])) {
101 try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: up3)
102 }
103}
104
105@Test func aRawCycleThatNormalisesAwayIsAccepted() {
106 // f0 and f2 face down: these triples say f1 < f0, f1 < f2, f2 < f0
107 #expect(throws: Never.self) {
108 try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]],
109 normals: [SIMD3(0, 0, -1), SIMD3(0, 0, 1), SIMD3(0, 0, -1)])
110 }
111}
112
113@Test func consecutiveOrderingsAgreeAwayFromTheMovingFaces() throws {
114 let file = try FoldFile(contentsOf: fixture("letter-sequential.fold"))
115 for step in try readSteps(file) {
116 let start = try LayerOrder(faceOrders: file.frames[step.index - 1].faceOrders,
117 normals: faceNormals(file.frames[step.index - 1], file))
118 let end = try LayerOrder(faceOrders: file.frames[step.index].faceOrders,
119 normals: faceNormals(file.frames[step.index], file))
120 #expect(start.agreesWith(end, ignoring: step.movingFaceUnion), "step \(step.index)")
121 }
122}
123
124@Test func stackDepthCountsTheLayersOverOnePoint() throws {
125 let file = try FoldFile(contentsOf: fixture("letter-sequential.fold"))
126 #expect(stackDepth(positions: file.frames[2].verticesCoords, faces: file.facesVertices) == 3)
127}
128
129@Test func stackDepthOfAFlatSheetIsOne() throws {
130 // four faces meet at the centre vertex
131 let file = try FoldFile(contentsOf: fixture("quarters.fold"))
132 #expect(stackDepth(positions: file.frames[0].verticesCoords, faces: file.facesVertices) == 1)
133}
134
135/// 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
136/// x ∈ [1, 2], hinged at x = 1. Step 1 valley folds M over onto N.
137private func pageTurnOverStillLayer() -> (FoldFile, FoldStep) {
138 let start: [SIMD3<Double>] = [
139 SIMD3(0, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 1, 0), SIMD3(0, 1, 0),
140 SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 1, 0), SIMD3(1, 1, 0), SIMD3(0, 1, 0),
141 ]
142 var end = start
143 end[6] = SIMD3(0, 0, 0)
144 end[7] = SIMD3(0, 1, 0)
145 let boundary = Array(repeating: EdgeAssignment.boundary, count: 10)
146 let file = FoldFile(
147 fileSpec: 1.2,
148 fileAuthor: "Traditional",
149 edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 0],
150 [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 4], [5, 8]],
151 facesVertices: [[0, 1, 2, 3], [4, 5, 8, 9], [5, 6, 7, 8]],
152 edgesFirstCreasedAtStep: Array(repeating: nil, count: 10) + [1],
153 frames: [
154 FoldFrame(verticesCoords: start, edgesAssignment: boundary + [.flat],
155 edgesFoldAngle: Array(repeating: 0, count: 11),
156 faceOrders: [[0, 1, -1], [0, 2, -1]]),
157 // normals +z, +z, −z: S below N, S below M, N below M
158 FoldFrame(verticesCoords: end, edgesAssignment: boundary + [.valley],
159 edgesFoldAngle: Array(repeating: 0, count: 10) + [180],
160 faceOrders: [[0, 1, -1], [0, 2, 1], [1, 2, 1]]),
161 ]
162 )
163 let step = FoldStep(
164 index: 1,
165 kind: .fold,
166 instruction: "Turn the page over.",
167 // M traverses the hinge from v8 down to v5
168 axes: [FoldAxis(edge: 10, origin: SIMD3(1, 0, 0), direction: SIMD3(0, -1, 0), movingFaces: [2],
169 startAngle: 0, endAngle: 180)],
170 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))
171 )
172 return (file, step)
173}
174
175/// `half.fold`'s sheet with its faces listed right half first, so the flap is f0. Step 1 valley
176/// folds f0 over f1; step 2 unfolds it flat again.
177private func halfUnfolded() -> (FoldFile, FoldStep) {
178 let flat: [SIMD3<Double>] = [
179 SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(2, 0, 0), SIMD3(2, 2, 0), SIMD3(1, 2, 0), SIMD3(0, 2, 0),
180 ]
181 var folded = flat
182 folded[2] = SIMD3(0, 0, 0)
183 folded[3] = SIMD3(0, 2, 0)
184 let boundary = Array(repeating: EdgeAssignment.boundary, count: 6)
185 func frame(_ points: [SIMD3<Double>], angle: Double, faceOrders: [[Int]]) -> FoldFrame {
186 FoldFrame(verticesCoords: points, edgesAssignment: boundary + [angle == 0 ? .flat : .valley],
187 edgesFoldAngle: Array(repeating: 0, count: 6) + [angle], faceOrders: faceOrders)
188 }
189 let file = FoldFile(
190 fileSpec: 1.2,
191 fileAuthor: "Traditional",
192 edgesVertices: [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]],
193 facesVertices: [[1, 2, 3, 4], [0, 1, 4, 5]],
194 edgesFirstCreasedAtStep: Array(repeating: nil, count: 6) + [1],
195 frames: [
196 frame(flat, angle: 0, faceOrders: []),
197 // f0 faces −z, f1 faces +z: f0 on top
198 frame(folded, angle: 180, faceOrders: [[0, 1, 1]]),
199 frame(flat, angle: 0, faceOrders: []),
200 ]
201 )
202 let step = FoldStep(
203 index: 2,
204 kind: .fold,
205 instruction: "Unfold.",
206 // f0 traverses the crease from v4 down to v1
207 axes: [FoldAxis(edge: 6, origin: SIMD3(1, 0, 0), direction: SIMD3(0, -1, 0), movingFaces: [0],
208 startAngle: 180, endAngle: 0)],
209 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))
210 )
211 return (file, step)
212}