import simd /// As-rigid-as-possible interpolation between rigid poses, on a mesh finer /// than the crease pattern. Each small triangle's rotation is interpolated /// (slerp) from one pose to the next, and the vertex positions that best fit /// those rotated edges are solved by least squares. Where the poses cannot be /// joined rigidly, the paper bends and stretches; the strain is the largest /// relative change in an edge's length. struct Bending { let coarse: Sheet let kinematics: Kinematics let fine: Sheet /// The coarse face each fine face lies in. let parent: [Int] let triangles: [(Int, Int, Int)] let pinned: [Int] let factor: [[Double]] // Cholesky factor of the normal matrix init(_ coarse: Sheet, _ kinematics: Kinematics, spacing: Double, creases: [(V2, V2)], pinFace: Int) { self.coarse = coarse self.kinematics = kinematics let xs = coarse.verts.map(\.x), ys = coarse.verts.map(\.y) var grid: [(V2, V2)] = [] var x = xs.min()! + spacing while x < xs.max()! - 1e-9 { grid.append((V2(x, ys.min()!), V2(x, ys.max()!))); x += spacing } var y = ys.min()! + spacing while y < ys.max()! - 1e-9 { grid.append((V2(xs.min()!, y), V2(xs.max()!, y))); y += spacing } let boundary = [V2(xs.min()!, ys.min()!), V2(xs.max()!, ys.min()!), V2(xs.max()!, ys.max()!), V2(xs.min()!, ys.max()!)] let fine = Sheet(boundary: boundary, creases: creases + grid) self.fine = fine let parent = fine.faces.map { f in let c = f.map { fine.verts[$0] }.reduce(V2.zero, +) / Double(f.count) return coarse.face(containing: c) } self.parent = parent var tris: [(Int, Int, Int)] = [] for f in fine.faces { for k in 1..<(f.count - 1) { tris.append((f[0], f[k], f[k + 1])) } } self.triangles = tris let pinnedSet = Set(fine.faces.indices.filter { parent[$0] == pinFace }.flatMap { fine.faces[$0] }) self.pinned = Array(pinnedSet) // Normal matrix of Σ |x_j − x_i − d_ij|² + w Σ |x_p − target|², one per coordinate. let n = fine.verts.count var A = Array(repeating: [Double](repeating: 0, count: n), count: n) for (a, b, c) in tris { for (i, j) in [(a, b), (b, c), (c, a)] { A[i][i] += 1; A[j][j] += 1; A[i][j] -= 1; A[j][i] -= 1 } } for p in pinnedSet { A[p][p] += 1e6 } self.factor = cholesky(A) } /// Fine-vertex positions of a rigid coarse pose. func pose(_ angles: [Double]) -> [V3] { let T = kinematics.transforms(angles) var out = [V3](repeating: .zero, count: fine.verts.count) for (f, face) in fine.faces.enumerated() { for v in face { out[v] = T[parent[f]].apply(V3(fine.verts[v], 0)) } } return out } /// Each coarse face's rotation in a pose. func rotations(_ angles: [Double]) -> [simd_quatd] { kinematics.transforms(angles).map(\.q) } /// Positions at s in 0…1 between two rigid poses. func interpolate(from a: [Double], to b: [Double], at s: Double) -> [V3] { let qa = rotations(a), qb = rotations(b) let rest = pose(a) let target = pose(b) let pinTarget = rest.indices.map { simd_mix(rest[$0], target[$0], V3(repeating: s)) } let n = fine.verts.count var rhs = [V3](repeating: .zero, count: n) var faceOfTriangle: [Int] = [] for (f, face) in fine.faces.enumerated() { for _ in 1..<(face.count - 1) { faceOfTriangle.append(f) } } for (t, (a0, b0, c0)) in triangles.enumerated() { let f = parent[faceOfTriangle[t]] var q1 = qb[f] * qa[f].inverse if q1.real < 0 { q1 = simd_quatd(vector: -q1.vector) } let q = simd_slerp(simd_quatd(ix: 0, iy: 0, iz: 0, r: 1), q1, s) for (i, j) in [(a0, b0), (b0, c0), (c0, a0)] { let d = q.act(rest[j] - rest[i]) rhs[j] += d; rhs[i] -= d } } for p in pinned { rhs[p] += 1e6 * pinTarget[p] } var out = [V3](repeating: .zero, count: n) for k in 0..<3 { let col = cholSolve(factor, rhs.map { $0[k] }) for i in 0.. Double { var worst = 0.0 for (a, b, c) in triangles { for (i, j) in [(a, b), (b, c), (c, a)] { if outside > 0, simd_distance((fine.verts[i] + fine.verts[j]) / 2, center) < outside { continue } let rest = simd_distance(fine.verts[i], fine.verts[j]) worst = max(worst, abs(simd_distance(x[i], x[j]) - rest) / rest) } } return worst } func triangleSoup(_ x: [V3]) -> [[V3]] { triangles.map { [x[$0.0], x[$0.1], x[$0.2]] } } } func cholesky(_ A: [[Double]]) -> [[Double]] { let n = A.count var L = Array(repeating: [Double](repeating: 0, count: n), count: n) for j in 0.. [Double] { let n = b.count var y = b for i in 0.. [V3] { let n = fine.verts.count var A = Array(repeating: [Double](repeating: 0, count: n), count: n) for (a, b, c) in triangles { for (i, j) in [(a, b), (b, c), (c, a)] { A[i][i] += 1; A[j][j] += 1; A[i][j] -= 1; A[j][i] -= 1 } } for (h, _) in handles { A[h][h] += 1e6 } let L = cholesky(A) let rest = fine.verts.map { V3($0, 0) } var x = x0 for _ in 0..