Commit 8cbe0195a2
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/Folding/FoldParameterMapping.swift +9 −6
| @@ -28,7 +28,7 @@ public struct FoldParameterMapping: Sendable { | ||
| 28 | 28 | private static let fallbackThrow = 160.0 |
| 29 | 29 | /// How far past the best point so far the search continues, in points. |
| 30 | 30 | private static let slack = 8.0 |
| 31 | /// How near its segment's end a point counts as the end, in points. | |
| 31 | /// How near its segment's end, along the path, a point counts as the end, in points. | |
| 32 | 32 | private static let endTolerance = 0.5 |
| 33 | 33 | |
| 34 | 34 | public init(file: FoldFile, step: FoldStep, camera: Camera, viewportSize: SIMD2<Double>) { |
| @@ -64,8 +64,9 @@ public struct FoldParameterMapping: Sendable { | ||
| 64 | 64 | /// `grabbedAt` is the centroid's screen point when the drag began. The search walks the path |
| 65 | 65 | /// outward from `current`, within the segment `current` is in: a fold-and-unfold's return half |
| 66 | 66 | /// once `current` reaches 0.5, a non-rigid step's next phase once `current` reaches the end of |
| 67 | /// the one before. A point within half a point of its segment's end on screen is the end, so a | |
| 68 | /// segment whose path turns back at its end can be finished. | |
| 67 | /// the one before. Segments are one way: once `current` reaches a segment's end, the segments | |
| 68 | /// before it are not searched again. A point within half a point of its segment's end, measured | |
| 69 | /// along the path, is the end, so a segment whose path turns back at its end can be finished. | |
| 69 | 70 | /// |
| 70 | 71 | /// When the path is degenerate the drag's downward component advances the fold over 160 |
| 71 | 72 | /// points from `current`, which is then the parameter when the drag began. |
| @@ -79,11 +80,11 @@ public struct FoldParameterMapping: Sendable { | ||
| 79 | 80 | var start = segment.lowerBound |
| 80 | 81 | while start + 1 < segment.upperBound && ts[start + 1] <= current { start += 1 } |
| 81 | 82 | |
| 82 | func nearest(onEdge i: Int) -> (t: Double, point: SIMD2<Double>, distance: Double) { | |
| 83 | func nearest(onEdge i: Int) -> (edge: Int, u: Double, distance: Double) { | |
| 83 | 84 | let a = points[i], d = points[i + 1] - points[i] |
| 84 | 85 | let lengthSquared = dot(d, d) |
| 85 | 86 | let u = lengthSquared > 0 ? min(max(dot(target - a, d) / lengthSquared, 0), 1) : 0 |
| 86 | return ((1 - u) * ts[i] + u * ts[i + 1], a + u * d, distance(a + u * d, target)) | |
| 87 | return (i, u, distance(a + u * d, target)) | |
| 87 | 88 | } |
| 88 | 89 | var best = nearest(onEdge: start) |
| 89 | 90 | for direction in [1, -1] { |
| @@ -99,7 +100,9 @@ public struct FoldParameterMapping: Sendable { | ||
| 99 | 100 | } |
| 100 | 101 | } |
| 101 | 102 | let end = segment.upperBound |
| 102 | return distance(best.point, points[end]) <= Self.endTolerance ? ts[end] : best.t | |
| 103 | var toEnd = (1 - best.u) * distance(points[best.edge], points[best.edge + 1]) | |
| 104 | for i in best.edge + 1 ..< end { toEnd += distance(points[i], points[i + 1]) } | |
| 105 | return toEnd <= Self.endTolerance ? ts[end] : (1 - best.u) * ts[best.edge] + best.u * ts[best.edge + 1] | |
| 103 | 106 | } |
| 104 | 107 | } |
| 105 | 108 | |
Packages/FoldKit/Tests/FoldingTests/FoldParameterTests.swift +15
| @@ -1,3 +1,4 @@ | ||
| 1 | import FoldFormat | |
| 1 | 2 | import FoldKitTestSupport |
| 2 | 3 | import Folding |
| 3 | 4 | import simd |
| @@ -59,3 +60,17 @@ import Testing | ||
| 59 | 60 | let back = m.parameter(grabbedAt: m.path[0], translation: .zero, current: atPeak) |
| 60 | 61 | #expect(back > 0.99) |
| 61 | 62 | } |
| 63 | ||
| 64 | @Test func aPathThatRetracesItselfOnScreenIsFollowedWithoutJumpingToItsEnd() throws { | |
| 65 | // seen along (1, 0, 1), the flap's centroid passes the screen point of its end at 90° | |
| 66 | let file = try FoldFile(contentsOf: fixture("half.fold")) | |
| 67 | let m = FoldParameterMapping(file: file, step: try readStep(1, in: file), | |
| 68 | camera: Camera(eye: SIMD3(1, 1, 0) + normalize(SIMD3(1, 0, 1)) * 4, target: SIMD3(1, 1, 0), | |
| 69 | up: SIMD3(0, 1, 0), fieldOfView: .pi / 4), | |
| 70 | viewportSize: SIMD2(800, 600)) | |
| 71 | var t = 0.0 | |
| 72 | for (k, p) in m.path.enumerated().dropFirst() { | |
| 73 | t = m.parameter(grabbedAt: m.path[0], translation: p - m.path[0], current: t) | |
| 74 | #expect(abs(t - Double(k) / 128) < 0.01, "sample \(k)") | |
| 75 | } | |
| 76 | } | |