rustracer/src/translate.rs

35 lines
904 B
Rust

use crate::hittable::{HitRecord, Hittable};
use crate::{Aabb, Ray, Vec3};
pub struct Translate<H: Hittable> {
hittable: H,
offset: Vec3
}
impl<H: Hittable> Translate<H> {
pub fn new(hittable: H, offset: Vec3) -> Self {
Self { hittable, offset }
}
}
impl<H: Hittable> Hittable for Translate<H> {
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
let moved_ray = Ray::new(
ray.origin() - self.offset,
ray.direction(),
ray.time());
self.hittable.hit(&moved_ray, t_min, t_max).map(|mut hr| {
hr.point += self.offset;
hr
})
}
fn bounding_box(&self, time0: f64, time1: f64) -> Option<Aabb> {
self.hittable.bounding_box(time0, time1).map(|mut b| {
b.minimum += self.offset;
b.maximum += self.offset;
b
})
}
}