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,32 +266,49 @@ 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> {
|
||||
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);
|
||||
let b = 2. * dot(ray.direction, sphere_to_ray);
|
||||
let c = dot(sphere_to_ray, sphere_to_ray) - 1.;
|
||||
let discriminant = b * b - 4. * a * c;
|
||||
if discriminant < 0. {
|
||||
return Intersections::default();
|
||||
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);
|
||||
let b = 2. * dot(ray.direction, sphere_to_ray);
|
||||
let c = dot(sphere_to_ray, sphere_to_ray) - 1.;
|
||||
let discriminant = b * b - 4. * a * c;
|
||||
if discriminant < 0. {
|
||||
return Intersections::default();
|
||||
}
|
||||
Intersections::new(vec![
|
||||
Intersection::new((-b - discriminant.sqrt()) / (2. * a), &shape),
|
||||
Intersection::new((-b + discriminant.sqrt()) / (2. * a), &shape),
|
||||
])
|
||||
}
|
||||
Intersections::new(vec![
|
||||
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> {
|
||||
if (ray.direction.y).abs() < EPSILON {
|
||||
return Intersections::default();
|
||||
|
||||
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();
|
||||
}
|
||||
Intersections::new(vec![Intersection::new(
|
||||
-ray.origin.y / ray.direction.y,
|
||||
&shape,
|
||||
)])
|
||||
}
|
||||
Intersections::new(vec![Intersection::new(
|
||||
-ray.origin.y / ray.direction.y,
|
||||
&shape,
|
||||
)])
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user