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 {
2020 advanceToFirstTurnOverStep(app)
2121 let before = app.staticTexts["step.index"].label
2222 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
2424 drag(paper, from: CGVector(dx: 0.5, dy: 0.05), to: CGVector(dx: 0.5, dy: 0.95))
2525 XCTAssertEqual(app.staticTexts["step.index"].label, before)
2626 XCTAssertEqual(paper.value as? String, "0%")
Packages/FoldKit/Sources/Folding/FoldParameterMapping.swift +10 −7
@@ -14,7 +14,8 @@ import simd
1414public struct FoldParameterMapping: Sendable {
1515 /// The drag's path at 129 evenly spaced values of t: the followed centroids, each segment's
1616 /// 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.
1819 public let path: [SIMD2<Double>]
1920 /// Whether every segment is a straight line down, so a drag cannot follow the fold anywhere.
2021 public let isDegenerate: Bool
@@ -36,8 +37,9 @@ public struct FoldParameterMapping: Sendable {
3637
3738 private static let samples = 129
3839 private static let minimumLength = 8.0
39 /// Points dragged down for the whole step where a segment's path is under `minimumLength`.
40 private static let fallbackThrow = 160.0
40 /// Points dragged down for the whole step where a segment's path is under `minimumLength`: 160,
41 /// or half the view's height when that is less, so a fallback stretch ends inside the view.
42 private let fallbackThrow: Double
4143 /// How much further, in points, another axis's centroid must travel to replace the previous
4244 /// segment's axis.
4345 private static let preference = 0.5
@@ -47,6 +49,7 @@ public struct FoldParameterMapping: Sendable {
4749 private static let endTolerance = 0.5
4850
4951 public init(file: FoldFile, step: FoldStep, camera: Camera, viewportSize: SIMD2<Double>) {
52 fallbackThrow = min(160, viewportSize.y / 2)
5053 var followed: [(faces: [Int], window: ClosedRange<Double>)]
5154 var boundaries: Set<Double> = [0, 1], peaks: Set<Double> = []
5255 if step.nonRigid, let pop = step.pop {
@@ -90,7 +93,7 @@ public struct FoldParameterMapping: Sendable {
9093 let start = segment.lowerBound == 0 ? screen[0][choice ?? 0] : points[segment.lowerBound]
9194 for i in segment {
9295 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)
9497 }
9598 if choice == nil { fallbacks += 1 }
9699 previous = choice ?? previous
@@ -132,10 +135,10 @@ public struct FoldParameterMapping: Sendable {
132135 /// both ways, except a fold-and-unfold's peak, which is crossed only forward. A point within
133136 /// half a point of the path's end, measured along the path, is the end.
134137 ///
135 /// When the path is degenerate the drag's downward component advances the fold over 160
136 /// points from `current`, which is then the position when the drag began.
138 /// When the path is degenerate the drag's downward component advances the fold over
139 /// `fallbackThrow` points from `current`, which is then the position when the drag began.
137140 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) }
139142
140143 let target = grabbedAt + translation
141144 let s = current.segment
Packages/FoldKit/Tests/FoldingTests/FoldParameterTests.swift +15 −2
@@ -132,11 +132,24 @@ private func reverseFixture(openAngle: Double) throws -> FoldFile {
132132
133133/// The mapping for the reverse fixture's non-rigid step, seen from above. Opened to 30° from flat
134134/// 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 {
136137 let file = try reverseFixture(openAngle: openAngle)
137138 return FoldParameterMapping(file: file, step: try readSteps(file)[1],
138139 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 }
140153}
141154
142155@Test func aNonRigidStepCanBeDraggedBackThroughItsPhases() throws {