use crate::{Error, Frame, Result, Twist};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BaseState {
frame: Frame,
velocity: Twist,
acceleration: Twist,
}
impl BaseState {
pub(crate) fn stationary_unchecked(frame: Frame) -> Self {
Self {
frame,
velocity: Twist::zeros(),
acceleration: Twist::zeros(),
}
}
pub fn stationary(frame: Frame) -> Result<Self> {
validate_frame(&frame)?;
Ok(Self::stationary_unchecked(frame))
}
pub fn new(frame: Frame, velocity: Twist, acceleration: Twist) -> Result<Self> {
validate_frame(&frame)?;
validate_twist(velocity, "velocity")?;
validate_twist(acceleration, "acceleration")?;
Ok(Self {
frame,
velocity,
acceleration,
})
}
pub const fn frame(&self) -> &Frame {
&self.frame
}
pub const fn velocity(&self) -> Twist {
self.velocity
}
pub const fn acceleration(&self) -> Twist {
self.acceleration
}
}
pub(crate) fn validate_frame(frame: &Frame) -> Result<()> {
if frame
.translation
.vector
.iter()
.chain(frame.rotation.coords.iter())
.all(|value| value.is_finite())
{
Ok(())
} else {
Err(Error::InvalidBaseState {
field: "frame",
reason: "must contain only finite values",
})
}
}
fn validate_twist(value: Twist, field: &'static str) -> Result<()> {
if value
.angular
.iter()
.chain(value.linear.iter())
.all(|value| value.is_finite())
{
Ok(())
} else {
Err(Error::InvalidBaseState {
field,
reason: "must contain only finite values",
})
}
}