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