| @@ -4,7 +4,7 @@ |
| 4 | 4 | |
| 5 | 5 | **Goal:** An iOS app that folds traditional origami models under your finger and generates their Yoshizawa–Randlett diagrams from the same data, so the diagram and the model cannot disagree. |
| 6 | 6 | |
| 7 | | **Architecture:** A model is one FOLD file whose `file_frames` are its steps, sharing one topology: frame 0 is the sheet already subdivided by every crease the sequence will use. Each frame carries an authored `krz:step` naming its kind, technique, instruction, axes, moving sets and viewpoint. Intermediate geometry is rigid rotation of the moving facets on one parameter the finger drives; how coupled multi-axis steps move is decided by measurement in Task 0 before anything encodes it. Draw order is hybrid — the depth buffer between clusters of near-coplanar overlapping faces, the active `faceOrders` within each. The frames are produced by a macOS fold-script compiler that ships with the repo but not in the app. |
| 7 | **Architecture:** A model is one FOLD file whose `file_frames` are its steps, sharing one topology: frame 0 is the sheet already subdivided by every crease the sequence will use. Each frame carries an authored `krz:step` naming its kind, technique, instruction, axes, moving sets and viewpoint. Intermediate geometry is rigid rotation of the moving facets on one parameter the finger drives: a step's axes form a tree, each with its own window on that parameter and, where faces form loops, an angle curve `foldc` solves (decision 0001). Reverse folds, sinks and inversions have no rigid path and are flagged non-rigid, moving by the model Task 0b chooses. Draw order is hybrid — the depth buffer between clusters of near-coplanar overlapping faces, the active `faceOrders` within each. The frames are produced by a macOS fold-script compiler that ships with the repo but not in the app. |
| 8 | 8 | |
| 9 | 9 | **Tech Stack:** Swift 6, iOS 26, SwiftUI, Metal, `simd`, Swift Testing, XcodeGen. No third-party runtime dependencies in the app. |
| 10 | 10 | |
| @@ -26,7 +26,8 @@ |
| 26 | 26 | - **One topology across every frame.** Enforced by the decoder and the validator. |
| 27 | 27 | - **Steps are authored, never derived from a frame diff.** That includes `instruction`, which is the accessibility label. |
| 28 | 28 | - **`turnOver` and `rotate` change the coordinates.** Frame n is frame n − 1 rotated about its bounding-box centre; the camera does not move. |
| 29 | | - Paper is rigid flat facets hinged at straight creases. No bending, stiffness, self-collision response or crumpling. |
| 29 | - Paper is rigid flat facets hinged at straight creases, except during a step flagged `nonRigid`. No stiffness, self-collision response or crumpling. |
| 30 | - **Interpolation (decision 0001, option A).** A step's axes form a tree: an axis may name a parent axis whose motion carries it. Each axis has a window on *t* (default 0…1) and either a linear angle or a solved curve of 257 samples, rebuilt by cubic Hermite interpolation. Steps whose technique has no rigid path — inside and outside reverse folds, sinks, inversions — carry `nonRigid: true`; the isometry gate does not apply to them and Task 0b's strain bound does. |
| 30 | 31 | - Layer order is authored, never computed. Corrections to the compiler's guess live in the fold script. |
| 31 | 32 | - v1 ships **four** traditional models: the cup, the crane, the sampan and the box (the masu). Every `file_author` is exactly `"Traditional"`, enforced by the validator. |
| 32 | 33 | - Every shipped model is produced by `foldc` from a committed crease pattern and fold script, and recompiling them reproduces the shipped file (structurally, coordinates within 1e-12). Hand-edited `.fold` files are not shipped. |
| @@ -38,7 +39,7 @@ |
| 38 | 39 | |
| 39 | 40 | ## Review Focus |
| 40 | 41 | |
| 41 | | 1. **A step with several simultaneous axes.** A petal fold and a reverse fold move several facet sets about several creases at once on one shared parameter, and their faces form closed loops around interior vertices. The interpolation model for these is measured in Task 0 and implemented in Task 4; the axis records are read in Task 3. |
| 42 | 1. **A step with several simultaneous axes.** A petal fold moves several facet sets about several creases at once on one shared parameter; where faces form closed loops around interior vertices the angles are coupled nonlinearly, and some axes are carried by others. Decision 0001 settled the model; the axis tree, windows and curves are read in Task 3, applied in Task 4 and solved in Task 7. Steps with no rigid path are Task 0b's. |
| 42 | 43 | 2. **Two facets that are both moving.** The end frame's ordering does not order two simultaneously moving sets against each other during the motion. Such a step must carry `krz:faceOrders_midFold` or be rejected. Test in Task 2. Task 0 also reports whether this rule has a consumer the clusters do not already cover. |
| 43 | 44 | 3. **Coplanar stacked facets, and orbiting underneath.** The depth buffer cannot order coplanar or nearly coplanar facets, and `faceOrders`'s `s` sign reverses when the view crosses `g`'s plane. Stacked facets must not z-fight at any orbit angle or at the start and end of a fold, and the stack must not invert when you orbit under it. Test in Tasks 5 and 10. |
| 44 | 45 | 4. **A degenerate drag direction.** When the moving facet's centroid travels nearly along the view direction the drag projection approaches zero and the fold becomes uncontrollable. Test in Task 12. |
| @@ -58,6 +59,7 @@ Every fixture except where noted has `file_author: "Traditional"`, an `instructi |
| 58 | 59 | | `mountain.fold` | as `half.fold` | 1: mountain fold f1 behind f0 about e6, 0 → −180 | |
| 59 | 60 | | `letter.fold` | 3×3; v0 (0,0), v1 (1,0), v2 (2,0), v3 (3,0), v4 (3,3), v5 (2,3), v6 (1,3), v7 (0,3); e0–e7 the boundary, e8 = v1–v6 (x = 1, valley), e9 = v2–v5 (x = 2, mountain); f0 = [v0,v1,v6,v7], f1 = [v1,v2,v5,v6], f2 = [v2,v3,v4,v5] | 1: one step, two axes: f0 valley folds in front of f1, f2 mountain folds behind it. The sets overlap in projection on opposite sides of f1, so frame 1 carries `krz:faceOrders_midFold` | |
| 60 | 61 | | `letter-sequential.fold` | as `letter.fold`, both creases valley | 1: f0 over f1. 2: f2 over both, `repeatOf: 1` | |
| 62 | | `letter-staggered.fold` | as `letter.fold`, both creases valley | 1: one step, two axes: f0 over f1 on window 0…0.5, then f2 over both on 0.5…1. The sets overlap in projection, so frame 1 carries `krz:faceOrders_midFold`, equal to its `faceOrders`. Frame 1 is `letter-sequential.fold`'s frame 2 | |
| 61 | 63 | | `quarters.fold` | 2×2; v0 (0,0), v1 (1,0), v2 (2,0), v3 (2,1), v4 (2,2), v5 (1,2), v6 (0,2), v7 (0,1), v8 (1,1); e0–e7 the boundary, e8 = v1–v8, e9 = v8–v5 (x = 1), e10 = v7–v8, e11 = v8–v3 (y = 1); f0 = [v0,v1,v8,v7], f1 = [v1,v2,v3,v8], f2 = [v8,v3,v4,v5], f3 = [v7,v8,v5,v6] | 1: `foldAndUnfold` on e8, moving f1 and f2, peak 180; frame 1 carries `krz:faceOrders_midFold` with the order at the peak (f1 on f0, f2 on f3). 2: valley fold the top half (f2, f3) down about e10. 3: valley fold the right half of both layers (f1, f2) over about e8 — e8 and e9 are now coincident; e9 is on the flipped layer and ends at −180, `M` | |
| 62 | 64 | | `occlusion.fold` | 4×4; v0 (0,0), v1 (2,0), v2 (2.5,0), v3 (4,0), v4 (4,4), v5 (2.5,4), v6 (0,4), v7 (0,2); e0–e7 the boundary, e8 = v7–v1 (diagonal), e9 = v2–v5; f0 = [v0,v1,v7], f1 = [v1,v2,v5,v6,v7], f2 = [v2,v3,v4,v5] | 1: valley fold the corner f0 up about e8 (v0 lands on (2,2)). 2: valley fold the strip f2 over about e9 (it lands on x ∈ [1, 2.5], covering part of f0). 3: `turnOver`, vertical, so that step 3's diagram draws frame 2 | |
| 63 | 65 | |
| @@ -98,6 +100,7 @@ Done once, before Task 0. |
| 98 | 100 | | Batch | Tasks | Branch | |
| 99 | 101 | |---|---|---| |
| 100 | 102 | | A | 0 — interpolation spike | `spike-interpolation` | |
| 103 | | A2 | 0b — non-rigid steps, petal fold, sampan, masu | `spike-non-rigid` | |
| 101 | 104 | | B | 1–3 — decoding, validator, steps | `format-and-steps` | |
| 102 | 105 | | C | 4–6 — interpolation, layer order, interpenetration | `folding-engine` | |
| 103 | 106 | | D | 7–8 — `foldc`, `foldlint`, the cup | `compiler-and-cup` | |
| @@ -105,7 +108,7 @@ Done once, before Task 0. |
| 105 | 108 | | F | 11–14 — the app | `app` | |
| 106 | 109 | | G | 15 — crane, sampan, box | `models` | |
| 107 | 110 | |
| 108 | | Batch A merges alone, since Task 0 can stop the plan. Each batch commits task by task on its branch, ends with `gitbay mr create --source <branch> --target main`, merges when `swift test --package-path Packages/FoldKit` (and, from batch F, the `xcodebuild` test) is green, and the branch is then deleted locally and remotely. Commit messages end with `Ref #N` for the batch's issue; the batch's last commit uses `Closes #N`. |
| 111 | Batches A and A2 merge alone, since Tasks 0 and 0b can stop the plan. Each batch commits task by task on its branch, ends with `gitbay mr create --source <branch> --target main`, merges when `swift test --package-path Packages/FoldKit` (and, from batch F, the `xcodebuild` test) is green, and the branch is then deleted locally and remotely. Commit messages end with `Ref #N` for the batch's issue; the batch's last commit uses `Closes #N`. |
| 109 | 112 | 4. If `xcrun metal --version` fails, run `xcodebuild -downloadComponent MetalToolchain`. SwiftPM invokes `metal` for the `.metal` file in Task 10; on 2026-10-19 this machine had no Metal toolchain. |
| 110 | 113 | |
| 111 | 114 | --- |
| @@ -150,18 +153,69 @@ Done once, before Task 0. |
| 150 | 153 | |
| 151 | 154 | This is a measurement, not TDD. |
| 152 | 155 | |
| 153 | | - [ ] **Step 1: Build the spike package and the five cases** |
| 154 | | - [ ] **Step 2: Implement M1 and M2, and M3 and M4 as far as case 4 needs them** |
| 155 | | - [ ] **Step 3: Run and record** |
| 156 | - [x] **Step 1: Build the spike package and the five cases** |
| 157 | - [x] **Step 2: Implement M1 and M2, and M3 and M4 as far as case 4 needs them** |
| 158 | - [x] **Step 3: Run and record** |
| 156 | 159 | |
| 157 | 160 | Run: `swift run --package-path spikes/interpolation` |
| 158 | 161 | |
| 162 | - [x] **Step 4: Write the decision record and apply the exit criteria** |
| 163 | - [x] **Step 5: Commit** |
| 164 | |
| 165 | ```bash |
| 166 | git add spikes docs |
| 167 | git commit -m "Interpolation model spike and decision record" |
| 168 | ``` |
| 169 | |
| 170 | --- |
| 171 | |
| 172 | ### Task 0b: Non-rigid steps and the remaining coupled steps |
| 173 | |
| 174 | **Files:** |
| 175 | - Modify: `spikes/interpolation` — cases 6–9 |
| 176 | - Create: `docs/decisions/0002-non-rigid-steps.md` |
| 177 | - Modify at the end: the design doc's *Interpolation* section, this plan's Tasks 1, 3, 4, 7, 8 and 15, and `CLAUDE.md` |
| 178 | |
| 179 | **Why.** Decision 0001 found no rigid motion that makes an inside reverse fold with the flap closed, and chose option A: such steps are flagged `nonRigid` and move by a designed non-rigid motion held to a stated strain bound. This task chooses that motion and the bound, and measures the coupled steps 0001 left open. Task 1 encodes the result, so it comes first. |
| 180 | |
| 181 | **Cases**, continuing 0001's numbering: |
| 182 | |
| 183 | 6. **Reverse fold with bending**, on case 4's flap. Candidates: |
| 184 | - **virtual creases** — extra edges in the flap's layers near the vertex (a fan from it), never drawn, flat in every frame and free to fold during the step. If some number of them gives a rigid path that keeps the flap mostly closed, the step is rigid on a finer mesh: zero gap, and the bend shows as their largest angle. They are subdivision edges in the format, which already allows them. |
| 185 | - **pose interpolation** — per-face transforms through solved intermediate poses (the flap opened to φ, the tip swung through), interpolated between them; strain is the gap. |
| 186 | |
| 187 | Measure for each: the largest gap, the largest opening of the flap, the largest virtual-crease angle, and collisions, across φ and the number of virtual creases. 0001 measured naive schedules at 0.24–1.6 of the flap's height; the question is how far a designed motion gets below that. |
| 188 | 7. **Petal fold, verified.** A bird-base crease pattern with its mountain–valley assignment from a citable source — Lang, *Origami Design Secrets*, or the base folded in Oriedita and exported — recorded with its source. Check it is flat-foldable at every vertex (Kawasaki, and Maekawa against the assignment). Then measure the petal fold as 0001's case 5 did, and check that the inner layers' corners stay down. |
| 189 | 8. **Sampan, turning inside out.** The crease pattern of the sequence Task 15 will author, with its source. Does any rigid path exist? Rigid (M2), or flagged. |
| 190 | 9. **Masu, raising the walls.** The traditional masu's crease pattern, walls ending at 90°. Does M2 find a rigid path? Rigid, or flagged. |
| 191 | |
| 192 | **Exit criteria:** |
| 193 | |
| 194 | - `docs/decisions/0002-non-rigid-steps.md` holds: the non-rigid model and the data it adds to `krz:step`; the strain metric and the bound the app promises; and, for every v1 step that is not a single independent fold, whether it is rigid (M2) or flagged, with the case that stands in for it. |
| 195 | - In the same merge request, apply the result: |
| 196 | - the design doc's *Interpolation* section; |
| 197 | - Task 1 (the non-rigid step's data) and Task 3; |
| 198 | - Task 4 (the non-rigid path and its tests); |
| 199 | - Task 7 (how `foldc` produces a non-rigid step); |
| 200 | - Task 8 (the bound's test for flagged steps); |
| 201 | - Task 15 (which steps are flagged); |
| 202 | - `CLAUDE.md`. |
| 203 | - If a v1 step is neither rigid nor within a bound worth promising: **stop**. The author decides, as after Task 0. |
| 204 | |
| 205 | This is a measurement, not TDD. |
| 206 | |
| 207 | - [ ] **Step 1: Find the bird base and the sampan's crease patterns, and record their sources** |
| 208 | - [ ] **Step 2: Add cases 6–9 to the spike** |
| 209 | - [ ] **Step 3: Run and record** |
| 210 | |
| 211 | Run: `swift run -c release --package-path spikes/interpolation` |
| 212 | |
| 159 | 213 | - [ ] **Step 4: Write the decision record and apply the exit criteria** |
| 160 | 214 | - [ ] **Step 5: Commit** |
| 161 | 215 | |
| 162 | 216 | ```bash |
| 163 | 217 | git add spikes docs |
| 164 | | git commit -m "Interpolation model spike and decision record" |
| 218 | git commit -m "Non-rigid steps: model, bound and the remaining coupled steps" |
| 165 | 219 | ``` |
| 166 | 220 | |
| 167 | 221 | --- |
| @@ -185,9 +239,9 @@ git commit -m "Interpolation model spike and decision record" |
| 185 | 239 | - `enum StepKind: String, Codable, Sendable { case fold, foldAndUnfold, turnOver, rotate }` |
| 186 | 240 | - `enum StepTechnique: String, Codable, Sendable { case plain, insideReverse, outsideReverse, squash, petal, sink }` |
| 187 | 241 | - `enum TurnOverAxis: String, Codable, Sendable { case vertical, horizontal }` |
| 188 | | - `struct AxisRecord: Codable, Equatable, Sendable { let edge: Int; let movingFaces: [Int]; let startAngle: Double; let endAngle: Double; let peakAngle: Double? }` |
| 242 | - `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 |
| 189 | 243 | - `struct Viewpoint: Codable, Equatable, Sendable { let eye: SIMD3<Double>; let up: SIMD3<Double> }` — `eye` is the unit direction toward the viewer |
| 190 | | - `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 viewpoint: Viewpoint }` |
| 244 | - `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 viewpoint: Viewpoint }` — `nonRigid` decodes to `false` when absent; Task 0b adds the data a non-rigid step carries |
| 191 | 245 | - `struct FoldFrame: Codable, Sendable { var verticesCoords: [SIMD3<Double>]; var edgesAssignment: [EdgeAssignment]; var edgesFoldAngle: [Double]; var faceOrders: [[Int]]; var faceOrdersMidFold: [[Int]]?; var step: StepRecord? }` |
| 192 | 246 | - `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 |
| 193 | 247 | - `var (FoldFile).stepCount: Int { get }` — `frames.count − 1` |
| @@ -245,6 +299,21 @@ Package manifest: swift-tools-version 6.2, platforms `.iOS(.v26)`, `.macOS(.v26) |
| 245 | 299 | #expect(step?.viewpoint == Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))) |
| 246 | 300 | } |
| 247 | 301 | |
| 302 | @Test func decodesAnAxisTreeWithWindowsAndACurve() throws { |
| 303 | let json = #"{"edge": 3, "movingFaces": [2], "startAngle": 0, "endAngle": 180, "parent": 0,"# + |
| 304 | #" "window": [0.25, 1], "angles": [0, 90, 180]}"# |
| 305 | let axis = try JSONDecoder().decode(AxisRecord.self, from: Data(json.utf8)) |
| 306 | #expect(axis.parent == 0) |
| 307 | #expect(axis.window == [0.25, 1]) |
| 308 | #expect(axis.angles == [0, 90, 180]) |
| 309 | } |
| 310 | |
| 311 | @Test func windowsDecodeOnAStaggeredStep() throws { |
| 312 | let step = try FoldFile(contentsOf: fixture("letter-staggered.fold")).frames[1].step |
| 313 | #expect(step?.axes.map(\.window) == [[0, 0.5], [0.5, 1]]) |
| 314 | #expect(step?.nonRigid == false) |
| 315 | } |
| 316 | |
| 248 | 317 | @Test func decodesFirstCreasedAtStepWithNullForBoundaries() throws { |
| 249 | 318 | let f = try FoldFile(contentsOf: fixture("half.fold")) |
| 250 | 319 | #expect(f.edgesFirstCreasedAtStep == [nil, nil, nil, nil, nil, nil, 1]) |
| @@ -324,7 +393,7 @@ The interpenetration checker (Task 6) finds faces that collide in motion. It can |
| 324 | 393 | |
| 325 | 394 | ```swift |
| 326 | 395 | @Test(arguments: ["square.fold", "half.fold", "mountain.fold", "letter.fold", "letter-sequential.fold", |
| 327 | | "quarters.fold", "occlusion.fold"]) |
| 396 | "letter-staggered.fold", "quarters.fold", "occlusion.fold"]) |
| 328 | 397 | func acceptsAValidModel(_ name: String) throws { |
| 329 | 398 | #expect(validate(try FoldFile(contentsOf: fixture(name))).isEmpty) |
| 330 | 399 | } |
| @@ -446,21 +515,26 @@ git commit -m "FOLD validator: topology, angles, isometry, cycles and mid-fold o |
| 446 | 515 | **Files:** |
| 447 | 516 | - Create: `Packages/FoldKit/Sources/Folding/FoldStep.swift` |
| 448 | 517 | - Create: `Packages/FoldKit/Sources/Folding/StepReader.swift` |
| 518 | - Create: `Packages/FoldKit/Sources/FoldKitTestSupport/DegreeFourVertex.swift` |
| 449 | 519 | - Test: `Packages/FoldKit/Tests/FoldingTests/StepReadingTests.swift` |
| 450 | 520 | |
| 451 | 521 | **Interfaces:** |
| 452 | 522 | - Consumes: Task 1 types. |
| 453 | 523 | - Produces: |
| 454 | | - `struct FoldAxis: Equatable, Sendable { let edge: Int; let origin: SIMD3<Double>; let direction: SIMD3<Double>; let movingFaces: Set<Int>; let startAngle: Double; let endAngle: Double; let peakAngle: Double? }` |
| 524 | - `struct FoldAxis: Equatable, Sendable { let edge: Int; let origin: SIMD3<Double>; let direction: SIMD3<Double>; let movingFaces: Set<Int>; let startAngle: Double; let endAngle: Double; let peakAngle: Double?; let parent: Int?; let window: ClosedRange<Double>; let curve: [Double]? }` |
| 455 | 525 | - `func (FoldAxis).angle(at t: Double, kind: StepKind) -> Double` |
| 456 | | - `struct FoldStep: Equatable, Sendable { let index: Int; let kind: StepKind; let technique: StepTechnique; let instruction: String; let axes: [FoldAxis]; let rotateDegrees: Double?; let turnOverAxis: TurnOverAxis?; let repeatOf: Int?; let viewpoint: Viewpoint }` |
| 526 | - `struct FoldStep: Equatable, Sendable { let index: Int; let kind: StepKind; let technique: StepTechnique; let instruction: String; let axes: [FoldAxis]; let rotateDegrees: Double?; let turnOverAxis: TurnOverAxis?; let repeatOf: Int?; let nonRigid: Bool; let viewpoint: Viewpoint }` — `axes` is ordered so every parent comes before its children |
| 457 | 527 | - `var (FoldStep).movingFaceUnion: Set<Int> { get }` |
| 458 | 528 | - `func readSteps(_ file: FoldFile) throws -> [FoldStep]` — `result[k].index == k + 1` |
| 459 | | - `enum StepError: Error, Equatable, Sendable { case axisNotAnEdge(step: Int, edge: Int); case axisNotOnMovingSetBoundary(step: Int, edge: Int); case movingFacesOutOfRange(step: Int); case overlappingMovingSetsWithinAStep(step: Int); case rotateWithoutDegrees(step: Int); case turnOverWithoutAxis(step: Int); case foldAndUnfoldWithoutPeak(step: Int); case missingInstruction(step: Int) }` |
| 529 | - `enum StepError: Error, Equatable, Sendable { case axisNotAnEdge(step: Int, edge: Int); case axisNotOnMovingSetBoundary(step: Int, edge: Int); case movingFacesOutOfRange(step: Int); case overlappingMovingSetsWithinAStep(step: Int); case rotateWithoutDegrees(step: Int); case turnOverWithoutAxis(step: Int); case foldAndUnfoldWithoutPeak(step: Int); case missingInstruction(step: Int); case axisParentCycle(step: Int); case axisWindowOutOfRange(step: Int); case curveTooShort(step: Int) }` |
| 460 | 530 | |
| 461 | 531 | `readSteps` **reads** the authored `krz:step`; it does not infer axes or moving sets from a frame diff. Deriving a multi-axis decomposition has genuine ambiguities — which side moved, whether a reorientation is a fold, how to partition simultaneous axes — and the author already knows the answer. This task and Task 4 own Review Focus item 1, in the form Task 0 settles. |
| 462 | 532 | |
| 463 | | `FoldAxis.origin` and `direction` come from the axis edge's endpoints in frame `index − 1`. The direction is the way the moving face adjacent to the edge traverses it in `faces_vertices` order, and the origin is the vertex that traversal starts from, so that a positive rotation about it (right-hand rule) turns the moving face toward its normal — a valley — and the rotation for any step is `angle(t) − startAngle` with FOLD's own sign. `angle(at:)` is `startAngle + t·(endAngle − startAngle)` for `fold`, and `startAngle + (peakAngle − startAngle)·(1 − |2t − 1|)` for `foldAndUnfold`. |
| 533 | `FoldAxis.origin` and `direction` come from the axis edge's endpoints in frame `index − 1`. The direction is the way the moving face adjacent to the edge traverses it in `faces_vertices` order, and the origin is the vertex that traversal starts from, so that a positive rotation about it (right-hand rule) turns the moving face toward its normal — a valley — and the rotation for any step is `angle(t) − startAngle` with FOLD's own sign. `angle(at:)` first maps `t` onto the axis's window, `u = clamp((t − t0) / (t1 − t0), 0, 1)`, so an axis waits before its window and holds after it. Then it is `startAngle + u·(endAngle − startAngle)` for `fold`, `startAngle + (peakAngle − startAngle)·(1 − |2u − 1|)` for `foldAndUnfold`, or, when the axis has a curve, the curve at `u` by cubic Hermite interpolation with central-difference tangents (one-sided at the ends). Decision 0001 measured that 257 samples rebuilt this way keep a coupled vertex's tear under 1e-6. |
| 534 | |
| 535 | For an axis with a parent, `origin` and `direction` are still in frame `index − 1`'s coordinates: Task 4 applies the axis's own rotation first and its ancestors' after, so the parent's motion carries it. |
| 536 | |
| 537 | `degreeFourVertex()` builds decision 0001's case 3 in code, since its curves cannot be typed: a 2×2 sheet with creases from the centre at 0°, 60°, 140° and 260° (sectors 60°, 80°, 120°, 100°), and one `fold` step from flat to flat-folded. The face between the 140° and 260° creases holds still. Its axes are the 140° crease, linear 0 → 180; the 260° crease, a curve; and the 0° crease, carried by the 260° axis, linear 0 → 180. The 60° crease is off the tree. The curve is the closed form for a flat-foldable degree-4 vertex, `ρ = 2·atan(k·tan(90°·u))` with `k = cos 70° / cos 10°`, sampled at 257 points; the 60° crease follows as `−ρ`. Frame 1 is the flat-folded sheet. `degreeFourExactAngle(_:at:)` evaluates the closed form directly. |
| 464 | 538 | |
| 465 | 539 | - [ ] **Step 1: Write the failing tests** |
| 466 | 540 | |
| @@ -519,6 +593,20 @@ func movingFaceSetsWithinOneStepAreDisjoint(_ name: String) throws { |
| 519 | 593 | #expect(try readSteps(f)[0].instruction == f.frames[1].step?.instruction) |
| 520 | 594 | } |
| 521 | 595 | |
| 596 | @Test func aWindowedAxisWaitsAndThenHolds() throws { |
| 597 | let s = try readSteps(FoldFile(contentsOf: fixture("letter-staggered.fold")))[0] |
| 598 | #expect(s.axes[0].angle(at: 0.25, kind: s.kind) == 90) |
| 599 | #expect(s.axes[1].angle(at: 0.25, kind: s.kind) == 0) |
| 600 | #expect(s.axes[0].angle(at: 0.75, kind: s.kind) == 180) |
| 601 | #expect(s.axes[1].angle(at: 0.75, kind: s.kind) == 90) |
| 602 | } |
| 603 | |
| 604 | @Test func aCurveIsInterpolatedBetweenItsSamples() throws { |
| 605 | let axis = try readSteps(degreeFourVertex()).first!.axes.first { $0.curve != nil }! |
| 606 | let t = 0.5 + 0.5 / 256 // halfway between two samples |
| 607 | #expect(abs(axis.angle(at: t, kind: .fold) - degreeFourExactAngle(axis.edge, at: t)) < 1e-6) |
| 608 | } |
| 609 | |
| 522 | 610 | @Test func rotateWithoutDegreesThrows() { |
| 523 | 611 | #expect(throws: StepError.rotateWithoutDegrees(step: 3)) { |
| 524 | 612 | try readSteps(FoldFile(contentsOf: fixture("rotate-no-degrees.fold"))) |
| @@ -559,7 +647,7 @@ git commit -m "Read authored fold steps, including multi-axis and reorientation |
| 559 | 647 | - Create: `Packages/FoldKit/Sources/Folding/FoldInterpolator.swift` |
| 560 | 648 | - Test: `Packages/FoldKit/Tests/FoldingTests/InterpolationTests.swift` |
| 561 | 649 | |
| 562 | | **Interfaces** (as of before Task 0; Task 0 amends them for coupled steps): |
| 650 | **Interfaces** (decision 0001; Task 0b adds the path for `nonRigid` steps): |
| 563 | 651 | - Consumes: Task 1 and Task 3 types. |
| 564 | 652 | - Produces: |
| 565 | 653 | - `struct FoldInterpolator: Sendable { init(file: FoldFile, step: FoldStep) }` — interpolates from frame `step.index − 1` to frame `step.index` |
| @@ -571,7 +659,7 @@ There is no separate orientation output: a `turnOver` or `rotate` step's end fra |
| 571 | 659 | |
| 572 | 660 | ```swift |
| 573 | 661 | let modelFixtures = ["half.fold", "mountain.fold", "letter.fold", "letter-sequential.fold", |
| 574 | | "quarters.fold", "occlusion.fold"] |
| 662 | "letter-staggered.fold", "quarters.fold", "occlusion.fold"] |
| 575 | 663 | |
| 576 | 664 | @Test func tZeroReproducesTheStartFrame() throws { |
| 577 | 665 | let (file, i) = try interpolator("half.fold", step: 1) |
| @@ -629,6 +717,31 @@ func everyEdgeLengthIsPreservedThroughoutEveryStep(_ name: String) throws { |
| 629 | 717 | .allSatisfy { simd_distance($0, $1) < 1e-9 }) |
| 630 | 718 | } |
| 631 | 719 | |
| 720 | @Test func aCoupledVertexStaysWholeThroughout() throws { |
| 721 | // decision 0001, case 3: curves from the closed form, sampled at 257 points |
| 722 | let file = degreeFourVertex() |
| 723 | let i = FoldInterpolator(file: file, step: try readSteps(file)[0]) |
| 724 | for n in 0...1024 { |
| 725 | #expect(maxEdgeLengthDrift(i.positions(at: Double(n) / 1024), file) < 1e-6) |
| 726 | } |
| 727 | #expect(zip(i.positions(at: 1), file.frames[1].verticesCoords) |
| 728 | .allSatisfy { simd_distance($0, $1) < 1e-9 }) |
| 729 | } |
| 730 | |
| 731 | @Test func theSameVertexWithLinearAnglesTears() throws { |
| 732 | // the control: drop the curve and the paper does not hold together |
| 733 | let file = degreeFourVertex(linear: true) |
| 734 | let i = FoldInterpolator(file: file, step: try readSteps(file)[0]) |
| 735 | #expect(maxEdgeLengthDrift(i.positions(at: 0.5), file) > 0.1) |
| 736 | } |
| 737 | |
| 738 | @Test func aStaggeredAxisMovesOnlyInItsWindow() throws { |
| 739 | let (file, i) = try interpolator("letter-staggered.fold", step: 1) |
| 740 | let start = file.frames[0].verticesCoords |
| 741 | #expect(simd_distance(i.positions(at: 0.25)[3], start[3]) < 1e-12) // right flap waits |
| 742 | #expect(simd_distance(i.positions(at: 0.5)[0], SIMD3(2, 0, 0)) < 1e-9) // left flap down |
| 743 | } |
| 744 | |
| 632 | 745 | @Test func turnOverRotatesTheModelRigidly() throws { |
| 633 | 746 | let (_, i) = try interpolator("half.fold", step: 2) |
| 634 | 747 | #expect(pairwiseDistancesAgree(i.positions(at: 0.5), i.positions(at: 0))) |
| @@ -652,7 +765,7 @@ Expected: FAIL. |
| 652 | 765 | |
| 653 | 766 | - [ ] **Step 3: Implement `positions(at:)`** |
| 654 | 767 | |
| 655 | | For each axis, rotate its moving faces' vertices about the line through `origin` along `direction` by `angle(t) − startAngle`, converted to radians, with `simd_quatd(angle:axis:)` about the origin-shifted axis. A vertex in no moving set is unchanged. Reorientation steps rotate every vertex as described above. |
| 768 | Each axis's own transform is the rotation about the line through `origin` along `direction` by `angle(t) − startAngle`, converted to radians, built with `simd_quatd(angle:axis:)` about the origin-shifted axis. A moving face's transform is its axis's own transform followed by its parent's full transform, and so on up the tree, so a carried axis moves with its parent. A vertex in no moving set is unchanged. A vertex shared by faces of different sets takes its position from the first face in `faces_vertices` order that contains it. `foldc`'s curves keep the copies within the isometry tolerance, and the catalogue test (Task 8) checks this. Reorientation steps rotate every vertex as described above. `degreeFourVertex(linear:)` gains a `linear` flag that replaces the curves with straight lines, for the control test. |
| 656 | 769 | |
| 657 | 770 | - [ ] **Step 4: Run tests to verify they pass** |
| 658 | 771 | |
| @@ -663,7 +776,7 @@ Expected: PASS. |
| 663 | 776 | |
| 664 | 777 | ```bash |
| 665 | 778 | git add Packages/FoldKit |
| 666 | | git commit -m "Rigid interpolation, including reorientation steps" |
| 779 | git commit -m "Rigid interpolation on an axis tree with windows and solved curves" |
| 667 | 780 | ``` |
| 668 | 781 | |
| 669 | 782 | --- |
| @@ -834,6 +947,12 @@ git commit -m "Near-coplanar clustering and view-relative layer order" |
| 834 | 947 | #expect(!interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty) |
| 835 | 948 | } |
| 836 | 949 | |
| 950 | @Test func staggeredFlapsDoNotCollide() throws { |
| 951 | // the same two flaps on separate windows of t |
| 952 | let (file, i) = try interpolator("letter-staggered.fold", step: 1) |
| 953 | #expect(interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty) |
| 954 | } |
| 955 | |
| 837 | 956 | @Test func aFaceSwingingThroughAFaceItSharesAVertexWithIsDetected() throws { |
| 838 | 957 | // built in code: fixed triangle (0,0,0), (1,1.2,0.4), (-1,1.2,0.4); moving triangle |
| 839 | 958 | // (0,0,0), (0,2,0), (-1,1,0) hinged on its edge along the y-axis, folding toward +z |
| @@ -890,9 +1009,10 @@ git commit -m "Facet interpenetration checker" |
| 890 | 1009 | - Create: `Packages/FoldKit/Sources/FoldCompiler/FoldScript.swift` |
| 891 | 1010 | - Create: `Packages/FoldKit/Sources/FoldCompiler/FrameCompiler.swift` |
| 892 | 1011 | - Create: `Packages/FoldKit/Sources/FoldCompiler/LayerOrderGuess.swift` |
| 893 | | - Create: `Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/{half,mountain,letter,letter-sequential,quarters,occlusion}-cp.fold` — each model fixture's sheet and creases as an editor would export them: no frames, every crease carrying its **final** `M`/`V` assignment from the fixture's last frame (`F` for a crease that ends flat). `compile` resets frame 0 to `F` and 0 |
| 894 | | - Create: `Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/{half,mountain,letter,letter-sequential,quarters,occlusion}.foldscript` |
| 1012 | - Create: `Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/{half,mountain,letter,letter-sequential,letter-staggered,quarters,occlusion}-cp.fold` — each model fixture's sheet and creases as an editor would export them: no frames, every crease carrying its **final** `M`/`V` assignment from the fixture's last frame (`F` for a crease that ends flat). `compile` resets frame 0 to `F` and 0 |
| 1013 | - Create: `Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/{half,mountain,letter,letter-sequential,letter-staggered,quarters,occlusion}.foldscript` |
| 895 | 1014 | - Create: `Packages/FoldKit/Sources/FoldKitTestSupport/Fixtures/{unresolved-crease,angle-out-of-range}.foldscript` |
| 1015 | - Modify: `Packages/FoldKit/Sources/FoldKitTestSupport/DegreeFourVertex.swift` — `degreeFourPattern()` and `degreeFourScript()`, the same vertex as a crease pattern and a one-step script driven by the 140° crease |
| 896 | 1016 | - Test: `Packages/FoldKit/Tests/FoldCompilerTests/CompilerTests.swift` |
| 897 | 1017 | |
| 898 | 1018 | **Interfaces:** |
| @@ -900,7 +1020,7 @@ git commit -m "Facet interpenetration checker" |
| 900 | 1020 | - Produces: |
| 901 | 1021 | - `struct FoldScript: Codable, Sendable { let creasePattern: String; let title: String; let steps: [ScriptStep] }` — `creasePattern` is relative to the script |
| 902 | 1022 | - `init (FoldScript).init(contentsOf url: URL) throws` |
| 903 | | - `struct ScriptStep: Codable, Sendable { let kind: StepKind; let technique: StepTechnique?; let instruction: String; let creases: [CreaseRef]; let movingSide: [SIMD2<Double>]; let angles: [Double]?; let peakAngles: [Double]?; let rotateDegrees: Double?; let turnOverAxis: TurnOverAxis?; let repeatOf: Int?; let viewpoint: Viewpoint?; let faceOrders: [OrderOverride]?; let faceOrdersMidFold: [OrderOverride]? }` |
| 1023 | - `struct ScriptStep: Codable, Sendable { let kind: StepKind; let technique: StepTechnique?; let instruction: String; let creases: [CreaseRef]; let movingSide: [SIMD2<Double>]; let angles: [Double]?; let peakAngles: [Double]?; let rotateDegrees: Double?; let turnOverAxis: TurnOverAxis?; let repeatOf: Int?; let viewpoint: Viewpoint?; let faceOrders: [OrderOverride]?; let faceOrdersMidFold: [OrderOverride]?; let driver: CreaseRef?; let windows: [[Double]]? }` — `windows` pairs with `creases` |
| 904 | 1024 | - `enum CreaseRef: Codable, Sendable { case betweenVertices(SIMD2<Double>, SIMD2<Double>); case edgeIndex(Int) }` |
| 905 | 1025 | - `struct OrderOverride: Codable, Sendable { let above: SIMD2<Double>; let below: SIMD2<Double> }` — points in crease-pattern coordinates naming two faces, "above" as seen from the step's viewpoint |
| 906 | 1026 | - `func compile(_ script: FoldScript, pattern: FoldFile) throws -> FoldFile` |
| @@ -911,7 +1031,9 @@ git commit -m "Facet interpenetration checker" |
| 911 | 1031 | |
| 912 | 1032 | **This task is the reason the project is buildable.** No existing FOLD tool authors a multi-frame sequence with folded coordinates, per-frame layer order and per-step moving sets; hand-writing them is hundreds of exact numbers per frame. `foldc` takes a crease pattern exported from an existing editor plus a short script that names each step's creases (by the points they run between), which side moves (by a point inside it), and the step kind, and **computes** every frame by applying the rotations in order. It guesses `faceOrders`, applies the script's overrides, and runs the Task 2 validator and the Task 6 checker after each step. |
| 913 | 1033 | |
| 914 | | Defaults: `angles` are each crease's end fold angle, ±180 by the pattern's assignment; `viewpoint` is the previous step's, or `[0, 0, 1]` / `[0, 1, 0]` for step 1; `technique` is `plain`. |
| 1034 | Defaults: `angles` are each crease's end fold angle, ±180 by the pattern's assignment; `viewpoint` is the previous step's, or `[0, 0, 1]` / `[0, 1, 0]` for step 1; `technique` is `plain`; `driver` is the first crease; `windows` are all `[0, 1]`. |
| 1035 | |
| 1036 | **Coupled steps (decision 0001).** The step's creases cut the sheet into regions, each connected across edges this step does not fold. The fixed region is the one no `movingSide` point lies in. A spanning tree of regions, grown from the fixed region across the step's creases, gives each moving region its axis and each axis its parent. A step crease left off that tree closes a loop. If there is none, every axis is linear on its window. If there are some, `foldc` holds the driver linear on its window and solves the other tree axes as the spike does: damped Gauss–Newton on the gap between vertex copies, continued along *t*. It writes each solved axis's curve at 257 points. Each curve is then checked at four times that density, and its samples are doubled until the gap stays under 1e-6 × the sheet's size. A loop that does not close (the solver's gap stays above that) fails the step: the step has no rigid motion and needs `nonRigid` (Task 0b). |
| 915 | 1037 | |
| 916 | 1038 | Layer-order corrections are `faceOrders` overrides **in the script**. The generated file is never edited; Task 8 checks that recompiling reproduces it. |
| 917 | 1039 | |
| @@ -920,7 +1042,7 @@ macOS only. Never linked into the app. |
| 920 | 1042 | - [ ] **Step 1: Write the failing tests** |
| 921 | 1043 | |
| 922 | 1044 | ```swift |
| 923 | | @Test(arguments: ["half", "mountain", "letter", "letter-sequential", "quarters", "occlusion"]) |
| 1045 | @Test(arguments: ["half", "mountain", "letter", "letter-sequential", "letter-staggered", "quarters", "occlusion"]) |
| 924 | 1046 | func compilingASyntheticScriptReproducesItsHandWrittenFixture(_ name: String) throws { |
| 925 | 1047 | let out = try compile(FoldScript(contentsOf: fixture("\(name).foldscript")), |
| 926 | 1048 | pattern: FoldFile(contentsOf: fixture("\(name)-cp.fold"))) |
| @@ -960,6 +1082,17 @@ func compilingASyntheticScriptReproducesItsHandWrittenFixture(_ name: String) th |
| 960 | 1082 | #expect(throws: Never.self) { try LayerOrder(faceOrders: swapped, normals: faceNormals(frame, file)) } |
| 961 | 1083 | } |
| 962 | 1084 | |
| 1085 | @Test func aCoupledStepIsSolvedIntoCurves() throws { |
| 1086 | // decision 0001's case 3 as a script: the 140° crease drives, the 260° crease |
| 1087 | // gets a curve, the 0° crease rides on it, the 60° crease closes the loop |
| 1088 | let out = try compile(degreeFourScript(), pattern: degreeFourPattern()) |
| 1089 | let axes = try #require(out.frames[1].step?.axes) |
| 1090 | #expect(axes.filter { $0.angles != nil }.count == 1) |
| 1091 | #expect(axes.contains { $0.parent != nil }) |
| 1092 | let curved = axes.first { $0.angles != nil }! |
| 1093 | #expect(abs(curved.angles![128] - degreeFourExactAngle(curved.edge, at: 0.5)) < 1e-6) |
| 1094 | } |
| 1095 | |
| 963 | 1096 | @Test func anUnresolvedCreaseNamesItsStep() { |
| 964 | 1097 | #expect(throws: CompileError.unresolvedCrease(step: 2)) { |
| 965 | 1098 | try compile(FoldScript(contentsOf: fixture("unresolved-crease.foldscript")), |
| @@ -1039,11 +1172,12 @@ Its crease pattern and `.foldscript` are committed next to the generated `.fold` |
| 1039 | 1172 | } |
| 1040 | 1173 | |
| 1041 | 1174 | @Test func noShippedModelStretchesAtAnyStep() throws { |
| 1175 | // flagged non-rigid steps are held to Task 0b's bound instead |
| 1042 | 1176 | for file in try ModelLibrary.all() { |
| 1043 | | for step in try readSteps(file) { |
| 1177 | for step in try readSteps(file) where !step.nonRigid { |
| 1044 | 1178 | let i = FoldInterpolator(file: file, step: step) |
| 1045 | | for t in stride(from: 0.0, through: 1.0, by: 1.0 / 64) { |
| 1046 | | #expect(maxEdgeLengthDrift(i.positions(at: t), file) < 1e-9, |
| 1179 | for t in stride(from: 0.0, through: 1.0, by: 1.0 / 256) { |
| 1180 | #expect(maxEdgeLengthDrift(i.positions(at: t), file) < 1e-6, |
| 1047 | 1181 | "\(file.fileTitle ?? "?") step \(step.index) t \(t)") |
| 1048 | 1182 | } |
| 1049 | 1183 | } |
| @@ -1215,7 +1349,7 @@ Reference, per the spec: Randlett's conventions in *The Art of Origami* (1961) a |
| 1215 | 1349 | } |
| 1216 | 1350 | |
| 1217 | 1351 | @Test func everyStepMatchesItsGoldenImage() throws { |
| 1218 | | for name in ["half", "mountain", "letter", "letter-sequential", "quarters", "occlusion"] { |
| 1352 | for name in ["half", "mountain", "letter", "letter-sequential", "letter-staggered", "quarters", "occlusion"] { |
| 1219 | 1353 | let file = try FoldFile(contentsOf: fixture("\(name).fold")) |
| 1220 | 1354 | for step in try readSteps(file) { |
| 1221 | 1355 | try expectSnapshot(render(buildDiagram(file: file, step: step)), named: "\(name)-\(step.index)") |
| @@ -1758,7 +1892,7 @@ Expected: FAIL — only the cup is in the catalogue. |
| 1758 | 1892 | |
| 1759 | 1893 | - [ ] **Step 3: Author the three models** |
| 1760 | 1894 | |
| 1761 | | Box and sampan first, then the crane. Neither is shallow: the masu's wall-raising and the sampan's final turn inside out are coupled 3D steps, and both use whatever Task 0 decided for coupled steps. Run `foldc`, then `foldlint`, correcting guessed orderings with script overrides where the interpenetration checker or the validator's layer-order rules object. Check each in the app before committing: the checks prove no facet passes through another and no ordering is impossible, but only your eye tells you the fold reads correctly. |
| 1895 | Box and sampan first, then the crane. Neither is shallow: the masu's wall-raising and the sampan's final turn inside out are coupled 3D steps, classified by Task 0b as rigid or flagged. The crane's inside reverse folds (neck, tail, head) are flagged `nonRigid` and use Task 0b's model; its petal folds use whatever 0b found. Run `foldc`, then `foldlint`, correcting guessed orderings with script overrides where the interpenetration checker or the validator's layer-order rules object. Check each in the app before committing: the checks prove no facet passes through another and no ordering is impossible, but only your eye tells you the fold reads correctly. |
| 1762 | 1896 | |
| 1763 | 1897 | - [ ] **Step 4: Run tests to verify they pass** |
| 1764 | 1898 | |