aboutsummaryrefslogtreecommitdiff
path: root/src/core/ray.rs
blob: 19d3cf137abbd2745e419539af6d1618c63b77f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! The ray class used when probing the 3d scene
use crate::core::Vector3f;
use crate::Float;

/// A ray that is sent into the world.
/// This is the main type used for testing intersections.
pub struct Ray {
    /// Origin of the ray
    pub origin: Vector3f,
    /// Direction is assumed to be a unit vector.
    pub direction: Vector3f,
}

impl Ray {
    pub fn new(origin: Vector3f, direction: Vector3f) -> Ray {
        Ray {
            origin,
            direction,
        }
    }

    pub fn new_to(origin: Vector3f, target: Vector3f) -> Ray {
        let dir = (target - origin).norm();
        Ray {
            origin,
            direction: dir
        }
    }

    /// Resolve a point on the ray at time t
    pub fn at(&self, t: Float) -> Vector3f {
        self.origin + self.direction * t
    }
}