extern crate nalgebra as na; use na::*; use na::geometry::Point3; #[derive(Clone, Copy, Debug)] pub struct Ray { pub origin: Point3, pub direction: Unit> } impl Ray { pub fn from_parts(origin: Point3, direction: Unit>) -> Self { Ray { origin: origin, direction: direction } } pub fn new(origin: Point3, direction: Vector3) -> Self { Ray::from_parts(origin, Unit::new_normalize(direction)) } pub fn from_points(a: Point3, b: Point3) -> Self { Ray::new(a, b - a) } pub fn project(&self, t: f32) -> Point3 { self.origin + t * self.direction.into_inner() } } #[derive(Clone, Copy, Debug, PartialEq)] pub struct Color { pub red: f32, pub green: f32, pub blue: f32, _private: () // Private field prevents direct construction } impl Color { pub fn new(red: f32, green: f32, blue: f32) -> Self { Color { red: clamp(red, 0.0, 1.0), green: clamp(green, 0.0, 1.0), blue: clamp(blue, 0.0, 1.0), _private: () } } pub fn to_byte_array(&self) -> [u8; 3] { let red = (255.0 * self.red) as u8; let green = (255.0 * self.green) as u8; let blue = (255.0 * self.blue) as u8; [red, green, blue] } pub fn black() -> Self { Color { red: 0.0, green: 0.0, blue: 0.0, _private: () } } pub fn white() -> Self { Color { red: 1.0, green: 1.0, blue: 1.0, _private: () } } }