| @@ -418,9 +418,11 @@ func sheetExtent(_ file: FoldFile) -> Double { |
| 418 | 418 | /// Holds the driver (the first axis in `record`) linear on its window and solves the other axes |
| 419 | 419 | /// on a loop's cycle, the tree path between the two sides of a loop crease, so the loops stay |
| 420 | 420 | /// closed: damped Gauss–Newton on the gap between vertex copies, continued along the window from |
| 421 | | /// the previous samples. Each solved axis takes the driver's window and a curve of 257 samples, or |
| 422 | | /// none when it is linear to within 1e-9°; the last sample is the end angle exactly. Axes on no |
| 423 | | /// cycle stay linear on their own windows. |
| 421 | /// the previous samples. Where closure leaves the solved axes free, each sample is then moved |
| 422 | /// toward the linear angles within those free directions (`LoopKinematics.settle`). Each solved |
| 423 | /// axis takes the driver's window and a curve of 257 samples, or none when it is linear to within |
| 424 | /// 1e-9°; the last sample is the end angle exactly. Axes on no cycle stay linear on their own |
| 425 | /// windows. |
| 424 | 426 | /// |
| 425 | 427 | /// - Throws: `solvedAxisWindowConflict` when a solved axis is scripted on another window. |
| 426 | 428 | func solveLoops(_ n: Int, _ record: StepRecord, _ step: FoldStep, loops: [Int], |
| @@ -451,6 +453,7 @@ func solveLoops(_ n: Int, _ record: StepRecord, _ step: FoldStep, loops: [Int], |
| 451 | 453 | } |
| 452 | 454 | guess[driver] = linear(step.axes[driver], u) |
| 453 | 455 | guard kinematics.solve(&guess, free: free) <= tolerance else { throw CompileError.noRigidMotion(step: n) } |
| 456 | kinematics.settle(&guess, toward: step.axes.map { linear($0, u) }, free: free, tolerance: tolerance) |
| 454 | 457 | samples.append(guess) |
| 455 | 458 | } |
| 456 | 459 | |
| @@ -650,16 +653,7 @@ struct LoopKinematics { |
| 650 | 653 | var cost = r.reduce(0) { $0 + $1 * $1 } |
| 651 | 654 | for _ in 0..<iterations { |
| 652 | 655 | if cost < 1e-26 { break } |
| 653 | | let h = 1e-6 |
| 654 | | var jacobian = Array(repeating: [Double](repeating: 0, count: free.count), count: r.count) |
| 655 | | for (k, a) in free.enumerated() { |
| 656 | | var probe = angles |
| 657 | | probe[a] += h |
| 658 | | let plus = residual(probe) |
| 659 | | probe[a] -= 2 * h |
| 660 | | let minus = residual(probe) |
| 661 | | for i in r.indices { jacobian[i][k] = (plus[i] - minus[i]) / (2 * h) } |
| 662 | | } |
| 656 | let jacobian = self.jacobian(angles, free: free) |
| 663 | 657 | var improved = false |
| 664 | 658 | for _ in 0..<20 { |
| 665 | 659 | var normal = Array(repeating: [Double](repeating: 0, count: free.count), count: free.count) |
| @@ -693,8 +687,92 @@ struct LoopKinematics { |
| 693 | 687 | } |
| 694 | 688 | return gap(angles) |
| 695 | 689 | } |
| 690 | |
| 691 | /// The residual's derivatives by the angles in `free`, by central differences: one row per |
| 692 | /// residual entry, one column per free axis. |
| 693 | func jacobian(_ angles: [Double], free: [Int]) -> [[Double]] { |
| 694 | let h = 1e-6 |
| 695 | var jacobian: [[Double]] = [] |
| 696 | for (k, a) in free.enumerated() { |
| 697 | var probe = angles |
| 698 | probe[a] += h |
| 699 | let plus = residual(probe) |
| 700 | probe[a] -= 2 * h |
| 701 | let minus = residual(probe) |
| 702 | if k == 0 { jacobian = Array(repeating: [Double](repeating: 0, count: free.count), count: plus.count) } |
| 703 | for i in plus.indices { jacobian[i][k] = (plus[i] - minus[i]) / (2 * h) } |
| 704 | } |
| 705 | return jacobian |
| 706 | } |
| 707 | |
| 708 | /// Moves the angles in `free` toward `target` within the null space of the closure Jacobian J, |
| 709 | /// by δ = ε(JᵀJ + εI)⁻¹(target − angles) three times, then solves again. ε is 1e-6 of JᵀJ's |
| 710 | /// largest eigenvalue, so δ takes the null space's part of the difference and almost none of |
| 711 | /// the rest. When JᵀJ has no eigenvalue below 1e-10 of its largest (closure fixes every free |
| 712 | /// axis, as at a vertex with one degree of freedom) or the solve does not close to `tolerance`, |
| 713 | /// `angles` is left as it was. |
| 714 | func settle(_ angles: inout [Double], toward target: [Double], free: [Int], tolerance: Double) { |
| 715 | var x = angles |
| 716 | for _ in 0..<3 { |
| 717 | let j = jacobian(x, free: free) |
| 718 | var normal = Array(repeating: [Double](repeating: 0, count: free.count), count: free.count) |
| 719 | for row in j { |
| 720 | for a in free.indices { |
| 721 | for b in free.indices { normal[a][b] += row[a] * row[b] } |
| 722 | } |
| 723 | } |
| 724 | let (values, vectors) = symmetricEigen(normal) |
| 725 | guard let largest = values.max(), largest > 0, values.contains(where: { $0 < 1e-10 * largest }) else { return } |
| 726 | let epsilon = 1e-6 * largest |
| 727 | let difference = free.map { target[$0] - x[$0] } |
| 728 | for (value, vector) in zip(values, vectors) { |
| 729 | let along = zip(vector, difference).reduce(0) { $0 + $1.0 * $1.1 } * epsilon / (value + epsilon) |
| 730 | for (k, a) in free.enumerated() { x[a] += along * vector[k] } |
| 731 | } |
| 732 | } |
| 733 | guard solve(&x, free: free) <= tolerance else { return } |
| 734 | angles = x |
| 735 | } |
| 696 | 736 | } |
| 697 | 737 | |
| 738 | /// The eigenvalues of a symmetric matrix and a unit eigenvector for each, by cyclic Jacobi |
| 739 | /// rotations. |
| 740 | private func symmetricEigen(_ matrix: [[Double]]) -> (values: [Double], vectors: [[Double]]) { |
| 741 | let n = matrix.count |
| 742 | var a = matrix |
| 743 | var v = (0..<n).map { i in (0..<n).map { $0 == i ? 1.0 : 0.0 } } |
| 744 | for _ in 0..<100 { |
| 745 | var off = 0.0 |
| 746 | for p in 0..<n { for q in (p + 1)..<n { off += a[p][q] * a[p][q] } } |
| 747 | let scale = (0..<n).reduce(0) { $0 + a[$1][$1] * a[$1][$1] } |
| 748 | if off <= 1e-30 * scale { break } |
| 749 | for p in 0..<n { |
| 750 | for q in (p + 1)..<n where a[p][q] != 0 { |
| 751 | let theta = (a[q][q] - a[p][p]) / (2 * a[p][q]) |
| 752 | let t = (theta >= 0 ? 1.0 : -1.0) / (abs(theta) + (theta * theta + 1).squareRoot()) |
| 753 | let c = 1 / (t * t + 1).squareRoot(), s = t * c |
| 754 | for k in 0..<n { |
| 755 | let (akp, akq) = (a[k][p], a[k][q]) |
| 756 | a[k][p] = c * akp - s * akq |
| 757 | a[k][q] = s * akp + c * akq |
| 758 | } |
| 759 | for k in 0..<n { |
| 760 | let (apk, aqk) = (a[p][k], a[q][k]) |
| 761 | a[p][k] = c * apk - s * aqk |
| 762 | a[q][k] = s * apk + c * aqk |
| 763 | } |
| 764 | for k in 0..<n { |
| 765 | let (vkp, vkq) = (v[k][p], v[k][q]) |
| 766 | v[k][p] = c * vkp - s * vkq |
| 767 | v[k][q] = s * vkp + c * vkq |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | return ((0..<n).map { a[$0][$0] }, (0..<n).map { i in (0..<n).map { v[$0][i] } }) |
| 773 | } |
| 774 | |
| 775 | |
| 698 | 776 | /// Gaussian elimination with partial pivoting; nil when `a` is singular. |
| 699 | 777 | private func solveLinear(_ a: [[Double]], _ b: [Double]) -> [Double]? { |
| 700 | 778 | var m = a, x = b |