rust_pathtracer/
ray.rs

1use crate::prelude::*;
2use rhai::Engine;
3
4/// Ray
5#[derive(PartialEq, Debug, Copy, Clone)]
6pub struct Ray {
7    pub origin          : F3,
8    pub direction       : F3,
9
10    pub inv_direction   : F3,
11
12    pub sign_x          : usize,
13    pub sign_y          : usize,
14    pub sign_z          : usize,
15}
16
17impl Ray {
18    pub fn new(origin: F3, direction: F3) -> Self {
19
20        Self {
21            origin,
22            direction,
23
24            inv_direction   : F3::new(1.0 / direction.x, 1.0 / direction.y, 1.0 / direction.z),
25            sign_x          : (direction.x < 0.0) as usize,
26            sign_y          : (direction.y < 0.0) as usize,
27            sign_z          : (direction.z < 0.0) as usize
28        }
29    }
30
31    pub fn at(&self, dist: &F) -> F3 {
32        self.origin + *dist * self.direction
33    }
34
35    pub fn get_origin(&mut self) -> F3 {
36        self.origin
37    }
38
39    pub fn get_direction(&mut self) -> F3 {
40        self.direction
41    }
42
43    /// Register to the engine
44    pub fn register(engine: &mut Engine) {
45        engine.register_type_with_name::<Ray>("Ray")
46            .register_get("origin", Ray::get_origin)
47            .register_get("direction", Ray::get_direction);
48    }
49}