use crate::{
num::{DefaultFloat, Float},
vec::Vector,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ray<T, const N: usize> {
pub pos: Vector<N, T>,
pub dir: Vector<N, T>,
}
pub type Ray3<T = DefaultFloat> = Ray<T, 3>;
impl<const N: usize, T> Ray<T, N> {
pub fn new(pos: Vector<N, T>, dir: Vector<N, T>) -> Self { Ray { pos, dir } }
}
impl<const N: usize, T: Float> Ray<T, N> {
pub fn at(&self, t: T) -> Vector<N, T> { self.pos + self.dir * t }
pub fn step(&mut self, t: T) { self.pos = self.pos + (self.dir * t) }
pub fn flip(&self) -> Self { Ray::new(self.pos, -self.dir) }
pub fn set_length(&mut self, t: T) { self.dir = self.dir.norm() * t; }
}