Commit aa644a399c
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/FoldKitTestSupport/Helpers.swift +6
| @@ -331,6 +331,12 @@ public func compiled(_ name: String) throws -> FoldFile { | ||
| 331 | 331 | pattern: FoldFile(contentsOf: fixture("\(name)-cp.fold"))) |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | /// `compiled(name)` with step `step`'s `openAngle` set to `degrees`. Steps count from 1. | |
| 335 | public func compiled(_ name: String, openAngle degrees: Double, step: Int) throws -> FoldFile { | |
| 336 | try compile(FoldScript(contentsOf: fixture("\(name).foldscript")).withOpenAngle(degrees, step: step), | |
| 337 | pattern: FoldFile(contentsOf: fixture("\(name)-cp.fold"))) | |
| 338 | } | |
| 339 | ||
| 334 | 340 | /// Whether two files have the same metadata, topology, assignments, layer orders and steps, with |
| 335 | 341 | /// coordinates, angles and pop phases within `tolerance`. Face orders compare as sets; |
| 336 | 342 | /// `file_creator` is ignored. |
Packages/FoldKit/Tests/FoldingTests/FoldParameterTests.swift +35 −9
| @@ -41,9 +41,7 @@ import Testing | ||
| 41 | 41 | |
| 42 | 42 | @Test func aNonRigidStepIsDraggedPhaseByPhase() throws { |
| 43 | 43 | // following the path's own samples in order reaches t = 1 without ever going back |
| 44 | let m = try FoldParameterMapping(file: compiled("reverse"), step: readSteps(compiled("reverse"))[1], | |
| 45 | camera: Camera(eye: SIMD3(2, 1, 8), target: SIMD3(2, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), | |
| 46 | viewportSize: SIMD2(800, 600)) | |
| 44 | let m = try reverseMapping(openAngle: 30) | |
| 47 | 45 | var t = 0.0 |
| 48 | 46 | for p in m.path.dropFirst() { |
| 49 | 47 | let next = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) |
| @@ -127,9 +125,15 @@ import Testing | ||
| 127 | 125 | #expect(abs(back - 0.6) < 0.01) |
| 128 | 126 | } |
| 129 | 127 | |
| 130 | /// The mapping for the reverse fixture's non-rigid step, seen from above. | |
| 131 | private func reverseMapping() throws -> FoldParameterMapping { | |
| 132 | let file = try compiled("reverse") | |
| 128 | /// The reverse fixture opened to `openAngle` from flat. | |
| 129 | private 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. | |
| 135 | private func reverseMapping(openAngle: Double) throws -> FoldParameterMapping { | |
| 136 | let file = try reverseFixture(openAngle: openAngle) | |
| 133 | 137 | return FoldParameterMapping(file: file, step: try readSteps(file)[1], |
| 134 | 138 | camera: Camera(eye: SIMD3(2, 1, 8), target: SIMD3(2, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), |
| 135 | 139 | viewportSize: SIMD2(800, 600)) |
| @@ -137,7 +141,7 @@ private func reverseMapping() throws -> FoldParameterMapping { | ||
| 137 | 141 | |
| 138 | 142 | @Test func aNonRigidStepCanBeDraggedBackThroughItsPhases() throws { |
| 139 | 143 | // the pop's path starts back along the open's, so dragging back passes that turnaround too |
| 140 | let m = try reverseMapping() | |
| 144 | let m = try reverseMapping(openAngle: 30) | |
| 141 | 145 | var t = 0.0 |
| 142 | 146 | for p in m.path.dropFirst() { t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) } |
| 143 | 147 | #expect(t == 1) |
| @@ -177,8 +181,8 @@ private func reverseMapping() throws -> FoldParameterMapping { | ||
| 177 | 181 | @Test func aDragOnARetracedStretchStaysInItsPhase() throws { |
| 178 | 182 | // the pop's samples 44...54 lie within half a point of the open's path; a finger there stays |
| 179 | 183 | // in the phase the drag is in until it reaches the turnaround |
| 180 | let m = try reverseMapping() | |
| 181 | let phases = try readSteps(compiled("reverse"))[1].pop!.phases | |
| 184 | let m = try reverseMapping(openAngle: 30) | |
| 185 | let phases = try readSteps(reverseFixture(openAngle: 30))[1].pop!.phases | |
| 182 | 186 | var opening = m.position(at: 0) |
| 183 | 187 | for k in 1...36 { opening = m.position(grabbedAt: m.path[0], translation: m.path[k] - m.path[0], current: opening) } |
| 184 | 188 | var popping = opening |
| @@ -190,3 +194,25 @@ private func reverseMapping() throws -> FoldParameterMapping { | ||
| 190 | 194 | #expect(m.position(grabbedAt: m.path[0], translation: p, current: popping).t > phases.lowerBound, "sample \(k)") |
| 191 | 195 | } |
| 192 | 196 | } |
| 197 | ||
| 198 | @Test func aPopTooSmallToFollowIsDraggedThroughItsTurnarounds() throws { | |
| 199 | // at the default 20° from flat the pop moves the tip too little to follow, so its path is the | |
| 200 | // straight line down, retracing the open's seen from above; a finger following the path | |
| 201 | // passes through each turnaround, where the drag changes phase, forward and back | |
| 202 | let m = try reverseMapping(openAngle: 20) | |
| 203 | let phases = try readSteps(reverseFixture(openAngle: 20))[1].pop!.phases | |
| 204 | let ts = ((1...128).map { Double($0) / 128 } + [phases.lowerBound, phases.upperBound]).sorted() | |
| 205 | var position = m.position(at: 0) | |
| 206 | for t in ts { | |
| 207 | let next = m.position(grabbedAt: m.path[0], translation: m.point(at: t) - m.path[0], current: position) | |
| 208 | #expect(next.t >= position.t - 1e-9) | |
| 209 | position = next | |
| 210 | } | |
| 211 | #expect(position.t == 1) | |
| 212 | for t in ts.reversed().dropFirst() + [0] { | |
| 213 | let next = m.position(grabbedAt: m.path[0], translation: m.point(at: t) - m.path[0], current: position) | |
| 214 | #expect(next.t <= position.t + 1e-9) | |
| 215 | position = next | |
| 216 | } | |
| 217 | #expect(position.t < 0.01) | |
| 218 | } | |