raytracers/rtiow/src/cuboid.rs

91 lines
2.4 KiB
Rust

use std::sync::Arc;
use aabb::AABB;
use flip_normals::FlipNormals;
use hitable::Hit;
use hitable::HitRecord;
use hitable_list::HitableList;
use material::Material;
use ray::Ray;
use rect::XYRect;
use rect::XZRect;
use rect::YZRect;
use vec3::Vec3;
pub struct Cuboid {
p_min: Vec3,
p_max: Vec3,
walls: HitableList,
}
impl Cuboid {
// This clippy doesn't work right with Arc.
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn new(p_min: Vec3, p_max: Vec3, material: Arc<Material>) -> Cuboid {
Cuboid {
p_min,
p_max,
walls: HitableList::new(vec![
Box::new(XYRect::new(
p_min.x,
p_max.x,
p_min.y,
p_max.y,
p_max.z,
Arc::clone(&material),
)),
Box::new(FlipNormals::new(XYRect::new(
p_min.x,
p_max.x,
p_min.y,
p_max.y,
p_min.z,
Arc::clone(&material),
))),
Box::new(XZRect::new(
p_min.x,
p_max.x,
p_min.z,
p_max.z,
p_max.y,
Arc::clone(&material),
)),
Box::new(FlipNormals::new(XZRect::new(
p_min.x,
p_max.x,
p_min.z,
p_max.z,
p_min.y,
Arc::clone(&material),
))),
Box::new(YZRect::new(
p_min.y,
p_max.y,
p_min.z,
p_max.z,
p_max.x,
Arc::clone(&material),
)),
Box::new(FlipNormals::new(YZRect::new(
p_min.y,
p_max.y,
p_min.z,
p_max.z,
p_min.x,
Arc::clone(&material),
))),
]),
}
}
}
impl Hit for Cuboid {
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
self.walls.hit(r, t_min, t_max)
}
fn bounding_box(&self, _t_min: f32, _t_max: f32) -> Option<AABB> {
Some(AABB::new(self.p_min, self.p_max))
}
}