89 lines
2.0 KiB
Rust
89 lines
2.0 KiB
Rust
use std::sync::Arc;
|
|
use crate::{Aabb, HittableList, Material, Plane, Point3, Ray, Rect2D};
|
|
use crate::hittable::{HitRecord, Hittable};
|
|
|
|
pub struct Cuboid {
|
|
minimum: Point3,
|
|
maximum: Point3,
|
|
sides: HittableList
|
|
}
|
|
|
|
impl Cuboid {
|
|
pub fn new(p0: Point3, p1: Point3, material: Arc<Material>) -> Self {
|
|
let mut sides: HittableList = Vec::with_capacity(6);
|
|
sides.push(Arc::new(Rect2D::new(
|
|
Plane::XY,
|
|
p0.x(),
|
|
p1.x(),
|
|
p0.y(),
|
|
p1.y(),
|
|
p1.z(),
|
|
material.clone()
|
|
)));
|
|
sides.push(Arc::new(Rect2D::new(
|
|
Plane::XY,
|
|
p0.x(),
|
|
p1.x(),
|
|
p0.y(),
|
|
p1.y(),
|
|
p0.z(),
|
|
material.clone()
|
|
)));
|
|
sides.push(Arc::new(Rect2D::new(
|
|
Plane::XZ,
|
|
p0.x(),
|
|
p1.x(),
|
|
p0.z(),
|
|
p1.z(),
|
|
p1.y(),
|
|
material.clone()
|
|
)));
|
|
sides.push(Arc::new(Rect2D::new(
|
|
Plane::XZ,
|
|
p0.x(),
|
|
p1.x(),
|
|
p0.z(),
|
|
p1.z(),
|
|
p0.y(),
|
|
material.clone()
|
|
)));
|
|
sides.push(Arc::new(Rect2D::new(
|
|
Plane::YZ,
|
|
p0.y(),
|
|
p1.y(),
|
|
p0.z(),
|
|
p1.z(),
|
|
p1.x(),
|
|
material.clone()
|
|
)));
|
|
sides.push(Arc::new(Rect2D::new(
|
|
Plane::YZ,
|
|
p0.y(),
|
|
p1.y(),
|
|
p0.z(),
|
|
p1.z(),
|
|
p0.x(),
|
|
material.clone()
|
|
)));
|
|
|
|
Cuboid {
|
|
minimum: p0,
|
|
maximum: p1,
|
|
sides
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Hittable for Cuboid {
|
|
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
|
|
ray.hit_world(&self.sides, t_min, t_max)
|
|
}
|
|
|
|
fn bounding_box(&self, _time0: f64, _time1: f64) -> Option<Aabb> {
|
|
Some(Aabb {
|
|
minimum: self.minimum,
|
|
maximum: self.maximum
|
|
})
|
|
}
|
|
}
|