| @@ -4,19 +4,34 @@ import Synchronization |
| 4 | 4 | |
| 5 | 5 | /// As-rigid-as-possible interpolation on a render mesh between two rigid poses of the FOLD faces |
| 6 | 6 | /// (decision 0002's pop). At `s` each triangle's edges are its edges in the first pose turned by |
| 7 | | /// `s` of its face's rotation from the first pose to the second (slerp), and the mesh vertices |
| 8 | | /// are the least-squares fit to those edges, with the vertices of held faces kept where the first |
| 9 | | /// pose has them. At `s = 0` and `s = 1` the fit is exact, so it reproduces both poses. |
| 7 | /// its target, and the mesh vertices are the least-squares fit to those edges, with the vertices of |
| 8 | /// held faces kept where the first pose has them. A triangle's target is `s` of its face's |
| 9 | /// rotation from the first pose to the second (slerp); for a triangle in `blends` (decision 0003's |
| 10 | /// band) it is slerp(mid, own, w), where own is that slerp, mid = slerp(own, other, ½), other is |
| 11 | /// the slerp of the partner face's rotation, and w = weight + (1 − weight)(1 − 4s(1 − s)). At |
| 12 | /// `s = 0` and `s = 1` every target is the face's own rotation and the fit is exact, so it |
| 13 | /// reproduces both poses. |
| 10 | 14 | struct Bending: Sendable { |
| 15 | /// A triangle whose turn is blended toward its partner face's. |
| 16 | struct Blend: Sendable { |
| 17 | let triangle: Int |
| 18 | let partner: Int |
| 19 | /// 1 where the triangle keeps its own turn throughout. |
| 20 | let weight: Double |
| 21 | } |
| 22 | |
| 11 | 23 | /// The mesh vertices in the first pose. |
| 12 | 24 | private let rest: [SIMD3<Double>] |
| 13 | 25 | /// Per FOLD face, its rotation from the first pose to the second. |
| 14 | 26 | private let turns: [simd_quatd] |
| 27 | /// The blended triangles' faces, partners and weights; the k-th takes target `turns.count + k`. |
| 28 | private let blends: [(face: Int, partner: Int, weight: Double)] |
| 15 | 29 | /// The mesh vertices that move, in the solve's row order. |
| 16 | 30 | private let free: [Int] |
| 17 | | /// Per row and FOLD face, the signed sum of the rest edge vectors of that face's triangles at |
| 18 | | /// the row's vertex; the right-hand side adds each turned by `s` of the face's turn. |
| 19 | | private let terms: [(row: Int, face: Int, vector: SIMD3<Double>)] |
| 31 | /// Per row and target, the signed sum of the rest edge vectors of that target's triangles at |
| 32 | /// the row's vertex; the right-hand side adds each turned by the target at `s`. Targets are the |
| 33 | /// FOLD faces, then the blended triangles. |
| 34 | private let terms: [(row: Int, target: Int, vector: SIMD3<Double>)] |
| 20 | 35 | /// Per row, what the held vertices contribute to the normal equation's right-hand side. |
| 21 | 36 | private let held: [SIMD3<Double>] |
| 22 | 37 | private let factor: SparseCholesky |
| @@ -24,8 +39,9 @@ struct Bending: Sendable { |
| 24 | 39 | /// - Parameters: |
| 25 | 40 | /// - first, second: the FOLD vertex positions of two rigid poses. |
| 26 | 41 | /// - heldFaces: FOLD faces placed alike in both poses, whose mesh vertices stay put. |
| 42 | /// - blends: triangles whose turns blend toward a partner face's. |
| 27 | 43 | init(mesh: RenderMesh, faces: [[Int]], from first: [SIMD3<Double>], to second: [SIMD3<Double>], |
| 28 | | heldFaces: Set<Int>) { |
| 44 | heldFaces: Set<Int>, blends: [Blend] = []) { |
| 29 | 45 | rest = mesh.pose(first) |
| 30 | 46 | turns = faces.map { corners in |
| 31 | 47 | // one corner triple for both poses, chosen on the flat sheet so ties cannot split |
| @@ -35,6 +51,9 @@ struct Bending: Sendable { |
| 35 | 51 | if q.real < 0 { q = simd_quatd(vector: -q.vector) } |
| 36 | 52 | return q |
| 37 | 53 | } |
| 54 | self.blends = blends.map { (mesh.faceOf[$0.triangle], $0.partner, $0.weight) } |
| 55 | var target = mesh.faceOf |
| 56 | for (k, blend) in blends.enumerated() { target[blend.triangle] = faces.count + k } |
| 38 | 57 | |
| 39 | 58 | var isHeld = Array(repeating: false, count: mesh.vertices.count) |
| 40 | 59 | for (t, (a, b, c)) in mesh.triangles.enumerated() where heldFaces.contains(mesh.faceOf[t]) { |
| @@ -50,12 +69,11 @@ struct Bending: Sendable { |
| 50 | 69 | var sums: [SIMD2<Int>: SIMD3<Double>] = [:] |
| 51 | 70 | var held = Array(repeating: SIMD3<Double>.zero, count: free.count) |
| 52 | 71 | for (t, (a, b, c)) in mesh.triangles.enumerated() { |
| 53 | | let face = mesh.faceOf[t] |
| 54 | 72 | for (i, j) in [(a, b), (b, c), (c, a)] { |
| 55 | 73 | let d = rest[j] - rest[i] |
| 56 | 74 | for (v, other, sign) in [(i, j, -1.0), (j, i, 1.0)] where row[v] >= 0 { |
| 57 | 75 | matrix[SIMD2(row[v], row[v]), default: 0] += 1 |
| 58 | | sums[SIMD2(row[v], face), default: .zero] += sign * d |
| 76 | sums[SIMD2(row[v], target[t]), default: .zero] += sign * d |
| 59 | 77 | if row[other] >= 0 { |
| 60 | 78 | if row[other] < row[v] { matrix[SIMD2(row[v], row[other]), default: 0] -= 1 } |
| 61 | 79 | } else { |
| @@ -72,7 +90,12 @@ struct Bending: Sendable { |
| 72 | 90 | /// The mesh vertex positions at `s` in 0...1. |
| 73 | 91 | func positions(at s: Double) -> [SIMD3<Double>] { |
| 74 | 92 | let identity = simd_quatd(ix: 0, iy: 0, iz: 0, r: 1) |
| 75 | | let turned = turns.map { simd_slerp(identity, $0, s) } |
| 93 | var turned = turns.map { simd_slerp(identity, $0, s) } |
| 94 | let middle = 1 - 4 * s * (1 - s) |
| 95 | turned += blends.map { face, partner, weight in |
| 96 | let own = turned[face] |
| 97 | return simd_slerp(simd_slerp(own, turned[partner], 0.5), own, weight + (1 - weight) * middle) |
| 98 | } |
| 76 | 99 | let n = free.count |
| 77 | 100 | var rhs = [Double](repeating: 0, count: 3 * n) |
| 78 | 101 | for (r, h) in held.enumerated() { |
| @@ -80,8 +103,8 @@ struct Bending: Sendable { |
| 80 | 103 | rhs[n + r] = h.y |
| 81 | 104 | rhs[2 * n + r] = h.z |
| 82 | 105 | } |
| 83 | | for (r, face, vector) in terms { |
| 84 | | let d = turned[face].act(vector) |
| 106 | for (r, target, vector) in terms { |
| 107 | let d = turned[target].act(vector) |
| 85 | 108 | rhs[r] += d.x |
| 86 | 109 | rhs[n + r] += d.y |
| 87 | 110 | rhs[2 * n + r] += d.z |