Commit 6124a8919a
Verified · cmc
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 | |||
| 158 | if file.edgesFirstCreasedAtStep[e] == nil { file.edgesFirstCreasedAtStep[e] = n } | 158 | if file.edgesFirstCreasedAtStep[e] == nil { file.edgesFirstCreasedAtStep[e] = n } |
| 159 | } | 159 | } |
| 160 | } | 160 | } |
| 161 | try checkTravel(n, step) | ||
| 162 | let guess = guessFaceOrders(previous: previous, next: frame, step: record, file: file) | 161 | let guess = guessFaceOrders(previous: previous, next: frame, step: record, file: file) |
| 163 | frame.faceOrders = try naming(n) { | 162 | frame.faceOrders = try naming(n) { |
| 164 | try applyOrderOverrides(script.faceOrders ?? [], to: guess, frame: frame, viewpoint: viewpoint, file: file) | 163 | 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 | |||
| 185 | 184 | ||
| 186 | let defects = validate(file) | 185 | let defects = validate(file) |
| 187 | guard defects.isEmpty else { throw CompileError.stepFailed(step: n, defects: defects) } | 186 | guard defects.isEmpty else { throw CompileError.stepFailed(step: n, defects: defects) } |
| 187 | try checkTravel(n, step) | ||
| 188 | let motion = layerMotionDefects(step: n, in: file) | 188 | let motion = layerMotionDefects(step: n, in: file) |
| 189 | guard motion.isEmpty else { throw CompileError.stepFailed(step: n, defects: motion) } | 189 | guard motion.isEmpty else { throw CompileError.stepFailed(step: n, defects: motion) } |
| 190 | let collisions = step.nonRigid | 190 | let collisions = step.nonRigid |
| @@ -501,15 +501,18 @@ func axesOnCycles(_ step: FoldStep, loops: [Int], _ file: FoldFile) -> Set<Int> | |||
| 501 | } | 501 | } |
| 502 | } | 502 | } |
| 503 | 503 | ||
| 504 | /// Checks every axis's angle stays between its start angle and its end angle, or its peak angle | 504 | /// Checks every axis's angle stays within ±180°, and a linear axis's between its start angle and |
| 505 | /// for a fold-and-unfold, to within 1e-6°, at four times the curve sample density over its | 505 | /// its end angle, or its peak angle for a fold-and-unfold, to within 1e-6°, at four times the |
| 506 | /// window. A non-rigid step is checked on its pop's open and close axes, which are what move it. | 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. | ||
| 507 | func checkTravel(_ n: Int, _ step: FoldStep) throws { | 508 | func checkTravel(_ n: Int, _ step: FoldStep) throws { |
| 508 | let moving = step.pop.map { ($0.openAxes + $0.closeAxes, StepKind.fold) } ?? (step.axes, step.kind) | 509 | let moving = step.pop.map { ($0.openAxes + $0.closeAxes, StepKind.fold) } ?? (step.axes, step.kind) |
| 509 | let intervals = 4 * (curveSamples - 1) | 510 | let intervals = 4 * (curveSamples - 1) |
| 510 | for axis in moving.0 { | 511 | for axis in moving.0 { |
| 511 | let far = moving.1 == .foldAndUnfold ? axis.peakAngle ?? axis.endAngle : axis.endAngle | 512 | 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 | ||
| 513 | let w = axis.window | 516 | let w = axis.window |
| 514 | for k in 0...intervals { | 517 | for k in 0...intervals { |
| 515 | let angle = axis.angle(at: w.lowerBound + Double(k) / Double(intervals) * (w.upperBound - w.lowerBound), | 518 | 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 | |||
| 2 | @testable import FoldCompiler | 2 | @testable import FoldCompiler |
| 3 | import FoldFormat | 3 | import FoldFormat |
| 4 | import Folding | 4 | import Folding |
| 5 | import Foundation | ||
| 5 | 6 | ||
| 6 | @Test func aSecondFoldAndUnfoldAxisWithCollinearEdgesTurnsBackAtItsPeak() throws { | 7 | @Test func aSecondFoldAndUnfoldAxisWithCollinearEdgesTurnsBackAtItsPeak() throws { |
| 7 | let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern()) | 8 | let file = try compile(strips(.foldAndUnfold), pattern: stripsPattern()) |
| @@ -34,6 +35,33 @@ import Folding | |||
| 34 | #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: a.edge)) { try checkTravel(1, broken) } | 35 | #expect(throws: CompileError.angleOutsideTravel(step: 1, edge: a.edge)) { try checkTravel(1, broken) } |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 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 | |||
| 37 | /// A 4 × 2 sheet with creases x = 1 and x = 3, each split by y = 1 into two collinear edges. | 65 | /// A 4 × 2 sheet with creases x = 1 and x = 3, each split by y = 1 into two collinear edges. |
| 38 | private func stripsPattern() -> FoldFile { | 66 | private func stripsPattern() -> FoldFile { |
| 39 | let points: [SIMD3<Double>] = [[0, 0, 0], [1, 0, 0], [3, 0, 0], [4, 0, 0], [4, 1, 0], [4, 2, 0], | 67 | let points: [SIMD3<Double>] = [[0, 0, 0], [1, 0, 0], [3, 0, 0], [4, 0, 0], [4, 1, 0], [4, 2, 0], |