aboutsummaryrefslogtreecommitdiff
path: root/src/hittable/instance/translate.rs
blob: 8478bd471243b2cb8a13bbce4ccaf0a68c955e84 (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
use std::sync::Arc;

use crate::{
    hittable::{HitRecord, Hittable, AABB},
    ray::Ray,
    vec3::Vec3,
};

pub struct Translate {
    pub hittable: Arc<dyn Hittable>,
    pub offset: Vec3,
}

impl Hittable for Translate {
    fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
        let moved_ray = Ray {
            origin: &ray.origin - &self.offset,
            direction: ray.direction.clone(),
            time: ray.time,
        };
        let mut hit_record = self.hittable.hit(&moved_ray, t_min, t_max)?;
        hit_record.p += self.offset.clone();
        let normal = hit_record.normal.clone();
        hit_record.set_face_normal(&moved_ray, &normal);
        Some(hit_record)
    }

    fn bounding_box(&self, time_start: f64, time_end: f64) -> Option<AABB> {
        let output_box = self.hittable.bounding_box(time_start, time_end)?;
        Some(AABB {
            minimum: &output_box.minimum + &self.offset,
            maximum: &output_box.maximum + &self.offset,
        })
    }
}