--- a/src/kdtree.cc Sun Nov 25 17:58:29 2007 +0100
+++ b/src/kdtree.cc Sun Nov 25 21:47:10 2007 +0100
@@ -87,6 +87,14 @@
return nearest_shape;
}
+KdNode::~KdNode()
+{
+ if (isLeaf())
+ ;//delete shapes; // this sigsegvs for unknown reason
+ else
+ delete[] children;
+}
+
void KdNode::subdivide(BBox bbox, int depth)
{
if (depth >= 20 || shapes->size() <= 4)
@@ -229,6 +237,7 @@
float rnum = splitpos->rnum;
// split this node
+ //delete shapes;
children = new KdNode[2];
int state = 0;
for (sh = sslist.begin(); sh != sslist.end(); sh++)
@@ -311,12 +320,12 @@
if (!bbox.intersect(ray, a, b))
return NULL;
- stack<StackElem*> st;
-
/* pointers to the far child node and current node */
KdNode *farchild, *node;
node = root; /* start from the kd-tree root node */
+ stack<StackElem*> st;
+
StackElem *enPt = new StackElem();
enPt->t = a; /* set the signed distance */
enPt->node = NULL;
@@ -332,12 +341,12 @@
exPt->t = b;
exPt->pb = ray.o + ray.dir * b;
exPt->node = NULL; /* set termination flag */
-
- st.push(enPt);
+ st.push(exPt);
/* loop, traverse through the whole kd-tree, until an object is intersected or ray leaves the scene */
while (node)
{
+ exPt = st.top();
/* loop until a leaf is found */
while (!node->isLeaf())
{
@@ -379,16 +388,14 @@
/* signed distance to the splitting plane */
t = (splitVal - ray.o.cell[axis]) / ray.dir.cell[axis];
- /* setup the new exit point */
- st.push(exPt);
+ /* setup the new exit point and push it onto stack */
exPt = new StackElem();
-
- /* push values onto the stack */
exPt->t = t;
exPt->node = farchild;
exPt->pb[axis] = splitVal;
exPt->pb[(axis+1)%3] = ray.o.cell[(axis+1)%3] + t * ray.dir.cell[(axis+1)%3];
exPt->pb[(axis+2)%3] = ray.o.cell[(axis+2)%3] + t * ray.dir.cell[(axis+2)%3];
+ st.push(exPt);
} /* while */
/* current node is the leaf . . . empty or full */
@@ -402,21 +409,23 @@
&& dist >= enPt->t)
nearest_shape = *shape;
+ delete enPt;
+
if (nearest_shape)
{
+ while (!st.empty())
+ {
+ delete st.top();
+ st.pop();
+ }
nearest_distance = dist;
return nearest_shape;
}
- /* pop from the stack */
- delete enPt;
- enPt = exPt; /* the signed distance intervals are adjacent */
+ enPt = exPt;
/* retrieve the pointer to the next node, it is possible that ray traversal terminates */
node = enPt->node;
-
- // pop
- exPt = st.top();
st.pop();
} /* while */