Add Ray constructor that ensures direction vector is normalized

This commit is contained in:
Matthew Gordon 2019-11-06 17:32:02 -05:00
parent ff046808da
commit 9ebfae5898
1 changed files with 10 additions and 1 deletions

View File

@ -1,11 +1,20 @@
use nalgebra::{RealField, Vector3}; use nalgebra::{RealField, Vector3};
#[derive(Clone,Debug)] #[derive(Clone, Debug)]
struct Ray<T: RealField> { struct Ray<T: RealField> {
origin: Vector3<T>, origin: Vector3<T>,
direction: Vector3<T>, direction: Vector3<T>,
} }
impl<T: RealField> Ray<T> {
fn new(origin: Vector3<T>, direction: Vector3<T>) -> Ray<T> {
Ray {
origin,
direction: direction.normalize(),
}
}
}
impl<T: RealField> Ray<T> { impl<T: RealField> Ray<T> {
fn point_at(&self, t: T) -> Vector3<T> { fn point_at(&self, t: T) -> Vector3<T> {
return self.origin + self.direction * t; return self.origin + self.direction * t;