use crate::resolver::{error::Error, nofailure, *};
use core::ops::{Add, Div, Mul, Neg};
use num_traits::{ConstOne, Float, Zero};
#[derive(Debug, Clone, Copy)]
pub struct Trajectory<N> {
height: N,
time: N,
impulse: N,
gravity: N,
}
impl<N: Copy> Trajectory<N> {
#[inline]
pub fn height(&self) -> N {
self.height
}
#[inline]
pub fn time(&self) -> N {
self.time
}
#[inline]
pub fn impulse(&self) -> N {
self.impulse
}
#[inline]
pub fn gravity(&self) -> N {
self.gravity
}
}
impl<N> Trajectory<N>
where
N: Copy + Zero + Neg<Output = N> + Add<Output = N> + Mul<Output = N> + Div<Output = N>,
{
pub fn from_height_and_time(height: N, time: N) -> Result<Self, Error> {
let impulse = impulse_from_height_and_time(height, time)?;
let gravity = gravity_from_height_and_time(height, time)?;
Ok(Self {
height,
time,
impulse,
gravity,
})
}
}
impl<N> Trajectory<N>
where
N: Copy
+ Zero
+ ConstOne
+ Neg<Output = N>
+ Add<Output = N>
+ Mul<Output = N>
+ Div<Output = N>,
{
pub fn from_height_and_impulse(height: N, impulse: N) -> Result<Self, Error> {
let time = time_from_height_and_impulse(height, impulse)?;
let gravity = gravity_from_height_and_impulse(height, impulse)?;
Ok(Self {
height,
time,
impulse,
gravity,
})
}
}
impl<N> Trajectory<N>
where
N: Zero + Float + Div<Output = N>,
{
pub fn from_height_and_gravity(height: N, gravity: N) -> Result<Self, Error> {
let time = time_from_height_and_gravity(height, gravity)?;
let impulse = nofailure::impulse_from_height_and_gravity(height, gravity);
Ok(Self {
height,
time,
impulse,
gravity,
})
}
}
impl<N> Trajectory<N>
where
N: Copy
+ Zero
+ ConstOne
+ Neg<Output = N>
+ Add<Output = N>
+ Mul<Output = N>
+ Div<Output = N>,
{
pub fn from_time_and_impulse(time: N, impulse: N) -> Result<Self, Error> {
let height = nofailure::height_from_time_and_impulse(time, impulse);
let gravity = gravity_from_time_and_impulse(time, impulse)?;
Ok(Self {
height,
time,
impulse,
gravity,
})
}
}
impl<N> Trajectory<N>
where
N: Copy + ConstOne + Neg<Output = N> + Add<Output = N> + Mul<Output = N> + Div<Output = N>,
{
pub fn from_time_and_gravity(time: N, gravity: N) -> Self {
let height = nofailure::height_from_time_and_gravity(time, gravity);
let impulse = nofailure::impulse_from_time_and_gravity(time, gravity);
Self {
height,
time,
impulse,
gravity,
}
}
}
impl<N> Trajectory<N>
where
N: Copy
+ Zero
+ ConstOne
+ Neg<Output = N>
+ Add<Output = N>
+ Mul<Output = N>
+ Div<Output = N>,
{
pub fn from_impulse_and_gravity(impulse: N, gravity: N) -> Result<Self, Error> {
let height = height_from_impulse_and_gravity(impulse, gravity)?;
let time = time_from_impulse_and_gravity(impulse, gravity)?;
Ok(Self {
height,
time,
impulse,
gravity,
})
}
}