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 @@ |
| 1 | 1 | import Accelerate |
| 2 | 2 | import simd |
| 3 | import Synchronization |
| 3 | 4 | |
| 4 | 5 | /// As-rigid-as-possible interpolation on a render mesh between two rigid poses of the FOLD faces |
| 5 | 6 | /// (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 { |
| 99 | 100 | return simd_double3x3(columns: (along, cross(normal, along), normal)) |
| 100 | 101 | } |
| 101 | 102 | |
| 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. |
| 104 | 105 | private final class SparseCholesky: @unchecked Sendable { |
| 105 | 106 | private let factor: SparseOpaqueFactorization_Double |
| 106 | 107 | private let size: Int |
| 108 | private let lock = Mutex(()) |
| 107 | 109 | |
| 108 | 110 | /// `lower` holds the entries on and below the diagonal, keyed by (row, column). |
| 109 | 111 | init(size: Int, lower: [SIMD2<Int>: Double]) { |
| @@ -127,7 +129,7 @@ private final class SparseCholesky: @unchecked Sendable { |
| 127 | 129 | values.withUnsafeMutableBufferPointer { buffer in |
| 128 | 130 | let b = DenseMatrix_Double(rowCount: Int32(size), columnCount: Int32(columns), columnStride: Int32(size), |
| 129 | 131 | attributes: SparseAttributes_t(), data: buffer.baseAddress!) |
| 130 | | SparseSolve(factor, b) |
| 132 | lock.withLock { _ in SparseSolve(factor, b) } |
| 131 | 133 | } |
| 132 | 134 | } |
| 133 | 135 | } |
Packages/FoldKit/Sources/Folding/FoldStep.swift
+9
| @@ -96,9 +96,18 @@ public struct FoldStep: Equatable, Sendable { |
| 96 | 96 | self.viewpoint = viewpoint |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | /// The faces the step's axes move. A non-rigid step's pop holds the other faces still. |
| 99 | 100 | public var movingFaceUnion: Set<Int> { |
| 100 | 101 | axes.reduce(into: Set<Int>()) { $0.formUnion($1.movingFaces) } |
| 101 | 102 | } |
| 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 | } |
| 102 | 111 | } |
| 103 | 112 | |
| 104 | 113 | public 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] { |
| 43 | 43 | let size = sheetExtent(file) |
| 44 | 44 | let pop = step.nonRigid ? step.pop : nil |
| 45 | 45 | 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 |
| 47 | 47 | let interpolator = FoldInterpolator(file: file, step: step) |
| 48 | 48 | let check = { (frame: FoldFrame, axes: [FoldAxis], departure: Bool) in |
| 49 | 49 | 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 |
| 28 | 28 | /// The stacking order of faces in one state of a step, kept as raw `faceOrders` triples so that |
| 29 | 29 | /// each is read against its `g`'s normal wherever the faces are. |
| 30 | 30 | public struct LayerOrder: Sendable { |
| 31 | | private struct Triple: Sendable { |
| 31 | struct Triple: Sendable { |
| 32 | 32 | let f: Int, g: Int, s: Int |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | | private let triples: [Triple] |
| 35 | let triples: [Triple] |
| 36 | 36 | /// Each pair's relation in the form `rigidInvariantFaceOrders` gives, from the source that |
| 37 | 37 | /// supplied the pair. |
| 38 | 38 | private let invariant: [SIMD2<Int>: Int] |
| @@ -77,7 +77,7 @@ public struct LayerOrder: Sendable { |
| 77 | 77 | /// |
| 78 | 78 | /// A triple whose `g` moves in the step and whose `f` does not is replaced, with its source's |
| 79 | 79 | /// 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`. |
| 81 | 81 | public static func active(for step: FoldStep, in file: FoldFile) throws -> LayerOrder { |
| 82 | 82 | let start = file.frames[step.index - 1], end = file.frames[step.index] |
| 83 | 83 | let first: (faceOrders: [[Int]], normals: [SIMD3<Double>]) |
| @@ -90,7 +90,7 @@ public struct LayerOrder: Sendable { |
| 90 | 90 | } else { |
| 91 | 91 | first = (end.faceOrders, faceNormals(end, file)) |
| 92 | 92 | } |
| 93 | | let moving = step.movingFaceUnion |
| 93 | let moving = step.allMovingFaces |
| 94 | 94 | let sources = [first, (start.faceOrders, faceNormals(start, file))].map { source in |
| 95 | 95 | (faceOrders: source.faceOrders.map { t -> [Int] in |
| 96 | 96 | 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 |
| 117 | 117 | #expect(structurallyEqual(nudged(by: 1e-13), file, tolerance: 1e-12)) |
| 118 | 118 | #expect(!structurallyEqual(nudged(by: 1e-9), file, tolerance: 1e-12)) |
| 119 | 119 | } |
| 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 @@ |
| 1 | 1 | import Testing |
| 2 | 2 | import simd |
| 3 | 3 | import FoldFormat |
| 4 | | import Folding |
| 4 | @testable import Folding |
| 5 | 5 | import FoldKitTestSupport |
| 6 | 6 | |
| 7 | 7 | let up2 = [SIMD3<Double>(0, 0, 1), SIMD3(0, 0, 1)] |
| @@ -87,6 +87,15 @@ let up3 = up2 + [SIMD3(0, 0, 1)] |
| 87 | 87 | #expect(c.map(\.faces) == [[1, 0]]) |
| 88 | 88 | } |
| 89 | 89 | |
| 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 | |
| 90 | 99 | @Test func cyclicOrderThrows() { |
| 91 | 100 | #expect(throws: LayerError.cyclic(cycle: [0, 1, 2])) { |
| 92 | 101 | try LayerOrder(faceOrders: [[0, 1, 1], [1, 2, 1], [2, 0, 1]], normals: up3) |