src/octree.cc
branchpyrit
changeset 43 0b8b968b42d1
parent 42 fbdeb3e04543
child 44 3763b26244f0
--- a/src/octree.cc	Sat Dec 29 13:53:33 2007 +0100
+++ b/src/octree.cc	Sun Dec 30 00:11:47 2007 +0100
@@ -9,16 +9,21 @@
 
 OctreeNode::~OctreeNode()
 {
-	if (shapes != NULL)
+	if (isLeaf())
+	{
+		leaf = leaf^1; // zero leaf bit
 		delete shapes;
+	}
 	else
 		delete[] children;
 }
 
 void OctreeNode::subdivide(BBox bbox, int maxdepth)
 {
-	// make children
-	children = new OctreeNode[8];
+	ShapeList *l_shapes = getShapes();
+
+	// prepare children (this also sets this node as non-leaf)
+	makeChildren();
 
 	// evaluate centres for axes
 	const Float xsplit = (bbox.L.x + bbox.H.x)*0.5;
@@ -42,7 +47,7 @@
 	// distribute shapes to children
 	ShapeList::iterator sh;
 	unsigned int shapenum = 0;
-	for (sh = shapes->begin(); sh != shapes->end(); sh++)
+	for (sh = l_shapes->begin(); sh != l_shapes->end(); sh++)
 	{
 		for (int i = 0; i < 8; i++)
 			if ((*sh)->intersect_bbox(childbb[i]))
@@ -52,21 +57,21 @@
 			}
 	}
 
-	if ((shapes->size() <= 8 && shapenum > 2*shapes->size())
-	|| shapenum >= 6*shapes->size())
+	if ((l_shapes->size() <= 8 && shapenum > 2*l_shapes->size())
+	|| shapenum >= 6*l_shapes->size())
 	{
 		// bad subdivision, revert
 		delete[] children;
+		setShapes(l_shapes);
 		return;
 	}
 
 	// remove shapes and set this node to non-leaf
-	delete shapes;
-	shapes = NULL;
+	delete l_shapes;
 
 	// recursive subdivision
 	for (int i = 0; i < 8; i++)
-		if (maxdepth > 1 && getChild(i)->shapes->size() > 4)
+		if (maxdepth > 1 && getChild(i)->getShapes()->size() > 4)
 			children[i].subdivide(childbb[i], maxdepth-1);
 }
 
@@ -196,7 +201,7 @@
 					ShapeList::iterator shape;
 					//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++)
+					for (shape = node->getShapes()->begin(); shape != node->getShapes()->end(); shape++)
 						if (*shape != origin_shape && (*shape)->intersect(ray, dist))
 						{
 							nearest_shape = *shape;