Commit 8cbe0195a2

8cbe0195a2dcb701a558f1eae012af30a6292a30

parent: 10af936c92

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 07:07 UTC

Measure the fold path's end tolerance along the path

Ref #6

Layout: unified · split

Packages/FoldKit/Sources/Folding/FoldParameterMapping.swift +9 −6
@@ -28,7 +28,7 @@ public struct FoldParameterMapping: Sendable {
2828 private static let fallbackThrow = 160.0
2929 /// How far past the best point so far the search continues, in points.
3030 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.
3232 private static let endTolerance = 0.5
3333
3434 public init(file: FoldFile, step: FoldStep, camera: Camera, viewportSize: SIMD2<Double>) {
@@ -64,8 +64,9 @@ public struct FoldParameterMapping: Sendable {
6464 /// `grabbedAt` is the centroid's screen point when the drag began. The search walks the path
6565 /// outward from `current`, within the segment `current` is in: a fold-and-unfold's return half
6666 /// 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.
6970 ///
7071 /// When the path is degenerate the drag's downward component advances the fold over 160
7172 /// points from `current`, which is then the parameter when the drag began.
@@ -79,11 +80,11 @@ public struct FoldParameterMapping: Sendable {
7980 var start = segment.lowerBound
8081 while start + 1 < segment.upperBound && ts[start + 1] <= current { start += 1 }
8182
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) {
8384 let a = points[i], d = points[i + 1] - points[i]
8485 let lengthSquared = dot(d, d)
8586 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))
8788 }
8889 var best = nearest(onEdge: start)
8990 for direction in [1, -1] {
@@ -99,7 +100,9 @@ public struct FoldParameterMapping: Sendable {
99100 }
100101 }
101102 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]
103106 }
104107}
105108
Packages/FoldKit/Tests/FoldingTests/FoldParameterTests.swift +15
@@ -1,3 +1,4 @@
1import FoldFormat
12import FoldKitTestSupport
23import Folding
34import simd
@@ -59,3 +60,17 @@ import Testing
5960 let back = m.parameter(grabbedAt: m.path[0], translation: .zero, current: atPeak)
6061 #expect(back > 0.99)
6162}
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}