diff --git a/shapes/Box.cc b/shapes/Box.cc index 5a77263..12d0a75 100644 --- a/shapes/Box.cc +++ b/shapes/Box.cc @@ -20,6 +20,40 @@ Shape::IntersectionList Box::intersect(refptr _this, const Ray & ray) Ray ray_inv = m_inverse.transform_ray(ray); IntersectionList res; + + /*** optimization ***/ + if (ray_inv.getOrigin()[0] > m_size[0]) + { + if (ray_inv.getDirection() % Vector::X > 0) + return res; + } + else if (ray_inv.getOrigin()[0] < -m_size[0]) + { + if (ray_inv.getDirection() % Vector::X < 0) + return res; + } + if (ray_inv.getOrigin()[1] > m_size[1]) + { + if (ray_inv.getDirection() % Vector::Y > 0) + return res; + } + else if (ray_inv.getOrigin()[1] < -m_size[1]) + { + if (ray_inv.getDirection() % Vector::Y < 0) + return res; + } + if (ray_inv.getOrigin()[2] > m_size[2]) + { + if (ray_inv.getDirection() % Vector::Z > 0) + return res; + } + else if (ray_inv.getOrigin()[2] < -m_size[2]) + { + if (ray_inv.getDirection() % Vector::Z < 0) + return res; + } + /*** end optimization ***/ + /* * Ray equation: R = R0 + tRd * x = R0x + tRdx diff --git a/shapes/Intersect.cc b/shapes/Intersect.cc index 2546c1c..e0d2dc8 100644 --- a/shapes/Intersect.cc +++ b/shapes/Intersect.cc @@ -33,8 +33,12 @@ Shape::IntersectionList Intersect::intersect(refptr _this, const Ray & ra { Ray ray_inv = m_inverse.transform_ray(ray); IntersectionList res1 = m_shape1->intersect(m_shape1, ray_inv); - if (res1.size() == 0) /* optimization */ + + /*** optimization ***/ + if (res1.size() == 0) return res1; + /*** end optimization ***/ + IntersectionList res2 = m_shape2->intersect(m_shape2, ray_inv); BoolIntersectionList merged(res1, res2, ray_inv.getOrigin()); diff --git a/shapes/Sphere.cc b/shapes/Sphere.cc index 1f6925a..c8794bd 100644 --- a/shapes/Sphere.cc +++ b/shapes/Sphere.cc @@ -16,6 +16,7 @@ Shape::IntersectionList Sphere::intersect(refptr _this, const Ray & ray) Ray ray_inv = m_inverse.transform_ray(ray); IntersectionList res; + QuadraticSolver solver(1.0, 2 * ( ray_inv.getOrigin()[0] * ray_inv.getDirection()[0] + ray_inv.getOrigin()[1] * ray_inv.getDirection()[1] diff --git a/shapes/Subtract.cc b/shapes/Subtract.cc index dd7a646..5b4eed1 100644 --- a/shapes/Subtract.cc +++ b/shapes/Subtract.cc @@ -33,8 +33,12 @@ Shape::IntersectionList Subtract::intersect(refptr _this, const Ray & ray { Ray ray_inv = m_inverse.transform_ray(ray); IntersectionList res1 = m_shape1->intersect(m_shape1, ray_inv); - if (res1.size() == 0) /* optimization */ + + /*** optimization ***/ + if (res1.size() == 0) return res1; + /*** end optimization ***/ + IntersectionList res2 = m_shape2->intersect(m_shape2, ray_inv); BoolIntersectionList merged(res1, res2, ray_inv.getOrigin()); diff --git a/util/Vector.cc b/util/Vector.cc index 3c5065e..d0328ae 100644 --- a/util/Vector.cc +++ b/util/Vector.cc @@ -6,6 +6,10 @@ #include "Vector.h" +const Vector Vector::X(1, 0, 0); +const Vector Vector::Y(0, 1, 0); +const Vector Vector::Z(0, 0, 1); + Vector::Vector() { m_array[0] = 0.0; diff --git a/util/Vector.h b/util/Vector.h index 7993441..3c4f7cd 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -32,6 +32,10 @@ class Vector Vector & operator+=(const Vector & v2); Vector & operator-=(const Vector & v2); + static const Vector X; + static const Vector Y; + static const Vector Z; + protected: double m_array[3]; };