Commit b3465f1dda

b3465f1ddadc24c0d73336d8acf73dc8904805d3

parent: 5e1d0001ad

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-19 22:04 UTC

Plan: subdivision edges by motion, end-frame check, compiler and layer-order follow-ups

Ref #2

Layout: unified · split

docs/superpowers/plans/2026-10-19-fold.md +16 −4
@@ -19,7 +19,7 @@
1919- `edges_assignment` codes: `B` boundary, `M` mountain, `V` valley, `F` flat, `U` unassigned. `C` and `J` are rejected.
2020- `edges_foldAngle` is degrees in −180...180, positive for valley, negative for mountain, 0 for `F`, `U` and `B`.
2121- `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, never the axis edge of any step, `edges_krz:firstCreasedAtStep` null, never drawn. A crease made only by a fold-and-unfold is flat in every frame but is an axis, so it is not a subdivision edge and needs `edges_krz:firstCreasedAtStep`.
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`.
2323- Extension keys: `edges_krz:firstCreasedAtStep` (key frame, inherited), `krz:step` (frames 1…N), `krz:faceOrders_midFold` (frames 1…N).
2424- **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.
2525- Every frame in `file_frames` carries `frame_parent: 0` and `frame_inherit: true`, and never overrides `edges_vertices` or `faces_vertices`.
@@ -945,7 +945,7 @@ Expected: FAIL.
945945
946946- [ ] **Step 3: Implement `LayerOrder`, `CoplanarCluster` and `clusters`**
947947
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.
949949
950950- [ ] **Step 4: Run tests to verify they pass**
951951
@@ -1164,7 +1164,7 @@ Expected: FAIL.
11641164
11651165- [ ] **Step 3: Implement `FoldScript`, `compile`, `guessFaceOrders`, `applyOrderOverrides` and the `foldc` entry point**
11661166
1167Resolve 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.
1167Every 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.
11681168
11691169- [ ] **Step 4: Run tests to verify they pass**
11701170
@@ -1215,6 +1215,7 @@ Decision 0002: an inside reverse fold has no rigid motion with the flap closed.
12151215- builds `openAxes` (the spine as one linear axis);
12161216- builds `popped` (the reverse-fold branch with the spine held at `openAngle`, solved as in Task 7);
12171217- builds `closeAxes` (from `popped` to the end frame, with the spine driven and the rest solved);
1218- checks `popped` has one position per vertex;
12181219- measures the pop's strain on the step's render mesh, and fails the step above **10%**.
12191220
12201221Decision 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"
13361337- Consumes: Tasks 1–7b.
13371338- Produces:
13381339 - `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
13401341
13411342**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.
13421343
@@ -1345,6 +1346,17 @@ Its crease pattern and `.foldscript` are committed next to the generated `.fold`
13451346- [ ] **Step 1: Write the failing tests**
13461347
13471348```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
13481360@Test func everyShippedModelValidates() throws {
13491361 for file in try ModelLibrary.all() { #expect(validate(file).isEmpty, "\(file.fileTitle ?? "?")") }
13501362}
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
141141several regions or are nonconvex; `J` is rejected and faces must be convex.
142142A crease that ends inside the sheet (a pinch) leaves a nonconvex face around
143143its end, which is split by a **subdivision edge**: assigned `F`, flat in
144every frame, never any step's axis, with no `edges_krz:firstCreasedAtStep`, and never drawn.
144every frame, creased by no step (no step moves its two faces differently), with no `edges_krz:firstCreasedAtStep`, and never drawn.
145145
146146### Frames and step numbering
147147