1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Simple easing functions
//!
//! All functions have the signature `fn<F: Float>(F) -> F` (letting you use `f32`, `f64`, or any other type that implements [`num_traits`'s `Float`](https://docs.rs/num-traits/0.2.0/num_traits/float/trait.Float.html)). Input should range from `0.0` to `1.0`, and output is generally in the `0.0` to `1.0` range (except for `elastic` and `back`, which return values slightly outside). `0.0` always maps to `0.0`, and `1.0` always maps to `1.0`.

#![cfg_attr(test, feature(concat_idents))]

extern crate num_traits;
use num_traits::Float;

use std::f64::consts::{PI,FRAC_PI_2};

fn lit<F: Float>(f: f64) -> F {
  F::from(f).unwrap()
}

// Linear

#[inline]
pub fn linear<F: Float>(t: F) -> F {
  t
}

// Quadratic

#[inline]
pub fn quad_in<F: Float>(t: F) -> F {
  t * t
}

#[inline]
pub fn quad_out<F: Float>(t: F) -> F {
  -t * (t - lit::<F>(2.0))
}

#[inline]
pub fn quad_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(2.0) * t * t
  } else {
    (lit::<F>(-2.0) * t * t) + (lit::<F>(4.0) * t) - lit::<F>(1.0)
  }
}

// Cubic

#[inline]
pub fn cubic_in<F: Float>(t: F) -> F {
  t * t * t
}

#[inline]
pub fn cubic_out<F: Float>(t: F) -> F {
  let f = t - lit::<F>(1.0);
  f * f * f + lit::<F>(1.0)
}

#[inline]
pub fn cubic_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(4.0) * t * t * t
  } else {
    let f = (lit::<F>(2.0) * t) - lit::<F>(2.0);
    lit::<F>(0.5) * f * f * f + lit::<F>(1.0)
  }
}

// Quartic

#[inline]
pub fn quart_in<F: Float>(t: F) -> F {
  t * t * t * t
}

#[inline]
pub fn quart_out<F: Float>(t: F) -> F {
  let f = t - lit::<F>(1.0);
  f * f * f * (lit::<F>(1.0) - t) + lit::<F>(1.0)
}

#[inline]
pub fn quart_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(8.0) * t * t * t * t
  } else {
    let f = t - lit::<F>(1.0);
    lit::<F>(-8.0) * f * f * f * f + lit::<F>(1.0)
  }
}

// Quintic

#[inline]
pub fn quint_in<F: Float>(t: F) -> F {
  t * t * t * t * t
}

#[inline]
pub fn quint_out<F: Float>(t: F) -> F {
  let f = t - lit::<F>(1.0);
  f * f * f * f * f + lit::<F>(1.0)
}

#[inline]
pub fn quint_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(16.0) * t * t * t * t * t
  } else {
    let f = (lit::<F>(2.0) * t) - lit::<F>(2.0);
    lit::<F>(0.5) * f * f * f * f * f + lit::<F>(1.0)
  }
}

// Sine

#[inline]
pub fn sine_in<F: Float>(t: F) -> F {
  ((t - lit::<F>(1.0)) * lit::<F>(FRAC_PI_2)).sin() + lit::<F>(1.0)
}

#[inline]
pub fn sine_out<F: Float>(t: F) -> F {
  (t * lit::<F>(FRAC_PI_2)).sin()
}

#[inline]
pub fn sine_inout<F: Float>(t: F) -> F {
  lit::<F>(0.5) * (lit::<F>(1.0) - (t * lit::<F>(PI)).cos())
}

// Circular

#[inline]
pub fn circ_in<F: Float>(t: F) -> F {
  lit::<F>(1.0) - (lit::<F>(1.0) - t * t).sqrt()
}

#[inline]
pub fn circ_out<F: Float>(t: F) -> F {
  ((lit::<F>(2.0) - t) * t).sqrt()
}

#[inline]
pub fn circ_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(0.5) * (lit::<F>(1.0) - (lit::<F>(1.0) - lit::<F>(4.0) * t * t).sqrt())
  } else {
    lit::<F>(0.5) * ((-(lit::<F>(2.0) * t - lit::<F>(3.0)) * (lit::<F>(2.0) * t - lit::<F>(1.0))).sqrt() + lit::<F>(1.0))
  }
}

// Exponential

#[inline]
pub fn expo_in<F: Float>(t: F) -> F {
  if t == lit::<F>(0.0) {
    lit::<F>(0.0)
  } else {
    lit::<F>(2.0).powf(lit::<F>(10.0) * (t - lit::<F>(1.0)))
  }
}


#[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
#[inline]
pub fn expo_out<F: Float>(t: F) -> F {
  if t == lit::<F>(1.0) {
    lit::<F>(1.0)
  } else {
    lit::<F>(1.0) - lit::<F>(2.0).powf(lit::<F>(-10.0) * t)
  }
}

#[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
#[inline]
pub fn expo_inout<F: Float>(t: F) -> F {
  if t == lit::<F>(0.0) {
    lit::<F>(0.0)
  } else if t == lit::<F>(1.0) {
    lit::<F>(1.0)
  } else if t < lit::<F>(0.5) {
    lit::<F>(0.5) * lit::<F>(2.0).powf(lit::<F>(20.0) * t - lit::<F>(10.0))
  } else {
    lit::<F>(-0.5) * lit::<F>(2.0).powf(lit::<F>(-20.0) * t + lit::<F>(10.0)) + lit::<F>(1.0)
  }
}

// Elastic

#[inline]
pub fn elastic_in<F: Float>(t: F) -> F {
  (lit::<F>(13.0) * lit::<F>(FRAC_PI_2) * t).sin() * lit::<F>(2.0).powf(lit::<F>(10.0) * (t - lit::<F>(1.0)))
}

#[inline]
pub fn elastic_out<F: Float>(t: F) -> F {
  (lit::<F>(-13.0) * lit::<F>(FRAC_PI_2) * (t + lit::<F>(1.0))).sin() * lit::<F>(2.0).powf(lit::<F>(-10.0) * t) + lit::<F>(1.0)
}

#[inline]
pub fn elastic_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(0.5) * (lit::<F>(13.0) * lit::<F>(FRAC_PI_2) * lit::<F>(2.0) * t).sin() * lit::<F>(2.0).powf(lit::<F>(10.0) * (lit::<F>(2.0) * t - lit::<F>(1.0)))
  } else {
    lit::<F>(0.5) * ((lit::<F>(-13.0) * lit::<F>(FRAC_PI_2) * lit::<F>(2.0) * t).sin() * (lit::<F>(2.0)).powf(lit::<F>(-10.0) * (lit::<F>(2.0) * t - lit::<F>(1.0))) + lit::<F>(2.0))
  }
}

// Back

#[inline]
pub fn back_in<F: Float>(t: F) -> F {
  t * t * t - t * (t * lit::<F>(PI)).sin()
}

#[inline]
pub fn back_out<F: Float>(t: F) -> F {
  let f = lit::<F>(1.0) - t;
  lit::<F>(1.0) - f * f * f + f * (f * lit::<F>(PI)).sin()
}

#[inline]
pub fn back_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    let f = lit::<F>(2.0) * t;
    lit::<F>(0.5) * (f * f * f - f * (f * lit::<F>(PI)).sin())
  } else {
    let f = lit::<F>(2.0) - lit::<F>(2.0) * t;
    lit::<F>(0.5) * (lit::<F>(1.0) - (f * f * f - f * (f * lit::<F>(PI)).sin())) + lit::<F>(0.5)
  }
}

// Bounce

#[inline]
pub fn bounce_in<F: Float>(t: F) -> F {
  lit::<F>(1.0) - bounce_out(lit::<F>(1.0) - t)
}

#[inline]
pub fn bounce_out<F: Float>(t: F) -> F {
  if t < lit::<F>(4.0 / 11.0) {
    lit::<F>(121.0 / 16.0) * t * t
  } else if t < lit::<F>(8.0 / 11.0) {
    lit::<F>(363.0 / 40.0) * t * t - lit::<F>(99.0 / 10.0) * t + lit::<F>(17.0 / 5.0)
  } else if t < lit::<F>(9.0 / 10.0) {
    lit::<F>(4356.0 / 361.0) * t * t - lit::<F>(35442.0 / 1805.0) * t + lit::<F>(16061.0 / 1805.0)
  } else {
    lit::<F>(54.0 / 5.0) * t * t - lit::<F>(513.0 / 25.0) * t + lit::<F>(268.0 / 25.0)
  }
}

#[inline]
pub fn bounce_inout<F: Float>(t: F) -> F {
  if t < lit::<F>(0.5) {
    lit::<F>(0.5) * bounce_in(t * lit::<F>(2.0))
  } else {
    lit::<F>(0.5) * bounce_out(t * lit::<F>(2.0) - lit::<F>(1.0)) + lit::<F>(0.5)
  }
}


#[cfg(test)]
mod tests {
  use num_traits::Float;
  use super::lit;

  fn assert_float_eq<A: Float, B: Float>(a: A, b: B) {
    let a = a.to_f64().unwrap();
    let b = b.to_f64().unwrap();

    assert!(a - b < 0.00001, "a = {}, b = {}", a, b);
  }

  macro_rules! tests {
    ($name:ident) => {
      mod $name{
        use super::*;
        use super::super::*;

        #[test] fn in32() { test::<f32, _>(concat_idents!($name, _in)); }
        #[test] fn in64() { test::<f64, _>(concat_idents!($name, _in)); }
        #[test] fn out32() { test::<f32, _>(concat_idents!($name, _out)); }
        #[test] fn out64() { test::<f64, _>(concat_idents!($name, _out)); }
        #[test] fn inout32() { test::<f32, _>(concat_idents!($name, _inout)); }
        #[test] fn inout64() { test::<f64, _>(concat_idents!($name, _inout)); }
        #[test] fn trio32() { test_trio::<f32, _, _, _>(concat_idents!($name, _in),
                                                        concat_idents!($name, _out),
                                                        concat_idents!($name, _inout)); }
        #[test] fn trio64() { test_trio::<f64, _, _, _>(concat_idents!($name, _in),
                                                        concat_idents!($name, _out),
                                                        concat_idents!($name, _inout)); }
      }
    }
  }

  fn test<T: Float, F>(f: F)
    where F: Fn(T) -> T {
    assert_float_eq(f(lit::<T>(0.0)), 0.0);
    assert_float_eq(f(lit::<T>(1.0)), 1.0);
  }

  fn test_trio<T: Float, FIN, FOUT, FINOUT>(fin: FIN, fout: FOUT, finout: FINOUT)
    where FIN: Fn(T) -> T,
          FOUT: Fn(T) -> T,
          FINOUT: Fn(T) -> T {

    let n = 99;

    for i in 0..n+1 {
      let t = T::from(i).unwrap() / T::from(n).unwrap();
      println!("{}", t.to_f32().unwrap());

      assert_float_eq::<T, T>(fin(t), lit::<T>(1.0) - (fout(lit::<T>(1.0) - t)));

      if t < lit(0.5) {
        assert_float_eq(finout(t), fin(t*lit::<T>(2.0)) / lit::<T>(2.0));
      } else {
        assert_float_eq(finout(t), fout((t-lit::<T>(0.5))*lit::<T>(2.0)) / lit::<T>(2.0) + lit::<T>(0.5));
      }
    }
  }

  tests!(quad);
  tests!(cubic);
  tests!(quart);
  tests!(quint);
  tests!(sine);
  tests!(circ);
  tests!(expo);
  tests!(elastic);
  tests!(back);
  tests!(bounce);
  #[test]
  fn test_linear() {
    test::<f32, _>(super::linear);
    test::<f64, _>(super::linear);
  }
}