shapes: name space helper implementations in a sub module.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0e8a0e4163
commit
5600d6c561
@ -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);
|
||||
@ -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),
|
||||
])
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
@ -294,4 +310,5 @@ fn intersect_plane<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> {
|
||||
-ray.origin.y / ray.direction.y,
|
||||
&shape,
|
||||
)])
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user