Optimized bounding sphere intersection test
This commit is contained in:
parent
14f1ed2a31
commit
b53ea71eb0
|
@ -18,17 +18,10 @@ pub struct Bound {
|
||||||
|
|
||||||
impl Bound {
|
impl Bound {
|
||||||
pub fn is_intersected(&self, ray: Ray) -> bool {
|
pub fn is_intersected(&self, ray: Ray) -> bool {
|
||||||
|
|
||||||
if self.bypass { return true; }
|
if self.bypass { return true; }
|
||||||
|
|
||||||
let l = ray.origin - self.center;
|
let l = ray.origin - self.center;
|
||||||
|
l.norm_squared() >= self.radius * self.radius
|
||||||
let b_2 = ray.direction.dot(&l);
|
|
||||||
let c = l.norm_squared() - self.radius * self.radius;
|
|
||||||
|
|
||||||
let discr = b_2 * b_2 * c;
|
|
||||||
|
|
||||||
discr >= 0.0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn contains(&self, point: &Point3<f32>) -> bool { distance(&self.center, point) < self.radius }
|
pub fn contains(&self, point: &Point3<f32>) -> bool { distance(&self.center, point) < self.radius }
|
||||||
|
|
|
@ -51,7 +51,7 @@ impl Surface for Sphere {
|
||||||
|
|
||||||
let l = ray.origin - self.center;
|
let l = ray.origin - self.center;
|
||||||
let b = 2.0 * ray.direction.dot(&l);
|
let b = 2.0 * ray.direction.dot(&l);
|
||||||
let c = l.normsquared() - self.radius * self.radius;
|
let c = l.norm_squared() - self.radius * self.radius;
|
||||||
|
|
||||||
let (mut t0, mut t1) = solve_quadratic(b, c)?;
|
let (mut t0, mut t1) = solve_quadratic(b, c)?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue