use crate::frame::FrameError;
use crate::{Point3, UnitVec3, Vec3};
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Axis {
pub origin: Point3,
pub direction: UnitVec3,
}
impl Axis {
pub fn new(origin: Point3, direction: Vec3) -> Result<Self, FrameError> {
if !(origin.coords.iter().all(|c| c.is_finite()) && direction.iter().all(|c| c.is_finite()))
{
return Err(FrameError::NonFinite);
}
let direction = UnitVec3::try_new(direction, 0.0).ok_or(FrameError::ZeroAxis)?;
Ok(Axis { origin, direction })
}
pub fn z_at(origin: Point3) -> Self {
Axis {
origin,
direction: Vec3::z_axis(),
}
}
pub fn at(&self, t: f64) -> Point3 {
self.origin + t * self.direction.into_inner()
}
}