Packages/FoldKit/Sources/FoldCompiler/FoldScript.swift

141 lines · 6505 bytes

5 symbols in this file
  1import FoldFormat
  2import Foundation
  3
  4/// A fold script: a crease pattern and the steps that fold it. `creasePattern` is a path
  5/// relative to the script.
  6public struct FoldScript: Codable, Sendable {
  7    public let creasePattern: String
  8    public let title: String
  9    public let steps: [ScriptStep]
 10
 11    public init(creasePattern: String, title: String, steps: [ScriptStep]) {
 12        self.creasePattern = creasePattern
 13        self.title = title
 14        self.steps = steps
 15    }
 16
 17    public init(contentsOf url: URL) throws {
 18        self = try JSONDecoder().decode(FoldScript.self, from: Data(contentsOf: url))
 19    }
 20}
 21
 22/// One authored step. Points are in crease-pattern coordinates.
 23///
 24/// - `creases`: the creases the step folds; each must separate a moving region from the rest.
 25/// - `movingSide`: a point inside each region that moves; the region no point lies in is fixed.
 26/// - `angles`, `peakAngles`, `windows`: one per crease. An angle defaults to ±180 by the crease
 27///   pattern's assignment, a window to `[0, 1]`.
 28/// - `viewpoint`: defaults to the previous step's, or eye `[0, 0, 1]`, up `[0, 1, 0]` for step 1.
 29/// - `driver`: the crease held linear when the step's creases close a loop; defaults to the first.
 30/// - `faceOrders`, `faceOrdersMidFold`: corrections to the guessed layer order.
 31/// - `spine`, `openAngle`, `openOn`: a reverse fold or sink, compiled by the non-rigid path.
 32///   `openAngle` defaults to 20. `openOn` names the crease whose opening opens the flap when that
 33///   is not the spine: the spine of the earlier reverse fold whose flap this step's flap is in.
 34public struct ScriptStep: Codable, Sendable {
 35    public let kind: StepKind
 36    public let technique: StepTechnique?
 37    public let instruction: String
 38    public let creases: [CreaseRef]
 39    public let movingSide: [SIMD2<Double>]
 40    public let angles: [Double]?
 41    public let peakAngles: [Double]?
 42    public let rotateDegrees: Double?
 43    public let turnOverAxis: TurnOverAxis?
 44    public let repeatOf: Int?
 45    public let viewpoint: Viewpoint?
 46    public let faceOrders: [OrderOverride]?
 47    public let faceOrdersMidFold: [OrderOverride]?
 48    public let driver: CreaseRef?
 49    public let windows: [[Double]]?
 50    public let spine: CreaseRef?
 51    public let openAngle: Double?
 52    public let openOn: CreaseRef?
 53
 54    public init(kind: StepKind, technique: StepTechnique? = nil, instruction: String, creases: [CreaseRef] = [],
 55                movingSide: [SIMD2<Double>] = [], angles: [Double]? = nil, peakAngles: [Double]? = nil,
 56                rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil, repeatOf: Int? = nil,
 57                viewpoint: Viewpoint? = nil, faceOrders: [OrderOverride]? = nil,
 58                faceOrdersMidFold: [OrderOverride]? = nil, driver: CreaseRef? = nil, windows: [[Double]]? = nil,
 59                spine: CreaseRef? = nil, openAngle: Double? = nil, openOn: CreaseRef? = nil) {
 60        self.kind = kind
 61        self.technique = technique
 62        self.instruction = instruction
 63        self.creases = creases
 64        self.movingSide = movingSide
 65        self.angles = angles
 66        self.peakAngles = peakAngles
 67        self.rotateDegrees = rotateDegrees
 68        self.turnOverAxis = turnOverAxis
 69        self.repeatOf = repeatOf
 70        self.viewpoint = viewpoint
 71        self.faceOrders = faceOrders
 72        self.faceOrdersMidFold = faceOrdersMidFold
 73        self.driver = driver
 74        self.windows = windows
 75        self.spine = spine
 76        self.openAngle = openAngle
 77        self.openOn = openOn
 78    }
 79
 80    public init(from decoder: any Decoder) throws {
 81        let c = try decoder.container(keyedBy: CodingKeys.self)
 82        kind = try c.decode(StepKind.self, forKey: .kind)
 83        technique = try c.decodeIfPresent(StepTechnique.self, forKey: .technique)
 84        instruction = try c.decode(String.self, forKey: .instruction)
 85        creases = try c.decodeIfPresent([CreaseRef].self, forKey: .creases) ?? []
 86        movingSide = try c.decodeIfPresent([SIMD2<Double>].self, forKey: .movingSide) ?? []
 87        angles = try c.decodeIfPresent([Double].self, forKey: .angles)
 88        peakAngles = try c.decodeIfPresent([Double].self, forKey: .peakAngles)
 89        rotateDegrees = try c.decodeIfPresent(Double.self, forKey: .rotateDegrees)
 90        turnOverAxis = try c.decodeIfPresent(TurnOverAxis.self, forKey: .turnOverAxis)
 91        repeatOf = try c.decodeIfPresent(Int.self, forKey: .repeatOf)
 92        viewpoint = try c.decodeIfPresent(Viewpoint.self, forKey: .viewpoint)
 93        faceOrders = try c.decodeIfPresent([OrderOverride].self, forKey: .faceOrders)
 94        faceOrdersMidFold = try c.decodeIfPresent([OrderOverride].self, forKey: .faceOrdersMidFold)
 95        driver = try c.decodeIfPresent(CreaseRef.self, forKey: .driver)
 96        windows = try c.decodeIfPresent([[Double]].self, forKey: .windows)
 97        spine = try c.decodeIfPresent(CreaseRef.self, forKey: .spine)
 98        openAngle = try c.decodeIfPresent(Double.self, forKey: .openAngle)
 99        openOn = try c.decodeIfPresent(CreaseRef.self, forKey: .openOn)
100    }
101}
102
103/// A crease, as the two points it runs between or as an edge index. In a script, `[[x0, y0],
104/// [x1, y1]]` or an integer.
105public enum CreaseRef: Codable, Sendable, Equatable {
106    case betweenVertices(SIMD2<Double>, SIMD2<Double>)
107    case edgeIndex(Int)
108
109    public init(from decoder: any Decoder) throws {
110        let c = try decoder.singleValueContainer()
111        if let edge = try? c.decode(Int.self) {
112            self = .edgeIndex(edge)
113            return
114        }
115        let ends = try c.decode([SIMD2<Double>].self)
116        guard ends.count == 2 else {
117            throw DecodingError.dataCorruptedError(in: c, debugDescription: "a crease runs between two points")
118        }
119        self = .betweenVertices(ends[0], ends[1])
120    }
121
122    public func encode(to encoder: any Encoder) throws {
123        var c = encoder.singleValueContainer()
124        switch self {
125        case .betweenVertices(let a, let b): try c.encode([a, b])
126        case .edgeIndex(let edge): try c.encode(edge)
127        }
128    }
129}
130
131/// Puts the face containing `above` over the face containing `below`, as seen from the step's
132/// viewpoint. Points are in crease-pattern coordinates.
133public struct OrderOverride: Codable, Sendable, Equatable {
134    public let above: SIMD2<Double>
135    public let below: SIMD2<Double>
136
137    public init(above: SIMD2<Double>, below: SIMD2<Double>) {
138        self.above = above
139        self.below = below
140    }
141}