shapes: name space helper implementations in a sub module.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bill Thiede 2021-07-21 13:17:25 -07:00
parent 0e8a0e4163
commit 5600d6c561

View File

@ -266,12 +266,19 @@ impl Shape {
/// ```
pub fn intersect<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
match shape.geometry {
Geometry::Sphere => intersect_sphere(shape, ray),
Geometry::Plane => intersect_plane(shape, ray),
Geometry::Sphere => sphere::intersect(shape, ray),
Geometry::Plane => plane::intersect(shape, ray),
}
}
fn intersect_sphere<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
mod sphere {
use crate::{
intersections::{Intersection, Intersections},
rays::Ray,
shapes::Shape,
tuples::{dot, Tuple},
};
pub fn intersect<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
let ray = ray.transform(shape.inverse_transform);
let sphere_to_ray = ray.origin - Tuple::point(0., 0., 0.);
let a = dot(ray.direction, ray.direction);
@ -286,7 +293,16 @@ fn intersect_sphere<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
Intersection::new((-b + discriminant.sqrt()) / (2. * a), &shape),
])
}
fn intersect_plane<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
}
mod plane {
use crate::{
intersections::{Intersection, Intersections},
rays::Ray,
shapes::Shape,
EPSILON,
};
pub fn intersect<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
if (ray.direction.y).abs() < EPSILON {
return Intersections::default();
}
@ -295,3 +311,4 @@ fn intersect_plane<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
&shape,
)])
}
}