Commit 8c3426673d

8c3426673df704aa2980bab2a9df042648adc80f

parent: 9d5135d823

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 21:33 UTC

Rebuild solved curves to second order at their ends

The first and last intervals used one-sided first differences, which
opened a closure gap above 1e-6 at the start of the crane's neck close.

Ref #7

Layout: unified · split

Packages/FoldKit/Sources/Folding/FoldStep.swift +3 −3
@@ -128,7 +128,7 @@ public enum StepError: Error, Equatable, Sendable {
128} 128}
129 129
130/// Cubic Hermite interpolation of evenly spaced samples over 0...1, with central-difference 130/// Cubic Hermite interpolation of evenly spaced samples over 0...1, with central-difference
131/// tangents inside and one-sided differences at the ends. 131/// tangents inside and second-order one-sided differences at the ends.
132private func hermite(_ samples: [Double], at u: Double) -> Double { 132private func hermite(_ samples: [Double], at u: Double) -> Double {
133 let n = samples.count - 1 133 let n = samples.count - 1
134 let s = u * Double(n) 134 let s = u * Double(n)
@@ -136,8 +136,8 @@ private func hermite(_ samples: [Double], at u: Double) -> Double {
136 let f = s - Double(i) 136 let f = s - Double(i)
137 func tangent(_ j: Int) -> Double { 137 func tangent(_ j: Int) -> Double {
138 switch j { 138 switch j {
139 case 0: samples[1] - samples[0] 139 case 0: n < 2 ? samples[1] - samples[0] : (4 * samples[1] - 3 * samples[0] - samples[2]) / 2
140 case n: samples[n] - samples[n - 1] 140 case n: n < 2 ? samples[n] - samples[n - 1] : (3 * samples[n] - 4 * samples[n - 1] + samples[n - 2]) / 2
141 default: (samples[j + 1] - samples[j - 1]) / 2 141 default: (samples[j + 1] - samples[j - 1]) / 2
142 } 142 }
143 } 143 }
Packages/FoldKit/Tests/FoldingTests/InterpolationTests.swift +10
@@ -107,3 +107,13 @@ func everyEdgeLengthIsPreservedThroughoutEveryStep(_ name: String) throws {
107@Test func theGridSheetIsAValidFile() { 107@Test func theGridSheetIsAValidFile() {
108 #expect(validate(gridSheet(cells: 12)).isEmpty) 108 #expect(validate(gridSheet(cells: 12)).isEmpty)
109} 109}
110
111@Test func aSolvedCurveIsRebuiltToSecondOrderAtItsEnds() {
112 // 180 u² at 257 samples: the rebuilt curve is exact for a quadratic in its first and last intervals
113 let samples = (0...256).map { 180 * pow(Double($0) / 256, 2) }
114 let axis = FoldAxis(edge: 0, origin: .zero, direction: SIMD3(1, 0, 0), movingFaces: [], startAngle: 0,
115 endAngle: 180, curve: samples)
116 for u in [0.5 / 256, 1 - 0.5 / 256] {
117 #expect(abs(axis.angle(at: u, kind: .fold) - 180 * u * u) < 1e-9, "u \(u)")
118 }
119}