Commit 8c3426673d
Verified · cmc
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 | 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. | |
| 132 | 132 | private func hermite(_ samples: [Double], at u: Double) -> Double { |
| 133 | 133 | let n = samples.count - 1 |
| 134 | 134 | let s = u * Double(n) |
| @@ -136,8 +136,8 @@ private func hermite(_ samples: [Double], at u: Double) -> Double { | ||
| 136 | 136 | let f = s - Double(i) |
| 137 | 137 | func tangent(_ j: Int) -> Double { |
| 138 | 138 | switch j { |
| 139 | case 0: samples[1] - samples[0] | |
| 140 | case n: samples[n] - samples[n - 1] | |
| 139 | case 0: n < 2 ? samples[1] - samples[0] : (4 * samples[1] - 3 * samples[0] - samples[2]) / 2 | |
| 140 | case n: n < 2 ? samples[n] - samples[n - 1] : (3 * samples[n] - 4 * samples[n - 1] + samples[n - 2]) / 2 | |
| 141 | 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 | 107 | @Test func theGridSheetIsAValidFile() { |
| 108 | 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 | } | |