| @@ -14,7 +14,8 @@ import simd |
| 14 | public struct FoldParameterMapping: Sendable { |
14 | public 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 |