Packages/FoldKit/Sources/FoldFormat/StepRecord.swift
119 lines · 4958 bytes
7 symbols in this file
1public enum StepKind: String, Codable, Sendable {
2 case fold, foldAndUnfold, turnOver, rotate
3}
4
5public enum StepTechnique: String, Codable, Sendable {
6 case plain, insideReverse, outsideReverse, squash, petal, sink
7}
8
9public enum TurnOverAxis: String, Codable, Sendable {
10 case vertical, horizontal
11}
12
13/// One crease of a step. `parent` is the index, in the same step's `axes`, of the axis whose
14/// motion carries this one; `window` is `[t0, t1]`, `[0, 1]` when absent; `angles` is a solved
15/// curve, the fold angle at 257 evenly spaced points of the window, absent for a linear axis.
16public struct AxisRecord: Codable, Equatable, Sendable {
17 public let edge: Int
18 public let movingFaces: [Int]
19 public let startAngle: Double
20 public let endAngle: Double
21 public let peakAngle: Double?
22 public let parent: Int?
23 public let window: [Double]?
24 public let angles: [Double]?
25
26 public init(edge: Int, movingFaces: [Int], startAngle: Double, endAngle: Double,
27 peakAngle: Double? = nil, parent: Int? = nil, window: [Double]? = nil,
28 angles: [Double]? = nil) {
29 self.edge = edge
30 self.movingFaces = movingFaces
31 self.startAngle = startAngle
32 self.endAngle = endAngle
33 self.peakAngle = peakAngle
34 self.parent = parent
35 self.window = window
36 self.angles = angles
37 }
38}
39
40/// `eye` is the unit direction from the model toward the viewer; `up` is screen-up.
41public struct Viewpoint: Codable, Equatable, Sendable {
42 public let eye: SIMD3<Double>
43 public let up: SIMD3<Double>
44
45 public init(eye: SIMD3<Double>, up: SIMD3<Double>) {
46 self.eye = eye
47 self.up = up
48 }
49}
50
51/// The open, pop and close of a non-rigid step: `openAxes` open the flap on `[0, phases[0]]`, the
52/// first turning the opening crease to `openAngle` (the `spine`, or for a nested opening an
53/// earlier fold's spine), every vertex reaches `popped` by `phases[1]`, and `closeAxes`, relative
54/// to `popped`, close the flap on `[phases[1], 1]`. `spine` lists the kept edges of the spine's
55/// line and, for a nested opening, of the opening crease's line: the edges the open and close turn
56/// and return, which the pop does not crease.
57public struct PopRecord: Codable, Equatable, Sendable {
58 public let spine: [Int]
59 public let openAngle: Double
60 public let phases: [Double]
61 public let openAxes: [AxisRecord]
62 public let popped: [SIMD3<Double>]
63 public let closeAxes: [AxisRecord]
64
65 public init(spine: [Int], openAngle: Double, phases: [Double], openAxes: [AxisRecord],
66 popped: [SIMD3<Double>], closeAxes: [AxisRecord]) {
67 self.spine = spine
68 self.openAngle = openAngle
69 self.phases = phases
70 self.openAxes = openAxes
71 self.popped = popped
72 self.closeAxes = closeAxes
73 }
74}
75
76/// `krz:step`: the authored step that produced a frame. A non-rigid step's `axes` still list its
77/// creases with their start and end angles; its motion is `pop`.
78public struct StepRecord: Codable, Equatable, Sendable {
79 public let kind: StepKind
80 public let technique: StepTechnique
81 public let instruction: String?
82 public let axes: [AxisRecord]
83 public let rotateDegrees: Double?
84 public let turnOverAxis: TurnOverAxis?
85 public let repeatOf: Int?
86 public let nonRigid: Bool
87 public let pop: PopRecord?
88 public let viewpoint: Viewpoint
89
90 public init(kind: StepKind, technique: StepTechnique = .plain, instruction: String?,
91 axes: [AxisRecord], rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil,
92 repeatOf: Int? = nil, nonRigid: Bool = false, pop: PopRecord? = nil,
93 viewpoint: Viewpoint) {
94 self.kind = kind
95 self.technique = technique
96 self.instruction = instruction
97 self.axes = axes
98 self.rotateDegrees = rotateDegrees
99 self.turnOverAxis = turnOverAxis
100 self.repeatOf = repeatOf
101 self.nonRigid = nonRigid
102 self.pop = pop
103 self.viewpoint = viewpoint
104 }
105
106 public init(from decoder: any Decoder) throws {
107 let c = try decoder.container(keyedBy: CodingKeys.self)
108 kind = try c.decode(StepKind.self, forKey: .kind)
109 technique = try c.decodeIfPresent(StepTechnique.self, forKey: .technique) ?? .plain
110 instruction = try c.decodeIfPresent(String.self, forKey: .instruction)
111 axes = try c.decode([AxisRecord].self, forKey: .axes)
112 rotateDegrees = try c.decodeIfPresent(Double.self, forKey: .rotateDegrees)
113 turnOverAxis = try c.decodeIfPresent(TurnOverAxis.self, forKey: .turnOverAxis)
114 repeatOf = try c.decodeIfPresent(Int.self, forKey: .repeatOf)
115 nonRigid = try c.decodeIfPresent(Bool.self, forKey: .nonRigid) ?? false
116 pop = try c.decodeIfPresent(PopRecord.self, forKey: .pop)
117 viewpoint = try c.decode(Viewpoint.self, forKey: .viewpoint)
118 }
119}