Commit db09babfe7

db09babfe76cac61a0c2a73c4c84aa7946269679

parent: b56cd8779b

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-23 02:31 UTC

Review fixes: docs for the fallback, band and mesh; mesh test over the shipped models

Ref #7

Layout: unified · split

Packages/FoldKit/Sources/FoldCompiler/FrameCompiler.swift −1
@@ -776,7 +776,6 @@ private func symmetricEigen(_ matrix: [[Double]]) -> (values: [Double], vectors:
776 return ((0..<n).map { a[$0][$0] }, (0..<n).map { i in (0..<n).map { v[$0][i] } }) 776 return ((0..<n).map { a[$0][$0] }, (0..<n).map { i in (0..<n).map { v[$0][i] } })
777} 777}
778 778
779
780/// Gaussian elimination with partial pivoting; nil when `a` is singular. 779/// Gaussian elimination with partial pivoting; nil when `a` is singular.
781private func solveLinear(_ a: [[Double]], _ b: [Double]) -> [Double]? { 780private func solveLinear(_ a: [[Double]], _ b: [Double]) -> [Double]? {
782 var m = a, x = b 781 var m = a, x = b
Packages/FoldKit/Sources/Folding/FoldInterpolator.swift +8 −2
@@ -171,7 +171,12 @@ public struct FoldInterpolator: Sendable {
171/// toward the layer across the spine. The tip is `tip`; its spine is the edges of `spine` with both 171/// toward the layer across the spine. The tip is `tip`; its spine is the edges of `spine` with both
172/// faces in it. A tip face's partner is the face across the spine from the faces it joins without 172/// faces in it. A tip face's partner is the face across the spine from the faces it joins without
173/// crossing the spine. A triangle at distance d from the spine's line in `start` (frame n − 1), 173/// crossing the spine. A triangle at distance d from the spine's line in `start` (frame n − 1),
174/// its centroid's, has weight d / b, b being the largest such distance of a tip triangle. 174/// its centroid's, has weight d / b, b being the largest such distance of a tip triangle; one
175/// with weight 1 or more is not blended.
176///
177/// Limitations: each tip layer takes one partner face, the first met across the spine, so a
178/// layer facing several faces across it blends toward only one of them; and the spine's line is
179/// the first tip-spine edge's, so a tip spine that is not straight is measured from that edge.
175private func band(mesh: RenderMesh, file: FoldFile, tip: Set<Int>, spine: [Int], 180private func band(mesh: RenderMesh, file: FoldFile, tip: Set<Int>, spine: [Int],
176 start: [SIMD3<Double>]) -> [Bending.Blend] { 181 start: [SIMD3<Double>]) -> [Bending.Blend] {
177 let edgeFaces = edgesFaces(file) 182 let edgeFaces = edgesFaces(file)
@@ -217,7 +222,8 @@ private func band(mesh: RenderMesh, file: FoldFile, tip: Set<Int>, spine: [Int],
217 222
218/// Decision 0003's ruling R78: the tip's mesh vertices on its spine's line in `start` (frame 223/// Decision 0003's ruling R78: the tip's mesh vertices on its spine's line in `start` (frame
219/// n − 1), grouped where they coincide in `start` and in every pose of `poses`, so that the 224/// n − 1), grouped where they coincide in `start` and in every pose of `poses`, so that the
220/// halves' coincident fold edges on that line move together during the pop. 225/// halves' coincident fold edges on that line move together during the pop. As for the band, the
226/// line is the first tip-spine edge's.
221private func spineTies(mesh: RenderMesh, file: FoldFile, tip: Set<Int>, spine: [Int], start: [SIMD3<Double>], 227private func spineTies(mesh: RenderMesh, file: FoldFile, tip: Set<Int>, spine: [Int], start: [SIMD3<Double>],
222 poses: [[SIMD3<Double>]]) -> [[Int]] { 228 poses: [[SIMD3<Double>]]) -> [[Int]] {
223 let edgeFaces = edgesFaces(file) 229 let edgeFaces = edgesFaces(file)
Packages/FoldKit/Sources/Folding/FoldParameterMapping.swift +4 −1
@@ -38,7 +38,10 @@ public struct FoldParameterMapping: Sendable {
38 private static let samples = 129 38 private static let samples = 129
39 private static let minimumLength = 8.0 39 private static let minimumLength = 8.0
40 /// Points dragged down for the whole step where a segment's path is under `minimumLength`: 160, 40 /// Points dragged down for the whole step where a segment's path is under `minimumLength`: 160,
41 /// or half the view's height when that is less, so a fallback stretch ends inside the view. 41 /// or half the view's height when that is less. A stretch that starts in the upper half of the
42 /// view then ends inside it; one that starts lower can run past its bottom edge, as the crane's
43 /// head pop does on iPhone (it starts at y ≈ 316 of 362 points). Its direction stays down
44 /// pending a check on a device (ruling R84).
42 private let fallbackThrow: Double 45 private let fallbackThrow: Double
43 /// How much further, in points, another axis's centroid must travel to replace the previous 46 /// How much further, in points, another axis's centroid must travel to replace the previous
44 /// segment's axis. 47 /// segment's axis.
Packages/FoldKit/Sources/Folding/LayerOrder.swift +5 −3
@@ -202,9 +202,9 @@ public func stackDepth(positions: [SIMD3<Double>], faces: [[Int]]) -> Int {
202 202
203// MARK: - Helpers 203// MARK: - Helpers
204 204
205/// The connected components of `linked` over `0..<count`, each in index order, ordered by their 205/// The faces of each near-coplanar cluster, in index order, ordered by their smallest face, as
206/// smallest member. 206/// `LayerOrder.clusters` links them: normals within 2° of one line, each face's vertices within
207/// The faces of each near-coplanar cluster, in index order, as `LayerOrder.clusters` links them. 207/// sin 2° × the positions' bounding-box diagonal of the other's plane, and interiors overlapping.
208func clusterMembers(positions: [SIMD3<Double>], faces: [[Int]]) -> [[Int]] { 208func clusterMembers(positions: [SIMD3<Double>], faces: [[Int]]) -> [[Int]] {
209 let normals = faceNormals(positions: positions, faces: faces) 209 let normals = faceNormals(positions: positions, faces: faces)
210 let diagonal = boundingBoxDiagonal(positions) 210 let diagonal = boundingBoxDiagonal(positions)
@@ -226,6 +226,8 @@ func clusterMembers(positions: [SIMD3<Double>], faces: [[Int]]) -> [[Int]] {
226 return components(faces.count, linked: linked) 226 return components(faces.count, linked: linked)
227} 227}
228 228
229/// The connected components of `linked` over `0..<count`, each in index order, ordered by their
230/// smallest member.
229private func components(_ count: Int, linked: (Int, Int) -> Bool) -> [[Int]] { 231private func components(_ count: Int, linked: (Int, Int) -> Bool) -> [[Int]] {
230 var component = Array<Int?>(repeating: nil, count: count) 232 var component = Array<Int?>(repeating: nil, count: count)
231 var result: [[Int]] = [] 233 var result: [[Int]] = []
Packages/FoldKit/Sources/Folding/RenderMesh.swift +1
@@ -7,6 +7,7 @@ import simd
7/// is triangulated on those points by ear clipping, point insertion and Lawson flips, so it is 7/// is triangulated on those points by ear clipping, point insertion and Lawson flips, so it is
8/// Delaunay within the face. Faces sharing a side share its points, so the mesh is one connected 8/// Delaunay within the face. Faces sharing a side share its points, so the mesh is one connected
9/// sheet. The FOLD topology is unchanged; a non-rigid step bends this mesh, not the FOLD faces. 9/// sheet. The FOLD topology is unchanged; a non-rigid step bends this mesh, not the FOLD faces.
10/// The construction assumes convex faces, which the validator enforces.
10public struct RenderMesh: Sendable { 11public struct RenderMesh: Sendable {
11 /// Frame 0 positions in its plane. 12 /// Frame 0 positions in its plane.
12 public let vertices: [SIMD2<Double>] 13 public let vertices: [SIMD2<Double>]
Packages/FoldKit/Tests/FoldingTests/RenderMeshTests.swift +8 −3
@@ -89,10 +89,15 @@ private func meshTestFiles() throws -> [FoldFile] {
89 offGrid()] 89 offGrid()]
90} 90}
91 91
92/// The shipped models' crease patterns, whose faces are their frame 0's.
93private func shippedPatterns() throws -> [FoldFile] {
94 try ["cup", "box", "sampan", "crane"].map { try FoldFile(contentsOf: pattern($0)) }
95}
96
92@Test func noMeshEdgeIsMuchShorterThanTheSpacing() throws { 97@Test func noMeshEdgeIsMuchShorterThanTheSpacing() throws {
93 // decision 0003: 0.15 of the spacing, except where a crease is shorter or creases meet sharply, 98 // decision 0003: 0.15 of the spacing, except where a crease is shorter or creases meet sharply;
94 // which none of these sheets has 99 // none of these sheets needs the exception, the shipped models' included
95 for file in try meshTestFiles() { 100 for file in try meshTestFiles() + shippedPatterns() {
96 let spacing = RenderMesh.spacing(for: file) 101 let spacing = RenderMesh.spacing(for: file)
97 let mesh = RenderMesh(file: file, spacing: spacing) 102 let mesh = RenderMesh(file: file, spacing: spacing)
98 let shortest = edges(mesh).map { simd_distance(mesh.vertices[$0.x], mesh.vertices[$0.y]) }.min()! 103 let shortest = edges(mesh).map { simd_distance(mesh.vertices[$0.x], mesh.vertices[$0.y]) }.min()!
docs/decisions/0003-bending-openings.md +8 −7
@@ -97,8 +97,7 @@ The fixture is not a clean comparison. Its pop strain depends on the mesh, rangi
97each other, at about 9%. The grid's 7.46% is partly an artifact of its edges running 97each other, at about 9%. The grid's 7.46% is partly an artifact of its edges running
98only in three directions. 98only in three directions.
99 99
100**Recommendation: the Delaunay construction.** Keep the bound on edge strain and report 100**Recommendation: the Delaunay construction.** Keep the bound on edge strain.
101principal strain alongside it.
102 101
103## 2. Crane: neck, tail and head 102## 2. Crane: neck, tail and head
104 103
@@ -324,8 +323,7 @@ them.
324 is a rigid M2 motion, like any coupled step. 323 is a rigid M2 motion, like any coupled step.
3253. **Pop: banded slerp** (§2), at a default opening of 20° from flat. Plain slerp also 3243. **Pop: banded slerp** (§2), at a default opening of 20° from flat. Plain slerp also
326 passes the crane at 20° (8.74%, 9.01%), with little margin. 325 passes the crane at 20° (8.74%, 9.01%), with little margin.
3274. **Bound: 10% edge strain on the Delaunay mesh, unchanged.** Principal strain is 3264. **Bound: 10% edge strain on the Delaunay mesh, unchanged.**
328 reported beside it.
3295. **Sampan: not solved by bending.** The options are under *Decisions for the author*. 3275. **Sampan: not solved by bending.** The options are under *Decisions for the author*.
330 328
331## Cost 329## Cost
@@ -365,11 +363,14 @@ them.
365 - With `openOn`, the open axes are the tree that the loop solver closes, driven by 363 - With `openOn`, the open axes are the tree that the loop solver closes, driven by
366 `openOn` from its angle to ±α; the close is the same in reverse after the pop. 364 `openOn` from its angle to ±α; the close is the same in reverse after the pop.
367 - Mesh interpenetration is checked in all three phases. 365 - Mesh interpenetration is checked in all three phases.
368- **`Bending`**: per-triangle turn targets. A tip triangle at distance d from the tip's 366- **`Bending`**: per-triangle turn targets. A tip triangle lies at distance d from the
369 spine (in frame n − 1) has width b, the tip's width there. Its target is 367 tip's spine (in frame n − 1); b is one width for the whole tip, the largest such d of a
368 tip triangle, as the spike computed it. A triangle with d / b < 1 has target
370 slerp(mid, own, w), where: 369 slerp(mid, own, w), where:
371 - mid = slerp(own, other, ½); 370 - mid = slerp(own, other, ½);
372 - w = min(1, d / b) + (1 − min(1, d / b))·(1 − 4s(1 − s)). 371 - w = d / b + (1 − d / b)·(1 − 4s(1 − s)).
372
373 Triangles with d / b ≥ 1 are not blended and keep their own turn.
373- **Crane pattern:** bend lines on the spine's line through the front wing's centre and 374- **Crane pattern:** bend lines on the spine's line through the front wing's centre and
374 the layer under it, added with the neck and tail creases. 375 the layer under it, added with the neck and tail creases.
375- **Tests** (Task 15): 376- **Tests** (Task 15):