| @@ -17,13 +17,28 @@ public struct FoldAxis: Equatable, Sendable { |
| 17 | public let window: ClosedRange<Double> |
17 | public let window: ClosedRange<Double> |
| 18 | public let curve: [Double]? |
18 | public let curve: [Double]? |
| 19 | |
19 | |
| |
20 | public init(edge: Int, origin: SIMD3<Double>, direction: SIMD3<Double>, movingFaces: Set<Int>, |
| |
21 | startAngle: Double, endAngle: Double, peakAngle: Double? = nil, parent: Int? = nil, |
| |
22 | window: ClosedRange<Double> = 0...1, curve: [Double]? = nil) { |
| |
23 | self.edge = edge |
| |
24 | self.origin = origin |
| |
25 | self.direction = direction |
| |
26 | self.movingFaces = movingFaces |
| |
27 | self.startAngle = startAngle |
| |
28 | self.endAngle = endAngle |
| |
29 | self.peakAngle = peakAngle |
| |
30 | self.parent = parent |
| |
31 | self.window = window |
| |
32 | self.curve = curve |
| |
33 | } |
| |
34 | |
| 20 | /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the |
35 | /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the |
| 21 | /// start and after it holds at the end. |
36 | /// start and after it holds at the end. |
| 22 | public func angle(at t: Double, kind: StepKind) -> Double { |
37 | public func angle(at t: Double, kind: StepKind) -> Double { |
| 23 | let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1) |
38 | let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1) |
| 24 | if let curve { return hermite(curve, at: u) } |
39 | if let curve { return hermite(curve, at: u) } |
| 25 | if kind == .foldAndUnfold { |
40 | if kind == .foldAndUnfold { |
| 26 | return startAngle + (peakAngle! - startAngle) * (1 - abs(2 * u - 1)) |
41 | return startAngle + ((peakAngle ?? endAngle) - startAngle) * (1 - abs(2 * u - 1)) |
| 27 | } |
42 | } |
| 28 | return startAngle + u * (endAngle - startAngle) |
43 | return startAngle + u * (endAngle - startAngle) |
| 29 | } |
44 | } |
| @@ -38,6 +53,16 @@ public struct Pop: Equatable, Sendable { |
| 38 | public let openAxes: [FoldAxis] |
53 | public let openAxes: [FoldAxis] |
| 39 | public let popped: [SIMD3<Double>] |
54 | public let popped: [SIMD3<Double>] |
| 40 | public let closeAxes: [FoldAxis] |
55 | public let closeAxes: [FoldAxis] |
| |
56 | |
| |
57 | public init(spine: [Int], openAngle: Double, phases: ClosedRange<Double>, openAxes: [FoldAxis], |
| |
58 | popped: [SIMD3<Double>], closeAxes: [FoldAxis]) { |
| |
59 | self.spine = spine |
| |
60 | self.openAngle = openAngle |
| |
61 | self.phases = phases |
| |
62 | self.openAxes = openAxes |
| |
63 | self.popped = popped |
| |
64 | self.closeAxes = closeAxes |
| |
65 | } |
| 41 | } |
66 | } |
| 42 | |
67 | |
| 43 | /// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before |
68 | /// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before |
| @@ -55,6 +80,22 @@ public struct FoldStep: Equatable, Sendable { |
| 55 | public let pop: Pop? |
80 | public let pop: Pop? |
| 56 | public let viewpoint: Viewpoint |
81 | public let viewpoint: Viewpoint |
| 57 | |
82 | |
| |
83 | public init(index: Int, kind: StepKind, technique: StepTechnique = .plain, instruction: String, |
| |
84 | axes: [FoldAxis], rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil, |
| |
85 | repeatOf: Int? = nil, nonRigid: Bool = false, pop: Pop? = nil, viewpoint: Viewpoint) { |
| |
86 | self.index = index |
| |
87 | self.kind = kind |
| |
88 | self.technique = technique |
| |
89 | self.instruction = instruction |
| |
90 | self.axes = axes |
| |
91 | self.rotateDegrees = rotateDegrees |
| |
92 | self.turnOverAxis = turnOverAxis |
| |
93 | self.repeatOf = repeatOf |
| |
94 | self.nonRigid = nonRigid |
| |
95 | self.pop = pop |
| |
96 | self.viewpoint = viewpoint |
| |
97 | } |
| |
98 | |
| 58 | public var movingFaceUnion: Set<Int> { |
99 | public var movingFaceUnion: Set<Int> { |
| 59 | axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) } |
100 | axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) } |
| 60 | } |
101 | } |
| @@ -68,6 +109,7 @@ public enum StepError: Error, Equatable, Sendable { |
| 68 | case rotateWithoutDegrees(step: Int) |
109 | case rotateWithoutDegrees(step: Int) |
| 69 | case turnOverWithoutAxis(step: Int) |
110 | case turnOverWithoutAxis(step: Int) |
| 70 | case foldAndUnfoldWithoutPeak(step: Int) |
111 | case foldAndUnfoldWithoutPeak(step: Int) |
| |
112 | case foldAndUnfoldDoesNotReturn(step: Int) |
| 71 | case missingInstruction(step: Int) |
113 | case missingInstruction(step: Int) |
| 72 | case axisParentCycle(step: Int) |
114 | case axisParentCycle(step: Int) |
| 73 | case axisWindowOutOfRange(step: Int) |
115 | case axisWindowOutOfRange(step: Int) |