use rand::Rng;
pub use super::mouse_driver::{HumanMouse, MousePersona};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Step {
pub dx: f32,
pub dy: f32,
pub dt_ms: u32,
}
#[derive(Debug, Clone)]
pub struct Trace {
pub steps: Vec<Step>,
}
impl Trace {
pub fn duration_ms(&self) -> u32 {
self.steps.iter().map(|s| s.dt_ms).sum()
}
pub fn arc_length(&self) -> f32 {
let mut total = 0.0f32;
for s in &self.steps {
total += (s.dx * s.dx + s.dy * s.dy).sqrt();
}
total
}
pub fn cumulative(&self) -> Vec<(f32, f32, u32)> {
let mut out = Vec::with_capacity(self.steps.len() + 1);
let mut x = 0.0f32;
let mut y = 0.0f32;
let mut t = 0u32;
out.push((x, y, t));
for s in &self.steps {
x += s.dx;
y += s.dy;
t = t.saturating_add(s.dt_ms);
out.push((x, y, t));
}
out
}
}
pub struct MouseSampler {
corpus: Vec<Trace>,
}
impl MouseSampler {
pub fn new() -> Self {
Self {
corpus: bundled_corpus(),
}
}
pub fn with_extra_traces(mut self, mut extra: Vec<Trace>) -> Self {
self.corpus.append(&mut extra);
self
}
pub fn sample(&self, x0: f32, y0: f32, x1: f32, y1: f32) -> Trace {
let mut rng = rand::rngs::StdRng::from_entropy_via_thread_local();
let chosen = crate::choice::random_item_with_rng(&self.corpus, &mut rng)
.cloned()
.unwrap_or_else(|| Trace {
steps: vec![Step {
dx: x1 - x0,
dy: y1 - y0,
dt_ms: 250,
}],
});
affine_transform_with_jitter(&chosen, (x0, y0), (x1, y1), &mut rng)
}
#[allow(clippy::too_many_arguments)] pub fn resampled_path(
&self,
x0: f64,
y0: f64,
x1: f64,
y1: f64,
n_points: usize,
jitter_px: f64,
rng: &mut impl Rng,
) -> Vec<(f64, f64)> {
let n = n_points.max(2);
let cumul: Vec<(f32, f32)> = match crate::choice::random_item_with_rng(&self.corpus, rng) {
Some(t) => t.cumulative().into_iter().map(|(x, y, _)| (x, y)).collect(),
None => vec![(0.0, 0.0), (1.0, 1.0)],
};
let m = cumul.len().max(2);
let last = cumul.last().copied().unwrap_or((1.0, 1.0));
let (ex, ey) = (f64::from(last.0), f64::from(last.1));
let jitter = jitter_px.abs();
let mut out = Vec::with_capacity(n);
for i in 0..n {
let s = i as f64 / (n - 1) as f64;
let fpos = s * (m - 1) as f64;
let lo = fpos.floor() as usize;
let hi = (lo + 1).min(m - 1);
let frac = fpos - lo as f64;
let a = cumul.get(lo).copied().unwrap_or((0.0, 0.0));
let b = cumul.get(hi).copied().unwrap_or(a);
let nx = f64::from(a.0) + (f64::from(b.0) - f64::from(a.0)) * frac;
let ny = f64::from(a.1) + (f64::from(b.1) - f64::from(a.1)) * frac;
let ux = if ex.abs() > 1e-9 { nx / ex } else { s };
let uy = if ey.abs() > 1e-9 { ny / ey } else { s };
let mut px = x0 + (x1 - x0) * ux;
let mut py = y0 + (y1 - y0) * uy;
if i != 0 && i != n - 1 && jitter > 0.0 {
px += rng.gen_range(-jitter..=jitter);
py += rng.gen_range(-jitter..=jitter);
}
out.push((px, py));
}
out[0] = (x0, y0);
out[n - 1] = (x1, y1);
out
}
pub fn corpus(&self) -> &[Trace] {
&self.corpus
}
}
impl Default for MouseSampler {
fn default() -> Self {
Self::new()
}
}
trait FromEntropyViaThreadLocal {
fn from_entropy_via_thread_local() -> Self;
}
impl FromEntropyViaThreadLocal for rand::rngs::StdRng {
fn from_entropy_via_thread_local() -> Self {
use rand::SeedableRng;
rand::rngs::StdRng::from_seed(rand::random())
}
}
fn affine_transform_with_jitter(
trace: &Trace,
start: (f32, f32),
end: (f32, f32),
rng: &mut impl Rng,
) -> Trace {
let cumul = trace.cumulative();
let final_xy = cumul.last().copied().unwrap_or((1.0, 1.0, 0));
let scale_x = if final_xy.0.abs() < 1e-6 {
end.0 - start.0
} else {
(end.0 - start.0) / final_xy.0
};
let scale_y = if final_xy.1.abs() < 1e-6 {
end.1 - start.1
} else {
(end.1 - start.1) / final_xy.1
};
let mut steps = Vec::with_capacity(trace.steps.len());
for s in &trace.steps {
let dx = s.dx * scale_x + rng.gen_range(-2.0..=2.0);
let dy = s.dy * scale_y + rng.gen_range(-2.0..=2.0);
let dt_jitter = rng.gen_range(-5i32..=5);
let dt_ms = (s.dt_ms as i32 + dt_jitter).max(1) as u32;
steps.push(Step { dx, dy, dt_ms });
}
let summed = steps
.iter()
.fold((0.0f32, 0.0f32), |(ax, ay), s| (ax + s.dx, ay + s.dy));
if let Some(last) = steps.last_mut() {
last.dx += (end.0 - start.0) - summed.0;
last.dy += (end.1 - start.1) - summed.1;
}
Trace { steps }
}
fn bundled_corpus() -> Vec<Trace> {
vec![
Trace {
steps: build_trace(&[
(0.10, 0.05, 12),
(0.18, 0.10, 14),
(0.13, 0.13, 11),
(0.12, 0.16, 13),
(0.11, 0.18, 15),
(0.10, 0.16, 16),
(0.09, 0.13, 18),
(0.08, 0.06, 22),
(0.06, 0.02, 18),
(0.03, 0.01, 15),
]),
},
Trace {
steps: build_trace(&[
(0.04, 0.02, 22),
(0.06, 0.05, 20),
(0.08, 0.08, 19),
(0.10, 0.10, 18),
(0.11, 0.11, 18),
(0.12, 0.13, 17),
(0.13, 0.14, 16),
(0.13, 0.15, 16),
(0.10, 0.13, 17),
(0.07, 0.07, 18),
(0.04, 0.02, 22),
(0.02, 0.00, 25),
]),
},
Trace {
steps: build_trace(&[
(0.15, 0.12, 12),
(0.20, 0.18, 12),
(0.25, 0.22, 11),
(0.20, 0.18, 13),
(0.13, 0.12, 16),
(0.07, 0.10, 18),
(0.00, 0.08, 19),
(-0.02, 0.05, 18),
(0.02, 0.00, 17),
(-0.00, -0.05, 18),
]),
},
Trace {
steps: build_trace(&[
(0.05, 0.05, 14),
(0.10, 0.08, 13),
(0.12, 0.11, 12),
(0.13, 0.12, 13),
(0.02, -0.01, 11),
(-0.02, 0.01, 12),
(0.03, 0.00, 11),
(-0.01, 0.02, 12),
(0.13, 0.13, 14),
(0.11, 0.13, 15),
(0.10, 0.14, 16),
(0.08, 0.10, 17),
(0.06, 0.07, 18),
(0.10, 0.05, 18),
]),
},
Trace {
steps: build_trace(&[
(0.18, 0.18, 18),
(0.15, 0.15, 19),
(0.16, 0.15, 20),
(0.14, 0.14, 21),
(0.12, 0.12, 22),
(0.10, 0.10, 23),
(0.08, 0.08, 23),
(0.07, 0.08, 24),
]),
},
Trace {
steps: build_trace(&[
(0.02, 0.02, 30),
(0.03, 0.03, 35),
(0.02, 0.02, 40),
(0.10, 0.08, 12),
(0.15, 0.13, 11),
(0.18, 0.16, 11),
(0.18, 0.18, 11),
(0.15, 0.16, 12),
(0.10, 0.13, 14),
(0.07, 0.09, 16),
]),
},
Trace {
steps: build_trace(&[
(0.12, 0.05, 12),
(0.15, 0.08, 12),
(0.16, 0.12, 13),
(0.15, 0.15, 14),
(0.13, 0.16, 15),
(0.10, 0.16, 16),
(0.08, 0.13, 17),
(0.06, 0.10, 18),
(0.05, 0.05, 18),
]),
},
Trace {
steps: build_trace(&[
(0.06, 0.04, 16),
(0.10, 0.07, 14),
(0.13, 0.10, 13),
(0.01, 0.00, 28),
(0.12, 0.13, 13),
(0.13, 0.15, 14),
(0.13, 0.15, 15),
(-0.03, -0.02, 13),
(0.10, 0.13, 14),
(0.10, 0.10, 16),
(0.07, 0.07, 18),
(0.08, 0.08, 18),
]),
},
]
}
fn build_trace(steps: &[(f32, f32, u32)]) -> Vec<Step> {
let mut acc_x = 0.0f32;
let mut acc_y = 0.0f32;
let mut out = Vec::with_capacity(steps.len());
for (dx, dy, dt) in steps {
acc_x += dx;
acc_y += dy;
out.push(Step {
dx: *dx,
dy: *dy,
dt_ms: *dt,
});
}
if let Some(last) = out.last_mut() {
last.dx += 1.0 - acc_x;
last.dy += 1.0 - acc_y;
}
out
}
#[cfg(test)]
mod tests;