use crate::prelude::*;

#[derive(Debug, Clone, Copy)]
pub struct Frame {
    m: Mat3,
}

impl Frame {
    pub fn from_normal(n: Dir3) -> Self {
        let n = n.normalize();
        let x = if n.x().abs() > n.y().abs() {
            Dir3::new(n.z(), 0.0, -n.x())
        } else {
            Dir3::new(0.0, n.z(), -n.y())
        }
        .normalize();
        Self {
            m: Mat3::new([x, n, Dir3::cross(x, n)]),
        }
    }

    pub fn to_frame(&self, dir: Dir3) -> Dir3 {
        self.m.transform_transposed(dir)
    }

    pub fn to_world(&self, dir: Dir3) -> Dir3 {
        self.m.transform(dir)
    }
}

#[cfg(test)]
mod test {
    use super::Frame;
    use crate::prelude::*;

    #[test]
    fn frame() {
        let f = Frame::from_normal(Dir3::new(-1.0, 0.0, 0.0));

        dbg!(f);

        let t = dbg!(f.to_world(Dir3::new(1.0, 1.0, 1.0)));

        dbg!(f.to_frame(t));

        panic!()
    }
}
