import FoldFormat import simd /// Reads every frame's authored `krz:step`. `result[k]` is step `k + 1`. /// /// - Precondition: `validate(file)` is empty. On a file it rejects, this may trap on an /// out-of-range index. public func readSteps(_ file: FoldFile) throws -> [FoldStep] { try file.frames.indices.dropFirst().map { n in try readStep(n, in: file) } } /// Reads step `n` from frame n's `krz:step`, directing its axes against frame n − 1's /// coordinates. Only frames 0…n are read, so frames after n need not exist. /// /// - Precondition: frames 0…n are well formed: frame n − 1 has a coordinate for every vertex /// and the topology's indices are in range. public func readStep(_ n: Int, in file: FoldFile) throws -> FoldStep { guard let record = file.frames[n].step else { throw FoldError.missingStepRecord(step: n) } return try readStep(n, record, file, edgesFaces(file)) } private func readStep(_ n: Int, _ record: StepRecord, _ file: FoldFile, _ edgeFaces: [[Int]]) throws -> FoldStep { guard let instruction = record.instruction, !instruction.allSatisfy(\.isWhitespace) else { throw StepError.missingInstruction(step: n) } if record.kind == .rotate && record.rotateDegrees == nil { throw StepError.rotateWithoutDegrees(step: n) } if record.kind == .turnOver && record.turnOverAxis == nil { throw StepError.turnOverWithoutAxis(step: n) } if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.peakAngle == nil }) { throw StepError.foldAndUnfoldWithoutPeak(step: n) } if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.endAngle != $0.startAngle }) { throw StepError.foldAndUnfoldDoesNotReturn(step: n) } if record.nonRigid && record.pop == nil { throw StepError.nonRigidWithoutPop(step: n) } let start = file.frames[n - 1].verticesCoords let axes = try readAxes(record.axes, step: n, points: start, file, edgeFaces) var pop: Pop? if let p = record.pop { guard p.phases.count == 2, 0 < p.phases[0], p.phases[0] < p.phases[1], p.phases[1] < 1 else { throw StepError.popPhasesOutOfOrder(step: n) } pop = Pop(spine: p.spine, openAngle: p.openAngle, phases: p.phases[0]...p.phases[1], openAxes: try readAxes(p.openAxes, step: n, points: start, file, edgeFaces), popped: p.popped, closeAxes: try readAxes(p.closeAxes, step: n, points: p.popped, file, edgeFaces)) } return FoldStep(index: n, kind: record.kind, technique: record.technique, instruction: instruction, axes: axes, rotateDegrees: record.rotateDegrees, turnOverAxis: record.turnOverAxis, repeatOf: record.repeatOf, nonRigid: record.nonRigid, pop: pop, viewpoint: record.viewpoint) } /// One step's axes, directed against `points`, reordered so every parent precedes its children. private func readAxes(_ records: [AxisRecord], step n: Int, points: [SIMD3], _ file: FoldFile, _ edgeFaces: [[Int]]) throws -> [FoldAxis] { var lines: [(origin: SIMD3, direction: SIMD3)] = [] var moved = Set() for r in records { guard file.edgesVertices.indices.contains(r.edge) else { throw StepError.axisNotAnEdge(step: n, edge: r.edge) } guard r.movingFaces.allSatisfy(file.facesVertices.indices.contains) else { throw StepError.movingFacesOutOfRange(step: n) } let sides = edgeFaces[r.edge] let movingSides = sides.filter(r.movingFaces.contains) guard sides.count == 2, movingSides.count == 1 else { throw StepError.axisNotOnMovingSetBoundary(step: n, edge: r.edge) } let window = r.window ?? [0, 1] guard window.count == 2, 0 <= window[0], window[0] < window[1], window[1] <= 1 else { throw StepError.axisWindowOutOfRange(step: n) } if let curve = r.angles, curve.count < 2 { throw StepError.curveTooShort(step: n) } let faceSet = Set(r.movingFaces) guard moved.isDisjoint(with: faceSet) else { throw StepError.overlappingMovingSetsWithinAStep(step: n) } moved.formUnion(faceSet) lines.append(traversal(of: file.edgesVertices[r.edge], by: file.facesVertices[movingSides[0]], points)) } // place each axis after its parent, keeping the authored order otherwise var state = Array(repeating: 0, count: records.count) // 0 unplaced, 1 placing, 2 placed var order: [Int] = [] func place(_ i: Int) throws { if state[i] == 2 { return } guard state[i] == 0 else { throw StepError.axisParentCycle(step: n) } state[i] = 1 if let p = records[i].parent { guard records.indices.contains(p) else { throw StepError.axisParentCycle(step: n) } try place(p) } state[i] = 2 order.append(i) } for i in records.indices { try place(i) } var position = Array(repeating: 0, count: records.count) for (k, i) in order.enumerated() { position[i] = k } return order.map { i in let r = records[i] let window = r.window ?? [0, 1] return FoldAxis(edge: r.edge, origin: lines[i].origin, direction: lines[i].direction, movingFaces: Set(r.movingFaces), startAngle: r.startAngle, endAngle: r.endAngle, peakAngle: r.peakAngle, parent: r.parent.map { position[$0] }, window: window[0]...window[1], curve: r.angles) } } /// The edge as `face` traverses it in `faces_vertices` order: the corner it leaves from and the /// unit direction toward the next. private func traversal(of edge: [Int], by face: [Int], _ points: [SIMD3]) -> (origin: SIMD3, direction: SIMD3) { let i = face.indices.first { i in Set([face[i], face[(i + 1) % face.count]]) == Set(edge) }! let origin = points[face[i]] return (origin, normalize(points[face[(i + 1) % face.count]] - origin)) }