Packages/FoldKit/Tests/FoldingTests/FoldParameterTests.swift
231 lines · 11537 bytes
20 symbols in this file
theFacetTracksTheFingerdragPerpendicularToThePathDoesNotAdvanceTheFoldparameterIsClampedToUnitRangeheadOnFoldIsReportedDegeneratedegenerateMappingFallsBackToVerticalDragtheMappingIsNeverInfinitelySensitiveaNonRigidStepIsDraggedPhaseByPhasefoldAndUnfoldGoesOutThenBackaPathThatRetracesItselfOnScreenIsFollowedWithoutJumpingToItsEndaStaggeredStepIsFollowedAcrossItsWindowsaLateWindowIsReachedByTheVerticalDragAndThenFollowedaFoldAndUnfoldPeaksAtTheMiddleOfItsWindowreverseFixturereverseMappingaFallbackStretchFitsInHalfTheViewaNonRigidStepCanBeDraggedBackThroughItsPhasesaFoldAndUnfoldStillUnfoldsAfterItsPeakaStaggeredStepCanBeDraggedBackPastItsWindowBoundaryaDragOnARetracedStretchStaysInItsPhaseaPopTooSmallToFollowIsDraggedThroughItsTurnarounds
1import FoldFormat
2import FoldKitTestSupport
3import Folding
4import simd
5import Testing
6
7@Test func theFacetTracksTheFinger() throws {
8 let m = try mapping("half.fold", step: 1) // viewed from above
9 let target = m.path[32] // t = 0.25
10 let t = m.parameter(grabbedAt: m.path[0], translation: target - m.path[0], current: 0)
11 #expect(abs(t - 0.25) < 0.01)
12}
13
14@Test func dragPerpendicularToThePathDoesNotAdvanceTheFold() throws {
15 let m = try mapping("half.fold", step: 1)
16 let along = simd_normalize(m.path[1] - m.path[0])
17 let perp = SIMD2(-along.y, along.x)
18 #expect(m.parameter(grabbedAt: m.path[0], translation: perp * 50, current: 0) < 0.01)
19}
20
21@Test func parameterIsClampedToUnitRange() throws {
22 let m = try mapping("half.fold", step: 1)
23 let along = simd_normalize(m.path[128] - m.path[0])
24 #expect(m.parameter(grabbedAt: m.path[0], translation: along * 100_000, current: 0) == 1)
25 #expect(m.parameter(grabbedAt: m.path[0], translation: along * -100_000, current: 0) == 0)
26}
27
28@Test func headOnFoldIsReportedDegenerate() throws {
29 #expect(try mappingForFoldTowardTheCamera().isDegenerate)
30}
31
32@Test func degenerateMappingFallsBackToVerticalDrag() throws {
33 let m = try mappingForFoldTowardTheCamera()
34 #expect(m.parameter(grabbedAt: .zero, translation: SIMD2(0, 50), current: 0) > 0.1)
35}
36
37@Test func theMappingIsNeverInfinitelySensitive() throws {
38 let m = try mappingForFoldTowardTheCamera()
39 #expect(m.parameter(grabbedAt: .zero, translation: SIMD2(0, 1), current: 0) < 0.2)
40}
41
42@Test func aNonRigidStepIsDraggedPhaseByPhase() throws {
43 // following the path's own samples in order reaches t = 1 without ever going back
44 let m = try reverseMapping(openAngle: 30)
45 var t = 0.0
46 for p in m.path.dropFirst() {
47 let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t)
48 #expect(next >= t - 1e-9)
49 t = next
50 }
51 #expect(t > 0.99)
52}
53
54@Test func foldAndUnfoldGoesOutThenBack() throws {
55 let m = try mapping("quarters.fold", step: 1)
56 let atPeak = m.parameter(grabbedAt: m.path[0], translation: m.path[64] - m.path[0], current: 0)
57 #expect(abs(atPeak - 0.5) < 0.01)
58 let back = m.parameter(grabbedAt: m.path[0], translation: .zero, current: atPeak)
59 #expect(back > 0.99)
60}
61
62@Test func aPathThatRetracesItselfOnScreenIsFollowedWithoutJumpingToItsEnd() throws {
63 // seen along (1, 0, 1), the flap's centroid passes the screen point of its end at 90°
64 let file = try FoldFile(contentsOf: fixture("half.fold"))
65 let m = FoldParameterMapping(file: file, step: try readStep(1, in: file),
66 camera: Camera(eye: SIMD3(1, 1, 0) + normalize(SIMD3(1, 0, 1)) * 4, target: SIMD3(1, 1, 0),
67 up: SIMD3(0, 1, 0), fieldOfView: .pi / 4),
68 viewportSize: SIMD2(800, 600))
69 var t = 0.0
70 for (k, p) in m.path.enumerated().dropFirst() {
71 t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t)
72 #expect(abs(t - Double(k) / 128) < 0.01, "sample \(k)")
73 }
74}
75
76@Test func aStaggeredStepIsFollowedAcrossItsWindows() throws {
77 // two axes on windows 0...0.5 and 0.5...1; following the path's samples reaches each in turn.
78 // 0.02 allows the last sample before a slow window end, within the end tolerance, to count as it
79 let m = try mapping("letter-staggered.fold", step: 1)
80 var t = 0.0
81 for (k, p) in m.path.enumerated().dropFirst() {
82 t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t)
83 #expect(abs(t - Double(k) / 128) < 0.02, "sample \(k)")
84 }
85}
86
87@Test func aLateWindowIsReachedByTheVerticalDragAndThenFollowed() throws {
88 // nothing moves before 0.5, so that half of the path is the vertical fallback
89 let file = try FoldFile(contentsOf: fixture("half.fold"))
90 let full = try readStep(1, in: file)
91 let a = full.axes[0]
92 let step = FoldStep(index: 1, kind: .fold, instruction: full.instruction,
93 axes: [FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction,
94 movingFaces: a.movingFaces, startAngle: 0, endAngle: 180, window: 0.5...1)],
95 viewpoint: full.viewpoint)
96 let m = FoldParameterMapping(file: file, step: step,
97 camera: Camera(eye: SIMD3(1, 1, 4), target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4),
98 viewportSize: SIMD2(800, 600))
99 #expect(!m.isDegenerate)
100 let early = m.parameter(grabbedAt: m.path[0], translation: SIMD2(0, 40), current: 0)
101 #expect(abs(early - 0.25) < 0.01)
102 var t = 0.0
103 for (k, p) in m.path.enumerated().dropFirst() {
104 t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t)
105 #expect(abs(t - Double(k) / 128) < 0.02, "sample \(k)")
106 }
107}
108
109@Test func aFoldAndUnfoldPeaksAtTheMiddleOfItsWindow() throws {
110 let file = try FoldFile(contentsOf: fixture("quarters.fold"))
111 let full = try readStep(1, in: file)
112 let a = full.axes[0]
113 let step = FoldStep(index: 1, kind: .foldAndUnfold, instruction: full.instruction,
114 axes: [FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces,
115 startAngle: a.startAngle, endAngle: a.endAngle, peakAngle: a.peakAngle,
116 window: 0...0.6)],
117 viewpoint: full.viewpoint)
118 let m = FoldParameterMapping(file: file, step: step,
119 camera: Camera(eye: SIMD3(1, 1, 4), target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4),
120 viewportSize: SIMD2(800, 600))
121 let peak = m.point(at: 0.3)
122 let atPeak = m.parameter(grabbedAt: m.path[0], translation: peak - m.path[0], current: 0)
123 #expect(abs(atPeak - 0.3) < 0.01)
124 let back = m.parameter(grabbedAt: m.path[0], translation: .zero, current: atPeak)
125 #expect(abs(back - 0.6) < 0.01)
126}
127
128/// The reverse fixture opened to `openAngle` from flat.
129private func reverseFixture(openAngle: Double) throws -> FoldFile {
130 try compiled("reverse", openAngle: openAngle, step: 2)
131}
132
133/// The mapping for the reverse fixture's non-rigid step, seen from above. Opened to 30° from flat
134/// the pop moves the tip far enough to be followed, and its path starts back along the open's.
135private func reverseMapping(openAngle: Double, viewportSize: SIMD2<Double> = SIMD2(800, 600)) throws
136 -> FoldParameterMapping {
137 let file = try reverseFixture(openAngle: openAngle)
138 return FoldParameterMapping(file: file, step: try readSteps(file)[1],
139 camera: Camera(eye: SIMD3(2, 1, 8), target: SIMD3(2, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4),
140 viewportSize: viewportSize)
141}
142
143@Test func aFallbackStretchFitsInHalfTheView() throws {
144 // R81: at 20° from flat the pop is too small to follow; dragged straight down it spans at most
145 // half the view's height, so it ends inside a short view
146 let phases = try readSteps(reverseFixture(openAngle: 20))[1].pop!.phases
147 for height in [600.0, 200.0] {
148 let m = try reverseMapping(openAngle: 20, viewportSize: SIMD2(800, height))
149 let drop = m.point(at: phases.upperBound) - m.point(at: phases.lowerBound)
150 #expect(abs(drop.x) < 1e-9)
151 #expect(abs(drop.y - min(160, height / 2) * (phases.upperBound - phases.lowerBound)) < 1e-9, "height \(height)")
152 }
153}
154
155@Test func aNonRigidStepCanBeDraggedBackThroughItsPhases() throws {
156 // the pop's path starts back along the open's, so dragging back passes that turnaround too
157 let m = try reverseMapping(openAngle: 30)
158 var t = 0.0
159 for p in m.path.dropFirst() { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) }
160 #expect(t == 1)
161 for p in m.path.reversed().dropFirst() {
162 let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t)
163 #expect(next <= t + 1e-9)
164 t = next
165 }
166 #expect(t < 0.01)
167}
168
169@Test func aFoldAndUnfoldStillUnfoldsAfterItsPeak() throws {
170 // past the peak the return half retraces the outbound half, so moving toward the start unfolds
171 let m = try mapping("quarters.fold", step: 1)
172 var t = 0.0
173 for p in m.path[1...80] { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) }
174 #expect(abs(t - 0.625) < 0.01)
175 for p in m.path[0..<48].reversed() {
176 let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t)
177 #expect(next >= t - 1e-9)
178 t = next
179 }
180 #expect(t == 1)
181}
182
183@Test func aStaggeredStepCanBeDraggedBackPastItsWindowBoundary() throws {
184 // seen from above, the second window's path retraces the first's, so samples 63 and 65 are one
185 // screen point and only the drag's position says which window it is in
186 let m = try mapping("letter-staggered.fold", step: 1)
187 var position = m.position(at: 0)
188 for k in Array(1...96) + Array((0..<96).reversed()) {
189 position = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: position)
190 #expect(abs(position.t - Double(k) / 128) < 0.01, "sample \(k)")
191 }
192}
193
194@Test func aDragOnARetracedStretchStaysInItsPhase() throws {
195 // the pop's samples 44...54 lie within half a point of the open's path; a finger there stays
196 // in the phase the drag is in until it reaches the turnaround
197 let m = try reverseMapping(openAngle: 30)
198 let phases = try readSteps(reverseFixture(openAngle: 30))[1].pop!.phases
199 var opening = m.position(at: 0)
200 for k in 1...36 { opening = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: opening) }
201 var popping = opening
202 for k in 37...54 { popping = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: popping) }
203 #expect(popping.t > phases.lowerBound)
204 for k in 44...54 {
205 let p = m.path[k] - m.path[0]
206 #expect(m.position(grabbedAt: m.path[0], translation: p, current: opening).t < phases.lowerBound, "sample \(k)")
207 #expect(m.position(grabbedAt: m.path[0], translation: p, current: popping).t > phases.lowerBound, "sample \(k)")
208 }
209}
210
211@Test func aPopTooSmallToFollowIsDraggedThroughItsTurnarounds() throws {
212 // at the default 20° from flat the pop moves the tip too little to follow, so its path is the
213 // straight line down, retracing the open's seen from above; a finger following the path
214 // passes through each turnaround, where the drag changes phase, forward and back
215 let m = try reverseMapping(openAngle: 20)
216 let phases = try readSteps(reverseFixture(openAngle: 20))[1].pop!.phases
217 let ts = ((1...128).map { Double($0) / 128 } + [phases.lowerBound, phases.upperBound]).sorted()
218 var position = m.position(at: 0)
219 for t in ts {
220 let next = m.position(grabbedAt: m.path[0], translation: m.point(at: t) - m.path[0], current: position)
221 #expect(next.t >= position.t - 1e-9)
222 position = next
223 }
224 #expect(position.t == 1)
225 for t in ts.reversed().dropFirst() + [0] {
226 let next = m.position(grabbedAt: m.path[0], translation: m.point(at: t) - m.path[0], current: position)
227 #expect(next.t <= position.t + 1e-9)
228 position = next
229 }
230 #expect(position.t < 0.01)
231}