| 1 |
use crate::results::ModelerPartial; |
| 2 |
use crate::utils::{dist, nearest_point_on_segment}; |
| 3 |
use crate::{ModelerInput, ModelerParams}; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
pub(crate) struct PositionModeler { |
| 9 |
|
| 10 |
position_modeler_spring_mass_constant: f64, |
| 11 |
position_modeler_drag_constant: f64, |
| 12 |
|
| 13 |
pub(crate) state: ModelerPartial, |
| 14 |
} |
| 15 |
|
| 16 |
impl PositionModeler { |
| 17 |
pub(crate) fn new(params: ModelerParams, first_input: ModelerInput) -> Self { |
| 18 |
Self { |
| 19 |
position_modeler_spring_mass_constant: params.position_modeler_spring_mass_constant, |
| 20 |
position_modeler_drag_constant: params.position_modeler_drag_constant, |
| 21 |
state: ModelerPartial { |
| 22 |
pos: first_input.pos, |
| 23 |
velocity: (0.0, 0.0), |
| 24 |
acceleration: (0.0, 0.0), |
| 25 |
time: first_input.time, |
| 26 |
}, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
pub(crate) fn update(&mut self, anchor_pos: (f64, f64), time: f64) -> ModelerPartial { |
| 32 |
let delta_time = time - self.state.time; |
| 33 |
|
| 34 |
self.state.acceleration = ( |
| 35 |
(anchor_pos.0 - self.state.pos.0) / (self.position_modeler_spring_mass_constant) |
| 36 |
- self.position_modeler_drag_constant * self.state.velocity.0, |
| 37 |
(anchor_pos.1 - self.state.pos.1) / (self.position_modeler_spring_mass_constant) |
| 38 |
- self.position_modeler_drag_constant * self.state.velocity.1, |
| 39 |
); |
| 40 |
self.state.velocity = ( |
| 41 |
self.state.velocity.0 + (delta_time) * self.state.acceleration.0, |
| 42 |
self.state.velocity.1 + (delta_time) * self.state.acceleration.1, |
| 43 |
); |
| 44 |
self.state.pos = ( |
| 45 |
self.state.pos.0 + delta_time * self.state.velocity.0, |
| 46 |
self.state.pos.1 + delta_time * self.state.velocity.1, |
| 47 |
); |
| 48 |
self.state.time = time; |
| 49 |
|
| 50 |
self.state.clone() |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
pub(crate) fn update_along_linear_path( |
| 57 |
&mut self, |
| 58 |
start_pos: (f64, f64), |
| 59 |
start_time: f64, |
| 60 |
end_pos: (f64, f64), |
| 61 |
end_time: f64, |
| 62 |
n_steps: i32, |
| 63 |
) -> Vec<ModelerPartial> { |
| 64 |
(1..=n_steps) |
| 65 |
.map(|i| { |
| 66 |
let frac_adv = i as f64 / n_steps as f64; |
| 67 |
|
| 68 |
let anchor_pos = ( |
| 69 |
start_pos.0 + frac_adv * (end_pos.0 - start_pos.0), |
| 70 |
start_pos.1 + frac_adv * (end_pos.1 - start_pos.1), |
| 71 |
); |
| 72 |
let time = start_time + frac_adv * (end_time - start_time); |
| 73 |
|
| 74 |
self.update(anchor_pos, time) |
| 75 |
}) |
| 76 |
.collect() |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
pub(crate) fn model_end_of_stroke( |
| 87 |
&mut self, |
| 88 |
anchor_pos: (f64, f64), |
| 89 |
delta_time: f64, |
| 90 |
max_iterations: usize, |
| 91 |
stop_distance: f64, |
| 92 |
) -> Vec<ModelerPartial> { |
| 93 |
let initial_state = self.state.clone(); |
| 94 |
let mut delta_time = delta_time; |
| 95 |
|
| 96 |
let mut out_events = Vec::<ModelerPartial>::with_capacity(max_iterations); |
| 97 |
for _ in 0..max_iterations { |
| 98 |
let previous_state = self.state.clone(); |
| 99 |
let candidate = self.update(anchor_pos, previous_state.time + delta_time); |
| 100 |
|
| 101 |
if dist(previous_state.pos, candidate.pos) < stop_distance { |
| 102 |
|
| 103 |
self.state = initial_state; |
| 104 |
|
| 105 |
return out_events; |
| 106 |
} |
| 107 |
|
| 108 |
if nearest_point_on_segment( |
| 109 |
(previous_state.pos.0, previous_state.pos.1), |
| 110 |
(candidate.pos.0, candidate.pos.1), |
| 111 |
(anchor_pos.0, anchor_pos.1), |
| 112 |
) < 1.0 |
| 113 |
{ |
| 114 |
|
| 115 |
delta_time *= 0.5; |
| 116 |
self.state = previous_state; |
| 117 |
continue; |
| 118 |
} else { |
| 119 |
out_events.push(candidate.clone()); |
| 120 |
} |
| 121 |
|
| 122 |
if dist(candidate.pos, anchor_pos) < stop_distance { |
| 123 |
|
| 124 |
|
| 125 |
self.state = initial_state; |
| 126 |
return out_events; |
| 127 |
} |
| 128 |
} |
| 129 |
self.state = initial_state; |
| 130 |
out_events |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl ModelerPartial { |
| 135 |
#[cfg(test)] |
| 136 |
fn near(self, compare: ModelerPartial) -> bool { |
| 137 |
let tol = 0.005; |
| 138 |
approx::abs_diff_eq!(self.pos.0, compare.pos.0, epsilon = tol) |
| 139 |
&& approx::abs_diff_eq!(self.pos.1, compare.pos.1, epsilon = tol) |
| 140 |
&& approx::abs_diff_eq!(self.velocity.0, compare.velocity.0, epsilon = tol) |
| 141 |
&& approx::abs_diff_eq!(self.velocity.1, compare.velocity.1, epsilon = tol) |
| 142 |
&& approx::abs_diff_eq!(self.acceleration.0, compare.acceleration.0, epsilon = tol) |
| 143 |
&& approx::abs_diff_eq!(self.acceleration.1, compare.acceleration.1, epsilon = tol) |
| 144 |
&& approx::abs_diff_eq!(self.time, compare.time, epsilon = tol as f64) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn straight_line() { |
| 150 |
|
| 151 |
let mut modeler: PositionModeler = |
| 152 |
PositionModeler::new(ModelerParams::suggested(), ModelerInput::default()); |
| 153 |
let default_ts = 1. / 180 as f64; |
| 154 |
let mut current_time: f64 = 0.0; |
| 155 |
|
| 156 |
current_time += default_ts; |
| 157 |
assert!( |
| 158 |
modeler |
| 159 |
.update((1.0, 0.0), current_time) |
| 160 |
.near(ModelerPartial { |
| 161 |
pos: (0.0909, 0.0), |
| 162 |
velocity: (16.3636, 0.0), |
| 163 |
acceleration: (2945.4546, 0.0), |
| 164 |
time: current_time |
| 165 |
}) |
| 166 |
); |
| 167 |
|
| 168 |
current_time += default_ts; |
| 169 |
assert!( |
| 170 |
modeler |
| 171 |
.update((2.0, 0.0), current_time) |
| 172 |
.near(ModelerPartial { |
| 173 |
pos: (0.319, 0.0), |
| 174 |
velocity: (41.0579, 0.0), |
| 175 |
acceleration: (4444.9590, 0.0), |
| 176 |
time: current_time |
| 177 |
}) |
| 178 |
); |
| 179 |
|
| 180 |
current_time += default_ts; |
| 181 |
assert!( |
| 182 |
modeler |
| 183 |
.update((3.0, 0.0), current_time) |
| 184 |
.near(ModelerPartial { |
| 185 |
pos: (0.6996, 0.0), |
| 186 |
velocity: (68.5055, 0.0), |
| 187 |
acceleration: (4940.5737, 0.0), |
| 188 |
time: current_time |
| 189 |
}) |
| 190 |
); |
| 191 |
|
| 192 |
current_time += default_ts; |
| 193 |
assert!( |
| 194 |
modeler |
| 195 |
.update((4.0, 0.0), current_time) |
| 196 |
.near(ModelerPartial { |
| 197 |
pos: (1.228, 0.0), |
| 198 |
velocity: (95.1099, 0.0), |
| 199 |
acceleration: (4788.8003, 0.0), |
| 200 |
time: current_time |
| 201 |
}) |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn zigzag() { |
| 207 |
|
| 208 |
let mut current_time: f64 = 3.0; |
| 209 |
let mut modeler = PositionModeler::new( |
| 210 |
ModelerParams::suggested(), |
| 211 |
ModelerInput { |
| 212 |
pos: (-1.0, -1.0), |
| 213 |
time: current_time, |
| 214 |
..ModelerInput::default() |
| 215 |
}, |
| 216 |
); |
| 217 |
let default_ts = 1. / 180 as f64; |
| 218 |
|
| 219 |
current_time += default_ts; |
| 220 |
assert!( |
| 221 |
modeler |
| 222 |
.update((-0.5, -1.0), current_time) |
| 223 |
.near(ModelerPartial { |
| 224 |
pos: (-0.9545, -1.0), |
| 225 |
velocity: (8.1818, 0.0), |
| 226 |
acceleration: (1472.7273, 0.0), |
| 227 |
time: current_time |
| 228 |
}) |
| 229 |
); |
| 230 |
|
| 231 |
current_time += default_ts; |
| 232 |
assert!( |
| 233 |
modeler |
| 234 |
.update((-0.5, -0.5), current_time) |
| 235 |
.near(ModelerPartial { |
| 236 |
pos: (-0.886, -0.9545), |
| 237 |
velocity: (12.3471, 8.1818), |
| 238 |
acceleration: (749.7521, 1472.7273), |
| 239 |
time: current_time |
| 240 |
}) |
| 241 |
); |
| 242 |
|
| 243 |
current_time += default_ts; |
| 244 |
assert!( |
| 245 |
modeler |
| 246 |
.update((-0.0, -0.5), current_time) |
| 247 |
.near(ModelerPartial { |
| 248 |
pos: (-0.7643, -0.886), |
| 249 |
velocity: (21.9056, 12.3471), |
| 250 |
acceleration: (1720.5348, 749.7521), |
| 251 |
time: current_time |
| 252 |
}) |
| 253 |
); |
| 254 |
|
| 255 |
current_time += default_ts; |
| 256 |
assert!( |
| 257 |
modeler |
| 258 |
.update((0.0, 0.0), current_time) |
| 259 |
.near(ModelerPartial { |
| 260 |
pos: (-0.6218, -0.7643), |
| 261 |
velocity: (25.6493, 21.9056), |
| 262 |
acceleration: (673.8650, 1720.5348), |
| 263 |
time: current_time |
| 264 |
}) |
| 265 |
); |
| 266 |
|
| 267 |
current_time += default_ts; |
| 268 |
assert!( |
| 269 |
modeler |
| 270 |
.update((0.5, 0.0), current_time) |
| 271 |
.near(ModelerPartial { |
| 272 |
pos: (-0.4343, -0.6218), |
| 273 |
velocity: (33.7456, 25.6493), |
| 274 |
acceleration: (1457.3298, 673.8650), |
| 275 |
time: current_time |
| 276 |
}) |
| 277 |
) |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn sharp_turn() { |
| 282 |
|
| 283 |
let mut current_time: f64 = 1.6; |
| 284 |
let mut modeler = PositionModeler::new( |
| 285 |
ModelerParams::suggested(), |
| 286 |
ModelerInput { |
| 287 |
time: current_time, |
| 288 |
..ModelerInput::default() |
| 289 |
}, |
| 290 |
); |
| 291 |
let default_ts = 1. / 180 as f64; |
| 292 |
|
| 293 |
current_time += default_ts; |
| 294 |
assert!( |
| 295 |
modeler |
| 296 |
.update((0.25, 0.25), current_time) |
| 297 |
.near(ModelerPartial { |
| 298 |
pos: (0.0227, 0.0227), |
| 299 |
velocity: (4.0909, 4.0909), |
| 300 |
acceleration: (736.3636, 736.3636), |
| 301 |
time: current_time |
| 302 |
}) |
| 303 |
); |
| 304 |
current_time += default_ts; |
| 305 |
assert!( |
| 306 |
modeler |
| 307 |
.update((0.5, 0.5), current_time) |
| 308 |
.near(ModelerPartial { |
| 309 |
pos: (0.0798, 0.0798), |
| 310 |
velocity: (10.2645, 10.2645), |
| 311 |
acceleration: (1111.2397, 1111.2397), |
| 312 |
time: current_time |
| 313 |
}) |
| 314 |
); |
| 315 |
current_time += default_ts; |
| 316 |
assert!( |
| 317 |
modeler |
| 318 |
.update((0.75, 0.75), current_time) |
| 319 |
.near(ModelerPartial { |
| 320 |
pos: (0.1749, 0.1749), |
| 321 |
velocity: (17.1264, 17.1264), |
| 322 |
acceleration: (1235.1434, 1235.1434), |
| 323 |
time: current_time |
| 324 |
}) |
| 325 |
); |
| 326 |
current_time += default_ts; |
| 327 |
assert!( |
| 328 |
modeler |
| 329 |
.update((1.0, 1.0), current_time) |
| 330 |
.near(ModelerPartial { |
| 331 |
pos: (0.307, 0.307), |
| 332 |
velocity: (23.7775, 23.7775), |
| 333 |
acceleration: (1197.2001, 1197.2001), |
| 334 |
time: current_time |
| 335 |
}) |
| 336 |
); |
| 337 |
current_time += default_ts; |
| 338 |
assert!( |
| 339 |
modeler |
| 340 |
.update((1.25, 0.75), current_time) |
| 341 |
.near(ModelerPartial { |
| 342 |
pos: (0.472, 0.4265), |
| 343 |
velocity: (29.6975, 21.5157), |
| 344 |
acceleration: (1065.5977, -407.1296), |
| 345 |
time: current_time |
| 346 |
}) |
| 347 |
); |
| 348 |
current_time += default_ts; |
| 349 |
assert!( |
| 350 |
modeler |
| 351 |
.update((1.5, 0.5), current_time) |
| 352 |
.near(ModelerPartial { |
| 353 |
pos: (0.6644, 0.5049), |
| 354 |
velocity: (34.6406, 14.1117), |
| 355 |
acceleration: (889.7637, -1332.7158), |
| 356 |
time: current_time |
| 357 |
}) |
| 358 |
); |
| 359 |
current_time += default_ts; |
| 360 |
assert!( |
| 361 |
modeler |
| 362 |
.update((1.75, 0.25), current_time) |
| 363 |
.near(ModelerPartial { |
| 364 |
pos: (0.8786, 0.5288), |
| 365 |
velocity: (38.5482, 4.2955), |
| 366 |
acceleration: (703.3755, -1766.9114), |
| 367 |
time: current_time |
| 368 |
}) |
| 369 |
); |
| 370 |
current_time += default_ts; |
| 371 |
assert!( |
| 372 |
modeler |
| 373 |
.update((2.0, 0.0), current_time) |
| 374 |
.near(ModelerPartial { |
| 375 |
pos: (1.109, 0.495), |
| 376 |
velocity: (41.4794, -6.0756), |
| 377 |
acceleration: (527.5996, -1866.8005), |
| 378 |
time: current_time |
| 379 |
}) |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn smooth_turn() { |
| 385 |
use std::f64::consts::PI; |
| 386 |
let point_on_circle = |x: f64| (x.cos(), x.sin()); |
| 387 |
|
| 388 |
let mut current_time: f64 = 1.0; |
| 389 |
let mut modeler = PositionModeler::new( |
| 390 |
ModelerParams::suggested(), |
| 391 |
ModelerInput { |
| 392 |
time: current_time, |
| 393 |
pos: point_on_circle(0.0), |
| 394 |
..ModelerInput::default() |
| 395 |
}, |
| 396 |
); |
| 397 |
let default_ts = 1. / 180 as f64; |
| 398 |
current_time += default_ts; |
| 399 |
assert!( |
| 400 |
modeler |
| 401 |
.update(point_on_circle(PI * 0.125), current_time) |
| 402 |
.near(ModelerPartial { |
| 403 |
pos: (0.9931, 0.0348), |
| 404 |
velocity: (-1.2456, 6.2621), |
| 405 |
acceleration: (-224.2095, 1127.1768), |
| 406 |
time: current_time |
| 407 |
}) |
| 408 |
); |
| 409 |
current_time += default_ts; |
| 410 |
assert!( |
| 411 |
modeler |
| 412 |
.update(point_on_circle(PI * 0.25), current_time) |
| 413 |
.near(ModelerPartial { |
| 414 |
pos: (0.9629, 0.1168), |
| 415 |
velocity: (-5.4269, 14.7588), |
| 416 |
acceleration: (-752.6373, 1529.4097), |
| 417 |
time: current_time |
| 418 |
}) |
| 419 |
); |
| 420 |
current_time += default_ts; |
| 421 |
assert!( |
| 422 |
modeler |
| 423 |
.update(point_on_circle(PI * 0.375), current_time) |
| 424 |
.near(ModelerPartial { |
| 425 |
pos: (0.8921, 0.2394), |
| 426 |
velocity: (-12.7511, 22.0623), |
| 427 |
acceleration: (-1318.3523, 1314.6320), |
| 428 |
time: current_time |
| 429 |
}) |
| 430 |
); |
| 431 |
current_time += default_ts; |
| 432 |
assert!( |
| 433 |
modeler |
| 434 |
.update(point_on_circle(PI * 0.5), current_time) |
| 435 |
.near(ModelerPartial { |
| 436 |
pos: (0.7685, 0.3820), |
| 437 |
velocity: (-22.2485, 25.6844), |
| 438 |
acceleration: (-1709.5339, 651.9690), |
| 439 |
time: current_time |
| 440 |
}) |
| 441 |
); |
| 442 |
current_time += default_ts; |
| 443 |
assert!( |
| 444 |
modeler |
| 445 |
.update(point_on_circle(PI * 0.625), current_time) |
| 446 |
.near(ModelerPartial { |
| 447 |
pos: (0.5897, 0.5169), |
| 448 |
velocity: (-32.1865, 24.2771), |
| 449 |
acceleration: (-1788.8300, -253.3177), |
| 450 |
time: current_time |
| 451 |
}) |
| 452 |
); |
| 453 |
current_time += default_ts; |
| 454 |
assert!( |
| 455 |
modeler |
| 456 |
.update(point_on_circle(PI * 0.75), current_time) |
| 457 |
.near(ModelerPartial { |
| 458 |
pos: (0.3645, 0.6151), |
| 459 |
velocity: (-40.5319, 17.6785), |
| 460 |
acceleration: (-1502.1846, -1187.7462), |
| 461 |
time: current_time |
| 462 |
}) |
| 463 |
); |
| 464 |
current_time += default_ts; |
| 465 |
assert!( |
| 466 |
modeler |
| 467 |
.update(point_on_circle(PI * 0.875), current_time) |
| 468 |
.near(ModelerPartial { |
| 469 |
pos: (0.1123, 0.6529), |
| 470 |
velocity: (-45.4017, 6.8034), |
| 471 |
acceleration: (-876.5552, -1957.5056), |
| 472 |
time: current_time |
| 473 |
}) |
| 474 |
); |
| 475 |
current_time += default_ts; |
| 476 |
assert!( |
| 477 |
modeler |
| 478 |
.update(point_on_circle(PI), current_time) |
| 479 |
.near(ModelerPartial { |
| 480 |
pos: (-0.1402, 0.6162), |
| 481 |
velocity: (-45.4417, -6.6022), |
| 482 |
acceleration: (-7.2061, -2413.0093), |
| 483 |
time: current_time |
| 484 |
}) |
| 485 |
); |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn test_update_linear_path() { |
| 490 |
let mut modeler = PositionModeler::new( |
| 491 |
ModelerParams::suggested(), |
| 492 |
ModelerInput { |
| 493 |
time: 3.0, |
| 494 |
pos: (5.0, 10.0), |
| 495 |
..ModelerInput::default() |
| 496 |
}, |
| 497 |
); |
| 498 |
|
| 499 |
let linear_path = modeler.update_along_linear_path((5.0, 10.0), 3.0, (15., 10.), 3.05, 5); |
| 500 |
let expected = vec![ |
| 501 |
ModelerPartial { |
| 502 |
pos: (5.5891, 10.0), |
| 503 |
velocity: (58.9091, 0.0), |
| 504 |
acceleration: (5890.9092, 0.0), |
| 505 |
time: 3.01, |
| 506 |
}, |
| 507 |
ModelerPartial { |
| 508 |
pos: (6.7587, 10.0), |
| 509 |
velocity: (116.9613, 0.0), |
| 510 |
acceleration: (5805.2231, 0.0), |
| 511 |
time: 3.02, |
| 512 |
}, |
| 513 |
ModelerPartial { |
| 514 |
pos: (8.3355, 10.0), |
| 515 |
velocity: (157.6746, 0.0), |
| 516 |
acceleration: (4071.3291, 0.0), |
| 517 |
time: 3.03, |
| 518 |
}, |
| 519 |
ModelerPartial { |
| 520 |
pos: (10.1509, 10.0), |
| 521 |
velocity: (181.5411, 0.0), |
| 522 |
acceleration: (2386.6475, 0.0), |
| 523 |
time: 3.04, |
| 524 |
}, |
| 525 |
ModelerPartial { |
| 526 |
pos: (12.0875, 10.0), |
| 527 |
velocity: (193.6607, 0.0), |
| 528 |
acceleration: (1211.9609, 0.0), |
| 529 |
time: 3.05, |
| 530 |
}, |
| 531 |
]; |
| 532 |
|
| 533 |
assert!( |
| 534 |
linear_path |
| 535 |
.into_iter() |
| 536 |
.zip(expected) |
| 537 |
.fold(true, |acc, x| { acc && x.0.near(x.1) }) |
| 538 |
); |
| 539 |
|
| 540 |
|
| 541 |
let linear_path_2 = modeler.update_along_linear_path((15.0, 10.0), 3.05, (15.0, 16.0), 3.08, 3); |
| 542 |
let expected2 = vec![ |
| 543 |
ModelerPartial { |
| 544 |
pos: (13.4876, 10.5891), |
| 545 |
velocity: (140.0123, 58.9091), |
| 546 |
acceleration: (-5364.8398, 5890.9092), |
| 547 |
time: 3.06, |
| 548 |
}, |
| 549 |
ModelerPartial { |
| 550 |
pos: (14.3251, 11.7587), |
| 551 |
velocity: (83.7508, 116.9613), |
| 552 |
acceleration: (-5626.1528, 5805.2217), |
| 553 |
time: 3.07, |
| 554 |
}, |
| 555 |
ModelerPartial { |
| 556 |
pos: (14.7584, 13.3355), |
| 557 |
velocity: (43.3291, 157.6746), |
| 558 |
acceleration: (-4042.1616, 4071.3291), |
| 559 |
time: 3.08, |
| 560 |
}, |
| 561 |
]; |
| 562 |
|
| 563 |
assert!( |
| 564 |
linear_path_2 |
| 565 |
.into_iter() |
| 566 |
.zip(expected2) |
| 567 |
.fold(true, |acc, x| { acc && x.0.near(x.1) }) |
| 568 |
); |
| 569 |
} |
| 570 |
|
| 571 |
#[test] |
| 572 |
fn model_end_of_stroke_stationary() { |
| 573 |
let mut model = PositionModeler::new( |
| 574 |
ModelerParams::suggested(), |
| 575 |
ModelerInput { |
| 576 |
pos: (4.0, -2.0), |
| 577 |
..ModelerInput::default() |
| 578 |
}, |
| 579 |
); |
| 580 |
|
| 581 |
let result = model.model_end_of_stroke((3.0, -1.0), 1. / 180., 20, 0.01); |
| 582 |
let expected = vec![ |
| 583 |
ModelerPartial { |
| 584 |
pos: (3.9091, -1.9091), |
| 585 |
velocity: (-16.3636, 16.3636), |
| 586 |
acceleration: (-2945.4546, 2945.4546), |
| 587 |
time: 0.0056, |
| 588 |
}, |
| 589 |
ModelerPartial { |
| 590 |
pos: (3.7719, -1.7719), |
| 591 |
velocity: (-24.6942, 24.6942), |
| 592 |
acceleration: (-1499.5044, 1499.5042), |
| 593 |
time: 0.0111, |
| 594 |
}, |
| 595 |
ModelerPartial { |
| 596 |
pos: (3.6194, -1.6194), |
| 597 |
velocity: (-27.4476, 27.4476), |
| 598 |
acceleration: (-495.6155, 495.6150), |
| 599 |
time: 0.0167, |
| 600 |
}, |
| 601 |
ModelerPartial { |
| 602 |
pos: (3.4716, -1.4716), |
| 603 |
velocity: (-26.6045, 26.6044), |
| 604 |
acceleration: (151.7738, -151.7742), |
| 605 |
time: 0.0222, |
| 606 |
}, |
| 607 |
ModelerPartial { |
| 608 |
pos: (3.3401, -1.3401), |
| 609 |
velocity: (-23.6799, 23.6799), |
| 610 |
acceleration: (526.4102, -526.4102), |
| 611 |
time: 0.0278, |
| 612 |
}, |
| 613 |
ModelerPartial { |
| 614 |
pos: (3.2302, -1.2302), |
| 615 |
velocity: (-19.7725, 19.7725), |
| 616 |
acceleration: (703.3362, -703.3359), |
| 617 |
time: 0.0333, |
| 618 |
}, |
| 619 |
ModelerPartial { |
| 620 |
pos: (3.1434, -1.1434), |
| 621 |
velocity: (-15.6306, 15.6306), |
| 622 |
acceleration: (745.5521, -745.5518), |
| 623 |
time: 0.0389, |
| 624 |
}, |
| 625 |
ModelerPartial { |
| 626 |
pos: (3.0782, -1.0782), |
| 627 |
velocity: (-11.7244, 11.7244), |
| 628 |
acceleration: (703.1044, -703.1039), |
| 629 |
time: 0.0444, |
| 630 |
}, |
| 631 |
ModelerPartial { |
| 632 |
pos: (3.0320, -1.0320), |
| 633 |
velocity: (-8.3149, 8.3149), |
| 634 |
acceleration: (613.7169, -613.7166), |
| 635 |
time: 0.0500, |
| 636 |
}, |
| 637 |
ModelerPartial { |
| 638 |
pos: (3.0014, -1.0014), |
| 639 |
velocity: (-5.5133, 5.5133), |
| 640 |
acceleration: (504.2921, -504.2918), |
| 641 |
time: 0.0556, |
| 642 |
}, |
| 643 |
]; |
| 644 |
|
| 645 |
assert!( |
| 646 |
result |
| 647 |
.into_iter() |
| 648 |
.zip(expected) |
| 649 |
.fold(true, |acc, x| { acc && x.0.near(x.1) }) |
| 650 |
); |
| 651 |
} |
| 652 |
|
| 653 |
#[test] |
| 654 |
fn end_of_stroke_motion() { |
| 655 |
let mut model = PositionModeler { |
| 656 |
position_modeler_drag_constant: ModelerParams::suggested().position_modeler_drag_constant, |
| 657 |
position_modeler_spring_mass_constant: ModelerParams::suggested() |
| 658 |
.position_modeler_spring_mass_constant, |
| 659 |
state: ModelerPartial { |
| 660 |
pos: (-1.0, 2.0), |
| 661 |
velocity: (40.0, 10.0), |
| 662 |
acceleration: (0.0, 0.0), |
| 663 |
time: 1., |
| 664 |
}, |
| 665 |
}; |
| 666 |
|
| 667 |
let result = model.model_end_of_stroke((7.0, 2.0), 1. / 120., 20, 0.01); |
| 668 |
let expected = vec![ |
| 669 |
ModelerPartial { |
| 670 |
pos: (0.7697, 2.0333), |
| 671 |
velocity: (212.3636, 4.0000), |
| 672 |
acceleration: (20683.6367, -720.0000), |
| 673 |
time: 1.0083, |
| 674 |
}, |
| 675 |
ModelerPartial { |
| 676 |
pos: (2.7520, 2.0398), |
| 677 |
velocity: (237.8711, 0.7818), |
| 678 |
acceleration: (3060.8916, -386.1817), |
| 679 |
time: 1.0167, |
| 680 |
}, |
| 681 |
ModelerPartial { |
| 682 |
pos: (4.4138, 2.0343), |
| 683 |
velocity: (199.4186, -0.6654), |
| 684 |
acceleration: (-4614.2959, -173.6631), |
| 685 |
time: 1.0250, |
| 686 |
}, |
| 687 |
ModelerPartial { |
| 688 |
pos: (5.6075, 2.0251), |
| 689 |
velocity: (143.2474, -1.1081), |
| 690 |
acceleration: (-6740.5410, -53.1330), |
| 691 |
time: 1.0333, |
| 692 |
}, |
| 693 |
ModelerPartial { |
| 694 |
pos: (6.3698, 2.0162), |
| 695 |
velocity: (91.4784, -1.0586), |
| 696 |
acceleration: (-6212.2896, 5.9471), |
| 697 |
time: 1.0417, |
| 698 |
}, |
| 699 |
ModelerPartial { |
| 700 |
pos: (6.8037, 2.0094), |
| 701 |
velocity: (52.0592, -0.8222), |
| 702 |
acceleration: (-4730.2935, 28.3621), |
| 703 |
time: 1.0500, |
| 704 |
}, |
| 705 |
ModelerPartial { |
| 706 |
pos: (6.9655, 2.0065), |
| 707 |
velocity: (38.8512, -0.6909), |
| 708 |
acceleration: (-3169.9351, 31.5268), |
| 709 |
time: 1.0542, |
| 710 |
}, |
| 711 |
ModelerPartial { |
| 712 |
pos: (6.9850, 2.0062), |
| 713 |
velocity: (37.4471, -0.6750), |
| 714 |
acceleration: (-2695.7649, 30.5478), |
| 715 |
time: 1.0547, |
| 716 |
}, |
| 717 |
]; |
| 718 |
assert!( |
| 719 |
result |
| 720 |
.into_iter() |
| 721 |
.zip(expected) |
| 722 |
.fold(true, |acc, x| { acc && x.0.near(x.1) }) |
| 723 |
); |
| 724 |
} |
| 725 |
|
| 726 |
#[test] |
| 727 |
fn end_of_stroke_maxiters() { |
| 728 |
let mut model = PositionModeler { |
| 729 |
position_modeler_drag_constant: ModelerParams::suggested().position_modeler_drag_constant, |
| 730 |
position_modeler_spring_mass_constant: ModelerParams::suggested() |
| 731 |
.position_modeler_spring_mass_constant, |
| 732 |
state: ModelerPartial { |
| 733 |
pos: (8.0, -3.0), |
| 734 |
velocity: (-100.0, -150.0), |
| 735 |
acceleration: (0.0, 0.0), |
| 736 |
time: 1., |
| 737 |
}, |
| 738 |
}; |
| 739 |
|
| 740 |
let result = model.model_end_of_stroke((-9., -10.0), 0.0001, 10, 0.001); |
| 741 |
let expected = vec![ |
| 742 |
ModelerPartial { |
| 743 |
pos: (7.9896, -3.0151), |
| 744 |
velocity: (-104.2873, -150.9818), |
| 745 |
acceleration: (-42872.7266, -9818.1816), |
| 746 |
time: 1.0001, |
| 747 |
}, |
| 748 |
ModelerPartial { |
| 749 |
pos: (7.9787, -3.0303), |
| 750 |
velocity: (-108.5406, -151.9521), |
| 751 |
acceleration: (-42533.3242, -9703.0205), |
| 752 |
time: 1.0002, |
| 753 |
}, |
| 754 |
ModelerPartial { |
| 755 |
pos: (7.9674, -3.0456), |
| 756 |
velocity: (-112.7601, -152.9110), |
| 757 |
acceleration: (-42195.1211, -9588.4023), |
| 758 |
time: 1.0003, |
| 759 |
}, |
| 760 |
ModelerPartial { |
| 761 |
pos: (7.9557, -3.0610), |
| 762 |
velocity: (-116.9459, -153.8584), |
| 763 |
acceleration: (-41858.1016, -9474.3242), |
| 764 |
time: 1.0004, |
| 765 |
}, |
| 766 |
ModelerPartial { |
| 767 |
pos: (7.9436, -3.0764), |
| 768 |
velocity: (-121.0982, -154.7945), |
| 769 |
acceleration: (-41522.2734, -9360.7930), |
| 770 |
time: 1.0005, |
| 771 |
}, |
| 772 |
ModelerPartial { |
| 773 |
pos: (7.9311, -3.0920), |
| 774 |
velocity: (-125.2169, -155.7193), |
| 775 |
acceleration: (-41187.6445, -9247.7998), |
| 776 |
time: 1.0006, |
| 777 |
}, |
| 778 |
ModelerPartial { |
| 779 |
pos: (7.9182, -3.1077), |
| 780 |
velocity: (-129.3023, -156.6328), |
| 781 |
acceleration: (-40854.2109, -9135.3506), |
| 782 |
time: 1.0007, |
| 783 |
}, |
| 784 |
ModelerPartial { |
| 785 |
pos: (7.9048, -3.1234), |
| 786 |
velocity: (-133.3545, -157.5351), |
| 787 |
acceleration: (-40521.9727, -9023.4395), |
| 788 |
time: 1.0008, |
| 789 |
}, |
| 790 |
ModelerPartial { |
| 791 |
pos: (7.8911, -3.1393), |
| 792 |
velocity: (-137.3736, -158.4263), |
| 793 |
acceleration: (-40190.9414, -8912.0703), |
| 794 |
time: 1.0009, |
| 795 |
}, |
| 796 |
ModelerPartial { |
| 797 |
pos: (7.8770, -3.1552), |
| 798 |
velocity: (-141.3597, -159.3065), |
| 799 |
acceleration: (-39861.0977, -8801.2402), |
| 800 |
time: 1.0010, |
| 801 |
}, |
| 802 |
]; |
| 803 |
assert!( |
| 804 |
result |
| 805 |
.into_iter() |
| 806 |
.zip(expected) |
| 807 |
.fold(true, |acc, x| { acc && x.0.near(x.1) }) |
| 808 |
); |
| 809 |
} |