torcurve_rs/
lib.rs

1#![doc = include_str!("../README.md")]
2
3fn pinch(v: f64) -> f64 {
4    if v < 0.5 {
5        -v * v
6    } else {
7        v * v
8    }
9}
10/// a generalized, parameterized curve formula for generating lots of different curve shapes useful for easing, etc.
11///
12/// # Arguments
13/// * `x` - input that drives the curve. clamped to 0..=1
14/// * `a` - controls the start of the curve
15/// * `b` - controls the middle of the curve
16/// * `c` - pinches the tail of the curve
17///
18/// # Example
19/// ```
20/// use torcurve_rs::torcurve;
21/// fn run_code() {
22///     for i in 0..=10 {
23///          println!("curve {}", torcurve(i as f64 * 0.1, 3.0, 0.0, 0.0));
24///     }
25/// }
26/// ```
27///
28/// note: implemented using <https://jsfiddle.net/torcado194/5ocmt48a/latest> as reference,
29/// the js fiddle may be useful to tune the parameters
30///
31pub fn torcurve(x: f64, a: f64, b: f64, c: f64) -> f64 {
32    let c = pinch(c);
33    let x = x.clamp(0.0, 1.0); //clamp input to [0-1], behavior is undefined otherwise
34    let s = a.exp(); //could be any exponential like 2^a or 3^a, or just linear
35    let s2 = 1.0 / s;
36    let t = b.clamp(0.0, 1.0);
37    let u = c; //should normally be clamped but creates possibly useful results outside of the 0-1 range
38    let eps = 0.00001; //protect against div/0
39
40    let (c1, c2, c3) = if x < t {
41        // c1 = (t*x)/(x+s*(t-x)+eps);
42        let c1 = (t * x) / (x + s * (t - x) + eps);
43        //  c2 = t-Math.pow(1/(t+eps), s2-1)*Math.pow(Math.abs(x-t), s2);
44        let c2 = t - (1.0 / ((t + eps).powf(s2 - 1.0)) * (x - t).abs().powf(s2));
45        // c3 = Math.pow(1/(t+eps), s-1)*Math.pow(x,s);
46        let c3 = (1.0 / (t + eps)).powf(s - 1.0) * x.powf(s);
47        (c1, c2, c3)
48    } else {
49        // c1 = (1-t)*(x-1)/(1-x-s*(t-x)+eps)+1;
50        let c1 = (1.0 - t) * (x - 1.0) / (1.0 - x - s * (t - x) + eps) + 1.0;
51        // c2 = Math.pow(1/((1-t)+eps), s2-1)*Math.pow(Math.abs(x-t), s2)+t;
52        let c2 = (1.0 / ((1.0 - t) + eps)).powf(s2 - 1.0) * (x - t).abs().powf(s2) + t;
53        // c3 = 1-Math.pow(1/((1-t)+eps), s-1)*Math.pow(1-x,s);
54        let c3 = 1.0 - (1.0 / ((1.0 - t) + eps)).powf(s - 1.0) * (1.0 - x).powf(s);
55        (c1, c2, c3)
56    };
57    let res = if u <= 0.0 {
58        (-u) * c2 + (1.0 + u) * c1
59    } else {
60        (u) * c3 + (1.0 - u) * c1
61    };
62    if res.is_nan() {
63        0.0
64    } else {
65        res
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use crate::torcurve;
72
73    #[test]
74    fn it_works() {
75        // Sampled data from the javascript implementation
76        let test_samples = vec![
77            0.0,
78            0.09999800003999922,
79            0.19999600007999843,
80            0.29999400011999766,
81            0.39999200015999686,
82            0.5000099998000039,
83            0.6000079998400032,
84            0.7000059998800024,
85            0.8000039999200016,
86            0.9000019999600009,
87            1.0,
88        ];
89        for (i, test_item) in test_samples.iter().enumerate() {
90            assert_eq!(torcurve(i as f64 * 0.1, 0.0, 0.5, 0.0), *test_item);
91        }
92    }
93}