csci5607/assignment-1a/src/ray.rs

28 lines
700 B
Rust
Raw Normal View History

use nalgebra::Vector3;
2023-01-31 07:15:22 +00:00
2023-01-31 07:38:03 +00:00
/// A normalized parametric Ray of the form (origin + direction * time)
///
2023-01-31 20:41:23 +00:00
/// That means at any time t: f64, the point represented by origin + direction *
/// time occurs on the ray.
2023-01-31 20:39:23 +00:00
#[derive(Debug)]
2023-01-31 07:15:22 +00:00
pub struct Ray {
pub origin: Vector3<f64>,
pub direction: Vector3<f64>,
2023-01-31 07:15:22 +00:00
}
impl Ray {
2023-01-31 20:39:23 +00:00
/// Construct a ray from endpoints
2023-02-01 23:46:54 +00:00
pub fn from_endpoints(start: Vector3<f64>, end: Vector3<f64>) -> Self {
let delta = (end - start).normalize();
2023-01-31 20:39:23 +00:00
Ray {
origin: start,
direction: delta,
}
}
2023-01-31 07:38:03 +00:00
/// Evaluate the ray at a certain point in time, yielding a point
2023-02-01 23:46:54 +00:00
pub fn eval(&self, time: f64) -> Vector3<f64> {
2023-01-31 07:15:22 +00:00
self.origin + self.direction * time
}
}