Commit b4264f8b8c

b4264f8b8c0050a1aa473fcee9ebc09960c761e8

parent: dcab232542

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-23 01:25 UTC

Fit a fold drag's fallback stretch in half the view

The straight-down fallback moves 160 points per unit of t, or half the
view's height when that is less, so the crane's head pop ends inside the
paper view on iPhone (R81).

Ref #7

Layout: unified · split

App/UITests/ReorientGestureTests.swift +1 −1
@@ -20,7 +20,7 @@ final class ReorientGestureTests: XCTestCase {
20 advanceToFirstTurnOverStep(app) 20 advanceToFirstTurnOverStep(app)
21 let before = app.staticTexts["step.index"].label 21 let before = app.staticTexts["step.index"].label
22 let paper = app.otherElements["fold.paper"] 22 let paper = app.otherElements["fold.paper"]
23 // a downward drag longer than the fold drag's 160-point fallback throw 23 // a downward drag longer than the fold drag's fallback throw, at most 160 points
24 drag(paper, from: CGVector(dx: 0.5, dy: 0.05), to: CGVector(dx: 0.5, dy: 0.95)) 24 drag(paper, from: CGVector(dx: 0.5, dy: 0.05), to: CGVector(dx: 0.5, dy: 0.95))
25 XCTAssertEqual(app.staticTexts["step.index"].label, before) 25 XCTAssertEqual(app.staticTexts["step.index"].label, before)
26 XCTAssertEqual(paper.value as? String, "0%") 26 XCTAssertEqual(paper.value as? String, "0%")
Packages/FoldKit/Sources/Folding/FoldParameterMapping.swift +10 −7
@@ -14,7 +14,8 @@ import simd
14public struct FoldParameterMapping: Sendable { 14public struct FoldParameterMapping: Sendable {
15 /// The drag's path at 129 evenly spaced values of t: the followed centroids, each segment's 15 /// The drag's path at 129 evenly spaced values of t: the followed centroids, each segment's
16 /// translated to begin where the segment before it ended. A segment along which no covering 16 /// translated to begin where the segment before it ended. A segment along which no covering
17 /// axis's centroid travels 8 points is instead a straight line down, 160 points per unit of t. 17 /// axis's centroid travels 8 points is instead a straight line down, `fallbackThrow` points per
18 /// unit of t.
18 public let path: [SIMD2<Double>] 19 public let path: [SIMD2<Double>]
19 /// Whether every segment is a straight line down, so a drag cannot follow the fold anywhere. 20 /// Whether every segment is a straight line down, so a drag cannot follow the fold anywhere.
20 public let isDegenerate: Bool 21 public let isDegenerate: Bool
@@ -36,8 +37,9 @@ public struct FoldParameterMapping: Sendable {
36 37
37 private static let samples = 129 38 private static let samples = 129
38 private static let minimumLength = 8.0 39 private static let minimumLength = 8.0
39 /// Points dragged down for the whole step where a segment's path is under `minimumLength`. 40 /// Points dragged down for the whole step where a segment's path is under `minimumLength`: 160,
40 private static let fallbackThrow = 160.0 41 /// or half the view's height when that is less, so a fallback stretch ends inside the view.
42 private let fallbackThrow: Double
41 /// How much further, in points, another axis's centroid must travel to replace the previous 43 /// How much further, in points, another axis's centroid must travel to replace the previous
42 /// segment's axis. 44 /// segment's axis.
43 private static let preference = 0.5 45 private static let preference = 0.5
@@ -47,6 +49,7 @@ public struct FoldParameterMapping: Sendable {
47 private static let endTolerance = 0.5 49 private static let endTolerance = 0.5
48 50
49 public init(file: FoldFile, step: FoldStep, camera: Camera, viewportSize: SIMD2<Double>) { 51 public init(file: FoldFile, step: FoldStep, camera: Camera, viewportSize: SIMD2<Double>) {
52 fallbackThrow = min(160, viewportSize.y / 2)
50 var followed: [(faces: [Int], window: ClosedRange<Double>)] 53 var followed: [(faces: [Int], window: ClosedRange<Double>)]
51 var boundaries: Set<Double> = [0, 1], peaks: Set<Double> = [] 54 var boundaries: Set<Double> = [0, 1], peaks: Set<Double> = []
52 if step.nonRigid, let pop = step.pop { 55 if step.nonRigid, let pop = step.pop {
@@ -90,7 +93,7 @@ public struct FoldParameterMapping: Sendable {
90 let start = segment.lowerBound == 0 ? screen[0][choice ?? 0] : points[segment.lowerBound] 93 let start = segment.lowerBound == 0 ? screen[0][choice ?? 0] : points[segment.lowerBound]
91 for i in segment { 94 for i in segment {
92 points[i] = choice.map { start + screen[i][$0] - screen[segment.lowerBound][$0] } 95 points[i] = choice.map { start + screen[i][$0] - screen[segment.lowerBound][$0] }
93 ?? start + SIMD2(0, (ts[i] - a) * Self.fallbackThrow) 96 ?? start + SIMD2(0, (ts[i] - a) * fallbackThrow)
94 } 97 }
95 if choice == nil { fallbacks += 1 } 98 if choice == nil { fallbacks += 1 }
96 previous = choice ?? previous 99 previous = choice ?? previous
@@ -132,10 +135,10 @@ public struct FoldParameterMapping: Sendable {
132 /// both ways, except a fold-and-unfold's peak, which is crossed only forward. A point within 135 /// both ways, except a fold-and-unfold's peak, which is crossed only forward. A point within
133 /// half a point of the path's end, measured along the path, is the end. 136 /// half a point of the path's end, measured along the path, is the end.
134 /// 137 ///
135 /// When the path is degenerate the drag's downward component advances the fold over 160 138 /// When the path is degenerate the drag's downward component advances the fold over
136 /// points from `current`, which is then the position when the drag began. 139 /// `fallbackThrow` points from `current`, which is then the position when the drag began.
137 public func position(grabbedAt: SIMD2<Double>, translation: SIMD2<Double>, current: Position) -> Position { 140 public func position(grabbedAt: SIMD2<Double>, translation: SIMD2<Double>, current: Position) -> Position {
138 if isDegenerate { return position(at: current.t + translation.y / Self.fallbackThrow) } 141 if isDegenerate { return position(at: current.t + translation.y / fallbackThrow) }
139 142
140 let target = grabbedAt + translation 143 let target = grabbedAt + translation
141 let s = current.segment 144 let s = current.segment
Packages/FoldKit/Tests/FoldingTests/FoldParameterTests.swift +15 −2
@@ -132,11 +132,24 @@ private func reverseFixture(openAngle: Double) throws -> FoldFile {
132 132
133/// The mapping for the reverse fixture's non-rigid step, seen from above. Opened to 30° from flat 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. 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) throws -> FoldParameterMapping { 135private func reverseMapping(openAngle: Double, viewportSize: SIMD2<Double> = SIMD2(800, 600)) throws
136 -> FoldParameterMapping {
136 let file = try reverseFixture(openAngle: openAngle) 137 let file = try reverseFixture(openAngle: openAngle)
137 return FoldParameterMapping(file: file, step: try readSteps(file)[1], 138 return FoldParameterMapping(file: file, step: try readSteps(file)[1],
138 camera: Camera(eye: SIMD3(2, 1, 8), target: SIMD3(2, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), 139 camera: Camera(eye: SIMD3(2, 1, 8), target: SIMD3(2, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 4),
139 viewportSize: SIMD2(800, 600)) 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 }
140} 153}
141 154
142@Test func aNonRigidStepCanBeDraggedBackThroughItsPhases() throws { 155@Test func aNonRigidStepCanBeDraggedBackThroughItsPhases() throws {