Add HasBoundingBox trait with implementation for Sphere

This commit is contained in:
Matthew Gordon 2020-01-10 17:15:35 -05:00
parent abf71658b6
commit 75611d47d3
2 changed files with 28 additions and 3 deletions

View File

@ -60,6 +60,10 @@ pub trait IntersectP<T: RealField>: Send + Sync {
fn intersect(&self, ray: &Ray<T>) -> bool; fn intersect(&self, ray: &Ray<T>) -> bool;
} }
pub trait HasBoundingBox<T: RealField>: Send + Sync {
fn bounding_box(&self) -> BoundingBox<T>;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use quickcheck_macros::quickcheck; use quickcheck_macros::quickcheck;

View File

@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3};
use crate::materials::Material; use crate::materials::Material;
use super::{Intersect, IntersectionInfo, Ray}; use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray};
use std::sync::Arc; use std::sync::Arc;
@ -77,10 +77,17 @@ impl<T: RealField> Intersect<T> for Sphere<T> {
} }
} }
impl<T: RealField> HasBoundingBox<T> for Sphere<T> {
fn bounding_box(&self) -> BoundingBox<T> {
let radius_xyz = Vector3::new(self.radius, self.radius, self.radius);
BoundingBox::from_corners(self.centre + radius_xyz, self.centre - radius_xyz)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use quickcheck_macros::quickcheck;
use quickcheck::TestResult; use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
use super::*; use super::*;
use crate::materials::LambertianMaterial; use crate::materials::LambertianMaterial;
@ -158,4 +165,18 @@ mod tests {
(distance_to_centre - (info.distance + sphere.radius)).abs() < 0.00001, (distance_to_centre - (info.distance + sphere.radius)).abs() < 0.00001,
) )
} }
#[quickcheck]
fn all_points_on_sphere_are_in_bounding_box(
sphere_centre: Point3<f64>,
radius_vector: Vector3<f64>,
) -> bool {
let target_sphere = Sphere::new(
sphere_centre,
radius_vector.norm(),
Arc::new(LambertianMaterial::new_dummy()),
);
let bounding_box = target_sphere.bounding_box();
bounding_box.contains_point(sphere_centre + radius_vector)
}
} }