Packages/FoldKit/Sources/Folding/FoldStep.swift

00ea029d0652da12d0f4237769e7020b72cfa7f3
fold/Packages/FoldKit/Sources/Folding/FoldStep.swift history · blame · raw

147 lines · 6177 bytes

7 symbols in this file
  1import FoldFormat
  2
  3/// One crease of a step. `origin` and `direction` (a unit vector) are in the coordinates of the
  4/// frame the step starts from, directed as the moving face adjacent to `edge` traverses it, so a
  5/// positive rotation by the right-hand rule is a valley. `parent` is the index, in the step's
  6/// `axes`, of the axis whose motion carries this one. `curve` is the fold angle at evenly spaced
  7/// points of `window`; without it the angle is linear on the window.
  8public struct FoldAxis: Equatable, Sendable {
  9    public let edge: Int
 10    public let origin: SIMD3<Double>
 11    public let direction: SIMD3<Double>
 12    public let movingFaces: Set<Int>
 13    public let startAngle: Double
 14    public let endAngle: Double
 15    public let peakAngle: Double?
 16    public let parent: Int?
 17    public let window: ClosedRange<Double>
 18    public let curve: [Double]?
 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
 35    /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the
 36    /// start and after it holds at the end.
 37    public func angle(at t: Double, kind: StepKind) -> Double {
 38        let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1)
 39        if let curve { return hermite(curve, at: u) }
 40        if kind == .foldAndUnfold {
 41            return startAngle + ((peakAngle ?? endAngle) - startAngle) * (1 - abs(2 * u - 1))
 42        }
 43        return startAngle + u * (endAngle - startAngle)
 44    }
 45}
 46
 47/// The open, pop and close of a non-rigid step. `openAxes` are in the coordinates of the frame
 48/// the step starts from; `closeAxes` are in the coordinates of `popped`.
 49public struct Pop: Equatable, Sendable {
 50    public let spine: [Int]
 51    public let openAngle: Double
 52    public let phases: ClosedRange<Double>
 53    public let openAxes: [FoldAxis]
 54    public let popped: [SIMD3<Double>]
 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    }
 66}
 67
 68/// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before
 69/// its children.
 70public struct FoldStep: Equatable, Sendable {
 71    public let index: Int
 72    public let kind: StepKind
 73    public let technique: StepTechnique
 74    public let instruction: String
 75    public let axes: [FoldAxis]
 76    public let rotateDegrees: Double?
 77    public let turnOverAxis: TurnOverAxis?
 78    public let repeatOf: Int?
 79    public let nonRigid: Bool
 80    public let pop: Pop?
 81    public let viewpoint: Viewpoint
 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
 99    /// The faces the step's axes move. A non-rigid step's pop holds the other faces still.
100    public var movingFaceUnion: Set<Int> {
101        axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
102    }
103
104    /// Every face that moves during the step: `movingFaceUnion` and, in a non-rigid step, the faces
105    /// its open and close axes move, which include the faces that open on the spine.
106    public var allMovingFaces: Set<Int> {
107        let pop = nonRigid ? pop : nil
108        return (axes + (pop?.openAxes ?? []) + (pop?.closeAxes ?? []))
109            .reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
110    }
111}
112
113public enum StepError: Error, Equatable, Sendable {
114    case axisNotAnEdge(step: Int, edge: Int)
115    case axisNotOnMovingSetBoundary(step: Int, edge: Int)
116    case movingFacesOutOfRange(step: Int)
117    case overlappingMovingSetsWithinAStep(step: Int)
118    case rotateWithoutDegrees(step: Int)
119    case turnOverWithoutAxis(step: Int)
120    case foldAndUnfoldWithoutPeak(step: Int)
121    case foldAndUnfoldDoesNotReturn(step: Int)
122    case missingInstruction(step: Int)
123    case axisParentCycle(step: Int)
124    case axisWindowOutOfRange(step: Int)
125    case curveTooShort(step: Int)
126    case nonRigidWithoutPop(step: Int)
127    case popPhasesOutOfOrder(step: Int)
128}
129
130/// Cubic Hermite interpolation of evenly spaced samples over 0...1, with central-difference
131/// tangents inside and second-order one-sided differences at the ends.
132private func hermite(_ samples: [Double], at u: Double) -> Double {
133    let n = samples.count - 1
134    let s = u * Double(n)
135    let i = min(Int(s), n - 1)
136    let f = s - Double(i)
137    func tangent(_ j: Int) -> Double {
138        switch j {
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        default: (samples[j + 1] - samples[j - 1]) / 2
142        }
143    }
144    let f2 = f * f, f3 = f2 * f
145    return (2 * f3 - 3 * f2 + 1) * samples[i] + (f3 - 2 * f2 + f) * tangent(i)
146        + (3 * f2 - 2 * f3) * samples[i + 1] + (f3 - f2) * tangent(i + 1)
147}