use num_traits::Float;
use super::Approximation;
pub trait Threshold {
type Value;
type Velocity;
fn evaluate(&mut self, approximation: &Approximation<Self::Value, Self::Velocity>) -> bool;
}
pub struct And<A, B>(pub A, pub B)
where
A: Threshold,
B: Threshold<Value = A::Value>;
impl<A, B> Threshold for And<A, B>
where
A: Threshold,
B: Threshold<Value = A::Value, Velocity = A::Velocity>,
{
type Value = A::Value;
type Velocity = A::Velocity;
fn evaluate(&mut self, approximation: &Approximation<Self::Value, Self::Velocity>) -> bool {
self.0.evaluate(approximation) && self.1.evaluate(approximation)
}
}
pub struct DisplacementThreshold<T>
where
T: Float,
{
pub target: T,
pub sensitivity: T,
}
impl<T> Threshold for DisplacementThreshold<T>
where
T: Float,
{
type Value = T;
type Velocity = T;
fn evaluate(&mut self, approximation: &Approximation<Self::Value, Self::Velocity>) -> bool {
let displacement = approximation.value.sub(self.target);
displacement.abs() <= self.sensitivity
}
}
pub struct VelocityThreshold<T>(pub T);
impl<T> Threshold for VelocityThreshold<T>
where
T: Float,
{
type Value = T;
type Velocity = T;
fn evaluate(&mut self, approximation: &Approximation<Self::Value, Self::Velocity>) -> bool {
approximation.velocity.abs() <= self.0
}
}