Commit 6124a8919a

6124a8919aac9dd8c192c6322af2a7cbd4205281

parent: b3177eb0fc

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 16:56 UTC

Let solved curves turn back within a half turn

Ref #7

Layout: unified · split

Packages/FoldKit/Sources/FoldCompiler/FrameCompiler.swift +8 −5
@@ -158,7 +158,6 @@ private func appendStep(_ n: Int, _ script: ScriptStep, viewpoint: Viewpoint, to
158158 if file.edgesFirstCreasedAtStep[e] == nil { file.edgesFirstCreasedAtStep[e] = n }
159159 }
160160 }
161 try checkTravel(n, step)
162161 let guess = guessFaceOrders(previous: previous, next: frame, step: record, file: file)
163162 frame.faceOrders = try naming(n) {
164163 try applyOrderOverrides(script.faceOrders ?? [], to: guess, frame: frame, viewpoint: viewpoint, file: file)
@@ -185,6 +184,7 @@ private func appendStep(_ n: Int, _ script: ScriptStep, viewpoint: Viewpoint, to
185184
186185 let defects = validate(file)
187186 guard defects.isEmpty else { throw CompileError.stepFailed(step: n, defects: defects) }
187 try checkTravel(n, step)
188188 let motion = layerMotionDefects(step: n, in: file)
189189 guard motion.isEmpty else { throw CompileError.stepFailed(step: n, defects: motion) }
190190 let collisions = step.nonRigid
@@ -501,15 +501,18 @@ func axesOnCycles(_ step: FoldStep, loops: [Int], _ file: FoldFile) -> Set<Int>
501501 }
502502}
503503
504/// Checks every axis's angle stays between its start angle and its end angle, or its peak angle
505/// for a fold-and-unfold, to within 1e-6°, at four times the curve sample density over its
506/// window. A non-rigid step is checked on its pop's open and close axes, which are what move it.
504/// Checks every axis's angle stays within ±180°, and a linear axis's between its start angle and
505/// its end angle, or its peak angle for a fold-and-unfold, to within 1e-6°, at four times the
506/// curve sample density over its window. A solved curve may turn back within ±180°. A non-rigid
507/// step is checked on its pop's open and close axes, which are what move it.
507508func checkTravel(_ n: Int, _ step: FoldStep) throws {
508509 let moving = step.pop.map { ($0.openAxes + $0.closeAxes, StepKind.fold) } ?? (step.axes, step.kind)
509510 let intervals = 4 * (curveSamples - 1)
510511 for axis in moving.0 {
511512 let far = moving.1 == .foldAndUnfold ? axis.peakAngle ?? axis.endAngle : axis.endAngle
512 let (low, high) = (min(axis.startAngle, far) - angleSnapTolerance, max(axis.startAngle, far) + angleSnapTolerance)
513 let linear = axis.curve == nil
514 let low = (linear ? max(min(axis.startAngle, far), -180) : -180) - angleSnapTolerance
515 let high = (linear ? min(max(axis.startAngle, far), 180) : 180) + angleSnapTolerance
513516 let w = axis.window
514517 for k in 0...intervals {
515518 let angle = axis.angle(at: w.lowerBound + Double(k) / Double(intervals) * (w.upperBound - w.lowerBound),
Packages/FoldKit/Tests/FoldCompilerTests/CollinearAxisTests.swift +28
@@ -2,6 +2,7 @@ import Testing
22@testable import FoldCompiler
33import FoldFormat
44import Folding
5import Foundation
56
67@Test func aSecondFoldAndUnfoldAxisWithCollinearEdgesTurnsBackAtItsPeak() throws {
78 let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern())
@@ -34,6 +35,33 @@ import Folding
3435 #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: a.edge)) { try checkTravel(1, broken) }
3536}
3637
38@Test func aSolvedCurveMayBendPastItsEndAnglesWithinAHalfTurn() throws {
39 // a coupled crease flat at both ends that bends mid-step, and a hold-then-rise curve whose
40 // interpolation undershoots its start
41 let file = try compile(strips(.fold, windows: nil), pattern: stripsPattern())
42 let a = try readStep(1, in: file).axes[1]
43 let bend = (0...256).map { 120 * sin(.pi * Double($0) / 256) }
44 let rise = (0...256).map { $0 < 200 ? 0 : 90 * Double($0 - 199) / 57 }
45 for (end, curve) in [(0.0, bend), (90.0, rise)] {
46 let axis = FoldAxis(edge: a.edge, origin: a.origin, direction: a.direction, movingFaces: a.movingFaces,
47 startAngle: 0, endAngle: end, window: a.window, curve: curve)
48 #expect(axis.angle(at: 0.775, kind: .fold) < 0 || end == 0)
49 try checkTravel(1, FoldStep(index: 1, kind: .fold, instruction: "", axes: [axis],
50 viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))))
51 }
52}
53
54@Test func compilingRejectsAnAngleBeyondAHalfTurn() throws {
55 let script = FoldScript(creasePattern: "", title: "Strips", steps: [
56 ScriptStep(kind: .foldAndUnfold, instruction: "Fold both ends in and unfold.",
57 creases: [.edgeIndex(10), .edgeIndex(12)], movingSide: [SIMD2(0.5, 0.5), SIMD2(3.5, 0.5)],
58 peakAngles: [180, 200]),
59 ])
60 #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: 12)) {
61 try compile(script, pattern: stripsPattern())
62 }
63}
64
3765/// A 4 × 2 sheet with creases x = 1 and x = 3, each split by y = 1 into two collinear edges.
3866private func stripsPattern() -> FoldFile {
3967 let points: [SIMD3<Double>] = [[0, 0, 0], [1, 0, 0], [3, 0, 0], [4, 0, 0], [4, 1, 0], [4, 2, 0],