import FoldFormat /// One crease of a step. `origin` and `direction` (a unit vector) are in the coordinates of the /// frame the step starts from, directed as the moving face adjacent to `edge` traverses it, so a /// positive rotation by the right-hand rule is a valley. `parent` is the index, in the step's /// `axes`, of the axis whose motion carries this one. `curve` is the fold angle at evenly spaced /// points of `window`; without it the angle is linear on the window. public struct FoldAxis: Equatable, Sendable { public let edge: Int public let origin: SIMD3 public let direction: SIMD3 public let movingFaces: Set public let startAngle: Double public let endAngle: Double public let peakAngle: Double? public let parent: Int? public let window: ClosedRange public let curve: [Double]? public init(edge: Int, origin: SIMD3, direction: SIMD3, movingFaces: Set, startAngle: Double, endAngle: Double, peakAngle: Double? = nil, parent: Int? = nil, window: ClosedRange = 0...1, curve: [Double]? = nil) { self.edge = edge self.origin = origin self.direction = direction self.movingFaces = movingFaces self.startAngle = startAngle self.endAngle = endAngle self.peakAngle = peakAngle self.parent = parent self.window = window self.curve = curve } /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the /// start and after it holds at the end. public func angle(at t: Double, kind: StepKind) -> Double { let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1) if let curve { return hermite(curve, at: u) } if kind == .foldAndUnfold { return startAngle + ((peakAngle ?? endAngle) - startAngle) * (1 - abs(2 * u - 1)) } return startAngle + u * (endAngle - startAngle) } } /// The open, pop and close of a non-rigid step. `openAxes` are in the coordinates of the frame /// the step starts from; `closeAxes` are in the coordinates of `popped`. public struct Pop: Equatable, Sendable { public let spine: [Int] public let openAngle: Double public let phases: ClosedRange public let openAxes: [FoldAxis] public let popped: [SIMD3] public let closeAxes: [FoldAxis] public init(spine: [Int], openAngle: Double, phases: ClosedRange, openAxes: [FoldAxis], popped: [SIMD3], closeAxes: [FoldAxis]) { self.spine = spine self.openAngle = openAngle self.phases = phases self.openAxes = openAxes self.popped = popped self.closeAxes = closeAxes } } /// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before /// its children. public struct FoldStep: Equatable, Sendable { public let index: Int public let kind: StepKind public let technique: StepTechnique public let instruction: String public let axes: [FoldAxis] public let rotateDegrees: Double? public let turnOverAxis: TurnOverAxis? public let repeatOf: Int? public let nonRigid: Bool public let pop: Pop? public let viewpoint: Viewpoint public init(index: Int, kind: StepKind, technique: StepTechnique = .plain, instruction: String, axes: [FoldAxis], rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil, repeatOf: Int? = nil, nonRigid: Bool = false, pop: Pop? = nil, viewpoint: Viewpoint) { self.index = index self.kind = kind self.technique = technique self.instruction = instruction self.axes = axes self.rotateDegrees = rotateDegrees self.turnOverAxis = turnOverAxis self.repeatOf = repeatOf self.nonRigid = nonRigid self.pop = pop self.viewpoint = viewpoint } /// The faces the step's axes move. A non-rigid step's pop holds the other faces still. public var movingFaceUnion: Set { axes.reduce(into: Set()) { $0.formUnion($1.movingFaces) } } /// Every face that moves during the step: `movingFaceUnion` and, in a non-rigid step, the faces /// its open and close axes move, which include the faces that open on the spine. public var allMovingFaces: Set { let pop = nonRigid ? pop : nil return (axes + (pop?.openAxes ?? []) + (pop?.closeAxes ?? [])) .reduce(into: Set()) { $0.formUnion($1.movingFaces) } } } public enum StepError: Error, Equatable, Sendable { case axisNotAnEdge(step: Int, edge: Int) case axisNotOnMovingSetBoundary(step: Int, edge: Int) case movingFacesOutOfRange(step: Int) case overlappingMovingSetsWithinAStep(step: Int) case rotateWithoutDegrees(step: Int) case turnOverWithoutAxis(step: Int) case foldAndUnfoldWithoutPeak(step: Int) case foldAndUnfoldDoesNotReturn(step: Int) case missingInstruction(step: Int) case axisParentCycle(step: Int) case axisWindowOutOfRange(step: Int) case curveTooShort(step: Int) case nonRigidWithoutPop(step: Int) case popPhasesOutOfOrder(step: Int) } /// Cubic Hermite interpolation of evenly spaced samples over 0...1, with central-difference /// tangents inside and second-order one-sided differences at the ends. private func hermite(_ samples: [Double], at u: Double) -> Double { let n = samples.count - 1 let s = u * Double(n) let i = min(Int(s), n - 1) let f = s - Double(i) func tangent(_ j: Int) -> Double { switch j { case 0: n < 2 ? samples[1] - samples[0] : (4 * samples[1] - 3 * samples[0] - samples[2]) / 2 case n: n < 2 ? samples[n] - samples[n - 1] : (3 * samples[n] - 4 * samples[n - 1] + samples[n - 2]) / 2 default: (samples[j + 1] - samples[j - 1]) / 2 } } let f2 = f * f, f3 = f2 * f return (2 * f3 - 3 * f2 + 1) * samples[i] + (f3 - 2 * f2 + f) * tangent(i) + (3 * f2 - 2 * f3) * samples[i + 1] + (f3 - f2) * tangent(i + 1) }