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> { pub fn intersect<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
match shape.geometry { match shape.geometry {
Geometry::Sphere => intersect_sphere(shape, ray), Geometry::Sphere => sphere::intersect(shape, ray),
Geometry::Plane => intersect_plane(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 ray = ray.transform(shape.inverse_transform);
let sphere_to_ray = ray.origin - Tuple::point(0., 0., 0.); let sphere_to_ray = ray.origin - Tuple::point(0., 0., 0.);
let a = dot(ray.direction, ray.direction); let a = dot(ray.direction, ray.direction);
@ -285,8 +292,17 @@ fn intersect_sphere<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
Intersection::new((-b - discriminant.sqrt()) / (2. * a), &shape), Intersection::new((-b - discriminant.sqrt()) / (2. * a), &shape),
Intersection::new((-b + discriminant.sqrt()) / (2. * a), &shape), 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 { if (ray.direction.y).abs() < EPSILON {
return Intersections::default(); return Intersections::default();
} }
@ -294,4 +310,5 @@ fn intersect_plane<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
-ray.origin.y / ray.direction.y, -ray.origin.y / ray.direction.y,
&shape, &shape,
)]) )])
}
} }