use rand::{rngs::StdRng, Rng};
use super::Sample;
pub(crate) fn bezier_samples(
p0: (f64, f64),
p1: (f64, f64),
p2: (f64, f64),
p3: (f64, f64),
samples: usize,
t_jitter: f64,
rng: &mut StdRng,
) -> Vec<Sample> {
let mut out = Vec::with_capacity(samples);
let step = 1.0 / (samples as f64).max(1.0);
let mut last_t = 0.0;
for i in 0..samples {
let nominal = (i as f64) * step;
let jitter = (rng.gen::<f64>() - 0.5) * 2.0 * t_jitter;
let t = (nominal + jitter).clamp(0.0, 1.0);
let omt = 1.0 - t;
let x = omt.powi(3) * p0.0
+ 3.0 * omt.powi(2) * t * p1.0
+ 3.0 * omt * t.powi(2) * p2.0
+ t.powi(3) * p3.0;
let y = omt.powi(3) * p0.1
+ 3.0 * omt.powi(2) * t * p1.1
+ 3.0 * omt * t.powi(2) * p2.1
+ t.powi(3) * p3.1;
let dt_ms = (5.0 + 20.0 * (t - last_t).abs() * (samples as f64)).clamp(3.0, 30.0) as u64;
last_t = t;
out.push(Sample { x, y, dt_ms });
}
out
}
pub(crate) fn catmull_rom_samples(
control: &[(f64, f64)],
samples: usize,
rng: &mut StdRng,
) -> Vec<Sample> {
let mut pts = control.to_vec();
while pts.len() < 4 {
pts.push(*pts.last().unwrap());
}
let mut out = Vec::with_capacity(samples);
let segments = pts.len() - 3;
let samples_per_seg = samples / segments.max(1);
for seg in 0..segments {
let (p0, p1, p2, p3) = (pts[seg], pts[seg + 1], pts[seg + 2], pts[seg + 3]);
for i in 0..samples_per_seg {
let t = (i as f64) / (samples_per_seg as f64);
let t2 = t * t;
let t3 = t2 * t;
let f0 = -0.5 * t3 + t2 - 0.5 * t;
let f1 = 1.5 * t3 - 2.5 * t2 + 1.0;
let f2 = -1.5 * t3 + 2.0 * t2 + 0.5 * t;
let f3 = 0.5 * t3 - 0.5 * t2;
let x = f0 * p0.0 + f1 * p1.0 + f2 * p2.0 + f3 * p3.0;
let y = f0 * p0.1 + f1 * p1.1 + f2 * p2.1 + f3 * p3.1;
let dt_ms = rng.gen_range(5u64..20u64);
out.push(Sample { x, y, dt_ms });
}
}
out
}
pub(crate) fn linear_jittered_samples(
from: (f64, f64),
to: (f64, f64),
samples: usize,
jitter_px: f64,
rng: &mut StdRng,
) -> Vec<Sample> {
let mut out = Vec::with_capacity(samples);
let dx = to.0 - from.0;
let dy = to.1 - from.1;
for i in 0..samples {
let t = (i as f64) / ((samples - 1).max(1) as f64);
let jx = (rng.gen::<f64>() - 0.5) * 2.0 * jitter_px;
let jy = (rng.gen::<f64>() - 0.5) * 2.0 * jitter_px;
let x = from.0 + dx * t + jx;
let y = from.1 + dy * t + jy;
let dt_ms = rng.gen_range(8u64..18u64);
out.push(Sample { x, y, dt_ms });
}
out
}
pub(crate) fn overshoot_samples(
target: (f64, f64),
distance_px: f64,
recovery_ms: u64,
rng: &mut StdRng,
) -> Vec<Sample> {
let angle: f64 = rng.gen_range(0.0..(2.0 * std::f64::consts::PI));
let overshot_x = target.0 + distance_px * angle.cos();
let overshot_y = target.1 + distance_px * angle.sin();
vec![
Sample {
x: overshot_x,
y: overshot_y,
dt_ms: 10,
},
Sample {
x: target.0,
y: target.1,
dt_ms: recovery_ms,
},
]
}
pub(crate) fn correction_samples(
from: (f64, f64),
to: (f64, f64),
samples: usize,
rng: &mut StdRng,
) -> Vec<Sample> {
linear_jittered_samples(from, to, samples, 1.0, rng)
}
pub(crate) fn dwell_samples(
at: (f64, f64),
duration_ms: u64,
micro_jitter_px: f64,
rng: &mut StdRng,
) -> Vec<Sample> {
let jx = (rng.gen::<f64>() - 0.5) * 2.0 * micro_jitter_px;
let jy = (rng.gen::<f64>() - 0.5) * 2.0 * micro_jitter_px;
vec![
Sample {
x: at.0 + jx,
y: at.1 + jy,
dt_ms: duration_ms / 2,
},
Sample {
x: at.0,
y: at.1,
dt_ms: duration_ms / 2,
},
]
}