Commit d1caa68b31

d1caa68b31795f5bf51da99067a6cf7dcf60aeee

parent: 41f7bf531e

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 04:31 UTC

Name the step's full moving set and serialise pop solves

FoldStep.allMovingFaces adds a non-rigid step's open and close axes'
faces to movingFaceUnion. LayerOrder.active rewrites mixed pairs with it
and layerMotionDefects uses it. Solves on one sparse factor take a lock.

Ref #5

Layout: unified · split

Packages/FoldKit/Sources/Folding/Bending.swift +5 −3
@@ -1,5 +1,6 @@
11import Accelerate
22import simd
3import Synchronization
34
45/// As-rigid-as-possible interpolation on a render mesh between two rigid poses of the FOLD faces
56/// (decision 0002's pop). At `s` each triangle's edges are its edges in the first pose turned by
@@ -99,11 +100,12 @@ private func frame(_ p: [SIMD3<Double>]) -> simd_double3x3 {
99100 return simd_double3x3(columns: (along, cross(normal, along), normal))
100101}
101102
102/// Accelerate's sparse Cholesky factor of a symmetric positive definite matrix. Only read after
103/// `init`, so sharing it across threads is safe.
103/// Accelerate's sparse Cholesky factor of a symmetric positive definite matrix. Solves are
104/// serialised: Accelerate does not document concurrent `SparseSolve` calls on one factor as safe.
104105private final class SparseCholesky: @unchecked Sendable {
105106 private let factor: SparseOpaqueFactorization_Double
106107 private let size: Int
108 private let lock = Mutex(())
107109
108110 /// `lower` holds the entries on and below the diagonal, keyed by (row, column).
109111 init(size: Int, lower: [SIMD2<Int>: Double]) {
@@ -127,7 +129,7 @@ private final class SparseCholesky: @unchecked Sendable {
127129 values.withUnsafeMutableBufferPointer { buffer in
128130 let b = DenseMatrix_Double(rowCount: Int32(size), columnCount: Int32(columns), columnStride: Int32(size),
129131 attributes: SparseAttributes_t(), data: buffer.baseAddress!)
130 SparseSolve(factor, b)
132 lock.withLock { _ in SparseSolve(factor, b) }
131133 }
132134 }
133135}
Packages/FoldKit/Sources/Folding/FoldStep.swift +9
@@ -96,9 +96,18 @@ public struct FoldStep: Equatable, Sendable {
9696 self.viewpoint = viewpoint
9797 }
9898
99 /// The faces the step's axes move. A non-rigid step's pop holds the other faces still.
99100 public var movingFaceUnion: Set<Int> {
100101 axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
101102 }
103
104 /// Every face that moves during the step: `movingFaceUnion` and, in a non-rigid step, the faces
105 /// its open and close axes move, which include the faces that open on the spine.
106 public var allMovingFaces: Set<Int> {
107 let pop = nonRigid ? pop : nil
108 return (axes + (pop?.openAxes ?? []) + (pop?.closeAxes ?? []))
109 .reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
110 }
102111}
103112
104113public enum StepError: Error, Equatable, Sendable {
Packages/FoldKit/Sources/Folding/LayerMotion.swift +1 −1
@@ -43,7 +43,7 @@ public func layerMotionDefects(step n: Int, in file: FoldFile) -> [FoldDefect] {
4343 let size = sheetExtent(file)
4444 let pop = step.nonRigid ? step.pop : nil
4545 let departing = pop?.openAxes ?? step.axes, arriving = pop?.closeAxes ?? step.axes
46 let moving = (step.axes + departing + arriving).reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) }
46 let moving = step.allMovingFaces
4747 let interpolator = FoldInterpolator(file: file, step: step)
4848 let check = { (frame: FoldFrame, axes: [FoldAxis], departure: Bool) in
4949 crossings(frame, axes: axes, departure: departure, still: { !moving.contains($0) },
Packages/FoldKit/Sources/Folding/LayerOrder.swift +4 −4
@@ -28,11 +28,11 @@ private let parallelCosine = 1 - 1e-9
2828/// The stacking order of faces in one state of a step, kept as raw `faceOrders` triples so that
2929/// each is read against its `g`'s normal wherever the faces are.
3030public struct LayerOrder: Sendable {
31 private struct Triple: Sendable {
31 struct Triple: Sendable {
3232 let f: Int, g: Int, s: Int
3333 }
3434
35 private let triples: [Triple]
35 let triples: [Triple]
3636 /// Each pair's relation in the form `rigidInvariantFaceOrders` gives, from the source that
3737 /// supplied the pair.
3838 private let invariant: [SIMD2<Int>: Int]
@@ -77,7 +77,7 @@ public struct LayerOrder: Sendable {
7777 ///
7878 /// A triple whose `g` moves in the step and whose `f` does not is replaced, with its source's
7979 /// normals, by its reverse, so that the pair is read against the still face's normal, which
80 /// does not turn during the step.
80 /// does not turn during the step. A face moves when it is in `step.allMovingFaces`.
8181 public static func active(for step: FoldStep, in file: FoldFile) throws -> LayerOrder {
8282 let start = file.frames[step.index - 1], end = file.frames[step.index]
8383 let first: (faceOrders: [[Int]], normals: [SIMD3<Double>])
@@ -90,7 +90,7 @@ public struct LayerOrder: Sendable {
9090 } else {
9191 first = (end.faceOrders, faceNormals(end, file))
9292 }
93 let moving = step.movingFaceUnion
93 let moving = step.allMovingFaces
9494 let sources = [first, (start.faceOrders, faceNormals(start, file))].map { source in
9595 (faceOrders: source.faceOrders.map { t -> [Int] in
9696 guard t.count == 3, source.normals.indices.contains(t[0]), source.normals.indices.contains(t[1]),
Packages/FoldKit/Tests/FoldCompilerTests/NonRigidTests.swift +23
@@ -117,3 +117,26 @@ import FoldKitTestSupport
117117 #expect(structurallyEqual(nudged(by: 1e-13), file, tolerance: 1e-12))
118118 #expect(!structurallyEqual(nudged(by: 1e-9), file, tolerance: 1e-12))
119119}
120
121@Test func concurrentPopSolvesAgreeWithSerialOnes() async throws {
122 // one interpolator, so one factor, solved from eight tasks at once
123 let file = try compiled("reverse")
124 let step = try readSteps(file)[1]
125 let i = FoldInterpolator(file: file, step: step)
126 let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound)
127 let times = (1..<64).map { lo + Double($0) / 64 * (hi - lo) }
128 let serial = times.map { i.meshPositions(at: $0) }
129 let mismatches = await withTaskGroup(of: Int.self) { group in
130 for _ in 0..<8 {
131 group.addTask {
132 var count = 0
133 for _ in 0..<20 {
134 for (t, expected) in zip(times, serial) where i.meshPositions(at: t) != expected { count += 1 }
135 }
136 return count
137 }
138 }
139 return await group.reduce(0, +)
140 }
141 #expect(mismatches == 0)
142}
Packages/FoldKit/Tests/FoldingTests/LayerOrderTests.swift +10 −1
@@ -1,7 +1,7 @@
11import Testing
22import simd
33import FoldFormat
4import Folding
4@testable import Folding
55import FoldKitTestSupport
66
77let up2 = [SIMD3<Double>(0, 0, 1), SIMD3(0, 0, 1)]
@@ -87,6 +87,15 @@ let up3 = up2 + [SIMD3(0, 0, 1)]
8787 #expect(c.map(\.faces) == [[1, 0]])
8888}
8989
90@Test func aFaceOpeningOnTheSpineIsReadAgainstTheStillFace() throws {
91 // reverse step 2: f3 opens on the spine and closes again while f0 holds; both frames give [0, 3, 1]
92 let file = try compiled("reverse")
93 let step = try readSteps(file)[1]
94 #expect(!step.movingFaceUnion.contains(3))
95 let pairs = try LayerOrder.active(for: step, in: file).triples.map { [$0.f, $0.g] }
96 #expect(pairs.contains([3, 0]) && !pairs.contains([0, 3]))
97}
98
9099@Test func cyclicOrderThrows() {
91100 #expect(throws: LayerError.cyclic(cycle: [0, 1, 2])) {
92101 try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: up3)