src/scene.cc
branchpyrit
changeset 38 5d043eeb09d9
parent 36 b490093b0ac3
child 40 929aad02c5f2
--- a/src/scene.cc	Fri Dec 14 00:05:54 2007 +0100
+++ b/src/scene.cc	Fri Dec 14 10:34:31 2007 +0100
@@ -144,19 +144,31 @@
 	return false;
 }
 
+bool Sphere::intersect_bbox(const BBox &bbox) const
+{
+	register float dmin = 0;
+	for (int i = 0; i < 3; i++)
+	{
+		if (center[i] < bbox.L[i])
+			dmin += (center[i] - bbox.L[i])*(center[i] - bbox.L[i]);
+		else
+		if (center[i] > bbox.H[i])
+			dmin += (center[i] - bbox.H[i])*(center[i] - bbox.H[i]);
+	}
+	if (dmin <= sqr_radius)
+		return true;
+	return false;
+};
+
 BBox Sphere::get_bbox() const
 {
-	BBox bbox = BBox();
-	bbox.L = center - radius;
-	bbox.H = center + radius;
-	return bbox;
+	return BBox(center - radius, center + radius);
 }
 
 bool Box::intersect(const Ray &ray, Float &dist) const
 {
 	Float a,b;
-	bool res = get_bbox().intersect(ray, a, b);
-	if (res && a < dist)
+	if (get_bbox().intersect(ray, a, b) && a < dist)
 	{
 		dist = a;
 		return true;
@@ -165,6 +177,14 @@
 		return false;
 }
 
+bool Box::intersect_bbox(const BBox &bbox) const
+{
+	return (
+	H.x > bbox.L.x && L.x < bbox.H.x &&
+	H.y > bbox.L.y && L.y < bbox.H.y &&
+	H.z > bbox.L.z && L.z < bbox.H.z);
+}
+
 const Vector3 Box::normal(const Vector3 &P) const
 {
 	Vector3 N;