| 1 |
use crate::ModelerInput; |
| 2 |
use crate::utils::{dist, interp, interp2, nearest_point_on_segment}; |
| 3 |
use std::collections::VecDeque; |
| 4 |
|
| 5 |
|
| 6 |
#[allow(unused)] |
| 7 |
use crate::ModelerResult; |
| 8 |
#[allow(unused)] |
| 9 |
use crate::results::ModelerPartial; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#[doc = include_str!("../docs/notations.html")] |
| 18 |
#[doc = include_str!("../docs/stylus_state_modeler.html")] |
| 19 |
pub(crate) struct StateModeler { |
| 20 |
|
| 21 |
stylus_state_modeler_max_input_samples: usize, |
| 22 |
|
| 23 |
last_strokes: VecDeque<ModelerInput>, |
| 24 |
} |
| 25 |
|
| 26 |
impl Default for StateModeler { |
| 27 |
fn default() -> Self { |
| 28 |
Self { |
| 29 |
stylus_state_modeler_max_input_samples: 10, |
| 30 |
last_strokes: VecDeque::with_capacity(11), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl StateModeler { |
| 36 |
|
| 37 |
pub(crate) fn new(param: usize) -> Self { |
| 38 |
|
| 39 |
|
| 40 |
if param == 0 { |
| 41 |
return Self { |
| 42 |
stylus_state_modeler_max_input_samples: 1, |
| 43 |
last_strokes: VecDeque::with_capacity(2), |
| 44 |
}; |
| 45 |
} |
| 46 |
Self { |
| 47 |
stylus_state_modeler_max_input_samples: param, |
| 48 |
last_strokes: VecDeque::with_capacity(param + 1), |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
pub(crate) fn update(&mut self, input: ModelerInput) { |
| 54 |
|
| 55 |
self.last_strokes.push_back(input); |
| 56 |
if self.last_strokes.len() > self.stylus_state_modeler_max_input_samples { |
| 57 |
self.last_strokes.pop_front(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub(crate) fn reset(&mut self, max_input: usize) { |
| 63 |
self.last_strokes.clear(); |
| 64 |
self.stylus_state_modeler_max_input_samples = max_input; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
pub(crate) fn query(&mut self, pos: (f64, f64)) -> f64 { |
| 69 |
|
| 70 |
match self.last_strokes.len() { |
| 71 |
0 => 1.0, |
| 72 |
1 => self.last_strokes.front().unwrap().pressure, |
| 73 |
_ => { |
| 74 |
let mut distance = f64::INFINITY; |
| 75 |
let mut r: f64 = 0.0; |
| 76 |
|
| 77 |
let mut start_pressure: f64 = 1.0; |
| 78 |
let mut end_pressure: f64 = 1.0; |
| 79 |
|
| 80 |
for index_it in 0..self.last_strokes.len() - 1 { |
| 81 |
let start_pos = self.last_strokes.get(index_it).unwrap().pos; |
| 82 |
let end_pos = self.last_strokes.get(index_it + 1).unwrap().pos; |
| 83 |
|
| 84 |
let r_c = nearest_point_on_segment(start_pos, end_pos, pos); |
| 85 |
let point_c = interp2(start_pos, end_pos, r_c); |
| 86 |
|
| 87 |
if dist(pos, point_c) < distance { |
| 88 |
distance = dist(pos, point_c); |
| 89 |
r = r_c; |
| 90 |
start_pressure = self.last_strokes.get(index_it).unwrap().pressure; |
| 91 |
end_pressure = self.last_strokes.get(index_it + 1).unwrap().pressure; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
interp(start_pressure, end_pressure, r) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn state_modeler_straight() { |
| 103 |
let mut state_mod = StateModeler::new(10); |
| 104 |
approx::assert_relative_eq!(state_mod.query((0.0, 0.0)), 1.0); |
| 105 |
approx::assert_relative_eq!(state_mod.query((-5.0, 3.0)), 1.0); |
| 106 |
} |
| 107 |
#[test] |
| 108 |
fn query_single_output() { |
| 109 |
let mut state_mod = StateModeler::new(10); |
| 110 |
state_mod.update(ModelerInput { |
| 111 |
pos: (0.0, 0.0), |
| 112 |
pressure: 0.75, |
| 113 |
..ModelerInput::default() |
| 114 |
}); |
| 115 |
|
| 116 |
approx::assert_relative_eq!(state_mod.query((0.0, 0.0)), 0.75); |
| 117 |
approx::assert_relative_eq!(state_mod.query((1.0, 1.0)), 0.75); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn query_multiple_output() { |
| 122 |
let mut state_mod = StateModeler::default(); |
| 123 |
state_mod.update(ModelerInput { |
| 124 |
pos: (0.5, 1.5), |
| 125 |
pressure: 0.3, |
| 126 |
..Default::default() |
| 127 |
}); |
| 128 |
state_mod.update(ModelerInput { |
| 129 |
pos: (2.0, 1.5), |
| 130 |
pressure: 0.6, |
| 131 |
..Default::default() |
| 132 |
}); |
| 133 |
state_mod.update(ModelerInput { |
| 134 |
pos: (3.0, 3.5), |
| 135 |
pressure: 0.8, |
| 136 |
..Default::default() |
| 137 |
}); |
| 138 |
state_mod.update(ModelerInput { |
| 139 |
pos: (3.5, 4.0), |
| 140 |
pressure: 0.2, |
| 141 |
..Default::default() |
| 142 |
}); |
| 143 |
|
| 144 |
let tol = 1e-5; |
| 145 |
approx::assert_abs_diff_eq!(state_mod.query((0.0, 2.0)), 0.3, epsilon = tol); |
| 146 |
approx::assert_abs_diff_eq!(state_mod.query((1.0, 2.0)), 0.4, epsilon = tol); |
| 147 |
approx::assert_abs_diff_eq!(state_mod.query((2.0, 1.5)), 0.6, epsilon = tol); |
| 148 |
approx::assert_abs_diff_eq!(state_mod.query((2.5, 1.875)), 0.65, epsilon = tol); |
| 149 |
approx::assert_abs_diff_eq!(state_mod.query((2.5, 3.125)), 0.75, epsilon = tol); |
| 150 |
approx::assert_abs_diff_eq!(state_mod.query((2.5, 4.0)), 0.8, epsilon = tol); |
| 151 |
approx::assert_abs_diff_eq!(state_mod.query((2.5, 4.0)), 0.8, epsilon = tol); |
| 152 |
approx::assert_abs_diff_eq!(state_mod.query((3.0, 4.0)), 0.5, epsilon = tol); |
| 153 |
approx::assert_abs_diff_eq!(state_mod.query((4.0, 4.0)), 0.2, epsilon = tol); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn query_stale() { |
| 158 |
let mut state_mod = StateModeler::default(); |
| 159 |
state_mod.update(ModelerInput { |
| 160 |
pos: (1.0, 1.0), |
| 161 |
pressure: 0.6, |
| 162 |
..Default::default() |
| 163 |
}); |
| 164 |
state_mod.update(ModelerInput { |
| 165 |
pos: (-1.0, 2.0), |
| 166 |
pressure: 0.3, |
| 167 |
..Default::default() |
| 168 |
}); |
| 169 |
state_mod.update(ModelerInput { |
| 170 |
pos: (-4.0, 0.0), |
| 171 |
pressure: 0.9, |
| 172 |
..Default::default() |
| 173 |
}); |
| 174 |
state_mod.update(ModelerInput { |
| 175 |
pos: (-6.0, -3.0), |
| 176 |
pressure: 0.4, |
| 177 |
..Default::default() |
| 178 |
}); |
| 179 |
state_mod.update(ModelerInput { |
| 180 |
pos: (-5.0, -5.0), |
| 181 |
pressure: 0.3, |
| 182 |
..Default::default() |
| 183 |
}); |
| 184 |
state_mod.update(ModelerInput { |
| 185 |
pos: (-3.0, -4.0), |
| 186 |
pressure: 0.6, |
| 187 |
..Default::default() |
| 188 |
}); |
| 189 |
state_mod.update(ModelerInput { |
| 190 |
pos: (-6.0, -7.0), |
| 191 |
pressure: 0.9, |
| 192 |
..Default::default() |
| 193 |
}); |
| 194 |
state_mod.update(ModelerInput { |
| 195 |
pos: (-9.0, -8.0), |
| 196 |
pressure: 0.8, |
| 197 |
..Default::default() |
| 198 |
}); |
| 199 |
state_mod.update(ModelerInput { |
| 200 |
pos: (-11.0, -5.0), |
| 201 |
pressure: 0.2, |
| 202 |
..Default::default() |
| 203 |
}); |
| 204 |
state_mod.update(ModelerInput { |
| 205 |
pos: (-10.0, -2.0), |
| 206 |
pressure: 0.7, |
| 207 |
..Default::default() |
| 208 |
}); |
| 209 |
|
| 210 |
let tol = 1e-5; |
| 211 |
approx::assert_abs_diff_eq!(state_mod.query((2.0, 0.0)), 0.6, epsilon = tol); |
| 212 |
approx::assert_abs_diff_eq!(state_mod.query((1.0, 3.5)), 0.45, epsilon = tol); |
| 213 |
approx::assert_abs_diff_eq!(state_mod.query((-3.0, 17. / 6.)), 0.5, epsilon = tol); |
| 214 |
|
| 215 |
|
| 216 |
state_mod.update(ModelerInput { |
| 217 |
pos: (-8.0, 0.0), |
| 218 |
pressure: 0.6, |
| 219 |
..Default::default() |
| 220 |
}); |
| 221 |
|
| 222 |
approx::assert_abs_diff_eq!(state_mod.query((2.0, 0.0)), 0.3, epsilon = tol); |
| 223 |
approx::assert_abs_diff_eq!(state_mod.query((1.0, 3.5)), 0.3, epsilon = tol); |
| 224 |
approx::assert_relative_eq!(state_mod.query((-3.0, 17. / 6.)), 0.5, epsilon = tol); |
| 225 |
|
| 226 |
state_mod.update(ModelerInput { |
| 227 |
pos: (-8.0, 0.0), |
| 228 |
pressure: 0.6, |
| 229 |
..Default::default() |
| 230 |
}); |
| 231 |
|
| 232 |
approx::assert_abs_diff_eq!(state_mod.query((2.0, 0.0)), 0.9, epsilon = tol); |
| 233 |
approx::assert_abs_diff_eq!(state_mod.query((1.0, 3.5)), 0.9, epsilon = tol); |
| 234 |
approx::assert_abs_diff_eq!(state_mod.query((-3.0, 17. / 6.)), 0.9, epsilon = tol); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn query_reset() { |
| 239 |
let mut state_mod = StateModeler::default(); |
| 240 |
state_mod.update(ModelerInput { |
| 241 |
pos: (4.0, 5.0), |
| 242 |
pressure: 0.4, |
| 243 |
..Default::default() |
| 244 |
}); |
| 245 |
state_mod.update(ModelerInput { |
| 246 |
pos: (7.0, 8.0), |
| 247 |
pressure: 0.1, |
| 248 |
..Default::default() |
| 249 |
}); |
| 250 |
|
| 251 |
let tol = 1e-5; |
| 252 |
approx::assert_abs_diff_eq!(state_mod.query((10.0, 12.0)), 0.1, epsilon = tol); |
| 253 |
state_mod.reset(10); |
| 254 |
|
| 255 |
approx::assert_relative_eq!(state_mod.query((10.0, 12.0)), 1.0); |
| 256 |
|
| 257 |
state_mod.update(ModelerInput { |
| 258 |
pos: (-1.0, 4.0), |
| 259 |
pressure: 0.4, |
| 260 |
..Default::default() |
| 261 |
}); |
| 262 |
|
| 263 |
approx::assert_abs_diff_eq!(state_mod.query((6.0, 7.0)), 0.4, epsilon = tol); |
| 264 |
|
| 265 |
state_mod.update(ModelerInput { |
| 266 |
pos: (-3.0, 0.0), |
| 267 |
pressure: 0.7, |
| 268 |
..Default::default() |
| 269 |
}); |
| 270 |
|
| 271 |
approx::assert_abs_diff_eq!(state_mod.query((-2.0, 2.0)), 0.55, epsilon = tol); |
| 272 |
approx::assert_abs_diff_eq!(state_mod.query((0.0, 5.0)), 0.4, epsilon = tol); |
| 273 |
} |
| 274 |
|
| 275 |
|