src/octree.cc
branchpyrit
changeset 38 5d043eeb09d9
parent 37 5f954c0d34fc
child 42 fbdeb3e04543
--- a/src/octree.cc	Fri Dec 14 00:05:54 2007 +0100
+++ b/src/octree.cc	Fri Dec 14 10:34:31 2007 +0100
@@ -55,7 +55,8 @@
 			}
 	}
 
-	if (shapes->size() <= 8 && shapenum > 2*shapes->size())
+	if ((shapes->size() <= 8 && shapenum > 2*shapes->size())
+	|| shapenum >= 6*shapes->size())
 	{
 		// bad subdivision, revert
 		delete[] children;
@@ -78,7 +79,6 @@
 	ShapeList::iterator shape;
 	for (shape = shapes.begin(); shape != shapes.end(); shape++)
 		root->addShape(*shape);
-
 	root->subdivide(bbox, max_depth);
 	built = true;
 }
@@ -184,7 +184,7 @@
 	st_cur->next = -1;
 
 	Shape *nearest_shape = NULL;
-	while (nearest_shape == NULL)
+	for (;;)
 	{
 		if (st_cur->next == -1)
 		{
@@ -196,17 +196,16 @@
 				if (node->isLeaf())
 				{
 					ShapeList::iterator shape;
-					register Float mindist = max3(tx0,ty0,tz0);
-					/* correct & slow */
-					//Float dist = min(min3(tx1,ty1,tz1),nearest_distance);
-					/* faster */
-					register Float dist = nearest_distance;
+					//register Float mindist = max3(tx0,ty0,tz0);
+					register Float dist = min(nearest_distance, min3(tx1,ty1,tz1));
 					for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++)
-						if (*shape != origin_shape && (*shape)->intersect(ray, dist) && dist >= mindist)
+						if (*shape != origin_shape && (*shape)->intersect(ray, dist))
 						{
 							nearest_shape = *shape;
 							nearest_distance = dist;
 						}
+					if (nearest_shape != NULL)
+						return nearest_shape;
 				}
 				else
 				{
@@ -327,5 +326,4 @@
 		}
 		st_cur->next = -1;
 	}
-	return nearest_shape;
 }