FOLD decoding, validator and step reading (Tasks 1–3) !3
43 files changed, +3567 −6
Layout: unified · split
Packages/FoldKit/Package.swift added +23
| @@ -0,0 +1,23 @@ | |||
| 1 | // swift-tools-version: 6.2 | ||
| 2 | import PackageDescription | ||
| 3 | |||
| 4 | let package = Package( | ||
| 5 | name: "FoldKit", | ||
| 6 | platforms: [.iOS(.v26), .macOS(.v26)], | ||
| 7 | products: [ | ||
| 8 | .library(name: "FoldFormat", targets: ["FoldFormat"]), | ||
| 9 | .library(name: "Folding", targets: ["Folding"]), | ||
| 10 | ], | ||
| 11 | targets: [ | ||
| 12 | .target(name: "FoldFormat"), | ||
| 13 | .target(name: "Folding", dependencies: ["FoldFormat"]), | ||
| 14 | .target( | ||
| 15 | name: "FoldKitTestSupport", | ||
| 16 | dependencies: ["FoldFormat"], | ||
| 17 | resources: [.copy("Fixtures")] | ||
| 18 | ), | ||
| 19 | .testTarget(name: "FoldFormatTests", dependencies: ["FoldFormat", "FoldKitTestSupport"]), | ||
| 20 | .testTarget(name: "FoldingTests", dependencies: ["Folding", "FoldFormat", "FoldKitTestSupport"]), | ||
| 21 | ], | ||
| 22 | swiftLanguageModes: [.v6] | ||
| 23 | ) | ||
Packages/FoldKit/Sources/FoldFormat/EdgeAssignment.swift added +15
| @@ -0,0 +1,15 @@ | |||
| 1 | public enum EdgeAssignment: String, Codable, Sendable { | ||
| 2 | case boundary = "B" | ||
| 3 | case mountain = "M" | ||
| 4 | case valley = "V" | ||
| 5 | case flat = "F" | ||
| 6 | case unassigned = "U" | ||
| 7 | |||
| 8 | public init(from decoder: any Decoder) throws { | ||
| 9 | let code = try decoder.singleValueContainer().decode(String.self) | ||
| 10 | guard let assignment = EdgeAssignment(rawValue: code) else { | ||
| 11 | throw FoldError.unsupportedAssignment(code) | ||
| 12 | } | ||
| 13 | self = assignment | ||
| 14 | } | ||
| 15 | } | ||
Packages/FoldKit/Sources/FoldFormat/FoldFile.swift added +146
| @@ -0,0 +1,146 @@ | |||
| 1 | import Foundation | ||
| 2 | |||
| 3 | public enum FoldError: Error, Equatable, Sendable { | ||
| 4 | case unsupportedAssignment(String) | ||
| 5 | case malformedCoordinate(vertex: Int) | ||
| 6 | case missingStepRecord(step: Int) | ||
| 7 | case topologyInFrame(frame: Int) | ||
| 8 | case missingFrameInheritance(frame: Int) | ||
| 9 | /// An `edges_` array, named by its FOLD key, whose length differs from `edges_vertices`. | ||
| 10 | case edgeArrayLengthMismatch(frame: Int, key: String) | ||
| 11 | /// Entry `entry` of `faceOrders` or `krz:faceOrders_midFold`, named by `key`, is not | ||
| 12 | /// `[f, g, s]` with both faces in range and `s` in −1...1. | ||
| 13 | case malformedFaceOrder(frame: Int, key: String, entry: Int) | ||
| 14 | } | ||
| 15 | |||
| 16 | /// A FOLD file whose frames share one topology. `frames[0]` is the key frame, FOLD's top level; | ||
| 17 | /// `frames[n]` is frame n, `file_frames[n - 1]`, the result of step n. | ||
| 18 | public struct FoldFile: Codable, Sendable { | ||
| 19 | public var fileSpec: Double | ||
| 20 | public var fileCreator: String? | ||
| 21 | public var fileTitle: String? | ||
| 22 | public var fileAuthor: String? | ||
| 23 | public var fileClasses: [String] | ||
| 24 | public var edgesVertices: [[Int]] | ||
| 25 | public var facesVertices: [[Int]] | ||
| 26 | public var edgesFirstCreasedAtStep: [Int?] | ||
| 27 | public var frames: [FoldFrame] | ||
| 28 | |||
| 29 | public var stepCount: Int { frames.count - 1 } | ||
| 30 | |||
| 31 | public init(fileSpec: Double, fileCreator: String? = nil, fileTitle: String? = nil, | ||
| 32 | fileAuthor: String? = nil, fileClasses: [String] = [], edgesVertices: [[Int]], | ||
| 33 | facesVertices: [[Int]], edgesFirstCreasedAtStep: [Int?], frames: [FoldFrame]) { | ||
| 34 | self.fileSpec = fileSpec | ||
| 35 | self.fileCreator = fileCreator | ||
| 36 | self.fileTitle = fileTitle | ||
| 37 | self.fileAuthor = fileAuthor | ||
| 38 | self.fileClasses = fileClasses | ||
| 39 | self.edgesVertices = edgesVertices | ||
| 40 | self.facesVertices = facesVertices | ||
| 41 | self.edgesFirstCreasedAtStep = edgesFirstCreasedAtStep | ||
| 42 | self.frames = frames | ||
| 43 | } | ||
| 44 | |||
| 45 | public init(contentsOf url: URL) throws { | ||
| 46 | self = try JSONDecoder().decode(FoldFile.self, from: Data(contentsOf: url)) | ||
| 47 | } | ||
| 48 | |||
| 49 | enum CodingKeys: String, CodingKey { | ||
| 50 | case fileSpec = "file_spec" | ||
| 51 | case fileCreator = "file_creator" | ||
| 52 | case fileTitle = "file_title" | ||
| 53 | case fileAuthor = "file_author" | ||
| 54 | case fileClasses = "file_classes" | ||
| 55 | case edgesVertices = "edges_vertices" | ||
| 56 | case facesVertices = "faces_vertices" | ||
| 57 | case edgesFirstCreasedAtStep = "edges_krz:firstCreasedAtStep" | ||
| 58 | case fileFrames = "file_frames" | ||
| 59 | } | ||
| 60 | |||
| 61 | enum FrameKeys: String, CodingKey { | ||
| 62 | case frameParent = "frame_parent" | ||
| 63 | case frameInherit = "frame_inherit" | ||
| 64 | case edgesVertices = "edges_vertices" | ||
| 65 | case facesVertices = "faces_vertices" | ||
| 66 | } | ||
| 67 | |||
| 68 | public init(from decoder: any Decoder) throws { | ||
| 69 | let c = try decoder.container(keyedBy: CodingKeys.self) | ||
| 70 | fileSpec = try c.decode(Double.self, forKey: .fileSpec) | ||
| 71 | fileCreator = try c.decodeIfPresent(String.self, forKey: .fileCreator) | ||
| 72 | fileTitle = try c.decodeIfPresent(String.self, forKey: .fileTitle) | ||
| 73 | fileAuthor = try c.decodeIfPresent(String.self, forKey: .fileAuthor) | ||
| 74 | fileClasses = try c.decodeIfPresent([String].self, forKey: .fileClasses) ?? [] | ||
| 75 | edgesVertices = try c.decode([[Int]].self, forKey: .edgesVertices) | ||
| 76 | facesVertices = try c.decode([[Int]].self, forKey: .facesVertices) | ||
| 77 | edgesFirstCreasedAtStep = try c.decodeIfPresent([Int?].self, forKey: .edgesFirstCreasedAtStep) | ||
| 78 | ?? Array(repeating: nil, count: edgesVertices.count) | ||
| 79 | if edgesFirstCreasedAtStep.count != edgesVertices.count { | ||
| 80 | throw FoldError.edgeArrayLengthMismatch(frame: 0, key: CodingKeys.edgesFirstCreasedAtStep.rawValue) | ||
| 81 | } | ||
| 82 | |||
| 83 | let keyFrame = try FoldFrame(from: decoder, inheriting: nil) | ||
| 84 | frames = [keyFrame] | ||
| 85 | try checkShape(keyFrame, 0) | ||
| 86 | guard c.contains(.fileFrames) else { return } | ||
| 87 | var list = try c.nestedUnkeyedContainer(forKey: .fileFrames) | ||
| 88 | while !list.isAtEnd { | ||
| 89 | let n = frames.count | ||
| 90 | let frameDecoder = try list.superDecoder() | ||
| 91 | let meta = try frameDecoder.container(keyedBy: FrameKeys.self) | ||
| 92 | if meta.contains(.edgesVertices) || meta.contains(.facesVertices) { | ||
| 93 | throw FoldError.topologyInFrame(frame: n) | ||
| 94 | } | ||
| 95 | guard try meta.decodeIfPresent(Int.self, forKey: .frameParent) == 0, | ||
| 96 | try meta.decodeIfPresent(Bool.self, forKey: .frameInherit) == true else { | ||
| 97 | throw FoldError.missingFrameInheritance(frame: n) | ||
| 98 | } | ||
| 99 | let frame = try FoldFrame(from: frameDecoder, inheriting: keyFrame) | ||
| 100 | try checkShape(frame, n) | ||
| 101 | guard frame.step != nil else { throw FoldError.missingStepRecord(step: n) } | ||
| 102 | frames.append(frame) | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | /// Rejects frame `n`'s edge arrays that disagree with `edges_vertices` and its malformed | ||
| 107 | /// face orders. | ||
| 108 | private func checkShape(_ frame: FoldFrame, _ n: Int) throws { | ||
| 109 | let edgeArrays: [(FoldFrame.CodingKeys, Int)] = [(.edgesAssignment, frame.edgesAssignment.count), | ||
| 110 | (.edgesFoldAngle, frame.edgesFoldAngle.count)] | ||
| 111 | for (key, count) in edgeArrays where count != edgesVertices.count { | ||
| 112 | throw FoldError.edgeArrayLengthMismatch(frame: n, key: key.rawValue) | ||
| 113 | } | ||
| 114 | let faces = facesVertices.indices | ||
| 115 | let orders: [(FoldFrame.CodingKeys, [[Int]])] = [(.faceOrders, frame.faceOrders), | ||
| 116 | (.faceOrdersMidFold, frame.faceOrdersMidFold ?? [])] | ||
| 117 | for (key, list) in orders { | ||
| 118 | for (i, t) in list.enumerated() | ||
| 119 | where t.count != 3 || !faces.contains(t[0]) || !faces.contains(t[1]) || !(-1...1).contains(t[2]) { | ||
| 120 | throw FoldError.malformedFaceOrder(frame: n, key: key.rawValue, entry: i) | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | public func encode(to encoder: any Encoder) throws { | ||
| 126 | var c = encoder.container(keyedBy: CodingKeys.self) | ||
| 127 | try c.encode(fileSpec, forKey: .fileSpec) | ||
| 128 | try c.encodeIfPresent(fileCreator, forKey: .fileCreator) | ||
| 129 | try c.encodeIfPresent(fileTitle, forKey: .fileTitle) | ||
| 130 | try c.encodeIfPresent(fileAuthor, forKey: .fileAuthor) | ||
| 131 | try c.encode(fileClasses, forKey: .fileClasses) | ||
| 132 | try c.encode(edgesVertices, forKey: .edgesVertices) | ||
| 133 | try c.encode(facesVertices, forKey: .facesVertices) | ||
| 134 | try c.encode(edgesFirstCreasedAtStep, forKey: .edgesFirstCreasedAtStep) | ||
| 135 | try frames[0].encode(to: encoder) | ||
| 136 | guard frames.count > 1 else { return } | ||
| 137 | var list = c.nestedUnkeyedContainer(forKey: .fileFrames) | ||
| 138 | for frame in frames.dropFirst() { | ||
| 139 | let frameEncoder = list.superEncoder() | ||
| 140 | var meta = frameEncoder.container(keyedBy: FrameKeys.self) | ||
| 141 | try meta.encode(0, forKey: .frameParent) | ||
| 142 | try meta.encode(true, forKey: .frameInherit) | ||
| 143 | try frame.encode(to: frameEncoder) | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
Packages/FoldKit/Sources/FoldFormat/FoldFrame.swift added +73
| @@ -0,0 +1,73 @@ | |||
| 1 | /// The per-frame state of a model. Topology lives on `FoldFile`, shared by every frame. | ||
| 2 | public struct FoldFrame: Codable, Sendable { | ||
| 3 | public var verticesCoords: [SIMD3<Double>] | ||
| 4 | public var edgesAssignment: [EdgeAssignment] | ||
| 5 | public var edgesFoldAngle: [Double] | ||
| 6 | public var faceOrders: [[Int]] | ||
| 7 | public var faceOrdersMidFold: [[Int]]? | ||
| 8 | public var step: StepRecord? | ||
| 9 | |||
| 10 | public init(verticesCoords: [SIMD3<Double>], edgesAssignment: [EdgeAssignment], | ||
| 11 | edgesFoldAngle: [Double], faceOrders: [[Int]], faceOrdersMidFold: [[Int]]? = nil, | ||
| 12 | step: StepRecord? = nil) { | ||
| 13 | self.verticesCoords = verticesCoords | ||
| 14 | self.edgesAssignment = edgesAssignment | ||
| 15 | self.edgesFoldAngle = edgesFoldAngle | ||
| 16 | self.faceOrders = faceOrders | ||
| 17 | self.faceOrdersMidFold = faceOrdersMidFold | ||
| 18 | self.step = step | ||
| 19 | } | ||
| 20 | |||
| 21 | enum CodingKeys: String, CodingKey { | ||
| 22 | case verticesCoords = "vertices_coords" | ||
| 23 | case edgesAssignment = "edges_assignment" | ||
| 24 | case edgesFoldAngle = "edges_foldAngle" | ||
| 25 | case faceOrders | ||
| 26 | case faceOrdersMidFold = "krz:faceOrders_midFold" | ||
| 27 | case step = "krz:step" | ||
| 28 | } | ||
| 29 | |||
| 30 | public init(from decoder: any Decoder) throws { | ||
| 31 | try self.init(from: decoder, inheriting: nil) | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Decodes a frame, taking any of `vertices_coords`, `edges_assignment`, `edges_foldAngle` | ||
| 35 | /// and `faceOrders` it does not carry from `parent`. Without a parent, `edges_foldAngle` | ||
| 36 | /// defaults to zeros and `faceOrders` to empty. | ||
| 37 | init(from decoder: any Decoder, inheriting parent: FoldFrame?) throws { | ||
| 38 | let c = try decoder.container(keyedBy: CodingKeys.self) | ||
| 39 | if let parent, !c.contains(.verticesCoords) { | ||
| 40 | verticesCoords = parent.verticesCoords | ||
| 41 | } else { | ||
| 42 | let raw = try c.decode([[Double]].self, forKey: .verticesCoords) | ||
| 43 | verticesCoords = try raw.enumerated().map { vertex, xyz in | ||
| 44 | switch xyz.count { | ||
| 45 | case 2: SIMD3(xyz[0], xyz[1], 0) | ||
| 46 | case 3: SIMD3(xyz[0], xyz[1], xyz[2]) | ||
| 47 | default: throw FoldError.malformedCoordinate(vertex: vertex) | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | if let parent, !c.contains(.edgesAssignment) { | ||
| 52 | edgesAssignment = parent.edgesAssignment | ||
| 53 | } else { | ||
| 54 | edgesAssignment = try c.decode([EdgeAssignment].self, forKey: .edgesAssignment) | ||
| 55 | } | ||
| 56 | edgesFoldAngle = try c.decodeIfPresent([Double].self, forKey: .edgesFoldAngle) | ||
| 57 | ?? parent?.edgesFoldAngle | ||
| 58 | ?? Array(repeating: 0, count: edgesAssignment.count) | ||
| 59 | faceOrders = try c.decodeIfPresent([[Int]].self, forKey: .faceOrders) ?? parent?.faceOrders ?? [] | ||
| 60 | faceOrdersMidFold = try c.decodeIfPresent([[Int]].self, forKey: .faceOrdersMidFold) | ||
| 61 | step = try c.decodeIfPresent(StepRecord.self, forKey: .step) | ||
| 62 | } | ||
| 63 | |||
| 64 | public func encode(to encoder: any Encoder) throws { | ||
| 65 | var c = encoder.container(keyedBy: CodingKeys.self) | ||
| 66 | try c.encode(verticesCoords.map { [$0.x, $0.y, $0.z] }, forKey: .verticesCoords) | ||
| 67 | try c.encode(edgesAssignment, forKey: .edgesAssignment) | ||
| 68 | try c.encode(edgesFoldAngle, forKey: .edgesFoldAngle) | ||
| 69 | try c.encode(faceOrders, forKey: .faceOrders) | ||
| 70 | try c.encodeIfPresent(faceOrdersMidFold, forKey: .faceOrdersMidFold) | ||
| 71 | try c.encodeIfPresent(step, forKey: .step) | ||
| 72 | } | ||
| 73 | } | ||
Packages/FoldKit/Sources/FoldFormat/FoldValidator.swift added +551
| @@ -0,0 +1,551 @@ | |||
| 1 | import simd | ||
| 2 | |||
| 3 | public enum FoldDefect: Equatable, Sendable { | ||
| 4 | case vertexIndexOutOfRange(edge: Int) | ||
| 5 | case faceVertexOutOfRange(face: Int) | ||
| 6 | case vertexCountChanged(frame: Int) | ||
| 7 | case foldAngleOutOfRange(frame: Int, edge: Int, degrees: Double) | ||
| 8 | case foldAngleSignDisagreesWithAssignment(frame: Int, edge: Int) | ||
| 9 | case boundaryEdgeFolded(frame: Int, edge: Int) | ||
| 10 | case faceOrderNotAntisymmetric(frame: Int, f: Int, g: Int) | ||
| 11 | case cyclicFaceOrder(frame: Int, cycle: [Int]) | ||
| 12 | case nonIsometricTransition(step: Int, edge: Int, from: Double, to: Double) | ||
| 13 | case overlappingMovingSetsWithoutMidFoldOrder(step: Int) | ||
| 14 | case startAndEndOrderDisagreeOnStillFaces(step: Int, f: Int, g: Int) | ||
| 15 | case foldAndUnfoldWithoutPeakOrder(step: Int) | ||
| 16 | case orderDisagreesWithCrease(frame: Int, edge: Int) | ||
| 17 | case interleavedFolds(frame: Int, edges: [Int]) | ||
| 18 | case faceInsideFold(frame: Int, face: Int, edge: Int) | ||
| 19 | case nonConvexFace(face: Int) | ||
| 20 | case missingInstruction(step: Int) | ||
| 21 | case authorNotTraditional(String?) | ||
| 22 | case missingFirstCreasedAtStep(edge: Int) | ||
| 23 | case faceDistorted(frame: Int, face: Int) | ||
| 24 | } | ||
| 25 | |||
| 26 | /// Lengths are compared to within this fraction of the sheet's longer side. | ||
| 27 | private let relativeTolerance = 1e-6 | ||
| 28 | /// A crease within this many degrees of ±180 is folded flat. | ||
| 29 | private let flatAngleTolerance = 1e-6 | ||
| 30 | /// Two unit normals whose dot product's magnitude reaches this lie on one line. | ||
| 31 | private let parallelCosine = 1 - 1e-9 | ||
| 32 | /// A normal component smaller than this is treated as zero when choosing the canonical normal. | ||
| 33 | private let canonicalComponentThreshold = 1e-6 | ||
| 34 | |||
| 35 | public func validate(_ file: FoldFile) -> [FoldDefect] { | ||
| 36 | var defects: [FoldDefect] = [] | ||
| 37 | let vertexCount = file.frames[0].verticesCoords.count | ||
| 38 | let inRange = { (v: Int) in v >= 0 && v < vertexCount } | ||
| 39 | for (e, ends) in file.edgesVertices.enumerated() where ends.count != 2 || !ends.allSatisfy(inRange) { | ||
| 40 | defects.append(.vertexIndexOutOfRange(edge: e)) | ||
| 41 | } | ||
| 42 | for (f, corners) in file.facesVertices.enumerated() where !corners.allSatisfy(inRange) { | ||
| 43 | defects.append(.faceVertexOutOfRange(face: f)) | ||
| 44 | } | ||
| 45 | let topologyIsSound = defects.isEmpty | ||
| 46 | |||
| 47 | if file.fileAuthor != "Traditional" { | ||
| 48 | defects.append(.authorNotTraditional(file.fileAuthor)) | ||
| 49 | } | ||
| 50 | for n in file.frames.indices.dropFirst() { | ||
| 51 | let instruction = file.frames[n].step?.instruction ?? "" | ||
| 52 | if instruction.allSatisfy(\.isWhitespace) { | ||
| 53 | defects.append(.missingInstruction(step: n)) | ||
| 54 | } | ||
| 55 | } | ||
| 56 | guard topologyIsSound else { return defects } | ||
| 57 | |||
| 58 | let tolerance = sheetSize(file) * relativeTolerance | ||
| 59 | let edgeFaces = edgesFaces(file) | ||
| 60 | defects += nonConvexFaces(file) | ||
| 61 | defects += missingFirstCreasedAtStep(file, edgeFaces: edgeFaces) | ||
| 62 | |||
| 63 | var geometryIsSound: [Bool] = [] | ||
| 64 | for (n, frame) in file.frames.enumerated() { | ||
| 65 | let sound = frame.verticesCoords.count == vertexCount | ||
| 66 | geometryIsSound.append(sound) | ||
| 67 | if !sound { defects.append(.vertexCountChanged(frame: n)) } | ||
| 68 | defects += angleDefects(frame, n, edgeCount: file.edgesVertices.count) | ||
| 69 | if sound { | ||
| 70 | defects += layerOrderDefects(frame, n, file, edgeFaces: edgeFaces, tolerance: tolerance) | ||
| 71 | defects += faceShapeDefects(frame, n, file, tolerance: tolerance) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | for n in file.frames.indices.dropFirst() { | ||
| 76 | guard let step = file.frames[n].step else { continue } | ||
| 77 | let frame = file.frames[n] | ||
| 78 | if step.kind == .foldAndUnfold && frame.faceOrdersMidFold == nil { | ||
| 79 | defects.append(.foldAndUnfoldWithoutPeakOrder(step: n)) | ||
| 80 | } | ||
| 81 | guard geometryIsSound[n - 1] && geometryIsSound[n] else { continue } | ||
| 82 | if frame.faceOrdersMidFold == nil && movingSetsOverlapInProjection(step: n, file) { | ||
| 83 | defects.append(.overlappingMovingSetsWithoutMidFoldOrder(step: n)) | ||
| 84 | } | ||
| 85 | defects += isometryDefects(file, step: n, tolerance: tolerance) | ||
| 86 | defects += stillFaceDefects(file, step: n, moving: Set(step.axes.flatMap(\.movingFaces))) | ||
| 87 | } | ||
| 88 | return defects | ||
| 89 | } | ||
| 90 | |||
| 91 | /// Whether two of step `n`'s moving sets overlap in projection along the step's viewpoint, with | ||
| 92 | /// both sets where they arrive, in frame `n`. | ||
| 93 | public func movingSetsOverlapInProjection(step n: Int, _ file: FoldFile) -> Bool { | ||
| 94 | guard n >= 1, n < file.frames.count, let step = file.frames[n].step, step.axes.count > 1 else { | ||
| 95 | return false | ||
| 96 | } | ||
| 97 | let points = file.frames[n].verticesCoords | ||
| 98 | let eye = normalize(step.viewpoint.eye) | ||
| 99 | let right = normalize(cross(step.viewpoint.up, eye)) | ||
| 100 | let up = cross(eye, right) | ||
| 101 | let tolerance = sheetSize(file) * relativeTolerance | ||
| 102 | |||
| 103 | func outline(_ face: Int) -> [SIMD2<Double>]? { | ||
| 104 | guard face >= 0, face < file.facesVertices.count else { return nil } | ||
| 105 | let corners = file.facesVertices[face] | ||
| 106 | guard corners.allSatisfy({ $0 >= 0 && $0 < points.count }) else { return nil } | ||
| 107 | return corners.map { SIMD2(dot(points[$0], right), dot(points[$0], up)) } | ||
| 108 | } | ||
| 109 | |||
| 110 | let sets = step.axes.map { axis in axis.movingFaces.compactMap { face in outline(face).map { (face, $0) } } } | ||
| 111 | for i in sets.indices { | ||
| 112 | for j in sets.indices where j > i { | ||
| 113 | for (a, p) in sets[i] { | ||
| 114 | for (b, q) in sets[j] where a != b && convexPolygonsOverlap(p, q, tolerance: tolerance) { | ||
| 115 | return true | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | return false | ||
| 121 | } | ||
| 122 | |||
| 123 | /// Each face's unit normal in `frame`, from the counterclockwise order of `faces_vertices`. | ||
| 124 | /// A degenerate face, or one naming a vertex the frame lacks, has a zero normal. | ||
| 125 | public func faceNormals(_ frame: FoldFrame, _ file: FoldFile) -> [SIMD3<Double>] { | ||
| 126 | faceNormals(positions: frame.verticesCoords, faces: file.facesVertices) | ||
| 127 | } | ||
| 128 | |||
| 129 | /// Each face's unit normal with its vertices at `positions`, from the counterclockwise order of | ||
| 130 | /// `faces`. A degenerate face, or one naming a vertex `positions` lacks, has a zero normal. | ||
| 131 | public func faceNormals(positions: [SIMD3<Double>], faces: [[Int]]) -> [SIMD3<Double>] { | ||
| 132 | faces.map { corners in | ||
| 133 | guard corners.allSatisfy({ $0 >= 0 && $0 < positions.count }) else { return .zero } | ||
| 134 | let n = newellNormal(corners.map { positions[$0] }) | ||
| 135 | let size = length(n) | ||
| 136 | return size > 0 ? n / size : .zero | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | /// The faces on each side of each edge, from consecutive corners of `faces_vertices`. An edge | ||
| 141 | /// without two ends has none. | ||
| 142 | public func edgesFaces(_ file: FoldFile) -> [[Int]] { | ||
| 143 | var byEnds: [SIMD2<Int>: [Int]] = [:] | ||
| 144 | for (f, corners) in file.facesVertices.enumerated() { | ||
| 145 | for i in corners.indices { | ||
| 146 | let u = corners[i], v = corners[(i + 1) % corners.count] | ||
| 147 | byEnds[SIMD2(min(u, v), max(u, v)), default: []].append(f) | ||
| 148 | } | ||
| 149 | } | ||
| 150 | return file.edgesVertices.map { ends in | ||
| 151 | guard ends.count == 2 else { return [] } | ||
| 152 | return byEnds[SIMD2(min(ends[0], ends[1]), max(ends[0], ends[1]))] ?? [] | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | /// Each triple `[f, g, s]` with `s ≠ 0` as a pair ordered along the canonical normal of the two | ||
| 157 | /// faces' common plane, the one of `g`'s normal and its negation whose first nonzero component is | ||
| 158 | /// positive. Triples with `s = 0`, a repeated face or a face outside `normals` are skipped. | ||
| 159 | public func normalisedFaceOrders(_ orders: [[Int]], normals: [SIMD3<Double>]) -> [(below: Int, above: Int)] { | ||
| 160 | validTriples(orders, faceCount: normals.count).map { f, g, s in | ||
| 161 | let ng = normals[g] | ||
| 162 | let fAbove = dot(ng, canonical(ng)) > 0 ? s > 0 : s < 0 | ||
| 163 | return fAbove ? (below: g, above: f) : (below: f, above: g) | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | // MARK: - File-level rules | ||
| 168 | |||
| 169 | private func sheetSize(_ file: FoldFile) -> Double { | ||
| 170 | let points = file.frames[0].verticesCoords | ||
| 171 | guard let first = points.first else { return 0 } | ||
| 172 | let low = points.reduce(first) { simd_min($0, $1) } | ||
| 173 | let high = points.reduce(first) { simd_max($0, $1) } | ||
| 174 | return (high - low).max() | ||
| 175 | } | ||
| 176 | |||
| 177 | private func nonConvexFaces(_ file: FoldFile) -> [FoldDefect] { | ||
| 178 | let points = file.frames[0].verticesCoords | ||
| 179 | var defects: [FoldDefect] = [] | ||
| 180 | for (f, corners) in file.facesVertices.enumerated() { | ||
| 181 | let polygon = corners.map { points[$0] } | ||
| 182 | let n = newellNormal(polygon) | ||
| 183 | guard length(n) > 0 else { continue } | ||
| 184 | let count = polygon.count | ||
| 185 | let reflex = polygon.indices.contains { i in | ||
| 186 | let into = polygon[i] - polygon[(i + count - 1) % count] | ||
| 187 | let out = polygon[(i + 1) % count] - polygon[i] | ||
| 188 | guard length(into) > 0, length(out) > 0 else { return false } | ||
| 189 | return dot(cross(normalize(into), normalize(out)), normalize(n)) < -1e-9 | ||
| 190 | } | ||
| 191 | if reflex { defects.append(.nonConvexFace(face: f)) } | ||
| 192 | } | ||
| 193 | return defects | ||
| 194 | } | ||
| 195 | |||
| 196 | /// A non-boundary edge without `edges_krz:firstCreasedAtStep` is a defect unless it is a | ||
| 197 | /// subdivision edge: `F` and flat in every frame, and creased by no step. | ||
| 198 | private func missingFirstCreasedAtStep(_ file: FoldFile, edgeFaces: [[Int]]) -> [FoldDefect] { | ||
| 199 | let creased = creasedEdges(file, edgeFaces: edgeFaces) | ||
| 200 | return file.edgesVertices.indices.compactMap { e in | ||
| 201 | let assignment = file.frames[0].edgesAssignment | ||
| 202 | guard e < assignment.count, assignment[e] != .boundary else { return nil } | ||
| 203 | if e < file.edgesFirstCreasedAtStep.count, file.edgesFirstCreasedAtStep[e] != nil { return nil } | ||
| 204 | let subdivision = !creased.contains(e) && file.frames.allSatisfy { frame in | ||
| 205 | e < frame.edgesAssignment.count && e < frame.edgesFoldAngle.count | ||
| 206 | && frame.edgesAssignment[e] == .flat && frame.edgesFoldAngle[e] == 0 | ||
| 207 | } | ||
| 208 | return subdivision ? nil : .missingFirstCreasedAtStep(edge: e) | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | /// The edges some step creases: those whose two faces the step moves differently. In each of a | ||
| 213 | /// step's axis lists (`axes`, and a pop's `openAxes` and `closeAxes`), a face moves with the | ||
| 214 | /// deepest axis whose `movingFaces` contains it, or stays fixed when none does. | ||
| 215 | private func creasedEdges(_ file: FoldFile, edgeFaces: [[Int]]) -> Set<Int> { | ||
| 216 | let lists = file.frames.compactMap(\.step).flatMap { step in | ||
| 217 | [step.axes] + (step.pop.map { [$0.openAxes, $0.closeAxes] } ?? []) | ||
| 218 | } | ||
| 219 | let groupings = lists.map(movementGroups) | ||
| 220 | return Set(edgeFaces.indices.filter { e in | ||
| 221 | let faces = edgeFaces[e] | ||
| 222 | return faces.count == 2 && groupings.contains { $0[faces[0]] != $0[faces[1]] } | ||
| 223 | }) | ||
| 224 | } | ||
| 225 | |||
| 226 | /// Each moved face's axis: the deepest in the parent tree whose `movingFaces` contains it, the | ||
| 227 | /// first listed among equals. A face absent from the result is fixed. | ||
| 228 | private func movementGroups(_ axes: [AxisRecord]) -> [Int: Int] { | ||
| 229 | func depth(_ i: Int) -> Int { | ||
| 230 | var d = 0, j = i | ||
| 231 | while d < axes.count, let p = axes[j].parent, axes.indices.contains(p) { | ||
| 232 | d += 1 | ||
| 233 | j = p | ||
| 234 | } | ||
| 235 | return d | ||
| 236 | } | ||
| 237 | var group: [Int: (axis: Int, depth: Int)] = [:] | ||
| 238 | for (i, axis) in axes.enumerated() { | ||
| 239 | let d = depth(i) | ||
| 240 | for f in axis.movingFaces where group[f].map({ d > $0.depth }) ?? true { | ||
| 241 | group[f] = (i, d) | ||
| 242 | } | ||
| 243 | } | ||
| 244 | return group.mapValues(\.axis) | ||
| 245 | } | ||
| 246 | |||
| 247 | // MARK: - Frame rules | ||
| 248 | |||
| 249 | /// Every pair of a face's vertices must be as far apart as in frame 0. | ||
| 250 | private func faceShapeDefects(_ frame: FoldFrame, _ n: Int, _ file: FoldFile, tolerance: Double) -> [FoldDefect] { | ||
| 251 | let flat = file.frames[0].verticesCoords, points = frame.verticesCoords | ||
| 252 | return file.facesVertices.indices.compactMap { f in | ||
| 253 | let corners = file.facesVertices[f] | ||
| 254 | let distorted = corners.indices.contains { i in | ||
| 255 | corners[(i + 1)...].contains { v in | ||
| 256 | let u = corners[i] | ||
| 257 | return abs(distance(points[u], points[v]) - distance(flat[u], flat[v])) > tolerance | ||
| 258 | } | ||
| 259 | } | ||
| 260 | return distorted ? .faceDistorted(frame: n, face: f) : nil | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | private func angleDefects(_ frame: FoldFrame, _ n: Int, edgeCount: Int) -> [FoldDefect] { | ||
| 265 | var defects: [FoldDefect] = [] | ||
| 266 | for e in 0..<min(edgeCount, frame.edgesAssignment.count, frame.edgesFoldAngle.count) { | ||
| 267 | let angle = frame.edgesFoldAngle[e] | ||
| 268 | if abs(angle) > 180 { | ||
| 269 | defects.append(.foldAngleOutOfRange(frame: n, edge: e, degrees: angle)) | ||
| 270 | } | ||
| 271 | switch frame.edgesAssignment[e] { | ||
| 272 | case .boundary: | ||
| 273 | if angle != 0 { defects.append(.boundaryEdgeFolded(frame: n, edge: e)) } | ||
| 274 | case .valley: | ||
| 275 | if !(angle > 0) { defects.append(.foldAngleSignDisagreesWithAssignment(frame: n, edge: e)) } | ||
| 276 | case .mountain: | ||
| 277 | if !(angle < 0) { defects.append(.foldAngleSignDisagreesWithAssignment(frame: n, edge: e)) } | ||
| 278 | case .flat, .unassigned: | ||
| 279 | if angle != 0 { defects.append(.foldAngleSignDisagreesWithAssignment(frame: n, edge: e)) } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | return defects | ||
| 283 | } | ||
| 284 | |||
| 285 | /// Antisymmetry, cycles, and the crease, nesting and inside rules over the frame's `faceOrders`. | ||
| 286 | private func layerOrderDefects(_ frame: FoldFrame, _ n: Int, _ file: FoldFile, edgeFaces: [[Int]], | ||
| 287 | tolerance: Double) -> [FoldDefect] { | ||
| 288 | var defects: [FoldDefect] = [] | ||
| 289 | let normals = faceNormals(frame, file) | ||
| 290 | let faceCount = file.facesVertices.count | ||
| 291 | |||
| 292 | var signs: [SIMD2<Int>: Int] = [:] | ||
| 293 | var reported: Set<SIMD2<Int>> = [] | ||
| 294 | for (f, g, s) in validTriples(frame.faceOrders, faceCount: faceCount) { | ||
| 295 | if let t = signs[SIMD2(g, f)] { | ||
| 296 | let expected = dot(normals[f], normals[g]) > 0 ? -t : t | ||
| 297 | if s != expected && reported.insert(SIMD2(min(f, g), max(f, g))).inserted { | ||
| 298 | defects.append(.faceOrderNotAntisymmetric(frame: n, f: f, g: g)) | ||
| 299 | } | ||
| 300 | } | ||
| 301 | signs[SIMD2(f, g)] = s | ||
| 302 | } | ||
| 303 | |||
| 304 | var aboveOf = Array(repeating: Set<Int>(), count: faceCount) | ||
| 305 | for pair in normalisedFaceOrders(frame.faceOrders, normals: normals) { | ||
| 306 | aboveOf[pair.below].insert(pair.above) | ||
| 307 | } | ||
| 308 | let graph = aboveOf.map { $0.sorted() } | ||
| 309 | if let cycle = firstCycle(graph) { | ||
| 310 | defects.append(.cyclicFaceOrder(frame: n, cycle: cycle)) | ||
| 311 | } | ||
| 312 | |||
| 313 | // isAbove[x][y]: y lies above x in the stack, directly or through other faces | ||
| 314 | let isAbove = reachability(graph) | ||
| 315 | let geometry = FrameGeometry(points: frame.verticesCoords, faces: file.facesVertices, | ||
| 316 | normals: normals, tolerance: tolerance) | ||
| 317 | let edgeCount = min(file.edgesVertices.count, frame.edgesAssignment.count, frame.edgesFoldAngle.count) | ||
| 318 | let flatCreases = (0..<edgeCount).filter { e in | ||
| 319 | abs(abs(frame.edgesFoldAngle[e]) - 180) <= flatAngleTolerance | ||
| 320 | && edgeFaces[e].count == 2 && edgeFaces[e][0] != edgeFaces[e][1] | ||
| 321 | } | ||
| 322 | |||
| 323 | // crease: a valley points each face's normal toward the other, a mountain away | ||
| 324 | for e in flatCreases { | ||
| 325 | let assignment = frame.edgesAssignment[e] | ||
| 326 | guard assignment == .valley || assignment == .mountain else { continue } | ||
| 327 | let f = edgeFaces[e][0], g = edgeFaces[e][1] | ||
| 328 | let gOnNormalSideOfF = assignment == .valley | ||
| 329 | let gAbove = gOnNormalSideOfF == (dot(normals[f], canonical(normals[f])) > 0) | ||
| 330 | let (below, above) = gAbove ? (f, g) : (g, f) | ||
| 331 | if isAbove[above][below] { | ||
| 332 | defects.append(.orderDisagreesWithCrease(frame: n, edge: e)) | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | // nesting: the face pairs of two coincident creases nest or are disjoint in the stack | ||
| 337 | for (i, e1) in flatCreases.enumerated() { | ||
| 338 | for e2 in flatCreases[(i + 1)...] { | ||
| 339 | let pair1 = edgeFaces[e1], pair2 = edgeFaces[e2] | ||
| 340 | let four = pair1 + pair2 | ||
| 341 | guard Set(four).count == 4, | ||
| 342 | four.allSatisfy({ geometry.coplanar($0, pair1[0]) }), | ||
| 343 | geometry.segmentsCoincide(file.edgesVertices[e1], file.edgesVertices[e2]), | ||
| 344 | geometry.onSameSide(of: file.edgesVertices[e1], pair1[0], pair2[0]) else { continue } | ||
| 345 | let determined = four.allSatisfy { x in four.allSatisfy { y in x == y || isAbove[x][y] || isAbove[y][x] } } | ||
| 346 | guard determined else { continue } | ||
| 347 | let rank = { (x: Int) in four.filter { isAbove[$0][x] }.count } | ||
| 348 | let (p, q) = (min(rank(pair1[0]), rank(pair1[1])), max(rank(pair1[0]), rank(pair1[1]))) | ||
| 349 | let (r, s) = (min(rank(pair2[0]), rank(pair2[1])), max(rank(pair2[0]), rank(pair2[1]))) | ||
| 350 | if (p < r && r < q && q < s) || (r < p && p < s && s < q) { | ||
| 351 | defects.append(.interleavedFolds(frame: n, edges: [e1, e2])) | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | // inside: a face whose interior the crease runs through cannot lie between the crease's faces | ||
| 357 | for e in flatCreases { | ||
| 358 | let a = edgeFaces[e][0], b = edgeFaces[e][1] | ||
| 359 | for h in 0..<faceCount where h != a && h != b && geometry.coplanar(h, a) { | ||
| 360 | let between = (isAbove[a][h] && isAbove[h][b]) || (isAbove[b][h] && isAbove[h][a]) | ||
| 361 | if between && geometry.segmentCrossesInterior(file.edgesVertices[e], face: h) { | ||
| 362 | defects.append(.faceInsideFold(frame: n, face: h, edge: e)) | ||
| 363 | } | ||
| 364 | } | ||
| 365 | } | ||
| 366 | return defects | ||
| 367 | } | ||
| 368 | |||
| 369 | // MARK: - Step rules | ||
| 370 | |||
| 371 | private func isometryDefects(_ file: FoldFile, step n: Int, tolerance: Double) -> [FoldDefect] { | ||
| 372 | let before = file.frames[n - 1].verticesCoords, after = file.frames[n].verticesCoords | ||
| 373 | return file.edgesVertices.enumerated().compactMap { e, ends in | ||
| 374 | let from = distance(before[ends[0]], before[ends[1]]) | ||
| 375 | let to = distance(after[ends[0]], after[ends[1]]) | ||
| 376 | return abs(to - from) > tolerance ? .nonIsometricTransition(step: n, edge: e, from: from, to: to) : nil | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | /// Frame n − 1 and frame n must order every pair of faces that step n leaves still the same way. | ||
| 381 | /// Each triple is compared in a form a rigid motion leaves unchanged: rewritten with the lower | ||
| 382 | /// face first, `s` negated when swapping two faces whose normals agree. | ||
| 383 | private func stillFaceDefects(_ file: FoldFile, step n: Int, moving: Set<Int>) -> [FoldDefect] { | ||
| 384 | func relations(_ frame: FoldFrame) -> [(pair: SIMD2<Int>, s: Int)] { | ||
| 385 | let normals = faceNormals(frame, file) | ||
| 386 | return validTriples(frame.faceOrders, faceCount: file.facesVertices.count) | ||
| 387 | .filter { f, g, _ in !moving.contains(f) && !moving.contains(g) } | ||
| 388 | .map { f, g, s in | ||
| 389 | if f < g { return (SIMD2(f, g), s) } | ||
| 390 | return (SIMD2(g, f), dot(normals[f], normals[g]) > 0 ? -s : s) | ||
| 391 | } | ||
| 392 | } | ||
| 393 | var after: [SIMD2<Int>: Int] = [:] | ||
| 394 | for (pair, s) in relations(file.frames[n]) where after[pair] == nil { | ||
| 395 | after[pair] = s | ||
| 396 | } | ||
| 397 | var reported: Set<SIMD2<Int>> = [] | ||
| 398 | var defects: [FoldDefect] = [] | ||
| 399 | for (pair, s) in relations(file.frames[n - 1]) { | ||
| 400 | if let t = after[pair], t != s, reported.insert(pair).inserted { | ||
| 401 | defects.append(.startAndEndOrderDisagreeOnStillFaces(step: n, f: pair[0], g: pair[1])) | ||
| 402 | } | ||
| 403 | } | ||
| 404 | return defects | ||
| 405 | } | ||
| 406 | |||
| 407 | // MARK: - Helpers | ||
| 408 | |||
| 409 | private func validTriples(_ orders: [[Int]], faceCount: Int) -> [(Int, Int, Int)] { | ||
| 410 | orders.compactMap { t in | ||
| 411 | guard t.count == 3 else { return nil } | ||
| 412 | let (f, g, s) = (t[0], t[1], t[2]) | ||
| 413 | guard f >= 0, f < faceCount, g >= 0, g < faceCount, f != g, s == 1 || s == -1 else { return nil } | ||
| 414 | return (f, g, s) | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | /// `n` or `-n`, whichever has its first nonzero component positive. | ||
| 419 | private func canonical(_ n: SIMD3<Double>) -> SIMD3<Double> { | ||
| 420 | for i in 0..<3 where abs(n[i]) > canonicalComponentThreshold { | ||
| 421 | return n[i] > 0 ? n : -n | ||
| 422 | } | ||
| 423 | return n | ||
| 424 | } | ||
| 425 | |||
| 426 | private func newellNormal(_ polygon: [SIMD3<Double>]) -> SIMD3<Double> { | ||
| 427 | var n = SIMD3<Double>.zero | ||
| 428 | for i in polygon.indices { | ||
| 429 | let p = polygon[i], q = polygon[(i + 1) % polygon.count] | ||
| 430 | n.x += (p.y - q.y) * (p.z + q.z) | ||
| 431 | n.y += (p.z - q.z) * (p.x + q.x) | ||
| 432 | n.z += (p.x - q.x) * (p.y + q.y) | ||
| 433 | } | ||
| 434 | return n | ||
| 435 | } | ||
| 436 | |||
| 437 | /// The first cycle a depth-first search of the below → above graph meets, as the faces along it, | ||
| 438 | /// each above the next. | ||
| 439 | private func firstCycle(_ graph: [[Int]]) -> [Int]? { | ||
| 440 | var state = Array(repeating: 0, count: graph.count) // 0 unvisited, 1 on the path, 2 finished | ||
| 441 | var path: [Int] = [] | ||
| 442 | func visit(_ u: Int) -> [Int]? { | ||
| 443 | state[u] = 1 | ||
| 444 | path.append(u) | ||
| 445 | for v in graph[u] { | ||
| 446 | if state[v] == 1, let start = path.lastIndex(of: v) { return Array(path[start...].reversed()) } | ||
| 447 | if state[v] == 0, let cycle = visit(v) { return cycle } | ||
| 448 | } | ||
| 449 | path.removeLast() | ||
| 450 | state[u] = 2 | ||
| 451 | return nil | ||
| 452 | } | ||
| 453 | for u in graph.indices where state[u] == 0 { | ||
| 454 | if let cycle = visit(u) { return cycle } | ||
| 455 | } | ||
| 456 | return nil | ||
| 457 | } | ||
| 458 | |||
| 459 | private func reachability(_ graph: [[Int]]) -> [[Bool]] { | ||
| 460 | graph.indices.map { start in | ||
| 461 | var seen = Array(repeating: false, count: graph.count) | ||
| 462 | var stack = graph[start] | ||
| 463 | while let u = stack.popLast() { | ||
| 464 | if seen[u] { continue } | ||
| 465 | seen[u] = true | ||
| 466 | stack += graph[u] | ||
| 467 | } | ||
| 468 | return seen | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | /// Whether two convex polygons share interior area wider than `tolerance`, by separating axes. | ||
| 473 | private func convexPolygonsOverlap(_ p: [SIMD2<Double>], _ q: [SIMD2<Double>], tolerance: Double) -> Bool { | ||
| 474 | var tested = false | ||
| 475 | for polygon in [p, q] { | ||
| 476 | for i in polygon.indices { | ||
| 477 | let edge = polygon[(i + 1) % polygon.count] - polygon[i] | ||
| 478 | guard length(edge) > tolerance else { continue } | ||
| 479 | let axis = normalize(SIMD2(-edge.y, edge.x)) | ||
| 480 | let a = p.map { dot($0, axis) }, b = q.map { dot($0, axis) } | ||
| 481 | guard let aMin = a.min(), let aMax = a.max(), let bMin = b.min(), let bMax = b.max() else { return false } | ||
| 482 | if min(aMax, bMax) - max(aMin, bMin) <= tolerance { return false } | ||
| 483 | tested = true | ||
| 484 | } | ||
| 485 | } | ||
| 486 | return tested | ||
| 487 | } | ||
| 488 | |||
| 489 | private struct FrameGeometry { | ||
| 490 | let points: [SIMD3<Double>] | ||
| 491 | let faces: [[Int]] | ||
| 492 | let normals: [SIMD3<Double>] | ||
| 493 | let tolerance: Double | ||
| 494 | |||
| 495 | func centroid(_ f: Int) -> SIMD3<Double> { | ||
| 496 | faces[f].reduce(.zero) { $0 + points[$1] } / Double(max(faces[f].count, 1)) | ||
| 497 | } | ||
| 498 | |||
| 499 | func coplanar(_ f: Int, _ g: Int) -> Bool { | ||
| 500 | abs(dot(normals[f], normals[g])) >= parallelCosine | ||
| 501 | && abs(dot(centroid(g) - centroid(f), normals[f])) <= tolerance | ||
| 502 | } | ||
| 503 | |||
| 504 | /// Whether two edges lie on one line and share a stretch longer than `tolerance`. | ||
| 505 | func segmentsCoincide(_ e1: [Int], _ e2: [Int]) -> Bool { | ||
| 506 | let a = points[e1[0]], b = points[e1[1]] | ||
| 507 | let span = length(b - a) | ||
| 508 | guard span > tolerance else { return false } | ||
| 509 | let direction = (b - a) / span | ||
| 510 | let along = e2.map { dot(points[$0] - a, direction) } | ||
| 511 | let offLine = e2.contains { v in | ||
| 512 | let d = points[v] - a | ||
| 513 | return length(d - dot(d, direction) * direction) > tolerance | ||
| 514 | } | ||
| 515 | guard !offLine else { return false } | ||
| 516 | return min(along.max()!, span) - max(along.min()!, 0) > tolerance | ||
| 517 | } | ||
| 518 | |||
| 519 | /// Whether faces `f` and `g` lie on the same side of the line through `edge`, within their plane. | ||
| 520 | func onSameSide(of edge: [Int], _ f: Int, _ g: Int) -> Bool { | ||
| 521 | let a = points[edge[0]] | ||
| 522 | let across = cross(normals[f], points[edge[1]] - a) | ||
| 523 | return dot(centroid(f) - a, across) * dot(centroid(g) - a, across) > 0 | ||
| 524 | } | ||
| 525 | |||
| 526 | /// Whether the segment `edge` runs through the interior of face `h` for longer than `tolerance`. | ||
| 527 | func segmentCrossesInterior(_ edge: [Int], face h: Int) -> Bool { | ||
| 528 | let a = points[edge[0]], b = points[edge[1]] | ||
| 529 | let polygon = faces[h].map { points[$0] } | ||
| 530 | let n = normals[h] | ||
| 531 | guard let corner = polygon.first, | ||
| 532 | abs(dot(a - corner, n)) <= tolerance, abs(dot(b - corner, n)) <= tolerance else { return false } | ||
| 533 | let d = b - a | ||
| 534 | var (t0, t1) = (0.0, 1.0) | ||
| 535 | for i in polygon.indices { | ||
| 536 | let p = polygon[i], side = polygon[(i + 1) % polygon.count] - p | ||
| 537 | guard length(side) > tolerance else { continue } | ||
| 538 | // counterclockwise about n, so cross(n, side) points inward; keep points deeper than tolerance | ||
| 539 | let inward = normalize(cross(n, side)) | ||
| 540 | let start = dot(a - p, inward) - tolerance, rate = dot(d, inward) | ||
| 541 | if rate == 0 { | ||
| 542 | if start < 0 { return false } | ||
| 543 | continue | ||
| 544 | } | ||
| 545 | let t = -start / rate | ||
| 546 | if rate > 0 { t0 = max(t0, t) } else { t1 = min(t1, t) } | ||
| 547 | if t0 >= t1 { return false } | ||
| 548 | } | ||
| 549 | return (t1 - t0) * length(d) > tolerance | ||
| 550 | } | ||
| 551 | } | ||
Packages/FoldKit/Sources/FoldFormat/StepRecord.swift added +116
| @@ -0,0 +1,116 @@ | |||
| 1 | public enum StepKind: String, Codable, Sendable { | ||
| 2 | case fold, foldAndUnfold, turnOver, rotate | ||
| 3 | } | ||
| 4 | |||
| 5 | public enum StepTechnique: String, Codable, Sendable { | ||
| 6 | case plain, insideReverse, outsideReverse, squash, petal, sink | ||
| 7 | } | ||
| 8 | |||
| 9 | public 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. | ||
| 16 | public 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. | ||
| 41 | public 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 about its `spine` to | ||
| 52 | /// `openAngle` on `[0, phases[0]]`, every vertex reaches `popped` by `phases[1]`, and | ||
| 53 | /// `closeAxes`, relative to `popped`, close the flap on `[phases[1], 1]`. | ||
| 54 | public struct PopRecord: Codable, Equatable, Sendable { | ||
| 55 | public let spine: [Int] | ||
| 56 | public let openAngle: Double | ||
| 57 | public let phases: [Double] | ||
| 58 | public let openAxes: [AxisRecord] | ||
| 59 | public let popped: [SIMD3<Double>] | ||
| 60 | public let closeAxes: [AxisRecord] | ||
| 61 | |||
| 62 | public init(spine: [Int], openAngle: Double, phases: [Double], openAxes: [AxisRecord], | ||
| 63 | popped: [SIMD3<Double>], closeAxes: [AxisRecord]) { | ||
| 64 | self.spine = spine | ||
| 65 | self.openAngle = openAngle | ||
| 66 | self.phases = phases | ||
| 67 | self.openAxes = openAxes | ||
| 68 | self.popped = popped | ||
| 69 | self.closeAxes = closeAxes | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | /// `krz:step`: the authored step that produced a frame. A non-rigid step's `axes` still list its | ||
| 74 | /// creases with their start and end angles; its motion is `pop`. | ||
| 75 | public struct StepRecord: Codable, Equatable, Sendable { | ||
| 76 | public let kind: StepKind | ||
| 77 | public let technique: StepTechnique | ||
| 78 | public let instruction: String? | ||
| 79 | public let axes: [AxisRecord] | ||
| 80 | public let rotateDegrees: Double? | ||
| 81 | public let turnOverAxis: TurnOverAxis? | ||
| 82 | public let repeatOf: Int? | ||
| 83 | public let nonRigid: Bool | ||
| 84 | public let pop: PopRecord? | ||
| 85 | public let viewpoint: Viewpoint | ||
| 86 | |||
| 87 | public init(kind: StepKind, technique: StepTechnique = .plain, instruction: String?, | ||
| 88 | axes: [AxisRecord], rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil, | ||
| 89 | repeatOf: Int? = nil, nonRigid: Bool = false, pop: PopRecord? = nil, | ||
| 90 | viewpoint: Viewpoint) { | ||
| 91 | self.kind = kind | ||
| 92 | self.technique = technique | ||
| 93 | self.instruction = instruction | ||
| 94 | self.axes = axes | ||
| 95 | self.rotateDegrees = rotateDegrees | ||
| 96 | self.turnOverAxis = turnOverAxis | ||
| 97 | self.repeatOf = repeatOf | ||
| 98 | self.nonRigid = nonRigid | ||
| 99 | self.pop = pop | ||
| 100 | self.viewpoint = viewpoint | ||
| 101 | } | ||
| 102 | |||
| 103 | public init(from decoder: any Decoder) throws { | ||
| 104 | let c = try decoder.container(keyedBy: CodingKeys.self) | ||
| 105 | kind = try c.decode(StepKind.self, forKey: .kind) | ||
| 106 | technique = try c.decodeIfPresent(StepTechnique.self, forKey: .technique) ?? .plain | ||
| 107 | instruction = try c.decodeIfPresent(String.self, forKey: .instruction) | ||
| 108 | axes = try c.decode([AxisRecord].self, forKey: .axes) | ||
| 109 | rotateDegrees = try c.decodeIfPresent(Double.self, forKey: .rotateDegrees) | ||
| 110 | turnOverAxis = try c.decodeIfPresent(TurnOverAxis.self, forKey: .turnOverAxis) | ||
| 111 | repeatOf = try c.decodeIfPresent(Int.self, forKey: .repeatOf) | ||
| 112 | nonRigid = try c.decodeIfPresent(Bool.self, forKey: .nonRigid) ?? false | ||
| 113 | pop = try c.decodeIfPresent(PopRecord.self, forKey: .pop) | ||
| 114 | viewpoint = try c.decode(Viewpoint.self, forKey: .viewpoint) | ||
| 115 | } | ||
| 116 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixture.swift added +8
| @@ -0,0 +1,8 @@ | |||
| 1 | import Foundation | ||
| 2 | |||
| 3 | public func fixture(_ name: String) -> URL { | ||
| 4 | guard let url = Bundle.module.url(forResource: name, withExtension: nil, subdirectory: "Fixtures") else { | ||
| 5 | fatalError("no fixture named \(name)") | ||
| 6 | } | ||
| 7 | return url | ||
| 8 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/angle-200.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 200], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/angle-sign.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, -180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/bad-index.fold added +12
| @@ -0,0 +1,12 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Square", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["singleModel"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [1, 1], [0, 1]], | ||
| 7 | "edges_vertices": [[9, 1], [1, 2], [2, 3], [3, 0]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 2, 3]], | ||
| 11 | "edges_krz:firstCreasedAtStep": [null, null, null, null] | ||
| 12 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/bad-moving-face.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [7], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/cyclic-order.fold added +66
| @@ -0,0 +1,66 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter, sequential", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0], [3, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "F"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 0], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the left third over.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 8, | ||
| 28 | "movingFaces": [0], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 45 | "faceOrders": [[0, 1, 1], [1, 2, -1], [2, 0, -1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "fold", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Repeat on the right: valley fold the right third over the top.", | ||
| 50 | "axes": [ | ||
| 51 | { | ||
| 52 | "edge": 9, | ||
| 53 | "movingFaces": [2], | ||
| 54 | "startAngle": 0, | ||
| 55 | "endAngle": 180 | ||
| 56 | } | ||
| 57 | ], | ||
| 58 | "repeatOf": 1, | ||
| 59 | "viewpoint": { | ||
| 60 | "eye": [0, 0, 1], | ||
| 61 | "up": [0, 1, 0] | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | ] | ||
| 66 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/designed-model.fold added +12
| @@ -0,0 +1,12 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Square", | ||
| 4 | "file_author": "A. Designer", | ||
| 5 | "file_classes": ["singleModel"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [1, 1], [0, 1]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 0]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 2, 3]], | ||
| 11 | "edges_krz:firstCreasedAtStep": [null, null, null, null] | ||
| 12 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/face-inside-fold.fold added +93
| @@ -0,0 +1,93 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Occlusion", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [2, 0], [2.5, 0], [4, 0], [4, 4], [2.5, 4], [0, 4], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [7, 1], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 7], [1, 2, 5, 6, 7], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 2, 0], [2, 0, 0], [2.5, 0, 0], [4, 0, 0], [4, 4, 0], [2.5, 4, 0], [0, 4, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "F"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 0], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the bottom left corner up to the centre.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 8, | ||
| 28 | "movingFaces": [0], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[2, 2, 0], [2, 0, 0], [2.5, 0, 0], [1, 0, 0], [1, 4, 0], [2.5, 4, 0], [0, 4, 0], [0, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 45 | "faceOrders": [[0, 1, 1], [0, 2, -1], [1, 2, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "fold", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Valley fold the right edge over along the crease.", | ||
| 50 | "axes": [ | ||
| 51 | { | ||
| 52 | "edge": 9, | ||
| 53 | "movingFaces": [2], | ||
| 54 | "startAngle": 0, | ||
| 55 | "endAngle": 180 | ||
| 56 | } | ||
| 57 | ], | ||
| 58 | "viewpoint": { | ||
| 59 | "eye": [0, 0, 1], | ||
| 60 | "up": [0, 1, 0] | ||
| 61 | } | ||
| 62 | } | ||
| 63 | }, | ||
| 64 | { | ||
| 65 | "frame_parent": 0, | ||
| 66 | "frame_inherit": true, | ||
| 67 | "vertices_coords": [ | ||
| 68 | [0.5, 2, 0], | ||
| 69 | [0.5, 0, 0], | ||
| 70 | [0, 0, 0], | ||
| 71 | [1.5, 0, 0], | ||
| 72 | [1.5, 4, 0], | ||
| 73 | [0, 4, 0], | ||
| 74 | [2.5, 4, 0], | ||
| 75 | [2.5, 2, 0] | ||
| 76 | ], | ||
| 77 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 78 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 79 | "faceOrders": [[0, 1, 1], [0, 2, 1], [1, 2, 1]], | ||
| 80 | "krz:step": { | ||
| 81 | "kind": "turnOver", | ||
| 82 | "technique": "plain", | ||
| 83 | "instruction": "Turn the paper over from side to side.", | ||
| 84 | "axes": [], | ||
| 85 | "turnOverAxis": "vertical", | ||
| 86 | "viewpoint": { | ||
| 87 | "eye": [0, 0, 1], | ||
| 88 | "up": [0, 1, 0] | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | ] | ||
| 93 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/folded-boundary.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [90, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/half.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/interleaved-folds.fold added +122
| @@ -0,0 +1,122 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Quarters", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 2], [0, 2], [0, 1], [1, 1]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 8], [8, 5], [7, 8], [8, 3]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 8, 7], [1, 2, 3, 8], [8, 3, 4, 5], [7, 8, 5, 6]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 1, 2, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [ | ||
| 18 | [0, 0, 0], | ||
| 19 | [1, 0, 0], | ||
| 20 | [2, 0, 0], | ||
| 21 | [2, 1, 0], | ||
| 22 | [2, 2, 0], | ||
| 23 | [1, 2, 0], | ||
| 24 | [0, 2, 0], | ||
| 25 | [0, 1, 0], | ||
| 26 | [1, 1, 0] | ||
| 27 | ], | ||
| 28 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F", "F", "F"], | ||
| 29 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 30 | "faceOrders": [], | ||
| 31 | "krz:faceOrders_midFold": [[0, 1, 1], [2, 3, 1]], | ||
| 32 | "krz:step": { | ||
| 33 | "kind": "foldAndUnfold", | ||
| 34 | "technique": "plain", | ||
| 35 | "instruction": "Valley fold in half from right to left and unfold.", | ||
| 36 | "axes": [ | ||
| 37 | { | ||
| 38 | "edge": 8, | ||
| 39 | "movingFaces": [1, 2], | ||
| 40 | "startAngle": 0, | ||
| 41 | "endAngle": 0, | ||
| 42 | "peakAngle": 180 | ||
| 43 | } | ||
| 44 | ], | ||
| 45 | "viewpoint": { | ||
| 46 | "eye": [0, 0, 1], | ||
| 47 | "up": [0, 1, 0] | ||
| 48 | } | ||
| 49 | } | ||
| 50 | }, | ||
| 51 | { | ||
| 52 | "frame_parent": 0, | ||
| 53 | "frame_inherit": true, | ||
| 54 | "vertices_coords": [ | ||
| 55 | [0, 0, 0], | ||
| 56 | [1, 0, 0], | ||
| 57 | [2, 0, 0], | ||
| 58 | [2, 1, 0], | ||
| 59 | [2, 0, 0], | ||
| 60 | [1, 0, 0], | ||
| 61 | [0, 0, 0], | ||
| 62 | [0, 1, 0], | ||
| 63 | [1, 1, 0] | ||
| 64 | ], | ||
| 65 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F", "V", "V"], | ||
| 66 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 67 | "faceOrders": [[0, 3, 1], [1, 2, 1]], | ||
| 68 | "krz:step": { | ||
| 69 | "kind": "fold", | ||
| 70 | "technique": "plain", | ||
| 71 | "instruction": "Valley fold the top half down.", | ||
| 72 | "axes": [ | ||
| 73 | { | ||
| 74 | "edge": 10, | ||
| 75 | "movingFaces": [2, 3], | ||
| 76 | "startAngle": 0, | ||
| 77 | "endAngle": 180 | ||
| 78 | } | ||
| 79 | ], | ||
| 80 | "viewpoint": { | ||
| 81 | "eye": [0, 0, 1], | ||
| 82 | "up": [0, 1, 0] | ||
| 83 | } | ||
| 84 | } | ||
| 85 | }, | ||
| 86 | { | ||
| 87 | "frame_parent": 0, | ||
| 88 | "frame_inherit": true, | ||
| 89 | "vertices_coords": [ | ||
| 90 | [0, 0, 0], | ||
| 91 | [1, 0, 0], | ||
| 92 | [0, 0, 0], | ||
| 93 | [0, 1, 0], | ||
| 94 | [0, 0, 0], | ||
| 95 | [1, 0, 0], | ||
| 96 | [0, 0, 0], | ||
| 97 | [0, 1, 0], | ||
| 98 | [1, 1, 0] | ||
| 99 | ], | ||
| 100 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "M", "V", "V"], | ||
| 101 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, -180, 180, 180], | ||
| 102 | "faceOrders": [[0, 1, 1], [0, 2, -1], [0, 3, 1], [1, 2, -1], [1, 3, -1], [2, 3, -1]], | ||
| 103 | "krz:step": { | ||
| 104 | "kind": "fold", | ||
| 105 | "technique": "plain", | ||
| 106 | "instruction": "Valley fold the right half over to the left.", | ||
| 107 | "axes": [ | ||
| 108 | { | ||
| 109 | "edge": 8, | ||
| 110 | "movingFaces": [1, 2], | ||
| 111 | "startAngle": 0, | ||
| 112 | "endAngle": 180 | ||
| 113 | } | ||
| 114 | ], | ||
| 115 | "viewpoint": { | ||
| 116 | "eye": [0, 0, 1], | ||
| 117 | "up": [0, 1, 0] | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | ] | ||
| 122 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/letter-no-midfold.fold added +46
| @@ -0,0 +1,46 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "M"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, -180], | ||
| 20 | "faceOrders": [[0, 1, 1], [0, 2, -1], [1, 2, -1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the left third over and mountain fold the right third behind.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 8, | ||
| 28 | "movingFaces": [0], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | }, | ||
| 32 | { | ||
| 33 | "edge": 9, | ||
| 34 | "movingFaces": [2], | ||
| 35 | "startAngle": 0, | ||
| 36 | "endAngle": -180 | ||
| 37 | } | ||
| 38 | ], | ||
| 39 | "viewpoint": { | ||
| 40 | "eye": [0, 0, 1], | ||
| 41 | "up": [0, 1, 0] | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | ] | ||
| 46 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/letter-sequential.fold added +66
| @@ -0,0 +1,66 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter, sequential", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0], [3, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "F"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 0], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the left third over.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 8, | ||
| 28 | "movingFaces": [0], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 45 | "faceOrders": [[0, 1, 1], [0, 2, 1], [1, 2, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "fold", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Repeat on the right: valley fold the right third over the top.", | ||
| 50 | "axes": [ | ||
| 51 | { | ||
| 52 | "edge": 9, | ||
| 53 | "movingFaces": [2], | ||
| 54 | "startAngle": 0, | ||
| 55 | "endAngle": 180 | ||
| 56 | } | ||
| 57 | ], | ||
| 58 | "repeatOf": 1, | ||
| 59 | "viewpoint": { | ||
| 60 | "eye": [0, 0, 1], | ||
| 61 | "up": [0, 1, 0] | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | ] | ||
| 66 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/letter-staggered.fold added +49
| @@ -0,0 +1,49 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter, staggered", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 20 | "faceOrders": [[0, 1, 1], [0, 2, 1], [1, 2, 1]], | ||
| 21 | "krz:faceOrders_midFold": [[0, 1, 1], [0, 2, 1], [1, 2, 1]], | ||
| 22 | "krz:step": { | ||
| 23 | "kind": "fold", | ||
| 24 | "technique": "plain", | ||
| 25 | "instruction": "Valley fold the left third over, then the right third over the top.", | ||
| 26 | "axes": [ | ||
| 27 | { | ||
| 28 | "edge": 8, | ||
| 29 | "movingFaces": [0], | ||
| 30 | "startAngle": 0, | ||
| 31 | "endAngle": 180, | ||
| 32 | "window": [0, 0.5] | ||
| 33 | }, | ||
| 34 | { | ||
| 35 | "edge": 9, | ||
| 36 | "movingFaces": [2], | ||
| 37 | "startAngle": 0, | ||
| 38 | "endAngle": 180, | ||
| 39 | "window": [0.5, 1] | ||
| 40 | } | ||
| 41 | ], | ||
| 42 | "viewpoint": { | ||
| 43 | "eye": [0, 0, 1], | ||
| 44 | "up": [0, 1, 0] | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | ] | ||
| 49 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/letter-technique.fold added +47
| @@ -0,0 +1,47 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "M"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, -180], | ||
| 20 | "faceOrders": [[0, 1, 1], [0, 2, -1], [1, 2, -1]], | ||
| 21 | "krz:faceOrders_midFold": [[0, 1, 1], [0, 2, -1], [1, 2, -1]], | ||
| 22 | "krz:step": { | ||
| 23 | "kind": "fold", | ||
| 24 | "technique": "insideReverse", | ||
| 25 | "instruction": "Valley fold the left third over and mountain fold the right third behind.", | ||
| 26 | "axes": [ | ||
| 27 | { | ||
| 28 | "edge": 8, | ||
| 29 | "movingFaces": [0], | ||
| 30 | "startAngle": 0, | ||
| 31 | "endAngle": 180 | ||
| 32 | }, | ||
| 33 | { | ||
| 34 | "edge": 9, | ||
| 35 | "movingFaces": [2], | ||
| 36 | "startAngle": 0, | ||
| 37 | "endAngle": -180 | ||
| 38 | } | ||
| 39 | ], | ||
| 40 | "viewpoint": { | ||
| 41 | "eye": [0, 0, 1], | ||
| 42 | "up": [0, 1, 0] | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | ] | ||
| 47 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/letter.fold added +47
| @@ -0,0 +1,47 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "M"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, -180], | ||
| 20 | "faceOrders": [[0, 1, 1], [0, 2, -1], [1, 2, -1]], | ||
| 21 | "krz:faceOrders_midFold": [[0, 1, 1], [0, 2, -1], [1, 2, -1]], | ||
| 22 | "krz:step": { | ||
| 23 | "kind": "fold", | ||
| 24 | "technique": "plain", | ||
| 25 | "instruction": "Valley fold the left third over and mountain fold the right third behind.", | ||
| 26 | "axes": [ | ||
| 27 | { | ||
| 28 | "edge": 8, | ||
| 29 | "movingFaces": [0], | ||
| 30 | "startAngle": 0, | ||
| 31 | "endAngle": 180 | ||
| 32 | }, | ||
| 33 | { | ||
| 34 | "edge": 9, | ||
| 35 | "movingFaces": [2], | ||
| 36 | "startAngle": 0, | ||
| 37 | "endAngle": -180 | ||
| 38 | } | ||
| 39 | ], | ||
| 40 | "viewpoint": { | ||
| 41 | "eye": [0, 0, 1], | ||
| 42 | "up": [0, 1, 0] | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | ] | ||
| 47 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/mountain.fold added +40
| @@ -0,0 +1,40 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Mountain", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "M"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, -180], | ||
| 20 | "faceOrders": [[0, 1, -1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Mountain fold the right half behind.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": -180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | ] | ||
| 40 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/no-instruction.fold added +77
| @@ -0,0 +1,77 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "axes": [ | ||
| 25 | { | ||
| 26 | "edge": 6, | ||
| 27 | "movingFaces": [1], | ||
| 28 | "startAngle": 0, | ||
| 29 | "endAngle": 180 | ||
| 30 | } | ||
| 31 | ], | ||
| 32 | "viewpoint": { | ||
| 33 | "eye": [0, 0, 1], | ||
| 34 | "up": [0, 1, 0] | ||
| 35 | } | ||
| 36 | } | ||
| 37 | }, | ||
| 38 | { | ||
| 39 | "frame_parent": 0, | ||
| 40 | "frame_inherit": true, | ||
| 41 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 42 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 43 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 44 | "faceOrders": [[0, 1, 1]], | ||
| 45 | "krz:step": { | ||
| 46 | "kind": "turnOver", | ||
| 47 | "technique": "plain", | ||
| 48 | "instruction": "Turn the paper over from side to side.", | ||
| 49 | "axes": [], | ||
| 50 | "turnOverAxis": "vertical", | ||
| 51 | "viewpoint": { | ||
| 52 | "eye": [0, 0, 1], | ||
| 53 | "up": [0, 1, 0] | ||
| 54 | } | ||
| 55 | } | ||
| 56 | }, | ||
| 57 | { | ||
| 58 | "frame_parent": 0, | ||
| 59 | "frame_inherit": true, | ||
| 60 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 61 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 62 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 63 | "faceOrders": [[0, 1, 1]], | ||
| 64 | "krz:step": { | ||
| 65 | "kind": "rotate", | ||
| 66 | "technique": "plain", | ||
| 67 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 68 | "axes": [], | ||
| 69 | "rotateDegrees": 90, | ||
| 70 | "viewpoint": { | ||
| 71 | "eye": [0, 0, 1], | ||
| 72 | "up": [0, 1, 0] | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | ] | ||
| 77 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/nonconvex.fold added +12
| @@ -0,0 +1,12 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "L", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["singleModel"], | ||
| 6 | "vertices_coords": [[0, 0], [2, 0], [2, 1], [1, 1], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 2, 3, 4, 5]], | ||
| 11 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null] | ||
| 12 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/nonrigid-no-pop.fold added +79
| @@ -0,0 +1,79 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "nonRigid": true, | ||
| 34 | "viewpoint": { | ||
| 35 | "eye": [0, 0, 1], | ||
| 36 | "up": [0, 1, 0] | ||
| 37 | } | ||
| 38 | } | ||
| 39 | }, | ||
| 40 | { | ||
| 41 | "frame_parent": 0, | ||
| 42 | "frame_inherit": true, | ||
| 43 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 44 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 45 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 46 | "faceOrders": [[0, 1, 1]], | ||
| 47 | "krz:step": { | ||
| 48 | "kind": "turnOver", | ||
| 49 | "technique": "plain", | ||
| 50 | "instruction": "Turn the paper over from side to side.", | ||
| 51 | "axes": [], | ||
| 52 | "turnOverAxis": "vertical", | ||
| 53 | "viewpoint": { | ||
| 54 | "eye": [0, 0, 1], | ||
| 55 | "up": [0, 1, 0] | ||
| 56 | } | ||
| 57 | } | ||
| 58 | }, | ||
| 59 | { | ||
| 60 | "frame_parent": 0, | ||
| 61 | "frame_inherit": true, | ||
| 62 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 63 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 64 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 65 | "faceOrders": [[0, 1, 1]], | ||
| 66 | "krz:step": { | ||
| 67 | "kind": "rotate", | ||
| 68 | "technique": "plain", | ||
| 69 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 70 | "axes": [], | ||
| 71 | "rotateDegrees": 90, | ||
| 72 | "viewpoint": { | ||
| 73 | "eye": [0, 0, 1], | ||
| 74 | "up": [0, 1, 0] | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | ] | ||
| 79 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/occlusion.fold added +93
| @@ -0,0 +1,93 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Occlusion", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [2, 0], [2.5, 0], [4, 0], [4, 4], [2.5, 4], [0, 4], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [7, 1], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 7], [1, 2, 5, 6, 7], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 2, 0], [2, 0, 0], [2.5, 0, 0], [4, 0, 0], [4, 4, 0], [2.5, 4, 0], [0, 4, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "F"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 0], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the bottom left corner up to the centre.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 8, | ||
| 28 | "movingFaces": [0], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[2, 2, 0], [2, 0, 0], [2.5, 0, 0], [1, 0, 0], [1, 4, 0], [2.5, 4, 0], [0, 4, 0], [0, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 45 | "faceOrders": [[0, 1, 1], [0, 2, 1], [1, 2, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "fold", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Valley fold the right edge over along the crease.", | ||
| 50 | "axes": [ | ||
| 51 | { | ||
| 52 | "edge": 9, | ||
| 53 | "movingFaces": [2], | ||
| 54 | "startAngle": 0, | ||
| 55 | "endAngle": 180 | ||
| 56 | } | ||
| 57 | ], | ||
| 58 | "viewpoint": { | ||
| 59 | "eye": [0, 0, 1], | ||
| 60 | "up": [0, 1, 0] | ||
| 61 | } | ||
| 62 | } | ||
| 63 | }, | ||
| 64 | { | ||
| 65 | "frame_parent": 0, | ||
| 66 | "frame_inherit": true, | ||
| 67 | "vertices_coords": [ | ||
| 68 | [0.5, 2, 0], | ||
| 69 | [0.5, 0, 0], | ||
| 70 | [0, 0, 0], | ||
| 71 | [1.5, 0, 0], | ||
| 72 | [1.5, 4, 0], | ||
| 73 | [0, 4, 0], | ||
| 74 | [2.5, 4, 0], | ||
| 75 | [2.5, 2, 0] | ||
| 76 | ], | ||
| 77 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 78 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 79 | "faceOrders": [[0, 1, 1], [0, 2, 1], [1, 2, 1]], | ||
| 80 | "krz:step": { | ||
| 81 | "kind": "turnOver", | ||
| 82 | "technique": "plain", | ||
| 83 | "instruction": "Turn the paper over from side to side.", | ||
| 84 | "axes": [], | ||
| 85 | "turnOverAxis": "vertical", | ||
| 86 | "viewpoint": { | ||
| 87 | "eye": [0, 0, 1], | ||
| 88 | "up": [0, 1, 0] | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | ] | ||
| 93 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/order-against-crease.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, -1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/quarters.fold added +122
| @@ -0,0 +1,122 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Quarters", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 2], [0, 2], [0, 1], [1, 1]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 8], [8, 5], [7, 8], [8, 3]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 8, 7], [1, 2, 3, 8], [8, 3, 4, 5], [7, 8, 5, 6]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 1, 2, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [ | ||
| 18 | [0, 0, 0], | ||
| 19 | [1, 0, 0], | ||
| 20 | [2, 0, 0], | ||
| 21 | [2, 1, 0], | ||
| 22 | [2, 2, 0], | ||
| 23 | [1, 2, 0], | ||
| 24 | [0, 2, 0], | ||
| 25 | [0, 1, 0], | ||
| 26 | [1, 1, 0] | ||
| 27 | ], | ||
| 28 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F", "F", "F"], | ||
| 29 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 30 | "faceOrders": [], | ||
| 31 | "krz:faceOrders_midFold": [[0, 1, 1], [2, 3, 1]], | ||
| 32 | "krz:step": { | ||
| 33 | "kind": "foldAndUnfold", | ||
| 34 | "technique": "plain", | ||
| 35 | "instruction": "Valley fold in half from right to left and unfold.", | ||
| 36 | "axes": [ | ||
| 37 | { | ||
| 38 | "edge": 8, | ||
| 39 | "movingFaces": [1, 2], | ||
| 40 | "startAngle": 0, | ||
| 41 | "endAngle": 0, | ||
| 42 | "peakAngle": 180 | ||
| 43 | } | ||
| 44 | ], | ||
| 45 | "viewpoint": { | ||
| 46 | "eye": [0, 0, 1], | ||
| 47 | "up": [0, 1, 0] | ||
| 48 | } | ||
| 49 | } | ||
| 50 | }, | ||
| 51 | { | ||
| 52 | "frame_parent": 0, | ||
| 53 | "frame_inherit": true, | ||
| 54 | "vertices_coords": [ | ||
| 55 | [0, 0, 0], | ||
| 56 | [1, 0, 0], | ||
| 57 | [2, 0, 0], | ||
| 58 | [2, 1, 0], | ||
| 59 | [2, 0, 0], | ||
| 60 | [1, 0, 0], | ||
| 61 | [0, 0, 0], | ||
| 62 | [0, 1, 0], | ||
| 63 | [1, 1, 0] | ||
| 64 | ], | ||
| 65 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F", "V", "V"], | ||
| 66 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 67 | "faceOrders": [[0, 3, 1], [1, 2, 1]], | ||
| 68 | "krz:step": { | ||
| 69 | "kind": "fold", | ||
| 70 | "technique": "plain", | ||
| 71 | "instruction": "Valley fold the top half down.", | ||
| 72 | "axes": [ | ||
| 73 | { | ||
| 74 | "edge": 10, | ||
| 75 | "movingFaces": [2, 3], | ||
| 76 | "startAngle": 0, | ||
| 77 | "endAngle": 180 | ||
| 78 | } | ||
| 79 | ], | ||
| 80 | "viewpoint": { | ||
| 81 | "eye": [0, 0, 1], | ||
| 82 | "up": [0, 1, 0] | ||
| 83 | } | ||
| 84 | } | ||
| 85 | }, | ||
| 86 | { | ||
| 87 | "frame_parent": 0, | ||
| 88 | "frame_inherit": true, | ||
| 89 | "vertices_coords": [ | ||
| 90 | [0, 0, 0], | ||
| 91 | [1, 0, 0], | ||
| 92 | [0, 0, 0], | ||
| 93 | [0, 1, 0], | ||
| 94 | [0, 0, 0], | ||
| 95 | [1, 0, 0], | ||
| 96 | [0, 0, 0], | ||
| 97 | [0, 1, 0], | ||
| 98 | [1, 1, 0] | ||
| 99 | ], | ||
| 100 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "M", "V", "V"], | ||
| 101 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, -180, 180, 180], | ||
| 102 | "faceOrders": [[0, 1, 1], [0, 2, -1], [0, 3, 1], [1, 2, 1], [1, 3, -1], [2, 3, -1]], | ||
| 103 | "krz:step": { | ||
| 104 | "kind": "fold", | ||
| 105 | "technique": "plain", | ||
| 106 | "instruction": "Valley fold the right half over to the left.", | ||
| 107 | "axes": [ | ||
| 108 | { | ||
| 109 | "edge": 8, | ||
| 110 | "movingFaces": [1, 2], | ||
| 111 | "startAngle": 0, | ||
| 112 | "endAngle": 180 | ||
| 113 | } | ||
| 114 | ], | ||
| 115 | "viewpoint": { | ||
| 116 | "eye": [0, 0, 1], | ||
| 117 | "up": [0, 1, 0] | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | ] | ||
| 122 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/retopologised.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0], [0.5, 1, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/rotate-no-degrees.fold added +77
| @@ -0,0 +1,77 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "viewpoint": { | ||
| 71 | "eye": [0, 0, 1], | ||
| 72 | "up": [0, 1, 0] | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | ] | ||
| 77 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/square.fold added +12
| @@ -0,0 +1,12 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Square", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["singleModel"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [1, 1], [0, 1]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 0]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 2, 3]], | ||
| 11 | "edges_krz:firstCreasedAtStep": [null, null, null, null] | ||
| 12 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/still-face-reorder.fold added +66
| @@ -0,0 +1,66 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Letter, sequential", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [3, 0], [3, 3], [2, 3], [1, 3], [0, 3]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 0], [1, 6], [2, 5]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "F", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 6, 7], [1, 2, 5, 6], [2, 3, 4, 5]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, null, null, 1, 2], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0], [3, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "F"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 0], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the left third over.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 8, | ||
| 28 | "movingFaces": [0], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[2, 0, 0], [1, 0, 0], [2, 0, 0], [1, 0, 0], [1, 3, 0], [2, 3, 0], [1, 3, 0], [2, 3, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "B", "B", "V", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0, 0, 180, 180], | ||
| 45 | "faceOrders": [[0, 1, -1], [0, 2, 1], [1, 2, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "fold", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Repeat on the right: valley fold the right third over the top.", | ||
| 50 | "axes": [ | ||
| 51 | { | ||
| 52 | "edge": 9, | ||
| 53 | "movingFaces": [2], | ||
| 54 | "startAngle": 0, | ||
| 55 | "endAngle": 180 | ||
| 56 | } | ||
| 57 | ], | ||
| 58 | "repeatOf": 1, | ||
| 59 | "viewpoint": { | ||
| 60 | "eye": [0, 0, 1], | ||
| 61 | "up": [0, 1, 0] | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | ] | ||
| 66 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/stretched-step.fold added +78
| @@ -0,0 +1,78 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0.01, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | { | ||
| 40 | "frame_parent": 0, | ||
| 41 | "frame_inherit": true, | ||
| 42 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 43 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 44 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 45 | "faceOrders": [[0, 1, 1]], | ||
| 46 | "krz:step": { | ||
| 47 | "kind": "turnOver", | ||
| 48 | "technique": "plain", | ||
| 49 | "instruction": "Turn the paper over from side to side.", | ||
| 50 | "axes": [], | ||
| 51 | "turnOverAxis": "vertical", | ||
| 52 | "viewpoint": { | ||
| 53 | "eye": [0, 0, 1], | ||
| 54 | "up": [0, 1, 0] | ||
| 55 | } | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | "frame_parent": 0, | ||
| 60 | "frame_inherit": true, | ||
| 61 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 62 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 63 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 64 | "faceOrders": [[0, 1, 1]], | ||
| 65 | "krz:step": { | ||
| 66 | "kind": "rotate", | ||
| 67 | "technique": "plain", | ||
| 68 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 69 | "axes": [], | ||
| 70 | "rotateDegrees": 90, | ||
| 71 | "viewpoint": { | ||
| 72 | "eye": [0, 0, 1], | ||
| 73 | "up": [0, 1, 0] | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/topology-in-frame.fold added +79
| @@ -0,0 +1,79 @@ | |||
| 1 | { | ||
| 2 | "file_spec": 1.2, | ||
| 3 | "file_title": "Half", | ||
| 4 | "file_author": "Traditional", | ||
| 5 | "file_classes": ["diagrams"], | ||
| 6 | "vertices_coords": [[0, 0], [1, 0], [2, 0], [2, 2], [1, 2], [0, 2]], | ||
| 7 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]], | ||
| 8 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "F"], | ||
| 9 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 0], | ||
| 10 | "faces_vertices": [[0, 1, 4, 5], [1, 2, 3, 4]], | ||
| 11 | "faceOrders": [], | ||
| 12 | "edges_krz:firstCreasedAtStep": [null, null, null, null, null, null, 1], | ||
| 13 | "file_frames": [ | ||
| 14 | { | ||
| 15 | "frame_parent": 0, | ||
| 16 | "frame_inherit": true, | ||
| 17 | "vertices_coords": [[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 2, 0], [1, 2, 0], [0, 2, 0]], | ||
| 18 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 19 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 20 | "faceOrders": [[0, 1, 1]], | ||
| 21 | "krz:step": { | ||
| 22 | "kind": "fold", | ||
| 23 | "technique": "plain", | ||
| 24 | "instruction": "Valley fold the right half over to the left.", | ||
| 25 | "axes": [ | ||
| 26 | { | ||
| 27 | "edge": 6, | ||
| 28 | "movingFaces": [1], | ||
| 29 | "startAngle": 0, | ||
| 30 | "endAngle": 180 | ||
| 31 | } | ||
| 32 | ], | ||
| 33 | "viewpoint": { | ||
| 34 | "eye": [0, 0, 1], | ||
| 35 | "up": [0, 1, 0] | ||
| 36 | } | ||
| 37 | }, | ||
| 38 | "edges_vertices": [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0], [1, 4]] | ||
| 39 | }, | ||
| 40 | { | ||
| 41 | "frame_parent": 0, | ||
| 42 | "frame_inherit": true, | ||
| 43 | "vertices_coords": [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 2, 0], [0, 2, 0], [1, 2, 0]], | ||
| 44 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 45 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 46 | "faceOrders": [[0, 1, 1]], | ||
| 47 | "krz:step": { | ||
| 48 | "kind": "turnOver", | ||
| 49 | "technique": "plain", | ||
| 50 | "instruction": "Turn the paper over from side to side.", | ||
| 51 | "axes": [], | ||
| 52 | "turnOverAxis": "vertical", | ||
| 53 | "viewpoint": { | ||
| 54 | "eye": [0, 0, 1], | ||
| 55 | "up": [0, 1, 0] | ||
| 56 | } | ||
| 57 | } | ||
| 58 | }, | ||
| 59 | { | ||
| 60 | "frame_parent": 0, | ||
| 61 | "frame_inherit": true, | ||
| 62 | "vertices_coords": [[1.5, 1.5, 0], [1.5, 0.5, 0], [1.5, 1.5, 0], [-0.5, 1.5, 0], [-0.5, 0.5, 0], [-0.5, 1.5, 0]], | ||
| 63 | "edges_assignment": ["B", "B", "B", "B", "B", "B", "V"], | ||
| 64 | "edges_foldAngle": [0, 0, 0, 0, 0, 0, 180], | ||
| 65 | "faceOrders": [[0, 1, 1]], | ||
| 66 | "krz:step": { | ||
| 67 | "kind": "rotate", | ||
| 68 | "technique": "plain", | ||
| 69 | "instruction": "Rotate the paper a quarter turn counterclockwise.", | ||
| 70 | "axes": [], | ||
| 71 | "rotateDegrees": 90, | ||
| 72 | "viewpoint": { | ||
| 73 | "eye": [0, 0, 1], | ||
| 74 | "up": [0, 1, 0] | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | ] | ||
| 79 | } | ||
Packages/FoldKit/Sources/FoldKitTestSupport/Helpers.swift added +101
| @@ -0,0 +1,101 @@ | |||
| 1 | import Foundation | ||
| 2 | import FoldFormat | ||
| 3 | |||
| 4 | /// Decision 0001's case 3: a flat-foldable degree-4 vertex at the centre of a 2×2 sheet, creases | ||
| 5 | /// at 0°, 60°, 140° and 260° (sectors 60°, 80°, 120°, 100°), folded from flat to flat in one step. | ||
| 6 | /// | ||
| 7 | /// Vertices: v0 the centre, v1 (2, 1) the 0° crease's end, v2 (2, 2), v3 the 60° crease's end, | ||
| 8 | /// v4 (0, 2), v5 the 140° crease's end, v6 (0, 0), v7 the 260° crease's end, v8 (2, 0). | ||
| 9 | /// Edges: e0–e7 the boundary from v1 counterclockwise, e8 the 0° crease, e9 60°, e10 140°, | ||
| 10 | /// e11 260°. Faces: f0 the 60° sector, f1 the 80°, f2 the 120° (held), f3 the 100°. | ||
| 11 | /// | ||
| 12 | /// The 140° crease is driven linearly 0 → 180, the 0° crease follows it exactly, and the 260° and | ||
| 13 | /// 60° creases are ρ and −ρ, `ρ = 2·atan(k·tan(90°·u))`, `k = cos 70° / cos 10°`. The 260° axis | ||
| 14 | /// carries the 0° axis; the 60° crease is off the tree. With `linear` the 260° axis is a straight | ||
| 15 | /// line 0 → 180 instead of the curve, which tears the vertex. | ||
| 16 | public func degreeFourVertex(linear: Bool = false) -> FoldFile { | ||
| 17 | let c = SIMD3<Double>(1, 1, 0) | ||
| 18 | let flat: [SIMD3<Double>] = [ | ||
| 19 | c, | ||
| 20 | SIMD3(2, 1, 0), | ||
| 21 | SIMD3(2, 2, 0), | ||
| 22 | SIMD3(1 + 1 / tan(radians(60)), 2, 0), | ||
| 23 | SIMD3(0, 2, 0), | ||
| 24 | SIMD3(0, 1 + tan(radians(40)), 0), | ||
| 25 | SIMD3(0, 0, 0), | ||
| 26 | SIMD3(1 - tan(radians(10)), 0, 0), | ||
| 27 | SIMD3(2, 0, 0), | ||
| 28 | ] | ||
| 29 | |||
| 30 | // Flat-folded, each 180° fold is a reflection across its crease line in the held face's plane. | ||
| 31 | func reflect(_ p: SIMD3<Double>, across degrees: Double) -> SIMD3<Double> { | ||
| 32 | let d = p - c, a = 2 * radians(degrees) | ||
| 33 | return c + SIMD3(d.x * cos(a) + d.y * sin(a), d.x * sin(a) - d.y * cos(a), 0) | ||
| 34 | } | ||
| 35 | let folded: [SIMD3<Double>] = [ | ||
| 36 | c, | ||
| 37 | reflect(flat[1], across: 260), // f3 | ||
| 38 | reflect(reflect(flat[2], across: 0), across: 260), // f0, carried by f3 | ||
| 39 | reflect(flat[3], across: 140), // f1 | ||
| 40 | reflect(flat[4], across: 140), // f1 | ||
| 41 | flat[5], flat[6], flat[7], // f2, held | ||
| 42 | reflect(flat[8], across: 260), // f3 | ||
| 43 | ] | ||
| 44 | |||
| 45 | let creases = 8..<12 | ||
| 46 | let boundary = Array(repeating: EdgeAssignment.boundary, count: 8) | ||
| 47 | // the last sample is the end frame's angle exactly, as foldc writes it | ||
| 48 | let curve = (0..<256).map { degreeFourExactAngle(11, at: Double($0) / 256) } + [180] | ||
| 49 | let step = StepRecord( | ||
| 50 | kind: .fold, | ||
| 51 | technique: .plain, | ||
| 52 | instruction: "Fold the vertex flat.", | ||
| 53 | axes: [ | ||
| 54 | AxisRecord(edge: 10, movingFaces: [1], startAngle: 0, endAngle: 180), | ||
| 55 | AxisRecord(edge: 11, movingFaces: [3], startAngle: 0, endAngle: 180, | ||
| 56 | angles: linear ? nil : curve), | ||
| 57 | AxisRecord(edge: 8, movingFaces: [0], startAngle: 0, endAngle: 180, parent: 1), | ||
| 58 | ], | ||
| 59 | viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)) | ||
| 60 | ) | ||
| 61 | // Bottom to top along +z: f2, f1, f0, f3. f2 and f0 face +z, f1 and f3 face −z, and s is | ||
| 62 | // read on the side g's normal points toward. | ||
| 63 | let orders = [[0, 1, -1], [0, 2, 1], [0, 3, 1], [1, 2, 1], [1, 3, 1], [2, 3, 1]] | ||
| 64 | |||
| 65 | return FoldFile( | ||
| 66 | fileSpec: 1.2, | ||
| 67 | fileTitle: "Degree-4 vertex", | ||
| 68 | fileAuthor: "Traditional", | ||
| 69 | fileClasses: ["diagrams"], | ||
| 70 | edgesVertices: [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 1], | ||
| 71 | [0, 1], [0, 3], [0, 5], [0, 7]], | ||
| 72 | facesVertices: [[0, 1, 2, 3], [0, 3, 4, 5], [0, 5, 6, 7], [0, 7, 8, 1]], | ||
| 73 | edgesFirstCreasedAtStep: Array(repeating: nil, count: 8) + creases.map { _ in 1 }, | ||
| 74 | frames: [ | ||
| 75 | FoldFrame(verticesCoords: flat, | ||
| 76 | edgesAssignment: boundary + creases.map { _ in .flat }, | ||
| 77 | edgesFoldAngle: Array(repeating: 0, count: 12), | ||
| 78 | faceOrders: []), | ||
| 79 | FoldFrame(verticesCoords: folded, | ||
| 80 | edgesAssignment: boundary + [.valley, .mountain, .valley, .valley], | ||
| 81 | edgesFoldAngle: Array(repeating: 0, count: 8) + [180, -180, 180, 180], | ||
| 82 | faceOrders: orders, | ||
| 83 | faceOrdersMidFold: orders, | ||
| 84 | step: step), | ||
| 85 | ] | ||
| 86 | ) | ||
| 87 | } | ||
| 88 | |||
| 89 | /// The fold angle, in degrees, of one of `degreeFourVertex()`'s creases at `t`, from the closed form. | ||
| 90 | public func degreeFourExactAngle(_ edge: Int, at t: Double) -> Double { | ||
| 91 | let k = cos(radians(70)) / cos(radians(10)) | ||
| 92 | let rho = 2 * atan2(k * sin(radians(90 * t)), cos(radians(90 * t))) * 180 / .pi | ||
| 93 | switch edge { | ||
| 94 | case 8, 10: return 180 * t | ||
| 95 | case 11: return rho | ||
| 96 | case 9: return -rho | ||
| 97 | default: preconditionFailure("edge \(edge) is not a crease of the degree-4 vertex") | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | private func radians(_ degrees: Double) -> Double { degrees * .pi / 180 } | ||
Packages/FoldKit/Sources/Folding/FoldStep.swift added +138
| @@ -0,0 +1,138 @@ | |||
| 1 | import FoldFormat | ||
| 2 | |||
| 3 | /// One crease of a step. `origin` and `direction` (a unit vector) are in the coordinates of the | ||
| 4 | /// frame the step starts from, directed as the moving face adjacent to `edge` traverses it, so a | ||
| 5 | /// positive rotation by the right-hand rule is a valley. `parent` is the index, in the step's | ||
| 6 | /// `axes`, of the axis whose motion carries this one. `curve` is the fold angle at evenly spaced | ||
| 7 | /// points of `window`; without it the angle is linear on the window. | ||
| 8 | public struct FoldAxis: Equatable, Sendable { | ||
| 9 | public let edge: Int | ||
| 10 | public let origin: SIMD3<Double> | ||
| 11 | public let direction: SIMD3<Double> | ||
| 12 | public let movingFaces: Set<Int> | ||
| 13 | public let startAngle: Double | ||
| 14 | public let endAngle: Double | ||
| 15 | public let peakAngle: Double? | ||
| 16 | public let parent: Int? | ||
| 17 | public let window: ClosedRange<Double> | ||
| 18 | public let curve: [Double]? | ||
| 19 | |||
| 20 | public init(edge: Int, origin: SIMD3<Double>, direction: SIMD3<Double>, movingFaces: Set<Int>, | ||
| 21 | startAngle: Double, endAngle: Double, peakAngle: Double? = nil, parent: Int? = nil, | ||
| 22 | window: ClosedRange<Double> = 0...1, curve: [Double]? = nil) { | ||
| 23 | self.edge = edge | ||
| 24 | self.origin = origin | ||
| 25 | self.direction = direction | ||
| 26 | self.movingFaces = movingFaces | ||
| 27 | self.startAngle = startAngle | ||
| 28 | self.endAngle = endAngle | ||
| 29 | self.peakAngle = peakAngle | ||
| 30 | self.parent = parent | ||
| 31 | self.window = window | ||
| 32 | self.curve = curve | ||
| 33 | } | ||
| 34 | |||
| 35 | /// The fold angle in degrees at `t` of the step. Before its window the axis waits at the | ||
| 36 | /// start and after it holds at the end. | ||
| 37 | public func angle(at t: Double, kind: StepKind) -> Double { | ||
| 38 | let u = min(max((t - window.lowerBound) / (window.upperBound - window.lowerBound), 0), 1) | ||
| 39 | if let curve { return hermite(curve, at: u) } | ||
| 40 | if kind == .foldAndUnfold { | ||
| 41 | return startAngle + ((peakAngle ?? endAngle) - startAngle) * (1 - abs(2 * u - 1)) | ||
| 42 | } | ||
| 43 | return startAngle + u * (endAngle - startAngle) | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | /// The open, pop and close of a non-rigid step. `openAxes` are in the coordinates of the frame | ||
| 48 | /// the step starts from; `closeAxes` are in the coordinates of `popped`. | ||
| 49 | public struct Pop: Equatable, Sendable { | ||
| 50 | public let spine: [Int] | ||
| 51 | public let openAngle: Double | ||
| 52 | public let phases: ClosedRange<Double> | ||
| 53 | public let openAxes: [FoldAxis] | ||
| 54 | public let popped: [SIMD3<Double>] | ||
| 55 | public let closeAxes: [FoldAxis] | ||
| 56 | |||
| 57 | public init(spine: [Int], openAngle: Double, phases: ClosedRange<Double>, openAxes: [FoldAxis], | ||
| 58 | popped: [SIMD3<Double>], closeAxes: [FoldAxis]) { | ||
| 59 | self.spine = spine | ||
| 60 | self.openAngle = openAngle | ||
| 61 | self.phases = phases | ||
| 62 | self.openAxes = openAxes | ||
| 63 | self.popped = popped | ||
| 64 | self.closeAxes = closeAxes | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | /// Step `index`, taking frame `index − 1` to frame `index`. `axes` lists every parent before | ||
| 69 | /// its children. | ||
| 70 | public struct FoldStep: Equatable, Sendable { | ||
| 71 | public let index: Int | ||
| 72 | public let kind: StepKind | ||
| 73 | public let technique: StepTechnique | ||
| 74 | public let instruction: String | ||
| 75 | public let axes: [FoldAxis] | ||
| 76 | public let rotateDegrees: Double? | ||
| 77 | public let turnOverAxis: TurnOverAxis? | ||
| 78 | public let repeatOf: Int? | ||
| 79 | public let nonRigid: Bool | ||
| 80 | public let pop: Pop? | ||
| 81 | public let viewpoint: Viewpoint | ||
| 82 | |||
| 83 | public init(index: Int, kind: StepKind, technique: StepTechnique = .plain, instruction: String, | ||
| 84 | axes: [FoldAxis], rotateDegrees: Double? = nil, turnOverAxis: TurnOverAxis? = nil, | ||
| 85 | repeatOf: Int? = nil, nonRigid: Bool = false, pop: Pop? = nil, viewpoint: Viewpoint) { | ||
| 86 | self.index = index | ||
| 87 | self.kind = kind | ||
| 88 | self.technique = technique | ||
| 89 | self.instruction = instruction | ||
| 90 | self.axes = axes | ||
| 91 | self.rotateDegrees = rotateDegrees | ||
| 92 | self.turnOverAxis = turnOverAxis | ||
| 93 | self.repeatOf = repeatOf | ||
| 94 | self.nonRigid = nonRigid | ||
| 95 | self.pop = pop | ||
| 96 | self.viewpoint = viewpoint | ||
| 97 | } | ||
| 98 | |||
| 99 | public var movingFaceUnion: Set<Int> { | ||
| 100 | axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | public enum StepError: Error, Equatable, Sendable { | ||
| 105 | case axisNotAnEdge(step: Int, edge: Int) | ||
| 106 | case axisNotOnMovingSetBoundary(step: Int, edge: Int) | ||
| 107 | case movingFacesOutOfRange(step: Int) | ||
| 108 | case overlappingMovingSetsWithinAStep(step: Int) | ||
| 109 | case rotateWithoutDegrees(step: Int) | ||
| 110 | case turnOverWithoutAxis(step: Int) | ||
| 111 | case foldAndUnfoldWithoutPeak(step: Int) | ||
| 112 | case foldAndUnfoldDoesNotReturn(step: Int) | ||
| 113 | case missingInstruction(step: Int) | ||
| 114 | case axisParentCycle(step: Int) | ||
| 115 | case axisWindowOutOfRange(step: Int) | ||
| 116 | case curveTooShort(step: Int) | ||
| 117 | case nonRigidWithoutPop(step: Int) | ||
| 118 | case popPhasesOutOfOrder(step: Int) | ||
| 119 | } | ||
| 120 | |||
| 121 | /// Cubic Hermite interpolation of evenly spaced samples over 0...1, with central-difference | ||
| 122 | /// tangents inside and one-sided differences at the ends. | ||
| 123 | private func hermite(_ samples: [Double], at u: Double) -> Double { | ||
| 124 | let n = samples.count - 1 | ||
| 125 | let s = u * Double(n) | ||
| 126 | let i = min(Int(s), n - 1) | ||
| 127 | let f = s - Double(i) | ||
| 128 | func tangent(_ j: Int) -> Double { | ||
| 129 | switch j { | ||
| 130 | case 0: samples[1] - samples[0] | ||
| 131 | case n: samples[n] - samples[n - 1] | ||
| 132 | default: (samples[j + 1] - samples[j - 1]) / 2 | ||
| 133 | } | ||
| 134 | } | ||
| 135 | let f2 = f * f, f3 = f2 * f | ||
| 136 | return (2 * f3 - 3 * f2 + 1) * samples[i] + (f3 - 2 * f2 + f) * tangent(i) | ||
| 137 | + (3 * f2 - 2 * f3) * samples[i + 1] + (f3 - f2) * tangent(i + 1) | ||
| 138 | } | ||
Packages/FoldKit/Sources/Folding/StepReader.swift added +125
| @@ -0,0 +1,125 @@ | |||
| 1 | import FoldFormat | ||
| 2 | import simd | ||
| 3 | |||
| 4 | /// Reads every frame's authored `krz:step`. `result[k]` is step `k + 1`. | ||
| 5 | /// | ||
| 6 | /// - Precondition: `validate(file)` is empty. On a file it rejects, this may trap on an | ||
| 7 | /// out-of-range index. | ||
| 8 | public func readSteps(_ file: FoldFile) throws -> [FoldStep] { | ||
| 9 | let edgeFaces = edgesFaces(file) | ||
| 10 | return try file.frames.indices.dropFirst().map { n in | ||
| 11 | guard let record = file.frames[n].step else { throw FoldError.missingStepRecord(step: n) } | ||
| 12 | return try readStep(n, record, file, edgeFaces) | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | private func readStep(_ n: Int, _ record: StepRecord, _ file: FoldFile, _ edgeFaces: [[Int]]) throws -> FoldStep { | ||
| 17 | guard let instruction = record.instruction, !instruction.allSatisfy(\.isWhitespace) else { | ||
| 18 | throw StepError.missingInstruction(step: n) | ||
| 19 | } | ||
| 20 | if record.kind == .rotate && record.rotateDegrees == nil { | ||
| 21 | throw StepError.rotateWithoutDegrees(step: n) | ||
| 22 | } | ||
| 23 | if record.kind == .turnOver && record.turnOverAxis == nil { | ||
| 24 | throw StepError.turnOverWithoutAxis(step: n) | ||
| 25 | } | ||
| 26 | if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.peakAngle == nil }) { | ||
| 27 | throw StepError.foldAndUnfoldWithoutPeak(step: n) | ||
| 28 | } | ||
| 29 | if record.kind == .foldAndUnfold && record.axes.contains(where: { $0.endAngle != $0.startAngle }) { | ||
| 30 | throw StepError.foldAndUnfoldDoesNotReturn(step: n) | ||
| 31 | } | ||
| 32 | if record.nonRigid && record.pop == nil { | ||
| 33 | throw StepError.nonRigidWithoutPop(step: n) | ||
| 34 | } | ||
| 35 | |||
| 36 | let start = file.frames[n - 1].verticesCoords | ||
| 37 | let axes = try readAxes(record.axes, step: n, points: start, file, edgeFaces) | ||
| 38 | |||
| 39 | var pop: Pop? | ||
| 40 | if let p = record.pop { | ||
| 41 | guard p.phases.count == 2, 0 < p.phases[0], p.phases[0] < p.phases[1], p.phases[1] < 1 else { | ||
| 42 | throw StepError.popPhasesOutOfOrder(step: n) | ||
| 43 | } | ||
| 44 | pop = Pop(spine: p.spine, openAngle: p.openAngle, phases: p.phases[0]...p.phases[1], | ||
| 45 | openAxes: try readAxes(p.openAxes, step: n, points: start, file, edgeFaces), | ||
| 46 | popped: p.popped, | ||
| 47 | closeAxes: try readAxes(p.closeAxes, step: n, points: p.popped, file, edgeFaces)) | ||
| 48 | } | ||
| 49 | |||
| 50 | return FoldStep(index: n, kind: record.kind, technique: record.technique, instruction: instruction, | ||
| 51 | axes: axes, rotateDegrees: record.rotateDegrees, turnOverAxis: record.turnOverAxis, | ||
| 52 | repeatOf: record.repeatOf, nonRigid: record.nonRigid, pop: pop, | ||
| 53 | viewpoint: record.viewpoint) | ||
| 54 | } | ||
| 55 | |||
| 56 | /// One step's axes, directed against `points`, reordered so every parent precedes its children. | ||
| 57 | private func readAxes(_ records: [AxisRecord], step n: Int, points: [SIMD3<Double>], _ file: FoldFile, | ||
| 58 | _ edgeFaces: [[Int]]) throws -> [FoldAxis] { | ||
| 59 | var lines: [(origin: SIMD3<Double>, direction: SIMD3<Double>)] = [] | ||
| 60 | var moved = Set<Int>() | ||
| 61 | for r in records { | ||
| 62 | guard file.edgesVertices.indices.contains(r.edge) else { | ||
| 63 | throw StepError.axisNotAnEdge(step: n, edge: r.edge) | ||
| 64 | } | ||
| 65 | guard r.movingFaces.allSatisfy(file.facesVertices.indices.contains) else { | ||
| 66 | throw StepError.movingFacesOutOfRange(step: n) | ||
| 67 | } | ||
| 68 | let sides = edgeFaces[r.edge] | ||
| 69 | let movingSides = sides.filter(r.movingFaces.contains) | ||
| 70 | guard sides.count == 2, movingSides.count == 1 else { | ||
| 71 | throw StepError.axisNotOnMovingSetBoundary(step: n, edge: r.edge) | ||
| 72 | } | ||
| 73 | let window = r.window ?? [0, 1] | ||
| 74 | guard window.count == 2, 0 <= window[0], window[0] < window[1], window[1] <= 1 else { | ||
| 75 | throw StepError.axisWindowOutOfRange(step: n) | ||
| 76 | } | ||
| 77 | if let curve = r.angles, curve.count < 2 { | ||
| 78 | throw StepError.curveTooShort(step: n) | ||
| 79 | } | ||
| 80 | let faceSet = Set(r.movingFaces) | ||
| 81 | guard moved.isDisjoint(with: faceSet) else { | ||
| 82 | throw StepError.overlappingMovingSetsWithinAStep(step: n) | ||
| 83 | } | ||
| 84 | moved.formUnion(faceSet) | ||
| 85 | lines.append(traversal(of: file.edgesVertices[r.edge], by: file.facesVertices[movingSides[0]], points)) | ||
| 86 | } | ||
| 87 | |||
| 88 | // place each axis after its parent, keeping the authored order otherwise | ||
| 89 | var state = Array(repeating: 0, count: records.count) // 0 unplaced, 1 placing, 2 placed | ||
| 90 | var order: [Int] = [] | ||
| 91 | func place(_ i: Int) throws { | ||
| 92 | if state[i] == 2 { return } | ||
| 93 | guard state[i] == 0 else { throw StepError.axisParentCycle(step: n) } | ||
| 94 | state[i] = 1 | ||
| 95 | if let p = records[i].parent { | ||
| 96 | guard records.indices.contains(p) else { throw StepError.axisParentCycle(step: n) } | ||
| 97 | try place(p) | ||
| 98 | } | ||
| 99 | state[i] = 2 | ||
| 100 | order.append(i) | ||
| 101 | } | ||
| 102 | for i in records.indices { try place(i) } | ||
| 103 | |||
| 104 | var position = Array(repeating: 0, count: records.count) | ||
| 105 | for (k, i) in order.enumerated() { position[i] = k } | ||
| 106 | return order.map { i in | ||
| 107 | let r = records[i] | ||
| 108 | let window = r.window ?? [0, 1] | ||
| 109 | return FoldAxis(edge: r.edge, origin: lines[i].origin, direction: lines[i].direction, | ||
| 110 | movingFaces: Set(r.movingFaces), startAngle: r.startAngle, endAngle: r.endAngle, | ||
| 111 | peakAngle: r.peakAngle, parent: r.parent.map { position[$0] }, | ||
| 112 | window: window[0]...window[1], curve: r.angles) | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | /// The edge as `face` traverses it in `faces_vertices` order: the corner it leaves from and the | ||
| 117 | /// unit direction toward the next. | ||
| 118 | private func traversal(of edge: [Int], by face: [Int], _ points: [SIMD3<Double>]) | ||
| 119 | -> (origin: SIMD3<Double>, direction: SIMD3<Double>) { | ||
| 120 | let i = face.indices.first { i in | ||
| 121 | Set([face[i], face[(i + 1) % face.count]]) == Set(edge) | ||
| 122 | }! | ||
| 123 | let origin = points[face[i]] | ||
| 124 | return (origin, normalize(points[face[(i + 1) % face.count]] - origin)) | ||
| 125 | } | ||
Packages/FoldKit/Tests/FoldFormatTests/DecodingTests.swift added +137
| @@ -0,0 +1,137 @@ | |||
| 1 | import Foundation | ||
| 2 | import Testing | ||
| 3 | import FoldFormat | ||
| 4 | import FoldKitTestSupport | ||
| 5 | |||
| 6 | @Test func decodesUnitSquareWithFourBoundaryEdges() throws { | ||
| 7 | let f = try FoldFile(contentsOf: fixture("square.fold")) | ||
| 8 | #expect(f.frames[0].verticesCoords.count == 4) | ||
| 9 | #expect(f.frames[0].edgesAssignment.allSatisfy { $0 == .boundary }) | ||
| 10 | #expect(f.stepCount == 0) | ||
| 11 | } | ||
| 12 | |||
| 13 | @Test func topologyLivesOnTheFileNotTheFrame() throws { | ||
| 14 | let f = try FoldFile(contentsOf: fixture("square.fold")) | ||
| 15 | #expect(f.edgesVertices.count == 4) | ||
| 16 | #expect(f.facesVertices.count == 1) | ||
| 17 | } | ||
| 18 | |||
| 19 | @Test func aFrameCarryingTopologyIsRejected() { | ||
| 20 | #expect(throws: FoldError.topologyInFrame(frame: 1)) { | ||
| 21 | try FoldFile(contentsOf: fixture("topology-in-frame.fold")) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | @Test func decodesTwoElementCoordsAsZeroZ() throws { | ||
| 26 | #expect(try FoldFile(contentsOf: fixture("square.fold")).frames[0].verticesCoords | ||
| 27 | .allSatisfy { $0.z == 0 }) | ||
| 28 | } | ||
| 29 | |||
| 30 | @Test func frameNIsTheResultOfStepN() throws { | ||
| 31 | let f = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 32 | #expect(f.stepCount == 3) | ||
| 33 | #expect(f.frames[0].step == nil) | ||
| 34 | #expect(f.frames[1].step?.kind == .fold) | ||
| 35 | #expect(f.frames[2].step?.kind == .turnOver) | ||
| 36 | #expect(f.frames[3].step?.rotateDegrees == 90) | ||
| 37 | } | ||
| 38 | |||
| 39 | @Test func decodesTheStepRecordIncludingItsInstructionAndViewpoint() throws { | ||
| 40 | let step = try FoldFile(contentsOf: fixture("half.fold")).frames[1].step | ||
| 41 | #expect(step?.axes.count == 1) | ||
| 42 | #expect(step?.technique == .plain) | ||
| 43 | #expect(step?.instruction?.isEmpty == false) | ||
| 44 | #expect(step?.viewpoint == Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))) | ||
| 45 | } | ||
| 46 | |||
| 47 | @Test func decodesAnAxisTreeWithWindowsAndACurve() throws { | ||
| 48 | let json = #"{"edge": 3, "movingFaces": [2], "startAngle": 0, "endAngle": 180, "parent": 0,"# + | ||
| 49 | #" "window": [0.25, 1], "angles": [0, 90, 180]}"# | ||
| 50 | let axis = try JSONDecoder().decode(AxisRecord.self, from: Data(json.utf8)) | ||
| 51 | #expect(axis.parent == 0) | ||
| 52 | #expect(axis.window == [0.25, 1]) | ||
| 53 | #expect(axis.angles == [0, 90, 180]) | ||
| 54 | } | ||
| 55 | |||
| 56 | @Test func decodesAPopRecord() throws { | ||
| 57 | let json = #"{"spine": [0, 1], "openAngle": 30, "phases": [0.3333333333333333, 0.6666666666666666],"# + | ||
| 58 | #" "openAxes": [], "popped": [[0, 0, 0], [1, 0, 0]], "closeAxes": []}"# | ||
| 59 | let pop = try JSONDecoder().decode(PopRecord.self, from: Data(json.utf8)) | ||
| 60 | #expect(pop.openAngle == 30) | ||
| 61 | #expect(pop.popped.count == 2) | ||
| 62 | } | ||
| 63 | |||
| 64 | @Test func windowsDecodeOnAStaggeredStep() throws { | ||
| 65 | let step = try FoldFile(contentsOf: fixture("letter-staggered.fold")).frames[1].step | ||
| 66 | #expect(step?.axes.map(\.window) == [[0, 0.5], [0.5, 1]]) | ||
| 67 | #expect(step?.nonRigid == false) | ||
| 68 | } | ||
| 69 | |||
| 70 | @Test func decodesFirstCreasedAtStepWithNullForBoundaries() throws { | ||
| 71 | let f = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 72 | #expect(f.edgesFirstCreasedAtStep == [nil, nil, nil, nil, nil, nil, 1]) | ||
| 73 | } | ||
| 74 | |||
| 75 | @Test func roundTripsThroughEncodingUnchanged() throws { | ||
| 76 | let f = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 77 | let again = try JSONDecoder().decode(FoldFile.self, from: JSONEncoder().encode(f)) | ||
| 78 | #expect(again.edgesVertices == f.edgesVertices) | ||
| 79 | #expect(again.frames.map(\.verticesCoords) == f.frames.map(\.verticesCoords)) | ||
| 80 | #expect(again.frames.map(\.step) == f.frames.map(\.step)) | ||
| 81 | } | ||
| 82 | |||
| 83 | @Test func encodedFramesInheritFromTheKeyFrame() throws { | ||
| 84 | let f = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 85 | let json = try JSONSerialization.jsonObject(with: JSONEncoder().encode(f)) as! [String: Any] | ||
| 86 | let frames = json["file_frames"] as! [[String: Any]] | ||
| 87 | #expect(frames.allSatisfy { $0["frame_parent"] as? Int == 0 && $0["frame_inherit"] as? Bool == true }) | ||
| 88 | #expect(frames.allSatisfy { $0["edges_vertices"] == nil }) | ||
| 89 | } | ||
| 90 | |||
| 91 | @Test(arguments: ["C", "J"]) | ||
| 92 | func rejectsCutAndJoinAssignments(_ code: String) { | ||
| 93 | #expect(throws: FoldError.unsupportedAssignment(code)) { | ||
| 94 | try JSONDecoder().decode(EdgeAssignment.self, from: Data("\"\(code)\"".utf8)) | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | /// half.fold with `change` applied to its JSON object. | ||
| 99 | private func decodeHalf(_ change: (inout [String: Any]) -> Void) throws -> FoldFile { | ||
| 100 | var json = try JSONSerialization.jsonObject(with: Data(contentsOf: fixture("half.fold"))) as! [String: Any] | ||
| 101 | change(&json) | ||
| 102 | return try JSONDecoder().decode(FoldFile.self, from: JSONSerialization.data(withJSONObject: json)) | ||
| 103 | } | ||
| 104 | |||
| 105 | @Test func rejectsAnEdgeArrayOfTheWrongLength() { | ||
| 106 | #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_krz:firstCreasedAtStep")) { | ||
| 107 | try decodeHalf { $0["edges_krz:firstCreasedAtStep"] = [NSNull(), 1] } | ||
| 108 | } | ||
| 109 | #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_assignment")) { | ||
| 110 | try decodeHalf { $0["edges_assignment"] = ["B", "B", "B", "B", "B", "B"] } | ||
| 111 | } | ||
| 112 | #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 2, key: "edges_foldAngle")) { | ||
| 113 | try decodeHalf { json in | ||
| 114 | var frames = json["file_frames"] as! [[String: Any]] | ||
| 115 | frames[1]["edges_foldAngle"] = [0, 0, 0, 0, 0, 0, 180, 0] | ||
| 116 | json["file_frames"] = frames | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | @Test(arguments: [[0, 1], [0, 2, 1], [-1, 1, 1], [0, 1, 2], [0, 1, 1, 0]]) | ||
| 122 | func rejectsAMalformedFaceOrder(_ triple: [Int]) { | ||
| 123 | #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "faceOrders", entry: 1)) { | ||
| 124 | try decodeHalf { json in | ||
| 125 | var frames = json["file_frames"] as! [[String: Any]] | ||
| 126 | frames[0]["faceOrders"] = [[0, 1, 1], triple] | ||
| 127 | json["file_frames"] = frames | ||
| 128 | } | ||
| 129 | } | ||
| 130 | #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "krz:faceOrders_midFold", entry: 0)) { | ||
| 131 | try decodeHalf { json in | ||
| 132 | var frames = json["file_frames"] as! [[String: Any]] | ||
| 133 | frames[0]["krz:faceOrders_midFold"] = [triple] | ||
| 134 | json["file_frames"] = frames | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
Packages/FoldKit/Tests/FoldFormatTests/ValidatorTests.swift added +136
| @@ -0,0 +1,136 @@ | |||
| 1 | import Foundation | ||
| 2 | import Testing | ||
| 3 | import FoldFormat | ||
| 4 | import FoldKitTestSupport | ||
| 5 | |||
| 6 | @Test(arguments: ["square.fold", "half.fold", "mountain.fold", "letter.fold", "letter-sequential.fold", | ||
| 7 | "letter-staggered.fold", "quarters.fold", "occlusion.fold"]) | ||
| 8 | func acceptsAValidModel(_ name: String) throws { | ||
| 9 | #expect(validate(try FoldFile(contentsOf: fixture(name))).isEmpty) | ||
| 10 | } | ||
| 11 | |||
| 12 | @Test func oppositeNormalsMakeAMutualAboveValid() { | ||
| 13 | // [f, g, +1] and [g, f, +1] agree when the faces face opposite ways | ||
| 14 | let pairs = normalisedFaceOrders([[0, 1, 1], [1, 0, 1]], | ||
| 15 | normals: [SIMD3(0, 0, 1), SIMD3(0, 0, -1)]) | ||
| 16 | #expect(Set(pairs.map { [$0.below, $0.above] }) == [[0, 1]]) | ||
| 17 | } | ||
| 18 | |||
| 19 | @Test func rejectsAnOrderTheCreaseMakesImpossible() throws { | ||
| 20 | #expect(validate(try FoldFile(contentsOf: fixture("order-against-crease.fold"))) | ||
| 21 | .contains(.orderDisagreesWithCrease(frame: 1, edge: 6))) | ||
| 22 | } | ||
| 23 | |||
| 24 | @Test func rejectsInterleavedFolds() throws { | ||
| 25 | let defects = validate(try FoldFile(contentsOf: fixture("interleaved-folds.fold"))) | ||
| 26 | #expect(defects.contains { if case .interleavedFolds(frame: 3, _) = $0 { return true }; return false }) | ||
| 27 | } | ||
| 28 | |||
| 29 | @Test func rejectsAFaceInsideAFoldItCrosses() throws { | ||
| 30 | let defects = validate(try FoldFile(contentsOf: fixture("face-inside-fold.fold"))) | ||
| 31 | #expect(defects.contains(.faceInsideFold(frame: 2, face: 2, edge: 8))) | ||
| 32 | } | ||
| 33 | |||
| 34 | @Test func rejectsAFoldAndUnfoldWithoutItsPeakOrder() throws { | ||
| 35 | var file = try FoldFile(contentsOf: fixture("quarters.fold")) | ||
| 36 | file.frames[1].faceOrdersMidFold = nil | ||
| 37 | #expect(validate(file).contains(.foldAndUnfoldWithoutPeakOrder(step: 1))) | ||
| 38 | } | ||
| 39 | |||
| 40 | @Test func rejectsANonConvexFace() throws { | ||
| 41 | #expect(validate(try FoldFile(contentsOf: fixture("nonconvex.fold"))) == [.nonConvexFace(face: 0)]) | ||
| 42 | } | ||
| 43 | |||
| 44 | @Test func rejectsAFrameThatChangesTheVertexCount() throws { | ||
| 45 | let defects = validate(try FoldFile(contentsOf: fixture("retopologised.fold"))) | ||
| 46 | #expect(defects.contains(.vertexCountChanged(frame: 2))) | ||
| 47 | } | ||
| 48 | |||
| 49 | @Test func rejectsOverlappingMovingSetsWithNoMidFoldOrder() throws { | ||
| 50 | let defects = validate(try FoldFile(contentsOf: fixture("letter-no-midfold.fold"))) | ||
| 51 | #expect(defects == [.overlappingMovingSetsWithoutMidFoldOrder(step: 1)]) | ||
| 52 | } | ||
| 53 | |||
| 54 | @Test func rejectsStartAndEndOrderDisagreementOnStillFaces() throws { | ||
| 55 | let defects = validate(try FoldFile(contentsOf: fixture("still-face-reorder.fold"))) | ||
| 56 | #expect(defects.contains { | ||
| 57 | if case .startAndEndOrderDisagreeOnStillFaces(step: 2, _, _) = $0 { return true }; return false }) | ||
| 58 | } | ||
| 59 | |||
| 60 | @Test func rejectsCyclicFaceOrder() throws { | ||
| 61 | let defects = validate(try FoldFile(contentsOf: fixture("cyclic-order.fold"))) | ||
| 62 | #expect(defects.contains { if case .cyclicFaceOrder(frame: 2, _) = $0 { return true }; return false }) | ||
| 63 | } | ||
| 64 | |||
| 65 | @Test func rejectsNonIsometricTransition() throws { | ||
| 66 | let defects = validate(try FoldFile(contentsOf: fixture("stretched-step.fold"))) | ||
| 67 | #expect(defects.contains { if case .nonIsometricTransition(step: 1, _, _, _) = $0 { return true }; return false }) | ||
| 68 | } | ||
| 69 | |||
| 70 | @Test func rejectsFoldAngleOutsideRange() throws { | ||
| 71 | #expect(validate(try FoldFile(contentsOf: fixture("angle-200.fold"))) | ||
| 72 | == [.foldAngleOutOfRange(frame: 1, edge: 6, degrees: 200)]) | ||
| 73 | } | ||
| 74 | |||
| 75 | @Test func rejectsAFoldAngleWhoseSignDisagreesWithItsAssignment() throws { | ||
| 76 | #expect(validate(try FoldFile(contentsOf: fixture("angle-sign.fold"))) | ||
| 77 | == [.foldAngleSignDisagreesWithAssignment(frame: 1, edge: 6)]) | ||
| 78 | } | ||
| 79 | |||
| 80 | @Test func rejectsAFoldedBoundaryEdge() throws { | ||
| 81 | let defects = validate(try FoldFile(contentsOf: fixture("folded-boundary.fold"))) | ||
| 82 | #expect(defects.contains(.boundaryEdgeFolded(frame: 1, edge: 0))) | ||
| 83 | } | ||
| 84 | |||
| 85 | @Test func rejectsAStepWithoutAnInstruction() throws { | ||
| 86 | #expect(validate(try FoldFile(contentsOf: fixture("no-instruction.fold"))) | ||
| 87 | == [.missingInstruction(step: 1)]) | ||
| 88 | } | ||
| 89 | |||
| 90 | @Test func rejectsANonTraditionalAuthor() throws { | ||
| 91 | #expect(validate(try FoldFile(contentsOf: fixture("designed-model.fold"))) | ||
| 92 | == [.authorNotTraditional("A. Designer")]) | ||
| 93 | } | ||
| 94 | |||
| 95 | @Test func rejectsEdgeReferencingMissingVertex() throws { | ||
| 96 | #expect(validate(try FoldFile(contentsOf: fixture("bad-index.fold"))) | ||
| 97 | == [.vertexIndexOutOfRange(edge: 0)]) | ||
| 98 | } | ||
| 99 | |||
| 100 | @Test func rejectsAFoldAndUnfoldCreaseWithoutFirstCreasedAtStep() throws { | ||
| 101 | // without step 3, x = 1 is only folded and unfolded in step 1, so e8 is F and flat throughout | ||
| 102 | var file = try FoldFile(contentsOf: fixture("quarters.fold")) | ||
| 103 | file.frames.removeLast() | ||
| 104 | file.edgesFirstCreasedAtStep[8] = nil | ||
| 105 | file.edgesFirstCreasedAtStep[9] = nil | ||
| 106 | // step 1 moves f1 and f2 and holds f0 and f3, so it creases e9 (f2 | f3) as well as e8 (f0 | f1) | ||
| 107 | #expect(validate(file) == [.missingFirstCreasedAtStep(edge: 8), .missingFirstCreasedAtStep(edge: 9)]) | ||
| 108 | } | ||
| 109 | |||
| 110 | @Test func rejectsAShearedFaceWhoseEdgesKeepTheirLengths() throws { | ||
| 111 | // f1 = [v1, v2, v3, v4] in half.fold's frame 1, sheared into a parallelogram on e6 = v1–v4 | ||
| 112 | var file = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 113 | let slant = SIMD3<Double>(-0.6, 0.8, 0) | ||
| 114 | file.frames[1].verticesCoords[2] = file.frames[1].verticesCoords[1] + slant | ||
| 115 | file.frames[1].verticesCoords[3] = file.frames[1].verticesCoords[4] + slant | ||
| 116 | let defects = validate(file) | ||
| 117 | #expect(defects.contains(.faceDistorted(frame: 1, face: 1))) | ||
| 118 | #expect(!defects.contains { if case .nonIsometricTransition = $0 { return true }; return false }) | ||
| 119 | } | ||
| 120 | |||
| 121 | @Test func aReportedCycleListsEachFaceAboveTheNext() throws { | ||
| 122 | // frame 2 puts f0 above f1, f1 above f2 and f2 above f0 | ||
| 123 | let defects = validate(try FoldFile(contentsOf: fixture("cyclic-order.fold"))) | ||
| 124 | let cycle = try #require(defects.lazy.compactMap { d -> [Int]? in | ||
| 125 | if case .cyclicFaceOrder(frame: 2, let cycle) = d { return cycle }; return nil | ||
| 126 | }.first) | ||
| 127 | let rotations = [[0, 1, 2], [1, 2, 0], [2, 0, 1]] | ||
| 128 | #expect(rotations.contains(cycle)) | ||
| 129 | } | ||
| 130 | |||
| 131 | @Test func normalsAtGivenPositionsAndTheFacesOfEachEdge() throws { | ||
| 132 | let file = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 133 | #expect(faceNormals(positions: file.frames[1].verticesCoords, faces: file.facesVertices) | ||
| 134 | == [SIMD3(0, 0, 1), SIMD3(0, 0, -1)]) | ||
| 135 | #expect(edgesFaces(file) == [[0], [1], [1], [1], [0], [0], [0, 1]]) | ||
| 136 | } | ||
Packages/FoldKit/Tests/FoldingTests/StepReadingTests.swift added +139
| @@ -0,0 +1,139 @@ | |||
| 1 | import Foundation | ||
| 2 | import Testing | ||
| 3 | import FoldFormat | ||
| 4 | import Folding | ||
| 5 | import FoldKitTestSupport | ||
| 6 | |||
| 7 | @Test func stepIndicesCountFromOne() throws { | ||
| 8 | let steps = try readSteps(FoldFile(contentsOf: fixture("half.fold"))) | ||
| 9 | #expect(steps.map(\.index) == [1, 2, 3]) | ||
| 10 | } | ||
| 11 | |||
| 12 | @Test func simpleValleyFoldReadsOneAxis() throws { | ||
| 13 | let step = try readSteps(FoldFile(contentsOf: fixture("half.fold")))[0] | ||
| 14 | #expect(step.axes.count == 1) | ||
| 15 | #expect(step.axes[0].endAngle == 180) | ||
| 16 | } | ||
| 17 | |||
| 18 | @Test func letterFoldReadsTwoSimultaneousAxes() throws { | ||
| 19 | let step = try readSteps(FoldFile(contentsOf: fixture("letter.fold")))[0] | ||
| 20 | #expect(step.axes.count == 2) | ||
| 21 | } | ||
| 22 | |||
| 23 | @Test(arguments: ["half.fold", "letter.fold", "letter-sequential.fold", "quarters.fold", "occlusion.fold"]) | ||
| 24 | func movingFaceSetsWithinOneStepAreDisjoint(_ name: String) throws { | ||
| 25 | for step in try readSteps(FoldFile(contentsOf: fixture(name))) { | ||
| 26 | let union = step.axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) } | ||
| 27 | #expect(union.count == step.axes.reduce(0) { $0 + $1.movingFaces.count }) | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | @Test func turnOverStepHasNoAxesAndAnAxisToTurnAbout() throws { | ||
| 32 | let turn = try readSteps(FoldFile(contentsOf: fixture("half.fold")))[1] | ||
| 33 | #expect(turn.kind == .turnOver) | ||
| 34 | #expect(turn.axes.isEmpty) | ||
| 35 | #expect(turn.turnOverAxis == .vertical) | ||
| 36 | } | ||
| 37 | |||
| 38 | @Test func rotateStepCarriesItsDegrees() throws { | ||
| 39 | #expect(try readSteps(FoldFile(contentsOf: fixture("half.fold")))[2].rotateDegrees == 90) | ||
| 40 | } | ||
| 41 | |||
| 42 | @Test func foldAndUnfoldPeaksMidwayAndReturns() throws { | ||
| 43 | let s = try readSteps(FoldFile(contentsOf: fixture("quarters.fold")))[0] | ||
| 44 | #expect(s.kind == .foldAndUnfold) | ||
| 45 | #expect(s.axes[0].angle(at: 0.5, kind: s.kind) == 180) | ||
| 46 | #expect(s.axes[0].angle(at: 1, kind: s.kind) == 0) | ||
| 47 | } | ||
| 48 | |||
| 49 | @Test func axisIsDirectedAsTheMovingFaceTraversesIt() throws { | ||
| 50 | // f1 = [v1, v2, v3, v4] traverses e6 = [v1, v4] from v4 to v1 | ||
| 51 | let axis = try readSteps(FoldFile(contentsOf: fixture("half.fold")))[0].axes[0] | ||
| 52 | #expect(axis.origin == SIMD3(1, 2, 0)) | ||
| 53 | #expect(axis.direction == SIMD3(0, -1, 0)) | ||
| 54 | } | ||
| 55 | |||
| 56 | @Test func instructionIsReadVerbatim() throws { | ||
| 57 | let f = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 58 | #expect(try readSteps(f)[0].instruction == f.frames[1].step?.instruction) | ||
| 59 | } | ||
| 60 | |||
| 61 | @Test func aWindowedAxisWaitsAndThenHolds() throws { | ||
| 62 | let s = try readSteps(FoldFile(contentsOf: fixture("letter-staggered.fold")))[0] | ||
| 63 | #expect(s.axes[0].angle(at: 0.25, kind: s.kind) == 90) | ||
| 64 | #expect(s.axes[1].angle(at: 0.25, kind: s.kind) == 0) | ||
| 65 | #expect(s.axes[0].angle(at: 0.75, kind: s.kind) == 180) | ||
| 66 | #expect(s.axes[1].angle(at: 0.75, kind: s.kind) == 90) | ||
| 67 | } | ||
| 68 | |||
| 69 | @Test func aCurveIsInterpolatedBetweenItsSamples() throws { | ||
| 70 | let axis = try readSteps(degreeFourVertex()).first!.axes.first { $0.curve != nil }! | ||
| 71 | let t = 0.5 + 0.5 / 256 // halfway between two samples | ||
| 72 | #expect(abs(axis.angle(at: t, kind: .fold) - degreeFourExactAngle(axis.edge, at: t)) < 1e-6) | ||
| 73 | } | ||
| 74 | |||
| 75 | @Test func aNonRigidStepNeedsItsPop() { | ||
| 76 | #expect(throws: StepError.nonRigidWithoutPop(step: 1)) { | ||
| 77 | try readSteps(FoldFile(contentsOf: fixture("nonrigid-no-pop.fold"))) | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | @Test func rotateWithoutDegreesThrows() { | ||
| 82 | #expect(throws: StepError.rotateWithoutDegrees(step: 3)) { | ||
| 83 | try readSteps(FoldFile(contentsOf: fixture("rotate-no-degrees.fold"))) | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | @Test func movingFaceOutOfRangeThrows() { | ||
| 88 | #expect(throws: StepError.movingFacesOutOfRange(step: 1)) { | ||
| 89 | try readSteps(FoldFile(contentsOf: fixture("bad-moving-face.fold"))) | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | @Test func parentsComeBeforeTheirChildren() throws { | ||
| 94 | // list the carried 0° axis first; readSteps must move its parent ahead of it | ||
| 95 | var file = degreeFourVertex() | ||
| 96 | let record = file.frames[1].step! | ||
| 97 | let reordered = [record.axes[2], record.axes[0], record.axes[1]].map { a in | ||
| 98 | AxisRecord(edge: a.edge, movingFaces: a.movingFaces, startAngle: a.startAngle, | ||
| 99 | endAngle: a.endAngle, peakAngle: a.peakAngle, parent: a.parent.map { _ in 2 }, | ||
| 100 | window: a.window, angles: a.angles) | ||
| 101 | } | ||
| 102 | file.frames[1].step = StepRecord(kind: record.kind, technique: record.technique, | ||
| 103 | instruction: record.instruction, axes: reordered, | ||
| 104 | viewpoint: record.viewpoint) | ||
| 105 | let axes = try readSteps(file)[0].axes | ||
| 106 | for (i, axis) in axes.enumerated() { | ||
| 107 | if let parent = axis.parent { #expect(parent < i) } | ||
| 108 | } | ||
| 109 | let child = try #require(axes.first { $0.parent != nil }) | ||
| 110 | #expect(axes[child.parent!].edge == record.axes[record.axes[2].parent!].edge) | ||
| 111 | } | ||
| 112 | |||
| 113 | @Test func theDegreeFourVertexIsAValidModel() { | ||
| 114 | #expect(validate(degreeFourVertex()) == []) | ||
| 115 | #expect(validate(degreeFourVertex(linear: true)) == []) | ||
| 116 | } | ||
| 117 | |||
| 118 | @Test func aFoldAndUnfoldThatDoesNotReturnThrows() throws { | ||
| 119 | var file = try FoldFile(contentsOf: fixture("quarters.fold")) | ||
| 120 | let record = file.frames[1].step! | ||
| 121 | let a = record.axes[0] | ||
| 122 | file.frames[1].step = StepRecord(kind: record.kind, instruction: record.instruction, | ||
| 123 | axes: [AxisRecord(edge: a.edge, movingFaces: a.movingFaces, | ||
| 124 | startAngle: a.startAngle, endAngle: 180, | ||
| 125 | peakAngle: a.peakAngle)], | ||
| 126 | viewpoint: record.viewpoint) | ||
| 127 | #expect(throws: StepError.foldAndUnfoldDoesNotReturn(step: 1)) { try readSteps(file) } | ||
| 128 | } | ||
| 129 | |||
| 130 | @Test func aFoldAndUnfoldAxisWithoutAPeakPeaksAtItsEndAngle() { | ||
| 131 | let axis = FoldAxis(edge: 0, origin: .zero, direction: SIMD3(0, 1, 0), movingFaces: [1], | ||
| 132 | startAngle: 0, endAngle: 90) | ||
| 133 | #expect(axis.angle(at: 0.5, kind: .foldAndUnfold) == 90) | ||
| 134 | let step = FoldStep(index: 1, kind: .foldAndUnfold, instruction: "Fold and unfold.", axes: [axis], | ||
| 135 | pop: Pop(spine: [], openAngle: 30, phases: 0.25...0.75, openAxes: [], | ||
| 136 | popped: [], closeAxes: []), | ||
| 137 | viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))) | ||
| 138 | #expect(step.movingFaceUnion == [1]) | ||
| 139 | } | ||
docs/superpowers/plans/2026-10-19-fold.md +17 −5
| @@ -19,7 +19,7 @@ | |||
| 19 | - `edges_assignment` codes: `B` boundary, `M` mountain, `V` valley, `F` flat, `U` unassigned. `C` and `J` are rejected. | 19 | - `edges_assignment` codes: `B` boundary, `M` mountain, `V` valley, `F` flat, `U` unassigned. `C` and `J` are rejected. |
| 20 | - `edges_foldAngle` is degrees in −180...180, positive for valley, negative for mountain, 0 for `F`, `U` and `B`. | 20 | - `edges_foldAngle` is degrees in −180...180, positive for valley, negative for mountain, 0 for `F`, `U` and `B`. |
| 21 | - `faceOrders` triples are `[f, g, s]`; `s = +1` means `f` is above `g` **on the side `g`'s normal points toward**, so the sign is evaluated against the current view direction, not absolutely, and two triples can only be compared after normalising each by its `g`'s folded-state normal. Cycles (after normalising) are rejected. | 21 | - `faceOrders` triples are `[f, g, s]`; `s = +1` means `f` is above `g` **on the side `g`'s normal points toward**, so the sign is evaluated against the current view direction, not absolutely, and two triples can only be compared after normalising each by its `g`'s folded-state normal. Cycles (after normalising) are rejected. |
| 22 | - Faces are convex. FOLD's alternative, splitting faces with `J` edges, is not supported. A crease that ends inside the sheet (a pinch or partial crease) leaves a nonconvex face around its end; split that face with a **subdivision edge** — assigned `F`, flat in every frame, `edges_krz:firstCreasedAtStep` null, never drawn. | 22 | - Faces are convex. FOLD's alternative, splitting faces with `J` edges, is not supported. A crease that ends inside the sheet (a pinch or partial crease) leaves a nonconvex face around its end; split that face with a **subdivision edge** — assigned `F`, flat in every frame, creased by no step, `edges_krz:firstCreasedAtStep` null, never drawn. A step creases an edge when its two faces move differently in that step: each face belongs to the deepest axis whose moving set contains it, or to the fixed group, and the edge is creased when the groups differ. So every edge a fold-and-unfold crosses is creased, not only its axis edge, and needs `edges_krz:firstCreasedAtStep`. |
| 23 | - Extension keys: `edges_krz:firstCreasedAtStep` (key frame, inherited), `krz:step` (frames 1…N), `krz:faceOrders_midFold` (frames 1…N). | 23 | - Extension keys: `edges_krz:firstCreasedAtStep` (key frame, inherited), `krz:step` (frames 1…N), `krz:faceOrders_midFold` (frames 1…N). |
| 24 | - **Step numbering.** Frame 0 is the key frame (the unfolded sheet); `file_frames[i]` is frame `i + 1`. Step n, counting from 1, takes frame n − 1 to frame n and is recorded in frame n's `krz:step`. Every index called a step — in the format, the validator's defects, `FoldStep.index`, the UI — is this n. Defects about one frame's state say `frame:`; defects about a transition or a step record say `step:`; frame n and step n share the number. | 24 | - **Step numbering.** Frame 0 is the key frame (the unfolded sheet); `file_frames[i]` is frame `i + 1`. Step n, counting from 1, takes frame n − 1 to frame n and is recorded in frame n's `krz:step`. Every index called a step — in the format, the validator's defects, `FoldStep.index`, the UI — is this n. Defects about one frame's state say `frame:`; defects about a transition or a step record say `step:`; frame n and step n share the number. |
| 25 | - Every frame in `file_frames` carries `frame_parent: 0` and `frame_inherit: true`, and never overrides `edges_vertices` or `faces_vertices`. | 25 | - Every frame in `file_frames` carries `frame_parent: 0` and `frame_inherit: true`, and never overrides `edges_vertices` or `faces_vertices`. |
| @@ -268,7 +268,7 @@ git commit -m "Non-rigid steps: model, bound and the remaining coupled steps" | |||
| 268 | - `struct AxisRecord: Codable, Equatable, Sendable { let edge: Int; let movingFaces: [Int]; let startAngle: Double; let endAngle: Double; let peakAngle: Double?; let parent: Int?; let window: [Double]?; let angles: [Double]? }` — `parent` is the index, in the same step's `axes`, of the axis whose motion carries this one; `window` is `[t0, t1]`, default `[0, 1]`; `angles` is a solved curve, the fold angle at 257 evenly spaced points of the window, absent for a linear axis | 268 | - `struct AxisRecord: Codable, Equatable, Sendable { let edge: Int; let movingFaces: [Int]; let startAngle: Double; let endAngle: Double; let peakAngle: Double?; let parent: Int?; let window: [Double]?; let angles: [Double]? }` — `parent` is the index, in the same step's `axes`, of the axis whose motion carries this one; `window` is `[t0, t1]`, default `[0, 1]`; `angles` is a solved curve, the fold angle at 257 evenly spaced points of the window, absent for a linear axis |
| 269 | - `struct Viewpoint: Codable, Equatable, Sendable { let eye: SIMD3<Double>; let up: SIMD3<Double> }` — `eye` is the unit direction toward the viewer | 269 | - `struct Viewpoint: Codable, Equatable, Sendable { let eye: SIMD3<Double>; let up: SIMD3<Double> }` — `eye` is the unit direction toward the viewer |
| 270 | - `struct StepRecord: Codable, Equatable, Sendable { let kind: StepKind; let technique: StepTechnique; let instruction: String?; let axes: [AxisRecord]; let rotateDegrees: Double?; let turnOverAxis: TurnOverAxis?; let repeatOf: Int?; let nonRigid: Bool; let pop: PopRecord?; let viewpoint: Viewpoint }` — `nonRigid` decodes to `false` when absent; a non-rigid step's `axes` still list its creases with their start and end angles, for the diagram and the validator, and its motion is `pop` | 270 | - `struct StepRecord: Codable, Equatable, Sendable { let kind: StepKind; let technique: StepTechnique; let instruction: String?; let axes: [AxisRecord]; let rotateDegrees: Double?; let turnOverAxis: TurnOverAxis?; let repeatOf: Int?; let nonRigid: Bool; let pop: PopRecord?; let viewpoint: Viewpoint }` — `nonRigid` decodes to `false` when absent; a non-rigid step's `axes` still list its creases with their start and end angles, for the diagram and the validator, and its motion is `pop` |
| 271 | - `struct PopRecord: Codable, Equatable, Sendable { let spine: [Int]; let openAngle: Double; let phases: [Double]; let openAxes: [AxisRecord]; let popped: [SIMD3<Double>]; let closeAxes: [AxisRecord] }` — decision 0002's open, pop, close: `spine` is the flap's spine edges; `openAngle` the spine's fold angle when open (default 30); `phases` the two boundaries on *t* (default `[1/3, 2/3]`); `openAxes` open the flap on `[0, phases[0]]`; `popped` is every vertex's position after the pop; `closeAxes`, relative to `popped`, close it on `[phases[1], 1]` | 271 | - `struct PopRecord: Codable, Equatable, Sendable { let spine: [Int]; let openAngle: Double; let phases: [Double]; let openAxes: [AxisRecord]; let popped: [SIMD3<Double>]; let closeAxes: [AxisRecord] }` — decision 0002's open, pop, close: `spine` is the flap's spine edges; `openAngle` the spine's fold angle when open (default 30); `phases` the two boundaries on *t* (default `[1/3, 2/3]`); `openAxes` open the flap on `[0, phases[0]]`; `popped` is every vertex's position after the pop; `closeAxes`, relative to `popped`, close it on `[phases[1], 1]`. Axis windows are on the step's whole *t*, as for any axis, so `foldc` writes each opening axis's window inside `[0, phases[0]]` and each closing axis's inside `[phases[1], 1]` |
| 272 | - `struct FoldFrame: Codable, Sendable { var verticesCoords: [SIMD3<Double>]; var edgesAssignment: [EdgeAssignment]; var edgesFoldAngle: [Double]; var faceOrders: [[Int]]; var faceOrdersMidFold: [[Int]]?; var step: StepRecord? }` | 272 | - `struct FoldFrame: Codable, Sendable { var verticesCoords: [SIMD3<Double>]; var edgesAssignment: [EdgeAssignment]; var edgesFoldAngle: [Double]; var faceOrders: [[Int]]; var faceOrdersMidFold: [[Int]]?; var step: StepRecord? }` |
| 273 | - `struct FoldFile: Codable, Sendable { var fileSpec: Double; var fileCreator: String?; var fileTitle: String?; var fileAuthor: String?; var fileClasses: [String]; var edgesVertices: [[Int]]; var facesVertices: [[Int]]; var edgesFirstCreasedAtStep: [Int?]; var frames: [FoldFrame] }` — `frames[0]` is the key frame, `frames[n]` is frame n | 273 | - `struct FoldFile: Codable, Sendable { var fileSpec: Double; var fileCreator: String?; var fileTitle: String?; var fileAuthor: String?; var fileClasses: [String]; var edgesVertices: [[Int]]; var facesVertices: [[Int]]; var edgesFirstCreasedAtStep: [Int?]; var frames: [FoldFrame] }` — `frames[0]` is the key frame, `frames[n]` is frame n |
| 274 | - `var (FoldFile).stepCount: Int { get }` — `frames.count − 1` | 274 | - `var (FoldFile).stepCount: Int { get }` — `frames.count − 1` |
| @@ -945,7 +945,7 @@ Expected: FAIL. | |||
| 945 | 945 | ||
| 946 | - [ ] **Step 3: Implement `LayerOrder`, `CoplanarCluster` and `clusters`** | 946 | - [ ] **Step 3: Implement `LayerOrder`, `CoplanarCluster` and `clusters`** |
| 947 | 947 | ||
| 948 | `init` normalises each source's triples with that source's normals and throws on a cycle within it, then keeps the raw triples. `agreesWith` compares pairs in the rigid-motion-invariant form Task 2's still-face rule uses. `clusters` links faces by the rule above, then topologically sorts each cluster against `viewDirection`, resolving each triple with `g`'s normal at `positions`, stably on unordered pairs. `stackDepth` overlays the projected outlines of each group of coincident faces, takes one interior point of each cell of that arrangement (its centroid), counts the faces whose open interior contains it, and returns the maximum. Counting at the arrangement's vertices instead would count every face meeting at a crease vertex, so a flat sheet would score its vertex degree. | 948 | `init` normalises each source's triples with that source's normals (`faceNormals(positions:faces:)` from FoldFormat) and throws on a cycle within it, the cycle listed so that each face lies above the next, as the validator reports it, then keeps the raw triples. `agreesWith` compares pairs in the rigid-motion-invariant form Task 2's still-face rule uses. `clusters` links faces by the rule above, then topologically sorts each cluster against `viewDirection`, resolving each triple with `g`'s normal at `positions`, stably on unordered pairs. `stackDepth` overlays the projected outlines of each group of coincident faces, takes one interior point of each cell of that arrangement (its centroid), counts the faces whose open interior contains it, and returns the maximum. Counting at the arrangement's vertices instead would count every face meeting at a crease vertex, so a flat sheet would score its vertex degree. |
| 949 | 949 | ||
| 950 | - [ ] **Step 4: Run tests to verify they pass** | 950 | - [ ] **Step 4: Run tests to verify they pass** |
| 951 | 951 | ||
| @@ -1164,7 +1164,7 @@ Expected: FAIL. | |||
| 1164 | 1164 | ||
| 1165 | - [ ] **Step 3: Implement `FoldScript`, `compile`, `guessFaceOrders`, `applyOrderOverrides` and the `foldc` entry point** | 1165 | - [ ] **Step 3: Implement `FoldScript`, `compile`, `guessFaceOrders`, `applyOrderOverrides` and the `foldc` entry point** |
| 1166 | 1166 | ||
| 1167 | Resolve each `CreaseRef` to an edge by nearest-endpoint match in crease-pattern coordinates with a 1e-6 tolerance. Resolve `movingSide` to a face by point-in-polygon in the crease pattern, then grow it to its connected component across edges that are not folded this step; the axis edge must lie on that set's boundary. A crease through several layers: every edge that lies on the axis line in the start frame and bounds the moving set is part of the fold, and each gets its own resulting fold angle and assignment. Apply each step's rotations in order to produce the next frame. Guess layer order by placing the moving set on the side of the existing stack it arrived from, its own layers reversed because it has turned over; this is right for plain valley and mountain folds and wrong for anything tucked between layers, which the script corrects with overrides — and the validator's consistency rules (Task 2) report the cases it gets wrong. For a `foldAndUnfold`, compute the order at the peak the same way and write it to `krz:faceOrders_midFold`. After the last step, every crease folded in the last frame must carry the crease pattern's assignment; a mismatch means the script folded something the wrong way. Write JSON with sorted keys so regenerated files diff cleanly. | 1167 | Every face of the crease pattern must be counterclockwise with the sheet's top side up; rewind clockwise faces, or reject a pattern exported y-down, so that valley and mountain keep their meaning. Every solved curve has exactly 257 samples. Resolve each `CreaseRef` to an edge by nearest-endpoint match in crease-pattern coordinates with a 1e-6 tolerance. Resolve `movingSide` to a face by point-in-polygon in the crease pattern, then grow it to its connected component across edges that are not folded this step; the axis edge must lie on that set's boundary. A crease through several layers: every edge that lies on the axis line in the start frame and bounds the moving set is part of the fold, and each gets its own resulting fold angle and assignment. Apply each step's rotations in order to produce the next frame. Guess layer order by placing the moving set on the side of the existing stack it arrived from, its own layers reversed because it has turned over; this is right for plain valley and mountain folds and wrong for anything tucked between layers, which the script corrects with overrides — and the validator's consistency rules (Task 2) report the cases it gets wrong. For a `foldAndUnfold`, compute the order at the peak the same way and write it to `krz:faceOrders_midFold`. After the last step, every crease folded in the last frame must carry the crease pattern's assignment; a mismatch means the script folded something the wrong way. Write JSON with sorted keys so regenerated files diff cleanly. |
| 1168 | 1168 | ||
| 1169 | - [ ] **Step 4: Run tests to verify they pass** | 1169 | - [ ] **Step 4: Run tests to verify they pass** |
| 1170 | 1170 | ||
| @@ -1215,6 +1215,7 @@ Decision 0002: an inside reverse fold has no rigid motion with the flap closed. | |||
| 1215 | - builds `openAxes` (the spine as one linear axis); | 1215 | - builds `openAxes` (the spine as one linear axis); |
| 1216 | - builds `popped` (the reverse-fold branch with the spine held at `openAngle`, solved as in Task 7); | 1216 | - builds `popped` (the reverse-fold branch with the spine held at `openAngle`, solved as in Task 7); |
| 1217 | - builds `closeAxes` (from `popped` to the end frame, with the spine driven and the rest solved); | 1217 | - builds `closeAxes` (from `popped` to the end frame, with the spine driven and the rest solved); |
| 1218 | - checks `popped` has one position per vertex; | ||
| 1218 | - measures the pop's strain on the step's render mesh, and fails the step above **10%**. | 1219 | - measures the pop's strain on the step's render mesh, and fails the step above **10%**. |
| 1219 | 1220 | ||
| 1220 | Decision 0002 measured 7.9% at 30° and 29% at 60° on this flap, at a mesh spacing of 1/32 of its longer side. Only the inside reverse fold was measured. Outside reverse folds and sinks use the same model until a model of theirs fails the bound; if one does, stop for a decision. | 1221 | Decision 0002 measured 7.9% at 30° and 29% at 60° on this flap, at a mesh spacing of 1/32 of its longer side. Only the inside reverse fold was measured. Outside reverse folds and sinks use the same model until a model of theirs fails the bound; if one does, stop for a decision. |
| @@ -1336,7 +1337,7 @@ git commit -m "Non-rigid steps: open, pop, close" | |||
| 1336 | - Consumes: Tasks 1–7b. | 1337 | - Consumes: Tasks 1–7b. |
| 1337 | - Produces: | 1338 | - Produces: |
| 1338 | - `enum ModelLibrary { static func all() throws -> [FoldFile]; static func names() throws -> [String]; static func model(named: String) throws -> FoldFile }` | 1339 | - `enum ModelLibrary { static func all() throws -> [FoldFile]; static func names() throws -> [String]; static func model(named: String) throws -> FoldFile }` |
| 1339 | - `foldlint <path>...` — exits 0 when clean, 1 with one defect per line | 1340 | - `foldlint <path>...` — runs `validate` and then `readSteps`, which rejects step records the validator does not check; exits 0 when clean, 1 with one defect per line |
| 1340 | 1341 | ||
| 1341 | **The cup is the model authored here, not the crane.** Six steps — five folds of one axis each and a turn-over — no reverse folds, at most six layers where the corner flaps cross. It exercises script, compile, validate, interpolate and the catalogue gate end to end without exercising coupled steps or deep stacks at the same time. The version authored folds the top flaps down — the front one, then after the turn-over the back one — rather than tucking them into the pocket, and it ends flat. Step 1's instruction starts "Valley fold"; Task 14's accessibility test reads it. | 1342 | **The cup is the model authored here, not the crane.** Six steps — five folds of one axis each and a turn-over — no reverse folds, at most six layers where the corner flaps cross. It exercises script, compile, validate, interpolate and the catalogue gate end to end without exercising coupled steps or deep stacks at the same time. The version authored folds the top flaps down — the front one, then after the turn-over the back one — rather than tucking them into the pocket, and it ends flat. Step 1's instruction starts "Valley fold"; Task 14's accessibility test reads it. |
| 1342 | 1343 | ||
| @@ -1345,6 +1346,17 @@ Its crease pattern and `.foldscript` are committed next to the generated `.fold` | |||
| 1345 | - [ ] **Step 1: Write the failing tests** | 1346 | - [ ] **Step 1: Write the failing tests** |
| 1346 | 1347 | ||
| 1347 | ```swift | 1348 | ```swift |
| 1349 | @Test func everyShippedStepEndsOnItsEndFrame() throws { | ||
| 1350 | // the step records and the frames must agree: interpolating a step to t = 1 reproduces frame n | ||
| 1351 | for file in try ModelLibrary.all() { | ||
| 1352 | for step in try readSteps(file) { | ||
| 1353 | let i = FoldInterpolator(file: file, step: step) | ||
| 1354 | #expect(zip(i.positions(at: 1), file.frames[step.index].verticesCoords) | ||
| 1355 | .allSatisfy { simd_distance($0, $1) < 1e-6 * sheetSize(file) }, "\(file.fileTitle ?? "?") step \(step.index)") | ||
| 1356 | } | ||
| 1357 | } | ||
| 1358 | } | ||
| 1359 | |||
| 1348 | @Test func everyShippedModelValidates() throws { | 1360 | @Test func everyShippedModelValidates() throws { |
| 1349 | for file in try ModelLibrary.all() { #expect(validate(file).isEmpty, "\(file.fileTitle ?? "?")") } | 1361 | for file in try ModelLibrary.all() { #expect(validate(file).isEmpty, "\(file.fileTitle ?? "?")") } |
| 1350 | } | 1362 | } |
docs/superpowers/specs/2026-10-19-fold-design.md +1 −1
| @@ -141,7 +141,7 @@ in scope needs one. It recommends `J` edges to split faces that overlap in | |||
| 141 | several regions or are nonconvex; `J` is rejected and faces must be convex. | 141 | several regions or are nonconvex; `J` is rejected and faces must be convex. |
| 142 | A crease that ends inside the sheet (a pinch) leaves a nonconvex face around | 142 | A crease that ends inside the sheet (a pinch) leaves a nonconvex face around |
| 143 | its end, which is split by a **subdivision edge**: assigned `F`, flat in | 143 | its end, which is split by a **subdivision edge**: assigned `F`, flat in |
| 144 | every frame, with no `edges_krz:firstCreasedAtStep`, and never drawn. | 144 | every frame, creased by no step (no step moves its two faces differently), with no `edges_krz:firstCreasedAtStep`, and never drawn. |
| 145 | 145 | ||
| 146 | ### Frames and step numbering | 146 | ### Frames and step numbering |
| 147 | 147 | ||