waft_physics/tolerance.rs
1//! Flutter counterpart: `physics/tolerance.dart`.
2
3use std::fmt::{self, Debug};
4
5/// Structure that specifies maximum allowable magnitudes for distances,
6/// durations, and velocity differences to be considered equal.
7#[derive(Clone, Copy)]
8pub struct Tolerance {
9 /// The magnitude of the maximum distance between two points for them to be
10 /// considered within tolerance.
11 ///
12 /// The units for the distance tolerance must be the same as the units used
13 /// for the distances that are to be compared to this tolerance.
14 pub distance: f64,
15
16 /// The magnitude of the maximum duration between two times for them to be
17 /// considered within tolerance.
18 ///
19 /// The units for the time tolerance must be the same as the units used
20 /// for the times that are to be compared to this tolerance.
21 pub time: f64,
22
23 /// The magnitude of the maximum difference between two velocities for them
24 /// to be considered within tolerance.
25 ///
26 /// The units for the velocity tolerance must be the same as the units used
27 /// for the velocities that are to be compared to this tolerance.
28 pub velocity: f64,
29}
30
31const EPSILON_DEFAULT: f64 = 1e-3;
32
33impl Tolerance {
34 /// Creates a [`Tolerance`] object. By default, the distance, time, and
35 /// velocity tolerances are all ±0.001; the constructor arguments override
36 /// this.
37 ///
38 /// The arguments should all be positive values.
39 pub const fn new(distance: f64, time: f64, velocity: f64) -> Tolerance {
40 Tolerance {
41 distance,
42 time,
43 velocity,
44 }
45 }
46
47 /// A default tolerance of 0.001 for all three values.
48 pub const DEFAULT_TOLERANCE: Tolerance = Tolerance {
49 distance: EPSILON_DEFAULT,
50 time: EPSILON_DEFAULT,
51 velocity: EPSILON_DEFAULT,
52 };
53}
54
55impl Default for Tolerance {
56 fn default() -> Tolerance {
57 Tolerance::DEFAULT_TOLERANCE
58 }
59}
60
61impl Debug for Tolerance {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 write!(
64 f,
65 "Tolerance(distance: ±{}, time: ±{}, velocity: ±{})",
66 self.distance, self.time, self.velocity
67 )
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn tolerance_control_test() {
77 let text = format!("{:?}", Tolerance::DEFAULT_TOLERANCE);
78 assert!(!text.contains('\n'));
79 assert!(!text.is_empty());
80 }
81}