import FoldFormat import Foundation /// A fold script: a crease pattern and the steps that fold it. `creasePattern` is a path /// relative to the script. public struct FoldScript: Codable, Sendable { public let creasePattern: String public let title: String public let steps: [ScriptStep] public init(creasePattern: String, title: String, steps: [ScriptStep]) { self.creasePattern = creasePattern self.title = title self.steps = steps } public init(contentsOf url: URL) throws { self = try JSONDecoder().decode(FoldScript.self, from: Data(contentsOf: url)) } } /// One authored step. Points are in crease-pattern coordinates. /// /// - `creases`: the creases the step folds; each must separate a moving region from the rest. /// - `movingSide`: a point inside each region that moves; the region no point lies in is fixed. /// - `angles`, `peakAngles`, `windows`: one per crease. An angle defaults to ±180 by the crease /// pattern's assignment, a window to `[0, 1]`. /// - `viewpoint`: defaults to the previous step's, or eye `[0, 0, 1]`, up `[0, 1, 0]` for step 1. /// - `driver`: the crease held linear when the step's creases close a loop; defaults to the first. /// - `faceOrders`, `faceOrdersMidFold`: corrections to the guessed layer order. /// - `spine`, `openAngle`, `openOn`: a reverse fold or sink, compiled by the non-rigid path. /// `openAngle` defaults to 20. `openOn` names the crease whose opening opens the flap when that /// is not the spine: the spine of the earlier reverse fold whose flap this step's flap is in. public struct ScriptStep: Codable, Sendable { public let kind: StepKind public let technique: StepTechnique? public let instruction: String public let creases: [CreaseRef] public let movingSide: [SIMD2] public let angles: [Double]? public let peakAngles: [Double]? public let rotateDegrees: Double? public let turnOverAxis: TurnOverAxis? public let repeatOf: Int? public let viewpoint: Viewpoint? public let faceOrders: [OrderOverride]? public let faceOrdersMidFold: [OrderOverride]? public let driver: CreaseRef? public let windows: [[Double]]? public let spine: CreaseRef? public let openAngle: Double? public let openOn: CreaseRef? public init(kind: StepKind, technique: StepTechnique? = nil, instruction: String, creases: [CreaseRef] = [], movingSide: [SIMD2] = [], angles: [Double]? = nil, peakAngles: [Double]? = nil, rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil, repeatOf: Int? = nil, viewpoint: Viewpoint? = nil, faceOrders: [OrderOverride]? = nil, faceOrdersMidFold: [OrderOverride]? = nil, driver: CreaseRef? = nil, windows: [[Double]]? = nil, spine: CreaseRef? = nil, openAngle: Double? = nil, openOn: CreaseRef? = nil) { self.kind = kind self.technique = technique self.instruction = instruction self.creases = creases self.movingSide = movingSide self.angles = angles self.peakAngles = peakAngles self.rotateDegrees = rotateDegrees self.turnOverAxis = turnOverAxis self.repeatOf = repeatOf self.viewpoint = viewpoint self.faceOrders = faceOrders self.faceOrdersMidFold = faceOrdersMidFold self.driver = driver self.windows = windows self.spine = spine self.openAngle = openAngle self.openOn = openOn } public init(from decoder: any Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) kind = try c.decode(StepKind.self, forKey: .kind) technique = try c.decodeIfPresent(StepTechnique.self, forKey: .technique) instruction = try c.decode(String.self, forKey: .instruction) creases = try c.decodeIfPresent([CreaseRef].self, forKey: .creases) ?? [] movingSide = try c.decodeIfPresent([SIMD2].self, forKey: .movingSide) ?? [] angles = try c.decodeIfPresent([Double].self, forKey: .angles) peakAngles = try c.decodeIfPresent([Double].self, forKey: .peakAngles) rotateDegrees = try c.decodeIfPresent(Double.self, forKey: .rotateDegrees) turnOverAxis = try c.decodeIfPresent(TurnOverAxis.self, forKey: .turnOverAxis) repeatOf = try c.decodeIfPresent(Int.self, forKey: .repeatOf) viewpoint = try c.decodeIfPresent(Viewpoint.self, forKey: .viewpoint) faceOrders = try c.decodeIfPresent([OrderOverride].self, forKey: .faceOrders) faceOrdersMidFold = try c.decodeIfPresent([OrderOverride].self, forKey: .faceOrdersMidFold) driver = try c.decodeIfPresent(CreaseRef.self, forKey: .driver) windows = try c.decodeIfPresent([[Double]].self, forKey: .windows) spine = try c.decodeIfPresent(CreaseRef.self, forKey: .spine) openAngle = try c.decodeIfPresent(Double.self, forKey: .openAngle) openOn = try c.decodeIfPresent(CreaseRef.self, forKey: .openOn) } } /// A crease, as the two points it runs between or as an edge index. In a script, `[[x0, y0], /// [x1, y1]]` or an integer. public enum CreaseRef: Codable, Sendable, Equatable { case betweenVertices(SIMD2, SIMD2) case edgeIndex(Int) public init(from decoder: any Decoder) throws { let c = try decoder.singleValueContainer() if let edge = try? c.decode(Int.self) { self = .edgeIndex(edge) return } let ends = try c.decode([SIMD2].self) guard ends.count == 2 else { throw DecodingError.dataCorruptedError(in: c, debugDescription: "a crease runs between two points") } self = .betweenVertices(ends[0], ends[1]) } public func encode(to encoder: any Encoder) throws { var c = encoder.singleValueContainer() switch self { case .betweenVertices(let a, let b): try c.encode([a, b]) case .edgeIndex(let edge): try c.encode(edge) } } } /// Puts the face containing `above` over the face containing `below`, as seen from the step's /// viewpoint. Points are in crease-pattern coordinates. public struct OrderOverride: Codable, Sendable, Equatable { public let above: SIMD2 public let below: SIMD2 public init(above: SIMD2, below: SIMD2) { self.above = above self.below = below } }