spikes/interpolation/Sources/Spike/Run.swift

75 lines · 2917 bytes

8 symbols in this file
 1import simd
 2
 3/// The geometry's fold angles, unwrapped against the previous sample (or, on
 4/// the first, against `ref`), so a crease that keeps turning past ±180 reads
 5/// as beyond the range: one face has gone through the other at the hinge.
 6func unwrap(_ m: [Double], toward ref: [Double]) -> [Double] {
 7    zip(m, ref).map { x, r in x + 360 * ((r - x) / 360).rounded() }
 8}
 9
10struct Sweep {
11    var maxGap = 0.0
12    var firstCollision: Double?
13    var samples: [(t: Double, angles: [Double])] = []
14    var end: [Double] { samples.last!.angles }
15    func angle(_ e: Int, at t: Double) -> Double {
16        samples.min { abs($0.t - t) < abs($1.t - t) }!.angles[e]
17    }
18    func range(_ e: Int) -> (Double, Double) {
19        let xs = samples.map { $0.angles[e] }
20        return (xs.min()!, xs.max()!)
21    }
22}
23
24/// Every angle prescribed as a function of t: no solving, so any tearing shows as gap.
25func prescribed(_ k: Kinematics, steps: Int = 400, collisions: Bool = true, _ angles: (Double) -> [Double]) -> Sweep {
26    var s = Sweep()
27    for i in 0...steps {
28        let t = Double(i) / Double(steps)
29        let a = angles(t)
30        s.maxGap = max(s.maxGap, k.gap(a))
31        if collisions, s.firstCollision == nil, !intersectingPairs(k.positions(a)).isEmpty { s.firstCollision = t }
32        s.samples.append((t, unwrap(k.measured(a), toward: s.samples.last?.angles ?? a)))
33    }
34    return s
35}
36
37/// Drivers prescribed, free angles solved so the paper stays whole; continued
38/// from the previous sample with a secant predictor. `seed` is the direction
39/// to leave the start in, for starts where several branches meet.
40func continuation(_ k: Kinematics, start: [Double], drivers: [Int: (Double) -> Double], free: [Int],
41                  seed: [Int: Double] = [:], steps: Int = 400, collisions: Bool = true) -> Sweep {
42    var s = Sweep()
43    var a = start
44    var last: [Double]? = nil
45    for i in 0...steps {
46        let t = Double(i) / Double(steps)
47        var guess = a
48        if let l = last {
49            for e in free { guess[e] += a[e] - l[e] }
50        } else if i == 1 {
51            for (e, d) in seed { guess[e] += d / Double(steps) }
52        }
53        for (e, f) in drivers { guess[e] = f(t) }
54        let g = k.solve(&guess, free: free)
55        s.maxGap = max(s.maxGap, g)
56        if collisions, s.firstCollision == nil, !intersectingPairs(k.positions(guess)).isEmpty { s.firstCollision = t }
57        s.samples.append((t, unwrap(k.measured(guess), toward: s.samples.last?.angles ?? start)))
58        if i > 0 { last = a }
59        a = guess
60    }
61    return s
62}
63
64func fmt(_ x: Double, _ digits: Int = 1) -> String {
65    let p = pow(10.0, Double(digits))
66    let r = (x * p).rounded() / p
67    return r == 0 ? "0" : String(r)
68}
69
70func sci(_ x: Double) -> String {
71    if x == 0 { return "0" }
72    let e = Int(floor(log10(abs(x))))
73    let m = x / pow(10.0, Double(e))
74    return "\(fmt(m, 2))e\(e)"
75}