use std::f64::consts::PI;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TrickCycle {
pub slot: f64,
pub play: f64,
}
impl TrickCycle {
pub fn at(&self, t: f64) -> (u32, Option<f64>) {
let f = t / self.slot.max(1e-6);
let slot = f.floor();
let u = (f - slot) / self.play.clamp(1e-6, 1.0);
(slot as i64 as u32, (u < 1.0).then_some(u.max(0.0)))
}
}
pub fn slot_hash(seed: u32, slot: u32) -> u32 {
let mut h = seed.wrapping_mul(0x9E37_79B1) ^ slot.wrapping_mul(0x85EB_CA77);
h ^= h >> 16;
h = h.wrapping_mul(0x7FEB_352D);
h ^= h >> 15;
h = h.wrapping_mul(0x846C_A68B);
h ^ (h >> 16)
}
pub fn back_in_out(u: f64, s: f64) -> f64 {
let s = s * 1.525;
let u = u.clamp(0.0, 1.0);
if u < 0.5 {
let v = 2.0 * u;
v * v * ((s + 1.0) * v - s) / 2.0
} else {
let v = 2.0 * u - 2.0;
(v * v * ((s + 1.0) * v + s) + 2.0) / 2.0
}
}
pub fn smooth(u: f64) -> f64 {
let u = u.clamp(0.0, 1.0);
u * u * (3.0 - 2.0 * u)
}
pub fn breathe(u: f64, depth: f64) -> f64 {
let u = u.clamp(0.0, 1.0);
if u < 0.6 {
1.0 - depth * smooth((PI * u / 0.6).sin())
} else {
1.0 + depth * 0.35 * (PI * (u - 0.6) / 0.4).sin()
}
}
pub fn turn_delta(a: f64, b: f64, fold: u32) -> f64 {
let p = 2.0 * PI / fold.max(1) as f64;
let d = b - a;
d - p * (d / p).round()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tricks_start_and_end_at_rest() {
for s in [0.0, 0.6, 1.1] {
assert!(back_in_out(0.0, s).abs() < 1e-12 && (back_in_out(1.0, s) - 1.0).abs() < 1e-12);
}
assert!(back_in_out(0.1, 1.0) < 0.0, "pulls back before it goes");
assert!((breathe(1.0, 0.3) - 1.0).abs() < 1e-9);
assert!(breathe(0.8, 0.3) > 1.0, "rebounds past rest");
}
#[test]
fn slots_and_hash() {
let c = TrickCycle { slot: 1.25, play: 1.0 };
assert_eq!(c.at(0.0), (0, Some(0.0)));
assert_eq!(c.at(2.5).0, 2);
assert_ne!(slot_hash(7, 1), slot_hash(7, 2));
assert_ne!(slot_hash(7, 1), slot_hash(8, 1));
assert_eq!([slot_hash(7, 0), slot_hash(7, 1), slot_hash(7, 12345)], [1431726239, 3423267222, 602759169]);
}
#[test]
fn symmetric_turns_take_the_short_way() {
use std::f64::consts::PI;
assert!((turn_delta(0.0, PI / 2.0 + 0.1, 4) - 0.1).abs() < 1e-9);
assert!(turn_delta(0.0, -3.0 * PI + 0.2, 4).abs() <= PI / 4.0 + 1e-9);
assert!((turn_delta(1.0, 1.0 + 4.0 * PI / 2.0, 4)).abs() < 1e-9);
}
}